LLVM 19.0.0git
LVType.cpp
Go to the documentation of this file.
1//===-- LVType.cpp --------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the LVType class.
10//
11//===----------------------------------------------------------------------===//
12
17
18using namespace llvm;
19using namespace llvm::logicalview;
20
21#define DEBUG_TYPE "Type"
22
23namespace {
24const char *const KindBaseType = "BaseType";
25const char *const KindConst = "Const";
26const char *const KindEnumerator = "Enumerator";
27const char *const KindImport = "Import";
28const char *const KindPointer = "Pointer";
29const char *const KindPointerMember = "PointerMember";
30const char *const KindReference = "Reference";
31const char *const KindRestrict = "Restrict";
32const char *const KindRvalueReference = "RvalueReference";
33const char *const KindSubrange = "Subrange";
34const char *const KindTemplateTemplate = "TemplateTemplate";
35const char *const KindTemplateType = "TemplateType";
36const char *const KindTemplateValue = "TemplateValue";
37const char *const KindTypeAlias = "TypeAlias";
38const char *const KindUndefined = "Undefined";
39const char *const KindUnaligned = "Unaligned";
40const char *const KindUnspecified = "Unspecified";
41const char *const KindVolatile = "Volatile";
42} // end anonymous namespace
43
44//===----------------------------------------------------------------------===//
45// DWARF Type.
46//===----------------------------------------------------------------------===//
47// Return a string representation for the type kind.
48const char *LVType::kind() const {
49 const char *Kind = KindUndefined;
50 if (getIsBase())
51 Kind = KindBaseType;
52 else if (getIsConst())
53 Kind = KindConst;
54 else if (getIsEnumerator())
55 Kind = KindEnumerator;
56 else if (getIsImport())
57 Kind = KindImport;
58 else if (getIsPointerMember())
59 Kind = KindPointerMember;
60 else if (getIsPointer())
61 Kind = KindPointer;
62 else if (getIsReference())
63 Kind = KindReference;
64 else if (getIsRestrict())
65 Kind = KindRestrict;
66 else if (getIsRvalueReference())
67 Kind = KindRvalueReference;
68 else if (getIsSubrange())
69 Kind = KindSubrange;
70 else if (getIsTemplateTypeParam())
71 Kind = KindTemplateType;
72 else if (getIsTemplateValueParam())
73 Kind = KindTemplateValue;
74 else if (getIsTemplateTemplateParam())
75 Kind = KindTemplateTemplate;
76 else if (getIsTypedef())
77 Kind = KindTypeAlias;
78 else if (getIsUnaligned())
79 Kind = KindUnaligned;
80 else if (getIsUnspecified())
81 Kind = KindUnspecified;
82 else if (getIsVolatile())
83 Kind = KindVolatile;
84 return Kind;
85}
86
87LVTypeDispatch LVType::Dispatch = {
88 {LVTypeKind::IsBase, &LVType::getIsBase},
89 {LVTypeKind::IsConst, &LVType::getIsConst},
90 {LVTypeKind::IsEnumerator, &LVType::getIsEnumerator},
91 {LVTypeKind::IsImport, &LVType::getIsImport},
92 {LVTypeKind::IsImportDeclaration, &LVType::getIsImportDeclaration},
93 {LVTypeKind::IsImportModule, &LVType::getIsImportModule},
94 {LVTypeKind::IsPointer, &LVType::getIsPointer},
95 {LVTypeKind::IsPointerMember, &LVType::getIsPointerMember},
96 {LVTypeKind::IsReference, &LVType::getIsReference},
97 {LVTypeKind::IsRestrict, &LVType::getIsRestrict},
98 {LVTypeKind::IsRvalueReference, &LVType::getIsRvalueReference},
99 {LVTypeKind::IsSubrange, &LVType::getIsSubrange},
100 {LVTypeKind::IsTemplateParam, &LVType::getIsTemplateParam},
101 {LVTypeKind::IsTemplateTemplateParam, &LVType::getIsTemplateTemplateParam},
102 {LVTypeKind::IsTemplateTypeParam, &LVType::getIsTemplateTypeParam},
103 {LVTypeKind::IsTemplateValueParam, &LVType::getIsTemplateValueParam},
104 {LVTypeKind::IsTypedef, &LVType::getIsTypedef},
105 {LVTypeKind::IsUnaligned, &LVType::getIsUnaligned},
106 {LVTypeKind::IsUnspecified, &LVType::getIsUnspecified},
107 {LVTypeKind::IsVolatile, &LVType::getIsVolatile}};
108
110 // Some DWARF tags are the representation of types. However, we associate
111 // some of them to scopes. The ones associated with types, do not have
112 // any reference tags, such as DW_AT_specification, DW_AT_abstract_origin,
113 // DW_AT_extension.
114
115 // Set the file/line information using the Debug Information entry.
116 setFile(/*Reference=*/nullptr);
117
118 if (LVElement *Element = getType())
119 Element->resolve();
120}
121
123 if (getIsResolvedName())
124 return;
125 setIsResolvedName();
126
127 // The templates are recorded as normal DWARF objects relationships;
128 // the template parameters are preserved to show the types used during
129 // the instantiation; however if a compare have been requested, those
130 // parameters needs to be resolved, so no conflicts are generated.
131 // The following DWARF illustrates this issue:
132 //
133 // a) Template Parameters are preserved:
134 // {Class} 'ConstArray<AtomTable>'
135 // {Inherits} -> 'ArrayBase'
136 // {TemplateType} 'taTYPE' -> 'AtomTable'
137 // {Member} 'mData' -> '* taTYPE'
138 //
139 // b) Template Parameters are resolved:
140 // {Class} 'ConstArray<AtomTable>'
141 // {Inherits} -> 'ArrayBase'
142 // {TemplateType} 'taTYPE' -> 'AtomTable'
143 // {Member} 'mData' -> '* AtomTable'
144 //
145 // In (b), the {Member} type have been resolved to use the real type.
146
148 if (BaseType && options().getAttributeArgument())
149 if (BaseType->isTemplateParam())
150 BaseType = BaseType->getType();
151
152 if (BaseType && !BaseType->getIsResolvedName())
153 BaseType->resolveName();
155
156 // In the case of unnamed types, try to generate a name for it, using
157 // the parents name and the line information. Ignore the template parameters.
158 if (!isNamed() && !getIsTemplateParam())
159 generateName();
160
162
163 // Resolve any given pattern.
165}
166
168 // The types do not have a DW_AT_specification or DW_AT_abstract_origin
169 // reference. Just return the type name.
170 return getName();
171}
172
173void LVType::markMissingParents(const LVTypes *References,
174 const LVTypes *Targets) {
175 if (!(References && Targets))
176 return;
177
178 LLVM_DEBUG({
179 dbgs() << "\n[LVType::markMissingParents]\n";
180 for (const LVType *Reference : *References)
181 dbgs() << "References: "
182 << "Kind = " << formattedKind(Reference->kind()) << ", "
183 << "Name = " << formattedName(Reference->getName()) << "\n";
184 for (const LVType *Target : *Targets)
185 dbgs() << "Targets : "
186 << "Kind = " << formattedKind(Target->kind()) << ", "
187 << "Name = " << formattedName(Target->getName()) << "\n";
188 });
189
190 for (LVType *Reference : *References) {
191 LLVM_DEBUG({
192 dbgs() << "Search Reference: Name = "
193 << formattedName(Reference->getName()) << "\n";
194 });
195 if (!Reference->findIn(Targets))
196 Reference->markBranchAsMissing();
197 }
198}
199
200LVType *LVType::findIn(const LVTypes *Targets) const {
201 if (!Targets)
202 return nullptr;
203
204 LLVM_DEBUG({
205 dbgs() << "\n[LVType::findIn]\n"
206 << "Reference: "
207 << "Level = " << getLevel() << ", "
208 << "Kind = " << formattedKind(kind()) << ", "
209 << "Name = " << formattedName(getName()) << "\n";
210 for (const LVType *Target : *Targets)
211 dbgs() << "Target : "
212 << "Level = " << Target->getLevel() << ", "
213 << "Kind = " << formattedKind(Target->kind()) << ", "
214 << "Name = " << formattedName(Target->getName()) << "\n";
215 });
216
217 for (LVType *Target : *Targets)
218 if (equals(Target))
219 return Target;
220
221 return nullptr;
222}
223
224// Check for a match on the arguments of a function.
225bool LVType::parametersMatch(const LVTypes *References,
226 const LVTypes *Targets) {
227 if (!References && !Targets)
228 return true;
229 if (References && Targets) {
230 LVTypes ReferenceTypes;
231 LVScopes ReferenceScopes;
232 getParameters(References, &ReferenceTypes, &ReferenceScopes);
233 LVTypes TargetTypes;
234 LVScopes TargetScopes;
235 getParameters(Targets, &TargetTypes, &TargetScopes);
236 if (!LVType::equals(&ReferenceTypes, &TargetTypes) ||
237 !LVScope::equals(&ReferenceScopes, &TargetScopes))
238 return false;
239 return true;
240 }
241 return false;
242}
243
244// Return the types which are parameters.
246 LVScopes *ScopesParam) {
247 if (!Types)
248 return;
249
250 // During a compare task, the template parameters are expanded to
251 // point to their real types, to avoid compare conflicts.
252 for (LVType *Type : *Types) {
253 if (!Type->getIsTemplateParam())
254 continue;
255 if (options().getAttributeArgument()) {
256 if (Type->getIsKindType())
257 TypesParam->push_back(Type->getTypeAsType());
258 else if (Type->getIsKindScope())
259 ScopesParam->push_back(Type->getTypeAsScope());
260 } else
261 TypesParam->push_back(Type);
262 }
263}
264
265bool LVType::equals(const LVType *Type) const {
266 return LVElement::equals(Type);
267}
268
269bool LVType::equals(const LVTypes *References, const LVTypes *Targets) {
270 if (!References && !Targets)
271 return true;
272 if (References && Targets && References->size() == Targets->size()) {
273 for (const LVType *Reference : *References)
274 if (!Reference->findIn(Targets))
275 return false;
276 return true;
277 }
278 return false;
279}
280
283}
284
285void LVType::print(raw_ostream &OS, bool Full) const {
286 if (getIncludeInPrint() &&
287 (getIsReference() || getReader().doPrintType(this))) {
291 }
292}
293
295 OS << formattedKind(kind()) << " " << formattedName(getName()) << "\n";
296}
297
298//===----------------------------------------------------------------------===//
299// DWARF typedef.
300//===----------------------------------------------------------------------===//
301// Return the underlying type for a typedef, which can be a type or scope.
304 if (BaseType)
305 // Underlying type is a scope.
306 return BaseType;
307
309 assert(Type && "Type definition does not have a type.");
310
311 BaseType = Type;
312 while (Type->getIsTypedef()) {
313 BaseType = Type->getTypeAsScope();
314 if (BaseType)
315 // Underlying type is a scope.
316 return BaseType;
317
318 Type = Type->getTypeAsType();
319 if (Type)
320 BaseType = Type;
321 }
322
323 return BaseType;
324}
325
327 // In the case of CodeView, the MSVC toolset generates a series of typedefs
328 // that refer to internal runtime structures, that we do not process. Those
329 // typedefs are marked as 'system'. They have an associated logical type,
330 // but the underlying type always is null.
331 if (getIsSystem())
332 return;
333
334 // Set the reference to the typedef type.
335 if (options().getAttributeUnderlying()) {
337 setIsTypedefReduced();
338 if (LVElement *Type = getType()) {
339 Type->resolveName();
341 }
342 }
343
344 // For the case of typedef'd anonymous structures:
345 // typedef struct { ... } Name;
346 // Propagate the typedef name to the anonymous structure.
347 LVScope *Aggregate = getTypeAsScope();
348 if (Aggregate && Aggregate->getIsAnonymous())
349 Aggregate->setName(getName());
350}
351
353 return LVType::equals(Type);
354}
355
357 OS << formattedKind(kind()) << " " << formattedName(getName()) << " -> "
359 << formattedName((getType() ? getType()->getName() : "")) << "\n";
360}
361
362//===----------------------------------------------------------------------===//
363// DWARF enumerator (DW_TAG_enumerator).
364//===----------------------------------------------------------------------===//
366 return LVType::equals(Type);
367}
368
370 OS << formattedKind(kind()) << " '" << getName()
371 << "' = " << formattedName(getValue()) << "\n";
372}
373
374//===----------------------------------------------------------------------===//
375// DWARF import (DW_TAG_imported_module / DW_TAG_imported_declaration).
376//===----------------------------------------------------------------------===//
377bool LVTypeImport::equals(const LVType *Type) const {
378 return LVType::equals(Type);
379}
380
382 std::string Attributes =
384
386 << formattedName((getType() ? getType()->getName() : "")) << "\n";
387}
388
389//===----------------------------------------------------------------------===//
390// DWARF Template parameter holder (type or param).
391//===----------------------------------------------------------------------===//
393 options().getAttributeTypename() ? setIncludeInPrint()
394 : resetIncludeInPrint();
395}
396
397// Encode the specific template argument.
399 // The incoming type is a template parameter; we have 3 kinds of parameters:
400 // - type parameter: resolve the instance (type);
401 // - value parameter: resolve the constant value
402 // - template parameter: resolve the name of the template.
403 // If the parameter type is a template instance (STL sample), we need to
404 // expand the type (template template case). For the following variable
405 // declarations:
406 // std::type<float> a_float;
407 // std::type<int> a_int;
408 // We must generate names like:
409 // "std::type<float,std::less<float>,std::allocator<float>,false>"
410 // "std::type<int,std::less<int>,std::allocator<int>,false>"
411 // Instead of the incomplete names:
412 // "type<float,less,allocator,false>"
413 // "type<int,less,allocator,false>"
414
415 if (getIsTemplateTypeParam()) {
416 // Get the type instance recorded in the template type; it can be a
417 // reference to a type or to a scope.
418
419 if (getIsKindType()) {
420 // The argument types always are qualified.
421 Name.append(std::string(getTypeQualifiedName()));
422
423 LVType *ArgType = getTypeAsType();
424 // For template arguments that are typedefs, use the underlying type,
425 // which can be a type or scope.
426 if (ArgType->getIsTypedef()) {
427 LVObject *BaseType = ArgType->getUnderlyingType();
428 Name.append(std::string(BaseType->getName()));
429 } else {
430 Name.append(std::string(ArgType->getName()));
431 }
432 } else {
433 if (getIsKindScope()) {
434 LVScope *ArgScope = getTypeAsScope();
435 // If the scope is a template, we have to resolve that template,
436 // by recursively traversing its arguments.
437 if (ArgScope->getIsTemplate())
438 ArgScope->encodeTemplateArguments(Name);
439 else {
440 // The argument types always are qualified.
441 Name.append(std::string(getTypeQualifiedName()));
442 Name.append(std::string(ArgScope->getName()));
443 }
444 }
445 }
446 } else
447 // Template value parameter or template template parameter.
448 Name.append(getValue());
449}
450
451bool LVTypeParam::equals(const LVType *Type) const {
452 if (!LVType::equals(Type))
453 return false;
454
455 // Checks the kind of template argument.
456 if (getIsTemplateTypeParam() && Type->getIsTemplateTypeParam())
457 return getType()->equals(Type->getType());
458
459 if ((getIsTemplateValueParam() && Type->getIsTemplateValueParam()) ||
460 (getIsTemplateTemplateParam() && Type->getIsTemplateTemplateParam()))
461 return getValueIndex() == Type->getValueIndex();
462
463 return false;
464}
465
467 OS << formattedKind(kind()) << " " << formattedName(getName()) << " -> "
469
470 // Depending on the type of parameter, the print includes different
471 // information: type, value or reference to a template.
472 if (getIsTemplateTypeParam()) {
474 return;
475 }
476 if (getIsTemplateValueParam()) {
477 OS << formattedName(getValue()) << " " << formattedName(getName()) << "\n";
478 return;
479 }
480 if (getIsTemplateTemplateParam())
481 OS << formattedName(getValue()) << "\n";
482}
483
484//===----------------------------------------------------------------------===//
485// DW_TAG_subrange_type
486//===----------------------------------------------------------------------===//
488 // There are 2 cases to represent the bounds information for an array:
489 // 1) DW_TAG_subrange_type
490 // DW_AT_type --> ref_type (type of count)
491 // DW_AT_count --> value (number of elements in subrange)
492
493 // 2) DW_TAG_subrange_type
494 // DW_AT_lower_bound --> value
495 // DW_AT_upper_bound --> value
496
497 // The idea is to represent the bounds as a string, depending on the format:
498 // 1) [count]
499 // 2) [lower..upper]
500
501 // Subrange information.
502 std::string String;
503
504 // Check if we have DW_AT_count subrange style.
505 if (getIsSubrangeCount())
506 // Get count subrange value. Assume 0 if missing.
507 raw_string_ostream(String) << "[" << getCount() << "]";
508 else
510 << "[" << getLowerBound() << ".." << getUpperBound() << "]";
511
513}
514
516 if (!LVType::equals(Type))
517 return false;
518
519 return getTypeName() == Type->getTypeName() && getName() == Type->getName();
520}
521
523 OS << formattedKind(kind()) << " -> " << typeOffsetAsString()
524 << formattedName(getTypeName()) << " " << formattedName(getName()) << "\n";
525}
AMDGPU Kernel Attributes
#define LLVM_DEBUG(X)
Definition: Debug.h:101
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:426
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Target - Wrapper for Target specific information.
const char * getName() const
getName - Get the target name.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
void printItem(LVElement *Element, LVComparePass Pass)
Definition: LVCompare.cpp:361
void resolveFullname(LVElement *BaseType, StringRef Name=emptyString())
Definition: LVElement.cpp:274
StringRef virtualityString(uint32_t Virtuality=dwarf::DW_VIRTUALITY_none) const
Definition: LVElement.cpp:171
void setName(StringRef ElementName) override
Definition: LVElement.cpp:96
StringRef getName() const override
Definition: LVElement.h:184
LVType * getTypeAsType() const
Definition: LVElement.cpp:30
LVElement * getType() const
Definition: LVElement.h:297
void setFile(LVElement *Reference=nullptr)
Definition: LVElement.cpp:374
StringRef getTypeName() const
Definition: LVElement.cpp:74
StringRef getTypeQualifiedName() const
Definition: LVElement.h:312
StringRef accessibilityString(uint32_t Access=dwarf::DW_ACCESS_private) const
Definition: LVElement.cpp:124
bool equals(const LVElement *Element) const
Definition: LVElement.cpp:475
std::string typeOffsetAsString() const
Definition: LVElement.cpp:116
bool isNamed() const override
Definition: LVElement.h:174
LVScope * getTypeAsScope() const
Definition: LVElement.cpp:36
virtual void print(raw_ostream &OS, bool Full=true) const
Definition: LVObject.cpp:160
LVLevel getLevel() const
Definition: LVObject.h:242
void resolvePatternMatch(LVLine *Line)
Definition: LVOptions.h:606
void encodeTemplateArguments(std::string &Name) const
Definition: LVScope.cpp:507
virtual bool equals(const LVScope *Scope) const
Definition: LVScope.cpp:942
void setUnderlyingType(LVElement *Element) override
Definition: LVType.h:160
bool equals(const LVType *Type) const override
Definition: LVType.cpp:352
void printExtra(raw_ostream &OS, bool Full=true) const override
Definition: LVType.cpp:356
LVElement * getUnderlyingType() override
Definition: LVType.cpp:302
void printExtra(raw_ostream &OS, bool Full=true) const override
Definition: LVType.cpp:369
StringRef getValue() const override
Definition: LVType.h:185
bool equals(const LVType *Type) const override
Definition: LVType.cpp:365
void printExtra(raw_ostream &OS, bool Full=true) const override
Definition: LVType.cpp:381
bool equals(const LVType *Type) const override
Definition: LVType.cpp:377
size_t getValueIndex() const override
Definition: LVType.h:231
void printExtra(raw_ostream &OS, bool Full=true) const override
Definition: LVType.cpp:466
void encodeTemplateArgument(std::string &Name) const override
Definition: LVType.cpp:398
bool equals(const LVType *Type) const override
Definition: LVType.cpp:451
StringRef getValue() const override
Definition: LVType.h:225
int64_t getUpperBound() const override
Definition: LVType.h:268
void printExtra(raw_ostream &OS, bool Full=true) const override
Definition: LVType.cpp:522
int64_t getCount() const override
Definition: LVType.h:257
bool equals(const LVType *Type) const override
Definition: LVType.cpp:515
int64_t getLowerBound() const override
Definition: LVType.h:265
void resolveReferences() override
Definition: LVType.cpp:109
virtual bool equals(const LVType *Type) const
Definition: LVType.cpp:265
void report(LVComparePass Pass) override
Definition: LVType.cpp:281
void printExtra(raw_ostream &OS, bool Full=true) const override
Definition: LVType.cpp:294
static bool parametersMatch(const LVTypes *References, const LVTypes *Targets)
Definition: LVType.cpp:225
StringRef resolveReferencesChain()
Definition: LVType.cpp:167
void resolveName() override
Definition: LVType.cpp:122
const char * kind() const override
Definition: LVType.cpp:48
static void getParameters(const LVTypes *Types, LVTypes *TypesParam, LVScopes *ScopesParam)
Definition: LVType.cpp:245
virtual LVElement * getUnderlyingType()
Definition: LVType.h:109
void print(raw_ostream &OS, bool Full=true) const override
Definition: LVType.cpp:285
static void markMissingParents(const LVTypes *References, const LVTypes *Targets)
Definition: LVType.cpp:173
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:660
LVReader & getReader()
Definition: LVReader.h:333
std::string formattedNames(StringRef Name1, StringRef Name2)
Definition: LVSupport.h:224
LVPatterns & patterns()
Definition: LVOptions.h:642
std::string formattedKind(StringRef Kind)
Definition: LVSupport.h:216
LVScopeCompileUnit * getReaderCompileUnit()
Definition: LVReader.h:337
std::string formattedName(StringRef Name)
Definition: LVSupport.h:220
LVOptions & options()
Definition: LVOptions.h:445
std::string formatAttributes(const StringRef First, Args... Others)
Definition: LVSupport.h:123
LVCompare & getComparator()
Definition: LVCompare.h:84
std::map< LVTypeKind, LVTypeGetFunction > LVTypeDispatch
Definition: LVType.h:47
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163