[ARM] Add support for MVE pre and post inc loads and stores
[llvm-core.git] / tools / dsymutil / DeclContext.cpp
blob1c33b672c26ba6e84da5df2284658eb15c092590
1 //===- tools/dsymutil/DeclContext.cpp - Declaration context ---------------===//
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 //===----------------------------------------------------------------------===//
9 #include "DeclContext.h"
10 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
11 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
12 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
14 namespace llvm {
15 namespace dsymutil {
17 /// Set the last DIE/CU a context was seen in and, possibly invalidate the
18 /// context if it is ambiguous.
19 ///
20 /// In the current implementation, we don't handle overloaded functions well,
21 /// because the argument types are not taken into account when computing the
22 /// DeclContext tree.
23 ///
24 /// Some of this is mitigated byt using mangled names that do contain the
25 /// arguments types, but sometimes (e.g. with function templates) we don't have
26 /// that. In that case, just do not unique anything that refers to the contexts
27 /// we are not able to distinguish.
28 ///
29 /// If a context that is not a namespace appears twice in the same CU, we know
30 /// it is ambiguous. Make it invalid.
31 bool DeclContext::setLastSeenDIE(CompileUnit &U, const DWARFDie &Die) {
32 if (LastSeenCompileUnitID == U.getUniqueID()) {
33 DWARFUnit &OrigUnit = U.getOrigUnit();
34 uint32_t FirstIdx = OrigUnit.getDIEIndex(LastSeenDIE);
35 U.getInfo(FirstIdx).Ctxt = nullptr;
36 return false;
39 LastSeenCompileUnitID = U.getUniqueID();
40 LastSeenDIE = Die;
41 return true;
44 PointerIntPair<DeclContext *, 1> DeclContextTree::getChildDeclContext(
45 DeclContext &Context, const DWARFDie &DIE, CompileUnit &U,
46 UniquingStringPool &StringPool, bool InClangModule) {
47 unsigned Tag = DIE.getTag();
49 // FIXME: dsymutil-classic compat: We should bail out here if we
50 // have a specification or an abstract_origin. We will get the
51 // parent context wrong here.
53 switch (Tag) {
54 default:
55 // By default stop gathering child contexts.
56 return PointerIntPair<DeclContext *, 1>(nullptr);
57 case dwarf::DW_TAG_module:
58 break;
59 case dwarf::DW_TAG_compile_unit:
60 return PointerIntPair<DeclContext *, 1>(&Context);
61 case dwarf::DW_TAG_subprogram:
62 // Do not unique anything inside CU local functions.
63 if ((Context.getTag() == dwarf::DW_TAG_namespace ||
64 Context.getTag() == dwarf::DW_TAG_compile_unit) &&
65 !dwarf::toUnsigned(DIE.find(dwarf::DW_AT_external), 0))
66 return PointerIntPair<DeclContext *, 1>(nullptr);
67 LLVM_FALLTHROUGH;
68 case dwarf::DW_TAG_member:
69 case dwarf::DW_TAG_namespace:
70 case dwarf::DW_TAG_structure_type:
71 case dwarf::DW_TAG_class_type:
72 case dwarf::DW_TAG_union_type:
73 case dwarf::DW_TAG_enumeration_type:
74 case dwarf::DW_TAG_typedef:
75 // Artificial things might be ambiguous, because they might be created on
76 // demand. For example implicitly defined constructors are ambiguous
77 // because of the way we identify contexts, and they won't be generated
78 // every time everywhere.
79 if (dwarf::toUnsigned(DIE.find(dwarf::DW_AT_artificial), 0))
80 return PointerIntPair<DeclContext *, 1>(nullptr);
81 break;
84 const char *Name = DIE.getName(DINameKind::LinkageName);
85 const char *ShortName = DIE.getName(DINameKind::ShortName);
86 StringRef NameRef;
87 StringRef ShortNameRef;
88 StringRef FileRef;
90 if (Name)
91 NameRef = StringPool.internString(Name);
92 else if (Tag == dwarf::DW_TAG_namespace)
93 // FIXME: For dsymutil-classic compatibility. I think uniquing within
94 // anonymous namespaces is wrong. There is no ODR guarantee there.
95 NameRef = StringPool.internString("(anonymous namespace)");
97 if (ShortName && ShortName != Name)
98 ShortNameRef = StringPool.internString(ShortName);
99 else
100 ShortNameRef = NameRef;
102 if (Tag != dwarf::DW_TAG_class_type && Tag != dwarf::DW_TAG_structure_type &&
103 Tag != dwarf::DW_TAG_union_type &&
104 Tag != dwarf::DW_TAG_enumeration_type && NameRef.empty())
105 return PointerIntPair<DeclContext *, 1>(nullptr);
107 unsigned Line = 0;
108 unsigned ByteSize = std::numeric_limits<uint32_t>::max();
110 if (!InClangModule) {
111 // Gather some discriminating data about the DeclContext we will be
112 // creating: File, line number and byte size. This shouldn't be necessary,
113 // because the ODR is just about names, but given that we do some
114 // approximations with overloaded functions and anonymous namespaces, use
115 // these additional data points to make the process safer.
117 // This is disabled for clang modules, because forward declarations of
118 // module-defined types do not have a file and line.
119 ByteSize = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_byte_size),
120 std::numeric_limits<uint64_t>::max());
121 if (Tag != dwarf::DW_TAG_namespace || !Name) {
122 if (unsigned FileNum =
123 dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_file), 0)) {
124 if (const auto *LT = U.getOrigUnit().getContext().getLineTableForUnit(
125 &U.getOrigUnit())) {
126 // FIXME: dsymutil-classic compatibility. I'd rather not
127 // unique anything in anonymous namespaces, but if we do, then
128 // verify that the file and line correspond.
129 if (!Name && Tag == dwarf::DW_TAG_namespace)
130 FileNum = 1;
132 if (LT->hasFileAtIndex(FileNum)) {
133 Line = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_line), 0);
134 // Cache the resolved paths based on the index in the line table,
135 // because calling realpath is expansive.
136 StringRef ResolvedPath = U.getResolvedPath(FileNum);
137 if (!ResolvedPath.empty()) {
138 FileRef = ResolvedPath;
139 } else {
140 std::string File;
141 bool FoundFileName = LT->getFileNameByIndex(
142 FileNum, U.getOrigUnit().getCompilationDir(),
143 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
144 File);
145 (void)FoundFileName;
146 assert(FoundFileName && "Must get file name from line table");
147 // Second level of caching, this time based on the file's parent
148 // path.
149 FileRef = PathResolver.resolve(File, StringPool);
150 U.setResolvedPath(FileNum, FileRef);
158 if (!Line && NameRef.empty())
159 return PointerIntPair<DeclContext *, 1>(nullptr);
161 // We hash NameRef, which is the mangled name, in order to get most
162 // overloaded functions resolve correctly.
164 // Strictly speaking, hashing the Tag is only necessary for a
165 // DW_TAG_module, to prevent uniquing of a module and a namespace
166 // with the same name.
168 // FIXME: dsymutil-classic won't unique the same type presented
169 // once as a struct and once as a class. Using the Tag in the fully
170 // qualified name hash to get the same effect.
171 unsigned Hash = hash_combine(Context.getQualifiedNameHash(), Tag, NameRef);
173 // FIXME: dsymutil-classic compatibility: when we don't have a name,
174 // use the filename.
175 if (Tag == dwarf::DW_TAG_namespace && NameRef == "(anonymous namespace)")
176 Hash = hash_combine(Hash, FileRef);
178 // Now look if this context already exists.
179 DeclContext Key(Hash, Line, ByteSize, Tag, NameRef, FileRef, Context);
180 auto ContextIter = Contexts.find(&Key);
182 if (ContextIter == Contexts.end()) {
183 // The context wasn't found.
184 bool Inserted;
185 DeclContext *NewContext =
186 new (Allocator) DeclContext(Hash, Line, ByteSize, Tag, NameRef, FileRef,
187 Context, DIE, U.getUniqueID());
188 std::tie(ContextIter, Inserted) = Contexts.insert(NewContext);
189 assert(Inserted && "Failed to insert DeclContext");
190 (void)Inserted;
191 } else if (Tag != dwarf::DW_TAG_namespace &&
192 !(*ContextIter)->setLastSeenDIE(U, DIE)) {
193 // The context was found, but it is ambiguous with another context
194 // in the same file. Mark it invalid.
195 return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1);
198 assert(ContextIter != Contexts.end());
199 // FIXME: dsymutil-classic compatibility. Union types aren't
200 // uniques, but their children might be.
201 if ((Tag == dwarf::DW_TAG_subprogram &&
202 Context.getTag() != dwarf::DW_TAG_structure_type &&
203 Context.getTag() != dwarf::DW_TAG_class_type) ||
204 (Tag == dwarf::DW_TAG_union_type))
205 return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1);
207 return PointerIntPair<DeclContext *, 1>(*ContextIter);
209 } // namespace dsymutil
210 } // namespace llvm