File: | clang/lib/AST/ASTImporter.cpp |
Warning: | line 2790, column 8 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===// | ||||||||||
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 file defines the ASTImporter class which imports AST nodes from one | ||||||||||
10 | // context into another context. | ||||||||||
11 | // | ||||||||||
12 | //===----------------------------------------------------------------------===// | ||||||||||
13 | |||||||||||
14 | #include "clang/AST/ASTImporter.h" | ||||||||||
15 | #include "clang/AST/ASTImporterSharedState.h" | ||||||||||
16 | #include "clang/AST/ASTContext.h" | ||||||||||
17 | #include "clang/AST/ASTDiagnostic.h" | ||||||||||
18 | #include "clang/AST/ASTStructuralEquivalence.h" | ||||||||||
19 | #include "clang/AST/Attr.h" | ||||||||||
20 | #include "clang/AST/Decl.h" | ||||||||||
21 | #include "clang/AST/DeclAccessPair.h" | ||||||||||
22 | #include "clang/AST/DeclBase.h" | ||||||||||
23 | #include "clang/AST/DeclCXX.h" | ||||||||||
24 | #include "clang/AST/DeclFriend.h" | ||||||||||
25 | #include "clang/AST/DeclGroup.h" | ||||||||||
26 | #include "clang/AST/DeclObjC.h" | ||||||||||
27 | #include "clang/AST/DeclTemplate.h" | ||||||||||
28 | #include "clang/AST/DeclVisitor.h" | ||||||||||
29 | #include "clang/AST/DeclarationName.h" | ||||||||||
30 | #include "clang/AST/Expr.h" | ||||||||||
31 | #include "clang/AST/ExprCXX.h" | ||||||||||
32 | #include "clang/AST/ExprObjC.h" | ||||||||||
33 | #include "clang/AST/ExternalASTSource.h" | ||||||||||
34 | #include "clang/AST/LambdaCapture.h" | ||||||||||
35 | #include "clang/AST/NestedNameSpecifier.h" | ||||||||||
36 | #include "clang/AST/OperationKinds.h" | ||||||||||
37 | #include "clang/AST/Stmt.h" | ||||||||||
38 | #include "clang/AST/StmtCXX.h" | ||||||||||
39 | #include "clang/AST/StmtObjC.h" | ||||||||||
40 | #include "clang/AST/StmtVisitor.h" | ||||||||||
41 | #include "clang/AST/TemplateBase.h" | ||||||||||
42 | #include "clang/AST/TemplateName.h" | ||||||||||
43 | #include "clang/AST/Type.h" | ||||||||||
44 | #include "clang/AST/TypeLoc.h" | ||||||||||
45 | #include "clang/AST/TypeVisitor.h" | ||||||||||
46 | #include "clang/AST/UnresolvedSet.h" | ||||||||||
47 | #include "clang/Basic/Builtins.h" | ||||||||||
48 | #include "clang/Basic/ExceptionSpecificationType.h" | ||||||||||
49 | #include "clang/Basic/FileManager.h" | ||||||||||
50 | #include "clang/Basic/IdentifierTable.h" | ||||||||||
51 | #include "clang/Basic/LLVM.h" | ||||||||||
52 | #include "clang/Basic/LangOptions.h" | ||||||||||
53 | #include "clang/Basic/SourceLocation.h" | ||||||||||
54 | #include "clang/Basic/SourceManager.h" | ||||||||||
55 | #include "clang/Basic/Specifiers.h" | ||||||||||
56 | #include "llvm/ADT/APSInt.h" | ||||||||||
57 | #include "llvm/ADT/ArrayRef.h" | ||||||||||
58 | #include "llvm/ADT/DenseMap.h" | ||||||||||
59 | #include "llvm/ADT/None.h" | ||||||||||
60 | #include "llvm/ADT/Optional.h" | ||||||||||
61 | #include "llvm/ADT/ScopeExit.h" | ||||||||||
62 | #include "llvm/ADT/STLExtras.h" | ||||||||||
63 | #include "llvm/ADT/SmallVector.h" | ||||||||||
64 | #include "llvm/Support/Casting.h" | ||||||||||
65 | #include "llvm/Support/ErrorHandling.h" | ||||||||||
66 | #include "llvm/Support/MemoryBuffer.h" | ||||||||||
67 | #include <algorithm> | ||||||||||
68 | #include <cassert> | ||||||||||
69 | #include <cstddef> | ||||||||||
70 | #include <memory> | ||||||||||
71 | #include <type_traits> | ||||||||||
72 | #include <utility> | ||||||||||
73 | |||||||||||
74 | namespace clang { | ||||||||||
75 | |||||||||||
76 | using llvm::make_error; | ||||||||||
77 | using llvm::Error; | ||||||||||
78 | using llvm::Expected; | ||||||||||
79 | using ExpectedType = llvm::Expected<QualType>; | ||||||||||
80 | using ExpectedStmt = llvm::Expected<Stmt *>; | ||||||||||
81 | using ExpectedExpr = llvm::Expected<Expr *>; | ||||||||||
82 | using ExpectedDecl = llvm::Expected<Decl *>; | ||||||||||
83 | using ExpectedSLoc = llvm::Expected<SourceLocation>; | ||||||||||
84 | using ExpectedName = llvm::Expected<DeclarationName>; | ||||||||||
85 | |||||||||||
86 | std::string ImportError::toString() const { | ||||||||||
87 | // FIXME: Improve error texts. | ||||||||||
88 | switch (Error) { | ||||||||||
89 | case NameConflict: | ||||||||||
90 | return "NameConflict"; | ||||||||||
91 | case UnsupportedConstruct: | ||||||||||
92 | return "UnsupportedConstruct"; | ||||||||||
93 | case Unknown: | ||||||||||
94 | return "Unknown error"; | ||||||||||
95 | } | ||||||||||
96 | llvm_unreachable("Invalid error code.")::llvm::llvm_unreachable_internal("Invalid error code.", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 96); | ||||||||||
97 | return "Invalid error code."; | ||||||||||
98 | } | ||||||||||
99 | |||||||||||
100 | void ImportError::log(raw_ostream &OS) const { | ||||||||||
101 | OS << toString(); | ||||||||||
102 | } | ||||||||||
103 | |||||||||||
104 | std::error_code ImportError::convertToErrorCode() const { | ||||||||||
105 | llvm_unreachable("Function not implemented.")::llvm::llvm_unreachable_internal("Function not implemented." , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 105); | ||||||||||
106 | } | ||||||||||
107 | |||||||||||
108 | char ImportError::ID; | ||||||||||
109 | |||||||||||
110 | template <class T> | ||||||||||
111 | SmallVector<Decl *, 2> | ||||||||||
112 | getCanonicalForwardRedeclChain(Redeclarable<T>* D) { | ||||||||||
113 | SmallVector<Decl *, 2> Redecls; | ||||||||||
114 | for (auto *R : D->getFirstDecl()->redecls()) { | ||||||||||
115 | if (R != D->getFirstDecl()) | ||||||||||
116 | Redecls.push_back(R); | ||||||||||
117 | } | ||||||||||
118 | Redecls.push_back(D->getFirstDecl()); | ||||||||||
119 | std::reverse(Redecls.begin(), Redecls.end()); | ||||||||||
120 | return Redecls; | ||||||||||
121 | } | ||||||||||
122 | |||||||||||
123 | SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) { | ||||||||||
124 | if (auto *FD = dyn_cast<FunctionDecl>(D)) | ||||||||||
125 | return getCanonicalForwardRedeclChain<FunctionDecl>(FD); | ||||||||||
126 | if (auto *VD = dyn_cast<VarDecl>(D)) | ||||||||||
127 | return getCanonicalForwardRedeclChain<VarDecl>(VD); | ||||||||||
128 | if (auto *TD = dyn_cast<TagDecl>(D)) | ||||||||||
129 | return getCanonicalForwardRedeclChain<TagDecl>(TD); | ||||||||||
130 | llvm_unreachable("Bad declaration kind")::llvm::llvm_unreachable_internal("Bad declaration kind", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 130); | ||||||||||
131 | } | ||||||||||
132 | |||||||||||
133 | void updateFlags(const Decl *From, Decl *To) { | ||||||||||
134 | // Check if some flags or attrs are new in 'From' and copy into 'To'. | ||||||||||
135 | // FIXME: Other flags or attrs? | ||||||||||
136 | if (From->isUsed(false) && !To->isUsed(false)) | ||||||||||
137 | To->setIsUsed(); | ||||||||||
138 | } | ||||||||||
139 | |||||||||||
140 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>, | ||||||||||
141 | public DeclVisitor<ASTNodeImporter, ExpectedDecl>, | ||||||||||
142 | public StmtVisitor<ASTNodeImporter, ExpectedStmt> { | ||||||||||
143 | ASTImporter &Importer; | ||||||||||
144 | |||||||||||
145 | // Use this instead of Importer.importInto . | ||||||||||
146 | template <typename ImportT> | ||||||||||
147 | LLVM_NODISCARD[[clang::warn_unused_result]] Error importInto(ImportT &To, const ImportT &From) { | ||||||||||
148 | return Importer.importInto(To, From); | ||||||||||
149 | } | ||||||||||
150 | |||||||||||
151 | // Use this to import pointers of specific type. | ||||||||||
152 | template <typename ImportT> | ||||||||||
153 | LLVM_NODISCARD[[clang::warn_unused_result]] Error importInto(ImportT *&To, ImportT *From) { | ||||||||||
154 | auto ToOrErr = Importer.Import(From); | ||||||||||
155 | if (ToOrErr) | ||||||||||
156 | To = cast_or_null<ImportT>(*ToOrErr); | ||||||||||
157 | return ToOrErr.takeError(); | ||||||||||
158 | } | ||||||||||
159 | |||||||||||
160 | // Call the import function of ASTImporter for a baseclass of type `T` and | ||||||||||
161 | // cast the return value to `T`. | ||||||||||
162 | template <typename T> | ||||||||||
163 | Expected<T *> import(T *From) { | ||||||||||
164 | auto ToOrErr = Importer.Import(From); | ||||||||||
165 | if (!ToOrErr) | ||||||||||
166 | return ToOrErr.takeError(); | ||||||||||
167 | return cast_or_null<T>(*ToOrErr); | ||||||||||
168 | } | ||||||||||
169 | |||||||||||
170 | template <typename T> | ||||||||||
171 | Expected<T *> import(const T *From) { | ||||||||||
172 | return import(const_cast<T *>(From)); | ||||||||||
173 | } | ||||||||||
174 | |||||||||||
175 | // Call the import function of ASTImporter for type `T`. | ||||||||||
176 | template <typename T> | ||||||||||
177 | Expected<T> import(const T &From) { | ||||||||||
178 | return Importer.Import(From); | ||||||||||
179 | } | ||||||||||
180 | |||||||||||
181 | // Import an Optional<T> by importing the contained T, if any. | ||||||||||
182 | template<typename T> | ||||||||||
183 | Expected<Optional<T>> import(Optional<T> From) { | ||||||||||
184 | if (!From) | ||||||||||
185 | return Optional<T>(); | ||||||||||
186 | return import(*From); | ||||||||||
187 | } | ||||||||||
188 | |||||||||||
189 | // Helper for chaining together multiple imports. If an error is detected, | ||||||||||
190 | // subsequent imports will return default constructed nodes, so that failure | ||||||||||
191 | // can be detected with a single conditional branch after a sequence of | ||||||||||
192 | // imports. | ||||||||||
193 | template <typename T> T importChecked(Error &Err, const T &From) { | ||||||||||
194 | // Don't attempt to import nodes if we hit an error earlier. | ||||||||||
195 | if (Err) | ||||||||||
196 | return T{}; | ||||||||||
197 | Expected<T> MaybeVal = import(From); | ||||||||||
198 | if (!MaybeVal) { | ||||||||||
199 | Err = MaybeVal.takeError(); | ||||||||||
200 | return T{}; | ||||||||||
201 | } | ||||||||||
202 | return *MaybeVal; | ||||||||||
203 | } | ||||||||||
204 | |||||||||||
205 | ExplicitSpecifier importExplicitSpecifier(Error &Err, | ||||||||||
206 | ExplicitSpecifier ESpec); | ||||||||||
207 | |||||||||||
208 | // Wrapper for an overload set. | ||||||||||
209 | template <typename ToDeclT> struct CallOverloadedCreateFun { | ||||||||||
210 | template <typename... Args> decltype(auto) operator()(Args &&... args) { | ||||||||||
211 | return ToDeclT::Create(std::forward<Args>(args)...); | ||||||||||
212 | } | ||||||||||
213 | }; | ||||||||||
214 | |||||||||||
215 | // Always use these functions to create a Decl during import. There are | ||||||||||
216 | // certain tasks which must be done after the Decl was created, e.g. we | ||||||||||
217 | // must immediately register that as an imported Decl. The parameter `ToD` | ||||||||||
218 | // will be set to the newly created Decl or if had been imported before | ||||||||||
219 | // then to the already imported Decl. Returns a bool value set to true if | ||||||||||
220 | // the `FromD` had been imported before. | ||||||||||
221 | template <typename ToDeclT, typename FromDeclT, typename... Args> | ||||||||||
222 | LLVM_NODISCARD[[clang::warn_unused_result]] bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, | ||||||||||
223 | Args &&... args) { | ||||||||||
224 | // There may be several overloads of ToDeclT::Create. We must make sure | ||||||||||
225 | // to call the one which would be chosen by the arguments, thus we use a | ||||||||||
226 | // wrapper for the overload set. | ||||||||||
227 | CallOverloadedCreateFun<ToDeclT> OC; | ||||||||||
228 | return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, | ||||||||||
229 | std::forward<Args>(args)...); | ||||||||||
230 | } | ||||||||||
231 | // Use this overload if a special Type is needed to be created. E.g if we | ||||||||||
232 | // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl` | ||||||||||
233 | // then: | ||||||||||
234 | // TypedefNameDecl *ToTypedef; | ||||||||||
235 | // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...); | ||||||||||
236 | template <typename NewDeclT, typename ToDeclT, typename FromDeclT, | ||||||||||
237 | typename... Args> | ||||||||||
238 | LLVM_NODISCARD[[clang::warn_unused_result]] bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, | ||||||||||
239 | Args &&... args) { | ||||||||||
240 | CallOverloadedCreateFun<NewDeclT> OC; | ||||||||||
241 | return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, | ||||||||||
242 | std::forward<Args>(args)...); | ||||||||||
243 | } | ||||||||||
244 | // Use this version if a special create function must be | ||||||||||
245 | // used, e.g. CXXRecordDecl::CreateLambda . | ||||||||||
246 | template <typename ToDeclT, typename CreateFunT, typename FromDeclT, | ||||||||||
247 | typename... Args> | ||||||||||
248 | LLVM_NODISCARD[[clang::warn_unused_result]] bool | ||||||||||
249 | GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun, | ||||||||||
250 | FromDeclT *FromD, Args &&... args) { | ||||||||||
251 | if (Importer.getImportDeclErrorIfAny(FromD)) { | ||||||||||
252 | ToD = nullptr; | ||||||||||
253 | return true; // Already imported but with error. | ||||||||||
254 | } | ||||||||||
255 | ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD)); | ||||||||||
256 | if (ToD) | ||||||||||
257 | return true; // Already imported. | ||||||||||
258 | ToD = CreateFun(std::forward<Args>(args)...); | ||||||||||
259 | // Keep track of imported Decls. | ||||||||||
260 | Importer.RegisterImportedDecl(FromD, ToD); | ||||||||||
261 | InitializeImportedDecl(FromD, ToD); | ||||||||||
262 | return false; // A new Decl is created. | ||||||||||
263 | } | ||||||||||
264 | |||||||||||
265 | void InitializeImportedDecl(Decl *FromD, Decl *ToD) { | ||||||||||
266 | ToD->IdentifierNamespace = FromD->IdentifierNamespace; | ||||||||||
267 | if (FromD->isUsed()) | ||||||||||
268 | ToD->setIsUsed(); | ||||||||||
269 | if (FromD->isImplicit()) | ||||||||||
270 | ToD->setImplicit(); | ||||||||||
271 | } | ||||||||||
272 | |||||||||||
273 | // Check if we have found an existing definition. Returns with that | ||||||||||
274 | // definition if yes, otherwise returns null. | ||||||||||
275 | Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) { | ||||||||||
276 | const FunctionDecl *Definition = nullptr; | ||||||||||
277 | if (D->doesThisDeclarationHaveABody() && | ||||||||||
278 | FoundFunction->hasBody(Definition)) | ||||||||||
279 | return Importer.MapImported(D, const_cast<FunctionDecl *>(Definition)); | ||||||||||
280 | return nullptr; | ||||||||||
281 | } | ||||||||||
282 | |||||||||||
283 | void addDeclToContexts(Decl *FromD, Decl *ToD) { | ||||||||||
284 | if (Importer.isMinimalImport()) { | ||||||||||
285 | // In minimal import case the decl must be added even if it is not | ||||||||||
286 | // contained in original context, for LLDB compatibility. | ||||||||||
287 | // FIXME: Check if a better solution is possible. | ||||||||||
288 | if (!FromD->getDescribedTemplate() && | ||||||||||
289 | FromD->getFriendObjectKind() == Decl::FOK_None) | ||||||||||
290 | ToD->getLexicalDeclContext()->addDeclInternal(ToD); | ||||||||||
291 | return; | ||||||||||
292 | } | ||||||||||
293 | |||||||||||
294 | DeclContext *FromDC = FromD->getDeclContext(); | ||||||||||
295 | DeclContext *FromLexicalDC = FromD->getLexicalDeclContext(); | ||||||||||
296 | DeclContext *ToDC = ToD->getDeclContext(); | ||||||||||
297 | DeclContext *ToLexicalDC = ToD->getLexicalDeclContext(); | ||||||||||
298 | |||||||||||
299 | bool Visible = false; | ||||||||||
300 | if (FromDC->containsDeclAndLoad(FromD)) { | ||||||||||
301 | ToDC->addDeclInternal(ToD); | ||||||||||
302 | Visible = true; | ||||||||||
303 | } | ||||||||||
304 | if (ToDC != ToLexicalDC && FromLexicalDC->containsDeclAndLoad(FromD)) { | ||||||||||
305 | ToLexicalDC->addDeclInternal(ToD); | ||||||||||
306 | Visible = true; | ||||||||||
307 | } | ||||||||||
308 | |||||||||||
309 | // If the Decl was added to any context, it was made already visible. | ||||||||||
310 | // Otherwise it is still possible that it should be visible. | ||||||||||
311 | if (!Visible) { | ||||||||||
312 | if (auto *FromNamed = dyn_cast<NamedDecl>(FromD)) { | ||||||||||
313 | auto *ToNamed = cast<NamedDecl>(ToD); | ||||||||||
314 | DeclContextLookupResult FromLookup = | ||||||||||
315 | FromDC->lookup(FromNamed->getDeclName()); | ||||||||||
316 | for (NamedDecl *ND : FromLookup) | ||||||||||
317 | if (ND == FromNamed) { | ||||||||||
318 | ToDC->makeDeclVisibleInContext(ToNamed); | ||||||||||
319 | break; | ||||||||||
320 | } | ||||||||||
321 | } | ||||||||||
322 | } | ||||||||||
323 | } | ||||||||||
324 | |||||||||||
325 | public: | ||||||||||
326 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {} | ||||||||||
327 | |||||||||||
328 | using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit; | ||||||||||
329 | using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit; | ||||||||||
330 | using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit; | ||||||||||
331 | |||||||||||
332 | // Importing types | ||||||||||
333 | ExpectedType VisitType(const Type *T); | ||||||||||
334 | ExpectedType VisitAtomicType(const AtomicType *T); | ||||||||||
335 | ExpectedType VisitBuiltinType(const BuiltinType *T); | ||||||||||
336 | ExpectedType VisitDecayedType(const DecayedType *T); | ||||||||||
337 | ExpectedType VisitComplexType(const ComplexType *T); | ||||||||||
338 | ExpectedType VisitPointerType(const PointerType *T); | ||||||||||
339 | ExpectedType VisitBlockPointerType(const BlockPointerType *T); | ||||||||||
340 | ExpectedType VisitLValueReferenceType(const LValueReferenceType *T); | ||||||||||
341 | ExpectedType VisitRValueReferenceType(const RValueReferenceType *T); | ||||||||||
342 | ExpectedType VisitMemberPointerType(const MemberPointerType *T); | ||||||||||
343 | ExpectedType VisitConstantArrayType(const ConstantArrayType *T); | ||||||||||
344 | ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T); | ||||||||||
345 | ExpectedType VisitVariableArrayType(const VariableArrayType *T); | ||||||||||
346 | ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T); | ||||||||||
347 | // FIXME: DependentSizedExtVectorType | ||||||||||
348 | ExpectedType VisitVectorType(const VectorType *T); | ||||||||||
349 | ExpectedType VisitExtVectorType(const ExtVectorType *T); | ||||||||||
350 | ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T); | ||||||||||
351 | ExpectedType VisitFunctionProtoType(const FunctionProtoType *T); | ||||||||||
352 | ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T); | ||||||||||
353 | ExpectedType VisitParenType(const ParenType *T); | ||||||||||
354 | ExpectedType VisitTypedefType(const TypedefType *T); | ||||||||||
355 | ExpectedType VisitTypeOfExprType(const TypeOfExprType *T); | ||||||||||
356 | // FIXME: DependentTypeOfExprType | ||||||||||
357 | ExpectedType VisitTypeOfType(const TypeOfType *T); | ||||||||||
358 | ExpectedType VisitDecltypeType(const DecltypeType *T); | ||||||||||
359 | ExpectedType VisitUnaryTransformType(const UnaryTransformType *T); | ||||||||||
360 | ExpectedType VisitAutoType(const AutoType *T); | ||||||||||
361 | ExpectedType VisitDeducedTemplateSpecializationType( | ||||||||||
362 | const DeducedTemplateSpecializationType *T); | ||||||||||
363 | ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T); | ||||||||||
364 | // FIXME: DependentDecltypeType | ||||||||||
365 | ExpectedType VisitRecordType(const RecordType *T); | ||||||||||
366 | ExpectedType VisitEnumType(const EnumType *T); | ||||||||||
367 | ExpectedType VisitAttributedType(const AttributedType *T); | ||||||||||
368 | ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T); | ||||||||||
369 | ExpectedType VisitSubstTemplateTypeParmType( | ||||||||||
370 | const SubstTemplateTypeParmType *T); | ||||||||||
371 | ExpectedType VisitTemplateSpecializationType( | ||||||||||
372 | const TemplateSpecializationType *T); | ||||||||||
373 | ExpectedType VisitElaboratedType(const ElaboratedType *T); | ||||||||||
374 | ExpectedType VisitDependentNameType(const DependentNameType *T); | ||||||||||
375 | ExpectedType VisitPackExpansionType(const PackExpansionType *T); | ||||||||||
376 | ExpectedType VisitDependentTemplateSpecializationType( | ||||||||||
377 | const DependentTemplateSpecializationType *T); | ||||||||||
378 | ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T); | ||||||||||
379 | ExpectedType VisitObjCObjectType(const ObjCObjectType *T); | ||||||||||
380 | ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); | ||||||||||
381 | |||||||||||
382 | // Importing declarations | ||||||||||
383 | Error ImportDeclParts(NamedDecl *D, DeclarationName &Name, NamedDecl *&ToD, | ||||||||||
384 | SourceLocation &Loc); | ||||||||||
385 | Error ImportDeclParts( | ||||||||||
386 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, | ||||||||||
387 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc); | ||||||||||
388 | Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); | ||||||||||
389 | Error ImportDeclarationNameLoc( | ||||||||||
390 | const DeclarationNameInfo &From, DeclarationNameInfo &To); | ||||||||||
391 | Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); | ||||||||||
392 | Error ImportDeclContext( | ||||||||||
393 | Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC); | ||||||||||
394 | Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To); | ||||||||||
395 | |||||||||||
396 | Expected<CXXCastPath> ImportCastPath(CastExpr *E); | ||||||||||
397 | Expected<APValue> ImportAPValue(const APValue &FromValue); | ||||||||||
398 | |||||||||||
399 | using Designator = DesignatedInitExpr::Designator; | ||||||||||
400 | |||||||||||
401 | /// What we should import from the definition. | ||||||||||
402 | enum ImportDefinitionKind { | ||||||||||
403 | /// Import the default subset of the definition, which might be | ||||||||||
404 | /// nothing (if minimal import is set) or might be everything (if minimal | ||||||||||
405 | /// import is not set). | ||||||||||
406 | IDK_Default, | ||||||||||
407 | /// Import everything. | ||||||||||
408 | IDK_Everything, | ||||||||||
409 | /// Import only the bare bones needed to establish a valid | ||||||||||
410 | /// DeclContext. | ||||||||||
411 | IDK_Basic | ||||||||||
412 | }; | ||||||||||
413 | |||||||||||
414 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { | ||||||||||
415 | return IDK == IDK_Everything || | ||||||||||
416 | (IDK == IDK_Default && !Importer.isMinimalImport()); | ||||||||||
417 | } | ||||||||||
418 | |||||||||||
419 | Error ImportInitializer(VarDecl *From, VarDecl *To); | ||||||||||
420 | Error ImportDefinition( | ||||||||||
421 | RecordDecl *From, RecordDecl *To, | ||||||||||
422 | ImportDefinitionKind Kind = IDK_Default); | ||||||||||
423 | Error ImportDefinition( | ||||||||||
424 | EnumDecl *From, EnumDecl *To, | ||||||||||
425 | ImportDefinitionKind Kind = IDK_Default); | ||||||||||
426 | Error ImportDefinition( | ||||||||||
427 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, | ||||||||||
428 | ImportDefinitionKind Kind = IDK_Default); | ||||||||||
429 | Error ImportDefinition( | ||||||||||
430 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, | ||||||||||
431 | ImportDefinitionKind Kind = IDK_Default); | ||||||||||
432 | Error ImportTemplateArguments( | ||||||||||
433 | const TemplateArgument *FromArgs, unsigned NumFromArgs, | ||||||||||
434 | SmallVectorImpl<TemplateArgument> &ToArgs); | ||||||||||
435 | Expected<TemplateArgument> | ||||||||||
436 | ImportTemplateArgument(const TemplateArgument &From); | ||||||||||
437 | |||||||||||
438 | template <typename InContainerTy> | ||||||||||
439 | Error ImportTemplateArgumentListInfo( | ||||||||||
440 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo); | ||||||||||
441 | |||||||||||
442 | template<typename InContainerTy> | ||||||||||
443 | Error ImportTemplateArgumentListInfo( | ||||||||||
444 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, | ||||||||||
445 | const InContainerTy &Container, TemplateArgumentListInfo &Result); | ||||||||||
446 | |||||||||||
447 | using TemplateArgsTy = SmallVector<TemplateArgument, 8>; | ||||||||||
448 | using FunctionTemplateAndArgsTy = | ||||||||||
449 | std::tuple<FunctionTemplateDecl *, TemplateArgsTy>; | ||||||||||
450 | Expected<FunctionTemplateAndArgsTy> | ||||||||||
451 | ImportFunctionTemplateWithTemplateArgsFromSpecialization( | ||||||||||
452 | FunctionDecl *FromFD); | ||||||||||
453 | Error ImportTemplateParameterLists(const DeclaratorDecl *FromD, | ||||||||||
454 | DeclaratorDecl *ToD); | ||||||||||
455 | |||||||||||
456 | Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD); | ||||||||||
457 | |||||||||||
458 | Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD); | ||||||||||
459 | |||||||||||
460 | Error ImportDefaultArgOfParmVarDecl(const ParmVarDecl *FromParam, | ||||||||||
461 | ParmVarDecl *ToParam); | ||||||||||
462 | |||||||||||
463 | template <typename T> | ||||||||||
464 | bool hasSameVisibilityContextAndLinkage(T *Found, T *From); | ||||||||||
465 | |||||||||||
466 | bool IsStructuralMatch(Decl *From, Decl *To, bool Complain); | ||||||||||
467 | bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, | ||||||||||
468 | bool Complain = true); | ||||||||||
469 | bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, | ||||||||||
470 | bool Complain = true); | ||||||||||
471 | bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); | ||||||||||
472 | bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC); | ||||||||||
473 | bool IsStructuralMatch(FunctionTemplateDecl *From, | ||||||||||
474 | FunctionTemplateDecl *To); | ||||||||||
475 | bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To); | ||||||||||
476 | bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); | ||||||||||
477 | bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To); | ||||||||||
478 | ExpectedDecl VisitDecl(Decl *D); | ||||||||||
479 | ExpectedDecl VisitImportDecl(ImportDecl *D); | ||||||||||
480 | ExpectedDecl VisitEmptyDecl(EmptyDecl *D); | ||||||||||
481 | ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D); | ||||||||||
482 | ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D); | ||||||||||
483 | ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D); | ||||||||||
484 | ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D); | ||||||||||
485 | ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D); | ||||||||||
486 | ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); | ||||||||||
487 | ExpectedDecl VisitTypedefDecl(TypedefDecl *D); | ||||||||||
488 | ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D); | ||||||||||
489 | ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); | ||||||||||
490 | ExpectedDecl VisitLabelDecl(LabelDecl *D); | ||||||||||
491 | ExpectedDecl VisitEnumDecl(EnumDecl *D); | ||||||||||
492 | ExpectedDecl VisitRecordDecl(RecordDecl *D); | ||||||||||
493 | ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D); | ||||||||||
494 | ExpectedDecl VisitFunctionDecl(FunctionDecl *D); | ||||||||||
495 | ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D); | ||||||||||
496 | ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D); | ||||||||||
497 | ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D); | ||||||||||
498 | ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D); | ||||||||||
499 | ExpectedDecl VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D); | ||||||||||
500 | ExpectedDecl VisitFieldDecl(FieldDecl *D); | ||||||||||
501 | ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D); | ||||||||||
502 | ExpectedDecl VisitFriendDecl(FriendDecl *D); | ||||||||||
503 | ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D); | ||||||||||
504 | ExpectedDecl VisitVarDecl(VarDecl *D); | ||||||||||
505 | ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D); | ||||||||||
506 | ExpectedDecl VisitParmVarDecl(ParmVarDecl *D); | ||||||||||
507 | ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D); | ||||||||||
508 | ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); | ||||||||||
509 | ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D); | ||||||||||
510 | ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D); | ||||||||||
511 | ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D); | ||||||||||
512 | ExpectedDecl VisitUsingDecl(UsingDecl *D); | ||||||||||
513 | ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D); | ||||||||||
514 | ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D); | ||||||||||
515 | ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); | ||||||||||
516 | ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); | ||||||||||
517 | ExpectedDecl VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D); | ||||||||||
518 | ExpectedDecl | ||||||||||
519 | VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D); | ||||||||||
520 | |||||||||||
521 | Expected<ObjCTypeParamList *> | ||||||||||
522 | ImportObjCTypeParamList(ObjCTypeParamList *list); | ||||||||||
523 | |||||||||||
524 | ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); | ||||||||||
525 | ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); | ||||||||||
526 | ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D); | ||||||||||
527 | ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D); | ||||||||||
528 | ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); | ||||||||||
529 | ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); | ||||||||||
530 | ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); | ||||||||||
531 | ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); | ||||||||||
532 | ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D); | ||||||||||
533 | ExpectedDecl VisitClassTemplateSpecializationDecl( | ||||||||||
534 | ClassTemplateSpecializationDecl *D); | ||||||||||
535 | ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D); | ||||||||||
536 | ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); | ||||||||||
537 | ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D); | ||||||||||
538 | |||||||||||
539 | // Importing statements | ||||||||||
540 | ExpectedStmt VisitStmt(Stmt *S); | ||||||||||
541 | ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S); | ||||||||||
542 | ExpectedStmt VisitDeclStmt(DeclStmt *S); | ||||||||||
543 | ExpectedStmt VisitNullStmt(NullStmt *S); | ||||||||||
544 | ExpectedStmt VisitCompoundStmt(CompoundStmt *S); | ||||||||||
545 | ExpectedStmt VisitCaseStmt(CaseStmt *S); | ||||||||||
546 | ExpectedStmt VisitDefaultStmt(DefaultStmt *S); | ||||||||||
547 | ExpectedStmt VisitLabelStmt(LabelStmt *S); | ||||||||||
548 | ExpectedStmt VisitAttributedStmt(AttributedStmt *S); | ||||||||||
549 | ExpectedStmt VisitIfStmt(IfStmt *S); | ||||||||||
550 | ExpectedStmt VisitSwitchStmt(SwitchStmt *S); | ||||||||||
551 | ExpectedStmt VisitWhileStmt(WhileStmt *S); | ||||||||||
552 | ExpectedStmt VisitDoStmt(DoStmt *S); | ||||||||||
553 | ExpectedStmt VisitForStmt(ForStmt *S); | ||||||||||
554 | ExpectedStmt VisitGotoStmt(GotoStmt *S); | ||||||||||
555 | ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S); | ||||||||||
556 | ExpectedStmt VisitContinueStmt(ContinueStmt *S); | ||||||||||
557 | ExpectedStmt VisitBreakStmt(BreakStmt *S); | ||||||||||
558 | ExpectedStmt VisitReturnStmt(ReturnStmt *S); | ||||||||||
559 | // FIXME: MSAsmStmt | ||||||||||
560 | // FIXME: SEHExceptStmt | ||||||||||
561 | // FIXME: SEHFinallyStmt | ||||||||||
562 | // FIXME: SEHTryStmt | ||||||||||
563 | // FIXME: SEHLeaveStmt | ||||||||||
564 | // FIXME: CapturedStmt | ||||||||||
565 | ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S); | ||||||||||
566 | ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S); | ||||||||||
567 | ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S); | ||||||||||
568 | // FIXME: MSDependentExistsStmt | ||||||||||
569 | ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); | ||||||||||
570 | ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); | ||||||||||
571 | ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); | ||||||||||
572 | ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S); | ||||||||||
573 | ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); | ||||||||||
574 | ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); | ||||||||||
575 | ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); | ||||||||||
576 | |||||||||||
577 | // Importing expressions | ||||||||||
578 | ExpectedStmt VisitExpr(Expr *E); | ||||||||||
579 | ExpectedStmt VisitSourceLocExpr(SourceLocExpr *E); | ||||||||||
580 | ExpectedStmt VisitVAArgExpr(VAArgExpr *E); | ||||||||||
581 | ExpectedStmt VisitChooseExpr(ChooseExpr *E); | ||||||||||
582 | ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E); | ||||||||||
583 | ExpectedStmt VisitGenericSelectionExpr(GenericSelectionExpr *E); | ||||||||||
584 | ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E); | ||||||||||
585 | ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E); | ||||||||||
586 | ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); | ||||||||||
587 | ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E); | ||||||||||
588 | ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); | ||||||||||
589 | ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E); | ||||||||||
590 | ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E); | ||||||||||
591 | ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E); | ||||||||||
592 | ExpectedStmt VisitFixedPointLiteral(FixedPointLiteral *E); | ||||||||||
593 | ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E); | ||||||||||
594 | ExpectedStmt VisitStringLiteral(StringLiteral *E); | ||||||||||
595 | ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E); | ||||||||||
596 | ExpectedStmt VisitAtomicExpr(AtomicExpr *E); | ||||||||||
597 | ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E); | ||||||||||
598 | ExpectedStmt VisitConstantExpr(ConstantExpr *E); | ||||||||||
599 | ExpectedStmt VisitParenExpr(ParenExpr *E); | ||||||||||
600 | ExpectedStmt VisitParenListExpr(ParenListExpr *E); | ||||||||||
601 | ExpectedStmt VisitStmtExpr(StmtExpr *E); | ||||||||||
602 | ExpectedStmt VisitUnaryOperator(UnaryOperator *E); | ||||||||||
603 | ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); | ||||||||||
604 | ExpectedStmt VisitBinaryOperator(BinaryOperator *E); | ||||||||||
605 | ExpectedStmt VisitConditionalOperator(ConditionalOperator *E); | ||||||||||
606 | ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E); | ||||||||||
607 | ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E); | ||||||||||
608 | ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); | ||||||||||
609 | ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E); | ||||||||||
610 | ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E); | ||||||||||
611 | ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E); | ||||||||||
612 | ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E); | ||||||||||
613 | ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E); | ||||||||||
614 | ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE); | ||||||||||
615 | ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E); | ||||||||||
616 | ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E); | ||||||||||
617 | ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); | ||||||||||
618 | ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); | ||||||||||
619 | ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); | ||||||||||
620 | ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); | ||||||||||
621 | ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); | ||||||||||
622 | ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E); | ||||||||||
623 | ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E); | ||||||||||
624 | ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E); | ||||||||||
625 | ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E); | ||||||||||
626 | ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E); | ||||||||||
627 | ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E); | ||||||||||
628 | ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); | ||||||||||
629 | ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); | ||||||||||
630 | ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); | ||||||||||
631 | ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); | ||||||||||
632 | ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E); | ||||||||||
633 | ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E); | ||||||||||
634 | ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E); | ||||||||||
635 | ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); | ||||||||||
636 | ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); | ||||||||||
637 | ExpectedStmt VisitMemberExpr(MemberExpr *E); | ||||||||||
638 | ExpectedStmt VisitCallExpr(CallExpr *E); | ||||||||||
639 | ExpectedStmt VisitLambdaExpr(LambdaExpr *LE); | ||||||||||
640 | ExpectedStmt VisitInitListExpr(InitListExpr *E); | ||||||||||
641 | ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E); | ||||||||||
642 | ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E); | ||||||||||
643 | ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E); | ||||||||||
644 | ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E); | ||||||||||
645 | ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); | ||||||||||
646 | ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E); | ||||||||||
647 | ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E); | ||||||||||
648 | ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E); | ||||||||||
649 | ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E); | ||||||||||
650 | ExpectedStmt VisitCXXFoldExpr(CXXFoldExpr *E); | ||||||||||
651 | |||||||||||
652 | template<typename IIter, typename OIter> | ||||||||||
653 | Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { | ||||||||||
654 | using ItemT = std::remove_reference_t<decltype(*Obegin)>; | ||||||||||
655 | for (; Ibegin != Iend; ++Ibegin, ++Obegin) { | ||||||||||
656 | Expected<ItemT> ToOrErr = import(*Ibegin); | ||||||||||
657 | if (!ToOrErr) | ||||||||||
658 | return ToOrErr.takeError(); | ||||||||||
659 | *Obegin = *ToOrErr; | ||||||||||
660 | } | ||||||||||
661 | return Error::success(); | ||||||||||
662 | } | ||||||||||
663 | |||||||||||
664 | // Import every item from a container structure into an output container. | ||||||||||
665 | // If error occurs, stops at first error and returns the error. | ||||||||||
666 | // The output container should have space for all needed elements (it is not | ||||||||||
667 | // expanded, new items are put into from the beginning). | ||||||||||
668 | template<typename InContainerTy, typename OutContainerTy> | ||||||||||
669 | Error ImportContainerChecked( | ||||||||||
670 | const InContainerTy &InContainer, OutContainerTy &OutContainer) { | ||||||||||
671 | return ImportArrayChecked( | ||||||||||
672 | InContainer.begin(), InContainer.end(), OutContainer.begin()); | ||||||||||
673 | } | ||||||||||
674 | |||||||||||
675 | template<typename InContainerTy, typename OIter> | ||||||||||
676 | Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) { | ||||||||||
677 | return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin); | ||||||||||
678 | } | ||||||||||
679 | |||||||||||
680 | Error ImportOverriddenMethods(CXXMethodDecl *ToMethod, | ||||||||||
681 | CXXMethodDecl *FromMethod); | ||||||||||
682 | |||||||||||
683 | Expected<FunctionDecl *> FindFunctionTemplateSpecialization( | ||||||||||
684 | FunctionDecl *FromFD); | ||||||||||
685 | |||||||||||
686 | // Returns true if the given function has a placeholder return type and | ||||||||||
687 | // that type is declared inside the body of the function. | ||||||||||
688 | // E.g. auto f() { struct X{}; return X(); } | ||||||||||
689 | bool hasAutoReturnTypeDeclaredInside(FunctionDecl *D); | ||||||||||
690 | }; | ||||||||||
691 | |||||||||||
692 | template <typename InContainerTy> | ||||||||||
693 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( | ||||||||||
694 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, | ||||||||||
695 | const InContainerTy &Container, TemplateArgumentListInfo &Result) { | ||||||||||
696 | auto ToLAngleLocOrErr = import(FromLAngleLoc); | ||||||||||
697 | if (!ToLAngleLocOrErr) | ||||||||||
698 | return ToLAngleLocOrErr.takeError(); | ||||||||||
699 | auto ToRAngleLocOrErr = import(FromRAngleLoc); | ||||||||||
700 | if (!ToRAngleLocOrErr) | ||||||||||
701 | return ToRAngleLocOrErr.takeError(); | ||||||||||
702 | |||||||||||
703 | TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr); | ||||||||||
704 | if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo)) | ||||||||||
705 | return Err; | ||||||||||
706 | Result = ToTAInfo; | ||||||||||
707 | return Error::success(); | ||||||||||
708 | } | ||||||||||
709 | |||||||||||
710 | template <> | ||||||||||
711 | Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>( | ||||||||||
712 | const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) { | ||||||||||
713 | return ImportTemplateArgumentListInfo( | ||||||||||
714 | From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result); | ||||||||||
715 | } | ||||||||||
716 | |||||||||||
717 | template <> | ||||||||||
718 | Error ASTNodeImporter::ImportTemplateArgumentListInfo< | ||||||||||
719 | ASTTemplateArgumentListInfo>( | ||||||||||
720 | const ASTTemplateArgumentListInfo &From, | ||||||||||
721 | TemplateArgumentListInfo &Result) { | ||||||||||
722 | return ImportTemplateArgumentListInfo( | ||||||||||
723 | From.LAngleLoc, From.RAngleLoc, From.arguments(), Result); | ||||||||||
724 | } | ||||||||||
725 | |||||||||||
726 | Expected<ASTNodeImporter::FunctionTemplateAndArgsTy> | ||||||||||
727 | ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization( | ||||||||||
728 | FunctionDecl *FromFD) { | ||||||||||
729 | assert(FromFD->getTemplatedKind() ==((FromFD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplateSpecialization ) ? static_cast<void> (0) : __assert_fail ("FromFD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplateSpecialization" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 730, __PRETTY_FUNCTION__)) | ||||||||||
730 | FunctionDecl::TK_FunctionTemplateSpecialization)((FromFD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplateSpecialization ) ? static_cast<void> (0) : __assert_fail ("FromFD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplateSpecialization" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 730, __PRETTY_FUNCTION__)); | ||||||||||
731 | |||||||||||
732 | FunctionTemplateAndArgsTy Result; | ||||||||||
733 | |||||||||||
734 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); | ||||||||||
735 | if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate())) | ||||||||||
736 | return std::move(Err); | ||||||||||
737 | |||||||||||
738 | // Import template arguments. | ||||||||||
739 | auto TemplArgs = FTSInfo->TemplateArguments->asArray(); | ||||||||||
740 | if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(), | ||||||||||
741 | std::get<1>(Result))) | ||||||||||
742 | return std::move(Err); | ||||||||||
743 | |||||||||||
744 | return Result; | ||||||||||
745 | } | ||||||||||
746 | |||||||||||
747 | template <> | ||||||||||
748 | Expected<TemplateParameterList *> | ||||||||||
749 | ASTNodeImporter::import(TemplateParameterList *From) { | ||||||||||
750 | SmallVector<NamedDecl *, 4> To(From->size()); | ||||||||||
751 | if (Error Err = ImportContainerChecked(*From, To)) | ||||||||||
752 | return std::move(Err); | ||||||||||
753 | |||||||||||
754 | ExpectedExpr ToRequiresClause = import(From->getRequiresClause()); | ||||||||||
755 | if (!ToRequiresClause) | ||||||||||
756 | return ToRequiresClause.takeError(); | ||||||||||
757 | |||||||||||
758 | auto ToTemplateLocOrErr = import(From->getTemplateLoc()); | ||||||||||
759 | if (!ToTemplateLocOrErr) | ||||||||||
760 | return ToTemplateLocOrErr.takeError(); | ||||||||||
761 | auto ToLAngleLocOrErr = import(From->getLAngleLoc()); | ||||||||||
762 | if (!ToLAngleLocOrErr) | ||||||||||
763 | return ToLAngleLocOrErr.takeError(); | ||||||||||
764 | auto ToRAngleLocOrErr = import(From->getRAngleLoc()); | ||||||||||
765 | if (!ToRAngleLocOrErr) | ||||||||||
766 | return ToRAngleLocOrErr.takeError(); | ||||||||||
767 | |||||||||||
768 | return TemplateParameterList::Create( | ||||||||||
769 | Importer.getToContext(), | ||||||||||
770 | *ToTemplateLocOrErr, | ||||||||||
771 | *ToLAngleLocOrErr, | ||||||||||
772 | To, | ||||||||||
773 | *ToRAngleLocOrErr, | ||||||||||
774 | *ToRequiresClause); | ||||||||||
775 | } | ||||||||||
776 | |||||||||||
777 | template <> | ||||||||||
778 | Expected<TemplateArgument> | ||||||||||
779 | ASTNodeImporter::import(const TemplateArgument &From) { | ||||||||||
780 | switch (From.getKind()) { | ||||||||||
781 | case TemplateArgument::Null: | ||||||||||
782 | return TemplateArgument(); | ||||||||||
783 | |||||||||||
784 | case TemplateArgument::Type: { | ||||||||||
785 | ExpectedType ToTypeOrErr = import(From.getAsType()); | ||||||||||
786 | if (!ToTypeOrErr) | ||||||||||
787 | return ToTypeOrErr.takeError(); | ||||||||||
788 | return TemplateArgument(*ToTypeOrErr); | ||||||||||
789 | } | ||||||||||
790 | |||||||||||
791 | case TemplateArgument::Integral: { | ||||||||||
792 | ExpectedType ToTypeOrErr = import(From.getIntegralType()); | ||||||||||
793 | if (!ToTypeOrErr) | ||||||||||
794 | return ToTypeOrErr.takeError(); | ||||||||||
795 | return TemplateArgument(From, *ToTypeOrErr); | ||||||||||
796 | } | ||||||||||
797 | |||||||||||
798 | case TemplateArgument::Declaration: { | ||||||||||
799 | Expected<ValueDecl *> ToOrErr = import(From.getAsDecl()); | ||||||||||
800 | if (!ToOrErr) | ||||||||||
801 | return ToOrErr.takeError(); | ||||||||||
802 | ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl()); | ||||||||||
803 | if (!ToTypeOrErr) | ||||||||||
804 | return ToTypeOrErr.takeError(); | ||||||||||
805 | return TemplateArgument(*ToOrErr, *ToTypeOrErr); | ||||||||||
806 | } | ||||||||||
807 | |||||||||||
808 | case TemplateArgument::NullPtr: { | ||||||||||
809 | ExpectedType ToTypeOrErr = import(From.getNullPtrType()); | ||||||||||
810 | if (!ToTypeOrErr) | ||||||||||
811 | return ToTypeOrErr.takeError(); | ||||||||||
812 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true); | ||||||||||
813 | } | ||||||||||
814 | |||||||||||
815 | case TemplateArgument::Template: { | ||||||||||
816 | Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate()); | ||||||||||
817 | if (!ToTemplateOrErr) | ||||||||||
818 | return ToTemplateOrErr.takeError(); | ||||||||||
819 | |||||||||||
820 | return TemplateArgument(*ToTemplateOrErr); | ||||||||||
821 | } | ||||||||||
822 | |||||||||||
823 | case TemplateArgument::TemplateExpansion: { | ||||||||||
824 | Expected<TemplateName> ToTemplateOrErr = | ||||||||||
825 | import(From.getAsTemplateOrTemplatePattern()); | ||||||||||
826 | if (!ToTemplateOrErr) | ||||||||||
827 | return ToTemplateOrErr.takeError(); | ||||||||||
828 | |||||||||||
829 | return TemplateArgument( | ||||||||||
830 | *ToTemplateOrErr, From.getNumTemplateExpansions()); | ||||||||||
831 | } | ||||||||||
832 | |||||||||||
833 | case TemplateArgument::Expression: | ||||||||||
834 | if (ExpectedExpr ToExpr = import(From.getAsExpr())) | ||||||||||
835 | return TemplateArgument(*ToExpr); | ||||||||||
836 | else | ||||||||||
837 | return ToExpr.takeError(); | ||||||||||
838 | |||||||||||
839 | case TemplateArgument::Pack: { | ||||||||||
840 | SmallVector<TemplateArgument, 2> ToPack; | ||||||||||
841 | ToPack.reserve(From.pack_size()); | ||||||||||
842 | if (Error Err = ImportTemplateArguments( | ||||||||||
843 | From.pack_begin(), From.pack_size(), ToPack)) | ||||||||||
844 | return std::move(Err); | ||||||||||
845 | |||||||||||
846 | return TemplateArgument( | ||||||||||
847 | llvm::makeArrayRef(ToPack).copy(Importer.getToContext())); | ||||||||||
848 | } | ||||||||||
849 | } | ||||||||||
850 | |||||||||||
851 | llvm_unreachable("Invalid template argument kind")::llvm::llvm_unreachable_internal("Invalid template argument kind" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 851); | ||||||||||
852 | } | ||||||||||
853 | |||||||||||
854 | template <> | ||||||||||
855 | Expected<TemplateArgumentLoc> | ||||||||||
856 | ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) { | ||||||||||
857 | Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument()); | ||||||||||
858 | if (!ArgOrErr) | ||||||||||
859 | return ArgOrErr.takeError(); | ||||||||||
860 | TemplateArgument Arg = *ArgOrErr; | ||||||||||
861 | |||||||||||
862 | TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo(); | ||||||||||
863 | |||||||||||
864 | TemplateArgumentLocInfo ToInfo; | ||||||||||
865 | if (Arg.getKind() == TemplateArgument::Expression) { | ||||||||||
866 | ExpectedExpr E = import(FromInfo.getAsExpr()); | ||||||||||
867 | if (!E) | ||||||||||
868 | return E.takeError(); | ||||||||||
869 | ToInfo = TemplateArgumentLocInfo(*E); | ||||||||||
870 | } else if (Arg.getKind() == TemplateArgument::Type) { | ||||||||||
871 | if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo())) | ||||||||||
872 | ToInfo = TemplateArgumentLocInfo(*TSIOrErr); | ||||||||||
873 | else | ||||||||||
874 | return TSIOrErr.takeError(); | ||||||||||
875 | } else { | ||||||||||
876 | auto ToTemplateQualifierLocOrErr = | ||||||||||
877 | import(FromInfo.getTemplateQualifierLoc()); | ||||||||||
878 | if (!ToTemplateQualifierLocOrErr) | ||||||||||
879 | return ToTemplateQualifierLocOrErr.takeError(); | ||||||||||
880 | auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc()); | ||||||||||
881 | if (!ToTemplateNameLocOrErr) | ||||||||||
882 | return ToTemplateNameLocOrErr.takeError(); | ||||||||||
883 | auto ToTemplateEllipsisLocOrErr = | ||||||||||
884 | import(FromInfo.getTemplateEllipsisLoc()); | ||||||||||
885 | if (!ToTemplateEllipsisLocOrErr) | ||||||||||
886 | return ToTemplateEllipsisLocOrErr.takeError(); | ||||||||||
887 | ToInfo = TemplateArgumentLocInfo( | ||||||||||
888 | Importer.getToContext(), *ToTemplateQualifierLocOrErr, | ||||||||||
889 | *ToTemplateNameLocOrErr, *ToTemplateEllipsisLocOrErr); | ||||||||||
890 | } | ||||||||||
891 | |||||||||||
892 | return TemplateArgumentLoc(Arg, ToInfo); | ||||||||||
893 | } | ||||||||||
894 | |||||||||||
895 | template <> | ||||||||||
896 | Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) { | ||||||||||
897 | if (DG.isNull()) | ||||||||||
898 | return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0); | ||||||||||
899 | size_t NumDecls = DG.end() - DG.begin(); | ||||||||||
900 | SmallVector<Decl *, 1> ToDecls; | ||||||||||
901 | ToDecls.reserve(NumDecls); | ||||||||||
902 | for (Decl *FromD : DG) { | ||||||||||
903 | if (auto ToDOrErr = import(FromD)) | ||||||||||
904 | ToDecls.push_back(*ToDOrErr); | ||||||||||
905 | else | ||||||||||
906 | return ToDOrErr.takeError(); | ||||||||||
907 | } | ||||||||||
908 | return DeclGroupRef::Create(Importer.getToContext(), | ||||||||||
909 | ToDecls.begin(), | ||||||||||
910 | NumDecls); | ||||||||||
911 | } | ||||||||||
912 | |||||||||||
913 | template <> | ||||||||||
914 | Expected<ASTNodeImporter::Designator> | ||||||||||
915 | ASTNodeImporter::import(const Designator &D) { | ||||||||||
916 | if (D.isFieldDesignator()) { | ||||||||||
917 | IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName()); | ||||||||||
918 | |||||||||||
919 | ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc()); | ||||||||||
920 | if (!ToDotLocOrErr) | ||||||||||
921 | return ToDotLocOrErr.takeError(); | ||||||||||
922 | |||||||||||
923 | ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc()); | ||||||||||
924 | if (!ToFieldLocOrErr) | ||||||||||
925 | return ToFieldLocOrErr.takeError(); | ||||||||||
926 | |||||||||||
927 | return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr); | ||||||||||
928 | } | ||||||||||
929 | |||||||||||
930 | ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc()); | ||||||||||
931 | if (!ToLBracketLocOrErr) | ||||||||||
932 | return ToLBracketLocOrErr.takeError(); | ||||||||||
933 | |||||||||||
934 | ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc()); | ||||||||||
935 | if (!ToRBracketLocOrErr) | ||||||||||
936 | return ToRBracketLocOrErr.takeError(); | ||||||||||
937 | |||||||||||
938 | if (D.isArrayDesignator()) | ||||||||||
939 | return Designator(D.getFirstExprIndex(), | ||||||||||
940 | *ToLBracketLocOrErr, *ToRBracketLocOrErr); | ||||||||||
941 | |||||||||||
942 | ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc()); | ||||||||||
943 | if (!ToEllipsisLocOrErr) | ||||||||||
944 | return ToEllipsisLocOrErr.takeError(); | ||||||||||
945 | |||||||||||
946 | assert(D.isArrayRangeDesignator())((D.isArrayRangeDesignator()) ? static_cast<void> (0) : __assert_fail ("D.isArrayRangeDesignator()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 946, __PRETTY_FUNCTION__)); | ||||||||||
947 | return Designator( | ||||||||||
948 | D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr, | ||||||||||
949 | *ToRBracketLocOrErr); | ||||||||||
950 | } | ||||||||||
951 | |||||||||||
952 | template <> | ||||||||||
953 | Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) { | ||||||||||
954 | VarDecl *Var = nullptr; | ||||||||||
955 | if (From.capturesVariable()) { | ||||||||||
956 | if (auto VarOrErr = import(From.getCapturedVar())) | ||||||||||
957 | Var = *VarOrErr; | ||||||||||
958 | else | ||||||||||
959 | return VarOrErr.takeError(); | ||||||||||
960 | } | ||||||||||
961 | |||||||||||
962 | auto LocationOrErr = import(From.getLocation()); | ||||||||||
963 | if (!LocationOrErr) | ||||||||||
964 | return LocationOrErr.takeError(); | ||||||||||
965 | |||||||||||
966 | SourceLocation EllipsisLoc; | ||||||||||
967 | if (From.isPackExpansion()) | ||||||||||
968 | if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc())) | ||||||||||
969 | return std::move(Err); | ||||||||||
970 | |||||||||||
971 | return LambdaCapture( | ||||||||||
972 | *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var, | ||||||||||
973 | EllipsisLoc); | ||||||||||
974 | } | ||||||||||
975 | |||||||||||
976 | template <typename T> | ||||||||||
977 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(T *Found, T *From) { | ||||||||||
978 | if (Found->getLinkageInternal() != From->getLinkageInternal()) | ||||||||||
979 | return false; | ||||||||||
980 | |||||||||||
981 | if (From->hasExternalFormalLinkage()) | ||||||||||
982 | return Found->hasExternalFormalLinkage(); | ||||||||||
983 | if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl()) | ||||||||||
984 | return false; | ||||||||||
985 | if (From->isInAnonymousNamespace()) | ||||||||||
986 | return Found->isInAnonymousNamespace(); | ||||||||||
987 | else | ||||||||||
988 | return !Found->isInAnonymousNamespace() && | ||||||||||
989 | !Found->hasExternalFormalLinkage(); | ||||||||||
990 | } | ||||||||||
991 | |||||||||||
992 | template <> | ||||||||||
993 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(TypedefNameDecl *Found, | ||||||||||
994 | TypedefNameDecl *From) { | ||||||||||
995 | if (Found->getLinkageInternal() != From->getLinkageInternal()) | ||||||||||
996 | return false; | ||||||||||
997 | |||||||||||
998 | if (From->isInAnonymousNamespace() && Found->isInAnonymousNamespace()) | ||||||||||
999 | return Importer.GetFromTU(Found) == From->getTranslationUnitDecl(); | ||||||||||
1000 | return From->isInAnonymousNamespace() == Found->isInAnonymousNamespace(); | ||||||||||
1001 | } | ||||||||||
1002 | |||||||||||
1003 | } // namespace clang | ||||||||||
1004 | |||||||||||
1005 | //---------------------------------------------------------------------------- | ||||||||||
1006 | // Import Types | ||||||||||
1007 | //---------------------------------------------------------------------------- | ||||||||||
1008 | |||||||||||
1009 | using namespace clang; | ||||||||||
1010 | |||||||||||
1011 | ExpectedType ASTNodeImporter::VisitType(const Type *T) { | ||||||||||
1012 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) | ||||||||||
1013 | << T->getTypeClassName(); | ||||||||||
1014 | return make_error<ImportError>(ImportError::UnsupportedConstruct); | ||||||||||
1015 | } | ||||||||||
1016 | |||||||||||
1017 | ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){ | ||||||||||
1018 | ExpectedType UnderlyingTypeOrErr = import(T->getValueType()); | ||||||||||
1019 | if (!UnderlyingTypeOrErr) | ||||||||||
1020 | return UnderlyingTypeOrErr.takeError(); | ||||||||||
1021 | |||||||||||
1022 | return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr); | ||||||||||
1023 | } | ||||||||||
1024 | |||||||||||
1025 | ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { | ||||||||||
1026 | switch (T->getKind()) { | ||||||||||
1027 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | ||||||||||
1028 | case BuiltinType::Id: \ | ||||||||||
1029 | return Importer.getToContext().SingletonId; | ||||||||||
1030 | #include "clang/Basic/OpenCLImageTypes.def" | ||||||||||
1031 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | ||||||||||
1032 | case BuiltinType::Id: \ | ||||||||||
1033 | return Importer.getToContext().Id##Ty; | ||||||||||
1034 | #include "clang/Basic/OpenCLExtensionTypes.def" | ||||||||||
1035 | #define SVE_TYPE(Name, Id, SingletonId) \ | ||||||||||
1036 | case BuiltinType::Id: \ | ||||||||||
1037 | return Importer.getToContext().SingletonId; | ||||||||||
1038 | #include "clang/Basic/AArch64SVEACLETypes.def" | ||||||||||
1039 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ | ||||||||||
1040 | case BuiltinType::Id: \ | ||||||||||
1041 | return Importer.getToContext().Id##Ty; | ||||||||||
1042 | #include "clang/Basic/PPCTypes.def" | ||||||||||
1043 | #define RVV_TYPE(Name, Id, SingletonId) \ | ||||||||||
1044 | case BuiltinType::Id: \ | ||||||||||
1045 | return Importer.getToContext().SingletonId; | ||||||||||
1046 | #include "clang/Basic/RISCVVTypes.def" | ||||||||||
1047 | #define SHARED_SINGLETON_TYPE(Expansion) | ||||||||||
1048 | #define BUILTIN_TYPE(Id, SingletonId) \ | ||||||||||
1049 | case BuiltinType::Id: return Importer.getToContext().SingletonId; | ||||||||||
1050 | #include "clang/AST/BuiltinTypes.def" | ||||||||||
1051 | |||||||||||
1052 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" | ||||||||||
1053 | // context supports C++. | ||||||||||
1054 | |||||||||||
1055 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" | ||||||||||
1056 | // context supports ObjC. | ||||||||||
1057 | |||||||||||
1058 | case BuiltinType::Char_U: | ||||||||||
1059 | // The context we're importing from has an unsigned 'char'. If we're | ||||||||||
1060 | // importing into a context with a signed 'char', translate to | ||||||||||
1061 | // 'unsigned char' instead. | ||||||||||
1062 | if (Importer.getToContext().getLangOpts().CharIsSigned) | ||||||||||
1063 | return Importer.getToContext().UnsignedCharTy; | ||||||||||
1064 | |||||||||||
1065 | return Importer.getToContext().CharTy; | ||||||||||
1066 | |||||||||||
1067 | case BuiltinType::Char_S: | ||||||||||
1068 | // The context we're importing from has an unsigned 'char'. If we're | ||||||||||
1069 | // importing into a context with a signed 'char', translate to | ||||||||||
1070 | // 'unsigned char' instead. | ||||||||||
1071 | if (!Importer.getToContext().getLangOpts().CharIsSigned) | ||||||||||
1072 | return Importer.getToContext().SignedCharTy; | ||||||||||
1073 | |||||||||||
1074 | return Importer.getToContext().CharTy; | ||||||||||
1075 | |||||||||||
1076 | case BuiltinType::WChar_S: | ||||||||||
1077 | case BuiltinType::WChar_U: | ||||||||||
1078 | // FIXME: If not in C++, shall we translate to the C equivalent of | ||||||||||
1079 | // wchar_t? | ||||||||||
1080 | return Importer.getToContext().WCharTy; | ||||||||||
1081 | } | ||||||||||
1082 | |||||||||||
1083 | llvm_unreachable("Invalid BuiltinType Kind!")::llvm::llvm_unreachable_internal("Invalid BuiltinType Kind!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 1083); | ||||||||||
1084 | } | ||||||||||
1085 | |||||||||||
1086 | ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) { | ||||||||||
1087 | ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType()); | ||||||||||
1088 | if (!ToOriginalTypeOrErr) | ||||||||||
1089 | return ToOriginalTypeOrErr.takeError(); | ||||||||||
1090 | |||||||||||
1091 | return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr); | ||||||||||
1092 | } | ||||||||||
1093 | |||||||||||
1094 | ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) { | ||||||||||
1095 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); | ||||||||||
1096 | if (!ToElementTypeOrErr) | ||||||||||
1097 | return ToElementTypeOrErr.takeError(); | ||||||||||
1098 | |||||||||||
1099 | return Importer.getToContext().getComplexType(*ToElementTypeOrErr); | ||||||||||
1100 | } | ||||||||||
1101 | |||||||||||
1102 | ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) { | ||||||||||
1103 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType()); | ||||||||||
1104 | if (!ToPointeeTypeOrErr) | ||||||||||
1105 | return ToPointeeTypeOrErr.takeError(); | ||||||||||
1106 | |||||||||||
1107 | return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr); | ||||||||||
1108 | } | ||||||||||
1109 | |||||||||||
1110 | ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { | ||||||||||
1111 | // FIXME: Check for blocks support in "to" context. | ||||||||||
1112 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType()); | ||||||||||
1113 | if (!ToPointeeTypeOrErr) | ||||||||||
1114 | return ToPointeeTypeOrErr.takeError(); | ||||||||||
1115 | |||||||||||
1116 | return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr); | ||||||||||
1117 | } | ||||||||||
1118 | |||||||||||
1119 | ExpectedType | ||||||||||
1120 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { | ||||||||||
1121 | // FIXME: Check for C++ support in "to" context. | ||||||||||
1122 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten()); | ||||||||||
1123 | if (!ToPointeeTypeOrErr) | ||||||||||
1124 | return ToPointeeTypeOrErr.takeError(); | ||||||||||
1125 | |||||||||||
1126 | return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr); | ||||||||||
1127 | } | ||||||||||
1128 | |||||||||||
1129 | ExpectedType | ||||||||||
1130 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { | ||||||||||
1131 | // FIXME: Check for C++0x support in "to" context. | ||||||||||
1132 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten()); | ||||||||||
1133 | if (!ToPointeeTypeOrErr) | ||||||||||
1134 | return ToPointeeTypeOrErr.takeError(); | ||||||||||
1135 | |||||||||||
1136 | return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr); | ||||||||||
1137 | } | ||||||||||
1138 | |||||||||||
1139 | ExpectedType | ||||||||||
1140 | ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { | ||||||||||
1141 | // FIXME: Check for C++ support in "to" context. | ||||||||||
1142 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType()); | ||||||||||
1143 | if (!ToPointeeTypeOrErr) | ||||||||||
1144 | return ToPointeeTypeOrErr.takeError(); | ||||||||||
1145 | |||||||||||
1146 | ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0)); | ||||||||||
1147 | if (!ClassTypeOrErr) | ||||||||||
1148 | return ClassTypeOrErr.takeError(); | ||||||||||
1149 | |||||||||||
1150 | return Importer.getToContext().getMemberPointerType( | ||||||||||
1151 | *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr()); | ||||||||||
1152 | } | ||||||||||
1153 | |||||||||||
1154 | ExpectedType | ||||||||||
1155 | ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { | ||||||||||
1156 | Error Err = Error::success(); | ||||||||||
1157 | auto ToElementType = importChecked(Err, T->getElementType()); | ||||||||||
1158 | auto ToSizeExpr = importChecked(Err, T->getSizeExpr()); | ||||||||||
1159 | if (Err) | ||||||||||
1160 | return std::move(Err); | ||||||||||
1161 | |||||||||||
1162 | return Importer.getToContext().getConstantArrayType( | ||||||||||
1163 | ToElementType, T->getSize(), ToSizeExpr, T->getSizeModifier(), | ||||||||||
1164 | T->getIndexTypeCVRQualifiers()); | ||||||||||
1165 | } | ||||||||||
1166 | |||||||||||
1167 | ExpectedType | ||||||||||
1168 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { | ||||||||||
1169 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); | ||||||||||
1170 | if (!ToElementTypeOrErr) | ||||||||||
1171 | return ToElementTypeOrErr.takeError(); | ||||||||||
1172 | |||||||||||
1173 | return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr, | ||||||||||
1174 | T->getSizeModifier(), | ||||||||||
1175 | T->getIndexTypeCVRQualifiers()); | ||||||||||
1176 | } | ||||||||||
1177 | |||||||||||
1178 | ExpectedType | ||||||||||
1179 | ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { | ||||||||||
1180 | Error Err = Error::success(); | ||||||||||
1181 | QualType ToElementType = importChecked(Err, T->getElementType()); | ||||||||||
1182 | Expr *ToSizeExpr = importChecked(Err, T->getSizeExpr()); | ||||||||||
1183 | SourceRange ToBracketsRange = importChecked(Err, T->getBracketsRange()); | ||||||||||
1184 | if (Err) | ||||||||||
1185 | return std::move(Err); | ||||||||||
1186 | return Importer.getToContext().getVariableArrayType( | ||||||||||
1187 | ToElementType, ToSizeExpr, T->getSizeModifier(), | ||||||||||
1188 | T->getIndexTypeCVRQualifiers(), ToBracketsRange); | ||||||||||
1189 | } | ||||||||||
1190 | |||||||||||
1191 | ExpectedType ASTNodeImporter::VisitDependentSizedArrayType( | ||||||||||
1192 | const DependentSizedArrayType *T) { | ||||||||||
1193 | Error Err = Error::success(); | ||||||||||
1194 | QualType ToElementType = importChecked(Err, T->getElementType()); | ||||||||||
1195 | Expr *ToSizeExpr = importChecked(Err, T->getSizeExpr()); | ||||||||||
1196 | SourceRange ToBracketsRange = importChecked(Err, T->getBracketsRange()); | ||||||||||
1197 | if (Err) | ||||||||||
1198 | return std::move(Err); | ||||||||||
1199 | // SizeExpr may be null if size is not specified directly. | ||||||||||
1200 | // For example, 'int a[]'. | ||||||||||
1201 | |||||||||||
1202 | return Importer.getToContext().getDependentSizedArrayType( | ||||||||||
1203 | ToElementType, ToSizeExpr, T->getSizeModifier(), | ||||||||||
1204 | T->getIndexTypeCVRQualifiers(), ToBracketsRange); | ||||||||||
1205 | } | ||||||||||
1206 | |||||||||||
1207 | ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) { | ||||||||||
1208 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); | ||||||||||
1209 | if (!ToElementTypeOrErr) | ||||||||||
1210 | return ToElementTypeOrErr.takeError(); | ||||||||||
1211 | |||||||||||
1212 | return Importer.getToContext().getVectorType(*ToElementTypeOrErr, | ||||||||||
1213 | T->getNumElements(), | ||||||||||
1214 | T->getVectorKind()); | ||||||||||
1215 | } | ||||||||||
1216 | |||||||||||
1217 | ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { | ||||||||||
1218 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); | ||||||||||
1219 | if (!ToElementTypeOrErr) | ||||||||||
1220 | return ToElementTypeOrErr.takeError(); | ||||||||||
1221 | |||||||||||
1222 | return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr, | ||||||||||
1223 | T->getNumElements()); | ||||||||||
1224 | } | ||||||||||
1225 | |||||||||||
1226 | ExpectedType | ||||||||||
1227 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { | ||||||||||
1228 | // FIXME: What happens if we're importing a function without a prototype | ||||||||||
1229 | // into C++? Should we make it variadic? | ||||||||||
1230 | ExpectedType ToReturnTypeOrErr = import(T->getReturnType()); | ||||||||||
1231 | if (!ToReturnTypeOrErr) | ||||||||||
1232 | return ToReturnTypeOrErr.takeError(); | ||||||||||
1233 | |||||||||||
1234 | return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr, | ||||||||||
1235 | T->getExtInfo()); | ||||||||||
1236 | } | ||||||||||
1237 | |||||||||||
1238 | ExpectedType | ||||||||||
1239 | ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { | ||||||||||
1240 | ExpectedType ToReturnTypeOrErr = import(T->getReturnType()); | ||||||||||
1241 | if (!ToReturnTypeOrErr) | ||||||||||
1242 | return ToReturnTypeOrErr.takeError(); | ||||||||||
1243 | |||||||||||
1244 | // Import argument types | ||||||||||
1245 | SmallVector<QualType, 4> ArgTypes; | ||||||||||
1246 | for (const auto &A : T->param_types()) { | ||||||||||
1247 | ExpectedType TyOrErr = import(A); | ||||||||||
1248 | if (!TyOrErr) | ||||||||||
1249 | return TyOrErr.takeError(); | ||||||||||
1250 | ArgTypes.push_back(*TyOrErr); | ||||||||||
1251 | } | ||||||||||
1252 | |||||||||||
1253 | // Import exception types | ||||||||||
1254 | SmallVector<QualType, 4> ExceptionTypes; | ||||||||||
1255 | for (const auto &E : T->exceptions()) { | ||||||||||
1256 | ExpectedType TyOrErr = import(E); | ||||||||||
1257 | if (!TyOrErr) | ||||||||||
1258 | return TyOrErr.takeError(); | ||||||||||
1259 | ExceptionTypes.push_back(*TyOrErr); | ||||||||||
1260 | } | ||||||||||
1261 | |||||||||||
1262 | FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); | ||||||||||
1263 | Error Err = Error::success(); | ||||||||||
1264 | FunctionProtoType::ExtProtoInfo ToEPI; | ||||||||||
1265 | ToEPI.ExtInfo = FromEPI.ExtInfo; | ||||||||||
1266 | ToEPI.Variadic = FromEPI.Variadic; | ||||||||||
1267 | ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; | ||||||||||
1268 | ToEPI.TypeQuals = FromEPI.TypeQuals; | ||||||||||
1269 | ToEPI.RefQualifier = FromEPI.RefQualifier; | ||||||||||
1270 | ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; | ||||||||||
1271 | ToEPI.ExceptionSpec.NoexceptExpr = | ||||||||||
1272 | importChecked(Err, FromEPI.ExceptionSpec.NoexceptExpr); | ||||||||||
1273 | ToEPI.ExceptionSpec.SourceDecl = | ||||||||||
1274 | importChecked(Err, FromEPI.ExceptionSpec.SourceDecl); | ||||||||||
1275 | ToEPI.ExceptionSpec.SourceTemplate = | ||||||||||
1276 | importChecked(Err, FromEPI.ExceptionSpec.SourceTemplate); | ||||||||||
1277 | ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; | ||||||||||
1278 | |||||||||||
1279 | if (Err) | ||||||||||
1280 | return std::move(Err); | ||||||||||
1281 | |||||||||||
1282 | return Importer.getToContext().getFunctionType( | ||||||||||
1283 | *ToReturnTypeOrErr, ArgTypes, ToEPI); | ||||||||||
1284 | } | ||||||||||
1285 | |||||||||||
1286 | ExpectedType ASTNodeImporter::VisitUnresolvedUsingType( | ||||||||||
1287 | const UnresolvedUsingType *T) { | ||||||||||
1288 | Error Err = Error::success(); | ||||||||||
1289 | auto ToD = importChecked(Err, T->getDecl()); | ||||||||||
1290 | auto ToPrevD = importChecked(Err, T->getDecl()->getPreviousDecl()); | ||||||||||
1291 | if (Err) | ||||||||||
1292 | return std::move(Err); | ||||||||||
1293 | |||||||||||
1294 | return Importer.getToContext().getTypeDeclType( | ||||||||||
1295 | ToD, cast_or_null<TypeDecl>(ToPrevD)); | ||||||||||
1296 | } | ||||||||||
1297 | |||||||||||
1298 | ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) { | ||||||||||
1299 | ExpectedType ToInnerTypeOrErr = import(T->getInnerType()); | ||||||||||
1300 | if (!ToInnerTypeOrErr) | ||||||||||
1301 | return ToInnerTypeOrErr.takeError(); | ||||||||||
1302 | |||||||||||
1303 | return Importer.getToContext().getParenType(*ToInnerTypeOrErr); | ||||||||||
1304 | } | ||||||||||
1305 | |||||||||||
1306 | ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { | ||||||||||
1307 | Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl()); | ||||||||||
1308 | if (!ToDeclOrErr) | ||||||||||
1309 | return ToDeclOrErr.takeError(); | ||||||||||
1310 | |||||||||||
1311 | return Importer.getToContext().getTypeDeclType(*ToDeclOrErr); | ||||||||||
1312 | } | ||||||||||
1313 | |||||||||||
1314 | ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { | ||||||||||
1315 | ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr()); | ||||||||||
1316 | if (!ToExprOrErr) | ||||||||||
1317 | return ToExprOrErr.takeError(); | ||||||||||
1318 | |||||||||||
1319 | return Importer.getToContext().getTypeOfExprType(*ToExprOrErr); | ||||||||||
1320 | } | ||||||||||
1321 | |||||||||||
1322 | ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { | ||||||||||
1323 | ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType()); | ||||||||||
1324 | if (!ToUnderlyingTypeOrErr) | ||||||||||
1325 | return ToUnderlyingTypeOrErr.takeError(); | ||||||||||
1326 | |||||||||||
1327 | return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr); | ||||||||||
1328 | } | ||||||||||
1329 | |||||||||||
1330 | ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { | ||||||||||
1331 | // FIXME: Make sure that the "to" context supports C++0x! | ||||||||||
1332 | ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr()); | ||||||||||
1333 | if (!ToExprOrErr) | ||||||||||
1334 | return ToExprOrErr.takeError(); | ||||||||||
1335 | |||||||||||
1336 | ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType()); | ||||||||||
1337 | if (!ToUnderlyingTypeOrErr) | ||||||||||
1338 | return ToUnderlyingTypeOrErr.takeError(); | ||||||||||
1339 | |||||||||||
1340 | return Importer.getToContext().getDecltypeType( | ||||||||||
1341 | *ToExprOrErr, *ToUnderlyingTypeOrErr); | ||||||||||
1342 | } | ||||||||||
1343 | |||||||||||
1344 | ExpectedType | ||||||||||
1345 | ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { | ||||||||||
1346 | ExpectedType ToBaseTypeOrErr = import(T->getBaseType()); | ||||||||||
1347 | if (!ToBaseTypeOrErr) | ||||||||||
1348 | return ToBaseTypeOrErr.takeError(); | ||||||||||
1349 | |||||||||||
1350 | ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType()); | ||||||||||
1351 | if (!ToUnderlyingTypeOrErr) | ||||||||||
1352 | return ToUnderlyingTypeOrErr.takeError(); | ||||||||||
1353 | |||||||||||
1354 | return Importer.getToContext().getUnaryTransformType( | ||||||||||
1355 | *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind()); | ||||||||||
1356 | } | ||||||||||
1357 | |||||||||||
1358 | ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) { | ||||||||||
1359 | // FIXME: Make sure that the "to" context supports C++11! | ||||||||||
1360 | ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType()); | ||||||||||
1361 | if (!ToDeducedTypeOrErr) | ||||||||||
1362 | return ToDeducedTypeOrErr.takeError(); | ||||||||||
1363 | |||||||||||
1364 | ExpectedDecl ToTypeConstraintConcept = import(T->getTypeConstraintConcept()); | ||||||||||
1365 | if (!ToTypeConstraintConcept) | ||||||||||
1366 | return ToTypeConstraintConcept.takeError(); | ||||||||||
1367 | |||||||||||
1368 | SmallVector<TemplateArgument, 2> ToTemplateArgs; | ||||||||||
1369 | ArrayRef<TemplateArgument> FromTemplateArgs = T->getTypeConstraintArguments(); | ||||||||||
1370 | if (Error Err = ImportTemplateArguments(FromTemplateArgs.data(), | ||||||||||
1371 | FromTemplateArgs.size(), | ||||||||||
1372 | ToTemplateArgs)) | ||||||||||
1373 | return std::move(Err); | ||||||||||
1374 | |||||||||||
1375 | return Importer.getToContext().getAutoType( | ||||||||||
1376 | *ToDeducedTypeOrErr, T->getKeyword(), /*IsDependent*/false, | ||||||||||
1377 | /*IsPack=*/false, cast_or_null<ConceptDecl>(*ToTypeConstraintConcept), | ||||||||||
1378 | ToTemplateArgs); | ||||||||||
1379 | } | ||||||||||
1380 | |||||||||||
1381 | ExpectedType ASTNodeImporter::VisitDeducedTemplateSpecializationType( | ||||||||||
1382 | const DeducedTemplateSpecializationType *T) { | ||||||||||
1383 | // FIXME: Make sure that the "to" context supports C++17! | ||||||||||
1384 | Expected<TemplateName> ToTemplateNameOrErr = import(T->getTemplateName()); | ||||||||||
1385 | if (!ToTemplateNameOrErr) | ||||||||||
1386 | return ToTemplateNameOrErr.takeError(); | ||||||||||
1387 | ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType()); | ||||||||||
1388 | if (!ToDeducedTypeOrErr) | ||||||||||
1389 | return ToDeducedTypeOrErr.takeError(); | ||||||||||
1390 | |||||||||||
1391 | return Importer.getToContext().getDeducedTemplateSpecializationType( | ||||||||||
1392 | *ToTemplateNameOrErr, *ToDeducedTypeOrErr, T->isDependentType()); | ||||||||||
1393 | } | ||||||||||
1394 | |||||||||||
1395 | ExpectedType ASTNodeImporter::VisitInjectedClassNameType( | ||||||||||
1396 | const InjectedClassNameType *T) { | ||||||||||
1397 | Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl()); | ||||||||||
1398 | if (!ToDeclOrErr) | ||||||||||
1399 | return ToDeclOrErr.takeError(); | ||||||||||
1400 | |||||||||||
1401 | ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType()); | ||||||||||
1402 | if (!ToInjTypeOrErr) | ||||||||||
1403 | return ToInjTypeOrErr.takeError(); | ||||||||||
1404 | |||||||||||
1405 | // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading | ||||||||||
1406 | // See comments in InjectedClassNameType definition for details | ||||||||||
1407 | // return Importer.getToContext().getInjectedClassNameType(D, InjType); | ||||||||||
1408 | enum { | ||||||||||
1409 | TypeAlignmentInBits = 4, | ||||||||||
1410 | TypeAlignment = 1 << TypeAlignmentInBits | ||||||||||
1411 | }; | ||||||||||
1412 | |||||||||||
1413 | return QualType(new (Importer.getToContext(), TypeAlignment) | ||||||||||
1414 | InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0); | ||||||||||
1415 | } | ||||||||||
1416 | |||||||||||
1417 | ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) { | ||||||||||
1418 | Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl()); | ||||||||||
1419 | if (!ToDeclOrErr) | ||||||||||
1420 | return ToDeclOrErr.takeError(); | ||||||||||
1421 | |||||||||||
1422 | return Importer.getToContext().getTagDeclType(*ToDeclOrErr); | ||||||||||
1423 | } | ||||||||||
1424 | |||||||||||
1425 | ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) { | ||||||||||
1426 | Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl()); | ||||||||||
1427 | if (!ToDeclOrErr) | ||||||||||
1428 | return ToDeclOrErr.takeError(); | ||||||||||
1429 | |||||||||||
1430 | return Importer.getToContext().getTagDeclType(*ToDeclOrErr); | ||||||||||
1431 | } | ||||||||||
1432 | |||||||||||
1433 | ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { | ||||||||||
1434 | ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType()); | ||||||||||
1435 | if (!ToModifiedTypeOrErr) | ||||||||||
1436 | return ToModifiedTypeOrErr.takeError(); | ||||||||||
1437 | ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType()); | ||||||||||
1438 | if (!ToEquivalentTypeOrErr) | ||||||||||
1439 | return ToEquivalentTypeOrErr.takeError(); | ||||||||||
1440 | |||||||||||
1441 | return Importer.getToContext().getAttributedType(T->getAttrKind(), | ||||||||||
1442 | *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr); | ||||||||||
1443 | } | ||||||||||
1444 | |||||||||||
1445 | ExpectedType ASTNodeImporter::VisitTemplateTypeParmType( | ||||||||||
1446 | const TemplateTypeParmType *T) { | ||||||||||
1447 | Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl()); | ||||||||||
1448 | if (!ToDeclOrErr) | ||||||||||
1449 | return ToDeclOrErr.takeError(); | ||||||||||
1450 | |||||||||||
1451 | return Importer.getToContext().getTemplateTypeParmType( | ||||||||||
1452 | T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr); | ||||||||||
1453 | } | ||||||||||
1454 | |||||||||||
1455 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType( | ||||||||||
1456 | const SubstTemplateTypeParmType *T) { | ||||||||||
1457 | ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0)); | ||||||||||
1458 | if (!ReplacedOrErr) | ||||||||||
1459 | return ReplacedOrErr.takeError(); | ||||||||||
1460 | const TemplateTypeParmType *Replaced = | ||||||||||
1461 | cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr()); | ||||||||||
1462 | |||||||||||
1463 | ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType()); | ||||||||||
1464 | if (!ToReplacementTypeOrErr) | ||||||||||
1465 | return ToReplacementTypeOrErr.takeError(); | ||||||||||
1466 | |||||||||||
1467 | return Importer.getToContext().getSubstTemplateTypeParmType( | ||||||||||
1468 | Replaced, (*ToReplacementTypeOrErr).getCanonicalType()); | ||||||||||
1469 | } | ||||||||||
1470 | |||||||||||
1471 | ExpectedType ASTNodeImporter::VisitTemplateSpecializationType( | ||||||||||
1472 | const TemplateSpecializationType *T) { | ||||||||||
1473 | auto ToTemplateOrErr = import(T->getTemplateName()); | ||||||||||
1474 | if (!ToTemplateOrErr) | ||||||||||
1475 | return ToTemplateOrErr.takeError(); | ||||||||||
1476 | |||||||||||
1477 | SmallVector<TemplateArgument, 2> ToTemplateArgs; | ||||||||||
1478 | if (Error Err = ImportTemplateArguments( | ||||||||||
1479 | T->getArgs(), T->getNumArgs(), ToTemplateArgs)) | ||||||||||
1480 | return std::move(Err); | ||||||||||
1481 | |||||||||||
1482 | QualType ToCanonType; | ||||||||||
1483 | if (!QualType(T, 0).isCanonical()) { | ||||||||||
1484 | QualType FromCanonType | ||||||||||
1485 | = Importer.getFromContext().getCanonicalType(QualType(T, 0)); | ||||||||||
1486 | if (ExpectedType TyOrErr = import(FromCanonType)) | ||||||||||
1487 | ToCanonType = *TyOrErr; | ||||||||||
1488 | else | ||||||||||
1489 | return TyOrErr.takeError(); | ||||||||||
1490 | } | ||||||||||
1491 | return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr, | ||||||||||
1492 | ToTemplateArgs, | ||||||||||
1493 | ToCanonType); | ||||||||||
1494 | } | ||||||||||
1495 | |||||||||||
1496 | ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { | ||||||||||
1497 | // Note: the qualifier in an ElaboratedType is optional. | ||||||||||
1498 | auto ToQualifierOrErr = import(T->getQualifier()); | ||||||||||
1499 | if (!ToQualifierOrErr) | ||||||||||
1500 | return ToQualifierOrErr.takeError(); | ||||||||||
1501 | |||||||||||
1502 | ExpectedType ToNamedTypeOrErr = import(T->getNamedType()); | ||||||||||
1503 | if (!ToNamedTypeOrErr) | ||||||||||
1504 | return ToNamedTypeOrErr.takeError(); | ||||||||||
1505 | |||||||||||
1506 | Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl()); | ||||||||||
1507 | if (!ToOwnedTagDeclOrErr) | ||||||||||
1508 | return ToOwnedTagDeclOrErr.takeError(); | ||||||||||
1509 | |||||||||||
1510 | return Importer.getToContext().getElaboratedType(T->getKeyword(), | ||||||||||
1511 | *ToQualifierOrErr, | ||||||||||
1512 | *ToNamedTypeOrErr, | ||||||||||
1513 | *ToOwnedTagDeclOrErr); | ||||||||||
1514 | } | ||||||||||
1515 | |||||||||||
1516 | ExpectedType | ||||||||||
1517 | ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) { | ||||||||||
1518 | ExpectedType ToPatternOrErr = import(T->getPattern()); | ||||||||||
1519 | if (!ToPatternOrErr) | ||||||||||
1520 | return ToPatternOrErr.takeError(); | ||||||||||
1521 | |||||||||||
1522 | return Importer.getToContext().getPackExpansionType(*ToPatternOrErr, | ||||||||||
1523 | T->getNumExpansions(), | ||||||||||
1524 | /*ExpactPack=*/false); | ||||||||||
1525 | } | ||||||||||
1526 | |||||||||||
1527 | ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType( | ||||||||||
1528 | const DependentTemplateSpecializationType *T) { | ||||||||||
1529 | auto ToQualifierOrErr = import(T->getQualifier()); | ||||||||||
1530 | if (!ToQualifierOrErr) | ||||||||||
1531 | return ToQualifierOrErr.takeError(); | ||||||||||
1532 | |||||||||||
1533 | IdentifierInfo *ToName = Importer.Import(T->getIdentifier()); | ||||||||||
1534 | |||||||||||
1535 | SmallVector<TemplateArgument, 2> ToPack; | ||||||||||
1536 | ToPack.reserve(T->getNumArgs()); | ||||||||||
1537 | if (Error Err = ImportTemplateArguments( | ||||||||||
1538 | T->getArgs(), T->getNumArgs(), ToPack)) | ||||||||||
1539 | return std::move(Err); | ||||||||||
1540 | |||||||||||
1541 | return Importer.getToContext().getDependentTemplateSpecializationType( | ||||||||||
1542 | T->getKeyword(), *ToQualifierOrErr, ToName, ToPack); | ||||||||||
1543 | } | ||||||||||
1544 | |||||||||||
1545 | ExpectedType | ||||||||||
1546 | ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) { | ||||||||||
1547 | auto ToQualifierOrErr = import(T->getQualifier()); | ||||||||||
1548 | if (!ToQualifierOrErr) | ||||||||||
1549 | return ToQualifierOrErr.takeError(); | ||||||||||
1550 | |||||||||||
1551 | IdentifierInfo *Name = Importer.Import(T->getIdentifier()); | ||||||||||
1552 | |||||||||||
1553 | QualType Canon; | ||||||||||
1554 | if (T != T->getCanonicalTypeInternal().getTypePtr()) { | ||||||||||
1555 | if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal())) | ||||||||||
1556 | Canon = (*TyOrErr).getCanonicalType(); | ||||||||||
1557 | else | ||||||||||
1558 | return TyOrErr.takeError(); | ||||||||||
1559 | } | ||||||||||
1560 | |||||||||||
1561 | return Importer.getToContext().getDependentNameType(T->getKeyword(), | ||||||||||
1562 | *ToQualifierOrErr, | ||||||||||
1563 | Name, Canon); | ||||||||||
1564 | } | ||||||||||
1565 | |||||||||||
1566 | ExpectedType | ||||||||||
1567 | ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { | ||||||||||
1568 | Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl()); | ||||||||||
1569 | if (!ToDeclOrErr) | ||||||||||
1570 | return ToDeclOrErr.takeError(); | ||||||||||
1571 | |||||||||||
1572 | return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr); | ||||||||||
1573 | } | ||||||||||
1574 | |||||||||||
1575 | ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { | ||||||||||
1576 | ExpectedType ToBaseTypeOrErr = import(T->getBaseType()); | ||||||||||
1577 | if (!ToBaseTypeOrErr) | ||||||||||
1578 | return ToBaseTypeOrErr.takeError(); | ||||||||||
1579 | |||||||||||
1580 | SmallVector<QualType, 4> TypeArgs; | ||||||||||
1581 | for (auto TypeArg : T->getTypeArgsAsWritten()) { | ||||||||||
1582 | if (ExpectedType TyOrErr = import(TypeArg)) | ||||||||||
1583 | TypeArgs.push_back(*TyOrErr); | ||||||||||
1584 | else | ||||||||||
1585 | return TyOrErr.takeError(); | ||||||||||
1586 | } | ||||||||||
1587 | |||||||||||
1588 | SmallVector<ObjCProtocolDecl *, 4> Protocols; | ||||||||||
1589 | for (auto *P : T->quals()) { | ||||||||||
1590 | if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P)) | ||||||||||
1591 | Protocols.push_back(*ProtocolOrErr); | ||||||||||
1592 | else | ||||||||||
1593 | return ProtocolOrErr.takeError(); | ||||||||||
1594 | |||||||||||
1595 | } | ||||||||||
1596 | |||||||||||
1597 | return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs, | ||||||||||
1598 | Protocols, | ||||||||||
1599 | T->isKindOfTypeAsWritten()); | ||||||||||
1600 | } | ||||||||||
1601 | |||||||||||
1602 | ExpectedType | ||||||||||
1603 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { | ||||||||||
1604 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType()); | ||||||||||
1605 | if (!ToPointeeTypeOrErr) | ||||||||||
1606 | return ToPointeeTypeOrErr.takeError(); | ||||||||||
1607 | |||||||||||
1608 | return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr); | ||||||||||
1609 | } | ||||||||||
1610 | |||||||||||
1611 | //---------------------------------------------------------------------------- | ||||||||||
1612 | // Import Declarations | ||||||||||
1613 | //---------------------------------------------------------------------------- | ||||||||||
1614 | Error ASTNodeImporter::ImportDeclParts( | ||||||||||
1615 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, | ||||||||||
1616 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) { | ||||||||||
1617 | // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop. | ||||||||||
1618 | // example: int struct_in_proto(struct data_t{int a;int b;} *d); | ||||||||||
1619 | // FIXME: We could support these constructs by importing a different type of | ||||||||||
1620 | // this parameter and by importing the original type of the parameter only | ||||||||||
1621 | // after the FunctionDecl is created. See | ||||||||||
1622 | // VisitFunctionDecl::UsedDifferentProtoType. | ||||||||||
1623 | DeclContext *OrigDC = D->getDeclContext(); | ||||||||||
1624 | FunctionDecl *FunDecl; | ||||||||||
1625 | if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) && | ||||||||||
1626 | FunDecl->hasBody()) { | ||||||||||
1627 | auto getLeafPointeeType = [](const Type *T) { | ||||||||||
1628 | while (T->isPointerType() || T->isArrayType()) { | ||||||||||
1629 | T = T->getPointeeOrArrayElementType(); | ||||||||||
1630 | } | ||||||||||
1631 | return T; | ||||||||||
1632 | }; | ||||||||||
1633 | for (const ParmVarDecl *P : FunDecl->parameters()) { | ||||||||||
1634 | const Type *LeafT = | ||||||||||
1635 | getLeafPointeeType(P->getType().getCanonicalType().getTypePtr()); | ||||||||||
1636 | auto *RT = dyn_cast<RecordType>(LeafT); | ||||||||||
1637 | if (RT && RT->getDecl() == D) { | ||||||||||
1638 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) | ||||||||||
1639 | << D->getDeclKindName(); | ||||||||||
1640 | return make_error<ImportError>(ImportError::UnsupportedConstruct); | ||||||||||
1641 | } | ||||||||||
1642 | } | ||||||||||
1643 | } | ||||||||||
1644 | |||||||||||
1645 | // Import the context of this declaration. | ||||||||||
1646 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | ||||||||||
1647 | return Err; | ||||||||||
1648 | |||||||||||
1649 | // Import the name of this declaration. | ||||||||||
1650 | if (Error Err = importInto(Name, D->getDeclName())) | ||||||||||
1651 | return Err; | ||||||||||
1652 | |||||||||||
1653 | // Import the location of this declaration. | ||||||||||
1654 | if (Error Err = importInto(Loc, D->getLocation())) | ||||||||||
1655 | return Err; | ||||||||||
1656 | |||||||||||
1657 | ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); | ||||||||||
1658 | if (ToD
| ||||||||||
1659 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD)) | ||||||||||
1660 | return Err; | ||||||||||
1661 | |||||||||||
1662 | return Error::success(); | ||||||||||
1663 | } | ||||||||||
1664 | |||||||||||
1665 | Error ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclarationName &Name, | ||||||||||
1666 | NamedDecl *&ToD, SourceLocation &Loc) { | ||||||||||
1667 | |||||||||||
1668 | // Import the name of this declaration. | ||||||||||
1669 | if (Error Err = importInto(Name, D->getDeclName())) | ||||||||||
1670 | return Err; | ||||||||||
1671 | |||||||||||
1672 | // Import the location of this declaration. | ||||||||||
1673 | if (Error Err = importInto(Loc, D->getLocation())) | ||||||||||
1674 | return Err; | ||||||||||
1675 | |||||||||||
1676 | ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); | ||||||||||
1677 | if (ToD) | ||||||||||
1678 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD)) | ||||||||||
1679 | return Err; | ||||||||||
1680 | |||||||||||
1681 | return Error::success(); | ||||||||||
1682 | } | ||||||||||
1683 | |||||||||||
1684 | Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { | ||||||||||
1685 | if (!FromD) | ||||||||||
1686 | return Error::success(); | ||||||||||
1687 | |||||||||||
1688 | if (!ToD) | ||||||||||
1689 | if (Error Err = importInto(ToD, FromD)) | ||||||||||
1690 | return Err; | ||||||||||
1691 | |||||||||||
1692 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) { | ||||||||||
1693 | if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) { | ||||||||||
1694 | if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && | ||||||||||
1695 | !ToRecord->getDefinition()) { | ||||||||||
1696 | if (Error Err = ImportDefinition(FromRecord, ToRecord)) | ||||||||||
1697 | return Err; | ||||||||||
1698 | } | ||||||||||
1699 | } | ||||||||||
1700 | return Error::success(); | ||||||||||
1701 | } | ||||||||||
1702 | |||||||||||
1703 | if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) { | ||||||||||
1704 | if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) { | ||||||||||
1705 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { | ||||||||||
1706 | if (Error Err = ImportDefinition(FromEnum, ToEnum)) | ||||||||||
1707 | return Err; | ||||||||||
1708 | } | ||||||||||
1709 | } | ||||||||||
1710 | return Error::success(); | ||||||||||
1711 | } | ||||||||||
1712 | |||||||||||
1713 | return Error::success(); | ||||||||||
1714 | } | ||||||||||
1715 | |||||||||||
1716 | Error | ||||||||||
1717 | ASTNodeImporter::ImportDeclarationNameLoc( | ||||||||||
1718 | const DeclarationNameInfo &From, DeclarationNameInfo& To) { | ||||||||||
1719 | // NOTE: To.Name and To.Loc are already imported. | ||||||||||
1720 | // We only have to import To.LocInfo. | ||||||||||
1721 | switch (To.getName().getNameKind()) { | ||||||||||
1722 | case DeclarationName::Identifier: | ||||||||||
1723 | case DeclarationName::ObjCZeroArgSelector: | ||||||||||
1724 | case DeclarationName::ObjCOneArgSelector: | ||||||||||
1725 | case DeclarationName::ObjCMultiArgSelector: | ||||||||||
1726 | case DeclarationName::CXXUsingDirective: | ||||||||||
1727 | case DeclarationName::CXXDeductionGuideName: | ||||||||||
1728 | return Error::success(); | ||||||||||
1729 | |||||||||||
1730 | case DeclarationName::CXXOperatorName: { | ||||||||||
1731 | if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange())) | ||||||||||
1732 | To.setCXXOperatorNameRange(*ToRangeOrErr); | ||||||||||
1733 | else | ||||||||||
1734 | return ToRangeOrErr.takeError(); | ||||||||||
1735 | return Error::success(); | ||||||||||
1736 | } | ||||||||||
1737 | case DeclarationName::CXXLiteralOperatorName: { | ||||||||||
1738 | if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc())) | ||||||||||
1739 | To.setCXXLiteralOperatorNameLoc(*LocOrErr); | ||||||||||
1740 | else | ||||||||||
1741 | return LocOrErr.takeError(); | ||||||||||
1742 | return Error::success(); | ||||||||||
1743 | } | ||||||||||
1744 | case DeclarationName::CXXConstructorName: | ||||||||||
1745 | case DeclarationName::CXXDestructorName: | ||||||||||
1746 | case DeclarationName::CXXConversionFunctionName: { | ||||||||||
1747 | if (auto ToTInfoOrErr = import(From.getNamedTypeInfo())) | ||||||||||
1748 | To.setNamedTypeInfo(*ToTInfoOrErr); | ||||||||||
1749 | else | ||||||||||
1750 | return ToTInfoOrErr.takeError(); | ||||||||||
1751 | return Error::success(); | ||||||||||
1752 | } | ||||||||||
1753 | } | ||||||||||
1754 | llvm_unreachable("Unknown name kind.")::llvm::llvm_unreachable_internal("Unknown name kind.", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 1754); | ||||||||||
1755 | } | ||||||||||
1756 | |||||||||||
1757 | Error | ||||||||||
1758 | ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { | ||||||||||
1759 | if (Importer.isMinimalImport() && !ForceImport) { | ||||||||||
1760 | auto ToDCOrErr = Importer.ImportContext(FromDC); | ||||||||||
1761 | return ToDCOrErr.takeError(); | ||||||||||
1762 | } | ||||||||||
1763 | |||||||||||
1764 | // We use strict error handling in case of records and enums, but not | ||||||||||
1765 | // with e.g. namespaces. | ||||||||||
1766 | // | ||||||||||
1767 | // FIXME Clients of the ASTImporter should be able to choose an | ||||||||||
1768 | // appropriate error handling strategy for their needs. For instance, | ||||||||||
1769 | // they may not want to mark an entire namespace as erroneous merely | ||||||||||
1770 | // because there is an ODR error with two typedefs. As another example, | ||||||||||
1771 | // the client may allow EnumConstantDecls with same names but with | ||||||||||
1772 | // different values in two distinct translation units. | ||||||||||
1773 | bool AccumulateChildErrors = isa<TagDecl>(FromDC); | ||||||||||
1774 | |||||||||||
1775 | Error ChildErrors = Error::success(); | ||||||||||
1776 | for (auto *From : FromDC->decls()) { | ||||||||||
1777 | ExpectedDecl ImportedOrErr = import(From); | ||||||||||
1778 | |||||||||||
1779 | // If we are in the process of ImportDefinition(...) for a RecordDecl we | ||||||||||
1780 | // want to make sure that we are also completing each FieldDecl. There | ||||||||||
1781 | // are currently cases where this does not happen and this is correctness | ||||||||||
1782 | // fix since operations such as code generation will expect this to be so. | ||||||||||
1783 | if (ImportedOrErr) { | ||||||||||
1784 | FieldDecl *FieldFrom = dyn_cast_or_null<FieldDecl>(From); | ||||||||||
1785 | Decl *ImportedDecl = *ImportedOrErr; | ||||||||||
1786 | FieldDecl *FieldTo = dyn_cast_or_null<FieldDecl>(ImportedDecl); | ||||||||||
1787 | if (FieldFrom && FieldTo) { | ||||||||||
1788 | RecordDecl *FromRecordDecl = nullptr; | ||||||||||
1789 | RecordDecl *ToRecordDecl = nullptr; | ||||||||||
1790 | // If we have a field that is an ArrayType we need to check if the array | ||||||||||
1791 | // element is a RecordDecl and if so we need to import the defintion. | ||||||||||
1792 | if (FieldFrom->getType()->isArrayType()) { | ||||||||||
1793 | // getBaseElementTypeUnsafe(...) handles multi-dimensonal arrays for us. | ||||||||||
1794 | FromRecordDecl = FieldFrom->getType()->getBaseElementTypeUnsafe()->getAsRecordDecl(); | ||||||||||
1795 | ToRecordDecl = FieldTo->getType()->getBaseElementTypeUnsafe()->getAsRecordDecl(); | ||||||||||
1796 | } | ||||||||||
1797 | |||||||||||
1798 | if (!FromRecordDecl || !ToRecordDecl) { | ||||||||||
1799 | const RecordType *RecordFrom = | ||||||||||
1800 | FieldFrom->getType()->getAs<RecordType>(); | ||||||||||
1801 | const RecordType *RecordTo = FieldTo->getType()->getAs<RecordType>(); | ||||||||||
1802 | |||||||||||
1803 | if (RecordFrom && RecordTo) { | ||||||||||
1804 | FromRecordDecl = RecordFrom->getDecl(); | ||||||||||
1805 | ToRecordDecl = RecordTo->getDecl(); | ||||||||||
1806 | } | ||||||||||
1807 | } | ||||||||||
1808 | |||||||||||
1809 | if (FromRecordDecl && ToRecordDecl) { | ||||||||||
1810 | if (FromRecordDecl->isCompleteDefinition() && | ||||||||||
1811 | !ToRecordDecl->isCompleteDefinition()) { | ||||||||||
1812 | Error Err = ImportDefinition(FromRecordDecl, ToRecordDecl); | ||||||||||
1813 | |||||||||||
1814 | if (Err && AccumulateChildErrors) | ||||||||||
1815 | ChildErrors = joinErrors(std::move(ChildErrors), std::move(Err)); | ||||||||||
1816 | else | ||||||||||
1817 | consumeError(std::move(Err)); | ||||||||||
1818 | } | ||||||||||
1819 | } | ||||||||||
1820 | } | ||||||||||
1821 | } else { | ||||||||||
1822 | if (AccumulateChildErrors) | ||||||||||
1823 | ChildErrors = | ||||||||||
1824 | joinErrors(std::move(ChildErrors), ImportedOrErr.takeError()); | ||||||||||
1825 | else | ||||||||||
1826 | consumeError(ImportedOrErr.takeError()); | ||||||||||
1827 | } | ||||||||||
1828 | } | ||||||||||
1829 | |||||||||||
1830 | // We reorder declarations in RecordDecls because they may have another order | ||||||||||
1831 | // in the "to" context than they have in the "from" context. This may happen | ||||||||||
1832 | // e.g when we import a class like this: | ||||||||||
1833 | // struct declToImport { | ||||||||||
1834 | // int a = c + b; | ||||||||||
1835 | // int b = 1; | ||||||||||
1836 | // int c = 2; | ||||||||||
1837 | // }; | ||||||||||
1838 | // During the import of `a` we import first the dependencies in sequence, | ||||||||||
1839 | // thus the order would be `c`, `b`, `a`. We will get the normal order by | ||||||||||
1840 | // first removing the already imported members and then adding them in the | ||||||||||
1841 | // order as they apper in the "from" context. | ||||||||||
1842 | // | ||||||||||
1843 | // Keeping field order is vital because it determines structure layout. | ||||||||||
1844 | // | ||||||||||
1845 | // Here and below, we cannot call field_begin() method and its callers on | ||||||||||
1846 | // ToDC if it has an external storage. Calling field_begin() will | ||||||||||
1847 | // automatically load all the fields by calling | ||||||||||
1848 | // LoadFieldsFromExternalStorage(). LoadFieldsFromExternalStorage() would | ||||||||||
1849 | // call ASTImporter::Import(). This is because the ExternalASTSource | ||||||||||
1850 | // interface in LLDB is implemented by the means of the ASTImporter. However, | ||||||||||
1851 | // calling an import at this point would result in an uncontrolled import, we | ||||||||||
1852 | // must avoid that. | ||||||||||
1853 | const auto *FromRD = dyn_cast<RecordDecl>(FromDC); | ||||||||||
1854 | if (!FromRD) | ||||||||||
1855 | return ChildErrors; | ||||||||||
1856 | |||||||||||
1857 | auto ToDCOrErr = Importer.ImportContext(FromDC); | ||||||||||
1858 | if (!ToDCOrErr) { | ||||||||||
1859 | consumeError(std::move(ChildErrors)); | ||||||||||
1860 | return ToDCOrErr.takeError(); | ||||||||||
1861 | } | ||||||||||
1862 | |||||||||||
1863 | DeclContext *ToDC = *ToDCOrErr; | ||||||||||
1864 | // Remove all declarations, which may be in wrong order in the | ||||||||||
1865 | // lexical DeclContext and then add them in the proper order. | ||||||||||
1866 | for (auto *D : FromRD->decls()) { | ||||||||||
1867 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<FriendDecl>(D)) { | ||||||||||
1868 | assert(D && "DC contains a null decl")((D && "DC contains a null decl") ? static_cast<void > (0) : __assert_fail ("D && \"DC contains a null decl\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 1868, __PRETTY_FUNCTION__)); | ||||||||||
1869 | Decl *ToD = Importer.GetAlreadyImportedOrNull(D); | ||||||||||
1870 | // Remove only the decls which we successfully imported. | ||||||||||
1871 | if (ToD) { | ||||||||||
1872 | assert(ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD))((ToDC == ToD->getLexicalDeclContext() && ToDC-> containsDecl(ToD)) ? static_cast<void> (0) : __assert_fail ("ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD)" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 1872, __PRETTY_FUNCTION__)); | ||||||||||
1873 | // Remove the decl from its wrong place in the linked list. | ||||||||||
1874 | ToDC->removeDecl(ToD); | ||||||||||
1875 | // Add the decl to the end of the linked list. | ||||||||||
1876 | // This time it will be at the proper place because the enclosing for | ||||||||||
1877 | // loop iterates in the original (good) order of the decls. | ||||||||||
1878 | ToDC->addDeclInternal(ToD); | ||||||||||
1879 | } | ||||||||||
1880 | } | ||||||||||
1881 | } | ||||||||||
1882 | |||||||||||
1883 | return ChildErrors; | ||||||||||
1884 | } | ||||||||||
1885 | |||||||||||
1886 | Error ASTNodeImporter::ImportDeclContext( | ||||||||||
1887 | Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) { | ||||||||||
1888 | auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext()); | ||||||||||
1889 | if (!ToDCOrErr) | ||||||||||
1890 | return ToDCOrErr.takeError(); | ||||||||||
1891 | ToDC = *ToDCOrErr; | ||||||||||
1892 | |||||||||||
1893 | if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) { | ||||||||||
1894 | auto ToLexicalDCOrErr = Importer.ImportContext( | ||||||||||
1895 | FromD->getLexicalDeclContext()); | ||||||||||
1896 | if (!ToLexicalDCOrErr) | ||||||||||
1897 | return ToLexicalDCOrErr.takeError(); | ||||||||||
1898 | ToLexicalDC = *ToLexicalDCOrErr; | ||||||||||
1899 | } else | ||||||||||
1900 | ToLexicalDC = ToDC; | ||||||||||
1901 | |||||||||||
1902 | return Error::success(); | ||||||||||
1903 | } | ||||||||||
1904 | |||||||||||
1905 | Error ASTNodeImporter::ImportImplicitMethods( | ||||||||||
1906 | const CXXRecordDecl *From, CXXRecordDecl *To) { | ||||||||||
1907 | assert(From->isCompleteDefinition() && To->getDefinition() == To &&((From->isCompleteDefinition() && To->getDefinition () == To && "Import implicit methods to or from non-definition" ) ? static_cast<void> (0) : __assert_fail ("From->isCompleteDefinition() && To->getDefinition() == To && \"Import implicit methods to or from non-definition\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 1908, __PRETTY_FUNCTION__)) | ||||||||||
1908 | "Import implicit methods to or from non-definition")((From->isCompleteDefinition() && To->getDefinition () == To && "Import implicit methods to or from non-definition" ) ? static_cast<void> (0) : __assert_fail ("From->isCompleteDefinition() && To->getDefinition() == To && \"Import implicit methods to or from non-definition\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 1908, __PRETTY_FUNCTION__)); | ||||||||||
1909 | |||||||||||
1910 | for (CXXMethodDecl *FromM : From->methods()) | ||||||||||
1911 | if (FromM->isImplicit()) { | ||||||||||
1912 | Expected<CXXMethodDecl *> ToMOrErr = import(FromM); | ||||||||||
1913 | if (!ToMOrErr) | ||||||||||
1914 | return ToMOrErr.takeError(); | ||||||||||
1915 | } | ||||||||||
1916 | |||||||||||
1917 | return Error::success(); | ||||||||||
1918 | } | ||||||||||
1919 | |||||||||||
1920 | static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To, | ||||||||||
1921 | ASTImporter &Importer) { | ||||||||||
1922 | if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) { | ||||||||||
1923 | if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef)) | ||||||||||
1924 | To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr)); | ||||||||||
1925 | else | ||||||||||
1926 | return ToTypedefOrErr.takeError(); | ||||||||||
1927 | } | ||||||||||
1928 | return Error::success(); | ||||||||||
1929 | } | ||||||||||
1930 | |||||||||||
1931 | Error ASTNodeImporter::ImportDefinition( | ||||||||||
1932 | RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) { | ||||||||||
1933 | auto DefinitionCompleter = [To]() { | ||||||||||
1934 | // There are cases in LLDB when we first import a class without its | ||||||||||
1935 | // members. The class will have DefinitionData, but no members. Then, | ||||||||||
1936 | // importDefinition is called from LLDB, which tries to get the members, so | ||||||||||
1937 | // when we get here, the class already has the DefinitionData set, so we | ||||||||||
1938 | // must unset the CompleteDefinition here to be able to complete again the | ||||||||||
1939 | // definition. | ||||||||||
1940 | To->setCompleteDefinition(false); | ||||||||||
1941 | To->completeDefinition(); | ||||||||||
1942 | }; | ||||||||||
1943 | |||||||||||
1944 | if (To->getDefinition() || To->isBeingDefined()) { | ||||||||||
1945 | if (Kind == IDK_Everything || | ||||||||||
1946 | // In case of lambdas, the class already has a definition ptr set, but | ||||||||||
1947 | // the contained decls are not imported yet. Also, isBeingDefined was | ||||||||||
1948 | // set in CXXRecordDecl::CreateLambda. We must import the contained | ||||||||||
1949 | // decls here and finish the definition. | ||||||||||
1950 | (To->isLambda() && shouldForceImportDeclContext(Kind))) { | ||||||||||
1951 | if (To->isLambda()) { | ||||||||||
1952 | auto *FromCXXRD = cast<CXXRecordDecl>(From); | ||||||||||
1953 | SmallVector<LambdaCapture, 8> ToCaptures; | ||||||||||
1954 | ToCaptures.reserve(FromCXXRD->capture_size()); | ||||||||||
1955 | for (const auto &FromCapture : FromCXXRD->captures()) { | ||||||||||
1956 | if (auto ToCaptureOrErr = import(FromCapture)) | ||||||||||
1957 | ToCaptures.push_back(*ToCaptureOrErr); | ||||||||||
1958 | else | ||||||||||
1959 | return ToCaptureOrErr.takeError(); | ||||||||||
1960 | } | ||||||||||
1961 | cast<CXXRecordDecl>(To)->setCaptures(Importer.getToContext(), | ||||||||||
1962 | ToCaptures); | ||||||||||
1963 | } | ||||||||||
1964 | |||||||||||
1965 | Error Result = ImportDeclContext(From, /*ForceImport=*/true); | ||||||||||
1966 | // Finish the definition of the lambda, set isBeingDefined to false. | ||||||||||
1967 | if (To->isLambda()) | ||||||||||
1968 | DefinitionCompleter(); | ||||||||||
1969 | return Result; | ||||||||||
1970 | } | ||||||||||
1971 | |||||||||||
1972 | return Error::success(); | ||||||||||
1973 | } | ||||||||||
1974 | |||||||||||
1975 | To->startDefinition(); | ||||||||||
1976 | // Complete the definition even if error is returned. | ||||||||||
1977 | // The RecordDecl may be already part of the AST so it is better to | ||||||||||
1978 | // have it in complete state even if something is wrong with it. | ||||||||||
1979 | auto DefinitionCompleterScopeExit = | ||||||||||
1980 | llvm::make_scope_exit(DefinitionCompleter); | ||||||||||
1981 | |||||||||||
1982 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) | ||||||||||
1983 | return Err; | ||||||||||
1984 | |||||||||||
1985 | // Add base classes. | ||||||||||
1986 | auto *ToCXX = dyn_cast<CXXRecordDecl>(To); | ||||||||||
1987 | auto *FromCXX = dyn_cast<CXXRecordDecl>(From); | ||||||||||
1988 | if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) { | ||||||||||
1989 | |||||||||||
1990 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); | ||||||||||
1991 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); | ||||||||||
1992 | |||||||||||
1993 | #define FIELD(Name, Width, Merge) \ | ||||||||||
1994 | ToData.Name = FromData.Name; | ||||||||||
1995 | #include "clang/AST/CXXRecordDeclDefinitionBits.def" | ||||||||||
1996 | |||||||||||
1997 | // Copy over the data stored in RecordDeclBits | ||||||||||
1998 | ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions()); | ||||||||||
1999 | |||||||||||
2000 | SmallVector<CXXBaseSpecifier *, 4> Bases; | ||||||||||
2001 | for (const auto &Base1 : FromCXX->bases()) { | ||||||||||
2002 | ExpectedType TyOrErr = import(Base1.getType()); | ||||||||||
2003 | if (!TyOrErr) | ||||||||||
2004 | return TyOrErr.takeError(); | ||||||||||
2005 | |||||||||||
2006 | SourceLocation EllipsisLoc; | ||||||||||
2007 | if (Base1.isPackExpansion()) { | ||||||||||
2008 | if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc())) | ||||||||||
2009 | EllipsisLoc = *LocOrErr; | ||||||||||
2010 | else | ||||||||||
2011 | return LocOrErr.takeError(); | ||||||||||
2012 | } | ||||||||||
2013 | |||||||||||
2014 | // Ensure that we have a definition for the base. | ||||||||||
2015 | if (Error Err = | ||||||||||
2016 | ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl())) | ||||||||||
2017 | return Err; | ||||||||||
2018 | |||||||||||
2019 | auto RangeOrErr = import(Base1.getSourceRange()); | ||||||||||
2020 | if (!RangeOrErr) | ||||||||||
2021 | return RangeOrErr.takeError(); | ||||||||||
2022 | |||||||||||
2023 | auto TSIOrErr = import(Base1.getTypeSourceInfo()); | ||||||||||
2024 | if (!TSIOrErr) | ||||||||||
2025 | return TSIOrErr.takeError(); | ||||||||||
2026 | |||||||||||
2027 | Bases.push_back( | ||||||||||
2028 | new (Importer.getToContext()) CXXBaseSpecifier( | ||||||||||
2029 | *RangeOrErr, | ||||||||||
2030 | Base1.isVirtual(), | ||||||||||
2031 | Base1.isBaseOfClass(), | ||||||||||
2032 | Base1.getAccessSpecifierAsWritten(), | ||||||||||
2033 | *TSIOrErr, | ||||||||||
2034 | EllipsisLoc)); | ||||||||||
2035 | } | ||||||||||
2036 | if (!Bases.empty()) | ||||||||||
2037 | ToCXX->setBases(Bases.data(), Bases.size()); | ||||||||||
2038 | } | ||||||||||
2039 | |||||||||||
2040 | if (shouldForceImportDeclContext(Kind)) | ||||||||||
2041 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) | ||||||||||
2042 | return Err; | ||||||||||
2043 | |||||||||||
2044 | return Error::success(); | ||||||||||
2045 | } | ||||||||||
2046 | |||||||||||
2047 | Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) { | ||||||||||
2048 | if (To->getAnyInitializer()) | ||||||||||
2049 | return Error::success(); | ||||||||||
2050 | |||||||||||
2051 | Expr *FromInit = From->getInit(); | ||||||||||
2052 | if (!FromInit) | ||||||||||
2053 | return Error::success(); | ||||||||||
2054 | |||||||||||
2055 | ExpectedExpr ToInitOrErr = import(FromInit); | ||||||||||
2056 | if (!ToInitOrErr) | ||||||||||
2057 | return ToInitOrErr.takeError(); | ||||||||||
2058 | |||||||||||
2059 | To->setInit(*ToInitOrErr); | ||||||||||
2060 | if (EvaluatedStmt *FromEval = From->getEvaluatedStmt()) { | ||||||||||
2061 | EvaluatedStmt *ToEval = To->ensureEvaluatedStmt(); | ||||||||||
2062 | ToEval->HasConstantInitialization = FromEval->HasConstantInitialization; | ||||||||||
2063 | ToEval->HasConstantDestruction = FromEval->HasConstantDestruction; | ||||||||||
2064 | // FIXME: Also import the initializer value. | ||||||||||
2065 | } | ||||||||||
2066 | |||||||||||
2067 | // FIXME: Other bits to merge? | ||||||||||
2068 | return Error::success(); | ||||||||||
2069 | } | ||||||||||
2070 | |||||||||||
2071 | Error ASTNodeImporter::ImportDefinition( | ||||||||||
2072 | EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) { | ||||||||||
2073 | if (To->getDefinition() || To->isBeingDefined()) { | ||||||||||
2074 | if (Kind == IDK_Everything) | ||||||||||
2075 | return ImportDeclContext(From, /*ForceImport=*/true); | ||||||||||
2076 | return Error::success(); | ||||||||||
2077 | } | ||||||||||
2078 | |||||||||||
2079 | To->startDefinition(); | ||||||||||
2080 | |||||||||||
2081 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) | ||||||||||
2082 | return Err; | ||||||||||
2083 | |||||||||||
2084 | ExpectedType ToTypeOrErr = | ||||||||||
2085 | import(Importer.getFromContext().getTypeDeclType(From)); | ||||||||||
2086 | if (!ToTypeOrErr) | ||||||||||
2087 | return ToTypeOrErr.takeError(); | ||||||||||
2088 | |||||||||||
2089 | ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType()); | ||||||||||
2090 | if (!ToPromotionTypeOrErr) | ||||||||||
2091 | return ToPromotionTypeOrErr.takeError(); | ||||||||||
2092 | |||||||||||
2093 | if (shouldForceImportDeclContext(Kind)) | ||||||||||
2094 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) | ||||||||||
2095 | return Err; | ||||||||||
2096 | |||||||||||
2097 | // FIXME: we might need to merge the number of positive or negative bits | ||||||||||
2098 | // if the enumerator lists don't match. | ||||||||||
2099 | To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr, | ||||||||||
2100 | From->getNumPositiveBits(), | ||||||||||
2101 | From->getNumNegativeBits()); | ||||||||||
2102 | return Error::success(); | ||||||||||
2103 | } | ||||||||||
2104 | |||||||||||
2105 | Error ASTNodeImporter::ImportTemplateArguments( | ||||||||||
2106 | const TemplateArgument *FromArgs, unsigned NumFromArgs, | ||||||||||
2107 | SmallVectorImpl<TemplateArgument> &ToArgs) { | ||||||||||
2108 | for (unsigned I = 0; I != NumFromArgs; ++I) { | ||||||||||
2109 | if (auto ToOrErr = import(FromArgs[I])) | ||||||||||
2110 | ToArgs.push_back(*ToOrErr); | ||||||||||
2111 | else | ||||||||||
2112 | return ToOrErr.takeError(); | ||||||||||
2113 | } | ||||||||||
2114 | |||||||||||
2115 | return Error::success(); | ||||||||||
2116 | } | ||||||||||
2117 | |||||||||||
2118 | // FIXME: Do not forget to remove this and use only 'import'. | ||||||||||
2119 | Expected<TemplateArgument> | ||||||||||
2120 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { | ||||||||||
2121 | return import(From); | ||||||||||
2122 | } | ||||||||||
2123 | |||||||||||
2124 | template <typename InContainerTy> | ||||||||||
2125 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( | ||||||||||
2126 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) { | ||||||||||
2127 | for (const auto &FromLoc : Container) { | ||||||||||
2128 | if (auto ToLocOrErr = import(FromLoc)) | ||||||||||
2129 | ToTAInfo.addArgument(*ToLocOrErr); | ||||||||||
2130 | else | ||||||||||
2131 | return ToLocOrErr.takeError(); | ||||||||||
2132 | } | ||||||||||
2133 | return Error::success(); | ||||||||||
2134 | } | ||||||||||
2135 | |||||||||||
2136 | static StructuralEquivalenceKind | ||||||||||
2137 | getStructuralEquivalenceKind(const ASTImporter &Importer) { | ||||||||||
2138 | return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal | ||||||||||
2139 | : StructuralEquivalenceKind::Default; | ||||||||||
2140 | } | ||||||||||
2141 | |||||||||||
2142 | bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) { | ||||||||||
2143 | StructuralEquivalenceContext Ctx( | ||||||||||
2144 | Importer.getFromContext(), Importer.getToContext(), | ||||||||||
2145 | Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), | ||||||||||
2146 | false, Complain); | ||||||||||
2147 | return Ctx.IsEquivalent(From, To); | ||||||||||
2148 | } | ||||||||||
2149 | |||||||||||
2150 | bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, | ||||||||||
2151 | RecordDecl *ToRecord, bool Complain) { | ||||||||||
2152 | // Eliminate a potential failure point where we attempt to re-import | ||||||||||
2153 | // something we're trying to import while completing ToRecord. | ||||||||||
2154 | Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); | ||||||||||
2155 | if (ToOrigin) { | ||||||||||
2156 | auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin); | ||||||||||
2157 | if (ToOriginRecord) | ||||||||||
2158 | ToRecord = ToOriginRecord; | ||||||||||
2159 | } | ||||||||||
2160 | |||||||||||
2161 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), | ||||||||||
2162 | ToRecord->getASTContext(), | ||||||||||
2163 | Importer.getNonEquivalentDecls(), | ||||||||||
2164 | getStructuralEquivalenceKind(Importer), | ||||||||||
2165 | false, Complain); | ||||||||||
2166 | return Ctx.IsEquivalent(FromRecord, ToRecord); | ||||||||||
2167 | } | ||||||||||
2168 | |||||||||||
2169 | bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, | ||||||||||
2170 | bool Complain) { | ||||||||||
2171 | StructuralEquivalenceContext Ctx( | ||||||||||
2172 | Importer.getFromContext(), Importer.getToContext(), | ||||||||||
2173 | Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), | ||||||||||
2174 | false, Complain); | ||||||||||
2175 | return Ctx.IsEquivalent(FromVar, ToVar); | ||||||||||
2176 | } | ||||||||||
2177 | |||||||||||
2178 | bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { | ||||||||||
2179 | // Eliminate a potential failure point where we attempt to re-import | ||||||||||
2180 | // something we're trying to import while completing ToEnum. | ||||||||||
2181 | if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum)) | ||||||||||
2182 | if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin)) | ||||||||||
2183 | ToEnum = ToOriginEnum; | ||||||||||
2184 | |||||||||||
2185 | StructuralEquivalenceContext Ctx( | ||||||||||
2186 | Importer.getFromContext(), Importer.getToContext(), | ||||||||||
2187 | Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer)); | ||||||||||
2188 | return Ctx.IsEquivalent(FromEnum, ToEnum); | ||||||||||
2189 | } | ||||||||||
2190 | |||||||||||
2191 | bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From, | ||||||||||
2192 | FunctionTemplateDecl *To) { | ||||||||||
2193 | StructuralEquivalenceContext Ctx( | ||||||||||
2194 | Importer.getFromContext(), Importer.getToContext(), | ||||||||||
2195 | Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), | ||||||||||
2196 | false, false); | ||||||||||
2197 | return Ctx.IsEquivalent(From, To); | ||||||||||
2198 | } | ||||||||||
2199 | |||||||||||
2200 | bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) { | ||||||||||
2201 | StructuralEquivalenceContext Ctx( | ||||||||||
2202 | Importer.getFromContext(), Importer.getToContext(), | ||||||||||
2203 | Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), | ||||||||||
2204 | false, false); | ||||||||||
2205 | return Ctx.IsEquivalent(From, To); | ||||||||||
2206 | } | ||||||||||
2207 | |||||||||||
2208 | bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, | ||||||||||
2209 | EnumConstantDecl *ToEC) { | ||||||||||
2210 | const llvm::APSInt &FromVal = FromEC->getInitVal(); | ||||||||||
2211 | const llvm::APSInt &ToVal = ToEC->getInitVal(); | ||||||||||
2212 | |||||||||||
2213 | return FromVal.isSigned() == ToVal.isSigned() && | ||||||||||
2214 | FromVal.getBitWidth() == ToVal.getBitWidth() && | ||||||||||
2215 | FromVal == ToVal; | ||||||||||
2216 | } | ||||||||||
2217 | |||||||||||
2218 | bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, | ||||||||||
2219 | ClassTemplateDecl *To) { | ||||||||||
2220 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), | ||||||||||
2221 | Importer.getToContext(), | ||||||||||
2222 | Importer.getNonEquivalentDecls(), | ||||||||||
2223 | getStructuralEquivalenceKind(Importer)); | ||||||||||
2224 | return Ctx.IsEquivalent(From, To); | ||||||||||
2225 | } | ||||||||||
2226 | |||||||||||
2227 | bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, | ||||||||||
2228 | VarTemplateDecl *To) { | ||||||||||
2229 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), | ||||||||||
2230 | Importer.getToContext(), | ||||||||||
2231 | Importer.getNonEquivalentDecls(), | ||||||||||
2232 | getStructuralEquivalenceKind(Importer)); | ||||||||||
2233 | return Ctx.IsEquivalent(From, To); | ||||||||||
2234 | } | ||||||||||
2235 | |||||||||||
2236 | ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) { | ||||||||||
2237 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) | ||||||||||
2238 | << D->getDeclKindName(); | ||||||||||
2239 | return make_error<ImportError>(ImportError::UnsupportedConstruct); | ||||||||||
2240 | } | ||||||||||
2241 | |||||||||||
2242 | ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) { | ||||||||||
2243 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) | ||||||||||
2244 | << D->getDeclKindName(); | ||||||||||
2245 | return make_error<ImportError>(ImportError::UnsupportedConstruct); | ||||||||||
2246 | } | ||||||||||
2247 | |||||||||||
2248 | ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) { | ||||||||||
2249 | // Import the context of this declaration. | ||||||||||
2250 | DeclContext *DC, *LexicalDC; | ||||||||||
2251 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | ||||||||||
2252 | return std::move(Err); | ||||||||||
2253 | |||||||||||
2254 | // Import the location of this declaration. | ||||||||||
2255 | ExpectedSLoc LocOrErr = import(D->getLocation()); | ||||||||||
2256 | if (!LocOrErr) | ||||||||||
2257 | return LocOrErr.takeError(); | ||||||||||
2258 | |||||||||||
2259 | EmptyDecl *ToD; | ||||||||||
2260 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr)) | ||||||||||
2261 | return ToD; | ||||||||||
2262 | |||||||||||
2263 | ToD->setLexicalDeclContext(LexicalDC); | ||||||||||
2264 | LexicalDC->addDeclInternal(ToD); | ||||||||||
2265 | return ToD; | ||||||||||
2266 | } | ||||||||||
2267 | |||||||||||
2268 | ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { | ||||||||||
2269 | TranslationUnitDecl *ToD = | ||||||||||
2270 | Importer.getToContext().getTranslationUnitDecl(); | ||||||||||
2271 | |||||||||||
2272 | Importer.MapImported(D, ToD); | ||||||||||
2273 | |||||||||||
2274 | return ToD; | ||||||||||
2275 | } | ||||||||||
2276 | |||||||||||
2277 | ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { | ||||||||||
2278 | ExpectedSLoc LocOrErr = import(D->getLocation()); | ||||||||||
2279 | if (!LocOrErr) | ||||||||||
2280 | return LocOrErr.takeError(); | ||||||||||
2281 | auto ColonLocOrErr = import(D->getColonLoc()); | ||||||||||
2282 | if (!ColonLocOrErr) | ||||||||||
2283 | return ColonLocOrErr.takeError(); | ||||||||||
2284 | |||||||||||
2285 | // Import the context of this declaration. | ||||||||||
2286 | auto DCOrErr = Importer.ImportContext(D->getDeclContext()); | ||||||||||
2287 | if (!DCOrErr) | ||||||||||
2288 | return DCOrErr.takeError(); | ||||||||||
2289 | DeclContext *DC = *DCOrErr; | ||||||||||
2290 | |||||||||||
2291 | AccessSpecDecl *ToD; | ||||||||||
2292 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(), | ||||||||||
2293 | DC, *LocOrErr, *ColonLocOrErr)) | ||||||||||
2294 | return ToD; | ||||||||||
2295 | |||||||||||
2296 | // Lexical DeclContext and Semantic DeclContext | ||||||||||
2297 | // is always the same for the accessSpec. | ||||||||||
2298 | ToD->setLexicalDeclContext(DC); | ||||||||||
2299 | DC->addDeclInternal(ToD); | ||||||||||
2300 | |||||||||||
2301 | return ToD; | ||||||||||
2302 | } | ||||||||||
2303 | |||||||||||
2304 | ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) { | ||||||||||
2305 | auto DCOrErr = Importer.ImportContext(D->getDeclContext()); | ||||||||||
2306 | if (!DCOrErr) | ||||||||||
2307 | return DCOrErr.takeError(); | ||||||||||
2308 | DeclContext *DC = *DCOrErr; | ||||||||||
2309 | DeclContext *LexicalDC = DC; | ||||||||||
2310 | |||||||||||
2311 | Error Err = Error::success(); | ||||||||||
2312 | auto ToLocation = importChecked(Err, D->getLocation()); | ||||||||||
2313 | auto ToRParenLoc = importChecked(Err, D->getRParenLoc()); | ||||||||||
2314 | auto ToAssertExpr = importChecked(Err, D->getAssertExpr()); | ||||||||||
2315 | auto ToMessage = importChecked(Err, D->getMessage()); | ||||||||||
2316 | if (Err) | ||||||||||
2317 | return std::move(Err); | ||||||||||
2318 | |||||||||||
2319 | StaticAssertDecl *ToD; | ||||||||||
2320 | if (GetImportedOrCreateDecl( | ||||||||||
2321 | ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage, | ||||||||||
2322 | ToRParenLoc, D->isFailed())) | ||||||||||
2323 | return ToD; | ||||||||||
2324 | |||||||||||
2325 | ToD->setLexicalDeclContext(LexicalDC); | ||||||||||
2326 | LexicalDC->addDeclInternal(ToD); | ||||||||||
2327 | return ToD; | ||||||||||
2328 | } | ||||||||||
2329 | |||||||||||
2330 | ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { | ||||||||||
2331 | // Import the major distinguishing characteristics of this namespace. | ||||||||||
2332 | DeclContext *DC, *LexicalDC; | ||||||||||
2333 | DeclarationName Name; | ||||||||||
2334 | SourceLocation Loc; | ||||||||||
2335 | NamedDecl *ToD; | ||||||||||
2336 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
2337 | return std::move(Err); | ||||||||||
2338 | if (ToD) | ||||||||||
2339 | return ToD; | ||||||||||
2340 | |||||||||||
2341 | NamespaceDecl *MergeWithNamespace = nullptr; | ||||||||||
2342 | if (!Name) { | ||||||||||
2343 | // This is an anonymous namespace. Adopt an existing anonymous | ||||||||||
2344 | // namespace if we can. | ||||||||||
2345 | // FIXME: Not testable. | ||||||||||
2346 | if (auto *TU = dyn_cast<TranslationUnitDecl>(DC)) | ||||||||||
2347 | MergeWithNamespace = TU->getAnonymousNamespace(); | ||||||||||
2348 | else | ||||||||||
2349 | MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); | ||||||||||
2350 | } else { | ||||||||||
2351 | SmallVector<NamedDecl *, 4> ConflictingDecls; | ||||||||||
2352 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
2353 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
2354 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace)) | ||||||||||
2355 | continue; | ||||||||||
2356 | |||||||||||
2357 | if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) { | ||||||||||
2358 | MergeWithNamespace = FoundNS; | ||||||||||
2359 | ConflictingDecls.clear(); | ||||||||||
2360 | break; | ||||||||||
2361 | } | ||||||||||
2362 | |||||||||||
2363 | ConflictingDecls.push_back(FoundDecl); | ||||||||||
2364 | } | ||||||||||
2365 | |||||||||||
2366 | if (!ConflictingDecls.empty()) { | ||||||||||
2367 | ExpectedName NameOrErr = Importer.HandleNameConflict( | ||||||||||
2368 | Name, DC, Decl::IDNS_Namespace, ConflictingDecls.data(), | ||||||||||
2369 | ConflictingDecls.size()); | ||||||||||
2370 | if (NameOrErr) | ||||||||||
2371 | Name = NameOrErr.get(); | ||||||||||
2372 | else | ||||||||||
2373 | return NameOrErr.takeError(); | ||||||||||
2374 | } | ||||||||||
2375 | } | ||||||||||
2376 | |||||||||||
2377 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | ||||||||||
2378 | if (!BeginLocOrErr) | ||||||||||
2379 | return BeginLocOrErr.takeError(); | ||||||||||
2380 | ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc()); | ||||||||||
2381 | if (!RBraceLocOrErr) | ||||||||||
2382 | return RBraceLocOrErr.takeError(); | ||||||||||
2383 | |||||||||||
2384 | // Create the "to" namespace, if needed. | ||||||||||
2385 | NamespaceDecl *ToNamespace = MergeWithNamespace; | ||||||||||
2386 | if (!ToNamespace) { | ||||||||||
2387 | if (GetImportedOrCreateDecl( | ||||||||||
2388 | ToNamespace, D, Importer.getToContext(), DC, D->isInline(), | ||||||||||
2389 | *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(), | ||||||||||
2390 | /*PrevDecl=*/nullptr)) | ||||||||||
2391 | return ToNamespace; | ||||||||||
2392 | ToNamespace->setRBraceLoc(*RBraceLocOrErr); | ||||||||||
2393 | ToNamespace->setLexicalDeclContext(LexicalDC); | ||||||||||
2394 | LexicalDC->addDeclInternal(ToNamespace); | ||||||||||
2395 | |||||||||||
2396 | // If this is an anonymous namespace, register it as the anonymous | ||||||||||
2397 | // namespace within its context. | ||||||||||
2398 | if (!Name) { | ||||||||||
2399 | if (auto *TU = dyn_cast<TranslationUnitDecl>(DC)) | ||||||||||
2400 | TU->setAnonymousNamespace(ToNamespace); | ||||||||||
2401 | else | ||||||||||
2402 | cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); | ||||||||||
2403 | } | ||||||||||
2404 | } | ||||||||||
2405 | Importer.MapImported(D, ToNamespace); | ||||||||||
2406 | |||||||||||
2407 | if (Error Err = ImportDeclContext(D)) | ||||||||||
2408 | return std::move(Err); | ||||||||||
2409 | |||||||||||
2410 | return ToNamespace; | ||||||||||
2411 | } | ||||||||||
2412 | |||||||||||
2413 | ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { | ||||||||||
2414 | // Import the major distinguishing characteristics of this namespace. | ||||||||||
2415 | DeclContext *DC, *LexicalDC; | ||||||||||
2416 | DeclarationName Name; | ||||||||||
2417 | SourceLocation Loc; | ||||||||||
2418 | NamedDecl *LookupD; | ||||||||||
2419 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc)) | ||||||||||
2420 | return std::move(Err); | ||||||||||
2421 | if (LookupD) | ||||||||||
2422 | return LookupD; | ||||||||||
2423 | |||||||||||
2424 | // NOTE: No conflict resolution is done for namespace aliases now. | ||||||||||
2425 | |||||||||||
2426 | Error Err = Error::success(); | ||||||||||
2427 | auto ToNamespaceLoc = importChecked(Err, D->getNamespaceLoc()); | ||||||||||
2428 | auto ToAliasLoc = importChecked(Err, D->getAliasLoc()); | ||||||||||
2429 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | ||||||||||
2430 | auto ToTargetNameLoc = importChecked(Err, D->getTargetNameLoc()); | ||||||||||
2431 | auto ToNamespace = importChecked(Err, D->getNamespace()); | ||||||||||
2432 | if (Err) | ||||||||||
2433 | return std::move(Err); | ||||||||||
2434 | |||||||||||
2435 | IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier()); | ||||||||||
2436 | |||||||||||
2437 | NamespaceAliasDecl *ToD; | ||||||||||
2438 | if (GetImportedOrCreateDecl( | ||||||||||
2439 | ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc, | ||||||||||
2440 | ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace)) | ||||||||||
2441 | return ToD; | ||||||||||
2442 | |||||||||||
2443 | ToD->setLexicalDeclContext(LexicalDC); | ||||||||||
2444 | LexicalDC->addDeclInternal(ToD); | ||||||||||
2445 | |||||||||||
2446 | return ToD; | ||||||||||
2447 | } | ||||||||||
2448 | |||||||||||
2449 | ExpectedDecl | ||||||||||
2450 | ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { | ||||||||||
2451 | // Import the major distinguishing characteristics of this typedef. | ||||||||||
2452 | DeclarationName Name; | ||||||||||
2453 | SourceLocation Loc; | ||||||||||
2454 | NamedDecl *ToD; | ||||||||||
2455 | // Do not import the DeclContext, we will import it once the TypedefNameDecl | ||||||||||
2456 | // is created. | ||||||||||
2457 | if (Error Err = ImportDeclParts(D, Name, ToD, Loc)) | ||||||||||
2458 | return std::move(Err); | ||||||||||
2459 | if (ToD) | ||||||||||
2460 | return ToD; | ||||||||||
2461 | |||||||||||
2462 | DeclContext *DC = cast_or_null<DeclContext>( | ||||||||||
2463 | Importer.GetAlreadyImportedOrNull(cast<Decl>(D->getDeclContext()))); | ||||||||||
2464 | DeclContext *LexicalDC = | ||||||||||
2465 | cast_or_null<DeclContext>(Importer.GetAlreadyImportedOrNull( | ||||||||||
2466 | cast<Decl>(D->getLexicalDeclContext()))); | ||||||||||
2467 | |||||||||||
2468 | // If this typedef is not in block scope, determine whether we've | ||||||||||
2469 | // seen a typedef with the same name (that we can merge with) or any | ||||||||||
2470 | // other entity by that name (which name lookup could conflict with). | ||||||||||
2471 | // Note: Repeated typedefs are not valid in C99: | ||||||||||
2472 | // 'typedef int T; typedef int T;' is invalid | ||||||||||
2473 | // We do not care about this now. | ||||||||||
2474 | if (DC && !DC->isFunctionOrMethod()) { | ||||||||||
2475 | SmallVector<NamedDecl *, 4> ConflictingDecls; | ||||||||||
2476 | unsigned IDNS = Decl::IDNS_Ordinary; | ||||||||||
2477 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
2478 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
2479 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | ||||||||||
2480 | continue; | ||||||||||
2481 | if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) { | ||||||||||
2482 | if (!hasSameVisibilityContextAndLinkage(FoundTypedef, D)) | ||||||||||
2483 | continue; | ||||||||||
2484 | |||||||||||
2485 | QualType FromUT = D->getUnderlyingType(); | ||||||||||
2486 | QualType FoundUT = FoundTypedef->getUnderlyingType(); | ||||||||||
2487 | if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) { | ||||||||||
2488 | // If the "From" context has a complete underlying type but we | ||||||||||
2489 | // already have a complete underlying type then return with that. | ||||||||||
2490 | if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType()) | ||||||||||
2491 | return Importer.MapImported(D, FoundTypedef); | ||||||||||
2492 | // FIXME Handle redecl chain. When you do that make consistent changes | ||||||||||
2493 | // in ASTImporterLookupTable too. | ||||||||||
2494 | } else { | ||||||||||
2495 | ConflictingDecls.push_back(FoundDecl); | ||||||||||
2496 | } | ||||||||||
2497 | } | ||||||||||
2498 | } | ||||||||||
2499 | |||||||||||
2500 | if (!ConflictingDecls.empty()) { | ||||||||||
2501 | ExpectedName NameOrErr = Importer.HandleNameConflict( | ||||||||||
2502 | Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); | ||||||||||
2503 | if (NameOrErr) | ||||||||||
2504 | Name = NameOrErr.get(); | ||||||||||
2505 | else | ||||||||||
2506 | return NameOrErr.takeError(); | ||||||||||
2507 | } | ||||||||||
2508 | } | ||||||||||
2509 | |||||||||||
2510 | Error Err = Error::success(); | ||||||||||
2511 | auto ToUnderlyingType = importChecked(Err, D->getUnderlyingType()); | ||||||||||
2512 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | ||||||||||
2513 | auto ToBeginLoc = importChecked(Err, D->getBeginLoc()); | ||||||||||
2514 | if (Err) | ||||||||||
2515 | return std::move(Err); | ||||||||||
2516 | |||||||||||
2517 | // Create the new typedef node. | ||||||||||
2518 | // FIXME: ToUnderlyingType is not used. | ||||||||||
2519 | (void)ToUnderlyingType; | ||||||||||
2520 | TypedefNameDecl *ToTypedef; | ||||||||||
2521 | if (IsAlias) { | ||||||||||
2522 | if (GetImportedOrCreateDecl<TypeAliasDecl>( | ||||||||||
2523 | ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc, | ||||||||||
2524 | Name.getAsIdentifierInfo(), ToTypeSourceInfo)) | ||||||||||
2525 | return ToTypedef; | ||||||||||
2526 | } else if (GetImportedOrCreateDecl<TypedefDecl>( | ||||||||||
2527 | ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc, | ||||||||||
2528 | Name.getAsIdentifierInfo(), ToTypeSourceInfo)) | ||||||||||
2529 | return ToTypedef; | ||||||||||
2530 | |||||||||||
2531 | // Import the DeclContext and set it to the Typedef. | ||||||||||
2532 | if ((Err = ImportDeclContext(D, DC, LexicalDC))) | ||||||||||
2533 | return std::move(Err); | ||||||||||
2534 | ToTypedef->setDeclContext(DC); | ||||||||||
2535 | ToTypedef->setLexicalDeclContext(LexicalDC); | ||||||||||
2536 | // Add to the lookupTable because we could not do that in MapImported. | ||||||||||
2537 | Importer.AddToLookupTable(ToTypedef); | ||||||||||
2538 | |||||||||||
2539 | ToTypedef->setAccess(D->getAccess()); | ||||||||||
2540 | |||||||||||
2541 | // Templated declarations should not appear in DeclContext. | ||||||||||
2542 | TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr; | ||||||||||
2543 | if (!FromAlias || !FromAlias->getDescribedAliasTemplate()) | ||||||||||
2544 | LexicalDC->addDeclInternal(ToTypedef); | ||||||||||
2545 | |||||||||||
2546 | return ToTypedef; | ||||||||||
2547 | } | ||||||||||
2548 | |||||||||||
2549 | ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { | ||||||||||
2550 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); | ||||||||||
2551 | } | ||||||||||
2552 | |||||||||||
2553 | ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { | ||||||||||
2554 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); | ||||||||||
2555 | } | ||||||||||
2556 | |||||||||||
2557 | ExpectedDecl | ||||||||||
2558 | ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { | ||||||||||
2559 | // Import the major distinguishing characteristics of this typedef. | ||||||||||
2560 | DeclContext *DC, *LexicalDC; | ||||||||||
2561 | DeclarationName Name; | ||||||||||
2562 | SourceLocation Loc; | ||||||||||
2563 | NamedDecl *FoundD; | ||||||||||
2564 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc)) | ||||||||||
2565 | return std::move(Err); | ||||||||||
2566 | if (FoundD) | ||||||||||
2567 | return FoundD; | ||||||||||
2568 | |||||||||||
2569 | // If this typedef is not in block scope, determine whether we've | ||||||||||
2570 | // seen a typedef with the same name (that we can merge with) or any | ||||||||||
2571 | // other entity by that name (which name lookup could conflict with). | ||||||||||
2572 | if (!DC->isFunctionOrMethod()) { | ||||||||||
2573 | SmallVector<NamedDecl *, 4> ConflictingDecls; | ||||||||||
2574 | unsigned IDNS = Decl::IDNS_Ordinary; | ||||||||||
2575 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
2576 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
2577 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | ||||||||||
2578 | continue; | ||||||||||
2579 | if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl)) | ||||||||||
2580 | return Importer.MapImported(D, FoundAlias); | ||||||||||
2581 | ConflictingDecls.push_back(FoundDecl); | ||||||||||
2582 | } | ||||||||||
2583 | |||||||||||
2584 | if (!ConflictingDecls.empty()) { | ||||||||||
2585 | ExpectedName NameOrErr = Importer.HandleNameConflict( | ||||||||||
2586 | Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); | ||||||||||
2587 | if (NameOrErr) | ||||||||||
2588 | Name = NameOrErr.get(); | ||||||||||
2589 | else | ||||||||||
2590 | return NameOrErr.takeError(); | ||||||||||
2591 | } | ||||||||||
2592 | } | ||||||||||
2593 | |||||||||||
2594 | Error Err = Error::success(); | ||||||||||
2595 | auto ToTemplateParameters = importChecked(Err, D->getTemplateParameters()); | ||||||||||
2596 | auto ToTemplatedDecl = importChecked(Err, D->getTemplatedDecl()); | ||||||||||
2597 | if (Err) | ||||||||||
2598 | return std::move(Err); | ||||||||||
2599 | |||||||||||
2600 | TypeAliasTemplateDecl *ToAlias; | ||||||||||
2601 | if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc, | ||||||||||
2602 | Name, ToTemplateParameters, ToTemplatedDecl)) | ||||||||||
2603 | return ToAlias; | ||||||||||
2604 | |||||||||||
2605 | ToTemplatedDecl->setDescribedAliasTemplate(ToAlias); | ||||||||||
2606 | |||||||||||
2607 | ToAlias->setAccess(D->getAccess()); | ||||||||||
2608 | ToAlias->setLexicalDeclContext(LexicalDC); | ||||||||||
2609 | LexicalDC->addDeclInternal(ToAlias); | ||||||||||
2610 | return ToAlias; | ||||||||||
2611 | } | ||||||||||
2612 | |||||||||||
2613 | ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { | ||||||||||
2614 | // Import the major distinguishing characteristics of this label. | ||||||||||
2615 | DeclContext *DC, *LexicalDC; | ||||||||||
2616 | DeclarationName Name; | ||||||||||
2617 | SourceLocation Loc; | ||||||||||
2618 | NamedDecl *ToD; | ||||||||||
2619 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
2620 | return std::move(Err); | ||||||||||
2621 | if (ToD) | ||||||||||
2622 | return ToD; | ||||||||||
2623 | |||||||||||
2624 | assert(LexicalDC->isFunctionOrMethod())((LexicalDC->isFunctionOrMethod()) ? static_cast<void> (0) : __assert_fail ("LexicalDC->isFunctionOrMethod()", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 2624, __PRETTY_FUNCTION__)); | ||||||||||
2625 | |||||||||||
2626 | LabelDecl *ToLabel; | ||||||||||
2627 | if (D->isGnuLocal()) { | ||||||||||
2628 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | ||||||||||
2629 | if (!BeginLocOrErr) | ||||||||||
2630 | return BeginLocOrErr.takeError(); | ||||||||||
2631 | if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc, | ||||||||||
2632 | Name.getAsIdentifierInfo(), *BeginLocOrErr)) | ||||||||||
2633 | return ToLabel; | ||||||||||
2634 | |||||||||||
2635 | } else { | ||||||||||
2636 | if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc, | ||||||||||
2637 | Name.getAsIdentifierInfo())) | ||||||||||
2638 | return ToLabel; | ||||||||||
2639 | |||||||||||
2640 | } | ||||||||||
2641 | |||||||||||
2642 | Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt()); | ||||||||||
2643 | if (!ToStmtOrErr) | ||||||||||
2644 | return ToStmtOrErr.takeError(); | ||||||||||
2645 | |||||||||||
2646 | ToLabel->setStmt(*ToStmtOrErr); | ||||||||||
2647 | ToLabel->setLexicalDeclContext(LexicalDC); | ||||||||||
2648 | LexicalDC->addDeclInternal(ToLabel); | ||||||||||
2649 | return ToLabel; | ||||||||||
2650 | } | ||||||||||
2651 | |||||||||||
2652 | ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { | ||||||||||
2653 | // Import the major distinguishing characteristics of this enum. | ||||||||||
2654 | DeclContext *DC, *LexicalDC; | ||||||||||
2655 | DeclarationName Name; | ||||||||||
2656 | SourceLocation Loc; | ||||||||||
2657 | NamedDecl *ToD; | ||||||||||
2658 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
2659 | return std::move(Err); | ||||||||||
2660 | if (ToD) | ||||||||||
2661 | return ToD; | ||||||||||
2662 | |||||||||||
2663 | // Figure out what enum name we're looking for. | ||||||||||
2664 | unsigned IDNS = Decl::IDNS_Tag; | ||||||||||
2665 | DeclarationName SearchName = Name; | ||||||||||
2666 | if (!SearchName && D->getTypedefNameForAnonDecl()) { | ||||||||||
2667 | if (Error Err = importInto( | ||||||||||
2668 | SearchName, D->getTypedefNameForAnonDecl()->getDeclName())) | ||||||||||
2669 | return std::move(Err); | ||||||||||
2670 | IDNS = Decl::IDNS_Ordinary; | ||||||||||
2671 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) | ||||||||||
2672 | IDNS |= Decl::IDNS_Ordinary; | ||||||||||
2673 | |||||||||||
2674 | // We may already have an enum of the same name; try to find and match it. | ||||||||||
2675 | EnumDecl *PrevDecl = nullptr; | ||||||||||
2676 | if (!DC->isFunctionOrMethod() && SearchName) { | ||||||||||
2677 | SmallVector<NamedDecl *, 4> ConflictingDecls; | ||||||||||
2678 | auto FoundDecls = | ||||||||||
2679 | Importer.findDeclsInToCtx(DC, SearchName); | ||||||||||
2680 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
2681 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | ||||||||||
2682 | continue; | ||||||||||
2683 | |||||||||||
2684 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) { | ||||||||||
2685 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) | ||||||||||
2686 | FoundDecl = Tag->getDecl(); | ||||||||||
2687 | } | ||||||||||
2688 | |||||||||||
2689 | if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) { | ||||||||||
2690 | if (!hasSameVisibilityContextAndLinkage(FoundEnum, D)) | ||||||||||
2691 | continue; | ||||||||||
2692 | if (IsStructuralMatch(D, FoundEnum)) { | ||||||||||
2693 | EnumDecl *FoundDef = FoundEnum->getDefinition(); | ||||||||||
2694 | if (D->isThisDeclarationADefinition() && FoundDef) | ||||||||||
2695 | return Importer.MapImported(D, FoundDef); | ||||||||||
2696 | PrevDecl = FoundEnum->getMostRecentDecl(); | ||||||||||
2697 | break; | ||||||||||
2698 | } | ||||||||||
2699 | ConflictingDecls.push_back(FoundDecl); | ||||||||||
2700 | } | ||||||||||
2701 | } | ||||||||||
2702 | |||||||||||
2703 | if (!ConflictingDecls.empty()) { | ||||||||||
2704 | ExpectedName NameOrErr = Importer.HandleNameConflict( | ||||||||||
2705 | SearchName, DC, IDNS, ConflictingDecls.data(), | ||||||||||
2706 | ConflictingDecls.size()); | ||||||||||
2707 | if (NameOrErr) | ||||||||||
2708 | Name = NameOrErr.get(); | ||||||||||
2709 | else | ||||||||||
2710 | return NameOrErr.takeError(); | ||||||||||
2711 | } | ||||||||||
2712 | } | ||||||||||
2713 | |||||||||||
2714 | Error Err = Error::success(); | ||||||||||
2715 | auto ToBeginLoc = importChecked(Err, D->getBeginLoc()); | ||||||||||
2716 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | ||||||||||
2717 | auto ToIntegerType = importChecked(Err, D->getIntegerType()); | ||||||||||
2718 | auto ToBraceRange = importChecked(Err, D->getBraceRange()); | ||||||||||
2719 | if (Err) | ||||||||||
2720 | return std::move(Err); | ||||||||||
2721 | |||||||||||
2722 | // Create the enum declaration. | ||||||||||
2723 | EnumDecl *D2; | ||||||||||
2724 | if (GetImportedOrCreateDecl( | ||||||||||
2725 | D2, D, Importer.getToContext(), DC, ToBeginLoc, | ||||||||||
2726 | Loc, Name.getAsIdentifierInfo(), PrevDecl, D->isScoped(), | ||||||||||
2727 | D->isScopedUsingClassTag(), D->isFixed())) | ||||||||||
2728 | return D2; | ||||||||||
2729 | |||||||||||
2730 | D2->setQualifierInfo(ToQualifierLoc); | ||||||||||
2731 | D2->setIntegerType(ToIntegerType); | ||||||||||
2732 | D2->setBraceRange(ToBraceRange); | ||||||||||
2733 | D2->setAccess(D->getAccess()); | ||||||||||
2734 | D2->setLexicalDeclContext(LexicalDC); | ||||||||||
2735 | addDeclToContexts(D, D2); | ||||||||||
2736 | |||||||||||
2737 | if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) { | ||||||||||
2738 | TemplateSpecializationKind SK = MemberInfo->getTemplateSpecializationKind(); | ||||||||||
2739 | EnumDecl *FromInst = D->getInstantiatedFromMemberEnum(); | ||||||||||
2740 | if (Expected<EnumDecl *> ToInstOrErr = import(FromInst)) | ||||||||||
2741 | D2->setInstantiationOfMemberEnum(*ToInstOrErr, SK); | ||||||||||
2742 | else | ||||||||||
2743 | return ToInstOrErr.takeError(); | ||||||||||
2744 | if (ExpectedSLoc POIOrErr = import(MemberInfo->getPointOfInstantiation())) | ||||||||||
2745 | D2->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); | ||||||||||
2746 | else | ||||||||||
2747 | return POIOrErr.takeError(); | ||||||||||
2748 | } | ||||||||||
2749 | |||||||||||
2750 | // Import the definition | ||||||||||
2751 | if (D->isCompleteDefinition()) | ||||||||||
2752 | if (Error Err = ImportDefinition(D, D2)) | ||||||||||
2753 | return std::move(Err); | ||||||||||
2754 | |||||||||||
2755 | return D2; | ||||||||||
2756 | } | ||||||||||
2757 | |||||||||||
2758 | ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { | ||||||||||
2759 | bool IsFriendTemplate = false; | ||||||||||
2760 | if (auto *DCXX
| ||||||||||
| |||||||||||
2761 | IsFriendTemplate = | ||||||||||
2762 | DCXX->getDescribedClassTemplate() && | ||||||||||
2763 | DCXX->getDescribedClassTemplate()->getFriendObjectKind() != | ||||||||||
2764 | Decl::FOK_None; | ||||||||||
2765 | } | ||||||||||
2766 | |||||||||||
2767 | // Import the major distinguishing characteristics of this record. | ||||||||||
2768 | DeclContext *DC = nullptr, *LexicalDC = nullptr; | ||||||||||
2769 | DeclarationName Name; | ||||||||||
2770 | SourceLocation Loc; | ||||||||||
2771 | NamedDecl *ToD = nullptr; | ||||||||||
2772 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
2773 | return std::move(Err); | ||||||||||
2774 | if (ToD
| ||||||||||
2775 | return ToD; | ||||||||||
2776 | |||||||||||
2777 | // Figure out what structure name we're looking for. | ||||||||||
2778 | unsigned IDNS = Decl::IDNS_Tag; | ||||||||||
2779 | DeclarationName SearchName = Name; | ||||||||||
2780 | if (!SearchName && D->getTypedefNameForAnonDecl()) { | ||||||||||
2781 | if (Error Err = importInto( | ||||||||||
2782 | SearchName, D->getTypedefNameForAnonDecl()->getDeclName())) | ||||||||||
2783 | return std::move(Err); | ||||||||||
2784 | IDNS = Decl::IDNS_Ordinary; | ||||||||||
2785 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) | ||||||||||
2786 | IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend; | ||||||||||
2787 | |||||||||||
2788 | // We may already have a record of the same name; try to find and match it. | ||||||||||
2789 | RecordDecl *PrevDecl = nullptr; | ||||||||||
2790 | if (!DC->isFunctionOrMethod() && !D->isLambda()) { | ||||||||||
| |||||||||||
2791 | SmallVector<NamedDecl *, 4> ConflictingDecls; | ||||||||||
2792 | auto FoundDecls = | ||||||||||
2793 | Importer.findDeclsInToCtx(DC, SearchName); | ||||||||||
2794 | if (!FoundDecls.empty()) { | ||||||||||
2795 | // We're going to have to compare D against potentially conflicting Decls, | ||||||||||
2796 | // so complete it. | ||||||||||
2797 | if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition()) | ||||||||||
2798 | D->getASTContext().getExternalSource()->CompleteType(D); | ||||||||||
2799 | } | ||||||||||
2800 | |||||||||||
2801 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
2802 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | ||||||||||
2803 | continue; | ||||||||||
2804 | |||||||||||
2805 | Decl *Found = FoundDecl; | ||||||||||
2806 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) { | ||||||||||
2807 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) | ||||||||||
2808 | Found = Tag->getDecl(); | ||||||||||
2809 | } | ||||||||||
2810 | |||||||||||
2811 | if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) { | ||||||||||
2812 | // Do not emit false positive diagnostic in case of unnamed | ||||||||||
2813 | // struct/union and in case of anonymous structs. Would be false | ||||||||||
2814 | // because there may be several anonymous/unnamed structs in a class. | ||||||||||
2815 | // E.g. these are both valid: | ||||||||||
2816 | // struct A { // unnamed structs | ||||||||||
2817 | // struct { struct A *next; } entry0; | ||||||||||
2818 | // struct { struct A *next; } entry1; | ||||||||||
2819 | // }; | ||||||||||
2820 | // struct X { struct { int a; }; struct { int b; }; }; // anon structs | ||||||||||
2821 | if (!SearchName) | ||||||||||
2822 | if (!IsStructuralMatch(D, FoundRecord, false)) | ||||||||||
2823 | continue; | ||||||||||
2824 | |||||||||||
2825 | if (!hasSameVisibilityContextAndLinkage(FoundRecord, D)) | ||||||||||
2826 | continue; | ||||||||||
2827 | |||||||||||
2828 | if (IsStructuralMatch(D, FoundRecord)) { | ||||||||||
2829 | RecordDecl *FoundDef = FoundRecord->getDefinition(); | ||||||||||
2830 | if (D->isThisDeclarationADefinition() && FoundDef) { | ||||||||||
2831 | // FIXME: Structural equivalence check should check for same | ||||||||||
2832 | // user-defined methods. | ||||||||||
2833 | Importer.MapImported(D, FoundDef); | ||||||||||
2834 | if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) { | ||||||||||
2835 | auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef); | ||||||||||
2836 | assert(FoundCXX && "Record type mismatch")((FoundCXX && "Record type mismatch") ? static_cast< void> (0) : __assert_fail ("FoundCXX && \"Record type mismatch\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 2836, __PRETTY_FUNCTION__)); | ||||||||||
2837 | |||||||||||
2838 | if (!Importer.isMinimalImport()) | ||||||||||
2839 | // FoundDef may not have every implicit method that D has | ||||||||||
2840 | // because implicit methods are created only if they are used. | ||||||||||
2841 | if (Error Err = ImportImplicitMethods(DCXX, FoundCXX)) | ||||||||||
2842 | return std::move(Err); | ||||||||||
2843 | } | ||||||||||
2844 | } | ||||||||||
2845 | PrevDecl = FoundRecord->getMostRecentDecl(); | ||||||||||
2846 | break; | ||||||||||
2847 | } | ||||||||||
2848 | ConflictingDecls.push_back(FoundDecl); | ||||||||||
2849 | } // kind is RecordDecl | ||||||||||
2850 | } // for | ||||||||||
2851 | |||||||||||
2852 | if (!ConflictingDecls.empty() && SearchName) { | ||||||||||
2853 | ExpectedName NameOrErr = Importer.HandleNameConflict( | ||||||||||
2854 | SearchName, DC, IDNS, ConflictingDecls.data(), | ||||||||||
2855 | ConflictingDecls.size()); | ||||||||||
2856 | if (NameOrErr) | ||||||||||
2857 | Name = NameOrErr.get(); | ||||||||||
2858 | else | ||||||||||
2859 | return NameOrErr.takeError(); | ||||||||||
2860 | } | ||||||||||
2861 | } | ||||||||||
2862 | |||||||||||
2863 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | ||||||||||
2864 | if (!BeginLocOrErr) | ||||||||||
2865 | return BeginLocOrErr.takeError(); | ||||||||||
2866 | |||||||||||
2867 | // Create the record declaration. | ||||||||||
2868 | RecordDecl *D2 = nullptr; | ||||||||||
2869 | CXXRecordDecl *D2CXX = nullptr; | ||||||||||
2870 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) { | ||||||||||
2871 | if (DCXX->isLambda()) { | ||||||||||
2872 | auto TInfoOrErr = import(DCXX->getLambdaTypeInfo()); | ||||||||||
2873 | if (!TInfoOrErr) | ||||||||||
2874 | return TInfoOrErr.takeError(); | ||||||||||
2875 | if (GetImportedOrCreateSpecialDecl( | ||||||||||
2876 | D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(), | ||||||||||
2877 | DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(), | ||||||||||
2878 | DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault())) | ||||||||||
2879 | return D2CXX; | ||||||||||
2880 | ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl()); | ||||||||||
2881 | if (!CDeclOrErr) | ||||||||||
2882 | return CDeclOrErr.takeError(); | ||||||||||
2883 | D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr, | ||||||||||
2884 | DCXX->hasKnownLambdaInternalLinkage()); | ||||||||||
2885 | D2CXX->setDeviceLambdaManglingNumber( | ||||||||||
2886 | DCXX->getDeviceLambdaManglingNumber()); | ||||||||||
2887 | } else if (DCXX->isInjectedClassName()) { | ||||||||||
2888 | // We have to be careful to do a similar dance to the one in | ||||||||||
2889 | // Sema::ActOnStartCXXMemberDeclarations | ||||||||||
2890 | const bool DelayTypeCreation = true; | ||||||||||
2891 | if (GetImportedOrCreateDecl( | ||||||||||
2892 | D2CXX, D, Importer.getToContext(), D->getTagKind(), DC, | ||||||||||
2893 | *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(), | ||||||||||
2894 | cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation)) | ||||||||||
2895 | return D2CXX; | ||||||||||
2896 | Importer.getToContext().getTypeDeclType( | ||||||||||
2897 | D2CXX, dyn_cast<CXXRecordDecl>(DC)); | ||||||||||
2898 | } else { | ||||||||||
2899 | if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(), | ||||||||||
2900 | D->getTagKind(), DC, *BeginLocOrErr, Loc, | ||||||||||
2901 | Name.getAsIdentifierInfo(), | ||||||||||
2902 | cast_or_null<CXXRecordDecl>(PrevDecl))) | ||||||||||
2903 | return D2CXX; | ||||||||||
2904 | } | ||||||||||
2905 | |||||||||||
2906 | D2 = D2CXX; | ||||||||||
2907 | D2->setAccess(D->getAccess()); | ||||||||||
2908 | D2->setLexicalDeclContext(LexicalDC); | ||||||||||
2909 | addDeclToContexts(D, D2); | ||||||||||
2910 | |||||||||||
2911 | if (ClassTemplateDecl *FromDescribed = | ||||||||||
2912 | DCXX->getDescribedClassTemplate()) { | ||||||||||
2913 | ClassTemplateDecl *ToDescribed; | ||||||||||
2914 | if (Error Err = importInto(ToDescribed, FromDescribed)) | ||||||||||
2915 | return std::move(Err); | ||||||||||
2916 | D2CXX->setDescribedClassTemplate(ToDescribed); | ||||||||||
2917 | if (!DCXX->isInjectedClassName() && !IsFriendTemplate) { | ||||||||||
2918 | // In a record describing a template the type should be an | ||||||||||
2919 | // InjectedClassNameType (see Sema::CheckClassTemplate). Update the | ||||||||||
2920 | // previously set type to the correct value here (ToDescribed is not | ||||||||||
2921 | // available at record create). | ||||||||||
2922 | // FIXME: The previous type is cleared but not removed from | ||||||||||
2923 | // ASTContext's internal storage. | ||||||||||
2924 | CXXRecordDecl *Injected = nullptr; | ||||||||||
2925 | for (NamedDecl *Found : D2CXX->noload_lookup(Name)) { | ||||||||||
2926 | auto *Record = dyn_cast<CXXRecordDecl>(Found); | ||||||||||
2927 | if (Record && Record->isInjectedClassName()) { | ||||||||||
2928 | Injected = Record; | ||||||||||
2929 | break; | ||||||||||
2930 | } | ||||||||||
2931 | } | ||||||||||
2932 | // Create an injected type for the whole redecl chain. | ||||||||||
2933 | SmallVector<Decl *, 2> Redecls = | ||||||||||
2934 | getCanonicalForwardRedeclChain(D2CXX); | ||||||||||
2935 | for (auto *R : Redecls) { | ||||||||||
2936 | auto *RI = cast<CXXRecordDecl>(R); | ||||||||||
2937 | RI->setTypeForDecl(nullptr); | ||||||||||
2938 | // Below we create a new injected type and assign that to the | ||||||||||
2939 | // canonical decl, subsequent declarations in the chain will reuse | ||||||||||
2940 | // that type. | ||||||||||
2941 | Importer.getToContext().getInjectedClassNameType( | ||||||||||
2942 | RI, ToDescribed->getInjectedClassNameSpecialization()); | ||||||||||
2943 | } | ||||||||||
2944 | // Set the new type for the previous injected decl too. | ||||||||||
2945 | if (Injected) { | ||||||||||
2946 | Injected->setTypeForDecl(nullptr); | ||||||||||
2947 | Importer.getToContext().getTypeDeclType(Injected, D2CXX); | ||||||||||
2948 | } | ||||||||||
2949 | } | ||||||||||
2950 | } else if (MemberSpecializationInfo *MemberInfo = | ||||||||||
2951 | DCXX->getMemberSpecializationInfo()) { | ||||||||||
2952 | TemplateSpecializationKind SK = | ||||||||||
2953 | MemberInfo->getTemplateSpecializationKind(); | ||||||||||
2954 | CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass(); | ||||||||||
2955 | |||||||||||
2956 | if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst)) | ||||||||||
2957 | D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK); | ||||||||||
2958 | else | ||||||||||
2959 | return ToInstOrErr.takeError(); | ||||||||||
2960 | |||||||||||
2961 | if (ExpectedSLoc POIOrErr = | ||||||||||
2962 | import(MemberInfo->getPointOfInstantiation())) | ||||||||||
2963 | D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation( | ||||||||||
2964 | *POIOrErr); | ||||||||||
2965 | else | ||||||||||
2966 | return POIOrErr.takeError(); | ||||||||||
2967 | } | ||||||||||
2968 | |||||||||||
2969 | } else { | ||||||||||
2970 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), | ||||||||||
2971 | D->getTagKind(), DC, *BeginLocOrErr, Loc, | ||||||||||
2972 | Name.getAsIdentifierInfo(), PrevDecl)) | ||||||||||
2973 | return D2; | ||||||||||
2974 | D2->setLexicalDeclContext(LexicalDC); | ||||||||||
2975 | addDeclToContexts(D, D2); | ||||||||||
2976 | } | ||||||||||
2977 | |||||||||||
2978 | if (auto BraceRangeOrErr = import(D->getBraceRange())) | ||||||||||
2979 | D2->setBraceRange(*BraceRangeOrErr); | ||||||||||
2980 | else | ||||||||||
2981 | return BraceRangeOrErr.takeError(); | ||||||||||
2982 | if (auto QualifierLocOrErr = import(D->getQualifierLoc())) | ||||||||||
2983 | D2->setQualifierInfo(*QualifierLocOrErr); | ||||||||||
2984 | else | ||||||||||
2985 | return QualifierLocOrErr.takeError(); | ||||||||||
2986 | |||||||||||
2987 | if (D->isAnonymousStructOrUnion()) | ||||||||||
2988 | D2->setAnonymousStructOrUnion(true); | ||||||||||
2989 | |||||||||||
2990 | if (D->isCompleteDefinition()) | ||||||||||
2991 | if (Error Err = ImportDefinition(D, D2, IDK_Default)) | ||||||||||
2992 | return std::move(Err); | ||||||||||
2993 | |||||||||||
2994 | return D2; | ||||||||||
2995 | } | ||||||||||
2996 | |||||||||||
2997 | ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { | ||||||||||
2998 | // Import the major distinguishing characteristics of this enumerator. | ||||||||||
2999 | DeclContext *DC, *LexicalDC; | ||||||||||
3000 | DeclarationName Name; | ||||||||||
3001 | SourceLocation Loc; | ||||||||||
3002 | NamedDecl *ToD; | ||||||||||
3003 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
3004 | return std::move(Err); | ||||||||||
3005 | if (ToD) | ||||||||||
3006 | return ToD; | ||||||||||
3007 | |||||||||||
3008 | // Determine whether there are any other declarations with the same name and | ||||||||||
3009 | // in the same context. | ||||||||||
3010 | if (!LexicalDC->isFunctionOrMethod()) { | ||||||||||
3011 | SmallVector<NamedDecl *, 4> ConflictingDecls; | ||||||||||
3012 | unsigned IDNS = Decl::IDNS_Ordinary; | ||||||||||
3013 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
3014 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
3015 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | ||||||||||
3016 | continue; | ||||||||||
3017 | |||||||||||
3018 | if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) { | ||||||||||
3019 | if (IsStructuralMatch(D, FoundEnumConstant)) | ||||||||||
3020 | return Importer.MapImported(D, FoundEnumConstant); | ||||||||||
3021 | ConflictingDecls.push_back(FoundDecl); | ||||||||||
3022 | } | ||||||||||
3023 | } | ||||||||||
3024 | |||||||||||
3025 | if (!ConflictingDecls.empty()) { | ||||||||||
3026 | ExpectedName NameOrErr = Importer.HandleNameConflict( | ||||||||||
3027 | Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); | ||||||||||
3028 | if (NameOrErr) | ||||||||||
3029 | Name = NameOrErr.get(); | ||||||||||
3030 | else | ||||||||||
3031 | return NameOrErr.takeError(); | ||||||||||
3032 | } | ||||||||||
3033 | } | ||||||||||
3034 | |||||||||||
3035 | ExpectedType TypeOrErr = import(D->getType()); | ||||||||||
3036 | if (!TypeOrErr) | ||||||||||
3037 | return TypeOrErr.takeError(); | ||||||||||
3038 | |||||||||||
3039 | ExpectedExpr InitOrErr = import(D->getInitExpr()); | ||||||||||
3040 | if (!InitOrErr) | ||||||||||
3041 | return InitOrErr.takeError(); | ||||||||||
3042 | |||||||||||
3043 | EnumConstantDecl *ToEnumerator; | ||||||||||
3044 | if (GetImportedOrCreateDecl( | ||||||||||
3045 | ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc, | ||||||||||
3046 | Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal())) | ||||||||||
3047 | return ToEnumerator; | ||||||||||
3048 | |||||||||||
3049 | ToEnumerator->setAccess(D->getAccess()); | ||||||||||
3050 | ToEnumerator->setLexicalDeclContext(LexicalDC); | ||||||||||
3051 | LexicalDC->addDeclInternal(ToEnumerator); | ||||||||||
3052 | return ToEnumerator; | ||||||||||
3053 | } | ||||||||||
3054 | |||||||||||
3055 | Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD, | ||||||||||
3056 | DeclaratorDecl *ToD) { | ||||||||||
3057 | unsigned int Num = FromD->getNumTemplateParameterLists(); | ||||||||||
3058 | if (Num == 0) | ||||||||||
3059 | return Error::success(); | ||||||||||
3060 | SmallVector<TemplateParameterList *, 2> ToTPLists(Num); | ||||||||||
3061 | for (unsigned int I = 0; I < Num; ++I) | ||||||||||
3062 | if (Expected<TemplateParameterList *> ToTPListOrErr = | ||||||||||
3063 | import(FromD->getTemplateParameterList(I))) | ||||||||||
3064 | ToTPLists[I] = *ToTPListOrErr; | ||||||||||
3065 | else | ||||||||||
3066 | return ToTPListOrErr.takeError(); | ||||||||||
3067 | ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists); | ||||||||||
3068 | return Error::success(); | ||||||||||
3069 | } | ||||||||||
3070 | |||||||||||
3071 | Error ASTNodeImporter::ImportTemplateInformation( | ||||||||||
3072 | FunctionDecl *FromFD, FunctionDecl *ToFD) { | ||||||||||
3073 | switch (FromFD->getTemplatedKind()) { | ||||||||||
3074 | case FunctionDecl::TK_NonTemplate: | ||||||||||
3075 | case FunctionDecl::TK_FunctionTemplate: | ||||||||||
3076 | return Error::success(); | ||||||||||
3077 | |||||||||||
3078 | case FunctionDecl::TK_MemberSpecialization: { | ||||||||||
3079 | TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind(); | ||||||||||
3080 | |||||||||||
3081 | if (Expected<FunctionDecl *> InstFDOrErr = | ||||||||||
3082 | import(FromFD->getInstantiatedFromMemberFunction())) | ||||||||||
3083 | ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK); | ||||||||||
3084 | else | ||||||||||
3085 | return InstFDOrErr.takeError(); | ||||||||||
3086 | |||||||||||
3087 | if (ExpectedSLoc POIOrErr = import( | ||||||||||
3088 | FromFD->getMemberSpecializationInfo()->getPointOfInstantiation())) | ||||||||||
3089 | ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); | ||||||||||
3090 | else | ||||||||||
3091 | return POIOrErr.takeError(); | ||||||||||
3092 | |||||||||||
3093 | return Error::success(); | ||||||||||
3094 | } | ||||||||||
3095 | |||||||||||
3096 | case FunctionDecl::TK_FunctionTemplateSpecialization: { | ||||||||||
3097 | auto FunctionAndArgsOrErr = | ||||||||||
3098 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); | ||||||||||
3099 | if (!FunctionAndArgsOrErr) | ||||||||||
3100 | return FunctionAndArgsOrErr.takeError(); | ||||||||||
3101 | |||||||||||
3102 | TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy( | ||||||||||
3103 | Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr)); | ||||||||||
3104 | |||||||||||
3105 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); | ||||||||||
3106 | TemplateArgumentListInfo ToTAInfo; | ||||||||||
3107 | const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten; | ||||||||||
3108 | if (FromTAArgsAsWritten) | ||||||||||
3109 | if (Error Err = ImportTemplateArgumentListInfo( | ||||||||||
3110 | *FromTAArgsAsWritten, ToTAInfo)) | ||||||||||
3111 | return Err; | ||||||||||
3112 | |||||||||||
3113 | ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation()); | ||||||||||
3114 | if (!POIOrErr) | ||||||||||
3115 | return POIOrErr.takeError(); | ||||||||||
3116 | |||||||||||
3117 | if (Error Err = ImportTemplateParameterLists(FromFD, ToFD)) | ||||||||||
3118 | return Err; | ||||||||||
3119 | |||||||||||
3120 | TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind(); | ||||||||||
3121 | ToFD->setFunctionTemplateSpecialization( | ||||||||||
3122 | std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr, | ||||||||||
3123 | TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr); | ||||||||||
3124 | return Error::success(); | ||||||||||
3125 | } | ||||||||||
3126 | |||||||||||
3127 | case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { | ||||||||||
3128 | auto *FromInfo = FromFD->getDependentSpecializationInfo(); | ||||||||||
3129 | UnresolvedSet<8> TemplDecls; | ||||||||||
3130 | unsigned NumTemplates = FromInfo->getNumTemplates(); | ||||||||||
3131 | for (unsigned I = 0; I < NumTemplates; I++) { | ||||||||||
3132 | if (Expected<FunctionTemplateDecl *> ToFTDOrErr = | ||||||||||
3133 | import(FromInfo->getTemplate(I))) | ||||||||||
3134 | TemplDecls.addDecl(*ToFTDOrErr); | ||||||||||
3135 | else | ||||||||||
3136 | return ToFTDOrErr.takeError(); | ||||||||||
3137 | } | ||||||||||
3138 | |||||||||||
3139 | // Import TemplateArgumentListInfo. | ||||||||||
3140 | TemplateArgumentListInfo ToTAInfo; | ||||||||||
3141 | if (Error Err = ImportTemplateArgumentListInfo( | ||||||||||
3142 | FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(), | ||||||||||
3143 | llvm::makeArrayRef( | ||||||||||
3144 | FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()), | ||||||||||
3145 | ToTAInfo)) | ||||||||||
3146 | return Err; | ||||||||||
3147 | |||||||||||
3148 | ToFD->setDependentTemplateSpecialization(Importer.getToContext(), | ||||||||||
3149 | TemplDecls, ToTAInfo); | ||||||||||
3150 | return Error::success(); | ||||||||||
3151 | } | ||||||||||
3152 | } | ||||||||||
3153 | llvm_unreachable("All cases should be covered!")::llvm::llvm_unreachable_internal("All cases should be covered!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3153); | ||||||||||
3154 | } | ||||||||||
3155 | |||||||||||
3156 | Expected<FunctionDecl *> | ||||||||||
3157 | ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) { | ||||||||||
3158 | auto FunctionAndArgsOrErr = | ||||||||||
3159 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); | ||||||||||
3160 | if (!FunctionAndArgsOrErr) | ||||||||||
3161 | return FunctionAndArgsOrErr.takeError(); | ||||||||||
3162 | |||||||||||
3163 | FunctionTemplateDecl *Template; | ||||||||||
3164 | TemplateArgsTy ToTemplArgs; | ||||||||||
3165 | std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr; | ||||||||||
3166 | void *InsertPos = nullptr; | ||||||||||
3167 | auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos); | ||||||||||
3168 | return FoundSpec; | ||||||||||
3169 | } | ||||||||||
3170 | |||||||||||
3171 | Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD, | ||||||||||
3172 | FunctionDecl *ToFD) { | ||||||||||
3173 | if (Stmt *FromBody = FromFD->getBody()) { | ||||||||||
3174 | if (ExpectedStmt ToBodyOrErr = import(FromBody)) | ||||||||||
3175 | ToFD->setBody(*ToBodyOrErr); | ||||||||||
3176 | else | ||||||||||
3177 | return ToBodyOrErr.takeError(); | ||||||||||
3178 | } | ||||||||||
3179 | return Error::success(); | ||||||||||
3180 | } | ||||||||||
3181 | |||||||||||
3182 | // Returns true if the given D has a DeclContext up to the TranslationUnitDecl | ||||||||||
3183 | // which is equal to the given DC. | ||||||||||
3184 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Decl *D) { | ||||||||||
3185 | const DeclContext *DCi = D->getDeclContext(); | ||||||||||
3186 | while (DCi != D->getTranslationUnitDecl()) { | ||||||||||
3187 | if (DCi == DC) | ||||||||||
3188 | return true; | ||||||||||
3189 | DCi = DCi->getParent(); | ||||||||||
3190 | } | ||||||||||
3191 | return false; | ||||||||||
3192 | } | ||||||||||
3193 | |||||||||||
3194 | bool ASTNodeImporter::hasAutoReturnTypeDeclaredInside(FunctionDecl *D) { | ||||||||||
3195 | QualType FromTy = D->getType(); | ||||||||||
3196 | const FunctionProtoType *FromFPT = FromTy->getAs<FunctionProtoType>(); | ||||||||||
3197 | assert(FromFPT && "Must be called on FunctionProtoType")((FromFPT && "Must be called on FunctionProtoType") ? static_cast<void> (0) : __assert_fail ("FromFPT && \"Must be called on FunctionProtoType\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3197, __PRETTY_FUNCTION__)); | ||||||||||
3198 | if (AutoType *AutoT = FromFPT->getReturnType()->getContainedAutoType()) { | ||||||||||
3199 | QualType DeducedT = AutoT->getDeducedType(); | ||||||||||
3200 | if (const RecordType *RecordT = | ||||||||||
3201 | DeducedT.isNull() ? nullptr : dyn_cast<RecordType>(DeducedT)) { | ||||||||||
3202 | RecordDecl *RD = RecordT->getDecl(); | ||||||||||
3203 | assert(RD)((RD) ? static_cast<void> (0) : __assert_fail ("RD", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3203, __PRETTY_FUNCTION__)); | ||||||||||
3204 | if (isAncestorDeclContextOf(D, RD)) { | ||||||||||
3205 | assert(RD->getLexicalDeclContext() == RD->getDeclContext())((RD->getLexicalDeclContext() == RD->getDeclContext()) ? static_cast<void> (0) : __assert_fail ("RD->getLexicalDeclContext() == RD->getDeclContext()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3205, __PRETTY_FUNCTION__)); | ||||||||||
3206 | return true; | ||||||||||
3207 | } | ||||||||||
3208 | } | ||||||||||
3209 | } | ||||||||||
3210 | if (const TypedefType *TypedefT = | ||||||||||
3211 | dyn_cast<TypedefType>(FromFPT->getReturnType())) { | ||||||||||
3212 | TypedefNameDecl *TD = TypedefT->getDecl(); | ||||||||||
3213 | assert(TD)((TD) ? static_cast<void> (0) : __assert_fail ("TD", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3213, __PRETTY_FUNCTION__)); | ||||||||||
3214 | if (isAncestorDeclContextOf(D, TD)) { | ||||||||||
3215 | assert(TD->getLexicalDeclContext() == TD->getDeclContext())((TD->getLexicalDeclContext() == TD->getDeclContext()) ? static_cast<void> (0) : __assert_fail ("TD->getLexicalDeclContext() == TD->getDeclContext()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3215, __PRETTY_FUNCTION__)); | ||||||||||
3216 | return true; | ||||||||||
3217 | } | ||||||||||
3218 | } | ||||||||||
3219 | return false; | ||||||||||
3220 | } | ||||||||||
3221 | |||||||||||
3222 | ExplicitSpecifier | ||||||||||
3223 | ASTNodeImporter::importExplicitSpecifier(Error &Err, ExplicitSpecifier ESpec) { | ||||||||||
3224 | Expr *ExplicitExpr = ESpec.getExpr(); | ||||||||||
3225 | if (ExplicitExpr) | ||||||||||
3226 | ExplicitExpr = importChecked(Err, ESpec.getExpr()); | ||||||||||
3227 | return ExplicitSpecifier(ExplicitExpr, ESpec.getKind()); | ||||||||||
3228 | } | ||||||||||
3229 | |||||||||||
3230 | ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { | ||||||||||
3231 | |||||||||||
3232 | SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D); | ||||||||||
3233 | auto RedeclIt = Redecls.begin(); | ||||||||||
3234 | // Import the first part of the decl chain. I.e. import all previous | ||||||||||
3235 | // declarations starting from the canonical decl. | ||||||||||
3236 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { | ||||||||||
3237 | ExpectedDecl ToRedeclOrErr = import(*RedeclIt); | ||||||||||
3238 | if (!ToRedeclOrErr) | ||||||||||
3239 | return ToRedeclOrErr.takeError(); | ||||||||||
3240 | } | ||||||||||
3241 | assert(*RedeclIt == D)((*RedeclIt == D) ? static_cast<void> (0) : __assert_fail ("*RedeclIt == D", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3241, __PRETTY_FUNCTION__)); | ||||||||||
3242 | |||||||||||
3243 | // Import the major distinguishing characteristics of this function. | ||||||||||
3244 | DeclContext *DC, *LexicalDC; | ||||||||||
3245 | DeclarationName Name; | ||||||||||
3246 | SourceLocation Loc; | ||||||||||
3247 | NamedDecl *ToD; | ||||||||||
3248 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
3249 | return std::move(Err); | ||||||||||
3250 | if (ToD) | ||||||||||
3251 | return ToD; | ||||||||||
3252 | |||||||||||
3253 | FunctionDecl *FoundByLookup = nullptr; | ||||||||||
3254 | FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate(); | ||||||||||
3255 | |||||||||||
3256 | // If this is a function template specialization, then try to find the same | ||||||||||
3257 | // existing specialization in the "to" context. The lookup below will not | ||||||||||
3258 | // find any specialization, but would find the primary template; thus, we | ||||||||||
3259 | // have to skip normal lookup in case of specializations. | ||||||||||
3260 | // FIXME handle member function templates (TK_MemberSpecialization) similarly? | ||||||||||
3261 | if (D->getTemplatedKind() == | ||||||||||
3262 | FunctionDecl::TK_FunctionTemplateSpecialization) { | ||||||||||
3263 | auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D); | ||||||||||
3264 | if (!FoundFunctionOrErr) | ||||||||||
3265 | return FoundFunctionOrErr.takeError(); | ||||||||||
3266 | if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) { | ||||||||||
3267 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) | ||||||||||
3268 | return Def; | ||||||||||
3269 | FoundByLookup = FoundFunction; | ||||||||||
3270 | } | ||||||||||
3271 | } | ||||||||||
3272 | // Try to find a function in our own ("to") context with the same name, same | ||||||||||
3273 | // type, and in the same context as the function we're importing. | ||||||||||
3274 | else if (!LexicalDC->isFunctionOrMethod()) { | ||||||||||
3275 | SmallVector<NamedDecl *, 4> ConflictingDecls; | ||||||||||
3276 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; | ||||||||||
3277 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
3278 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
3279 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | ||||||||||
3280 | continue; | ||||||||||
3281 | |||||||||||
3282 | if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) { | ||||||||||
3283 | if (!hasSameVisibilityContextAndLinkage(FoundFunction, D)) | ||||||||||
3284 | continue; | ||||||||||
3285 | |||||||||||
3286 | if (IsStructuralMatch(D, FoundFunction)) { | ||||||||||
3287 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) | ||||||||||
3288 | return Def; | ||||||||||
3289 | FoundByLookup = FoundFunction; | ||||||||||
3290 | break; | ||||||||||
3291 | } | ||||||||||
3292 | // FIXME: Check for overloading more carefully, e.g., by boosting | ||||||||||
3293 | // Sema::IsOverload out to the AST library. | ||||||||||
3294 | |||||||||||
3295 | // Function overloading is okay in C++. | ||||||||||
3296 | if (Importer.getToContext().getLangOpts().CPlusPlus) | ||||||||||
3297 | continue; | ||||||||||
3298 | |||||||||||
3299 | // Complain about inconsistent function types. | ||||||||||
3300 | Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent) | ||||||||||
3301 | << Name << D->getType() << FoundFunction->getType(); | ||||||||||
3302 | Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here) | ||||||||||
3303 | << FoundFunction->getType(); | ||||||||||
3304 | ConflictingDecls.push_back(FoundDecl); | ||||||||||
3305 | } | ||||||||||
3306 | } | ||||||||||
3307 | |||||||||||
3308 | if (!ConflictingDecls.empty()) { | ||||||||||
3309 | ExpectedName NameOrErr = Importer.HandleNameConflict( | ||||||||||
3310 | Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); | ||||||||||
3311 | if (NameOrErr) | ||||||||||
3312 | Name = NameOrErr.get(); | ||||||||||
3313 | else | ||||||||||
3314 | return NameOrErr.takeError(); | ||||||||||
3315 | } | ||||||||||
3316 | } | ||||||||||
3317 | |||||||||||
3318 | // We do not allow more than one in-class declaration of a function. This is | ||||||||||
3319 | // because AST clients like VTableBuilder asserts on this. VTableBuilder | ||||||||||
3320 | // assumes there is only one in-class declaration. Building a redecl | ||||||||||
3321 | // chain would result in more than one in-class declaration for | ||||||||||
3322 | // overrides (even if they are part of the same redecl chain inside the | ||||||||||
3323 | // derived class.) | ||||||||||
3324 | if (FoundByLookup) { | ||||||||||
3325 | if (isa<CXXMethodDecl>(FoundByLookup)) { | ||||||||||
3326 | if (D->getLexicalDeclContext() == D->getDeclContext()) { | ||||||||||
3327 | if (!D->doesThisDeclarationHaveABody()) { | ||||||||||
3328 | if (FunctionTemplateDecl *DescribedD = | ||||||||||
3329 | D->getDescribedFunctionTemplate()) { | ||||||||||
3330 | // Handle a "templated" function together with its described | ||||||||||
3331 | // template. This avoids need for a similar check at import of the | ||||||||||
3332 | // described template. | ||||||||||
3333 | assert(FoundByLookup->getDescribedFunctionTemplate() &&((FoundByLookup->getDescribedFunctionTemplate() && "Templated function mapped to non-templated?") ? static_cast <void> (0) : __assert_fail ("FoundByLookup->getDescribedFunctionTemplate() && \"Templated function mapped to non-templated?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3334, __PRETTY_FUNCTION__)) | ||||||||||
3334 | "Templated function mapped to non-templated?")((FoundByLookup->getDescribedFunctionTemplate() && "Templated function mapped to non-templated?") ? static_cast <void> (0) : __assert_fail ("FoundByLookup->getDescribedFunctionTemplate() && \"Templated function mapped to non-templated?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3334, __PRETTY_FUNCTION__)); | ||||||||||
3335 | Importer.MapImported(DescribedD, | ||||||||||
3336 | FoundByLookup->getDescribedFunctionTemplate()); | ||||||||||
3337 | } | ||||||||||
3338 | return Importer.MapImported(D, FoundByLookup); | ||||||||||
3339 | } else { | ||||||||||
3340 | // Let's continue and build up the redecl chain in this case. | ||||||||||
3341 | // FIXME Merge the functions into one decl. | ||||||||||
3342 | } | ||||||||||
3343 | } | ||||||||||
3344 | } | ||||||||||
3345 | } | ||||||||||
3346 | |||||||||||
3347 | DeclarationNameInfo NameInfo(Name, Loc); | ||||||||||
3348 | // Import additional name location/type info. | ||||||||||
3349 | if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo)) | ||||||||||
3350 | return std::move(Err); | ||||||||||
3351 | |||||||||||
3352 | QualType FromTy = D->getType(); | ||||||||||
3353 | // Set to true if we do not import the type of the function as is. There are | ||||||||||
3354 | // cases when the original type would result in an infinite recursion during | ||||||||||
3355 | // the import. To avoid an infinite recursion when importing, we create the | ||||||||||
3356 | // FunctionDecl with a simplified function type and update it only after the | ||||||||||
3357 | // relevant AST nodes are already imported. | ||||||||||
3358 | bool UsedDifferentProtoType = false; | ||||||||||
3359 | if (const auto *FromFPT = FromTy->getAs<FunctionProtoType>()) { | ||||||||||
3360 | QualType FromReturnTy = FromFPT->getReturnType(); | ||||||||||
3361 | // Functions with auto return type may define a struct inside their body | ||||||||||
3362 | // and the return type could refer to that struct. | ||||||||||
3363 | // E.g.: auto foo() { struct X{}; return X(); } | ||||||||||
3364 | // To avoid an infinite recursion when importing, create the FunctionDecl | ||||||||||
3365 | // with a simplified return type. | ||||||||||
3366 | if (hasAutoReturnTypeDeclaredInside(D)) { | ||||||||||
3367 | FromReturnTy = Importer.getFromContext().VoidTy; | ||||||||||
3368 | UsedDifferentProtoType = true; | ||||||||||
3369 | } | ||||||||||
3370 | FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); | ||||||||||
3371 | // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the | ||||||||||
3372 | // FunctionDecl that we are importing the FunctionProtoType for. | ||||||||||
3373 | // To avoid an infinite recursion when importing, create the FunctionDecl | ||||||||||
3374 | // with a simplified function type. | ||||||||||
3375 | if (FromEPI.ExceptionSpec.SourceDecl || | ||||||||||
3376 | FromEPI.ExceptionSpec.SourceTemplate || | ||||||||||
3377 | FromEPI.ExceptionSpec.NoexceptExpr) { | ||||||||||
3378 | FunctionProtoType::ExtProtoInfo DefaultEPI; | ||||||||||
3379 | FromEPI = DefaultEPI; | ||||||||||
3380 | UsedDifferentProtoType = true; | ||||||||||
3381 | } | ||||||||||
3382 | FromTy = Importer.getFromContext().getFunctionType( | ||||||||||
3383 | FromReturnTy, FromFPT->getParamTypes(), FromEPI); | ||||||||||
3384 | } | ||||||||||
3385 | |||||||||||
3386 | Error Err = Error::success(); | ||||||||||
3387 | auto T = importChecked(Err, FromTy); | ||||||||||
3388 | auto TInfo = importChecked(Err, D->getTypeSourceInfo()); | ||||||||||
3389 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | ||||||||||
3390 | auto ToEndLoc = importChecked(Err, D->getEndLoc()); | ||||||||||
3391 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | ||||||||||
3392 | auto TrailingRequiresClause = | ||||||||||
3393 | importChecked(Err, D->getTrailingRequiresClause()); | ||||||||||
3394 | if (Err) | ||||||||||
3395 | return std::move(Err); | ||||||||||
3396 | |||||||||||
3397 | // Import the function parameters. | ||||||||||
3398 | SmallVector<ParmVarDecl *, 8> Parameters; | ||||||||||
3399 | for (auto P : D->parameters()) { | ||||||||||
3400 | if (Expected<ParmVarDecl *> ToPOrErr = import(P)) | ||||||||||
3401 | Parameters.push_back(*ToPOrErr); | ||||||||||
3402 | else | ||||||||||
3403 | return ToPOrErr.takeError(); | ||||||||||
3404 | } | ||||||||||
3405 | |||||||||||
3406 | // Create the imported function. | ||||||||||
3407 | FunctionDecl *ToFunction = nullptr; | ||||||||||
3408 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { | ||||||||||
3409 | ExplicitSpecifier ESpec = | ||||||||||
3410 | importExplicitSpecifier(Err, FromConstructor->getExplicitSpecifier()); | ||||||||||
3411 | if (Err) | ||||||||||
3412 | return std::move(Err); | ||||||||||
3413 | if (GetImportedOrCreateDecl<CXXConstructorDecl>( | ||||||||||
3414 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), | ||||||||||
3415 | ToInnerLocStart, NameInfo, T, TInfo, ESpec, D->isInlineSpecified(), | ||||||||||
3416 | D->isImplicit(), D->getConstexprKind(), | ||||||||||
3417 | InheritedConstructor(), // FIXME: Properly import inherited | ||||||||||
3418 | // constructor info | ||||||||||
3419 | TrailingRequiresClause)) | ||||||||||
3420 | return ToFunction; | ||||||||||
3421 | } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) { | ||||||||||
3422 | |||||||||||
3423 | Error Err = Error::success(); | ||||||||||
3424 | auto ToOperatorDelete = importChecked( | ||||||||||
3425 | Err, const_cast<FunctionDecl *>(FromDtor->getOperatorDelete())); | ||||||||||
3426 | auto ToThisArg = importChecked(Err, FromDtor->getOperatorDeleteThisArg()); | ||||||||||
3427 | if (Err) | ||||||||||
3428 | return std::move(Err); | ||||||||||
3429 | |||||||||||
3430 | if (GetImportedOrCreateDecl<CXXDestructorDecl>( | ||||||||||
3431 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), | ||||||||||
3432 | ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(), | ||||||||||
3433 | D->isImplicit(), D->getConstexprKind(), TrailingRequiresClause)) | ||||||||||
3434 | return ToFunction; | ||||||||||
3435 | |||||||||||
3436 | CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction); | ||||||||||
3437 | |||||||||||
3438 | ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg); | ||||||||||
3439 | } else if (CXXConversionDecl *FromConversion = | ||||||||||
3440 | dyn_cast<CXXConversionDecl>(D)) { | ||||||||||
3441 | ExplicitSpecifier ESpec = | ||||||||||
3442 | importExplicitSpecifier(Err, FromConversion->getExplicitSpecifier()); | ||||||||||
3443 | if (Err) | ||||||||||
3444 | return std::move(Err); | ||||||||||
3445 | if (GetImportedOrCreateDecl<CXXConversionDecl>( | ||||||||||
3446 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), | ||||||||||
3447 | ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(), ESpec, | ||||||||||
3448 | D->getConstexprKind(), SourceLocation(), TrailingRequiresClause)) | ||||||||||
3449 | return ToFunction; | ||||||||||
3450 | } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) { | ||||||||||
3451 | if (GetImportedOrCreateDecl<CXXMethodDecl>( | ||||||||||
3452 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), | ||||||||||
3453 | ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(), | ||||||||||
3454 | Method->isInlineSpecified(), D->getConstexprKind(), | ||||||||||
3455 | SourceLocation(), TrailingRequiresClause)) | ||||||||||
3456 | return ToFunction; | ||||||||||
3457 | } else if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(D)) { | ||||||||||
3458 | ExplicitSpecifier ESpec = | ||||||||||
3459 | importExplicitSpecifier(Err, Guide->getExplicitSpecifier()); | ||||||||||
3460 | if (Err) | ||||||||||
3461 | return std::move(Err); | ||||||||||
3462 | if (GetImportedOrCreateDecl<CXXDeductionGuideDecl>( | ||||||||||
3463 | ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart, ESpec, | ||||||||||
3464 | NameInfo, T, TInfo, ToEndLoc)) | ||||||||||
3465 | return ToFunction; | ||||||||||
3466 | cast<CXXDeductionGuideDecl>(ToFunction) | ||||||||||
3467 | ->setIsCopyDeductionCandidate(Guide->isCopyDeductionCandidate()); | ||||||||||
3468 | } else { | ||||||||||
3469 | if (GetImportedOrCreateDecl( | ||||||||||
3470 | ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart, | ||||||||||
3471 | NameInfo, T, TInfo, D->getStorageClass(), D->isInlineSpecified(), | ||||||||||
3472 | D->hasWrittenPrototype(), D->getConstexprKind(), | ||||||||||
3473 | TrailingRequiresClause)) | ||||||||||
3474 | return ToFunction; | ||||||||||
3475 | } | ||||||||||
3476 | |||||||||||
3477 | // Connect the redecl chain. | ||||||||||
3478 | if (FoundByLookup) { | ||||||||||
3479 | auto *Recent = const_cast<FunctionDecl *>( | ||||||||||
3480 | FoundByLookup->getMostRecentDecl()); | ||||||||||
3481 | ToFunction->setPreviousDecl(Recent); | ||||||||||
3482 | // FIXME Probably we should merge exception specifications. E.g. In the | ||||||||||
3483 | // "To" context the existing function may have exception specification with | ||||||||||
3484 | // noexcept-unevaluated, while the newly imported function may have an | ||||||||||
3485 | // evaluated noexcept. A call to adjustExceptionSpec() on the imported | ||||||||||
3486 | // decl and its redeclarations may be required. | ||||||||||
3487 | } | ||||||||||
3488 | |||||||||||
3489 | ToFunction->setQualifierInfo(ToQualifierLoc); | ||||||||||
3490 | ToFunction->setAccess(D->getAccess()); | ||||||||||
3491 | ToFunction->setLexicalDeclContext(LexicalDC); | ||||||||||
3492 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); | ||||||||||
3493 | ToFunction->setTrivial(D->isTrivial()); | ||||||||||
3494 | ToFunction->setPure(D->isPure()); | ||||||||||
3495 | ToFunction->setDefaulted(D->isDefaulted()); | ||||||||||
3496 | ToFunction->setExplicitlyDefaulted(D->isExplicitlyDefaulted()); | ||||||||||
3497 | ToFunction->setDeletedAsWritten(D->isDeletedAsWritten()); | ||||||||||
3498 | ToFunction->setRangeEnd(ToEndLoc); | ||||||||||
3499 | |||||||||||
3500 | // Set the parameters. | ||||||||||
3501 | for (auto *Param : Parameters) { | ||||||||||
3502 | Param->setOwningFunction(ToFunction); | ||||||||||
3503 | ToFunction->addDeclInternal(Param); | ||||||||||
3504 | } | ||||||||||
3505 | ToFunction->setParams(Parameters); | ||||||||||
3506 | |||||||||||
3507 | // We need to complete creation of FunctionProtoTypeLoc manually with setting | ||||||||||
3508 | // params it refers to. | ||||||||||
3509 | if (TInfo) { | ||||||||||
3510 | if (auto ProtoLoc = | ||||||||||
3511 | TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) { | ||||||||||
3512 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) | ||||||||||
3513 | ProtoLoc.setParam(I, Parameters[I]); | ||||||||||
3514 | } | ||||||||||
3515 | } | ||||||||||
3516 | |||||||||||
3517 | // Import the describing template function, if any. | ||||||||||
3518 | if (FromFT) { | ||||||||||
3519 | auto ToFTOrErr = import(FromFT); | ||||||||||
3520 | if (!ToFTOrErr) | ||||||||||
3521 | return ToFTOrErr.takeError(); | ||||||||||
3522 | } | ||||||||||
3523 | |||||||||||
3524 | // Import Ctor initializers. | ||||||||||
3525 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { | ||||||||||
3526 | if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { | ||||||||||
3527 | SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers); | ||||||||||
3528 | // Import first, then allocate memory and copy if there was no error. | ||||||||||
3529 | if (Error Err = ImportContainerChecked( | ||||||||||
3530 | FromConstructor->inits(), CtorInitializers)) | ||||||||||
3531 | return std::move(Err); | ||||||||||
3532 | auto **Memory = | ||||||||||
3533 | new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; | ||||||||||
3534 | std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory); | ||||||||||
3535 | auto *ToCtor = cast<CXXConstructorDecl>(ToFunction); | ||||||||||
3536 | ToCtor->setCtorInitializers(Memory); | ||||||||||
3537 | ToCtor->setNumCtorInitializers(NumInitializers); | ||||||||||
3538 | } | ||||||||||
3539 | } | ||||||||||
3540 | |||||||||||
3541 | if (D->doesThisDeclarationHaveABody()) { | ||||||||||
3542 | Error Err = ImportFunctionDeclBody(D, ToFunction); | ||||||||||
3543 | |||||||||||
3544 | if (Err) | ||||||||||
3545 | return std::move(Err); | ||||||||||
3546 | } | ||||||||||
3547 | |||||||||||
3548 | // Import and set the original type in case we used another type. | ||||||||||
3549 | if (UsedDifferentProtoType) { | ||||||||||
3550 | if (ExpectedType TyOrErr = import(D->getType())) | ||||||||||
3551 | ToFunction->setType(*TyOrErr); | ||||||||||
3552 | else | ||||||||||
3553 | return TyOrErr.takeError(); | ||||||||||
3554 | } | ||||||||||
3555 | |||||||||||
3556 | // FIXME: Other bits to merge? | ||||||||||
3557 | |||||||||||
3558 | // If it is a template, import all related things. | ||||||||||
3559 | if (Error Err = ImportTemplateInformation(D, ToFunction)) | ||||||||||
3560 | return std::move(Err); | ||||||||||
3561 | |||||||||||
3562 | addDeclToContexts(D, ToFunction); | ||||||||||
3563 | |||||||||||
3564 | if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D)) | ||||||||||
3565 | if (Error Err = ImportOverriddenMethods(cast<CXXMethodDecl>(ToFunction), | ||||||||||
3566 | FromCXXMethod)) | ||||||||||
3567 | return std::move(Err); | ||||||||||
3568 | |||||||||||
3569 | // Import the rest of the chain. I.e. import all subsequent declarations. | ||||||||||
3570 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { | ||||||||||
3571 | ExpectedDecl ToRedeclOrErr = import(*RedeclIt); | ||||||||||
3572 | if (!ToRedeclOrErr) | ||||||||||
3573 | return ToRedeclOrErr.takeError(); | ||||||||||
3574 | } | ||||||||||
3575 | |||||||||||
3576 | return ToFunction; | ||||||||||
3577 | } | ||||||||||
3578 | |||||||||||
3579 | ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { | ||||||||||
3580 | return VisitFunctionDecl(D); | ||||||||||
3581 | } | ||||||||||
3582 | |||||||||||
3583 | ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { | ||||||||||
3584 | return VisitCXXMethodDecl(D); | ||||||||||
3585 | } | ||||||||||
3586 | |||||||||||
3587 | ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { | ||||||||||
3588 | return VisitCXXMethodDecl(D); | ||||||||||
3589 | } | ||||||||||
3590 | |||||||||||
3591 | ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { | ||||||||||
3592 | return VisitCXXMethodDecl(D); | ||||||||||
3593 | } | ||||||||||
3594 | |||||||||||
3595 | ExpectedDecl | ||||||||||
3596 | ASTNodeImporter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { | ||||||||||
3597 | return VisitFunctionDecl(D); | ||||||||||
3598 | } | ||||||||||
3599 | |||||||||||
3600 | ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { | ||||||||||
3601 | // Import the major distinguishing characteristics of a variable. | ||||||||||
3602 | DeclContext *DC, *LexicalDC; | ||||||||||
3603 | DeclarationName Name; | ||||||||||
3604 | SourceLocation Loc; | ||||||||||
3605 | NamedDecl *ToD; | ||||||||||
3606 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
3607 | return std::move(Err); | ||||||||||
3608 | if (ToD) | ||||||||||
3609 | return ToD; | ||||||||||
3610 | |||||||||||
3611 | // Determine whether we've already imported this field. | ||||||||||
3612 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
3613 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
3614 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) { | ||||||||||
3615 | // For anonymous fields, match up by index. | ||||||||||
3616 | if (!Name && | ||||||||||
3617 | ASTImporter::getFieldIndex(D) != | ||||||||||
3618 | ASTImporter::getFieldIndex(FoundField)) | ||||||||||
3619 | continue; | ||||||||||
3620 | |||||||||||
3621 | if (Importer.IsStructurallyEquivalent(D->getType(), | ||||||||||
3622 | FoundField->getType())) { | ||||||||||
3623 | Importer.MapImported(D, FoundField); | ||||||||||
3624 | // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the | ||||||||||
3625 | // initializer of a FieldDecl might not had been instantiated in the | ||||||||||
3626 | // "To" context. However, the "From" context might instantiated that, | ||||||||||
3627 | // thus we have to merge that. | ||||||||||
3628 | if (Expr *FromInitializer = D->getInClassInitializer()) { | ||||||||||
3629 | // We don't have yet the initializer set. | ||||||||||
3630 | if (FoundField->hasInClassInitializer() && | ||||||||||
3631 | !FoundField->getInClassInitializer()) { | ||||||||||
3632 | if (ExpectedExpr ToInitializerOrErr = import(FromInitializer)) | ||||||||||
3633 | FoundField->setInClassInitializer(*ToInitializerOrErr); | ||||||||||
3634 | else { | ||||||||||
3635 | // We can't return error here, | ||||||||||
3636 | // since we already mapped D as imported. | ||||||||||
3637 | // FIXME: warning message? | ||||||||||
3638 | consumeError(ToInitializerOrErr.takeError()); | ||||||||||
3639 | return FoundField; | ||||||||||
3640 | } | ||||||||||
3641 | } | ||||||||||
3642 | } | ||||||||||
3643 | return FoundField; | ||||||||||
3644 | } | ||||||||||
3645 | |||||||||||
3646 | // FIXME: Why is this case not handled with calling HandleNameConflict? | ||||||||||
3647 | Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent) | ||||||||||
3648 | << Name << D->getType() << FoundField->getType(); | ||||||||||
3649 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) | ||||||||||
3650 | << FoundField->getType(); | ||||||||||
3651 | |||||||||||
3652 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
3653 | } | ||||||||||
3654 | } | ||||||||||
3655 | |||||||||||
3656 | Error Err = Error::success(); | ||||||||||
3657 | auto ToType = importChecked(Err, D->getType()); | ||||||||||
3658 | auto ToTInfo = importChecked(Err, D->getTypeSourceInfo()); | ||||||||||
3659 | auto ToBitWidth = importChecked(Err, D->getBitWidth()); | ||||||||||
3660 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | ||||||||||
3661 | auto ToInitializer = importChecked(Err, D->getInClassInitializer()); | ||||||||||
3662 | if (Err) | ||||||||||
3663 | return std::move(Err); | ||||||||||
3664 | const Type *ToCapturedVLAType = nullptr; | ||||||||||
3665 | if (Error Err = Importer.importInto( | ||||||||||
3666 | ToCapturedVLAType, cast_or_null<Type>(D->getCapturedVLAType()))) | ||||||||||
3667 | return std::move(Err); | ||||||||||
3668 | |||||||||||
3669 | FieldDecl *ToField; | ||||||||||
3670 | if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC, | ||||||||||
3671 | ToInnerLocStart, Loc, Name.getAsIdentifierInfo(), | ||||||||||
3672 | ToType, ToTInfo, ToBitWidth, D->isMutable(), | ||||||||||
3673 | D->getInClassInitStyle())) | ||||||||||
3674 | return ToField; | ||||||||||
3675 | |||||||||||
3676 | ToField->setAccess(D->getAccess()); | ||||||||||
3677 | ToField->setLexicalDeclContext(LexicalDC); | ||||||||||
3678 | if (ToInitializer) | ||||||||||
3679 | ToField->setInClassInitializer(ToInitializer); | ||||||||||
3680 | ToField->setImplicit(D->isImplicit()); | ||||||||||
3681 | if (ToCapturedVLAType) | ||||||||||
3682 | ToField->setCapturedVLAType(cast<VariableArrayType>(ToCapturedVLAType)); | ||||||||||
3683 | LexicalDC->addDeclInternal(ToField); | ||||||||||
3684 | return ToField; | ||||||||||
3685 | } | ||||||||||
3686 | |||||||||||
3687 | ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { | ||||||||||
3688 | // Import the major distinguishing characteristics of a variable. | ||||||||||
3689 | DeclContext *DC, *LexicalDC; | ||||||||||
3690 | DeclarationName Name; | ||||||||||
3691 | SourceLocation Loc; | ||||||||||
3692 | NamedDecl *ToD; | ||||||||||
3693 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
3694 | return std::move(Err); | ||||||||||
3695 | if (ToD) | ||||||||||
3696 | return ToD; | ||||||||||
3697 | |||||||||||
3698 | // Determine whether we've already imported this field. | ||||||||||
3699 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
3700 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { | ||||||||||
3701 | if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { | ||||||||||
3702 | // For anonymous indirect fields, match up by index. | ||||||||||
3703 | if (!Name && | ||||||||||
3704 | ASTImporter::getFieldIndex(D) != | ||||||||||
3705 | ASTImporter::getFieldIndex(FoundField)) | ||||||||||
3706 | continue; | ||||||||||
3707 | |||||||||||
3708 | if (Importer.IsStructurallyEquivalent(D->getType(), | ||||||||||
3709 | FoundField->getType(), | ||||||||||
3710 | !Name.isEmpty())) { | ||||||||||
3711 | Importer.MapImported(D, FoundField); | ||||||||||
3712 | return FoundField; | ||||||||||
3713 | } | ||||||||||
3714 | |||||||||||
3715 | // If there are more anonymous fields to check, continue. | ||||||||||
3716 | if (!Name && I < N-1) | ||||||||||
3717 | continue; | ||||||||||
3718 | |||||||||||
3719 | // FIXME: Why is this case not handled with calling HandleNameConflict? | ||||||||||
3720 | Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent) | ||||||||||
3721 | << Name << D->getType() << FoundField->getType(); | ||||||||||
3722 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) | ||||||||||
3723 | << FoundField->getType(); | ||||||||||
3724 | |||||||||||
3725 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
3726 | } | ||||||||||
3727 | } | ||||||||||
3728 | |||||||||||
3729 | // Import the type. | ||||||||||
3730 | auto TypeOrErr = import(D->getType()); | ||||||||||
3731 | if (!TypeOrErr) | ||||||||||
3732 | return TypeOrErr.takeError(); | ||||||||||
3733 | |||||||||||
3734 | auto **NamedChain = | ||||||||||
3735 | new (Importer.getToContext()) NamedDecl*[D->getChainingSize()]; | ||||||||||
3736 | |||||||||||
3737 | unsigned i = 0; | ||||||||||
3738 | for (auto *PI : D->chain()) | ||||||||||
3739 | if (Expected<NamedDecl *> ToD = import(PI)) | ||||||||||
3740 | NamedChain[i++] = *ToD; | ||||||||||
3741 | else | ||||||||||
3742 | return ToD.takeError(); | ||||||||||
3743 | |||||||||||
3744 | llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()}; | ||||||||||
3745 | IndirectFieldDecl *ToIndirectField; | ||||||||||
3746 | if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC, | ||||||||||
3747 | Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH)) | ||||||||||
3748 | // FIXME here we leak `NamedChain` which is allocated before | ||||||||||
3749 | return ToIndirectField; | ||||||||||
3750 | |||||||||||
3751 | ToIndirectField->setAccess(D->getAccess()); | ||||||||||
3752 | ToIndirectField->setLexicalDeclContext(LexicalDC); | ||||||||||
3753 | LexicalDC->addDeclInternal(ToIndirectField); | ||||||||||
3754 | return ToIndirectField; | ||||||||||
3755 | } | ||||||||||
3756 | |||||||||||
3757 | /// Used as return type of getFriendCountAndPosition. | ||||||||||
3758 | struct FriendCountAndPosition { | ||||||||||
3759 | /// Number of similar looking friends. | ||||||||||
3760 | unsigned int TotalCount; | ||||||||||
3761 | /// Index of the specific FriendDecl. | ||||||||||
3762 | unsigned int IndexOfDecl; | ||||||||||
3763 | }; | ||||||||||
3764 | |||||||||||
3765 | template <class T> | ||||||||||
3766 | static FriendCountAndPosition getFriendCountAndPosition( | ||||||||||
3767 | const FriendDecl *FD, | ||||||||||
3768 | llvm::function_ref<T(const FriendDecl *)> GetCanTypeOrDecl) { | ||||||||||
3769 | unsigned int FriendCount = 0; | ||||||||||
3770 | llvm::Optional<unsigned int> FriendPosition; | ||||||||||
3771 | const auto *RD = cast<CXXRecordDecl>(FD->getLexicalDeclContext()); | ||||||||||
3772 | |||||||||||
3773 | T TypeOrDecl = GetCanTypeOrDecl(FD); | ||||||||||
3774 | |||||||||||
3775 | for (const FriendDecl *FoundFriend : RD->friends()) { | ||||||||||
3776 | if (FoundFriend == FD) { | ||||||||||
3777 | FriendPosition = FriendCount; | ||||||||||
3778 | ++FriendCount; | ||||||||||
3779 | } else if (!FoundFriend->getFriendDecl() == !FD->getFriendDecl() && | ||||||||||
3780 | GetCanTypeOrDecl(FoundFriend) == TypeOrDecl) { | ||||||||||
3781 | ++FriendCount; | ||||||||||
3782 | } | ||||||||||
3783 | } | ||||||||||
3784 | |||||||||||
3785 | assert(FriendPosition && "Friend decl not found in own parent.")((FriendPosition && "Friend decl not found in own parent." ) ? static_cast<void> (0) : __assert_fail ("FriendPosition && \"Friend decl not found in own parent.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3785, __PRETTY_FUNCTION__)); | ||||||||||
3786 | |||||||||||
3787 | return {FriendCount, *FriendPosition}; | ||||||||||
3788 | } | ||||||||||
3789 | |||||||||||
3790 | static FriendCountAndPosition getFriendCountAndPosition(const FriendDecl *FD) { | ||||||||||
3791 | if (FD->getFriendType()) | ||||||||||
3792 | return getFriendCountAndPosition<QualType>(FD, [](const FriendDecl *F) { | ||||||||||
3793 | if (TypeSourceInfo *TSI = F->getFriendType()) | ||||||||||
3794 | return TSI->getType().getCanonicalType(); | ||||||||||
3795 | llvm_unreachable("Wrong friend object type.")::llvm::llvm_unreachable_internal("Wrong friend object type." , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3795); | ||||||||||
3796 | }); | ||||||||||
3797 | else | ||||||||||
3798 | return getFriendCountAndPosition<Decl *>(FD, [](const FriendDecl *F) { | ||||||||||
3799 | if (Decl *D = F->getFriendDecl()) | ||||||||||
3800 | return D->getCanonicalDecl(); | ||||||||||
3801 | llvm_unreachable("Wrong friend object type.")::llvm::llvm_unreachable_internal("Wrong friend object type." , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3801); | ||||||||||
3802 | }); | ||||||||||
3803 | } | ||||||||||
3804 | |||||||||||
3805 | ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) { | ||||||||||
3806 | // Import the major distinguishing characteristics of a declaration. | ||||||||||
3807 | DeclContext *DC, *LexicalDC; | ||||||||||
3808 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | ||||||||||
3809 | return std::move(Err); | ||||||||||
3810 | |||||||||||
3811 | // Determine whether we've already imported this decl. | ||||||||||
3812 | // FriendDecl is not a NamedDecl so we cannot use lookup. | ||||||||||
3813 | // We try to maintain order and count of redundant friend declarations. | ||||||||||
3814 | const auto *RD = cast<CXXRecordDecl>(DC); | ||||||||||
3815 | FriendDecl *ImportedFriend = RD->getFirstFriend(); | ||||||||||
3816 | SmallVector<FriendDecl *, 2> ImportedEquivalentFriends; | ||||||||||
3817 | |||||||||||
3818 | while (ImportedFriend) { | ||||||||||
3819 | bool Match = false; | ||||||||||
3820 | if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) { | ||||||||||
3821 | Match = | ||||||||||
3822 | IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(), | ||||||||||
3823 | /*Complain=*/false); | ||||||||||
3824 | } else if (D->getFriendType() && ImportedFriend->getFriendType()) { | ||||||||||
3825 | Match = Importer.IsStructurallyEquivalent( | ||||||||||
3826 | D->getFriendType()->getType(), | ||||||||||
3827 | ImportedFriend->getFriendType()->getType(), /*Complain=*/false); | ||||||||||
3828 | } | ||||||||||
3829 | if (Match) | ||||||||||
3830 | ImportedEquivalentFriends.push_back(ImportedFriend); | ||||||||||
3831 | |||||||||||
3832 | ImportedFriend = ImportedFriend->getNextFriend(); | ||||||||||
3833 | } | ||||||||||
3834 | FriendCountAndPosition CountAndPosition = getFriendCountAndPosition(D); | ||||||||||
3835 | |||||||||||
3836 | assert(ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount &&((ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && "Class with non-matching friends is imported, ODR check wrong?" ) ? static_cast<void> (0) : __assert_fail ("ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && \"Class with non-matching friends is imported, ODR check wrong?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3837, __PRETTY_FUNCTION__)) | ||||||||||
3837 | "Class with non-matching friends is imported, ODR check wrong?")((ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && "Class with non-matching friends is imported, ODR check wrong?" ) ? static_cast<void> (0) : __assert_fail ("ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && \"Class with non-matching friends is imported, ODR check wrong?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3837, __PRETTY_FUNCTION__)); | ||||||||||
3838 | if (ImportedEquivalentFriends.size() == CountAndPosition.TotalCount) | ||||||||||
3839 | return Importer.MapImported( | ||||||||||
3840 | D, ImportedEquivalentFriends[CountAndPosition.IndexOfDecl]); | ||||||||||
3841 | |||||||||||
3842 | // Not found. Create it. | ||||||||||
3843 | // The declarations will be put into order later by ImportDeclContext. | ||||||||||
3844 | FriendDecl::FriendUnion ToFU; | ||||||||||
3845 | if (NamedDecl *FriendD = D->getFriendDecl()) { | ||||||||||
3846 | NamedDecl *ToFriendD; | ||||||||||
3847 | if (Error Err = importInto(ToFriendD, FriendD)) | ||||||||||
3848 | return std::move(Err); | ||||||||||
3849 | |||||||||||
3850 | if (FriendD->getFriendObjectKind() != Decl::FOK_None && | ||||||||||
3851 | !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator))) | ||||||||||
3852 | ToFriendD->setObjectOfFriendDecl(false); | ||||||||||
3853 | |||||||||||
3854 | ToFU = ToFriendD; | ||||||||||
3855 | } else { // The friend is a type, not a decl. | ||||||||||
3856 | if (auto TSIOrErr = import(D->getFriendType())) | ||||||||||
3857 | ToFU = *TSIOrErr; | ||||||||||
3858 | else | ||||||||||
3859 | return TSIOrErr.takeError(); | ||||||||||
3860 | } | ||||||||||
3861 | |||||||||||
3862 | SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists); | ||||||||||
3863 | auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>(); | ||||||||||
3864 | for (unsigned I = 0; I < D->NumTPLists; I++) { | ||||||||||
3865 | if (auto ListOrErr = import(FromTPLists[I])) | ||||||||||
3866 | ToTPLists[I] = *ListOrErr; | ||||||||||
3867 | else | ||||||||||
3868 | return ListOrErr.takeError(); | ||||||||||
3869 | } | ||||||||||
3870 | |||||||||||
3871 | auto LocationOrErr = import(D->getLocation()); | ||||||||||
3872 | if (!LocationOrErr) | ||||||||||
3873 | return LocationOrErr.takeError(); | ||||||||||
3874 | auto FriendLocOrErr = import(D->getFriendLoc()); | ||||||||||
3875 | if (!FriendLocOrErr) | ||||||||||
3876 | return FriendLocOrErr.takeError(); | ||||||||||
3877 | |||||||||||
3878 | FriendDecl *FrD; | ||||||||||
3879 | if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC, | ||||||||||
3880 | *LocationOrErr, ToFU, | ||||||||||
3881 | *FriendLocOrErr, ToTPLists)) | ||||||||||
3882 | return FrD; | ||||||||||
3883 | |||||||||||
3884 | FrD->setAccess(D->getAccess()); | ||||||||||
3885 | FrD->setLexicalDeclContext(LexicalDC); | ||||||||||
3886 | LexicalDC->addDeclInternal(FrD); | ||||||||||
3887 | return FrD; | ||||||||||
3888 | } | ||||||||||
3889 | |||||||||||
3890 | ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { | ||||||||||
3891 | // Import the major distinguishing characteristics of an ivar. | ||||||||||
3892 | DeclContext *DC, *LexicalDC; | ||||||||||
3893 | DeclarationName Name; | ||||||||||
3894 | SourceLocation Loc; | ||||||||||
3895 | NamedDecl *ToD; | ||||||||||
3896 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
3897 | return std::move(Err); | ||||||||||
3898 | if (ToD) | ||||||||||
3899 | return ToD; | ||||||||||
3900 | |||||||||||
3901 | // Determine whether we've already imported this ivar | ||||||||||
3902 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
3903 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
3904 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) { | ||||||||||
3905 | if (Importer.IsStructurallyEquivalent(D->getType(), | ||||||||||
3906 | FoundIvar->getType())) { | ||||||||||
3907 | Importer.MapImported(D, FoundIvar); | ||||||||||
3908 | return FoundIvar; | ||||||||||
3909 | } | ||||||||||
3910 | |||||||||||
3911 | Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent) | ||||||||||
3912 | << Name << D->getType() << FoundIvar->getType(); | ||||||||||
3913 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) | ||||||||||
3914 | << FoundIvar->getType(); | ||||||||||
3915 | |||||||||||
3916 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
3917 | } | ||||||||||
3918 | } | ||||||||||
3919 | |||||||||||
3920 | Error Err = Error::success(); | ||||||||||
3921 | auto ToType = importChecked(Err, D->getType()); | ||||||||||
3922 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | ||||||||||
3923 | auto ToBitWidth = importChecked(Err, D->getBitWidth()); | ||||||||||
3924 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | ||||||||||
3925 | if (Err) | ||||||||||
3926 | return std::move(Err); | ||||||||||
3927 | |||||||||||
3928 | ObjCIvarDecl *ToIvar; | ||||||||||
3929 | if (GetImportedOrCreateDecl( | ||||||||||
3930 | ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC), | ||||||||||
3931 | ToInnerLocStart, Loc, Name.getAsIdentifierInfo(), | ||||||||||
3932 | ToType, ToTypeSourceInfo, | ||||||||||
3933 | D->getAccessControl(),ToBitWidth, D->getSynthesize())) | ||||||||||
3934 | return ToIvar; | ||||||||||
3935 | |||||||||||
3936 | ToIvar->setLexicalDeclContext(LexicalDC); | ||||||||||
3937 | LexicalDC->addDeclInternal(ToIvar); | ||||||||||
3938 | return ToIvar; | ||||||||||
3939 | } | ||||||||||
3940 | |||||||||||
3941 | ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) { | ||||||||||
3942 | |||||||||||
3943 | SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D); | ||||||||||
3944 | auto RedeclIt = Redecls.begin(); | ||||||||||
3945 | // Import the first part of the decl chain. I.e. import all previous | ||||||||||
3946 | // declarations starting from the canonical decl. | ||||||||||
3947 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { | ||||||||||
3948 | ExpectedDecl RedeclOrErr = import(*RedeclIt); | ||||||||||
3949 | if (!RedeclOrErr) | ||||||||||
3950 | return RedeclOrErr.takeError(); | ||||||||||
3951 | } | ||||||||||
3952 | assert(*RedeclIt == D)((*RedeclIt == D) ? static_cast<void> (0) : __assert_fail ("*RedeclIt == D", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 3952, __PRETTY_FUNCTION__)); | ||||||||||
3953 | |||||||||||
3954 | // Import the major distinguishing characteristics of a variable. | ||||||||||
3955 | DeclContext *DC, *LexicalDC; | ||||||||||
3956 | DeclarationName Name; | ||||||||||
3957 | SourceLocation Loc; | ||||||||||
3958 | NamedDecl *ToD; | ||||||||||
3959 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
3960 | return std::move(Err); | ||||||||||
3961 | if (ToD) | ||||||||||
3962 | return ToD; | ||||||||||
3963 | |||||||||||
3964 | // Try to find a variable in our own ("to") context with the same name and | ||||||||||
3965 | // in the same context as the variable we're importing. | ||||||||||
3966 | VarDecl *FoundByLookup = nullptr; | ||||||||||
3967 | if (D->isFileVarDecl()) { | ||||||||||
3968 | SmallVector<NamedDecl *, 4> ConflictingDecls; | ||||||||||
3969 | unsigned IDNS = Decl::IDNS_Ordinary; | ||||||||||
3970 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
3971 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
3972 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | ||||||||||
3973 | continue; | ||||||||||
3974 | |||||||||||
3975 | if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) { | ||||||||||
3976 | if (!hasSameVisibilityContextAndLinkage(FoundVar, D)) | ||||||||||
3977 | continue; | ||||||||||
3978 | if (Importer.IsStructurallyEquivalent(D->getType(), | ||||||||||
3979 | FoundVar->getType())) { | ||||||||||
3980 | |||||||||||
3981 | // The VarDecl in the "From" context has a definition, but in the | ||||||||||
3982 | // "To" context we already have a definition. | ||||||||||
3983 | VarDecl *FoundDef = FoundVar->getDefinition(); | ||||||||||
3984 | if (D->isThisDeclarationADefinition() && FoundDef) | ||||||||||
3985 | // FIXME Check for ODR error if the two definitions have | ||||||||||
3986 | // different initializers? | ||||||||||
3987 | return Importer.MapImported(D, FoundDef); | ||||||||||
3988 | |||||||||||
3989 | // The VarDecl in the "From" context has an initializer, but in the | ||||||||||
3990 | // "To" context we already have an initializer. | ||||||||||
3991 | const VarDecl *FoundDInit = nullptr; | ||||||||||
3992 | if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit)) | ||||||||||
3993 | // FIXME Diagnose ODR error if the two initializers are different? | ||||||||||
3994 | return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit)); | ||||||||||
3995 | |||||||||||
3996 | FoundByLookup = FoundVar; | ||||||||||
3997 | break; | ||||||||||
3998 | } | ||||||||||
3999 | |||||||||||
4000 | const ArrayType *FoundArray | ||||||||||
4001 | = Importer.getToContext().getAsArrayType(FoundVar->getType()); | ||||||||||
4002 | const ArrayType *TArray | ||||||||||
4003 | = Importer.getToContext().getAsArrayType(D->getType()); | ||||||||||
4004 | if (FoundArray && TArray) { | ||||||||||
4005 | if (isa<IncompleteArrayType>(FoundArray) && | ||||||||||
4006 | isa<ConstantArrayType>(TArray)) { | ||||||||||
4007 | // Import the type. | ||||||||||
4008 | if (auto TyOrErr = import(D->getType())) | ||||||||||
4009 | FoundVar->setType(*TyOrErr); | ||||||||||
4010 | else | ||||||||||
4011 | return TyOrErr.takeError(); | ||||||||||
4012 | |||||||||||
4013 | FoundByLookup = FoundVar; | ||||||||||
4014 | break; | ||||||||||
4015 | } else if (isa<IncompleteArrayType>(TArray) && | ||||||||||
4016 | isa<ConstantArrayType>(FoundArray)) { | ||||||||||
4017 | FoundByLookup = FoundVar; | ||||||||||
4018 | break; | ||||||||||
4019 | } | ||||||||||
4020 | } | ||||||||||
4021 | |||||||||||
4022 | Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent) | ||||||||||
4023 | << Name << D->getType() << FoundVar->getType(); | ||||||||||
4024 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) | ||||||||||
4025 | << FoundVar->getType(); | ||||||||||
4026 | ConflictingDecls.push_back(FoundDecl); | ||||||||||
4027 | } | ||||||||||
4028 | } | ||||||||||
4029 | |||||||||||
4030 | if (!ConflictingDecls.empty()) { | ||||||||||
4031 | ExpectedName NameOrErr = Importer.HandleNameConflict( | ||||||||||
4032 | Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); | ||||||||||
4033 | if (NameOrErr) | ||||||||||
4034 | Name = NameOrErr.get(); | ||||||||||
4035 | else | ||||||||||
4036 | return NameOrErr.takeError(); | ||||||||||
4037 | } | ||||||||||
4038 | } | ||||||||||
4039 | |||||||||||
4040 | Error Err = Error::success(); | ||||||||||
4041 | auto ToType = importChecked(Err, D->getType()); | ||||||||||
4042 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | ||||||||||
4043 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | ||||||||||
4044 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | ||||||||||
4045 | if (Err) | ||||||||||
4046 | return std::move(Err); | ||||||||||
4047 | |||||||||||
4048 | // Create the imported variable. | ||||||||||
4049 | VarDecl *ToVar; | ||||||||||
4050 | if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC, | ||||||||||
4051 | ToInnerLocStart, Loc, | ||||||||||
4052 | Name.getAsIdentifierInfo(), | ||||||||||
4053 | ToType, ToTypeSourceInfo, | ||||||||||
4054 | D->getStorageClass())) | ||||||||||
4055 | return ToVar; | ||||||||||
4056 | |||||||||||
4057 | ToVar->setTSCSpec(D->getTSCSpec()); | ||||||||||
4058 | ToVar->setQualifierInfo(ToQualifierLoc); | ||||||||||
4059 | ToVar->setAccess(D->getAccess()); | ||||||||||
4060 | ToVar->setLexicalDeclContext(LexicalDC); | ||||||||||
4061 | |||||||||||
4062 | if (FoundByLookup) { | ||||||||||
4063 | auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl()); | ||||||||||
4064 | ToVar->setPreviousDecl(Recent); | ||||||||||
4065 | } | ||||||||||
4066 | |||||||||||
4067 | // Import the described template, if any. | ||||||||||
4068 | if (D->getDescribedVarTemplate()) { | ||||||||||
4069 | auto ToVTOrErr = import(D->getDescribedVarTemplate()); | ||||||||||
4070 | if (!ToVTOrErr) | ||||||||||
4071 | return ToVTOrErr.takeError(); | ||||||||||
4072 | } | ||||||||||
4073 | |||||||||||
4074 | if (Error Err = ImportInitializer(D, ToVar)) | ||||||||||
4075 | return std::move(Err); | ||||||||||
4076 | |||||||||||
4077 | if (D->isConstexpr()) | ||||||||||
4078 | ToVar->setConstexpr(true); | ||||||||||
4079 | |||||||||||
4080 | addDeclToContexts(D, ToVar); | ||||||||||
4081 | |||||||||||
4082 | // Import the rest of the chain. I.e. import all subsequent declarations. | ||||||||||
4083 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { | ||||||||||
4084 | ExpectedDecl RedeclOrErr = import(*RedeclIt); | ||||||||||
4085 | if (!RedeclOrErr) | ||||||||||
4086 | return RedeclOrErr.takeError(); | ||||||||||
4087 | } | ||||||||||
4088 | |||||||||||
4089 | return ToVar; | ||||||||||
4090 | } | ||||||||||
4091 | |||||||||||
4092 | ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { | ||||||||||
4093 | // Parameters are created in the translation unit's context, then moved | ||||||||||
4094 | // into the function declaration's context afterward. | ||||||||||
4095 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); | ||||||||||
4096 | |||||||||||
4097 | Error Err = Error::success(); | ||||||||||
4098 | auto ToDeclName = importChecked(Err, D->getDeclName()); | ||||||||||
4099 | auto ToLocation = importChecked(Err, D->getLocation()); | ||||||||||
4100 | auto ToType = importChecked(Err, D->getType()); | ||||||||||
4101 | if (Err) | ||||||||||
4102 | return std::move(Err); | ||||||||||
4103 | |||||||||||
4104 | // Create the imported parameter. | ||||||||||
4105 | ImplicitParamDecl *ToParm = nullptr; | ||||||||||
4106 | if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, | ||||||||||
4107 | ToLocation, ToDeclName.getAsIdentifierInfo(), | ||||||||||
4108 | ToType, D->getParameterKind())) | ||||||||||
4109 | return ToParm; | ||||||||||
4110 | return ToParm; | ||||||||||
4111 | } | ||||||||||
4112 | |||||||||||
4113 | Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl( | ||||||||||
4114 | const ParmVarDecl *FromParam, ParmVarDecl *ToParam) { | ||||||||||
4115 | ToParam->setHasInheritedDefaultArg(FromParam->hasInheritedDefaultArg()); | ||||||||||
4116 | ToParam->setKNRPromoted(FromParam->isKNRPromoted()); | ||||||||||
4117 | |||||||||||
4118 | if (FromParam->hasUninstantiatedDefaultArg()) { | ||||||||||
4119 | if (auto ToDefArgOrErr = import(FromParam->getUninstantiatedDefaultArg())) | ||||||||||
4120 | ToParam->setUninstantiatedDefaultArg(*ToDefArgOrErr); | ||||||||||
4121 | else | ||||||||||
4122 | return ToDefArgOrErr.takeError(); | ||||||||||
4123 | } else if (FromParam->hasUnparsedDefaultArg()) { | ||||||||||
4124 | ToParam->setUnparsedDefaultArg(); | ||||||||||
4125 | } else if (FromParam->hasDefaultArg()) { | ||||||||||
4126 | if (auto ToDefArgOrErr = import(FromParam->getDefaultArg())) | ||||||||||
4127 | ToParam->setDefaultArg(*ToDefArgOrErr); | ||||||||||
4128 | else | ||||||||||
4129 | return ToDefArgOrErr.takeError(); | ||||||||||
4130 | } | ||||||||||
4131 | |||||||||||
4132 | return Error::success(); | ||||||||||
4133 | } | ||||||||||
4134 | |||||||||||
4135 | ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { | ||||||||||
4136 | // Parameters are created in the translation unit's context, then moved | ||||||||||
4137 | // into the function declaration's context afterward. | ||||||||||
4138 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); | ||||||||||
4139 | |||||||||||
4140 | Error Err = Error::success(); | ||||||||||
4141 | auto ToDeclName = importChecked(Err, D->getDeclName()); | ||||||||||
4142 | auto ToLocation = importChecked(Err, D->getLocation()); | ||||||||||
4143 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | ||||||||||
4144 | auto ToType = importChecked(Err, D->getType()); | ||||||||||
4145 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | ||||||||||
4146 | if (Err) | ||||||||||
4147 | return std::move(Err); | ||||||||||
4148 | |||||||||||
4149 | ParmVarDecl *ToParm; | ||||||||||
4150 | if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, | ||||||||||
4151 | ToInnerLocStart, ToLocation, | ||||||||||
4152 | ToDeclName.getAsIdentifierInfo(), ToType, | ||||||||||
4153 | ToTypeSourceInfo, D->getStorageClass(), | ||||||||||
4154 | /*DefaultArg*/ nullptr)) | ||||||||||
4155 | return ToParm; | ||||||||||
4156 | |||||||||||
4157 | // Set the default argument. It should be no problem if it was already done. | ||||||||||
4158 | // Do not import the default expression before GetImportedOrCreateDecl call | ||||||||||
4159 | // to avoid possible infinite import loop because circular dependency. | ||||||||||
4160 | if (Error Err = ImportDefaultArgOfParmVarDecl(D, ToParm)) | ||||||||||
4161 | return std::move(Err); | ||||||||||
4162 | |||||||||||
4163 | if (D->isObjCMethodParameter()) { | ||||||||||
4164 | ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex()); | ||||||||||
4165 | ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier()); | ||||||||||
4166 | } else { | ||||||||||
4167 | ToParm->setScopeInfo(D->getFunctionScopeDepth(), | ||||||||||
4168 | D->getFunctionScopeIndex()); | ||||||||||
4169 | } | ||||||||||
4170 | |||||||||||
4171 | return ToParm; | ||||||||||
4172 | } | ||||||||||
4173 | |||||||||||
4174 | ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { | ||||||||||
4175 | // Import the major distinguishing characteristics of a method. | ||||||||||
4176 | DeclContext *DC, *LexicalDC; | ||||||||||
4177 | DeclarationName Name; | ||||||||||
4178 | SourceLocation Loc; | ||||||||||
4179 | NamedDecl *ToD; | ||||||||||
4180 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
4181 | return std::move(Err); | ||||||||||
4182 | if (ToD) | ||||||||||
4183 | return ToD; | ||||||||||
4184 | |||||||||||
4185 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
4186 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
4187 | if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) { | ||||||||||
4188 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) | ||||||||||
4189 | continue; | ||||||||||
4190 | |||||||||||
4191 | // Check return types. | ||||||||||
4192 | if (!Importer.IsStructurallyEquivalent(D->getReturnType(), | ||||||||||
4193 | FoundMethod->getReturnType())) { | ||||||||||
4194 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent) | ||||||||||
4195 | << D->isInstanceMethod() << Name << D->getReturnType() | ||||||||||
4196 | << FoundMethod->getReturnType(); | ||||||||||
4197 | Importer.ToDiag(FoundMethod->getLocation(), | ||||||||||
4198 | diag::note_odr_objc_method_here) | ||||||||||
4199 | << D->isInstanceMethod() << Name; | ||||||||||
4200 | |||||||||||
4201 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
4202 | } | ||||||||||
4203 | |||||||||||
4204 | // Check the number of parameters. | ||||||||||
4205 | if (D->param_size() != FoundMethod->param_size()) { | ||||||||||
4206 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent) | ||||||||||
4207 | << D->isInstanceMethod() << Name | ||||||||||
4208 | << D->param_size() << FoundMethod->param_size(); | ||||||||||
4209 | Importer.ToDiag(FoundMethod->getLocation(), | ||||||||||
4210 | diag::note_odr_objc_method_here) | ||||||||||
4211 | << D->isInstanceMethod() << Name; | ||||||||||
4212 | |||||||||||
4213 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
4214 | } | ||||||||||
4215 | |||||||||||
4216 | // Check parameter types. | ||||||||||
4217 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), | ||||||||||
4218 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); | ||||||||||
4219 | P != PEnd; ++P, ++FoundP) { | ||||||||||
4220 | if (!Importer.IsStructurallyEquivalent((*P)->getType(), | ||||||||||
4221 | (*FoundP)->getType())) { | ||||||||||
4222 | Importer.FromDiag((*P)->getLocation(), | ||||||||||
4223 | diag::warn_odr_objc_method_param_type_inconsistent) | ||||||||||
4224 | << D->isInstanceMethod() << Name | ||||||||||
4225 | << (*P)->getType() << (*FoundP)->getType(); | ||||||||||
4226 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) | ||||||||||
4227 | << (*FoundP)->getType(); | ||||||||||
4228 | |||||||||||
4229 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
4230 | } | ||||||||||
4231 | } | ||||||||||
4232 | |||||||||||
4233 | // Check variadic/non-variadic. | ||||||||||
4234 | // Check the number of parameters. | ||||||||||
4235 | if (D->isVariadic() != FoundMethod->isVariadic()) { | ||||||||||
4236 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent) | ||||||||||
4237 | << D->isInstanceMethod() << Name; | ||||||||||
4238 | Importer.ToDiag(FoundMethod->getLocation(), | ||||||||||
4239 | diag::note_odr_objc_method_here) | ||||||||||
4240 | << D->isInstanceMethod() << Name; | ||||||||||
4241 | |||||||||||
4242 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
4243 | } | ||||||||||
4244 | |||||||||||
4245 | // FIXME: Any other bits we need to merge? | ||||||||||
4246 | return Importer.MapImported(D, FoundMethod); | ||||||||||
4247 | } | ||||||||||
4248 | } | ||||||||||
4249 | |||||||||||
4250 | Error Err = Error::success(); | ||||||||||
4251 | auto ToEndLoc = importChecked(Err, D->getEndLoc()); | ||||||||||
4252 | auto ToReturnType = importChecked(Err, D->getReturnType()); | ||||||||||
4253 | auto ToReturnTypeSourceInfo = | ||||||||||
4254 | importChecked(Err, D->getReturnTypeSourceInfo()); | ||||||||||
4255 | if (Err) | ||||||||||
4256 | return std::move(Err); | ||||||||||
4257 | |||||||||||
4258 | ObjCMethodDecl *ToMethod; | ||||||||||
4259 | if (GetImportedOrCreateDecl( | ||||||||||
4260 | ToMethod, D, Importer.getToContext(), Loc, ToEndLoc, | ||||||||||
4261 | Name.getObjCSelector(), ToReturnType, ToReturnTypeSourceInfo, DC, | ||||||||||
4262 | D->isInstanceMethod(), D->isVariadic(), D->isPropertyAccessor(), | ||||||||||
4263 | D->isSynthesizedAccessorStub(), D->isImplicit(), D->isDefined(), | ||||||||||
4264 | D->getImplementationControl(), D->hasRelatedResultType())) | ||||||||||
4265 | return ToMethod; | ||||||||||
4266 | |||||||||||
4267 | // FIXME: When we decide to merge method definitions, we'll need to | ||||||||||
4268 | // deal with implicit parameters. | ||||||||||
4269 | |||||||||||
4270 | // Import the parameters | ||||||||||
4271 | SmallVector<ParmVarDecl *, 5> ToParams; | ||||||||||
4272 | for (auto *FromP : D->parameters()) { | ||||||||||
4273 | if (Expected<ParmVarDecl *> ToPOrErr = import(FromP)) | ||||||||||
4274 | ToParams.push_back(*ToPOrErr); | ||||||||||
4275 | else | ||||||||||
4276 | return ToPOrErr.takeError(); | ||||||||||
4277 | } | ||||||||||
4278 | |||||||||||
4279 | // Set the parameters. | ||||||||||
4280 | for (auto *ToParam : ToParams) { | ||||||||||
4281 | ToParam->setOwningFunction(ToMethod); | ||||||||||
4282 | ToMethod->addDeclInternal(ToParam); | ||||||||||
4283 | } | ||||||||||
4284 | |||||||||||
4285 | SmallVector<SourceLocation, 12> FromSelLocs; | ||||||||||
4286 | D->getSelectorLocs(FromSelLocs); | ||||||||||
4287 | SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size()); | ||||||||||
4288 | if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs)) | ||||||||||
4289 | return std::move(Err); | ||||||||||
4290 | |||||||||||
4291 | ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs); | ||||||||||
4292 | |||||||||||
4293 | ToMethod->setLexicalDeclContext(LexicalDC); | ||||||||||
4294 | LexicalDC->addDeclInternal(ToMethod); | ||||||||||
4295 | |||||||||||
4296 | // Implicit params are declared when Sema encounters the definition but this | ||||||||||
4297 | // never happens when the method is imported. Manually declare the implicit | ||||||||||
4298 | // params now that the MethodDecl knows its class interface. | ||||||||||
4299 | if (D->getSelfDecl()) | ||||||||||
4300 | ToMethod->createImplicitParams(Importer.getToContext(), | ||||||||||
4301 | ToMethod->getClassInterface()); | ||||||||||
4302 | |||||||||||
4303 | return ToMethod; | ||||||||||
4304 | } | ||||||||||
4305 | |||||||||||
4306 | ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { | ||||||||||
4307 | // Import the major distinguishing characteristics of a category. | ||||||||||
4308 | DeclContext *DC, *LexicalDC; | ||||||||||
4309 | DeclarationName Name; | ||||||||||
4310 | SourceLocation Loc; | ||||||||||
4311 | NamedDecl *ToD; | ||||||||||
4312 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
4313 | return std::move(Err); | ||||||||||
4314 | if (ToD) | ||||||||||
4315 | return ToD; | ||||||||||
4316 | |||||||||||
4317 | Error Err = Error::success(); | ||||||||||
4318 | auto ToVarianceLoc = importChecked(Err, D->getVarianceLoc()); | ||||||||||
4319 | auto ToLocation = importChecked(Err, D->getLocation()); | ||||||||||
4320 | auto ToColonLoc = importChecked(Err, D->getColonLoc()); | ||||||||||
4321 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | ||||||||||
4322 | if (Err) | ||||||||||
4323 | return std::move(Err); | ||||||||||
4324 | |||||||||||
4325 | ObjCTypeParamDecl *Result; | ||||||||||
4326 | if (GetImportedOrCreateDecl( | ||||||||||
4327 | Result, D, Importer.getToContext(), DC, D->getVariance(), | ||||||||||
4328 | ToVarianceLoc, D->getIndex(), | ||||||||||
4329 | ToLocation, Name.getAsIdentifierInfo(), | ||||||||||
4330 | ToColonLoc, ToTypeSourceInfo)) | ||||||||||
4331 | return Result; | ||||||||||
4332 | |||||||||||
4333 | Result->setLexicalDeclContext(LexicalDC); | ||||||||||
4334 | return Result; | ||||||||||
4335 | } | ||||||||||
4336 | |||||||||||
4337 | ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { | ||||||||||
4338 | // Import the major distinguishing characteristics of a category. | ||||||||||
4339 | DeclContext *DC, *LexicalDC; | ||||||||||
4340 | DeclarationName Name; | ||||||||||
4341 | SourceLocation Loc; | ||||||||||
4342 | NamedDecl *ToD; | ||||||||||
4343 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
4344 | return std::move(Err); | ||||||||||
4345 | if (ToD) | ||||||||||
4346 | return ToD; | ||||||||||
4347 | |||||||||||
4348 | ObjCInterfaceDecl *ToInterface; | ||||||||||
4349 | if (Error Err = importInto(ToInterface, D->getClassInterface())) | ||||||||||
4350 | return std::move(Err); | ||||||||||
4351 | |||||||||||
4352 | // Determine if we've already encountered this category. | ||||||||||
4353 | ObjCCategoryDecl *MergeWithCategory | ||||||||||
4354 | = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); | ||||||||||
4355 | ObjCCategoryDecl *ToCategory = MergeWithCategory; | ||||||||||
4356 | if (!ToCategory) { | ||||||||||
4357 | |||||||||||
4358 | Error Err = Error::success(); | ||||||||||
4359 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); | ||||||||||
4360 | auto ToCategoryNameLoc = importChecked(Err, D->getCategoryNameLoc()); | ||||||||||
4361 | auto ToIvarLBraceLoc = importChecked(Err, D->getIvarLBraceLoc()); | ||||||||||
4362 | auto ToIvarRBraceLoc = importChecked(Err, D->getIvarRBraceLoc()); | ||||||||||
4363 | if (Err) | ||||||||||
4364 | return std::move(Err); | ||||||||||
4365 | |||||||||||
4366 | if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC, | ||||||||||
4367 | ToAtStartLoc, Loc, | ||||||||||
4368 | ToCategoryNameLoc, | ||||||||||
4369 | Name.getAsIdentifierInfo(), ToInterface, | ||||||||||
4370 | /*TypeParamList=*/nullptr, | ||||||||||
4371 | ToIvarLBraceLoc, | ||||||||||
4372 | ToIvarRBraceLoc)) | ||||||||||
4373 | return ToCategory; | ||||||||||
4374 | |||||||||||
4375 | ToCategory->setLexicalDeclContext(LexicalDC); | ||||||||||
4376 | LexicalDC->addDeclInternal(ToCategory); | ||||||||||
4377 | // Import the type parameter list after MapImported, to avoid | ||||||||||
4378 | // loops when bringing in their DeclContext. | ||||||||||
4379 | if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList())) | ||||||||||
4380 | ToCategory->setTypeParamList(*PListOrErr); | ||||||||||
4381 | else | ||||||||||
4382 | return PListOrErr.takeError(); | ||||||||||
4383 | |||||||||||
4384 | // Import protocols | ||||||||||
4385 | SmallVector<ObjCProtocolDecl *, 4> Protocols; | ||||||||||
4386 | SmallVector<SourceLocation, 4> ProtocolLocs; | ||||||||||
4387 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc | ||||||||||
4388 | = D->protocol_loc_begin(); | ||||||||||
4389 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), | ||||||||||
4390 | FromProtoEnd = D->protocol_end(); | ||||||||||
4391 | FromProto != FromProtoEnd; | ||||||||||
4392 | ++FromProto, ++FromProtoLoc) { | ||||||||||
4393 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto)) | ||||||||||
4394 | Protocols.push_back(*ToProtoOrErr); | ||||||||||
4395 | else | ||||||||||
4396 | return ToProtoOrErr.takeError(); | ||||||||||
4397 | |||||||||||
4398 | if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc)) | ||||||||||
4399 | ProtocolLocs.push_back(*ToProtoLocOrErr); | ||||||||||
4400 | else | ||||||||||
4401 | return ToProtoLocOrErr.takeError(); | ||||||||||
4402 | } | ||||||||||
4403 | |||||||||||
4404 | // FIXME: If we're merging, make sure that the protocol list is the same. | ||||||||||
4405 | ToCategory->setProtocolList(Protocols.data(), Protocols.size(), | ||||||||||
4406 | ProtocolLocs.data(), Importer.getToContext()); | ||||||||||
4407 | |||||||||||
4408 | } else { | ||||||||||
4409 | Importer.MapImported(D, ToCategory); | ||||||||||
4410 | } | ||||||||||
4411 | |||||||||||
4412 | // Import all of the members of this category. | ||||||||||
4413 | if (Error Err = ImportDeclContext(D)) | ||||||||||
4414 | return std::move(Err); | ||||||||||
4415 | |||||||||||
4416 | // If we have an implementation, import it as well. | ||||||||||
4417 | if (D->getImplementation()) { | ||||||||||
4418 | if (Expected<ObjCCategoryImplDecl *> ToImplOrErr = | ||||||||||
4419 | import(D->getImplementation())) | ||||||||||
4420 | ToCategory->setImplementation(*ToImplOrErr); | ||||||||||
4421 | else | ||||||||||
4422 | return ToImplOrErr.takeError(); | ||||||||||
4423 | } | ||||||||||
4424 | |||||||||||
4425 | return ToCategory; | ||||||||||
4426 | } | ||||||||||
4427 | |||||||||||
4428 | Error ASTNodeImporter::ImportDefinition( | ||||||||||
4429 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) { | ||||||||||
4430 | if (To->getDefinition()) { | ||||||||||
4431 | if (shouldForceImportDeclContext(Kind)) | ||||||||||
4432 | if (Error Err = ImportDeclContext(From)) | ||||||||||
4433 | return Err; | ||||||||||
4434 | return Error::success(); | ||||||||||
4435 | } | ||||||||||
4436 | |||||||||||
4437 | // Start the protocol definition | ||||||||||
4438 | To->startDefinition(); | ||||||||||
4439 | |||||||||||
4440 | // Import protocols | ||||||||||
4441 | SmallVector<ObjCProtocolDecl *, 4> Protocols; | ||||||||||
4442 | SmallVector<SourceLocation, 4> ProtocolLocs; | ||||||||||
4443 | ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc = | ||||||||||
4444 | From->protocol_loc_begin(); | ||||||||||
4445 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), | ||||||||||
4446 | FromProtoEnd = From->protocol_end(); | ||||||||||
4447 | FromProto != FromProtoEnd; | ||||||||||
4448 | ++FromProto, ++FromProtoLoc) { | ||||||||||
4449 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto)) | ||||||||||
4450 | Protocols.push_back(*ToProtoOrErr); | ||||||||||
4451 | else | ||||||||||
4452 | return ToProtoOrErr.takeError(); | ||||||||||
4453 | |||||||||||
4454 | if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc)) | ||||||||||
4455 | ProtocolLocs.push_back(*ToProtoLocOrErr); | ||||||||||
4456 | else | ||||||||||
4457 | return ToProtoLocOrErr.takeError(); | ||||||||||
4458 | |||||||||||
4459 | } | ||||||||||
4460 | |||||||||||
4461 | // FIXME: If we're merging, make sure that the protocol list is the same. | ||||||||||
4462 | To->setProtocolList(Protocols.data(), Protocols.size(), | ||||||||||
4463 | ProtocolLocs.data(), Importer.getToContext()); | ||||||||||
4464 | |||||||||||
4465 | if (shouldForceImportDeclContext(Kind)) { | ||||||||||
4466 | // Import all of the members of this protocol. | ||||||||||
4467 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) | ||||||||||
4468 | return Err; | ||||||||||
4469 | } | ||||||||||
4470 | return Error::success(); | ||||||||||
4471 | } | ||||||||||
4472 | |||||||||||
4473 | ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { | ||||||||||
4474 | // If this protocol has a definition in the translation unit we're coming | ||||||||||
4475 | // from, but this particular declaration is not that definition, import the | ||||||||||
4476 | // definition and map to that. | ||||||||||
4477 | ObjCProtocolDecl *Definition = D->getDefinition(); | ||||||||||
4478 | if (Definition && Definition != D) { | ||||||||||
4479 | if (ExpectedDecl ImportedDefOrErr = import(Definition)) | ||||||||||
4480 | return Importer.MapImported(D, *ImportedDefOrErr); | ||||||||||
4481 | else | ||||||||||
4482 | return ImportedDefOrErr.takeError(); | ||||||||||
4483 | } | ||||||||||
4484 | |||||||||||
4485 | // Import the major distinguishing characteristics of a protocol. | ||||||||||
4486 | DeclContext *DC, *LexicalDC; | ||||||||||
4487 | DeclarationName Name; | ||||||||||
4488 | SourceLocation Loc; | ||||||||||
4489 | NamedDecl *ToD; | ||||||||||
4490 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
4491 | return std::move(Err); | ||||||||||
4492 | if (ToD) | ||||||||||
4493 | return ToD; | ||||||||||
4494 | |||||||||||
4495 | ObjCProtocolDecl *MergeWithProtocol = nullptr; | ||||||||||
4496 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
4497 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
4498 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) | ||||||||||
4499 | continue; | ||||||||||
4500 | |||||||||||
4501 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl))) | ||||||||||
4502 | break; | ||||||||||
4503 | } | ||||||||||
4504 | |||||||||||
4505 | ObjCProtocolDecl *ToProto = MergeWithProtocol; | ||||||||||
4506 | if (!ToProto) { | ||||||||||
4507 | auto ToAtBeginLocOrErr = import(D->getAtStartLoc()); | ||||||||||
4508 | if (!ToAtBeginLocOrErr) | ||||||||||
4509 | return ToAtBeginLocOrErr.takeError(); | ||||||||||
4510 | |||||||||||
4511 | if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC, | ||||||||||
4512 | Name.getAsIdentifierInfo(), Loc, | ||||||||||
4513 | *ToAtBeginLocOrErr, | ||||||||||
4514 | /*PrevDecl=*/nullptr)) | ||||||||||
4515 | return ToProto; | ||||||||||
4516 | ToProto->setLexicalDeclContext(LexicalDC); | ||||||||||
4517 | LexicalDC->addDeclInternal(ToProto); | ||||||||||
4518 | } | ||||||||||
4519 | |||||||||||
4520 | Importer.MapImported(D, ToProto); | ||||||||||
4521 | |||||||||||
4522 | if (D->isThisDeclarationADefinition()) | ||||||||||
4523 | if (Error Err = ImportDefinition(D, ToProto)) | ||||||||||
4524 | return std::move(Err); | ||||||||||
4525 | |||||||||||
4526 | return ToProto; | ||||||||||
4527 | } | ||||||||||
4528 | |||||||||||
4529 | ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { | ||||||||||
4530 | DeclContext *DC, *LexicalDC; | ||||||||||
4531 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | ||||||||||
4532 | return std::move(Err); | ||||||||||
4533 | |||||||||||
4534 | ExpectedSLoc ExternLocOrErr = import(D->getExternLoc()); | ||||||||||
4535 | if (!ExternLocOrErr) | ||||||||||
4536 | return ExternLocOrErr.takeError(); | ||||||||||
4537 | |||||||||||
4538 | ExpectedSLoc LangLocOrErr = import(D->getLocation()); | ||||||||||
4539 | if (!LangLocOrErr) | ||||||||||
4540 | return LangLocOrErr.takeError(); | ||||||||||
4541 | |||||||||||
4542 | bool HasBraces = D->hasBraces(); | ||||||||||
4543 | |||||||||||
4544 | LinkageSpecDecl *ToLinkageSpec; | ||||||||||
4545 | if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC, | ||||||||||
4546 | *ExternLocOrErr, *LangLocOrErr, | ||||||||||
4547 | D->getLanguage(), HasBraces)) | ||||||||||
4548 | return ToLinkageSpec; | ||||||||||
4549 | |||||||||||
4550 | if (HasBraces) { | ||||||||||
4551 | ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc()); | ||||||||||
4552 | if (!RBraceLocOrErr) | ||||||||||
4553 | return RBraceLocOrErr.takeError(); | ||||||||||
4554 | ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr); | ||||||||||
4555 | } | ||||||||||
4556 | |||||||||||
4557 | ToLinkageSpec->setLexicalDeclContext(LexicalDC); | ||||||||||
4558 | LexicalDC->addDeclInternal(ToLinkageSpec); | ||||||||||
4559 | |||||||||||
4560 | return ToLinkageSpec; | ||||||||||
4561 | } | ||||||||||
4562 | |||||||||||
4563 | ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) { | ||||||||||
4564 | DeclContext *DC, *LexicalDC; | ||||||||||
4565 | DeclarationName Name; | ||||||||||
4566 | SourceLocation Loc; | ||||||||||
4567 | NamedDecl *ToD = nullptr; | ||||||||||
4568 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
4569 | return std::move(Err); | ||||||||||
4570 | if (ToD) | ||||||||||
4571 | return ToD; | ||||||||||
4572 | |||||||||||
4573 | Error Err = Error::success(); | ||||||||||
4574 | auto ToLoc = importChecked(Err, D->getNameInfo().getLoc()); | ||||||||||
4575 | auto ToUsingLoc = importChecked(Err, D->getUsingLoc()); | ||||||||||
4576 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | ||||||||||
4577 | if (Err) | ||||||||||
4578 | return std::move(Err); | ||||||||||
4579 | |||||||||||
4580 | DeclarationNameInfo NameInfo(Name, ToLoc); | ||||||||||
4581 | if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo)) | ||||||||||
4582 | return std::move(Err); | ||||||||||
4583 | |||||||||||
4584 | UsingDecl *ToUsing; | ||||||||||
4585 | if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC, | ||||||||||
4586 | ToUsingLoc, ToQualifierLoc, NameInfo, | ||||||||||
4587 | D->hasTypename())) | ||||||||||
4588 | return ToUsing; | ||||||||||
4589 | |||||||||||
4590 | ToUsing->setLexicalDeclContext(LexicalDC); | ||||||||||
4591 | LexicalDC->addDeclInternal(ToUsing); | ||||||||||
4592 | |||||||||||
4593 | if (NamedDecl *FromPattern = | ||||||||||
4594 | Importer.getFromContext().getInstantiatedFromUsingDecl(D)) { | ||||||||||
4595 | if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern)) | ||||||||||
4596 | Importer.getToContext().setInstantiatedFromUsingDecl( | ||||||||||
4597 | ToUsing, *ToPatternOrErr); | ||||||||||
4598 | else | ||||||||||
4599 | return ToPatternOrErr.takeError(); | ||||||||||
4600 | } | ||||||||||
4601 | |||||||||||
4602 | for (UsingShadowDecl *FromShadow : D->shadows()) { | ||||||||||
4603 | if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow)) | ||||||||||
4604 | ToUsing->addShadowDecl(*ToShadowOrErr); | ||||||||||
4605 | else | ||||||||||
4606 | // FIXME: We return error here but the definition is already created | ||||||||||
4607 | // and available with lookups. How to fix this?.. | ||||||||||
4608 | return ToShadowOrErr.takeError(); | ||||||||||
4609 | } | ||||||||||
4610 | return ToUsing; | ||||||||||
4611 | } | ||||||||||
4612 | |||||||||||
4613 | ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) { | ||||||||||
4614 | DeclContext *DC, *LexicalDC; | ||||||||||
4615 | DeclarationName Name; | ||||||||||
4616 | SourceLocation Loc; | ||||||||||
4617 | NamedDecl *ToD = nullptr; | ||||||||||
4618 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
4619 | return std::move(Err); | ||||||||||
4620 | if (ToD) | ||||||||||
4621 | return ToD; | ||||||||||
4622 | |||||||||||
4623 | Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl()); | ||||||||||
4624 | if (!ToUsingOrErr) | ||||||||||
4625 | return ToUsingOrErr.takeError(); | ||||||||||
4626 | |||||||||||
4627 | Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl()); | ||||||||||
4628 | if (!ToTargetOrErr) | ||||||||||
4629 | return ToTargetOrErr.takeError(); | ||||||||||
4630 | |||||||||||
4631 | UsingShadowDecl *ToShadow; | ||||||||||
4632 | if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc, | ||||||||||
4633 | *ToUsingOrErr, *ToTargetOrErr)) | ||||||||||
4634 | return ToShadow; | ||||||||||
4635 | |||||||||||
4636 | ToShadow->setLexicalDeclContext(LexicalDC); | ||||||||||
4637 | ToShadow->setAccess(D->getAccess()); | ||||||||||
4638 | |||||||||||
4639 | if (UsingShadowDecl *FromPattern = | ||||||||||
4640 | Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) { | ||||||||||
4641 | if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern)) | ||||||||||
4642 | Importer.getToContext().setInstantiatedFromUsingShadowDecl( | ||||||||||
4643 | ToShadow, *ToPatternOrErr); | ||||||||||
4644 | else | ||||||||||
4645 | // FIXME: We return error here but the definition is already created | ||||||||||
4646 | // and available with lookups. How to fix this?.. | ||||||||||
4647 | return ToPatternOrErr.takeError(); | ||||||||||
4648 | } | ||||||||||
4649 | |||||||||||
4650 | LexicalDC->addDeclInternal(ToShadow); | ||||||||||
4651 | |||||||||||
4652 | return ToShadow; | ||||||||||
4653 | } | ||||||||||
4654 | |||||||||||
4655 | ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { | ||||||||||
4656 | DeclContext *DC, *LexicalDC; | ||||||||||
4657 | DeclarationName Name; | ||||||||||
4658 | SourceLocation Loc; | ||||||||||
4659 | NamedDecl *ToD = nullptr; | ||||||||||
4660 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
4661 | return std::move(Err); | ||||||||||
4662 | if (ToD) | ||||||||||
4663 | return ToD; | ||||||||||
4664 | |||||||||||
4665 | auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor()); | ||||||||||
4666 | if (!ToComAncestorOrErr) | ||||||||||
4667 | return ToComAncestorOrErr.takeError(); | ||||||||||
4668 | |||||||||||
4669 | Error Err = Error::success(); | ||||||||||
4670 | auto ToNominatedNamespace = importChecked(Err, D->getNominatedNamespace()); | ||||||||||
4671 | auto ToUsingLoc = importChecked(Err, D->getUsingLoc()); | ||||||||||
4672 | auto ToNamespaceKeyLocation = | ||||||||||
4673 | importChecked(Err, D->getNamespaceKeyLocation()); | ||||||||||
4674 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | ||||||||||
4675 | auto ToIdentLocation = importChecked(Err, D->getIdentLocation()); | ||||||||||
4676 | if (Err) | ||||||||||
4677 | return std::move(Err); | ||||||||||
4678 | |||||||||||
4679 | UsingDirectiveDecl *ToUsingDir; | ||||||||||
4680 | if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC, | ||||||||||
4681 | ToUsingLoc, | ||||||||||
4682 | ToNamespaceKeyLocation, | ||||||||||
4683 | ToQualifierLoc, | ||||||||||
4684 | ToIdentLocation, | ||||||||||
4685 | ToNominatedNamespace, *ToComAncestorOrErr)) | ||||||||||
4686 | return ToUsingDir; | ||||||||||
4687 | |||||||||||
4688 | ToUsingDir->setLexicalDeclContext(LexicalDC); | ||||||||||
4689 | LexicalDC->addDeclInternal(ToUsingDir); | ||||||||||
4690 | |||||||||||
4691 | return ToUsingDir; | ||||||||||
4692 | } | ||||||||||
4693 | |||||||||||
4694 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl( | ||||||||||
4695 | UnresolvedUsingValueDecl *D) { | ||||||||||
4696 | DeclContext *DC, *LexicalDC; | ||||||||||
4697 | DeclarationName Name; | ||||||||||
4698 | SourceLocation Loc; | ||||||||||
4699 | NamedDecl *ToD = nullptr; | ||||||||||
4700 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
4701 | return std::move(Err); | ||||||||||
4702 | if (ToD) | ||||||||||
4703 | return ToD; | ||||||||||
4704 | |||||||||||
4705 | Error Err = Error::success(); | ||||||||||
4706 | auto ToLoc = importChecked(Err, D->getNameInfo().getLoc()); | ||||||||||
4707 | auto ToUsingLoc = importChecked(Err, D->getUsingLoc()); | ||||||||||
4708 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | ||||||||||
4709 | auto ToEllipsisLoc = importChecked(Err, D->getEllipsisLoc()); | ||||||||||
4710 | if (Err) | ||||||||||
4711 | return std::move(Err); | ||||||||||
4712 | |||||||||||
4713 | DeclarationNameInfo NameInfo(Name, ToLoc); | ||||||||||
4714 | if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo)) | ||||||||||
4715 | return std::move(Err); | ||||||||||
4716 | |||||||||||
4717 | UnresolvedUsingValueDecl *ToUsingValue; | ||||||||||
4718 | if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC, | ||||||||||
4719 | ToUsingLoc, ToQualifierLoc, NameInfo, | ||||||||||
4720 | ToEllipsisLoc)) | ||||||||||
4721 | return ToUsingValue; | ||||||||||
4722 | |||||||||||
4723 | ToUsingValue->setAccess(D->getAccess()); | ||||||||||
4724 | ToUsingValue->setLexicalDeclContext(LexicalDC); | ||||||||||
4725 | LexicalDC->addDeclInternal(ToUsingValue); | ||||||||||
4726 | |||||||||||
4727 | return ToUsingValue; | ||||||||||
4728 | } | ||||||||||
4729 | |||||||||||
4730 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl( | ||||||||||
4731 | UnresolvedUsingTypenameDecl *D) { | ||||||||||
4732 | DeclContext *DC, *LexicalDC; | ||||||||||
4733 | DeclarationName Name; | ||||||||||
4734 | SourceLocation Loc; | ||||||||||
4735 | NamedDecl *ToD = nullptr; | ||||||||||
4736 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
4737 | return std::move(Err); | ||||||||||
4738 | if (ToD) | ||||||||||
4739 | return ToD; | ||||||||||
4740 | |||||||||||
4741 | Error Err = Error::success(); | ||||||||||
4742 | auto ToUsingLoc = importChecked(Err, D->getUsingLoc()); | ||||||||||
4743 | auto ToTypenameLoc = importChecked(Err, D->getTypenameLoc()); | ||||||||||
4744 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | ||||||||||
4745 | auto ToEllipsisLoc = importChecked(Err, D->getEllipsisLoc()); | ||||||||||
4746 | if (Err) | ||||||||||
4747 | return std::move(Err); | ||||||||||
4748 | |||||||||||
4749 | UnresolvedUsingTypenameDecl *ToUsing; | ||||||||||
4750 | if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC, | ||||||||||
4751 | ToUsingLoc, ToTypenameLoc, | ||||||||||
4752 | ToQualifierLoc, Loc, Name, ToEllipsisLoc)) | ||||||||||
4753 | return ToUsing; | ||||||||||
4754 | |||||||||||
4755 | ToUsing->setAccess(D->getAccess()); | ||||||||||
4756 | ToUsing->setLexicalDeclContext(LexicalDC); | ||||||||||
4757 | LexicalDC->addDeclInternal(ToUsing); | ||||||||||
4758 | |||||||||||
4759 | return ToUsing; | ||||||||||
4760 | } | ||||||||||
4761 | |||||||||||
4762 | ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { | ||||||||||
4763 | Decl* ToD = nullptr; | ||||||||||
4764 | switch (D->getBuiltinTemplateKind()) { | ||||||||||
4765 | case BuiltinTemplateKind::BTK__make_integer_seq: | ||||||||||
4766 | ToD = Importer.getToContext().getMakeIntegerSeqDecl(); | ||||||||||
4767 | break; | ||||||||||
4768 | case BuiltinTemplateKind::BTK__type_pack_element: | ||||||||||
4769 | ToD = Importer.getToContext().getTypePackElementDecl(); | ||||||||||
4770 | break; | ||||||||||
4771 | } | ||||||||||
4772 | assert(ToD && "BuiltinTemplateDecl of unsupported kind!")((ToD && "BuiltinTemplateDecl of unsupported kind!") ? static_cast<void> (0) : __assert_fail ("ToD && \"BuiltinTemplateDecl of unsupported kind!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 4772, __PRETTY_FUNCTION__)); | ||||||||||
4773 | Importer.MapImported(D, ToD); | ||||||||||
4774 | return ToD; | ||||||||||
4775 | } | ||||||||||
4776 | |||||||||||
4777 | Error ASTNodeImporter::ImportDefinition( | ||||||||||
4778 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) { | ||||||||||
4779 | if (To->getDefinition()) { | ||||||||||
4780 | // Check consistency of superclass. | ||||||||||
4781 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); | ||||||||||
4782 | if (FromSuper) { | ||||||||||
4783 | if (auto FromSuperOrErr = import(FromSuper)) | ||||||||||
4784 | FromSuper = *FromSuperOrErr; | ||||||||||
4785 | else | ||||||||||
4786 | return FromSuperOrErr.takeError(); | ||||||||||
4787 | } | ||||||||||
4788 | |||||||||||
4789 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); | ||||||||||
4790 | if ((bool)FromSuper != (bool)ToSuper || | ||||||||||
4791 | (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { | ||||||||||
4792 | Importer.ToDiag(To->getLocation(), | ||||||||||
4793 | diag::warn_odr_objc_superclass_inconsistent) | ||||||||||
4794 | << To->getDeclName(); | ||||||||||
4795 | if (ToSuper) | ||||||||||
4796 | Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) | ||||||||||
4797 | << To->getSuperClass()->getDeclName(); | ||||||||||
4798 | else | ||||||||||
4799 | Importer.ToDiag(To->getLocation(), | ||||||||||
4800 | diag::note_odr_objc_missing_superclass); | ||||||||||
4801 | if (From->getSuperClass()) | ||||||||||
4802 | Importer.FromDiag(From->getSuperClassLoc(), | ||||||||||
4803 | diag::note_odr_objc_superclass) | ||||||||||
4804 | << From->getSuperClass()->getDeclName(); | ||||||||||
4805 | else | ||||||||||
4806 | Importer.FromDiag(From->getLocation(), | ||||||||||
4807 | diag::note_odr_objc_missing_superclass); | ||||||||||
4808 | } | ||||||||||
4809 | |||||||||||
4810 | if (shouldForceImportDeclContext(Kind)) | ||||||||||
4811 | if (Error Err = ImportDeclContext(From)) | ||||||||||
4812 | return Err; | ||||||||||
4813 | return Error::success(); | ||||||||||
4814 | } | ||||||||||
4815 | |||||||||||
4816 | // Start the definition. | ||||||||||
4817 | To->startDefinition(); | ||||||||||
4818 | |||||||||||
4819 | // If this class has a superclass, import it. | ||||||||||
4820 | if (From->getSuperClass()) { | ||||||||||
4821 | if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo())) | ||||||||||
4822 | To->setSuperClass(*SuperTInfoOrErr); | ||||||||||
4823 | else | ||||||||||
4824 | return SuperTInfoOrErr.takeError(); | ||||||||||
4825 | } | ||||||||||
4826 | |||||||||||
4827 | // Import protocols | ||||||||||
4828 | SmallVector<ObjCProtocolDecl *, 4> Protocols; | ||||||||||
4829 | SmallVector<SourceLocation, 4> ProtocolLocs; | ||||||||||
4830 | ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc = | ||||||||||
4831 | From->protocol_loc_begin(); | ||||||||||
4832 | |||||||||||
4833 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), | ||||||||||
4834 | FromProtoEnd = From->protocol_end(); | ||||||||||
4835 | FromProto != FromProtoEnd; | ||||||||||
4836 | ++FromProto, ++FromProtoLoc) { | ||||||||||
4837 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto)) | ||||||||||
4838 | Protocols.push_back(*ToProtoOrErr); | ||||||||||
4839 | else | ||||||||||
4840 | return ToProtoOrErr.takeError(); | ||||||||||
4841 | |||||||||||
4842 | if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc)) | ||||||||||
4843 | ProtocolLocs.push_back(*ToProtoLocOrErr); | ||||||||||
4844 | else | ||||||||||
4845 | return ToProtoLocOrErr.takeError(); | ||||||||||
4846 | |||||||||||
4847 | } | ||||||||||
4848 | |||||||||||
4849 | // FIXME: If we're merging, make sure that the protocol list is the same. | ||||||||||
4850 | To->setProtocolList(Protocols.data(), Protocols.size(), | ||||||||||
4851 | ProtocolLocs.data(), Importer.getToContext()); | ||||||||||
4852 | |||||||||||
4853 | // Import categories. When the categories themselves are imported, they'll | ||||||||||
4854 | // hook themselves into this interface. | ||||||||||
4855 | for (auto *Cat : From->known_categories()) { | ||||||||||
4856 | auto ToCatOrErr = import(Cat); | ||||||||||
4857 | if (!ToCatOrErr) | ||||||||||
4858 | return ToCatOrErr.takeError(); | ||||||||||
4859 | } | ||||||||||
4860 | |||||||||||
4861 | // If we have an @implementation, import it as well. | ||||||||||
4862 | if (From->getImplementation()) { | ||||||||||
4863 | if (Expected<ObjCImplementationDecl *> ToImplOrErr = | ||||||||||
4864 | import(From->getImplementation())) | ||||||||||
4865 | To->setImplementation(*ToImplOrErr); | ||||||||||
4866 | else | ||||||||||
4867 | return ToImplOrErr.takeError(); | ||||||||||
4868 | } | ||||||||||
4869 | |||||||||||
4870 | // Import all of the members of this class. | ||||||||||
4871 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) | ||||||||||
4872 | return Err; | ||||||||||
4873 | |||||||||||
4874 | return Error::success(); | ||||||||||
4875 | } | ||||||||||
4876 | |||||||||||
4877 | Expected<ObjCTypeParamList *> | ||||||||||
4878 | ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { | ||||||||||
4879 | if (!list) | ||||||||||
4880 | return nullptr; | ||||||||||
4881 | |||||||||||
4882 | SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; | ||||||||||
4883 | for (auto *fromTypeParam : *list) { | ||||||||||
4884 | if (auto toTypeParamOrErr = import(fromTypeParam)) | ||||||||||
4885 | toTypeParams.push_back(*toTypeParamOrErr); | ||||||||||
4886 | else | ||||||||||
4887 | return toTypeParamOrErr.takeError(); | ||||||||||
4888 | } | ||||||||||
4889 | |||||||||||
4890 | auto LAngleLocOrErr = import(list->getLAngleLoc()); | ||||||||||
4891 | if (!LAngleLocOrErr) | ||||||||||
4892 | return LAngleLocOrErr.takeError(); | ||||||||||
4893 | |||||||||||
4894 | auto RAngleLocOrErr = import(list->getRAngleLoc()); | ||||||||||
4895 | if (!RAngleLocOrErr) | ||||||||||
4896 | return RAngleLocOrErr.takeError(); | ||||||||||
4897 | |||||||||||
4898 | return ObjCTypeParamList::create(Importer.getToContext(), | ||||||||||
4899 | *LAngleLocOrErr, | ||||||||||
4900 | toTypeParams, | ||||||||||
4901 | *RAngleLocOrErr); | ||||||||||
4902 | } | ||||||||||
4903 | |||||||||||
4904 | ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { | ||||||||||
4905 | // If this class has a definition in the translation unit we're coming from, | ||||||||||
4906 | // but this particular declaration is not that definition, import the | ||||||||||
4907 | // definition and map to that. | ||||||||||
4908 | ObjCInterfaceDecl *Definition = D->getDefinition(); | ||||||||||
4909 | if (Definition && Definition != D) { | ||||||||||
4910 | if (ExpectedDecl ImportedDefOrErr = import(Definition)) | ||||||||||
4911 | return Importer.MapImported(D, *ImportedDefOrErr); | ||||||||||
4912 | else | ||||||||||
4913 | return ImportedDefOrErr.takeError(); | ||||||||||
4914 | } | ||||||||||
4915 | |||||||||||
4916 | // Import the major distinguishing characteristics of an @interface. | ||||||||||
4917 | DeclContext *DC, *LexicalDC; | ||||||||||
4918 | DeclarationName Name; | ||||||||||
4919 | SourceLocation Loc; | ||||||||||
4920 | NamedDecl *ToD; | ||||||||||
4921 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
4922 | return std::move(Err); | ||||||||||
4923 | if (ToD) | ||||||||||
4924 | return ToD; | ||||||||||
4925 | |||||||||||
4926 | // Look for an existing interface with the same name. | ||||||||||
4927 | ObjCInterfaceDecl *MergeWithIface = nullptr; | ||||||||||
4928 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
4929 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
4930 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) | ||||||||||
4931 | continue; | ||||||||||
4932 | |||||||||||
4933 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl))) | ||||||||||
4934 | break; | ||||||||||
4935 | } | ||||||||||
4936 | |||||||||||
4937 | // Create an interface declaration, if one does not already exist. | ||||||||||
4938 | ObjCInterfaceDecl *ToIface = MergeWithIface; | ||||||||||
4939 | if (!ToIface) { | ||||||||||
4940 | ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc()); | ||||||||||
4941 | if (!AtBeginLocOrErr) | ||||||||||
4942 | return AtBeginLocOrErr.takeError(); | ||||||||||
4943 | |||||||||||
4944 | if (GetImportedOrCreateDecl( | ||||||||||
4945 | ToIface, D, Importer.getToContext(), DC, | ||||||||||
4946 | *AtBeginLocOrErr, Name.getAsIdentifierInfo(), | ||||||||||
4947 | /*TypeParamList=*/nullptr, | ||||||||||
4948 | /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl())) | ||||||||||
4949 | return ToIface; | ||||||||||
4950 | ToIface->setLexicalDeclContext(LexicalDC); | ||||||||||
4951 | LexicalDC->addDeclInternal(ToIface); | ||||||||||
4952 | } | ||||||||||
4953 | Importer.MapImported(D, ToIface); | ||||||||||
4954 | // Import the type parameter list after MapImported, to avoid | ||||||||||
4955 | // loops when bringing in their DeclContext. | ||||||||||
4956 | if (auto ToPListOrErr = | ||||||||||
4957 | ImportObjCTypeParamList(D->getTypeParamListAsWritten())) | ||||||||||
4958 | ToIface->setTypeParamList(*ToPListOrErr); | ||||||||||
4959 | else | ||||||||||
4960 | return ToPListOrErr.takeError(); | ||||||||||
4961 | |||||||||||
4962 | if (D->isThisDeclarationADefinition()) | ||||||||||
4963 | if (Error Err = ImportDefinition(D, ToIface)) | ||||||||||
4964 | return std::move(Err); | ||||||||||
4965 | |||||||||||
4966 | return ToIface; | ||||||||||
4967 | } | ||||||||||
4968 | |||||||||||
4969 | ExpectedDecl | ||||||||||
4970 | ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { | ||||||||||
4971 | ObjCCategoryDecl *Category; | ||||||||||
4972 | if (Error Err = importInto(Category, D->getCategoryDecl())) | ||||||||||
4973 | return std::move(Err); | ||||||||||
4974 | |||||||||||
4975 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); | ||||||||||
4976 | if (!ToImpl) { | ||||||||||
4977 | DeclContext *DC, *LexicalDC; | ||||||||||
4978 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | ||||||||||
4979 | return std::move(Err); | ||||||||||
4980 | |||||||||||
4981 | Error Err = Error::success(); | ||||||||||
4982 | auto ToLocation = importChecked(Err, D->getLocation()); | ||||||||||
4983 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); | ||||||||||
4984 | auto ToCategoryNameLoc = importChecked(Err, D->getCategoryNameLoc()); | ||||||||||
4985 | if (Err) | ||||||||||
4986 | return std::move(Err); | ||||||||||
4987 | |||||||||||
4988 | if (GetImportedOrCreateDecl( | ||||||||||
4989 | ToImpl, D, Importer.getToContext(), DC, | ||||||||||
4990 | Importer.Import(D->getIdentifier()), Category->getClassInterface(), | ||||||||||
4991 | ToLocation, ToAtStartLoc, ToCategoryNameLoc)) | ||||||||||
4992 | return ToImpl; | ||||||||||
4993 | |||||||||||
4994 | ToImpl->setLexicalDeclContext(LexicalDC); | ||||||||||
4995 | LexicalDC->addDeclInternal(ToImpl); | ||||||||||
4996 | Category->setImplementation(ToImpl); | ||||||||||
4997 | } | ||||||||||
4998 | |||||||||||
4999 | Importer.MapImported(D, ToImpl); | ||||||||||
5000 | if (Error Err = ImportDeclContext(D)) | ||||||||||
5001 | return std::move(Err); | ||||||||||
5002 | |||||||||||
5003 | return ToImpl; | ||||||||||
5004 | } | ||||||||||
5005 | |||||||||||
5006 | ExpectedDecl | ||||||||||
5007 | ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { | ||||||||||
5008 | // Find the corresponding interface. | ||||||||||
5009 | ObjCInterfaceDecl *Iface; | ||||||||||
5010 | if (Error Err = importInto(Iface, D->getClassInterface())) | ||||||||||
5011 | return std::move(Err); | ||||||||||
5012 | |||||||||||
5013 | // Import the superclass, if any. | ||||||||||
5014 | ObjCInterfaceDecl *Super; | ||||||||||
5015 | if (Error Err = importInto(Super, D->getSuperClass())) | ||||||||||
5016 | return std::move(Err); | ||||||||||
5017 | |||||||||||
5018 | ObjCImplementationDecl *Impl = Iface->getImplementation(); | ||||||||||
5019 | if (!Impl) { | ||||||||||
5020 | // We haven't imported an implementation yet. Create a new @implementation | ||||||||||
5021 | // now. | ||||||||||
5022 | DeclContext *DC, *LexicalDC; | ||||||||||
5023 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | ||||||||||
5024 | return std::move(Err); | ||||||||||
5025 | |||||||||||
5026 | Error Err = Error::success(); | ||||||||||
5027 | auto ToLocation = importChecked(Err, D->getLocation()); | ||||||||||
5028 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); | ||||||||||
5029 | auto ToSuperClassLoc = importChecked(Err, D->getSuperClassLoc()); | ||||||||||
5030 | auto ToIvarLBraceLoc = importChecked(Err, D->getIvarLBraceLoc()); | ||||||||||
5031 | auto ToIvarRBraceLoc = importChecked(Err, D->getIvarRBraceLoc()); | ||||||||||
5032 | if (Err) | ||||||||||
5033 | return std::move(Err); | ||||||||||
5034 | |||||||||||
5035 | if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(), | ||||||||||
5036 | DC, Iface, Super, | ||||||||||
5037 | ToLocation, | ||||||||||
5038 | ToAtStartLoc, | ||||||||||
5039 | ToSuperClassLoc, | ||||||||||
5040 | ToIvarLBraceLoc, | ||||||||||
5041 | ToIvarRBraceLoc)) | ||||||||||
5042 | return Impl; | ||||||||||
5043 | |||||||||||
5044 | Impl->setLexicalDeclContext(LexicalDC); | ||||||||||
5045 | |||||||||||
5046 | // Associate the implementation with the class it implements. | ||||||||||
5047 | Iface->setImplementation(Impl); | ||||||||||
5048 | Importer.MapImported(D, Iface->getImplementation()); | ||||||||||
5049 | } else { | ||||||||||
5050 | Importer.MapImported(D, Iface->getImplementation()); | ||||||||||
5051 | |||||||||||
5052 | // Verify that the existing @implementation has the same superclass. | ||||||||||
5053 | if ((Super && !Impl->getSuperClass()) || | ||||||||||
5054 | (!Super && Impl->getSuperClass()) || | ||||||||||
5055 | (Super && Impl->getSuperClass() && | ||||||||||
5056 | !declaresSameEntity(Super->getCanonicalDecl(), | ||||||||||
5057 | Impl->getSuperClass()))) { | ||||||||||
5058 | Importer.ToDiag(Impl->getLocation(), | ||||||||||
5059 | diag::warn_odr_objc_superclass_inconsistent) | ||||||||||
5060 | << Iface->getDeclName(); | ||||||||||
5061 | // FIXME: It would be nice to have the location of the superclass | ||||||||||
5062 | // below. | ||||||||||
5063 | if (Impl->getSuperClass()) | ||||||||||
5064 | Importer.ToDiag(Impl->getLocation(), | ||||||||||
5065 | diag::note_odr_objc_superclass) | ||||||||||
5066 | << Impl->getSuperClass()->getDeclName(); | ||||||||||
5067 | else | ||||||||||
5068 | Importer.ToDiag(Impl->getLocation(), | ||||||||||
5069 | diag::note_odr_objc_missing_superclass); | ||||||||||
5070 | if (D->getSuperClass()) | ||||||||||
5071 | Importer.FromDiag(D->getLocation(), | ||||||||||
5072 | diag::note_odr_objc_superclass) | ||||||||||
5073 | << D->getSuperClass()->getDeclName(); | ||||||||||
5074 | else | ||||||||||
5075 | Importer.FromDiag(D->getLocation(), | ||||||||||
5076 | diag::note_odr_objc_missing_superclass); | ||||||||||
5077 | |||||||||||
5078 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
5079 | } | ||||||||||
5080 | } | ||||||||||
5081 | |||||||||||
5082 | // Import all of the members of this @implementation. | ||||||||||
5083 | if (Error Err = ImportDeclContext(D)) | ||||||||||
5084 | return std::move(Err); | ||||||||||
5085 | |||||||||||
5086 | return Impl; | ||||||||||
5087 | } | ||||||||||
5088 | |||||||||||
5089 | ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { | ||||||||||
5090 | // Import the major distinguishing characteristics of an @property. | ||||||||||
5091 | DeclContext *DC, *LexicalDC; | ||||||||||
5092 | DeclarationName Name; | ||||||||||
5093 | SourceLocation Loc; | ||||||||||
5094 | NamedDecl *ToD; | ||||||||||
5095 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
5096 | return std::move(Err); | ||||||||||
5097 | if (ToD) | ||||||||||
5098 | return ToD; | ||||||||||
5099 | |||||||||||
5100 | // Check whether we have already imported this property. | ||||||||||
5101 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
5102 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
5103 | if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) { | ||||||||||
5104 | // Instance and class properties can share the same name but are different | ||||||||||
5105 | // declarations. | ||||||||||
5106 | if (FoundProp->isInstanceProperty() != D->isInstanceProperty()) | ||||||||||
5107 | continue; | ||||||||||
5108 | |||||||||||
5109 | // Check property types. | ||||||||||
5110 | if (!Importer.IsStructurallyEquivalent(D->getType(), | ||||||||||
5111 | FoundProp->getType())) { | ||||||||||
5112 | Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent) | ||||||||||
5113 | << Name << D->getType() << FoundProp->getType(); | ||||||||||
5114 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) | ||||||||||
5115 | << FoundProp->getType(); | ||||||||||
5116 | |||||||||||
5117 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
5118 | } | ||||||||||
5119 | |||||||||||
5120 | // FIXME: Check property attributes, getters, setters, etc.? | ||||||||||
5121 | |||||||||||
5122 | // Consider these properties to be equivalent. | ||||||||||
5123 | Importer.MapImported(D, FoundProp); | ||||||||||
5124 | return FoundProp; | ||||||||||
5125 | } | ||||||||||
5126 | } | ||||||||||
5127 | |||||||||||
5128 | Error Err = Error::success(); | ||||||||||
5129 | auto ToType = importChecked(Err, D->getType()); | ||||||||||
5130 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | ||||||||||
5131 | auto ToAtLoc = importChecked(Err, D->getAtLoc()); | ||||||||||
5132 | auto ToLParenLoc = importChecked(Err, D->getLParenLoc()); | ||||||||||
5133 | if (Err) | ||||||||||
5134 | return std::move(Err); | ||||||||||
5135 | |||||||||||
5136 | // Create the new property. | ||||||||||
5137 | ObjCPropertyDecl *ToProperty; | ||||||||||
5138 | if (GetImportedOrCreateDecl( | ||||||||||
5139 | ToProperty, D, Importer.getToContext(), DC, Loc, | ||||||||||
5140 | Name.getAsIdentifierInfo(), ToAtLoc, | ||||||||||
5141 | ToLParenLoc, ToType, | ||||||||||
5142 | ToTypeSourceInfo, D->getPropertyImplementation())) | ||||||||||
5143 | return ToProperty; | ||||||||||
5144 | |||||||||||
5145 | auto ToGetterName = importChecked(Err, D->getGetterName()); | ||||||||||
5146 | auto ToSetterName = importChecked(Err, D->getSetterName()); | ||||||||||
5147 | auto ToGetterNameLoc = importChecked(Err, D->getGetterNameLoc()); | ||||||||||
5148 | auto ToSetterNameLoc = importChecked(Err, D->getSetterNameLoc()); | ||||||||||
5149 | auto ToGetterMethodDecl = importChecked(Err, D->getGetterMethodDecl()); | ||||||||||
5150 | auto ToSetterMethodDecl = importChecked(Err, D->getSetterMethodDecl()); | ||||||||||
5151 | auto ToPropertyIvarDecl = importChecked(Err, D->getPropertyIvarDecl()); | ||||||||||
5152 | if (Err) | ||||||||||
5153 | return std::move(Err); | ||||||||||
5154 | |||||||||||
5155 | ToProperty->setLexicalDeclContext(LexicalDC); | ||||||||||
5156 | LexicalDC->addDeclInternal(ToProperty); | ||||||||||
5157 | |||||||||||
5158 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); | ||||||||||
5159 | ToProperty->setPropertyAttributesAsWritten( | ||||||||||
5160 | D->getPropertyAttributesAsWritten()); | ||||||||||
5161 | ToProperty->setGetterName(ToGetterName, ToGetterNameLoc); | ||||||||||
5162 | ToProperty->setSetterName(ToSetterName, ToSetterNameLoc); | ||||||||||
5163 | ToProperty->setGetterMethodDecl(ToGetterMethodDecl); | ||||||||||
5164 | ToProperty->setSetterMethodDecl(ToSetterMethodDecl); | ||||||||||
5165 | ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl); | ||||||||||
5166 | return ToProperty; | ||||||||||
5167 | } | ||||||||||
5168 | |||||||||||
5169 | ExpectedDecl | ||||||||||
5170 | ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { | ||||||||||
5171 | ObjCPropertyDecl *Property; | ||||||||||
5172 | if (Error Err = importInto(Property, D->getPropertyDecl())) | ||||||||||
5173 | return std::move(Err); | ||||||||||
5174 | |||||||||||
5175 | DeclContext *DC, *LexicalDC; | ||||||||||
5176 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | ||||||||||
5177 | return std::move(Err); | ||||||||||
5178 | |||||||||||
5179 | auto *InImpl = cast<ObjCImplDecl>(LexicalDC); | ||||||||||
5180 | |||||||||||
5181 | // Import the ivar (for an @synthesize). | ||||||||||
5182 | ObjCIvarDecl *Ivar = nullptr; | ||||||||||
5183 | if (Error Err = importInto(Ivar, D->getPropertyIvarDecl())) | ||||||||||
5184 | return std::move(Err); | ||||||||||
5185 | |||||||||||
5186 | ObjCPropertyImplDecl *ToImpl | ||||||||||
5187 | = InImpl->FindPropertyImplDecl(Property->getIdentifier(), | ||||||||||
5188 | Property->getQueryKind()); | ||||||||||
5189 | if (!ToImpl) { | ||||||||||
5190 | |||||||||||
5191 | Error Err = Error::success(); | ||||||||||
5192 | auto ToBeginLoc = importChecked(Err, D->getBeginLoc()); | ||||||||||
5193 | auto ToLocation = importChecked(Err, D->getLocation()); | ||||||||||
5194 | auto ToPropertyIvarDeclLoc = | ||||||||||
5195 | importChecked(Err, D->getPropertyIvarDeclLoc()); | ||||||||||
5196 | if (Err) | ||||||||||
5197 | return std::move(Err); | ||||||||||
5198 | |||||||||||
5199 | if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC, | ||||||||||
5200 | ToBeginLoc, | ||||||||||
5201 | ToLocation, Property, | ||||||||||
5202 | D->getPropertyImplementation(), Ivar, | ||||||||||
5203 | ToPropertyIvarDeclLoc)) | ||||||||||
5204 | return ToImpl; | ||||||||||
5205 | |||||||||||
5206 | ToImpl->setLexicalDeclContext(LexicalDC); | ||||||||||
5207 | LexicalDC->addDeclInternal(ToImpl); | ||||||||||
5208 | } else { | ||||||||||
5209 | // Check that we have the same kind of property implementation (@synthesize | ||||||||||
5210 | // vs. @dynamic). | ||||||||||
5211 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { | ||||||||||
5212 | Importer.ToDiag(ToImpl->getLocation(), | ||||||||||
5213 | diag::warn_odr_objc_property_impl_kind_inconsistent) | ||||||||||
5214 | << Property->getDeclName() | ||||||||||
5215 | << (ToImpl->getPropertyImplementation() | ||||||||||
5216 | == ObjCPropertyImplDecl::Dynamic); | ||||||||||
5217 | Importer.FromDiag(D->getLocation(), | ||||||||||
5218 | diag::note_odr_objc_property_impl_kind) | ||||||||||
5219 | << D->getPropertyDecl()->getDeclName() | ||||||||||
5220 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); | ||||||||||
5221 | |||||||||||
5222 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
5223 | } | ||||||||||
5224 | |||||||||||
5225 | // For @synthesize, check that we have the same | ||||||||||
5226 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && | ||||||||||
5227 | Ivar != ToImpl->getPropertyIvarDecl()) { | ||||||||||
5228 | Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), | ||||||||||
5229 | diag::warn_odr_objc_synthesize_ivar_inconsistent) | ||||||||||
5230 | << Property->getDeclName() | ||||||||||
5231 | << ToImpl->getPropertyIvarDecl()->getDeclName() | ||||||||||
5232 | << Ivar->getDeclName(); | ||||||||||
5233 | Importer.FromDiag(D->getPropertyIvarDeclLoc(), | ||||||||||
5234 | diag::note_odr_objc_synthesize_ivar_here) | ||||||||||
5235 | << D->getPropertyIvarDecl()->getDeclName(); | ||||||||||
5236 | |||||||||||
5237 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
5238 | } | ||||||||||
5239 | |||||||||||
5240 | // Merge the existing implementation with the new implementation. | ||||||||||
5241 | Importer.MapImported(D, ToImpl); | ||||||||||
5242 | } | ||||||||||
5243 | |||||||||||
5244 | return ToImpl; | ||||||||||
5245 | } | ||||||||||
5246 | |||||||||||
5247 | ExpectedDecl | ||||||||||
5248 | ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { | ||||||||||
5249 | // For template arguments, we adopt the translation unit as our declaration | ||||||||||
5250 | // context. This context will be fixed when the actual template declaration | ||||||||||
5251 | // is created. | ||||||||||
5252 | |||||||||||
5253 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | ||||||||||
5254 | if (!BeginLocOrErr) | ||||||||||
5255 | return BeginLocOrErr.takeError(); | ||||||||||
5256 | |||||||||||
5257 | ExpectedSLoc LocationOrErr = import(D->getLocation()); | ||||||||||
5258 | if (!LocationOrErr) | ||||||||||
5259 | return LocationOrErr.takeError(); | ||||||||||
5260 | |||||||||||
5261 | TemplateTypeParmDecl *ToD = nullptr; | ||||||||||
5262 | if (GetImportedOrCreateDecl( | ||||||||||
5263 | ToD, D, Importer.getToContext(), | ||||||||||
5264 | Importer.getToContext().getTranslationUnitDecl(), | ||||||||||
5265 | *BeginLocOrErr, *LocationOrErr, | ||||||||||
5266 | D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()), | ||||||||||
5267 | D->wasDeclaredWithTypename(), D->isParameterPack(), | ||||||||||
5268 | D->hasTypeConstraint())) | ||||||||||
5269 | return ToD; | ||||||||||
5270 | |||||||||||
5271 | // Import the type-constraint | ||||||||||
5272 | if (const TypeConstraint *TC = D->getTypeConstraint()) { | ||||||||||
5273 | |||||||||||
5274 | Error Err = Error::success(); | ||||||||||
5275 | auto ToNNS = importChecked(Err, TC->getNestedNameSpecifierLoc()); | ||||||||||
5276 | auto ToName = importChecked(Err, TC->getConceptNameInfo().getName()); | ||||||||||
5277 | auto ToNameLoc = importChecked(Err, TC->getConceptNameInfo().getLoc()); | ||||||||||
5278 | auto ToFoundDecl = importChecked(Err, TC->getFoundDecl()); | ||||||||||
5279 | auto ToNamedConcept = importChecked(Err, TC->getNamedConcept()); | ||||||||||
5280 | auto ToIDC = importChecked(Err, TC->getImmediatelyDeclaredConstraint()); | ||||||||||
5281 | if (Err) | ||||||||||
5282 | return std::move(Err); | ||||||||||
5283 | |||||||||||
5284 | TemplateArgumentListInfo ToTAInfo; | ||||||||||
5285 | const auto *ASTTemplateArgs = TC->getTemplateArgsAsWritten(); | ||||||||||
5286 | if (ASTTemplateArgs) | ||||||||||
5287 | if (Error Err = ImportTemplateArgumentListInfo(*ASTTemplateArgs, | ||||||||||
5288 | ToTAInfo)) | ||||||||||
5289 | return std::move(Err); | ||||||||||
5290 | |||||||||||
5291 | ToD->setTypeConstraint(ToNNS, DeclarationNameInfo(ToName, ToNameLoc), | ||||||||||
5292 | ToFoundDecl, ToNamedConcept, | ||||||||||
5293 | ASTTemplateArgs ? | ||||||||||
5294 | ASTTemplateArgumentListInfo::Create(Importer.getToContext(), | ||||||||||
5295 | ToTAInfo) : nullptr, | ||||||||||
5296 | ToIDC); | ||||||||||
5297 | } | ||||||||||
5298 | |||||||||||
5299 | if (D->hasDefaultArgument()) { | ||||||||||
5300 | Expected<TypeSourceInfo *> ToDefaultArgOrErr = | ||||||||||
5301 | import(D->getDefaultArgumentInfo()); | ||||||||||
5302 | if (!ToDefaultArgOrErr) | ||||||||||
5303 | return ToDefaultArgOrErr.takeError(); | ||||||||||
5304 | ToD->setDefaultArgument(*ToDefaultArgOrErr); | ||||||||||
5305 | } | ||||||||||
5306 | |||||||||||
5307 | return ToD; | ||||||||||
5308 | } | ||||||||||
5309 | |||||||||||
5310 | ExpectedDecl | ||||||||||
5311 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { | ||||||||||
5312 | |||||||||||
5313 | Error Err = Error::success(); | ||||||||||
5314 | auto ToDeclName = importChecked(Err, D->getDeclName()); | ||||||||||
5315 | auto ToLocation = importChecked(Err, D->getLocation()); | ||||||||||
5316 | auto ToType = importChecked(Err, D->getType()); | ||||||||||
5317 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | ||||||||||
5318 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | ||||||||||
5319 | if (Err) | ||||||||||
5320 | return std::move(Err); | ||||||||||
5321 | |||||||||||
5322 | NonTypeTemplateParmDecl *ToD = nullptr; | ||||||||||
5323 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), | ||||||||||
5324 | Importer.getToContext().getTranslationUnitDecl(), | ||||||||||
5325 | ToInnerLocStart, ToLocation, D->getDepth(), | ||||||||||
5326 | D->getPosition(), | ||||||||||
5327 | ToDeclName.getAsIdentifierInfo(), ToType, | ||||||||||
5328 | D->isParameterPack(), ToTypeSourceInfo)) | ||||||||||
5329 | return ToD; | ||||||||||
5330 | |||||||||||
5331 | if (D->hasDefaultArgument()) { | ||||||||||
5332 | ExpectedExpr ToDefaultArgOrErr = import(D->getDefaultArgument()); | ||||||||||
5333 | if (!ToDefaultArgOrErr) | ||||||||||
5334 | return ToDefaultArgOrErr.takeError(); | ||||||||||
5335 | ToD->setDefaultArgument(*ToDefaultArgOrErr); | ||||||||||
5336 | } | ||||||||||
5337 | |||||||||||
5338 | return ToD; | ||||||||||
5339 | } | ||||||||||
5340 | |||||||||||
5341 | ExpectedDecl | ||||||||||
5342 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { | ||||||||||
5343 | // Import the name of this declaration. | ||||||||||
5344 | auto NameOrErr = import(D->getDeclName()); | ||||||||||
5345 | if (!NameOrErr) | ||||||||||
5346 | return NameOrErr.takeError(); | ||||||||||
5347 | |||||||||||
5348 | // Import the location of this declaration. | ||||||||||
5349 | ExpectedSLoc LocationOrErr = import(D->getLocation()); | ||||||||||
5350 | if (!LocationOrErr) | ||||||||||
5351 | return LocationOrErr.takeError(); | ||||||||||
5352 | |||||||||||
5353 | // Import template parameters. | ||||||||||
5354 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); | ||||||||||
5355 | if (!TemplateParamsOrErr) | ||||||||||
5356 | return TemplateParamsOrErr.takeError(); | ||||||||||
5357 | |||||||||||
5358 | TemplateTemplateParmDecl *ToD = nullptr; | ||||||||||
5359 | if (GetImportedOrCreateDecl( | ||||||||||
5360 | ToD, D, Importer.getToContext(), | ||||||||||
5361 | Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr, | ||||||||||
5362 | D->getDepth(), D->getPosition(), D->isParameterPack(), | ||||||||||
5363 | (*NameOrErr).getAsIdentifierInfo(), *TemplateParamsOrErr)) | ||||||||||
5364 | return ToD; | ||||||||||
5365 | |||||||||||
5366 | if (D->hasDefaultArgument()) { | ||||||||||
5367 | Expected<TemplateArgumentLoc> ToDefaultArgOrErr = | ||||||||||
5368 | import(D->getDefaultArgument()); | ||||||||||
5369 | if (!ToDefaultArgOrErr) | ||||||||||
5370 | return ToDefaultArgOrErr.takeError(); | ||||||||||
5371 | ToD->setDefaultArgument(Importer.getToContext(), *ToDefaultArgOrErr); | ||||||||||
5372 | } | ||||||||||
5373 | |||||||||||
5374 | return ToD; | ||||||||||
5375 | } | ||||||||||
5376 | |||||||||||
5377 | // Returns the definition for a (forward) declaration of a TemplateDecl, if | ||||||||||
5378 | // it has any definition in the redecl chain. | ||||||||||
5379 | template <typename T> static auto getTemplateDefinition(T *D) -> T * { | ||||||||||
5380 | assert(D->getTemplatedDecl() && "Should be called on templates only")((D->getTemplatedDecl() && "Should be called on templates only" ) ? static_cast<void> (0) : __assert_fail ("D->getTemplatedDecl() && \"Should be called on templates only\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 5380, __PRETTY_FUNCTION__)); | ||||||||||
5381 | auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition(); | ||||||||||
5382 | if (!ToTemplatedDef) | ||||||||||
5383 | return nullptr; | ||||||||||
5384 | auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate(); | ||||||||||
5385 | return cast_or_null<T>(TemplateWithDef); | ||||||||||
5386 | } | ||||||||||
5387 | |||||||||||
5388 | ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { | ||||||||||
5389 | |||||||||||
5390 | // Import the major distinguishing characteristics of this class template. | ||||||||||
5391 | DeclContext *DC, *LexicalDC; | ||||||||||
5392 | DeclarationName Name; | ||||||||||
5393 | SourceLocation Loc; | ||||||||||
5394 | NamedDecl *ToD; | ||||||||||
5395 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
5396 | return std::move(Err); | ||||||||||
5397 | if (ToD) | ||||||||||
5398 | return ToD; | ||||||||||
5399 | |||||||||||
5400 | ClassTemplateDecl *FoundByLookup = nullptr; | ||||||||||
5401 | |||||||||||
5402 | // We may already have a template of the same name; try to find and match it. | ||||||||||
5403 | if (!DC->isFunctionOrMethod()) { | ||||||||||
5404 | SmallVector<NamedDecl *, 4> ConflictingDecls; | ||||||||||
5405 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
5406 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
5407 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary | | ||||||||||
5408 | Decl::IDNS_TagFriend)) | ||||||||||
5409 | continue; | ||||||||||
5410 | |||||||||||
5411 | Decl *Found = FoundDecl; | ||||||||||
5412 | auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found); | ||||||||||
5413 | if (FoundTemplate) { | ||||||||||
5414 | if (!hasSameVisibilityContextAndLinkage(FoundTemplate, D)) | ||||||||||
5415 | continue; | ||||||||||
5416 | |||||||||||
5417 | if (IsStructuralMatch(D, FoundTemplate)) { | ||||||||||
5418 | ClassTemplateDecl *TemplateWithDef = | ||||||||||
5419 | getTemplateDefinition(FoundTemplate); | ||||||||||
5420 | if (D->isThisDeclarationADefinition() && TemplateWithDef) | ||||||||||
5421 | return Importer.MapImported(D, TemplateWithDef); | ||||||||||
5422 | if (!FoundByLookup) | ||||||||||
5423 | FoundByLookup = FoundTemplate; | ||||||||||
5424 | // Search in all matches because there may be multiple decl chains, | ||||||||||
5425 | // see ASTTests test ImportExistingFriendClassTemplateDef. | ||||||||||
5426 | continue; | ||||||||||
5427 | } | ||||||||||
5428 | ConflictingDecls.push_back(FoundDecl); | ||||||||||
5429 | } | ||||||||||
5430 | } | ||||||||||
5431 | |||||||||||
5432 | if (!ConflictingDecls.empty()) { | ||||||||||
5433 | ExpectedName NameOrErr = Importer.HandleNameConflict( | ||||||||||
5434 | Name, DC, Decl::IDNS_Ordinary, ConflictingDecls.data(), | ||||||||||
5435 | ConflictingDecls.size()); | ||||||||||
5436 | if (NameOrErr) | ||||||||||
5437 | Name = NameOrErr.get(); | ||||||||||
5438 | else | ||||||||||
5439 | return NameOrErr.takeError(); | ||||||||||
5440 | } | ||||||||||
5441 | } | ||||||||||
5442 | |||||||||||
5443 | CXXRecordDecl *FromTemplated = D->getTemplatedDecl(); | ||||||||||
5444 | |||||||||||
5445 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); | ||||||||||
5446 | if (!TemplateParamsOrErr) | ||||||||||
5447 | return TemplateParamsOrErr.takeError(); | ||||||||||
5448 | |||||||||||
5449 | // Create the declaration that is being templated. | ||||||||||
5450 | CXXRecordDecl *ToTemplated; | ||||||||||
5451 | if (Error Err = importInto(ToTemplated, FromTemplated)) | ||||||||||
5452 | return std::move(Err); | ||||||||||
5453 | |||||||||||
5454 | // Create the class template declaration itself. | ||||||||||
5455 | ClassTemplateDecl *D2; | ||||||||||
5456 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name, | ||||||||||
5457 | *TemplateParamsOrErr, ToTemplated)) | ||||||||||
5458 | return D2; | ||||||||||
5459 | |||||||||||
5460 | ToTemplated->setDescribedClassTemplate(D2); | ||||||||||
5461 | |||||||||||
5462 | D2->setAccess(D->getAccess()); | ||||||||||
5463 | D2->setLexicalDeclContext(LexicalDC); | ||||||||||
5464 | |||||||||||
5465 | addDeclToContexts(D, D2); | ||||||||||
5466 | |||||||||||
5467 | if (FoundByLookup) { | ||||||||||
5468 | auto *Recent = | ||||||||||
5469 | const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl()); | ||||||||||
5470 | |||||||||||
5471 | // It is possible that during the import of the class template definition | ||||||||||
5472 | // we start the import of a fwd friend decl of the very same class template | ||||||||||
5473 | // and we add the fwd friend decl to the lookup table. But the ToTemplated | ||||||||||
5474 | // had been created earlier and by that time the lookup could not find | ||||||||||
5475 | // anything existing, so it has no previous decl. Later, (still during the | ||||||||||
5476 | // import of the fwd friend decl) we start to import the definition again | ||||||||||
5477 | // and this time the lookup finds the previous fwd friend class template. | ||||||||||
5478 | // In this case we must set up the previous decl for the templated decl. | ||||||||||
5479 | if (!ToTemplated->getPreviousDecl()) { | ||||||||||
5480 | assert(FoundByLookup->getTemplatedDecl() &&((FoundByLookup->getTemplatedDecl() && "Found decl must have its templated decl set" ) ? static_cast<void> (0) : __assert_fail ("FoundByLookup->getTemplatedDecl() && \"Found decl must have its templated decl set\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 5481, __PRETTY_FUNCTION__)) | ||||||||||
5481 | "Found decl must have its templated decl set")((FoundByLookup->getTemplatedDecl() && "Found decl must have its templated decl set" ) ? static_cast<void> (0) : __assert_fail ("FoundByLookup->getTemplatedDecl() && \"Found decl must have its templated decl set\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 5481, __PRETTY_FUNCTION__)); | ||||||||||
5482 | CXXRecordDecl *PrevTemplated = | ||||||||||
5483 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); | ||||||||||
5484 | if (ToTemplated != PrevTemplated) | ||||||||||
5485 | ToTemplated->setPreviousDecl(PrevTemplated); | ||||||||||
5486 | } | ||||||||||
5487 | |||||||||||
5488 | D2->setPreviousDecl(Recent); | ||||||||||
5489 | } | ||||||||||
5490 | |||||||||||
5491 | if (FromTemplated->isCompleteDefinition() && | ||||||||||
5492 | !ToTemplated->isCompleteDefinition()) { | ||||||||||
5493 | // FIXME: Import definition! | ||||||||||
5494 | } | ||||||||||
5495 | |||||||||||
5496 | return D2; | ||||||||||
5497 | } | ||||||||||
5498 | |||||||||||
5499 | ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl( | ||||||||||
5500 | ClassTemplateSpecializationDecl *D) { | ||||||||||
5501 | ClassTemplateDecl *ClassTemplate; | ||||||||||
5502 | if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate())) | ||||||||||
5503 | return std::move(Err); | ||||||||||
5504 | |||||||||||
5505 | // Import the context of this declaration. | ||||||||||
5506 | DeclContext *DC, *LexicalDC; | ||||||||||
5507 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | ||||||||||
5508 | return std::move(Err); | ||||||||||
5509 | |||||||||||
5510 | // Import template arguments. | ||||||||||
5511 | SmallVector<TemplateArgument, 2> TemplateArgs; | ||||||||||
5512 | if (Error Err = ImportTemplateArguments( | ||||||||||
5513 | D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs)) | ||||||||||
5514 | return std::move(Err); | ||||||||||
5515 | // Try to find an existing specialization with these template arguments and | ||||||||||
5516 | // template parameter list. | ||||||||||
5517 | void *InsertPos = nullptr; | ||||||||||
5518 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; | ||||||||||
5519 | ClassTemplatePartialSpecializationDecl *PartialSpec = | ||||||||||
5520 | dyn_cast<ClassTemplatePartialSpecializationDecl>(D); | ||||||||||
5521 | |||||||||||
5522 | // Import template parameters. | ||||||||||
5523 | TemplateParameterList *ToTPList = nullptr; | ||||||||||
5524 | |||||||||||
5525 | if (PartialSpec) { | ||||||||||
5526 | auto ToTPListOrErr = import(PartialSpec->getTemplateParameters()); | ||||||||||
5527 | if (!ToTPListOrErr) | ||||||||||
5528 | return ToTPListOrErr.takeError(); | ||||||||||
5529 | ToTPList = *ToTPListOrErr; | ||||||||||
5530 | PrevDecl = ClassTemplate->findPartialSpecialization(TemplateArgs, | ||||||||||
5531 | *ToTPListOrErr, | ||||||||||
5532 | InsertPos); | ||||||||||
5533 | } else | ||||||||||
5534 | PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); | ||||||||||
5535 | |||||||||||
5536 | if (PrevDecl) { | ||||||||||
5537 | if (IsStructuralMatch(D, PrevDecl)) { | ||||||||||
5538 | CXXRecordDecl *PrevDefinition = PrevDecl->getDefinition(); | ||||||||||
5539 | if (D->isThisDeclarationADefinition() && PrevDefinition) { | ||||||||||
5540 | Importer.MapImported(D, PrevDefinition); | ||||||||||
5541 | // Import those default field initializers which have been | ||||||||||
5542 | // instantiated in the "From" context, but not in the "To" context. | ||||||||||
5543 | for (auto *FromField : D->fields()) { | ||||||||||
5544 | auto ToOrErr = import(FromField); | ||||||||||
5545 | if (!ToOrErr) | ||||||||||
5546 | return ToOrErr.takeError(); | ||||||||||
5547 | } | ||||||||||
5548 | |||||||||||
5549 | // Import those methods which have been instantiated in the | ||||||||||
5550 | // "From" context, but not in the "To" context. | ||||||||||
5551 | for (CXXMethodDecl *FromM : D->methods()) { | ||||||||||
5552 | auto ToOrErr = import(FromM); | ||||||||||
5553 | if (!ToOrErr) | ||||||||||
5554 | return ToOrErr.takeError(); | ||||||||||
5555 | } | ||||||||||
5556 | |||||||||||
5557 | // TODO Import instantiated default arguments. | ||||||||||
5558 | // TODO Import instantiated exception specifications. | ||||||||||
5559 | // | ||||||||||
5560 | // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint | ||||||||||
5561 | // what else could be fused during an AST merge. | ||||||||||
5562 | return PrevDefinition; | ||||||||||
5563 | } | ||||||||||
5564 | } else { // ODR violation. | ||||||||||
5565 | // FIXME HandleNameConflict | ||||||||||
5566 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
5567 | } | ||||||||||
5568 | } | ||||||||||
5569 | |||||||||||
5570 | // Import the location of this declaration. | ||||||||||
5571 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | ||||||||||
5572 | if (!BeginLocOrErr) | ||||||||||
5573 | return BeginLocOrErr.takeError(); | ||||||||||
5574 | ExpectedSLoc IdLocOrErr = import(D->getLocation()); | ||||||||||
5575 | if (!IdLocOrErr) | ||||||||||
5576 | return IdLocOrErr.takeError(); | ||||||||||
5577 | |||||||||||
5578 | // Create the specialization. | ||||||||||
5579 | ClassTemplateSpecializationDecl *D2 = nullptr; | ||||||||||
5580 | if (PartialSpec) { | ||||||||||
5581 | // Import TemplateArgumentListInfo. | ||||||||||
5582 | TemplateArgumentListInfo ToTAInfo; | ||||||||||
5583 | const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten(); | ||||||||||
5584 | if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo)) | ||||||||||
5585 | return std::move(Err); | ||||||||||
5586 | |||||||||||
5587 | QualType CanonInjType; | ||||||||||
5588 | if (Error Err = importInto( | ||||||||||
5589 | CanonInjType, PartialSpec->getInjectedSpecializationType())) | ||||||||||
5590 | return std::move(Err); | ||||||||||
5591 | CanonInjType = CanonInjType.getCanonicalType(); | ||||||||||
5592 | |||||||||||
5593 | if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>( | ||||||||||
5594 | D2, D, Importer.getToContext(), D->getTagKind(), DC, | ||||||||||
5595 | *BeginLocOrErr, *IdLocOrErr, ToTPList, ClassTemplate, | ||||||||||
5596 | llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()), | ||||||||||
5597 | ToTAInfo, CanonInjType, | ||||||||||
5598 | cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl))) | ||||||||||
5599 | return D2; | ||||||||||
5600 | |||||||||||
5601 | // Update InsertPos, because preceding import calls may have invalidated | ||||||||||
5602 | // it by adding new specializations. | ||||||||||
5603 | auto *PartSpec2 = cast<ClassTemplatePartialSpecializationDecl>(D2); | ||||||||||
5604 | if (!ClassTemplate->findPartialSpecialization(TemplateArgs, ToTPList, | ||||||||||
5605 | InsertPos)) | ||||||||||
5606 | // Add this partial specialization to the class template. | ||||||||||
5607 | ClassTemplate->AddPartialSpecialization(PartSpec2, InsertPos); | ||||||||||
5608 | |||||||||||
5609 | } else { // Not a partial specialization. | ||||||||||
5610 | if (GetImportedOrCreateDecl( | ||||||||||
5611 | D2, D, Importer.getToContext(), D->getTagKind(), DC, | ||||||||||
5612 | *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs, | ||||||||||
5613 | PrevDecl)) | ||||||||||
5614 | return D2; | ||||||||||
5615 | |||||||||||
5616 | // Update InsertPos, because preceding import calls may have invalidated | ||||||||||
5617 | // it by adding new specializations. | ||||||||||
5618 | if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos)) | ||||||||||
5619 | // Add this specialization to the class template. | ||||||||||
5620 | ClassTemplate->AddSpecialization(D2, InsertPos); | ||||||||||
5621 | } | ||||||||||
5622 | |||||||||||
5623 | D2->setSpecializationKind(D->getSpecializationKind()); | ||||||||||
5624 | |||||||||||
5625 | // Set the context of this specialization/instantiation. | ||||||||||
5626 | D2->setLexicalDeclContext(LexicalDC); | ||||||||||
5627 | |||||||||||
5628 | // Add to the DC only if it was an explicit specialization/instantiation. | ||||||||||
5629 | if (D2->isExplicitInstantiationOrSpecialization()) { | ||||||||||
5630 | LexicalDC->addDeclInternal(D2); | ||||||||||
5631 | } | ||||||||||
5632 | |||||||||||
5633 | if (auto BraceRangeOrErr = import(D->getBraceRange())) | ||||||||||
5634 | D2->setBraceRange(*BraceRangeOrErr); | ||||||||||
5635 | else | ||||||||||
5636 | return BraceRangeOrErr.takeError(); | ||||||||||
5637 | |||||||||||
5638 | // Import the qualifier, if any. | ||||||||||
5639 | if (auto LocOrErr = import(D->getQualifierLoc())) | ||||||||||
5640 | D2->setQualifierInfo(*LocOrErr); | ||||||||||
5641 | else | ||||||||||
5642 | return LocOrErr.takeError(); | ||||||||||
5643 | |||||||||||
5644 | if (auto *TSI = D->getTypeAsWritten()) { | ||||||||||
5645 | if (auto TInfoOrErr = import(TSI)) | ||||||||||
5646 | D2->setTypeAsWritten(*TInfoOrErr); | ||||||||||
5647 | else | ||||||||||
5648 | return TInfoOrErr.takeError(); | ||||||||||
5649 | |||||||||||
5650 | if (auto LocOrErr = import(D->getTemplateKeywordLoc())) | ||||||||||
5651 | D2->setTemplateKeywordLoc(*LocOrErr); | ||||||||||
5652 | else | ||||||||||
5653 | return LocOrErr.takeError(); | ||||||||||
5654 | |||||||||||
5655 | if (auto LocOrErr = import(D->getExternLoc())) | ||||||||||
5656 | D2->setExternLoc(*LocOrErr); | ||||||||||
5657 | else | ||||||||||
5658 | return LocOrErr.takeError(); | ||||||||||
5659 | } | ||||||||||
5660 | |||||||||||
5661 | if (D->getPointOfInstantiation().isValid()) { | ||||||||||
5662 | if (auto POIOrErr = import(D->getPointOfInstantiation())) | ||||||||||
5663 | D2->setPointOfInstantiation(*POIOrErr); | ||||||||||
5664 | else | ||||||||||
5665 | return POIOrErr.takeError(); | ||||||||||
5666 | } | ||||||||||
5667 | |||||||||||
5668 | D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind()); | ||||||||||
5669 | |||||||||||
5670 | if (D->isCompleteDefinition()) | ||||||||||
5671 | if (Error Err = ImportDefinition(D, D2)) | ||||||||||
5672 | return std::move(Err); | ||||||||||
5673 | |||||||||||
5674 | return D2; | ||||||||||
5675 | } | ||||||||||
5676 | |||||||||||
5677 | ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { | ||||||||||
5678 | // Import the major distinguishing characteristics of this variable template. | ||||||||||
5679 | DeclContext *DC, *LexicalDC; | ||||||||||
5680 | DeclarationName Name; | ||||||||||
5681 | SourceLocation Loc; | ||||||||||
5682 | NamedDecl *ToD; | ||||||||||
5683 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
5684 | return std::move(Err); | ||||||||||
5685 | if (ToD) | ||||||||||
5686 | return ToD; | ||||||||||
5687 | |||||||||||
5688 | // We may already have a template of the same name; try to find and match it. | ||||||||||
5689 | assert(!DC->isFunctionOrMethod() &&((!DC->isFunctionOrMethod() && "Variable templates cannot be declared at function scope" ) ? static_cast<void> (0) : __assert_fail ("!DC->isFunctionOrMethod() && \"Variable templates cannot be declared at function scope\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 5690, __PRETTY_FUNCTION__)) | ||||||||||
5690 | "Variable templates cannot be declared at function scope")((!DC->isFunctionOrMethod() && "Variable templates cannot be declared at function scope" ) ? static_cast<void> (0) : __assert_fail ("!DC->isFunctionOrMethod() && \"Variable templates cannot be declared at function scope\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 5690, __PRETTY_FUNCTION__)); | ||||||||||
5691 | |||||||||||
5692 | SmallVector<NamedDecl *, 4> ConflictingDecls; | ||||||||||
5693 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
5694 | VarTemplateDecl *FoundByLookup = nullptr; | ||||||||||
5695 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
5696 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) | ||||||||||
5697 | continue; | ||||||||||
5698 | |||||||||||
5699 | if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(FoundDecl)) { | ||||||||||
5700 | // Use the templated decl, some linkage flags are set only there. | ||||||||||
5701 | if (!hasSameVisibilityContextAndLinkage(FoundTemplate->getTemplatedDecl(), | ||||||||||
5702 | D->getTemplatedDecl())) | ||||||||||
5703 | continue; | ||||||||||
5704 | if (IsStructuralMatch(D, FoundTemplate)) { | ||||||||||
5705 | // The Decl in the "From" context has a definition, but in the | ||||||||||
5706 | // "To" context we already have a definition. | ||||||||||
5707 | VarTemplateDecl *FoundDef = getTemplateDefinition(FoundTemplate); | ||||||||||
5708 | if (D->isThisDeclarationADefinition() && FoundDef) | ||||||||||
5709 | // FIXME Check for ODR error if the two definitions have | ||||||||||
5710 | // different initializers? | ||||||||||
5711 | return Importer.MapImported(D, FoundDef); | ||||||||||
5712 | |||||||||||
5713 | FoundByLookup = FoundTemplate; | ||||||||||
5714 | break; | ||||||||||
5715 | } | ||||||||||
5716 | ConflictingDecls.push_back(FoundDecl); | ||||||||||
5717 | } | ||||||||||
5718 | } | ||||||||||
5719 | |||||||||||
5720 | if (!ConflictingDecls.empty()) { | ||||||||||
5721 | ExpectedName NameOrErr = Importer.HandleNameConflict( | ||||||||||
5722 | Name, DC, Decl::IDNS_Ordinary, ConflictingDecls.data(), | ||||||||||
5723 | ConflictingDecls.size()); | ||||||||||
5724 | if (NameOrErr) | ||||||||||
5725 | Name = NameOrErr.get(); | ||||||||||
5726 | else | ||||||||||
5727 | return NameOrErr.takeError(); | ||||||||||
5728 | } | ||||||||||
5729 | |||||||||||
5730 | VarDecl *DTemplated = D->getTemplatedDecl(); | ||||||||||
5731 | |||||||||||
5732 | // Import the type. | ||||||||||
5733 | // FIXME: Value not used? | ||||||||||
5734 | ExpectedType TypeOrErr = import(DTemplated->getType()); | ||||||||||
5735 | if (!TypeOrErr) | ||||||||||
5736 | return TypeOrErr.takeError(); | ||||||||||
5737 | |||||||||||
5738 | // Create the declaration that is being templated. | ||||||||||
5739 | VarDecl *ToTemplated; | ||||||||||
5740 | if (Error Err = importInto(ToTemplated, DTemplated)) | ||||||||||
5741 | return std::move(Err); | ||||||||||
5742 | |||||||||||
5743 | // Create the variable template declaration itself. | ||||||||||
5744 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); | ||||||||||
5745 | if (!TemplateParamsOrErr) | ||||||||||
5746 | return TemplateParamsOrErr.takeError(); | ||||||||||
5747 | |||||||||||
5748 | VarTemplateDecl *ToVarTD; | ||||||||||
5749 | if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc, | ||||||||||
5750 | Name, *TemplateParamsOrErr, ToTemplated)) | ||||||||||
5751 | return ToVarTD; | ||||||||||
5752 | |||||||||||
5753 | ToTemplated->setDescribedVarTemplate(ToVarTD); | ||||||||||
5754 | |||||||||||
5755 | ToVarTD->setAccess(D->getAccess()); | ||||||||||
5756 | ToVarTD->setLexicalDeclContext(LexicalDC); | ||||||||||
5757 | LexicalDC->addDeclInternal(ToVarTD); | ||||||||||
5758 | |||||||||||
5759 | if (FoundByLookup) { | ||||||||||
5760 | auto *Recent = | ||||||||||
5761 | const_cast<VarTemplateDecl *>(FoundByLookup->getMostRecentDecl()); | ||||||||||
5762 | if (!ToTemplated->getPreviousDecl()) { | ||||||||||
5763 | auto *PrevTemplated = | ||||||||||
5764 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); | ||||||||||
5765 | if (ToTemplated != PrevTemplated) | ||||||||||
5766 | ToTemplated->setPreviousDecl(PrevTemplated); | ||||||||||
5767 | } | ||||||||||
5768 | ToVarTD->setPreviousDecl(Recent); | ||||||||||
5769 | } | ||||||||||
5770 | |||||||||||
5771 | if (DTemplated->isThisDeclarationADefinition() && | ||||||||||
5772 | !ToTemplated->isThisDeclarationADefinition()) { | ||||||||||
5773 | // FIXME: Import definition! | ||||||||||
5774 | } | ||||||||||
5775 | |||||||||||
5776 | return ToVarTD; | ||||||||||
5777 | } | ||||||||||
5778 | |||||||||||
5779 | ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl( | ||||||||||
5780 | VarTemplateSpecializationDecl *D) { | ||||||||||
5781 | // If this record has a definition in the translation unit we're coming from, | ||||||||||
5782 | // but this particular declaration is not that definition, import the | ||||||||||
5783 | // definition and map to that. | ||||||||||
5784 | VarDecl *Definition = D->getDefinition(); | ||||||||||
5785 | if (Definition && Definition != D) { | ||||||||||
5786 | if (ExpectedDecl ImportedDefOrErr = import(Definition)) | ||||||||||
5787 | return Importer.MapImported(D, *ImportedDefOrErr); | ||||||||||
5788 | else | ||||||||||
5789 | return ImportedDefOrErr.takeError(); | ||||||||||
5790 | } | ||||||||||
5791 | |||||||||||
5792 | VarTemplateDecl *VarTemplate = nullptr; | ||||||||||
5793 | if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate())) | ||||||||||
5794 | return std::move(Err); | ||||||||||
5795 | |||||||||||
5796 | // Import the context of this declaration. | ||||||||||
5797 | DeclContext *DC, *LexicalDC; | ||||||||||
5798 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | ||||||||||
5799 | return std::move(Err); | ||||||||||
5800 | |||||||||||
5801 | // Import the location of this declaration. | ||||||||||
5802 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | ||||||||||
5803 | if (!BeginLocOrErr) | ||||||||||
5804 | return BeginLocOrErr.takeError(); | ||||||||||
5805 | |||||||||||
5806 | auto IdLocOrErr = import(D->getLocation()); | ||||||||||
5807 | if (!IdLocOrErr) | ||||||||||
5808 | return IdLocOrErr.takeError(); | ||||||||||
5809 | |||||||||||
5810 | // Import template arguments. | ||||||||||
5811 | SmallVector<TemplateArgument, 2> TemplateArgs; | ||||||||||
5812 | if (Error Err = ImportTemplateArguments( | ||||||||||
5813 | D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs)) | ||||||||||
5814 | return std::move(Err); | ||||||||||
5815 | |||||||||||
5816 | // Try to find an existing specialization with these template arguments. | ||||||||||
5817 | void *InsertPos = nullptr; | ||||||||||
5818 | VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( | ||||||||||
5819 | TemplateArgs, InsertPos); | ||||||||||
5820 | if (D2) { | ||||||||||
5821 | // We already have a variable template specialization with these template | ||||||||||
5822 | // arguments. | ||||||||||
5823 | |||||||||||
5824 | // FIXME: Check for specialization vs. instantiation errors. | ||||||||||
5825 | |||||||||||
5826 | if (VarDecl *FoundDef = D2->getDefinition()) { | ||||||||||
5827 | if (!D->isThisDeclarationADefinition() || | ||||||||||
5828 | IsStructuralMatch(D, FoundDef)) { | ||||||||||
5829 | // The record types structurally match, or the "from" translation | ||||||||||
5830 | // unit only had a forward declaration anyway; call it the same | ||||||||||
5831 | // variable. | ||||||||||
5832 | return Importer.MapImported(D, FoundDef); | ||||||||||
5833 | } | ||||||||||
5834 | } | ||||||||||
5835 | } else { | ||||||||||
5836 | // Import the type. | ||||||||||
5837 | QualType T; | ||||||||||
5838 | if (Error Err = importInto(T, D->getType())) | ||||||||||
5839 | return std::move(Err); | ||||||||||
5840 | |||||||||||
5841 | auto TInfoOrErr = import(D->getTypeSourceInfo()); | ||||||||||
5842 | if (!TInfoOrErr) | ||||||||||
5843 | return TInfoOrErr.takeError(); | ||||||||||
5844 | |||||||||||
5845 | TemplateArgumentListInfo ToTAInfo; | ||||||||||
5846 | if (Error Err = ImportTemplateArgumentListInfo( | ||||||||||
5847 | D->getTemplateArgsInfo(), ToTAInfo)) | ||||||||||
5848 | return std::move(Err); | ||||||||||
5849 | |||||||||||
5850 | using PartVarSpecDecl = VarTemplatePartialSpecializationDecl; | ||||||||||
5851 | // Create a new specialization. | ||||||||||
5852 | if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) { | ||||||||||
5853 | // Import TemplateArgumentListInfo | ||||||||||
5854 | TemplateArgumentListInfo ArgInfos; | ||||||||||
5855 | const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten(); | ||||||||||
5856 | // NOTE: FromTAArgsAsWritten and template parameter list are non-null. | ||||||||||
5857 | if (Error Err = ImportTemplateArgumentListInfo( | ||||||||||
5858 | *FromTAArgsAsWritten, ArgInfos)) | ||||||||||
5859 | return std::move(Err); | ||||||||||
5860 | |||||||||||
5861 | auto ToTPListOrErr = import(FromPartial->getTemplateParameters()); | ||||||||||
5862 | if (!ToTPListOrErr) | ||||||||||
5863 | return ToTPListOrErr.takeError(); | ||||||||||
5864 | |||||||||||
5865 | PartVarSpecDecl *ToPartial; | ||||||||||
5866 | if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC, | ||||||||||
5867 | *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, | ||||||||||
5868 | VarTemplate, T, *TInfoOrErr, | ||||||||||
5869 | D->getStorageClass(), TemplateArgs, ArgInfos)) | ||||||||||
5870 | return ToPartial; | ||||||||||
5871 | |||||||||||
5872 | if (Expected<PartVarSpecDecl *> ToInstOrErr = import( | ||||||||||
5873 | FromPartial->getInstantiatedFromMember())) | ||||||||||
5874 | ToPartial->setInstantiatedFromMember(*ToInstOrErr); | ||||||||||
5875 | else | ||||||||||
5876 | return ToInstOrErr.takeError(); | ||||||||||
5877 | |||||||||||
5878 | if (FromPartial->isMemberSpecialization()) | ||||||||||
5879 | ToPartial->setMemberSpecialization(); | ||||||||||
5880 | |||||||||||
5881 | D2 = ToPartial; | ||||||||||
5882 | |||||||||||
5883 | } else { // Full specialization | ||||||||||
5884 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, | ||||||||||
5885 | *BeginLocOrErr, *IdLocOrErr, VarTemplate, | ||||||||||
5886 | T, *TInfoOrErr, | ||||||||||
5887 | D->getStorageClass(), TemplateArgs)) | ||||||||||
5888 | return D2; | ||||||||||
5889 | } | ||||||||||
5890 | |||||||||||
5891 | if (D->getPointOfInstantiation().isValid()) { | ||||||||||
5892 | if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation())) | ||||||||||
5893 | D2->setPointOfInstantiation(*POIOrErr); | ||||||||||
5894 | else | ||||||||||
5895 | return POIOrErr.takeError(); | ||||||||||
5896 | } | ||||||||||
5897 | |||||||||||
5898 | D2->setSpecializationKind(D->getSpecializationKind()); | ||||||||||
5899 | D2->setTemplateArgsInfo(ToTAInfo); | ||||||||||
5900 | |||||||||||
5901 | // Add this specialization to the class template. | ||||||||||
5902 | VarTemplate->AddSpecialization(D2, InsertPos); | ||||||||||
5903 | |||||||||||
5904 | // Import the qualifier, if any. | ||||||||||
5905 | if (auto LocOrErr = import(D->getQualifierLoc())) | ||||||||||
5906 | D2->setQualifierInfo(*LocOrErr); | ||||||||||
5907 | else | ||||||||||
5908 | return LocOrErr.takeError(); | ||||||||||
5909 | |||||||||||
5910 | if (D->isConstexpr()) | ||||||||||
5911 | D2->setConstexpr(true); | ||||||||||
5912 | |||||||||||
5913 | // Add the specialization to this context. | ||||||||||
5914 | D2->setLexicalDeclContext(LexicalDC); | ||||||||||
5915 | LexicalDC->addDeclInternal(D2); | ||||||||||
5916 | |||||||||||
5917 | D2->setAccess(D->getAccess()); | ||||||||||
5918 | } | ||||||||||
5919 | |||||||||||
5920 | if (Error Err = ImportInitializer(D, D2)) | ||||||||||
5921 | return std::move(Err); | ||||||||||
5922 | |||||||||||
5923 | return D2; | ||||||||||
5924 | } | ||||||||||
5925 | |||||||||||
5926 | ExpectedDecl | ||||||||||
5927 | ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { | ||||||||||
5928 | DeclContext *DC, *LexicalDC; | ||||||||||
5929 | DeclarationName Name; | ||||||||||
5930 | SourceLocation Loc; | ||||||||||
5931 | NamedDecl *ToD; | ||||||||||
5932 | |||||||||||
5933 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | ||||||||||
5934 | return std::move(Err); | ||||||||||
5935 | |||||||||||
5936 | if (ToD) | ||||||||||
5937 | return ToD; | ||||||||||
5938 | |||||||||||
5939 | const FunctionTemplateDecl *FoundByLookup = nullptr; | ||||||||||
5940 | |||||||||||
5941 | // Try to find a function in our own ("to") context with the same name, same | ||||||||||
5942 | // type, and in the same context as the function we're importing. | ||||||||||
5943 | // FIXME Split this into a separate function. | ||||||||||
5944 | if (!LexicalDC->isFunctionOrMethod()) { | ||||||||||
5945 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; | ||||||||||
5946 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | ||||||||||
5947 | for (auto *FoundDecl : FoundDecls) { | ||||||||||
5948 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | ||||||||||
5949 | continue; | ||||||||||
5950 | |||||||||||
5951 | if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) { | ||||||||||
5952 | if (!hasSameVisibilityContextAndLinkage(FoundTemplate, D)) | ||||||||||
5953 | continue; | ||||||||||
5954 | if (IsStructuralMatch(D, FoundTemplate)) { | ||||||||||
5955 | FunctionTemplateDecl *TemplateWithDef = | ||||||||||
5956 | getTemplateDefinition(FoundTemplate); | ||||||||||
5957 | if (D->isThisDeclarationADefinition() && TemplateWithDef) | ||||||||||
5958 | return Importer.MapImported(D, TemplateWithDef); | ||||||||||
5959 | |||||||||||
5960 | FoundByLookup = FoundTemplate; | ||||||||||
5961 | break; | ||||||||||
5962 | // TODO: handle conflicting names | ||||||||||
5963 | } | ||||||||||
5964 | } | ||||||||||
5965 | } | ||||||||||
5966 | } | ||||||||||
5967 | |||||||||||
5968 | auto ParamsOrErr = import(D->getTemplateParameters()); | ||||||||||
5969 | if (!ParamsOrErr) | ||||||||||
5970 | return ParamsOrErr.takeError(); | ||||||||||
5971 | |||||||||||
5972 | FunctionDecl *TemplatedFD; | ||||||||||
5973 | if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl())) | ||||||||||
5974 | return std::move(Err); | ||||||||||
5975 | |||||||||||
5976 | FunctionTemplateDecl *ToFunc; | ||||||||||
5977 | if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name, | ||||||||||
5978 | *ParamsOrErr, TemplatedFD)) | ||||||||||
5979 | return ToFunc; | ||||||||||
5980 | |||||||||||
5981 | TemplatedFD->setDescribedFunctionTemplate(ToFunc); | ||||||||||
5982 | |||||||||||
5983 | ToFunc->setAccess(D->getAccess()); | ||||||||||
5984 | ToFunc->setLexicalDeclContext(LexicalDC); | ||||||||||
5985 | LexicalDC->addDeclInternal(ToFunc); | ||||||||||
5986 | |||||||||||
5987 | if (FoundByLookup) { | ||||||||||
5988 | auto *Recent = | ||||||||||
5989 | const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl()); | ||||||||||
5990 | if (!TemplatedFD->getPreviousDecl()) { | ||||||||||
5991 | assert(FoundByLookup->getTemplatedDecl() &&((FoundByLookup->getTemplatedDecl() && "Found decl must have its templated decl set" ) ? static_cast<void> (0) : __assert_fail ("FoundByLookup->getTemplatedDecl() && \"Found decl must have its templated decl set\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 5992, __PRETTY_FUNCTION__)) | ||||||||||
5992 | "Found decl must have its templated decl set")((FoundByLookup->getTemplatedDecl() && "Found decl must have its templated decl set" ) ? static_cast<void> (0) : __assert_fail ("FoundByLookup->getTemplatedDecl() && \"Found decl must have its templated decl set\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 5992, __PRETTY_FUNCTION__)); | ||||||||||
5993 | auto *PrevTemplated = | ||||||||||
5994 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); | ||||||||||
5995 | if (TemplatedFD != PrevTemplated) | ||||||||||
5996 | TemplatedFD->setPreviousDecl(PrevTemplated); | ||||||||||
5997 | } | ||||||||||
5998 | ToFunc->setPreviousDecl(Recent); | ||||||||||
5999 | } | ||||||||||
6000 | |||||||||||
6001 | return ToFunc; | ||||||||||
6002 | } | ||||||||||
6003 | |||||||||||
6004 | //---------------------------------------------------------------------------- | ||||||||||
6005 | // Import Statements | ||||||||||
6006 | //---------------------------------------------------------------------------- | ||||||||||
6007 | |||||||||||
6008 | ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) { | ||||||||||
6009 | Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node) | ||||||||||
6010 | << S->getStmtClassName(); | ||||||||||
6011 | return make_error<ImportError>(ImportError::UnsupportedConstruct); | ||||||||||
6012 | } | ||||||||||
6013 | |||||||||||
6014 | |||||||||||
6015 | ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { | ||||||||||
6016 | if (Importer.returnWithErrorInTest()) | ||||||||||
6017 | return make_error<ImportError>(ImportError::UnsupportedConstruct); | ||||||||||
6018 | SmallVector<IdentifierInfo *, 4> Names; | ||||||||||
6019 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { | ||||||||||
6020 | IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I)); | ||||||||||
6021 | // ToII is nullptr when no symbolic name is given for output operand | ||||||||||
6022 | // see ParseStmtAsm::ParseAsmOperandsOpt | ||||||||||
6023 | Names.push_back(ToII); | ||||||||||
6024 | } | ||||||||||
6025 | |||||||||||
6026 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { | ||||||||||
6027 | IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I)); | ||||||||||
6028 | // ToII is nullptr when no symbolic name is given for input operand | ||||||||||
6029 | // see ParseStmtAsm::ParseAsmOperandsOpt | ||||||||||
6030 | Names.push_back(ToII); | ||||||||||
6031 | } | ||||||||||
6032 | |||||||||||
6033 | SmallVector<StringLiteral *, 4> Clobbers; | ||||||||||
6034 | for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { | ||||||||||
6035 | if (auto ClobberOrErr = import(S->getClobberStringLiteral(I))) | ||||||||||
6036 | Clobbers.push_back(*ClobberOrErr); | ||||||||||
6037 | else | ||||||||||
6038 | return ClobberOrErr.takeError(); | ||||||||||
6039 | |||||||||||
6040 | } | ||||||||||
6041 | |||||||||||
6042 | SmallVector<StringLiteral *, 4> Constraints; | ||||||||||
6043 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { | ||||||||||
6044 | if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I))) | ||||||||||
6045 | Constraints.push_back(*OutputOrErr); | ||||||||||
6046 | else | ||||||||||
6047 | return OutputOrErr.takeError(); | ||||||||||
6048 | } | ||||||||||
6049 | |||||||||||
6050 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { | ||||||||||
6051 | if (auto InputOrErr = import(S->getInputConstraintLiteral(I))) | ||||||||||
6052 | Constraints.push_back(*InputOrErr); | ||||||||||
6053 | else | ||||||||||
6054 | return InputOrErr.takeError(); | ||||||||||
6055 | } | ||||||||||
6056 | |||||||||||
6057 | SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() + | ||||||||||
6058 | S->getNumLabels()); | ||||||||||
6059 | if (Error Err = ImportContainerChecked(S->outputs(), Exprs)) | ||||||||||
6060 | return std::move(Err); | ||||||||||
6061 | |||||||||||
6062 | if (Error Err = | ||||||||||
6063 | ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs())) | ||||||||||
6064 | return std::move(Err); | ||||||||||
6065 | |||||||||||
6066 | if (Error Err = ImportArrayChecked( | ||||||||||
6067 | S->labels(), Exprs.begin() + S->getNumOutputs() + S->getNumInputs())) | ||||||||||
6068 | return std::move(Err); | ||||||||||
6069 | |||||||||||
6070 | ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc()); | ||||||||||
6071 | if (!AsmLocOrErr) | ||||||||||
6072 | return AsmLocOrErr.takeError(); | ||||||||||
6073 | auto AsmStrOrErr = import(S->getAsmString()); | ||||||||||
6074 | if (!AsmStrOrErr) | ||||||||||
6075 | return AsmStrOrErr.takeError(); | ||||||||||
6076 | ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc()); | ||||||||||
6077 | if (!RParenLocOrErr) | ||||||||||
6078 | return RParenLocOrErr.takeError(); | ||||||||||
6079 | |||||||||||
6080 | return new (Importer.getToContext()) GCCAsmStmt( | ||||||||||
6081 | Importer.getToContext(), | ||||||||||
6082 | *AsmLocOrErr, | ||||||||||
6083 | S->isSimple(), | ||||||||||
6084 | S->isVolatile(), | ||||||||||
6085 | S->getNumOutputs(), | ||||||||||
6086 | S->getNumInputs(), | ||||||||||
6087 | Names.data(), | ||||||||||
6088 | Constraints.data(), | ||||||||||
6089 | Exprs.data(), | ||||||||||
6090 | *AsmStrOrErr, | ||||||||||
6091 | S->getNumClobbers(), | ||||||||||
6092 | Clobbers.data(), | ||||||||||
6093 | S->getNumLabels(), | ||||||||||
6094 | *RParenLocOrErr); | ||||||||||
6095 | } | ||||||||||
6096 | |||||||||||
6097 | ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { | ||||||||||
6098 | |||||||||||
6099 | Error Err = Error::success(); | ||||||||||
6100 | auto ToDG = importChecked(Err, S->getDeclGroup()); | ||||||||||
6101 | auto ToBeginLoc = importChecked(Err, S->getBeginLoc()); | ||||||||||
6102 | auto ToEndLoc = importChecked(Err, S->getEndLoc()); | ||||||||||
6103 | if (Err) | ||||||||||
6104 | return std::move(Err); | ||||||||||
6105 | return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc); | ||||||||||
6106 | } | ||||||||||
6107 | |||||||||||
6108 | ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) { | ||||||||||
6109 | ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc()); | ||||||||||
6110 | if (!ToSemiLocOrErr) | ||||||||||
6111 | return ToSemiLocOrErr.takeError(); | ||||||||||
6112 | return new (Importer.getToContext()) NullStmt( | ||||||||||
6113 | *ToSemiLocOrErr, S->hasLeadingEmptyMacro()); | ||||||||||
6114 | } | ||||||||||
6115 | |||||||||||
6116 | ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { | ||||||||||
6117 | SmallVector<Stmt *, 8> ToStmts(S->size()); | ||||||||||
6118 | |||||||||||
6119 | if (Error Err = ImportContainerChecked(S->body(), ToStmts)) | ||||||||||
6120 | return std::move(Err); | ||||||||||
6121 | |||||||||||
6122 | ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc()); | ||||||||||
6123 | if (!ToLBracLocOrErr) | ||||||||||
6124 | return ToLBracLocOrErr.takeError(); | ||||||||||
6125 | |||||||||||
6126 | ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc()); | ||||||||||
6127 | if (!ToRBracLocOrErr) | ||||||||||
6128 | return ToRBracLocOrErr.takeError(); | ||||||||||
6129 | |||||||||||
6130 | return CompoundStmt::Create( | ||||||||||
6131 | Importer.getToContext(), ToStmts, | ||||||||||
6132 | *ToLBracLocOrErr, *ToRBracLocOrErr); | ||||||||||
6133 | } | ||||||||||
6134 | |||||||||||
6135 | ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { | ||||||||||
6136 | |||||||||||
6137 | Error Err = Error::success(); | ||||||||||
6138 | auto ToLHS = importChecked(Err, S->getLHS()); | ||||||||||
6139 | auto ToRHS = importChecked(Err, S->getRHS()); | ||||||||||
6140 | auto ToSubStmt = importChecked(Err, S->getSubStmt()); | ||||||||||
6141 | auto ToCaseLoc = importChecked(Err, S->getCaseLoc()); | ||||||||||
6142 | auto ToEllipsisLoc = importChecked(Err, S->getEllipsisLoc()); | ||||||||||
6143 | auto ToColonLoc = importChecked(Err, S->getColonLoc()); | ||||||||||
6144 | if (Err) | ||||||||||
6145 | return std::move(Err); | ||||||||||
6146 | |||||||||||
6147 | auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS, | ||||||||||
6148 | ToCaseLoc, ToEllipsisLoc, ToColonLoc); | ||||||||||
6149 | ToStmt->setSubStmt(ToSubStmt); | ||||||||||
6150 | |||||||||||
6151 | return ToStmt; | ||||||||||
6152 | } | ||||||||||
6153 | |||||||||||
6154 | ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { | ||||||||||
6155 | |||||||||||
6156 | Error Err = Error::success(); | ||||||||||
6157 | auto ToDefaultLoc = importChecked(Err, S->getDefaultLoc()); | ||||||||||
6158 | auto ToColonLoc = importChecked(Err, S->getColonLoc()); | ||||||||||
6159 | auto ToSubStmt = importChecked(Err, S->getSubStmt()); | ||||||||||
6160 | if (Err) | ||||||||||
6161 | return std::move(Err); | ||||||||||
6162 | |||||||||||
6163 | return new (Importer.getToContext()) DefaultStmt( | ||||||||||
6164 | ToDefaultLoc, ToColonLoc, ToSubStmt); | ||||||||||
6165 | } | ||||||||||
6166 | |||||||||||
6167 | ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { | ||||||||||
6168 | |||||||||||
6169 | Error Err = Error::success(); | ||||||||||
6170 | auto ToIdentLoc = importChecked(Err, S->getIdentLoc()); | ||||||||||
6171 | auto ToLabelDecl = importChecked(Err, S->getDecl()); | ||||||||||
6172 | auto ToSubStmt = importChecked(Err, S->getSubStmt()); | ||||||||||
6173 | if (Err) | ||||||||||
6174 | return std::move(Err); | ||||||||||
6175 | |||||||||||
6176 | return new (Importer.getToContext()) LabelStmt( | ||||||||||
6177 | ToIdentLoc, ToLabelDecl, ToSubStmt); | ||||||||||
6178 | } | ||||||||||
6179 | |||||||||||
6180 | ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { | ||||||||||
6181 | ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc()); | ||||||||||
6182 | if (!ToAttrLocOrErr) | ||||||||||
6183 | return ToAttrLocOrErr.takeError(); | ||||||||||
6184 | ArrayRef<const Attr*> FromAttrs(S->getAttrs()); | ||||||||||
6185 | SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); | ||||||||||
6186 | if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs)) | ||||||||||
6187 | return std::move(Err); | ||||||||||
6188 | ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt()); | ||||||||||
6189 | if (!ToSubStmtOrErr) | ||||||||||
6190 | return ToSubStmtOrErr.takeError(); | ||||||||||
6191 | |||||||||||
6192 | return AttributedStmt::Create( | ||||||||||
6193 | Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr); | ||||||||||
6194 | } | ||||||||||
6195 | |||||||||||
6196 | ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) { | ||||||||||
6197 | |||||||||||
6198 | Error Err = Error::success(); | ||||||||||
6199 | auto ToIfLoc = importChecked(Err, S->getIfLoc()); | ||||||||||
6200 | auto ToInit = importChecked(Err, S->getInit()); | ||||||||||
6201 | auto ToConditionVariable = importChecked(Err, S->getConditionVariable()); | ||||||||||
6202 | auto ToCond = importChecked(Err, S->getCond()); | ||||||||||
6203 | auto ToLParenLoc = importChecked(Err, S->getLParenLoc()); | ||||||||||
6204 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | ||||||||||
6205 | auto ToThen = importChecked(Err, S->getThen()); | ||||||||||
6206 | auto ToElseLoc = importChecked(Err, S->getElseLoc()); | ||||||||||
6207 | auto ToElse = importChecked(Err, S->getElse()); | ||||||||||
6208 | if (Err) | ||||||||||
6209 | return std::move(Err); | ||||||||||
6210 | |||||||||||
6211 | return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(), | ||||||||||
6212 | ToInit, ToConditionVariable, ToCond, ToLParenLoc, | ||||||||||
6213 | ToRParenLoc, ToThen, ToElseLoc, ToElse); | ||||||||||
6214 | } | ||||||||||
6215 | |||||||||||
6216 | ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { | ||||||||||
6217 | |||||||||||
6218 | Error Err = Error::success(); | ||||||||||
6219 | auto ToInit = importChecked(Err, S->getInit()); | ||||||||||
6220 | auto ToConditionVariable = importChecked(Err, S->getConditionVariable()); | ||||||||||
6221 | auto ToCond = importChecked(Err, S->getCond()); | ||||||||||
6222 | auto ToLParenLoc = importChecked(Err, S->getLParenLoc()); | ||||||||||
6223 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | ||||||||||
6224 | auto ToBody = importChecked(Err, S->getBody()); | ||||||||||
6225 | auto ToSwitchLoc = importChecked(Err, S->getSwitchLoc()); | ||||||||||
6226 | if (Err) | ||||||||||
6227 | return std::move(Err); | ||||||||||
6228 | |||||||||||
6229 | auto *ToStmt = | ||||||||||
6230 | SwitchStmt::Create(Importer.getToContext(), ToInit, ToConditionVariable, | ||||||||||
6231 | ToCond, ToLParenLoc, ToRParenLoc); | ||||||||||
6232 | ToStmt->setBody(ToBody); | ||||||||||
6233 | ToStmt->setSwitchLoc(ToSwitchLoc); | ||||||||||
6234 | |||||||||||
6235 | // Now we have to re-chain the cases. | ||||||||||
6236 | SwitchCase *LastChainedSwitchCase = nullptr; | ||||||||||
6237 | for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; | ||||||||||
6238 | SC = SC->getNextSwitchCase()) { | ||||||||||
6239 | Expected<SwitchCase *> ToSCOrErr = import(SC); | ||||||||||
6240 | if (!ToSCOrErr) | ||||||||||
6241 | return ToSCOrErr.takeError(); | ||||||||||
6242 | if (LastChainedSwitchCase) | ||||||||||
6243 | LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr); | ||||||||||
6244 | else | ||||||||||
6245 | ToStmt->setSwitchCaseList(*ToSCOrErr); | ||||||||||
6246 | LastChainedSwitchCase = *ToSCOrErr; | ||||||||||
6247 | } | ||||||||||
6248 | |||||||||||
6249 | return ToStmt; | ||||||||||
6250 | } | ||||||||||
6251 | |||||||||||
6252 | ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { | ||||||||||
6253 | |||||||||||
6254 | Error Err = Error::success(); | ||||||||||
6255 | auto ToConditionVariable = importChecked(Err, S->getConditionVariable()); | ||||||||||
6256 | auto ToCond = importChecked(Err, S->getCond()); | ||||||||||
6257 | auto ToBody = importChecked(Err, S->getBody()); | ||||||||||
6258 | auto ToWhileLoc = importChecked(Err, S->getWhileLoc()); | ||||||||||
6259 | auto ToLParenLoc = importChecked(Err, S->getLParenLoc()); | ||||||||||
6260 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | ||||||||||
6261 | if (Err) | ||||||||||
6262 | return std::move(Err); | ||||||||||
6263 | |||||||||||
6264 | return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond, | ||||||||||
6265 | ToBody, ToWhileLoc, ToLParenLoc, ToRParenLoc); | ||||||||||
6266 | } | ||||||||||
6267 | |||||||||||
6268 | ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) { | ||||||||||
6269 | |||||||||||
6270 | Error Err = Error::success(); | ||||||||||
6271 | auto ToBody = importChecked(Err, S->getBody()); | ||||||||||
6272 | auto ToCond = importChecked(Err, S->getCond()); | ||||||||||
6273 | auto ToDoLoc = importChecked(Err, S->getDoLoc()); | ||||||||||
6274 | auto ToWhileLoc = importChecked(Err, S->getWhileLoc()); | ||||||||||
6275 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | ||||||||||
6276 | if (Err) | ||||||||||
6277 | return std::move(Err); | ||||||||||
6278 | |||||||||||
6279 | return new (Importer.getToContext()) DoStmt( | ||||||||||
6280 | ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc); | ||||||||||
6281 | } | ||||||||||
6282 | |||||||||||
6283 | ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) { | ||||||||||
6284 | |||||||||||
6285 | Error Err = Error::success(); | ||||||||||
6286 | auto ToInit = importChecked(Err, S->getInit()); | ||||||||||
6287 | auto ToCond = importChecked(Err, S->getCond()); | ||||||||||
6288 | auto ToConditionVariable = importChecked(Err, S->getConditionVariable()); | ||||||||||
6289 | auto ToInc = importChecked(Err, S->getInc()); | ||||||||||
6290 | auto ToBody = importChecked(Err, S->getBody()); | ||||||||||
6291 | auto ToForLoc = importChecked(Err, S->getForLoc()); | ||||||||||
6292 | auto ToLParenLoc = importChecked(Err, S->getLParenLoc()); | ||||||||||
6293 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | ||||||||||
6294 | if (Err) | ||||||||||
6295 | return std::move(Err); | ||||||||||
6296 | |||||||||||
6297 | return new (Importer.getToContext()) ForStmt( | ||||||||||
6298 | Importer.getToContext(), | ||||||||||
6299 | ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc, | ||||||||||
6300 | ToRParenLoc); | ||||||||||
6301 | } | ||||||||||
6302 | |||||||||||
6303 | ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { | ||||||||||
6304 | |||||||||||
6305 | Error Err = Error::success(); | ||||||||||
6306 | auto ToLabel = importChecked(Err, S->getLabel()); | ||||||||||
6307 | auto ToGotoLoc = importChecked(Err, S->getGotoLoc()); | ||||||||||
6308 | auto ToLabelLoc = importChecked(Err, S->getLabelLoc()); | ||||||||||
6309 | if (Err) | ||||||||||
6310 | return std::move(Err); | ||||||||||
6311 | |||||||||||
6312 | return new (Importer.getToContext()) GotoStmt( | ||||||||||
6313 | ToLabel, ToGotoLoc, ToLabelLoc); | ||||||||||
6314 | } | ||||||||||
6315 | |||||||||||
6316 | ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { | ||||||||||
6317 | |||||||||||
6318 | Error Err = Error::success(); | ||||||||||
6319 | auto ToGotoLoc = importChecked(Err, S->getGotoLoc()); | ||||||||||
6320 | auto ToStarLoc = importChecked(Err, S->getStarLoc()); | ||||||||||
6321 | auto ToTarget = importChecked(Err, S->getTarget()); | ||||||||||
6322 | if (Err) | ||||||||||
6323 | return std::move(Err); | ||||||||||
6324 | |||||||||||
6325 | return new (Importer.getToContext()) IndirectGotoStmt( | ||||||||||
6326 | ToGotoLoc, ToStarLoc, ToTarget); | ||||||||||
6327 | } | ||||||||||
6328 | |||||||||||
6329 | ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { | ||||||||||
6330 | ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc()); | ||||||||||
6331 | if (!ToContinueLocOrErr) | ||||||||||
6332 | return ToContinueLocOrErr.takeError(); | ||||||||||
6333 | return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr); | ||||||||||
6334 | } | ||||||||||
6335 | |||||||||||
6336 | ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { | ||||||||||
6337 | auto ToBreakLocOrErr = import(S->getBreakLoc()); | ||||||||||
6338 | if (!ToBreakLocOrErr) | ||||||||||
6339 | return ToBreakLocOrErr.takeError(); | ||||||||||
6340 | return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr); | ||||||||||
6341 | } | ||||||||||
6342 | |||||||||||
6343 | ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { | ||||||||||
6344 | |||||||||||
6345 | Error Err = Error::success(); | ||||||||||
6346 | auto ToReturnLoc = importChecked(Err, S->getReturnLoc()); | ||||||||||
6347 | auto ToRetValue = importChecked(Err, S->getRetValue()); | ||||||||||
6348 | auto ToNRVOCandidate = importChecked(Err, S->getNRVOCandidate()); | ||||||||||
6349 | if (Err) | ||||||||||
6350 | return std::move(Err); | ||||||||||
6351 | |||||||||||
6352 | return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue, | ||||||||||
6353 | ToNRVOCandidate); | ||||||||||
6354 | } | ||||||||||
6355 | |||||||||||
6356 | ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { | ||||||||||
6357 | |||||||||||
6358 | Error Err = Error::success(); | ||||||||||
6359 | auto ToCatchLoc = importChecked(Err, S->getCatchLoc()); | ||||||||||
6360 | auto ToExceptionDecl = importChecked(Err, S->getExceptionDecl()); | ||||||||||
6361 | auto ToHandlerBlock = importChecked(Err, S->getHandlerBlock()); | ||||||||||
6362 | if (Err) | ||||||||||
6363 | return std::move(Err); | ||||||||||
6364 | |||||||||||
6365 | return new (Importer.getToContext()) CXXCatchStmt ( | ||||||||||
6366 | ToCatchLoc, ToExceptionDecl, ToHandlerBlock); | ||||||||||
6367 | } | ||||||||||
6368 | |||||||||||
6369 | ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { | ||||||||||
6370 | ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc()); | ||||||||||
6371 | if (!ToTryLocOrErr) | ||||||||||
6372 | return ToTryLocOrErr.takeError(); | ||||||||||
6373 | |||||||||||
6374 | ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock()); | ||||||||||
6375 | if (!ToTryBlockOrErr) | ||||||||||
6376 | return ToTryBlockOrErr.takeError(); | ||||||||||
6377 | |||||||||||
6378 | SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); | ||||||||||
6379 | for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { | ||||||||||
6380 | CXXCatchStmt *FromHandler = S->getHandler(HI); | ||||||||||
6381 | if (auto ToHandlerOrErr = import(FromHandler)) | ||||||||||
6382 | ToHandlers[HI] = *ToHandlerOrErr; | ||||||||||
6383 | else | ||||||||||
6384 | return ToHandlerOrErr.takeError(); | ||||||||||
6385 | } | ||||||||||
6386 | |||||||||||
6387 | return CXXTryStmt::Create( | ||||||||||
6388 | Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers); | ||||||||||
6389 | } | ||||||||||
6390 | |||||||||||
6391 | ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { | ||||||||||
6392 | |||||||||||
6393 | Error Err = Error::success(); | ||||||||||
6394 | auto ToInit = importChecked(Err, S->getInit()); | ||||||||||
6395 | auto ToRangeStmt = importChecked(Err, S->getRangeStmt()); | ||||||||||
6396 | auto ToBeginStmt = importChecked(Err, S->getBeginStmt()); | ||||||||||
6397 | auto ToEndStmt = importChecked(Err, S->getEndStmt()); | ||||||||||
6398 | auto ToCond = importChecked(Err, S->getCond()); | ||||||||||
6399 | auto ToInc = importChecked(Err, S->getInc()); | ||||||||||
6400 | auto ToLoopVarStmt = importChecked(Err, S->getLoopVarStmt()); | ||||||||||
6401 | auto ToBody = importChecked(Err, S->getBody()); | ||||||||||
6402 | auto ToForLoc = importChecked(Err, S->getForLoc()); | ||||||||||
6403 | auto ToCoawaitLoc = importChecked(Err, S->getCoawaitLoc()); | ||||||||||
6404 | auto ToColonLoc = importChecked(Err, S->getColonLoc()); | ||||||||||
6405 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | ||||||||||
6406 | if (Err) | ||||||||||
6407 | return std::move(Err); | ||||||||||
6408 | |||||||||||
6409 | return new (Importer.getToContext()) CXXForRangeStmt( | ||||||||||
6410 | ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt, | ||||||||||
6411 | ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc); | ||||||||||
6412 | } | ||||||||||
6413 | |||||||||||
6414 | ExpectedStmt | ||||||||||
6415 | ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { | ||||||||||
6416 | Error Err = Error::success(); | ||||||||||
6417 | auto ToElement = importChecked(Err, S->getElement()); | ||||||||||
6418 | auto ToCollection = importChecked(Err, S->getCollection()); | ||||||||||
6419 | auto ToBody = importChecked(Err, S->getBody()); | ||||||||||
6420 | auto ToForLoc = importChecked(Err, S->getForLoc()); | ||||||||||
6421 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | ||||||||||
6422 | if (Err) | ||||||||||
6423 | return std::move(Err); | ||||||||||
6424 | |||||||||||
6425 | return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement, | ||||||||||
6426 | ToCollection, | ||||||||||
6427 | ToBody, | ||||||||||
6428 | ToForLoc, | ||||||||||
6429 | ToRParenLoc); | ||||||||||
6430 | } | ||||||||||
6431 | |||||||||||
6432 | ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { | ||||||||||
6433 | |||||||||||
6434 | Error Err = Error::success(); | ||||||||||
6435 | auto ToAtCatchLoc = importChecked(Err, S->getAtCatchLoc()); | ||||||||||
6436 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | ||||||||||
6437 | auto ToCatchParamDecl = importChecked(Err, S->getCatchParamDecl()); | ||||||||||
6438 | auto ToCatchBody = importChecked(Err, S->getCatchBody()); | ||||||||||
6439 | if (Err) | ||||||||||
6440 | return std::move(Err); | ||||||||||
6441 | |||||||||||
6442 | return new (Importer.getToContext()) ObjCAtCatchStmt ( | ||||||||||
6443 | ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody); | ||||||||||
6444 | } | ||||||||||
6445 | |||||||||||
6446 | ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { | ||||||||||
6447 | ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc()); | ||||||||||
6448 | if (!ToAtFinallyLocOrErr) | ||||||||||
6449 | return ToAtFinallyLocOrErr.takeError(); | ||||||||||
6450 | ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody()); | ||||||||||
6451 | if (!ToAtFinallyStmtOrErr) | ||||||||||
6452 | return ToAtFinallyStmtOrErr.takeError(); | ||||||||||
6453 | return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr, | ||||||||||
6454 | *ToAtFinallyStmtOrErr); | ||||||||||
6455 | } | ||||||||||
6456 | |||||||||||
6457 | ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { | ||||||||||
6458 | |||||||||||
6459 | Error Err = Error::success(); | ||||||||||
6460 | auto ToAtTryLoc = importChecked(Err, S->getAtTryLoc()); | ||||||||||
6461 | auto ToTryBody = importChecked(Err, S->getTryBody()); | ||||||||||
6462 | auto ToFinallyStmt = importChecked(Err, S->getFinallyStmt()); | ||||||||||
6463 | if (Err) | ||||||||||
6464 | return std::move(Err); | ||||||||||
6465 | |||||||||||
6466 | SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); | ||||||||||
6467 | for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { | ||||||||||
6468 | ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI); | ||||||||||
6469 | if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt)) | ||||||||||
6470 | ToCatchStmts[CI] = *ToCatchStmtOrErr; | ||||||||||
6471 | else | ||||||||||
6472 | return ToCatchStmtOrErr.takeError(); | ||||||||||
6473 | } | ||||||||||
6474 | |||||||||||
6475 | return ObjCAtTryStmt::Create(Importer.getToContext(), | ||||||||||
6476 | ToAtTryLoc, ToTryBody, | ||||||||||
6477 | ToCatchStmts.begin(), ToCatchStmts.size(), | ||||||||||
6478 | ToFinallyStmt); | ||||||||||
6479 | } | ||||||||||
6480 | |||||||||||
6481 | ExpectedStmt | ||||||||||
6482 | ASTNodeImporter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { | ||||||||||
6483 | |||||||||||
6484 | Error Err = Error::success(); | ||||||||||
6485 | auto ToAtSynchronizedLoc = importChecked(Err, S->getAtSynchronizedLoc()); | ||||||||||
6486 | auto ToSynchExpr = importChecked(Err, S->getSynchExpr()); | ||||||||||
6487 | auto ToSynchBody = importChecked(Err, S->getSynchBody()); | ||||||||||
6488 | if (Err) | ||||||||||
6489 | return std::move(Err); | ||||||||||
6490 | |||||||||||
6491 | return new (Importer.getToContext()) ObjCAtSynchronizedStmt( | ||||||||||
6492 | ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); | ||||||||||
6493 | } | ||||||||||
6494 | |||||||||||
6495 | ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { | ||||||||||
6496 | ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc()); | ||||||||||
6497 | if (!ToThrowLocOrErr) | ||||||||||
6498 | return ToThrowLocOrErr.takeError(); | ||||||||||
6499 | ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr()); | ||||||||||
6500 | if (!ToThrowExprOrErr) | ||||||||||
6501 | return ToThrowExprOrErr.takeError(); | ||||||||||
6502 | return new (Importer.getToContext()) ObjCAtThrowStmt( | ||||||||||
6503 | *ToThrowLocOrErr, *ToThrowExprOrErr); | ||||||||||
6504 | } | ||||||||||
6505 | |||||||||||
6506 | ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt( | ||||||||||
6507 | ObjCAutoreleasePoolStmt *S) { | ||||||||||
6508 | ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc()); | ||||||||||
6509 | if (!ToAtLocOrErr) | ||||||||||
6510 | return ToAtLocOrErr.takeError(); | ||||||||||
6511 | ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt()); | ||||||||||
6512 | if (!ToSubStmtOrErr) | ||||||||||
6513 | return ToSubStmtOrErr.takeError(); | ||||||||||
6514 | return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr, | ||||||||||
6515 | *ToSubStmtOrErr); | ||||||||||
6516 | } | ||||||||||
6517 | |||||||||||
6518 | //---------------------------------------------------------------------------- | ||||||||||
6519 | // Import Expressions | ||||||||||
6520 | //---------------------------------------------------------------------------- | ||||||||||
6521 | ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) { | ||||||||||
6522 | Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node) | ||||||||||
6523 | << E->getStmtClassName(); | ||||||||||
6524 | return make_error<ImportError>(ImportError::UnsupportedConstruct); | ||||||||||
6525 | } | ||||||||||
6526 | |||||||||||
6527 | ExpectedStmt ASTNodeImporter::VisitSourceLocExpr(SourceLocExpr *E) { | ||||||||||
6528 | Error Err = Error::success(); | ||||||||||
6529 | auto BLoc = importChecked(Err, E->getBeginLoc()); | ||||||||||
6530 | auto RParenLoc = importChecked(Err, E->getEndLoc()); | ||||||||||
6531 | if (Err) | ||||||||||
6532 | return std::move(Err); | ||||||||||
6533 | auto ParentContextOrErr = Importer.ImportContext(E->getParentContext()); | ||||||||||
6534 | if (!ParentContextOrErr) | ||||||||||
6535 | return ParentContextOrErr.takeError(); | ||||||||||
6536 | |||||||||||
6537 | return new (Importer.getToContext()) | ||||||||||
6538 | SourceLocExpr(Importer.getToContext(), E->getIdentKind(), BLoc, RParenLoc, | ||||||||||
6539 | *ParentContextOrErr); | ||||||||||
6540 | } | ||||||||||
6541 | |||||||||||
6542 | ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { | ||||||||||
6543 | |||||||||||
6544 | Error Err = Error::success(); | ||||||||||
6545 | auto ToBuiltinLoc = importChecked(Err, E->getBuiltinLoc()); | ||||||||||
6546 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | ||||||||||
6547 | auto ToWrittenTypeInfo = importChecked(Err, E->getWrittenTypeInfo()); | ||||||||||
6548 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
6549 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6550 | if (Err) | ||||||||||
6551 | return std::move(Err); | ||||||||||
6552 | |||||||||||
6553 | return new (Importer.getToContext()) VAArgExpr( | ||||||||||
6554 | ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType, | ||||||||||
6555 | E->isMicrosoftABI()); | ||||||||||
6556 | } | ||||||||||
6557 | |||||||||||
6558 | ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) { | ||||||||||
6559 | |||||||||||
6560 | Error Err = Error::success(); | ||||||||||
6561 | auto ToCond = importChecked(Err, E->getCond()); | ||||||||||
6562 | auto ToLHS = importChecked(Err, E->getLHS()); | ||||||||||
6563 | auto ToRHS = importChecked(Err, E->getRHS()); | ||||||||||
6564 | auto ToBuiltinLoc = importChecked(Err, E->getBuiltinLoc()); | ||||||||||
6565 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
6566 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6567 | if (Err) | ||||||||||
6568 | return std::move(Err); | ||||||||||
6569 | |||||||||||
6570 | ExprValueKind VK = E->getValueKind(); | ||||||||||
6571 | ExprObjectKind OK = E->getObjectKind(); | ||||||||||
6572 | |||||||||||
6573 | // The value of CondIsTrue only matters if the value is not | ||||||||||
6574 | // condition-dependent. | ||||||||||
6575 | bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue(); | ||||||||||
6576 | |||||||||||
6577 | return new (Importer.getToContext()) | ||||||||||
6578 | ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK, | ||||||||||
6579 | ToRParenLoc, CondIsTrue); | ||||||||||
6580 | } | ||||||||||
6581 | |||||||||||
6582 | ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { | ||||||||||
6583 | ExpectedType TypeOrErr = import(E->getType()); | ||||||||||
6584 | if (!TypeOrErr) | ||||||||||
6585 | return TypeOrErr.takeError(); | ||||||||||
6586 | |||||||||||
6587 | ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc()); | ||||||||||
6588 | if (!BeginLocOrErr) | ||||||||||
6589 | return BeginLocOrErr.takeError(); | ||||||||||
6590 | |||||||||||
6591 | return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr); | ||||||||||
6592 | } | ||||||||||
6593 | |||||||||||
6594 | ExpectedStmt | ||||||||||
6595 | ASTNodeImporter::VisitGenericSelectionExpr(GenericSelectionExpr *E) { | ||||||||||
6596 | Error Err = Error::success(); | ||||||||||
6597 | auto ToGenericLoc = importChecked(Err, E->getGenericLoc()); | ||||||||||
6598 | auto *ToControllingExpr = importChecked(Err, E->getControllingExpr()); | ||||||||||
6599 | auto ToDefaultLoc = importChecked(Err, E->getDefaultLoc()); | ||||||||||
6600 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
6601 | if (Err) | ||||||||||
6602 | return std::move(Err); | ||||||||||
6603 | |||||||||||
6604 | ArrayRef<const TypeSourceInfo *> FromAssocTypes(E->getAssocTypeSourceInfos()); | ||||||||||
6605 | SmallVector<TypeSourceInfo *, 1> ToAssocTypes(FromAssocTypes.size()); | ||||||||||
6606 | if (Error Err = ImportContainerChecked(FromAssocTypes, ToAssocTypes)) | ||||||||||
6607 | return std::move(Err); | ||||||||||
6608 | |||||||||||
6609 | ArrayRef<const Expr *> FromAssocExprs(E->getAssocExprs()); | ||||||||||
6610 | SmallVector<Expr *, 1> ToAssocExprs(FromAssocExprs.size()); | ||||||||||
6611 | if (Error Err = ImportContainerChecked(FromAssocExprs, ToAssocExprs)) | ||||||||||
6612 | return std::move(Err); | ||||||||||
6613 | |||||||||||
6614 | const ASTContext &ToCtx = Importer.getToContext(); | ||||||||||
6615 | if (E->isResultDependent()) { | ||||||||||
6616 | return GenericSelectionExpr::Create( | ||||||||||
6617 | ToCtx, ToGenericLoc, ToControllingExpr, | ||||||||||
6618 | llvm::makeArrayRef(ToAssocTypes), llvm::makeArrayRef(ToAssocExprs), | ||||||||||
6619 | ToDefaultLoc, ToRParenLoc, E->containsUnexpandedParameterPack()); | ||||||||||
6620 | } | ||||||||||
6621 | |||||||||||
6622 | return GenericSelectionExpr::Create( | ||||||||||
6623 | ToCtx, ToGenericLoc, ToControllingExpr, llvm::makeArrayRef(ToAssocTypes), | ||||||||||
6624 | llvm::makeArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, | ||||||||||
6625 | E->containsUnexpandedParameterPack(), E->getResultIndex()); | ||||||||||
6626 | } | ||||||||||
6627 | |||||||||||
6628 | ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { | ||||||||||
6629 | |||||||||||
6630 | Error Err = Error::success(); | ||||||||||
6631 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | ||||||||||
6632 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6633 | auto ToFunctionName = importChecked(Err, E->getFunctionName()); | ||||||||||
6634 | if (Err) | ||||||||||
6635 | return std::move(Err); | ||||||||||
6636 | |||||||||||
6637 | return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType, | ||||||||||
6638 | E->getIdentKind(), ToFunctionName); | ||||||||||
6639 | } | ||||||||||
6640 | |||||||||||
6641 | ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { | ||||||||||
6642 | |||||||||||
6643 | Error Err = Error::success(); | ||||||||||
6644 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | ||||||||||
6645 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); | ||||||||||
6646 | auto ToDecl = importChecked(Err, E->getDecl()); | ||||||||||
6647 | auto ToLocation = importChecked(Err, E->getLocation()); | ||||||||||
6648 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6649 | if (Err) | ||||||||||
6650 | return std::move(Err); | ||||||||||
6651 | |||||||||||
6652 | NamedDecl *ToFoundD = nullptr; | ||||||||||
6653 | if (E->getDecl() != E->getFoundDecl()) { | ||||||||||
6654 | auto FoundDOrErr = import(E->getFoundDecl()); | ||||||||||
6655 | if (!FoundDOrErr) | ||||||||||
6656 | return FoundDOrErr.takeError(); | ||||||||||
6657 | ToFoundD = *FoundDOrErr; | ||||||||||
6658 | } | ||||||||||
6659 | |||||||||||
6660 | TemplateArgumentListInfo ToTAInfo; | ||||||||||
6661 | TemplateArgumentListInfo *ToResInfo = nullptr; | ||||||||||
6662 | if (E->hasExplicitTemplateArgs()) { | ||||||||||
6663 | if (Error Err = | ||||||||||
6664 | ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(), | ||||||||||
6665 | E->template_arguments(), ToTAInfo)) | ||||||||||
6666 | return std::move(Err); | ||||||||||
6667 | ToResInfo = &ToTAInfo; | ||||||||||
6668 | } | ||||||||||
6669 | |||||||||||
6670 | auto *ToE = DeclRefExpr::Create( | ||||||||||
6671 | Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, | ||||||||||
6672 | E->refersToEnclosingVariableOrCapture(), ToLocation, ToType, | ||||||||||
6673 | E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse()); | ||||||||||
6674 | if (E->hadMultipleCandidates()) | ||||||||||
6675 | ToE->setHadMultipleCandidates(true); | ||||||||||
6676 | return ToE; | ||||||||||
6677 | } | ||||||||||
6678 | |||||||||||
6679 | ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { | ||||||||||
6680 | ExpectedType TypeOrErr = import(E->getType()); | ||||||||||
6681 | if (!TypeOrErr) | ||||||||||
6682 | return TypeOrErr.takeError(); | ||||||||||
6683 | |||||||||||
6684 | return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr); | ||||||||||
6685 | } | ||||||||||
6686 | |||||||||||
6687 | ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) { | ||||||||||
6688 | ExpectedExpr ToInitOrErr = import(E->getInit()); | ||||||||||
6689 | if (!ToInitOrErr) | ||||||||||
6690 | return ToInitOrErr.takeError(); | ||||||||||
6691 | |||||||||||
6692 | ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc()); | ||||||||||
6693 | if (!ToEqualOrColonLocOrErr) | ||||||||||
6694 | return ToEqualOrColonLocOrErr.takeError(); | ||||||||||
6695 | |||||||||||
6696 | SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1); | ||||||||||
6697 | // List elements from the second, the first is Init itself | ||||||||||
6698 | for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) { | ||||||||||
6699 | if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I))) | ||||||||||
6700 | ToIndexExprs[I - 1] = *ToArgOrErr; | ||||||||||
6701 | else | ||||||||||
6702 | return ToArgOrErr.takeError(); | ||||||||||
6703 | } | ||||||||||
6704 | |||||||||||
6705 | SmallVector<Designator, 4> ToDesignators(E->size()); | ||||||||||
6706 | if (Error Err = ImportContainerChecked(E->designators(), ToDesignators)) | ||||||||||
6707 | return std::move(Err); | ||||||||||
6708 | |||||||||||
6709 | return DesignatedInitExpr::Create( | ||||||||||
6710 | Importer.getToContext(), ToDesignators, | ||||||||||
6711 | ToIndexExprs, *ToEqualOrColonLocOrErr, | ||||||||||
6712 | E->usesGNUSyntax(), *ToInitOrErr); | ||||||||||
6713 | } | ||||||||||
6714 | |||||||||||
6715 | ExpectedStmt | ||||||||||
6716 | ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { | ||||||||||
6717 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
6718 | if (!ToTypeOrErr) | ||||||||||
6719 | return ToTypeOrErr.takeError(); | ||||||||||
6720 | |||||||||||
6721 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | ||||||||||
6722 | if (!ToLocationOrErr) | ||||||||||
6723 | return ToLocationOrErr.takeError(); | ||||||||||
6724 | |||||||||||
6725 | return new (Importer.getToContext()) CXXNullPtrLiteralExpr( | ||||||||||
6726 | *ToTypeOrErr, *ToLocationOrErr); | ||||||||||
6727 | } | ||||||||||
6728 | |||||||||||
6729 | ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { | ||||||||||
6730 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
6731 | if (!ToTypeOrErr) | ||||||||||
6732 | return ToTypeOrErr.takeError(); | ||||||||||
6733 | |||||||||||
6734 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | ||||||||||
6735 | if (!ToLocationOrErr) | ||||||||||
6736 | return ToLocationOrErr.takeError(); | ||||||||||
6737 | |||||||||||
6738 | return IntegerLiteral::Create( | ||||||||||
6739 | Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr); | ||||||||||
6740 | } | ||||||||||
6741 | |||||||||||
6742 | |||||||||||
6743 | ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { | ||||||||||
6744 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
6745 | if (!ToTypeOrErr) | ||||||||||
6746 | return ToTypeOrErr.takeError(); | ||||||||||
6747 | |||||||||||
6748 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | ||||||||||
6749 | if (!ToLocationOrErr) | ||||||||||
6750 | return ToLocationOrErr.takeError(); | ||||||||||
6751 | |||||||||||
6752 | return FloatingLiteral::Create( | ||||||||||
6753 | Importer.getToContext(), E->getValue(), E->isExact(), | ||||||||||
6754 | *ToTypeOrErr, *ToLocationOrErr); | ||||||||||
6755 | } | ||||||||||
6756 | |||||||||||
6757 | ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) { | ||||||||||
6758 | auto ToTypeOrErr = import(E->getType()); | ||||||||||
6759 | if (!ToTypeOrErr) | ||||||||||
6760 | return ToTypeOrErr.takeError(); | ||||||||||
6761 | |||||||||||
6762 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); | ||||||||||
6763 | if (!ToSubExprOrErr) | ||||||||||
6764 | return ToSubExprOrErr.takeError(); | ||||||||||
6765 | |||||||||||
6766 | return new (Importer.getToContext()) ImaginaryLiteral( | ||||||||||
6767 | *ToSubExprOrErr, *ToTypeOrErr); | ||||||||||
6768 | } | ||||||||||
6769 | |||||||||||
6770 | ExpectedStmt ASTNodeImporter::VisitFixedPointLiteral(FixedPointLiteral *E) { | ||||||||||
6771 | auto ToTypeOrErr = import(E->getType()); | ||||||||||
6772 | if (!ToTypeOrErr) | ||||||||||
6773 | return ToTypeOrErr.takeError(); | ||||||||||
6774 | |||||||||||
6775 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | ||||||||||
6776 | if (!ToLocationOrErr) | ||||||||||
6777 | return ToLocationOrErr.takeError(); | ||||||||||
6778 | |||||||||||
6779 | return new (Importer.getToContext()) FixedPointLiteral( | ||||||||||
6780 | Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr, | ||||||||||
6781 | Importer.getToContext().getFixedPointScale(*ToTypeOrErr)); | ||||||||||
6782 | } | ||||||||||
6783 | |||||||||||
6784 | ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { | ||||||||||
6785 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
6786 | if (!ToTypeOrErr) | ||||||||||
6787 | return ToTypeOrErr.takeError(); | ||||||||||
6788 | |||||||||||
6789 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | ||||||||||
6790 | if (!ToLocationOrErr) | ||||||||||
6791 | return ToLocationOrErr.takeError(); | ||||||||||
6792 | |||||||||||
6793 | return new (Importer.getToContext()) CharacterLiteral( | ||||||||||
6794 | E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr); | ||||||||||
6795 | } | ||||||||||
6796 | |||||||||||
6797 | ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { | ||||||||||
6798 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
6799 | if (!ToTypeOrErr) | ||||||||||
6800 | return ToTypeOrErr.takeError(); | ||||||||||
6801 | |||||||||||
6802 | SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated()); | ||||||||||
6803 | if (Error Err = ImportArrayChecked( | ||||||||||
6804 | E->tokloc_begin(), E->tokloc_end(), ToLocations.begin())) | ||||||||||
6805 | return std::move(Err); | ||||||||||
6806 | |||||||||||
6807 | return StringLiteral::Create( | ||||||||||
6808 | Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(), | ||||||||||
6809 | *ToTypeOrErr, ToLocations.data(), ToLocations.size()); | ||||||||||
6810 | } | ||||||||||
6811 | |||||||||||
6812 | ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { | ||||||||||
6813 | |||||||||||
6814 | Error Err = Error::success(); | ||||||||||
6815 | auto ToLParenLoc = importChecked(Err, E->getLParenLoc()); | ||||||||||
6816 | auto ToTypeSourceInfo = importChecked(Err, E->getTypeSourceInfo()); | ||||||||||
6817 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6818 | auto ToInitializer = importChecked(Err, E->getInitializer()); | ||||||||||
6819 | if (Err) | ||||||||||
6820 | return std::move(Err); | ||||||||||
6821 | |||||||||||
6822 | return new (Importer.getToContext()) CompoundLiteralExpr( | ||||||||||
6823 | ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(), | ||||||||||
6824 | ToInitializer, E->isFileScope()); | ||||||||||
6825 | } | ||||||||||
6826 | |||||||||||
6827 | ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { | ||||||||||
6828 | |||||||||||
6829 | Error Err = Error::success(); | ||||||||||
6830 | auto ToBuiltinLoc = importChecked(Err, E->getBuiltinLoc()); | ||||||||||
6831 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6832 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
6833 | if (Err) | ||||||||||
6834 | return std::move(Err); | ||||||||||
6835 | |||||||||||
6836 | SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs()); | ||||||||||
6837 | if (Error Err = ImportArrayChecked( | ||||||||||
6838 | E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(), | ||||||||||
6839 | ToExprs.begin())) | ||||||||||
6840 | return std::move(Err); | ||||||||||
6841 | |||||||||||
6842 | return new (Importer.getToContext()) AtomicExpr( | ||||||||||
6843 | |||||||||||
6844 | ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc); | ||||||||||
6845 | } | ||||||||||
6846 | |||||||||||
6847 | ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { | ||||||||||
6848 | Error Err = Error::success(); | ||||||||||
6849 | auto ToAmpAmpLoc = importChecked(Err, E->getAmpAmpLoc()); | ||||||||||
6850 | auto ToLabelLoc = importChecked(Err, E->getLabelLoc()); | ||||||||||
6851 | auto ToLabel = importChecked(Err, E->getLabel()); | ||||||||||
6852 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6853 | if (Err) | ||||||||||
6854 | return std::move(Err); | ||||||||||
6855 | |||||||||||
6856 | return new (Importer.getToContext()) AddrLabelExpr( | ||||||||||
6857 | ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType); | ||||||||||
6858 | } | ||||||||||
6859 | ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) { | ||||||||||
6860 | Error Err = Error::success(); | ||||||||||
6861 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | ||||||||||
6862 | auto ToResult = importChecked(Err, E->getAPValueResult()); | ||||||||||
6863 | if (Err) | ||||||||||
6864 | return std::move(Err); | ||||||||||
6865 | |||||||||||
6866 | return ConstantExpr::Create(Importer.getToContext(), ToSubExpr, ToResult); | ||||||||||
6867 | } | ||||||||||
6868 | ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) { | ||||||||||
6869 | Error Err = Error::success(); | ||||||||||
6870 | auto ToLParen = importChecked(Err, E->getLParen()); | ||||||||||
6871 | auto ToRParen = importChecked(Err, E->getRParen()); | ||||||||||
6872 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | ||||||||||
6873 | if (Err) | ||||||||||
6874 | return std::move(Err); | ||||||||||
6875 | |||||||||||
6876 | return new (Importer.getToContext()) | ||||||||||
6877 | ParenExpr(ToLParen, ToRParen, ToSubExpr); | ||||||||||
6878 | } | ||||||||||
6879 | |||||||||||
6880 | ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { | ||||||||||
6881 | SmallVector<Expr *, 4> ToExprs(E->getNumExprs()); | ||||||||||
6882 | if (Error Err = ImportContainerChecked(E->exprs(), ToExprs)) | ||||||||||
6883 | return std::move(Err); | ||||||||||
6884 | |||||||||||
6885 | ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc()); | ||||||||||
6886 | if (!ToLParenLocOrErr) | ||||||||||
6887 | return ToLParenLocOrErr.takeError(); | ||||||||||
6888 | |||||||||||
6889 | ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc()); | ||||||||||
6890 | if (!ToRParenLocOrErr) | ||||||||||
6891 | return ToRParenLocOrErr.takeError(); | ||||||||||
6892 | |||||||||||
6893 | return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr, | ||||||||||
6894 | ToExprs, *ToRParenLocOrErr); | ||||||||||
6895 | } | ||||||||||
6896 | |||||||||||
6897 | ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { | ||||||||||
6898 | Error Err = Error::success(); | ||||||||||
6899 | auto ToSubStmt = importChecked(Err, E->getSubStmt()); | ||||||||||
6900 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6901 | auto ToLParenLoc = importChecked(Err, E->getLParenLoc()); | ||||||||||
6902 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
6903 | if (Err) | ||||||||||
6904 | return std::move(Err); | ||||||||||
6905 | |||||||||||
6906 | return new (Importer.getToContext()) | ||||||||||
6907 | StmtExpr(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc, | ||||||||||
6908 | E->getTemplateDepth()); | ||||||||||
6909 | } | ||||||||||
6910 | |||||||||||
6911 | ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { | ||||||||||
6912 | Error Err = Error::success(); | ||||||||||
6913 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | ||||||||||
6914 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6915 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
6916 | if (Err) | ||||||||||
6917 | return std::move(Err); | ||||||||||
6918 | |||||||||||
6919 | return UnaryOperator::Create( | ||||||||||
6920 | Importer.getToContext(), ToSubExpr, E->getOpcode(), ToType, | ||||||||||
6921 | E->getValueKind(), E->getObjectKind(), ToOperatorLoc, E->canOverflow(), | ||||||||||
6922 | E->getFPOptionsOverride()); | ||||||||||
6923 | } | ||||||||||
6924 | |||||||||||
6925 | ExpectedStmt | ||||||||||
6926 | |||||||||||
6927 | ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { | ||||||||||
6928 | Error Err = Error::success(); | ||||||||||
6929 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6930 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
6931 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
6932 | if (Err) | ||||||||||
6933 | return std::move(Err); | ||||||||||
6934 | |||||||||||
6935 | if (E->isArgumentType()) { | ||||||||||
6936 | Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr = | ||||||||||
6937 | import(E->getArgumentTypeInfo()); | ||||||||||
6938 | if (!ToArgumentTypeInfoOrErr) | ||||||||||
6939 | return ToArgumentTypeInfoOrErr.takeError(); | ||||||||||
6940 | |||||||||||
6941 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( | ||||||||||
6942 | E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc, | ||||||||||
6943 | ToRParenLoc); | ||||||||||
6944 | } | ||||||||||
6945 | |||||||||||
6946 | ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr()); | ||||||||||
6947 | if (!ToArgumentExprOrErr) | ||||||||||
6948 | return ToArgumentExprOrErr.takeError(); | ||||||||||
6949 | |||||||||||
6950 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( | ||||||||||
6951 | E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc); | ||||||||||
6952 | } | ||||||||||
6953 | |||||||||||
6954 | ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { | ||||||||||
6955 | Error Err = Error::success(); | ||||||||||
6956 | auto ToLHS = importChecked(Err, E->getLHS()); | ||||||||||
6957 | auto ToRHS = importChecked(Err, E->getRHS()); | ||||||||||
6958 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6959 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
6960 | if (Err) | ||||||||||
6961 | return std::move(Err); | ||||||||||
6962 | |||||||||||
6963 | return BinaryOperator::Create( | ||||||||||
6964 | Importer.getToContext(), ToLHS, ToRHS, E->getOpcode(), ToType, | ||||||||||
6965 | E->getValueKind(), E->getObjectKind(), ToOperatorLoc, | ||||||||||
6966 | E->getFPFeatures(Importer.getFromContext().getLangOpts())); | ||||||||||
6967 | } | ||||||||||
6968 | |||||||||||
6969 | ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { | ||||||||||
6970 | Error Err = Error::success(); | ||||||||||
6971 | auto ToCond = importChecked(Err, E->getCond()); | ||||||||||
6972 | auto ToQuestionLoc = importChecked(Err, E->getQuestionLoc()); | ||||||||||
6973 | auto ToLHS = importChecked(Err, E->getLHS()); | ||||||||||
6974 | auto ToColonLoc = importChecked(Err, E->getColonLoc()); | ||||||||||
6975 | auto ToRHS = importChecked(Err, E->getRHS()); | ||||||||||
6976 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6977 | if (Err) | ||||||||||
6978 | return std::move(Err); | ||||||||||
6979 | |||||||||||
6980 | return new (Importer.getToContext()) ConditionalOperator( | ||||||||||
6981 | ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType, | ||||||||||
6982 | E->getValueKind(), E->getObjectKind()); | ||||||||||
6983 | } | ||||||||||
6984 | |||||||||||
6985 | ExpectedStmt | ||||||||||
6986 | ASTNodeImporter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { | ||||||||||
6987 | Error Err = Error::success(); | ||||||||||
6988 | auto ToCommon = importChecked(Err, E->getCommon()); | ||||||||||
6989 | auto ToOpaqueValue = importChecked(Err, E->getOpaqueValue()); | ||||||||||
6990 | auto ToCond = importChecked(Err, E->getCond()); | ||||||||||
6991 | auto ToTrueExpr = importChecked(Err, E->getTrueExpr()); | ||||||||||
6992 | auto ToFalseExpr = importChecked(Err, E->getFalseExpr()); | ||||||||||
6993 | auto ToQuestionLoc = importChecked(Err, E->getQuestionLoc()); | ||||||||||
6994 | auto ToColonLoc = importChecked(Err, E->getColonLoc()); | ||||||||||
6995 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
6996 | if (Err) | ||||||||||
6997 | return std::move(Err); | ||||||||||
6998 | |||||||||||
6999 | return new (Importer.getToContext()) BinaryConditionalOperator( | ||||||||||
7000 | ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, | ||||||||||
7001 | ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(), | ||||||||||
7002 | E->getObjectKind()); | ||||||||||
7003 | } | ||||||||||
7004 | |||||||||||
7005 | ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { | ||||||||||
7006 | Error Err = Error::success(); | ||||||||||
7007 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | ||||||||||
7008 | auto ToQueriedTypeSourceInfo = | ||||||||||
7009 | importChecked(Err, E->getQueriedTypeSourceInfo()); | ||||||||||
7010 | auto ToDimensionExpression = importChecked(Err, E->getDimensionExpression()); | ||||||||||
7011 | auto ToEndLoc = importChecked(Err, E->getEndLoc()); | ||||||||||
7012 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7013 | if (Err) | ||||||||||
7014 | return std::move(Err); | ||||||||||
7015 | |||||||||||
7016 | return new (Importer.getToContext()) ArrayTypeTraitExpr( | ||||||||||
7017 | ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(), | ||||||||||
7018 | ToDimensionExpression, ToEndLoc, ToType); | ||||||||||
7019 | } | ||||||||||
7020 | |||||||||||
7021 | ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { | ||||||||||
7022 | Error Err = Error::success(); | ||||||||||
7023 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | ||||||||||
7024 | auto ToQueriedExpression = importChecked(Err, E->getQueriedExpression()); | ||||||||||
7025 | auto ToEndLoc = importChecked(Err, E->getEndLoc()); | ||||||||||
7026 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7027 | if (Err) | ||||||||||
7028 | return std::move(Err); | ||||||||||
7029 | |||||||||||
7030 | return new (Importer.getToContext()) ExpressionTraitExpr( | ||||||||||
7031 | ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(), | ||||||||||
7032 | ToEndLoc, ToType); | ||||||||||
7033 | } | ||||||||||
7034 | |||||||||||
7035 | ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { | ||||||||||
7036 | Error Err = Error::success(); | ||||||||||
7037 | auto ToLocation = importChecked(Err, E->getLocation()); | ||||||||||
7038 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7039 | auto ToSourceExpr = importChecked(Err, E->getSourceExpr()); | ||||||||||
7040 | if (Err) | ||||||||||
7041 | return std::move(Err); | ||||||||||
7042 | |||||||||||
7043 | return new (Importer.getToContext()) OpaqueValueExpr( | ||||||||||
7044 | ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr); | ||||||||||
7045 | } | ||||||||||
7046 | |||||||||||
7047 | ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { | ||||||||||
7048 | Error Err = Error::success(); | ||||||||||
7049 | auto ToLHS = importChecked(Err, E->getLHS()); | ||||||||||
7050 | auto ToRHS = importChecked(Err, E->getRHS()); | ||||||||||
7051 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7052 | auto ToRBracketLoc = importChecked(Err, E->getRBracketLoc()); | ||||||||||
7053 | if (Err) | ||||||||||
7054 | return std::move(Err); | ||||||||||
7055 | |||||||||||
7056 | return new (Importer.getToContext()) ArraySubscriptExpr( | ||||||||||
7057 | ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(), | ||||||||||
7058 | ToRBracketLoc); | ||||||||||
7059 | } | ||||||||||
7060 | |||||||||||
7061 | ExpectedStmt | ||||||||||
7062 | ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { | ||||||||||
7063 | Error Err = Error::success(); | ||||||||||
7064 | auto ToLHS = importChecked(Err, E->getLHS()); | ||||||||||
7065 | auto ToRHS = importChecked(Err, E->getRHS()); | ||||||||||
7066 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7067 | auto ToComputationLHSType = importChecked(Err, E->getComputationLHSType()); | ||||||||||
7068 | auto ToComputationResultType = | ||||||||||
7069 | importChecked(Err, E->getComputationResultType()); | ||||||||||
7070 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
7071 | if (Err) | ||||||||||
7072 | return std::move(Err); | ||||||||||
7073 | |||||||||||
7074 | return CompoundAssignOperator::Create( | ||||||||||
7075 | Importer.getToContext(), ToLHS, ToRHS, E->getOpcode(), ToType, | ||||||||||
7076 | E->getValueKind(), E->getObjectKind(), ToOperatorLoc, | ||||||||||
7077 | E->getFPFeatures(Importer.getFromContext().getLangOpts()), | ||||||||||
7078 | ToComputationLHSType, ToComputationResultType); | ||||||||||
7079 | } | ||||||||||
7080 | |||||||||||
7081 | Expected<CXXCastPath> | ||||||||||
7082 | ASTNodeImporter::ImportCastPath(CastExpr *CE) { | ||||||||||
7083 | CXXCastPath Path; | ||||||||||
7084 | for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) { | ||||||||||
7085 | if (auto SpecOrErr = import(*I)) | ||||||||||
7086 | Path.push_back(*SpecOrErr); | ||||||||||
7087 | else | ||||||||||
7088 | return SpecOrErr.takeError(); | ||||||||||
7089 | } | ||||||||||
7090 | return Path; | ||||||||||
7091 | } | ||||||||||
7092 | |||||||||||
7093 | ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { | ||||||||||
7094 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
7095 | if (!ToTypeOrErr) | ||||||||||
7096 | return ToTypeOrErr.takeError(); | ||||||||||
7097 | |||||||||||
7098 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); | ||||||||||
7099 | if (!ToSubExprOrErr) | ||||||||||
7100 | return ToSubExprOrErr.takeError(); | ||||||||||
7101 | |||||||||||
7102 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E); | ||||||||||
7103 | if (!ToBasePathOrErr) | ||||||||||
7104 | return ToBasePathOrErr.takeError(); | ||||||||||
7105 | |||||||||||
7106 | return ImplicitCastExpr::Create( | ||||||||||
7107 | Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr, | ||||||||||
7108 | &(*ToBasePathOrErr), E->getValueKind(), E->getFPFeatures()); | ||||||||||
7109 | } | ||||||||||
7110 | |||||||||||
7111 | ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) { | ||||||||||
7112 | Error Err = Error::success(); | ||||||||||
7113 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7114 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | ||||||||||
7115 | auto ToTypeInfoAsWritten = importChecked(Err, E->getTypeInfoAsWritten()); | ||||||||||
7116 | if (Err) | ||||||||||
7117 | return std::move(Err); | ||||||||||
7118 | |||||||||||
7119 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E); | ||||||||||
7120 | if (!ToBasePathOrErr) | ||||||||||
7121 | return ToBasePathOrErr.takeError(); | ||||||||||
7122 | CXXCastPath *ToBasePath = &(*ToBasePathOrErr); | ||||||||||
7123 | |||||||||||
7124 | switch (E->getStmtClass()) { | ||||||||||
7125 | case Stmt::CStyleCastExprClass: { | ||||||||||
7126 | auto *CCE = cast<CStyleCastExpr>(E); | ||||||||||
7127 | ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc()); | ||||||||||
7128 | if (!ToLParenLocOrErr) | ||||||||||
7129 | return ToLParenLocOrErr.takeError(); | ||||||||||
7130 | ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc()); | ||||||||||
7131 | if (!ToRParenLocOrErr) | ||||||||||
7132 | return ToRParenLocOrErr.takeError(); | ||||||||||
7133 | return CStyleCastExpr::Create( | ||||||||||
7134 | Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(), | ||||||||||
7135 | ToSubExpr, ToBasePath, CCE->getFPFeatures(), ToTypeInfoAsWritten, | ||||||||||
7136 | *ToLParenLocOrErr, *ToRParenLocOrErr); | ||||||||||
7137 | } | ||||||||||
7138 | |||||||||||
7139 | case Stmt::CXXFunctionalCastExprClass: { | ||||||||||
7140 | auto *FCE = cast<CXXFunctionalCastExpr>(E); | ||||||||||
7141 | ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc()); | ||||||||||
7142 | if (!ToLParenLocOrErr) | ||||||||||
7143 | return ToLParenLocOrErr.takeError(); | ||||||||||
7144 | ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc()); | ||||||||||
7145 | if (!ToRParenLocOrErr) | ||||||||||
7146 | return ToRParenLocOrErr.takeError(); | ||||||||||
7147 | return CXXFunctionalCastExpr::Create( | ||||||||||
7148 | Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten, | ||||||||||
7149 | E->getCastKind(), ToSubExpr, ToBasePath, FCE->getFPFeatures(), | ||||||||||
7150 | *ToLParenLocOrErr, *ToRParenLocOrErr); | ||||||||||
7151 | } | ||||||||||
7152 | |||||||||||
7153 | case Stmt::ObjCBridgedCastExprClass: { | ||||||||||
7154 | auto *OCE = cast<ObjCBridgedCastExpr>(E); | ||||||||||
7155 | ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc()); | ||||||||||
7156 | if (!ToLParenLocOrErr) | ||||||||||
7157 | return ToLParenLocOrErr.takeError(); | ||||||||||
7158 | ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc()); | ||||||||||
7159 | if (!ToBridgeKeywordLocOrErr) | ||||||||||
7160 | return ToBridgeKeywordLocOrErr.takeError(); | ||||||||||
7161 | return new (Importer.getToContext()) ObjCBridgedCastExpr( | ||||||||||
7162 | *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(), | ||||||||||
7163 | *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr); | ||||||||||
7164 | } | ||||||||||
7165 | default: | ||||||||||
7166 | llvm_unreachable("Cast expression of unsupported type!")::llvm::llvm_unreachable_internal("Cast expression of unsupported type!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 7166); | ||||||||||
7167 | return make_error<ImportError>(ImportError::UnsupportedConstruct); | ||||||||||
7168 | } | ||||||||||
7169 | } | ||||||||||
7170 | |||||||||||
7171 | ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) { | ||||||||||
7172 | SmallVector<OffsetOfNode, 4> ToNodes; | ||||||||||
7173 | for (int I = 0, N = E->getNumComponents(); I < N; ++I) { | ||||||||||
7174 | const OffsetOfNode &FromNode = E->getComponent(I); | ||||||||||
7175 | |||||||||||
7176 | SourceLocation ToBeginLoc, ToEndLoc; | ||||||||||
7177 | |||||||||||
7178 | if (FromNode.getKind() != OffsetOfNode::Base) { | ||||||||||
7179 | Error Err = Error::success(); | ||||||||||
7180 | ToBeginLoc = importChecked(Err, FromNode.getBeginLoc()); | ||||||||||
7181 | ToEndLoc = importChecked(Err, FromNode.getEndLoc()); | ||||||||||
7182 | if (Err) | ||||||||||
7183 | return std::move(Err); | ||||||||||
7184 | } | ||||||||||
7185 | |||||||||||
7186 | switch (FromNode.getKind()) { | ||||||||||
7187 | case OffsetOfNode::Array: | ||||||||||
7188 | ToNodes.push_back( | ||||||||||
7189 | OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc)); | ||||||||||
7190 | break; | ||||||||||
7191 | case OffsetOfNode::Base: { | ||||||||||
7192 | auto ToBSOrErr = import(FromNode.getBase()); | ||||||||||
7193 | if (!ToBSOrErr) | ||||||||||
7194 | return ToBSOrErr.takeError(); | ||||||||||
7195 | ToNodes.push_back(OffsetOfNode(*ToBSOrErr)); | ||||||||||
7196 | break; | ||||||||||
7197 | } | ||||||||||
7198 | case OffsetOfNode::Field: { | ||||||||||
7199 | auto ToFieldOrErr = import(FromNode.getField()); | ||||||||||
7200 | if (!ToFieldOrErr) | ||||||||||
7201 | return ToFieldOrErr.takeError(); | ||||||||||
7202 | ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc)); | ||||||||||
7203 | break; | ||||||||||
7204 | } | ||||||||||
7205 | case OffsetOfNode::Identifier: { | ||||||||||
7206 | IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName()); | ||||||||||
7207 | ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc)); | ||||||||||
7208 | break; | ||||||||||
7209 | } | ||||||||||
7210 | } | ||||||||||
7211 | } | ||||||||||
7212 | |||||||||||
7213 | SmallVector<Expr *, 4> ToExprs(E->getNumExpressions()); | ||||||||||
7214 | for (int I = 0, N = E->getNumExpressions(); I < N; ++I) { | ||||||||||
7215 | ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I)); | ||||||||||
7216 | if (!ToIndexExprOrErr) | ||||||||||
7217 | return ToIndexExprOrErr.takeError(); | ||||||||||
7218 | ToExprs[I] = *ToIndexExprOrErr; | ||||||||||
7219 | } | ||||||||||
7220 | |||||||||||
7221 | Error Err = Error::success(); | ||||||||||
7222 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7223 | auto ToTypeSourceInfo = importChecked(Err, E->getTypeSourceInfo()); | ||||||||||
7224 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
7225 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
7226 | if (Err) | ||||||||||
7227 | return std::move(Err); | ||||||||||
7228 | |||||||||||
7229 | return OffsetOfExpr::Create( | ||||||||||
7230 | Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes, | ||||||||||
7231 | ToExprs, ToRParenLoc); | ||||||||||
7232 | } | ||||||||||
7233 | |||||||||||
7234 | ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { | ||||||||||
7235 | Error Err = Error::success(); | ||||||||||
7236 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7237 | auto ToOperand = importChecked(Err, E->getOperand()); | ||||||||||
7238 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | ||||||||||
7239 | auto ToEndLoc = importChecked(Err, E->getEndLoc()); | ||||||||||
7240 | if (Err) | ||||||||||
7241 | return std::move(Err); | ||||||||||
7242 | |||||||||||
7243 | CanThrowResult ToCanThrow; | ||||||||||
7244 | if (E->isValueDependent()) | ||||||||||
7245 | ToCanThrow = CT_Dependent; | ||||||||||
7246 | else | ||||||||||
7247 | ToCanThrow = E->getValue() ? CT_Can : CT_Cannot; | ||||||||||
7248 | |||||||||||
7249 | return new (Importer.getToContext()) CXXNoexceptExpr( | ||||||||||
7250 | ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc); | ||||||||||
7251 | } | ||||||||||
7252 | |||||||||||
7253 | ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) { | ||||||||||
7254 | Error Err = Error::success(); | ||||||||||
7255 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | ||||||||||
7256 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7257 | auto ToThrowLoc = importChecked(Err, E->getThrowLoc()); | ||||||||||
7258 | if (Err) | ||||||||||
7259 | return std::move(Err); | ||||||||||
7260 | |||||||||||
7261 | return new (Importer.getToContext()) CXXThrowExpr( | ||||||||||
7262 | ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope()); | ||||||||||
7263 | } | ||||||||||
7264 | |||||||||||
7265 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { | ||||||||||
7266 | ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation()); | ||||||||||
7267 | if (!ToUsedLocOrErr) | ||||||||||
7268 | return ToUsedLocOrErr.takeError(); | ||||||||||
7269 | |||||||||||
7270 | auto ToParamOrErr = import(E->getParam()); | ||||||||||
7271 | if (!ToParamOrErr) | ||||||||||
7272 | return ToParamOrErr.takeError(); | ||||||||||
7273 | |||||||||||
7274 | auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext()); | ||||||||||
7275 | if (!UsedContextOrErr) | ||||||||||
7276 | return UsedContextOrErr.takeError(); | ||||||||||
7277 | |||||||||||
7278 | // Import the default arg if it was not imported yet. | ||||||||||
7279 | // This is needed because it can happen that during the import of the | ||||||||||
7280 | // default expression (from VisitParmVarDecl) the same ParmVarDecl is | ||||||||||
7281 | // encountered here. The default argument for a ParmVarDecl is set in the | ||||||||||
7282 | // ParmVarDecl only after it is imported (set in VisitParmVarDecl if not here, | ||||||||||
7283 | // see VisitParmVarDecl). | ||||||||||
7284 | ParmVarDecl *ToParam = *ToParamOrErr; | ||||||||||
7285 | if (!ToParam->getDefaultArg()) { | ||||||||||
7286 | Optional<ParmVarDecl *> FromParam = Importer.getImportedFromDecl(ToParam); | ||||||||||
7287 | assert(FromParam && "ParmVarDecl was not imported?")((FromParam && "ParmVarDecl was not imported?") ? static_cast <void> (0) : __assert_fail ("FromParam && \"ParmVarDecl was not imported?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 7287, __PRETTY_FUNCTION__)); | ||||||||||
7288 | |||||||||||
7289 | if (Error Err = ImportDefaultArgOfParmVarDecl(*FromParam, ToParam)) | ||||||||||
7290 | return std::move(Err); | ||||||||||
7291 | } | ||||||||||
7292 | |||||||||||
7293 | return CXXDefaultArgExpr::Create(Importer.getToContext(), *ToUsedLocOrErr, | ||||||||||
7294 | *ToParamOrErr, *UsedContextOrErr); | ||||||||||
7295 | } | ||||||||||
7296 | |||||||||||
7297 | ExpectedStmt | ||||||||||
7298 | ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { | ||||||||||
7299 | Error Err = Error::success(); | ||||||||||
7300 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7301 | auto ToTypeSourceInfo = importChecked(Err, E->getTypeSourceInfo()); | ||||||||||
7302 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
7303 | if (Err) | ||||||||||
7304 | return std::move(Err); | ||||||||||
7305 | |||||||||||
7306 | return new (Importer.getToContext()) CXXScalarValueInitExpr( | ||||||||||
7307 | ToType, ToTypeSourceInfo, ToRParenLoc); | ||||||||||
7308 | } | ||||||||||
7309 | |||||||||||
7310 | ExpectedStmt | ||||||||||
7311 | ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { | ||||||||||
7312 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); | ||||||||||
7313 | if (!ToSubExprOrErr) | ||||||||||
7314 | return ToSubExprOrErr.takeError(); | ||||||||||
7315 | |||||||||||
7316 | auto ToDtorOrErr = import(E->getTemporary()->getDestructor()); | ||||||||||
7317 | if (!ToDtorOrErr) | ||||||||||
7318 | return ToDtorOrErr.takeError(); | ||||||||||
7319 | |||||||||||
7320 | ASTContext &ToCtx = Importer.getToContext(); | ||||||||||
7321 | CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr); | ||||||||||
7322 | return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr); | ||||||||||
7323 | } | ||||||||||
7324 | |||||||||||
7325 | ExpectedStmt | ||||||||||
7326 | |||||||||||
7327 | ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { | ||||||||||
7328 | Error Err = Error::success(); | ||||||||||
7329 | auto ToConstructor = importChecked(Err, E->getConstructor()); | ||||||||||
7330 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7331 | auto ToTypeSourceInfo = importChecked(Err, E->getTypeSourceInfo()); | ||||||||||
7332 | auto ToParenOrBraceRange = importChecked(Err, E->getParenOrBraceRange()); | ||||||||||
7333 | if (Err) | ||||||||||
7334 | return std::move(Err); | ||||||||||
7335 | |||||||||||
7336 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); | ||||||||||
7337 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) | ||||||||||
7338 | return std::move(Err); | ||||||||||
7339 | |||||||||||
7340 | return CXXTemporaryObjectExpr::Create( | ||||||||||
7341 | Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs, | ||||||||||
7342 | ToParenOrBraceRange, E->hadMultipleCandidates(), | ||||||||||
7343 | E->isListInitialization(), E->isStdInitListInitialization(), | ||||||||||
7344 | E->requiresZeroInitialization()); | ||||||||||
7345 | } | ||||||||||
7346 | |||||||||||
7347 | ExpectedDecl ASTNodeImporter::VisitLifetimeExtendedTemporaryDecl( | ||||||||||
7348 | LifetimeExtendedTemporaryDecl *D) { | ||||||||||
7349 | DeclContext *DC, *LexicalDC; | ||||||||||
7350 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | ||||||||||
7351 | return std::move(Err); | ||||||||||
7352 | |||||||||||
7353 | Error Err = Error::success(); | ||||||||||
7354 | auto Temporary = importChecked(Err, D->getTemporaryExpr()); | ||||||||||
7355 | auto ExtendingDecl = importChecked(Err, D->getExtendingDecl()); | ||||||||||
7356 | if (Err) | ||||||||||
7357 | return std::move(Err); | ||||||||||
7358 | // FIXME: Should ManglingNumber get numbers associated with 'to' context? | ||||||||||
7359 | |||||||||||
7360 | LifetimeExtendedTemporaryDecl *To; | ||||||||||
7361 | if (GetImportedOrCreateDecl(To, D, Temporary, ExtendingDecl, | ||||||||||
7362 | D->getManglingNumber())) | ||||||||||
7363 | return To; | ||||||||||
7364 | |||||||||||
7365 | To->setLexicalDeclContext(LexicalDC); | ||||||||||
7366 | LexicalDC->addDeclInternal(To); | ||||||||||
7367 | return To; | ||||||||||
7368 | } | ||||||||||
7369 | |||||||||||
7370 | ExpectedStmt | ||||||||||
7371 | ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { | ||||||||||
7372 | Error Err = Error::success(); | ||||||||||
7373 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7374 | Expr *ToTemporaryExpr = importChecked( | ||||||||||
7375 | Err, E->getLifetimeExtendedTemporaryDecl() ? nullptr : E->getSubExpr()); | ||||||||||
7376 | auto ToMaterializedDecl = | ||||||||||
7377 | importChecked(Err, E->getLifetimeExtendedTemporaryDecl()); | ||||||||||
7378 | if (Err) | ||||||||||
7379 | return std::move(Err); | ||||||||||
7380 | |||||||||||
7381 | if (!ToTemporaryExpr) | ||||||||||
7382 | ToTemporaryExpr = cast<Expr>(ToMaterializedDecl->getTemporaryExpr()); | ||||||||||
7383 | |||||||||||
7384 | auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr( | ||||||||||
7385 | ToType, ToTemporaryExpr, E->isBoundToLvalueReference(), | ||||||||||
7386 | ToMaterializedDecl); | ||||||||||
7387 | |||||||||||
7388 | return ToMTE; | ||||||||||
7389 | } | ||||||||||
7390 | |||||||||||
7391 | ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) { | ||||||||||
7392 | Error Err = Error::success(); | ||||||||||
7393 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7394 | auto ToPattern = importChecked(Err, E->getPattern()); | ||||||||||
7395 | auto ToEllipsisLoc = importChecked(Err, E->getEllipsisLoc()); | ||||||||||
7396 | if (Err) | ||||||||||
7397 | return std::move(Err); | ||||||||||
7398 | |||||||||||
7399 | return new (Importer.getToContext()) PackExpansionExpr( | ||||||||||
7400 | ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions()); | ||||||||||
7401 | } | ||||||||||
7402 | |||||||||||
7403 | ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { | ||||||||||
7404 | Error Err = Error::success(); | ||||||||||
7405 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
7406 | auto ToPack = importChecked(Err, E->getPack()); | ||||||||||
7407 | auto ToPackLoc = importChecked(Err, E->getPackLoc()); | ||||||||||
7408 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
7409 | if (Err) | ||||||||||
7410 | return std::move(Err); | ||||||||||
7411 | |||||||||||
7412 | Optional<unsigned> Length; | ||||||||||
7413 | if (!E->isValueDependent()) | ||||||||||
7414 | Length = E->getPackLength(); | ||||||||||
7415 | |||||||||||
7416 | SmallVector<TemplateArgument, 8> ToPartialArguments; | ||||||||||
7417 | if (E->isPartiallySubstituted()) { | ||||||||||
7418 | if (Error Err = ImportTemplateArguments( | ||||||||||
7419 | E->getPartialArguments().data(), | ||||||||||
7420 | E->getPartialArguments().size(), | ||||||||||
7421 | ToPartialArguments)) | ||||||||||
7422 | return std::move(Err); | ||||||||||
7423 | } | ||||||||||
7424 | |||||||||||
7425 | return SizeOfPackExpr::Create( | ||||||||||
7426 | Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc, | ||||||||||
7427 | Length, ToPartialArguments); | ||||||||||
7428 | } | ||||||||||
7429 | |||||||||||
7430 | |||||||||||
7431 | ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) { | ||||||||||
7432 | Error Err = Error::success(); | ||||||||||
7433 | auto ToOperatorNew = importChecked(Err, E->getOperatorNew()); | ||||||||||
7434 | auto ToOperatorDelete = importChecked(Err, E->getOperatorDelete()); | ||||||||||
7435 | auto ToTypeIdParens = importChecked(Err, E->getTypeIdParens()); | ||||||||||
7436 | auto ToArraySize = importChecked(Err, E->getArraySize()); | ||||||||||
7437 | auto ToInitializer = importChecked(Err, E->getInitializer()); | ||||||||||
7438 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7439 | auto ToAllocatedTypeSourceInfo = | ||||||||||
7440 | importChecked(Err, E->getAllocatedTypeSourceInfo()); | ||||||||||
7441 | auto ToSourceRange = importChecked(Err, E->getSourceRange()); | ||||||||||
7442 | auto ToDirectInitRange = importChecked(Err, E->getDirectInitRange()); | ||||||||||
7443 | if (Err) | ||||||||||
7444 | return std::move(Err); | ||||||||||
7445 | |||||||||||
7446 | SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs()); | ||||||||||
7447 | if (Error Err = | ||||||||||
7448 | ImportContainerChecked(E->placement_arguments(), ToPlacementArgs)) | ||||||||||
7449 | return std::move(Err); | ||||||||||
7450 | |||||||||||
7451 | return CXXNewExpr::Create( | ||||||||||
7452 | Importer.getToContext(), E->isGlobalNew(), ToOperatorNew, | ||||||||||
7453 | ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(), | ||||||||||
7454 | ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(), | ||||||||||
7455 | ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange, | ||||||||||
7456 | ToDirectInitRange); | ||||||||||
7457 | } | ||||||||||
7458 | |||||||||||
7459 | ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { | ||||||||||
7460 | Error Err = Error::success(); | ||||||||||
7461 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7462 | auto ToOperatorDelete = importChecked(Err, E->getOperatorDelete()); | ||||||||||
7463 | auto ToArgument = importChecked(Err, E->getArgument()); | ||||||||||
7464 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | ||||||||||
7465 | if (Err) | ||||||||||
7466 | return std::move(Err); | ||||||||||
7467 | |||||||||||
7468 | return new (Importer.getToContext()) CXXDeleteExpr( | ||||||||||
7469 | ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(), | ||||||||||
7470 | E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument, | ||||||||||
7471 | ToBeginLoc); | ||||||||||
7472 | } | ||||||||||
7473 | |||||||||||
7474 | ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { | ||||||||||
7475 | Error Err = Error::success(); | ||||||||||
7476 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7477 | auto ToLocation = importChecked(Err, E->getLocation()); | ||||||||||
7478 | auto ToConstructor = importChecked(Err, E->getConstructor()); | ||||||||||
7479 | auto ToParenOrBraceRange = importChecked(Err, E->getParenOrBraceRange()); | ||||||||||
7480 | if (Err) | ||||||||||
7481 | return std::move(Err); | ||||||||||
7482 | |||||||||||
7483 | SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); | ||||||||||
7484 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) | ||||||||||
7485 | return std::move(Err); | ||||||||||
7486 | |||||||||||
7487 | return CXXConstructExpr::Create( | ||||||||||
7488 | Importer.getToContext(), ToType, ToLocation, ToConstructor, | ||||||||||
7489 | E->isElidable(), ToArgs, E->hadMultipleCandidates(), | ||||||||||
7490 | E->isListInitialization(), E->isStdInitListInitialization(), | ||||||||||
7491 | E->requiresZeroInitialization(), E->getConstructionKind(), | ||||||||||
7492 | ToParenOrBraceRange); | ||||||||||
7493 | } | ||||||||||
7494 | |||||||||||
7495 | ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) { | ||||||||||
7496 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); | ||||||||||
7497 | if (!ToSubExprOrErr) | ||||||||||
7498 | return ToSubExprOrErr.takeError(); | ||||||||||
7499 | |||||||||||
7500 | SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects()); | ||||||||||
7501 | if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects)) | ||||||||||
7502 | return std::move(Err); | ||||||||||
7503 | |||||||||||
7504 | return ExprWithCleanups::Create( | ||||||||||
7505 | Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(), | ||||||||||
7506 | ToObjects); | ||||||||||
7507 | } | ||||||||||
7508 | |||||||||||
7509 | ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { | ||||||||||
7510 | Error Err = Error::success(); | ||||||||||
7511 | auto ToCallee = importChecked(Err, E->getCallee()); | ||||||||||
7512 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7513 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
7514 | if (Err) | ||||||||||
7515 | return std::move(Err); | ||||||||||
7516 | |||||||||||
7517 | SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); | ||||||||||
7518 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) | ||||||||||
7519 | return std::move(Err); | ||||||||||
7520 | |||||||||||
7521 | return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, | ||||||||||
7522 | ToType, E->getValueKind(), ToRParenLoc, | ||||||||||
7523 | E->getFPFeatures()); | ||||||||||
7524 | } | ||||||||||
7525 | |||||||||||
7526 | ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { | ||||||||||
7527 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
7528 | if (!ToTypeOrErr) | ||||||||||
7529 | return ToTypeOrErr.takeError(); | ||||||||||
7530 | |||||||||||
7531 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | ||||||||||
7532 | if (!ToLocationOrErr) | ||||||||||
7533 | return ToLocationOrErr.takeError(); | ||||||||||
7534 | |||||||||||
7535 | return new (Importer.getToContext()) CXXThisExpr( | ||||||||||
7536 | *ToLocationOrErr, *ToTypeOrErr, E->isImplicit()); | ||||||||||
7537 | } | ||||||||||
7538 | |||||||||||
7539 | ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { | ||||||||||
7540 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
7541 | if (!ToTypeOrErr) | ||||||||||
7542 | return ToTypeOrErr.takeError(); | ||||||||||
7543 | |||||||||||
7544 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | ||||||||||
7545 | if (!ToLocationOrErr) | ||||||||||
7546 | return ToLocationOrErr.takeError(); | ||||||||||
7547 | |||||||||||
7548 | return new (Importer.getToContext()) CXXBoolLiteralExpr( | ||||||||||
7549 | E->getValue(), *ToTypeOrErr, *ToLocationOrErr); | ||||||||||
7550 | } | ||||||||||
7551 | |||||||||||
7552 | ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { | ||||||||||
7553 | Error Err = Error::success(); | ||||||||||
7554 | auto ToBase = importChecked(Err, E->getBase()); | ||||||||||
7555 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
7556 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | ||||||||||
7557 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); | ||||||||||
7558 | auto ToMemberDecl = importChecked(Err, E->getMemberDecl()); | ||||||||||
7559 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7560 | auto ToDecl = importChecked(Err, E->getFoundDecl().getDecl()); | ||||||||||
7561 | auto ToName = importChecked(Err, E->getMemberNameInfo().getName()); | ||||||||||
7562 | auto ToLoc = importChecked(Err, E->getMemberNameInfo().getLoc()); | ||||||||||
7563 | if (Err) | ||||||||||
7564 | return std::move(Err); | ||||||||||
7565 | |||||||||||
7566 | DeclAccessPair ToFoundDecl = | ||||||||||
7567 | DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess()); | ||||||||||
7568 | |||||||||||
7569 | DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc); | ||||||||||
7570 | |||||||||||
7571 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; | ||||||||||
7572 | if (E->hasExplicitTemplateArgs()) { | ||||||||||
7573 | if (Error Err = | ||||||||||
7574 | ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(), | ||||||||||
7575 | E->template_arguments(), ToTAInfo)) | ||||||||||
7576 | return std::move(Err); | ||||||||||
7577 | ResInfo = &ToTAInfo; | ||||||||||
7578 | } | ||||||||||
7579 | |||||||||||
7580 | return MemberExpr::Create(Importer.getToContext(), ToBase, E->isArrow(), | ||||||||||
7581 | ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, | ||||||||||
7582 | ToMemberDecl, ToFoundDecl, ToMemberNameInfo, | ||||||||||
7583 | ResInfo, ToType, E->getValueKind(), | ||||||||||
7584 | E->getObjectKind(), E->isNonOdrUse()); | ||||||||||
7585 | } | ||||||||||
7586 | |||||||||||
7587 | ExpectedStmt | ||||||||||
7588 | ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { | ||||||||||
7589 | Error Err = Error::success(); | ||||||||||
7590 | auto ToBase = importChecked(Err, E->getBase()); | ||||||||||
7591 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
7592 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | ||||||||||
7593 | auto ToScopeTypeInfo = importChecked(Err, E->getScopeTypeInfo()); | ||||||||||
7594 | auto ToColonColonLoc = importChecked(Err, E->getColonColonLoc()); | ||||||||||
7595 | auto ToTildeLoc = importChecked(Err, E->getTildeLoc()); | ||||||||||
7596 | if (Err) | ||||||||||
7597 | return std::move(Err); | ||||||||||
7598 | |||||||||||
7599 | PseudoDestructorTypeStorage Storage; | ||||||||||
7600 | if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) { | ||||||||||
7601 | IdentifierInfo *ToII = Importer.Import(FromII); | ||||||||||
7602 | ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc()); | ||||||||||
7603 | if (!ToDestroyedTypeLocOrErr) | ||||||||||
7604 | return ToDestroyedTypeLocOrErr.takeError(); | ||||||||||
7605 | Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr); | ||||||||||
7606 | } else { | ||||||||||
7607 | if (auto ToTIOrErr = import(E->getDestroyedTypeInfo())) | ||||||||||
7608 | Storage = PseudoDestructorTypeStorage(*ToTIOrErr); | ||||||||||
7609 | else | ||||||||||
7610 | return ToTIOrErr.takeError(); | ||||||||||
7611 | } | ||||||||||
7612 | |||||||||||
7613 | return new (Importer.getToContext()) CXXPseudoDestructorExpr( | ||||||||||
7614 | Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc, | ||||||||||
7615 | ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage); | ||||||||||
7616 | } | ||||||||||
7617 | |||||||||||
7618 | ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr( | ||||||||||
7619 | CXXDependentScopeMemberExpr *E) { | ||||||||||
7620 | Error Err = Error::success(); | ||||||||||
7621 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7622 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
7623 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | ||||||||||
7624 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); | ||||||||||
7625 | auto ToFirstQualifierFoundInScope = | ||||||||||
7626 | importChecked(Err, E->getFirstQualifierFoundInScope()); | ||||||||||
7627 | if (Err) | ||||||||||
7628 | return std::move(Err); | ||||||||||
7629 | |||||||||||
7630 | Expr *ToBase = nullptr; | ||||||||||
7631 | if (!E->isImplicitAccess()) { | ||||||||||
7632 | if (ExpectedExpr ToBaseOrErr = import(E->getBase())) | ||||||||||
7633 | ToBase = *ToBaseOrErr; | ||||||||||
7634 | else | ||||||||||
7635 | return ToBaseOrErr.takeError(); | ||||||||||
7636 | } | ||||||||||
7637 | |||||||||||
7638 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; | ||||||||||
7639 | |||||||||||
7640 | if (E->hasExplicitTemplateArgs()) { | ||||||||||
7641 | if (Error Err = | ||||||||||
7642 | ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(), | ||||||||||
7643 | E->template_arguments(), ToTAInfo)) | ||||||||||
7644 | return std::move(Err); | ||||||||||
7645 | ResInfo = &ToTAInfo; | ||||||||||
7646 | } | ||||||||||
7647 | auto ToMember = importChecked(Err, E->getMember()); | ||||||||||
7648 | auto ToMemberLoc = importChecked(Err, E->getMemberLoc()); | ||||||||||
7649 | if (Err) | ||||||||||
7650 | return std::move(Err); | ||||||||||
7651 | DeclarationNameInfo ToMemberNameInfo(ToMember, ToMemberLoc); | ||||||||||
7652 | |||||||||||
7653 | // Import additional name location/type info. | ||||||||||
7654 | if (Error Err = | ||||||||||
7655 | ImportDeclarationNameLoc(E->getMemberNameInfo(), ToMemberNameInfo)) | ||||||||||
7656 | return std::move(Err); | ||||||||||
7657 | |||||||||||
7658 | return CXXDependentScopeMemberExpr::Create( | ||||||||||
7659 | Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc, | ||||||||||
7660 | ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope, | ||||||||||
7661 | ToMemberNameInfo, ResInfo); | ||||||||||
7662 | } | ||||||||||
7663 | |||||||||||
7664 | ExpectedStmt | ||||||||||
7665 | ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { | ||||||||||
7666 | Error Err = Error::success(); | ||||||||||
7667 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | ||||||||||
7668 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); | ||||||||||
7669 | auto ToDeclName = importChecked(Err, E->getDeclName()); | ||||||||||
7670 | auto ToNameLoc = importChecked(Err, E->getNameInfo().getLoc()); | ||||||||||
7671 | auto ToLAngleLoc = importChecked(Err, E->getLAngleLoc()); | ||||||||||
7672 | auto ToRAngleLoc = importChecked(Err, E->getRAngleLoc()); | ||||||||||
7673 | if (Err) | ||||||||||
7674 | return std::move(Err); | ||||||||||
7675 | |||||||||||
7676 | DeclarationNameInfo ToNameInfo(ToDeclName, ToNameLoc); | ||||||||||
7677 | if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo)) | ||||||||||
7678 | return std::move(Err); | ||||||||||
7679 | |||||||||||
7680 | TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc); | ||||||||||
7681 | TemplateArgumentListInfo *ResInfo = nullptr; | ||||||||||
7682 | if (E->hasExplicitTemplateArgs()) { | ||||||||||
7683 | if (Error Err = | ||||||||||
7684 | ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo)) | ||||||||||
7685 | return std::move(Err); | ||||||||||
7686 | ResInfo = &ToTAInfo; | ||||||||||
7687 | } | ||||||||||
7688 | |||||||||||
7689 | return DependentScopeDeclRefExpr::Create( | ||||||||||
7690 | Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, | ||||||||||
7691 | ToNameInfo, ResInfo); | ||||||||||
7692 | } | ||||||||||
7693 | |||||||||||
7694 | ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr( | ||||||||||
7695 | CXXUnresolvedConstructExpr *E) { | ||||||||||
7696 | Error Err = Error::success(); | ||||||||||
7697 | auto ToLParenLoc = importChecked(Err, E->getLParenLoc()); | ||||||||||
7698 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
7699 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7700 | auto ToTypeSourceInfo = importChecked(Err, E->getTypeSourceInfo()); | ||||||||||
7701 | if (Err) | ||||||||||
7702 | return std::move(Err); | ||||||||||
7703 | |||||||||||
7704 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); | ||||||||||
7705 | if (Error Err = | ||||||||||
7706 | ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin())) | ||||||||||
7707 | return std::move(Err); | ||||||||||
7708 | |||||||||||
7709 | return CXXUnresolvedConstructExpr::Create( | ||||||||||
7710 | Importer.getToContext(), ToType, ToTypeSourceInfo, ToLParenLoc, | ||||||||||
7711 | llvm::makeArrayRef(ToArgs), ToRParenLoc); | ||||||||||
7712 | } | ||||||||||
7713 | |||||||||||
7714 | ExpectedStmt | ||||||||||
7715 | ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) { | ||||||||||
7716 | Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass()); | ||||||||||
7717 | if (!ToNamingClassOrErr) | ||||||||||
7718 | return ToNamingClassOrErr.takeError(); | ||||||||||
7719 | |||||||||||
7720 | auto ToQualifierLocOrErr = import(E->getQualifierLoc()); | ||||||||||
7721 | if (!ToQualifierLocOrErr) | ||||||||||
7722 | return ToQualifierLocOrErr.takeError(); | ||||||||||
7723 | |||||||||||
7724 | Error Err = Error::success(); | ||||||||||
7725 | auto ToName = importChecked(Err, E->getName()); | ||||||||||
7726 | auto ToNameLoc = importChecked(Err, E->getNameLoc()); | ||||||||||
7727 | if (Err) | ||||||||||
7728 | return std::move(Err); | ||||||||||
7729 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); | ||||||||||
7730 | |||||||||||
7731 | // Import additional name location/type info. | ||||||||||
7732 | if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo)) | ||||||||||
7733 | return std::move(Err); | ||||||||||
7734 | |||||||||||
7735 | UnresolvedSet<8> ToDecls; | ||||||||||
7736 | for (auto *D : E->decls()) | ||||||||||
7737 | if (auto ToDOrErr = import(D)) | ||||||||||
7738 | ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr)); | ||||||||||
7739 | else | ||||||||||
7740 | return ToDOrErr.takeError(); | ||||||||||
7741 | |||||||||||
7742 | if (E->hasExplicitTemplateArgs()) { | ||||||||||
7743 | TemplateArgumentListInfo ToTAInfo; | ||||||||||
7744 | if (Error Err = ImportTemplateArgumentListInfo( | ||||||||||
7745 | E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(), | ||||||||||
7746 | ToTAInfo)) | ||||||||||
7747 | return std::move(Err); | ||||||||||
7748 | |||||||||||
7749 | ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc()); | ||||||||||
7750 | if (!ToTemplateKeywordLocOrErr) | ||||||||||
7751 | return ToTemplateKeywordLocOrErr.takeError(); | ||||||||||
7752 | |||||||||||
7753 | return UnresolvedLookupExpr::Create( | ||||||||||
7754 | Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr, | ||||||||||
7755 | *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo, | ||||||||||
7756 | ToDecls.begin(), ToDecls.end()); | ||||||||||
7757 | } | ||||||||||
7758 | |||||||||||
7759 | return UnresolvedLookupExpr::Create( | ||||||||||
7760 | Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr, | ||||||||||
7761 | ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(), | ||||||||||
7762 | ToDecls.end()); | ||||||||||
7763 | } | ||||||||||
7764 | |||||||||||
7765 | ExpectedStmt | ||||||||||
7766 | ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { | ||||||||||
7767 | Error Err = Error::success(); | ||||||||||
7768 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7769 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
7770 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | ||||||||||
7771 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); | ||||||||||
7772 | auto ToName = importChecked(Err, E->getName()); | ||||||||||
7773 | auto ToNameLoc = importChecked(Err, E->getNameLoc()); | ||||||||||
7774 | if (Err) | ||||||||||
7775 | return std::move(Err); | ||||||||||
7776 | |||||||||||
7777 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); | ||||||||||
7778 | // Import additional name location/type info. | ||||||||||
7779 | if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo)) | ||||||||||
7780 | return std::move(Err); | ||||||||||
7781 | |||||||||||
7782 | UnresolvedSet<8> ToDecls; | ||||||||||
7783 | for (Decl *D : E->decls()) | ||||||||||
7784 | if (auto ToDOrErr = import(D)) | ||||||||||
7785 | ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr)); | ||||||||||
7786 | else | ||||||||||
7787 | return ToDOrErr.takeError(); | ||||||||||
7788 | |||||||||||
7789 | TemplateArgumentListInfo ToTAInfo; | ||||||||||
7790 | TemplateArgumentListInfo *ResInfo = nullptr; | ||||||||||
7791 | if (E->hasExplicitTemplateArgs()) { | ||||||||||
7792 | TemplateArgumentListInfo FromTAInfo; | ||||||||||
7793 | E->copyTemplateArgumentsInto(FromTAInfo); | ||||||||||
7794 | if (Error Err = ImportTemplateArgumentListInfo(FromTAInfo, ToTAInfo)) | ||||||||||
7795 | return std::move(Err); | ||||||||||
7796 | ResInfo = &ToTAInfo; | ||||||||||
7797 | } | ||||||||||
7798 | |||||||||||
7799 | Expr *ToBase = nullptr; | ||||||||||
7800 | if (!E->isImplicitAccess()) { | ||||||||||
7801 | if (ExpectedExpr ToBaseOrErr = import(E->getBase())) | ||||||||||
7802 | ToBase = *ToBaseOrErr; | ||||||||||
7803 | else | ||||||||||
7804 | return ToBaseOrErr.takeError(); | ||||||||||
7805 | } | ||||||||||
7806 | |||||||||||
7807 | return UnresolvedMemberExpr::Create( | ||||||||||
7808 | Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType, | ||||||||||
7809 | E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, | ||||||||||
7810 | ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end()); | ||||||||||
7811 | } | ||||||||||
7812 | |||||||||||
7813 | ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) { | ||||||||||
7814 | Error Err = Error::success(); | ||||||||||
7815 | auto ToCallee = importChecked(Err, E->getCallee()); | ||||||||||
7816 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7817 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
7818 | if (Err) | ||||||||||
7819 | return std::move(Err); | ||||||||||
7820 | |||||||||||
7821 | unsigned NumArgs = E->getNumArgs(); | ||||||||||
7822 | llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); | ||||||||||
7823 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) | ||||||||||
7824 | return std::move(Err); | ||||||||||
7825 | |||||||||||
7826 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) { | ||||||||||
7827 | return CXXOperatorCallExpr::Create( | ||||||||||
7828 | Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType, | ||||||||||
7829 | OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(), | ||||||||||
7830 | OCE->getADLCallKind()); | ||||||||||
7831 | } | ||||||||||
7832 | |||||||||||
7833 | return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType, | ||||||||||
7834 | E->getValueKind(), ToRParenLoc, E->getFPFeatures(), | ||||||||||
7835 | /*MinNumArgs=*/0, E->getADLCallKind()); | ||||||||||
7836 | } | ||||||||||
7837 | |||||||||||
7838 | ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) { | ||||||||||
7839 | CXXRecordDecl *FromClass = E->getLambdaClass(); | ||||||||||
7840 | auto ToClassOrErr = import(FromClass); | ||||||||||
7841 | if (!ToClassOrErr) | ||||||||||
7842 | return ToClassOrErr.takeError(); | ||||||||||
7843 | CXXRecordDecl *ToClass = *ToClassOrErr; | ||||||||||
7844 | |||||||||||
7845 | auto ToCallOpOrErr = import(E->getCallOperator()); | ||||||||||
7846 | if (!ToCallOpOrErr) | ||||||||||
7847 | return ToCallOpOrErr.takeError(); | ||||||||||
7848 | |||||||||||
7849 | SmallVector<Expr *, 8> ToCaptureInits(E->capture_size()); | ||||||||||
7850 | if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits)) | ||||||||||
7851 | return std::move(Err); | ||||||||||
7852 | |||||||||||
7853 | Error Err = Error::success(); | ||||||||||
7854 | auto ToIntroducerRange = importChecked(Err, E->getIntroducerRange()); | ||||||||||
7855 | auto ToCaptureDefaultLoc = importChecked(Err, E->getCaptureDefaultLoc()); | ||||||||||
7856 | auto ToEndLoc = importChecked(Err, E->getEndLoc()); | ||||||||||
7857 | if (Err) | ||||||||||
7858 | return std::move(Err); | ||||||||||
7859 | |||||||||||
7860 | return LambdaExpr::Create(Importer.getToContext(), ToClass, ToIntroducerRange, | ||||||||||
7861 | E->getCaptureDefault(), ToCaptureDefaultLoc, | ||||||||||
7862 | E->hasExplicitParameters(), | ||||||||||
7863 | E->hasExplicitResultType(), ToCaptureInits, | ||||||||||
7864 | ToEndLoc, E->containsUnexpandedParameterPack()); | ||||||||||
7865 | } | ||||||||||
7866 | |||||||||||
7867 | |||||||||||
7868 | ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) { | ||||||||||
7869 | Error Err = Error::success(); | ||||||||||
7870 | auto ToLBraceLoc = importChecked(Err, E->getLBraceLoc()); | ||||||||||
7871 | auto ToRBraceLoc = importChecked(Err, E->getRBraceLoc()); | ||||||||||
7872 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7873 | if (Err) | ||||||||||
7874 | return std::move(Err); | ||||||||||
7875 | |||||||||||
7876 | SmallVector<Expr *, 4> ToExprs(E->getNumInits()); | ||||||||||
7877 | if (Error Err = ImportContainerChecked(E->inits(), ToExprs)) | ||||||||||
7878 | return std::move(Err); | ||||||||||
7879 | |||||||||||
7880 | ASTContext &ToCtx = Importer.getToContext(); | ||||||||||
7881 | InitListExpr *To = new (ToCtx) InitListExpr( | ||||||||||
7882 | ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc); | ||||||||||
7883 | To->setType(ToType); | ||||||||||
7884 | |||||||||||
7885 | if (E->hasArrayFiller()) { | ||||||||||
7886 | if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller())) | ||||||||||
7887 | To->setArrayFiller(*ToFillerOrErr); | ||||||||||
7888 | else | ||||||||||
7889 | return ToFillerOrErr.takeError(); | ||||||||||
7890 | } | ||||||||||
7891 | |||||||||||
7892 | if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) { | ||||||||||
7893 | if (auto ToFDOrErr = import(FromFD)) | ||||||||||
7894 | To->setInitializedFieldInUnion(*ToFDOrErr); | ||||||||||
7895 | else | ||||||||||
7896 | return ToFDOrErr.takeError(); | ||||||||||
7897 | } | ||||||||||
7898 | |||||||||||
7899 | if (InitListExpr *SyntForm = E->getSyntacticForm()) { | ||||||||||
7900 | if (auto ToSyntFormOrErr = import(SyntForm)) | ||||||||||
7901 | To->setSyntacticForm(*ToSyntFormOrErr); | ||||||||||
7902 | else | ||||||||||
7903 | return ToSyntFormOrErr.takeError(); | ||||||||||
7904 | } | ||||||||||
7905 | |||||||||||
7906 | // Copy InitListExprBitfields, which are not handled in the ctor of | ||||||||||
7907 | // InitListExpr. | ||||||||||
7908 | To->sawArrayRangeDesignator(E->hadArrayRangeDesignator()); | ||||||||||
7909 | |||||||||||
7910 | return To; | ||||||||||
7911 | } | ||||||||||
7912 | |||||||||||
7913 | ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr( | ||||||||||
7914 | CXXStdInitializerListExpr *E) { | ||||||||||
7915 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
7916 | if (!ToTypeOrErr) | ||||||||||
7917 | return ToTypeOrErr.takeError(); | ||||||||||
7918 | |||||||||||
7919 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); | ||||||||||
7920 | if (!ToSubExprOrErr) | ||||||||||
7921 | return ToSubExprOrErr.takeError(); | ||||||||||
7922 | |||||||||||
7923 | return new (Importer.getToContext()) CXXStdInitializerListExpr( | ||||||||||
7924 | *ToTypeOrErr, *ToSubExprOrErr); | ||||||||||
7925 | } | ||||||||||
7926 | |||||||||||
7927 | ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr( | ||||||||||
7928 | CXXInheritedCtorInitExpr *E) { | ||||||||||
7929 | Error Err = Error::success(); | ||||||||||
7930 | auto ToLocation = importChecked(Err, E->getLocation()); | ||||||||||
7931 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7932 | auto ToConstructor = importChecked(Err, E->getConstructor()); | ||||||||||
7933 | if (Err) | ||||||||||
7934 | return std::move(Err); | ||||||||||
7935 | |||||||||||
7936 | return new (Importer.getToContext()) CXXInheritedCtorInitExpr( | ||||||||||
7937 | ToLocation, ToType, ToConstructor, E->constructsVBase(), | ||||||||||
7938 | E->inheritedFromVBase()); | ||||||||||
7939 | } | ||||||||||
7940 | |||||||||||
7941 | ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) { | ||||||||||
7942 | Error Err = Error::success(); | ||||||||||
7943 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7944 | auto ToCommonExpr = importChecked(Err, E->getCommonExpr()); | ||||||||||
7945 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | ||||||||||
7946 | if (Err) | ||||||||||
7947 | return std::move(Err); | ||||||||||
7948 | |||||||||||
7949 | return new (Importer.getToContext()) ArrayInitLoopExpr( | ||||||||||
7950 | ToType, ToCommonExpr, ToSubExpr); | ||||||||||
7951 | } | ||||||||||
7952 | |||||||||||
7953 | ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) { | ||||||||||
7954 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
7955 | if (!ToTypeOrErr) | ||||||||||
7956 | return ToTypeOrErr.takeError(); | ||||||||||
7957 | return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr); | ||||||||||
7958 | } | ||||||||||
7959 | |||||||||||
7960 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) { | ||||||||||
7961 | ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc()); | ||||||||||
7962 | if (!ToBeginLocOrErr) | ||||||||||
7963 | return ToBeginLocOrErr.takeError(); | ||||||||||
7964 | |||||||||||
7965 | auto ToFieldOrErr = import(E->getField()); | ||||||||||
7966 | if (!ToFieldOrErr) | ||||||||||
7967 | return ToFieldOrErr.takeError(); | ||||||||||
7968 | |||||||||||
7969 | auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext()); | ||||||||||
7970 | if (!UsedContextOrErr) | ||||||||||
7971 | return UsedContextOrErr.takeError(); | ||||||||||
7972 | |||||||||||
7973 | return CXXDefaultInitExpr::Create( | ||||||||||
7974 | Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr, *UsedContextOrErr); | ||||||||||
7975 | } | ||||||||||
7976 | |||||||||||
7977 | ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { | ||||||||||
7978 | Error Err = Error::success(); | ||||||||||
7979 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
7980 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | ||||||||||
7981 | auto ToTypeInfoAsWritten = importChecked(Err, E->getTypeInfoAsWritten()); | ||||||||||
7982 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | ||||||||||
7983 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
7984 | auto ToAngleBrackets = importChecked(Err, E->getAngleBrackets()); | ||||||||||
7985 | if (Err) | ||||||||||
7986 | return std::move(Err); | ||||||||||
7987 | |||||||||||
7988 | ExprValueKind VK = E->getValueKind(); | ||||||||||
7989 | CastKind CK = E->getCastKind(); | ||||||||||
7990 | auto ToBasePathOrErr = ImportCastPath(E); | ||||||||||
7991 | if (!ToBasePathOrErr) | ||||||||||
7992 | return ToBasePathOrErr.takeError(); | ||||||||||
7993 | |||||||||||
7994 | if (auto CCE = dyn_cast<CXXStaticCastExpr>(E)) { | ||||||||||
7995 | return CXXStaticCastExpr::Create( | ||||||||||
7996 | Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr), | ||||||||||
7997 | ToTypeInfoAsWritten, CCE->getFPFeatures(), ToOperatorLoc, ToRParenLoc, | ||||||||||
7998 | ToAngleBrackets); | ||||||||||
7999 | } else if (isa<CXXDynamicCastExpr>(E)) { | ||||||||||
8000 | return CXXDynamicCastExpr::Create( | ||||||||||
8001 | Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr), | ||||||||||
8002 | ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); | ||||||||||
8003 | } else if (isa<CXXReinterpretCastExpr>(E)) { | ||||||||||
8004 | return CXXReinterpretCastExpr::Create( | ||||||||||
8005 | Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr), | ||||||||||
8006 | ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); | ||||||||||
8007 | } else if (isa<CXXConstCastExpr>(E)) { | ||||||||||
8008 | return CXXConstCastExpr::Create( | ||||||||||
8009 | Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten, | ||||||||||
8010 | ToOperatorLoc, ToRParenLoc, ToAngleBrackets); | ||||||||||
8011 | } else { | ||||||||||
8012 | llvm_unreachable("Unknown cast type")::llvm::llvm_unreachable_internal("Unknown cast type", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8012); | ||||||||||
8013 | return make_error<ImportError>(); | ||||||||||
8014 | } | ||||||||||
8015 | } | ||||||||||
8016 | |||||||||||
8017 | ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr( | ||||||||||
8018 | SubstNonTypeTemplateParmExpr *E) { | ||||||||||
8019 | Error Err = Error::success(); | ||||||||||
8020 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
8021 | auto ToExprLoc = importChecked(Err, E->getExprLoc()); | ||||||||||
8022 | auto ToParameter = importChecked(Err, E->getParameter()); | ||||||||||
8023 | auto ToReplacement = importChecked(Err, E->getReplacement()); | ||||||||||
8024 | if (Err) | ||||||||||
8025 | return std::move(Err); | ||||||||||
8026 | |||||||||||
8027 | return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr( | ||||||||||
8028 | ToType, E->getValueKind(), ToExprLoc, ToParameter, | ||||||||||
8029 | E->isReferenceParameter(), ToReplacement); | ||||||||||
8030 | } | ||||||||||
8031 | |||||||||||
8032 | ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) { | ||||||||||
8033 | Error Err = Error::success(); | ||||||||||
8034 | auto ToType = importChecked(Err, E->getType()); | ||||||||||
8035 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | ||||||||||
8036 | auto ToEndLoc = importChecked(Err, E->getEndLoc()); | ||||||||||
8037 | if (Err) | ||||||||||
8038 | return std::move(Err); | ||||||||||
8039 | |||||||||||
8040 | SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs()); | ||||||||||
8041 | if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs)) | ||||||||||
8042 | return std::move(Err); | ||||||||||
8043 | |||||||||||
8044 | // According to Sema::BuildTypeTrait(), if E is value-dependent, | ||||||||||
8045 | // Value is always false. | ||||||||||
8046 | bool ToValue = (E->isValueDependent() ? false : E->getValue()); | ||||||||||
8047 | |||||||||||
8048 | return TypeTraitExpr::Create( | ||||||||||
8049 | Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs, | ||||||||||
8050 | ToEndLoc, ToValue); | ||||||||||
8051 | } | ||||||||||
8052 | |||||||||||
8053 | ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) { | ||||||||||
8054 | ExpectedType ToTypeOrErr = import(E->getType()); | ||||||||||
8055 | if (!ToTypeOrErr) | ||||||||||
8056 | return ToTypeOrErr.takeError(); | ||||||||||
8057 | |||||||||||
8058 | auto ToSourceRangeOrErr = import(E->getSourceRange()); | ||||||||||
8059 | if (!ToSourceRangeOrErr) | ||||||||||
8060 | return ToSourceRangeOrErr.takeError(); | ||||||||||
8061 | |||||||||||
8062 | if (E->isTypeOperand()) { | ||||||||||
8063 | if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo())) | ||||||||||
8064 | return new (Importer.getToContext()) CXXTypeidExpr( | ||||||||||
8065 | *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr); | ||||||||||
8066 | else | ||||||||||
8067 | return ToTSIOrErr.takeError(); | ||||||||||
8068 | } | ||||||||||
8069 | |||||||||||
8070 | ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand()); | ||||||||||
8071 | if (!ToExprOperandOrErr) | ||||||||||
8072 | return ToExprOperandOrErr.takeError(); | ||||||||||
8073 | |||||||||||
8074 | return new (Importer.getToContext()) CXXTypeidExpr( | ||||||||||
8075 | *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr); | ||||||||||
8076 | } | ||||||||||
8077 | |||||||||||
8078 | ExpectedStmt ASTNodeImporter::VisitCXXFoldExpr(CXXFoldExpr *E) { | ||||||||||
8079 | Error Err = Error::success(); | ||||||||||
8080 | |||||||||||
8081 | QualType ToType = importChecked(Err, E->getType()); | ||||||||||
8082 | UnresolvedLookupExpr *ToCallee = importChecked(Err, E->getCallee()); | ||||||||||
8083 | SourceLocation ToLParenLoc = importChecked(Err, E->getLParenLoc()); | ||||||||||
8084 | Expr *ToLHS = importChecked(Err, E->getLHS()); | ||||||||||
8085 | SourceLocation ToEllipsisLoc = importChecked(Err, E->getEllipsisLoc()); | ||||||||||
8086 | Expr *ToRHS = importChecked(Err, E->getRHS()); | ||||||||||
8087 | SourceLocation ToRParenLoc = importChecked(Err, E->getRParenLoc()); | ||||||||||
8088 | |||||||||||
8089 | if (Err) | ||||||||||
8090 | return std::move(Err); | ||||||||||
8091 | |||||||||||
8092 | return new (Importer.getToContext()) | ||||||||||
8093 | CXXFoldExpr(ToType, ToCallee, ToLParenLoc, ToLHS, E->getOperator(), | ||||||||||
8094 | ToEllipsisLoc, ToRHS, ToRParenLoc, E->getNumExpansions()); | ||||||||||
8095 | } | ||||||||||
8096 | |||||||||||
8097 | Error ASTNodeImporter::ImportOverriddenMethods(CXXMethodDecl *ToMethod, | ||||||||||
8098 | CXXMethodDecl *FromMethod) { | ||||||||||
8099 | Error ImportErrors = Error::success(); | ||||||||||
8100 | for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) { | ||||||||||
8101 | if (auto ImportedOrErr = import(FromOverriddenMethod)) | ||||||||||
8102 | ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>( | ||||||||||
8103 | (*ImportedOrErr)->getCanonicalDecl())); | ||||||||||
8104 | else | ||||||||||
8105 | ImportErrors = | ||||||||||
8106 | joinErrors(std::move(ImportErrors), ImportedOrErr.takeError()); | ||||||||||
8107 | } | ||||||||||
8108 | return ImportErrors; | ||||||||||
8109 | } | ||||||||||
8110 | |||||||||||
8111 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, | ||||||||||
8112 | ASTContext &FromContext, FileManager &FromFileManager, | ||||||||||
8113 | bool MinimalImport, | ||||||||||
8114 | std::shared_ptr<ASTImporterSharedState> SharedState) | ||||||||||
8115 | : SharedState(SharedState), ToContext(ToContext), FromContext(FromContext), | ||||||||||
8116 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), | ||||||||||
8117 | Minimal(MinimalImport), ODRHandling(ODRHandlingType::Conservative) { | ||||||||||
8118 | |||||||||||
8119 | // Create a default state without the lookup table: LLDB case. | ||||||||||
8120 | if (!SharedState) { | ||||||||||
8121 | this->SharedState = std::make_shared<ASTImporterSharedState>(); | ||||||||||
8122 | } | ||||||||||
8123 | |||||||||||
8124 | ImportedDecls[FromContext.getTranslationUnitDecl()] = | ||||||||||
8125 | ToContext.getTranslationUnitDecl(); | ||||||||||
8126 | } | ||||||||||
8127 | |||||||||||
8128 | ASTImporter::~ASTImporter() = default; | ||||||||||
8129 | |||||||||||
8130 | Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) { | ||||||||||
8131 | assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&((F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl >(*F)) && "Try to get field index for non-field.") ? static_cast<void> (0) : __assert_fail ("F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) && \"Try to get field index for non-field.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8132, __PRETTY_FUNCTION__)) | ||||||||||
8132 | "Try to get field index for non-field.")((F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl >(*F)) && "Try to get field index for non-field.") ? static_cast<void> (0) : __assert_fail ("F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) && \"Try to get field index for non-field.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8132, __PRETTY_FUNCTION__)); | ||||||||||
8133 | |||||||||||
8134 | auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); | ||||||||||
8135 | if (!Owner) | ||||||||||
8136 | return None; | ||||||||||
8137 | |||||||||||
8138 | unsigned Index = 0; | ||||||||||
8139 | for (const auto *D : Owner->decls()) { | ||||||||||
8140 | if (D == F) | ||||||||||
8141 | return Index; | ||||||||||
8142 | |||||||||||
8143 | if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) | ||||||||||
8144 | ++Index; | ||||||||||
8145 | } | ||||||||||
8146 | |||||||||||
8147 | llvm_unreachable("Field was not found in its parent context.")::llvm::llvm_unreachable_internal("Field was not found in its parent context." , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8147); | ||||||||||
8148 | |||||||||||
8149 | return None; | ||||||||||
8150 | } | ||||||||||
8151 | |||||||||||
8152 | ASTImporter::FoundDeclsTy | ||||||||||
8153 | ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) { | ||||||||||
8154 | // We search in the redecl context because of transparent contexts. | ||||||||||
8155 | // E.g. a simple C language enum is a transparent context: | ||||||||||
8156 | // enum E { A, B }; | ||||||||||
8157 | // Now if we had a global variable in the TU | ||||||||||
8158 | // int A; | ||||||||||
8159 | // then the enum constant 'A' and the variable 'A' violates ODR. | ||||||||||
8160 | // We can diagnose this only if we search in the redecl context. | ||||||||||
8161 | DeclContext *ReDC = DC->getRedeclContext(); | ||||||||||
8162 | if (SharedState->getLookupTable()) { | ||||||||||
8163 | ASTImporterLookupTable::LookupResult LookupResult = | ||||||||||
8164 | SharedState->getLookupTable()->lookup(ReDC, Name); | ||||||||||
8165 | return FoundDeclsTy(LookupResult.begin(), LookupResult.end()); | ||||||||||
8166 | } else { | ||||||||||
8167 | DeclContext::lookup_result NoloadLookupResult = ReDC->noload_lookup(Name); | ||||||||||
8168 | FoundDeclsTy Result(NoloadLookupResult.begin(), NoloadLookupResult.end()); | ||||||||||
8169 | // We must search by the slow case of localUncachedLookup because that is | ||||||||||
8170 | // working even if there is no LookupPtr for the DC. We could use | ||||||||||
8171 | // DC::buildLookup() to create the LookupPtr, but that would load external | ||||||||||
8172 | // decls again, we must avoid that case. | ||||||||||
8173 | // Also, even if we had the LookupPtr, we must find Decls which are not | ||||||||||
8174 | // in the LookupPtr, so we need the slow case. | ||||||||||
8175 | // These cases are handled in ASTImporterLookupTable, but we cannot use | ||||||||||
8176 | // that with LLDB since that traverses through the AST which initiates the | ||||||||||
8177 | // load of external decls again via DC::decls(). And again, we must avoid | ||||||||||
8178 | // loading external decls during the import. | ||||||||||
8179 | if (Result.empty()) | ||||||||||
8180 | ReDC->localUncachedLookup(Name, Result); | ||||||||||
8181 | return Result; | ||||||||||
8182 | } | ||||||||||
8183 | } | ||||||||||
8184 | |||||||||||
8185 | void ASTImporter::AddToLookupTable(Decl *ToD) { | ||||||||||
8186 | SharedState->addDeclToLookup(ToD); | ||||||||||
8187 | } | ||||||||||
8188 | |||||||||||
8189 | Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) { | ||||||||||
8190 | // Import the decl using ASTNodeImporter. | ||||||||||
8191 | ASTNodeImporter Importer(*this); | ||||||||||
8192 | return Importer.Visit(FromD); | ||||||||||
8193 | } | ||||||||||
8194 | |||||||||||
8195 | void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) { | ||||||||||
8196 | MapImported(FromD, ToD); | ||||||||||
8197 | } | ||||||||||
8198 | |||||||||||
8199 | llvm::Expected<ExprWithCleanups::CleanupObject> | ||||||||||
8200 | ASTImporter::Import(ExprWithCleanups::CleanupObject From) { | ||||||||||
8201 | if (auto *CLE = From.dyn_cast<CompoundLiteralExpr *>()) { | ||||||||||
8202 | if (Expected<Expr *> R = Import(CLE)) | ||||||||||
8203 | return ExprWithCleanups::CleanupObject(cast<CompoundLiteralExpr>(*R)); | ||||||||||
8204 | } | ||||||||||
8205 | |||||||||||
8206 | // FIXME: Handle BlockDecl when we implement importing BlockExpr in | ||||||||||
8207 | // ASTNodeImporter. | ||||||||||
8208 | return make_error<ImportError>(ImportError::UnsupportedConstruct); | ||||||||||
8209 | } | ||||||||||
8210 | |||||||||||
8211 | Expected<const Type *> ASTImporter::Import(const Type *FromT) { | ||||||||||
8212 | if (!FromT) | ||||||||||
8213 | return FromT; | ||||||||||
8214 | |||||||||||
8215 | // Check whether we've already imported this type. | ||||||||||
8216 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = | ||||||||||
8217 | ImportedTypes.find(FromT); | ||||||||||
8218 | if (Pos != ImportedTypes.end()) | ||||||||||
8219 | return Pos->second; | ||||||||||
8220 | |||||||||||
8221 | // Import the type | ||||||||||
8222 | ASTNodeImporter Importer(*this); | ||||||||||
8223 | ExpectedType ToTOrErr = Importer.Visit(FromT); | ||||||||||
8224 | if (!ToTOrErr) | ||||||||||
8225 | return ToTOrErr.takeError(); | ||||||||||
8226 | |||||||||||
8227 | // Record the imported type. | ||||||||||
8228 | ImportedTypes[FromT] = ToTOrErr->getTypePtr(); | ||||||||||
8229 | |||||||||||
8230 | return ToTOrErr->getTypePtr(); | ||||||||||
8231 | } | ||||||||||
8232 | |||||||||||
8233 | Expected<QualType> ASTImporter::Import(QualType FromT) { | ||||||||||
8234 | if (FromT.isNull()) | ||||||||||
8235 | return QualType{}; | ||||||||||
8236 | |||||||||||
8237 | Expected<const Type *> ToTyOrErr = Import(FromT.getTypePtr()); | ||||||||||
8238 | if (!ToTyOrErr) | ||||||||||
8239 | return ToTyOrErr.takeError(); | ||||||||||
8240 | |||||||||||
8241 | return ToContext.getQualifiedType(*ToTyOrErr, FromT.getLocalQualifiers()); | ||||||||||
8242 | } | ||||||||||
8243 | |||||||||||
8244 | Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) { | ||||||||||
8245 | if (!FromTSI) | ||||||||||
8246 | return FromTSI; | ||||||||||
8247 | |||||||||||
8248 | // FIXME: For now we just create a "trivial" type source info based | ||||||||||
8249 | // on the type and a single location. Implement a real version of this. | ||||||||||
8250 | ExpectedType TOrErr = Import(FromTSI->getType()); | ||||||||||
8251 | if (!TOrErr) | ||||||||||
8252 | return TOrErr.takeError(); | ||||||||||
8253 | ExpectedSLoc BeginLocOrErr = Import(FromTSI->getTypeLoc().getBeginLoc()); | ||||||||||
8254 | if (!BeginLocOrErr) | ||||||||||
8255 | return BeginLocOrErr.takeError(); | ||||||||||
8256 | |||||||||||
8257 | return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr); | ||||||||||
8258 | } | ||||||||||
8259 | |||||||||||
8260 | Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) { | ||||||||||
8261 | Attr *ToAttr = nullptr; | ||||||||||
8262 | SourceRange ToRange; | ||||||||||
8263 | if (Error Err = importInto(ToRange, FromAttr->getRange())) | ||||||||||
8264 | return std::move(Err); | ||||||||||
8265 | |||||||||||
8266 | // FIXME: Is there some kind of AttrVisitor to use here? | ||||||||||
8267 | switch (FromAttr->getKind()) { | ||||||||||
8268 | case attr::Aligned: { | ||||||||||
8269 | auto *From = cast<AlignedAttr>(FromAttr); | ||||||||||
8270 | AlignedAttr *To; | ||||||||||
8271 | auto CreateAlign = [&](bool IsAlignmentExpr, void *Alignment) { | ||||||||||
8272 | return AlignedAttr::Create(ToContext, IsAlignmentExpr, Alignment, ToRange, | ||||||||||
8273 | From->getSyntax(), | ||||||||||
8274 | From->getSemanticSpelling()); | ||||||||||
8275 | }; | ||||||||||
8276 | if (From->isAlignmentExpr()) { | ||||||||||
8277 | if (auto ToEOrErr = Import(From->getAlignmentExpr())) | ||||||||||
8278 | To = CreateAlign(true, *ToEOrErr); | ||||||||||
8279 | else | ||||||||||
8280 | return ToEOrErr.takeError(); | ||||||||||
8281 | } else { | ||||||||||
8282 | if (auto ToTOrErr = Import(From->getAlignmentType())) | ||||||||||
8283 | To = CreateAlign(false, *ToTOrErr); | ||||||||||
8284 | else | ||||||||||
8285 | return ToTOrErr.takeError(); | ||||||||||
8286 | } | ||||||||||
8287 | To->setInherited(From->isInherited()); | ||||||||||
8288 | To->setPackExpansion(From->isPackExpansion()); | ||||||||||
8289 | To->setImplicit(From->isImplicit()); | ||||||||||
8290 | ToAttr = To; | ||||||||||
8291 | break; | ||||||||||
8292 | } | ||||||||||
8293 | case attr::Format: { | ||||||||||
8294 | const auto *From = cast<FormatAttr>(FromAttr); | ||||||||||
8295 | FormatAttr *To; | ||||||||||
8296 | IdentifierInfo *ToAttrType = Import(From->getType()); | ||||||||||
8297 | To = FormatAttr::Create(ToContext, ToAttrType, From->getFormatIdx(), | ||||||||||
8298 | From->getFirstArg(), ToRange, From->getSyntax()); | ||||||||||
8299 | To->setInherited(From->isInherited()); | ||||||||||
8300 | ToAttr = To; | ||||||||||
8301 | break; | ||||||||||
8302 | } | ||||||||||
8303 | default: | ||||||||||
8304 | // FIXME: 'clone' copies every member but some of them should be imported. | ||||||||||
8305 | // Handle other Attrs that have parameters that should be imported. | ||||||||||
8306 | ToAttr = FromAttr->clone(ToContext); | ||||||||||
8307 | ToAttr->setRange(ToRange); | ||||||||||
8308 | break; | ||||||||||
8309 | } | ||||||||||
8310 | assert(ToAttr && "Attribute should be created.")((ToAttr && "Attribute should be created.") ? static_cast <void> (0) : __assert_fail ("ToAttr && \"Attribute should be created.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8310, __PRETTY_FUNCTION__)); | ||||||||||
8311 | |||||||||||
8312 | return ToAttr; | ||||||||||
8313 | } | ||||||||||
8314 | |||||||||||
8315 | Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const { | ||||||||||
8316 | auto Pos = ImportedDecls.find(FromD); | ||||||||||
8317 | if (Pos != ImportedDecls.end()) | ||||||||||
8318 | return Pos->second; | ||||||||||
8319 | else | ||||||||||
8320 | return nullptr; | ||||||||||
8321 | } | ||||||||||
8322 | |||||||||||
8323 | TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) { | ||||||||||
8324 | auto FromDPos = ImportedFromDecls.find(ToD); | ||||||||||
8325 | if (FromDPos == ImportedFromDecls.end()) | ||||||||||
8326 | return nullptr; | ||||||||||
8327 | return FromDPos->second->getTranslationUnitDecl(); | ||||||||||
8328 | } | ||||||||||
8329 | |||||||||||
8330 | Expected<Decl *> ASTImporter::Import(Decl *FromD) { | ||||||||||
8331 | if (!FromD) | ||||||||||
8332 | return nullptr; | ||||||||||
8333 | |||||||||||
8334 | // Push FromD to the stack, and remove that when we return. | ||||||||||
8335 | ImportPath.push(FromD); | ||||||||||
8336 | auto ImportPathBuilder = | ||||||||||
8337 | llvm::make_scope_exit([this]() { ImportPath.pop(); }); | ||||||||||
8338 | |||||||||||
8339 | // Check whether there was a previous failed import. | ||||||||||
8340 | // If yes return the existing error. | ||||||||||
8341 | if (auto Error = getImportDeclErrorIfAny(FromD)) | ||||||||||
8342 | return make_error<ImportError>(*Error); | ||||||||||
8343 | |||||||||||
8344 | // Check whether we've already imported this declaration. | ||||||||||
8345 | Decl *ToD = GetAlreadyImportedOrNull(FromD); | ||||||||||
8346 | if (ToD) { | ||||||||||
8347 | // Already imported (possibly from another TU) and with an error. | ||||||||||
8348 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { | ||||||||||
8349 | setImportDeclError(FromD, *Error); | ||||||||||
8350 | return make_error<ImportError>(*Error); | ||||||||||
8351 | } | ||||||||||
8352 | |||||||||||
8353 | // If FromD has some updated flags after last import, apply it. | ||||||||||
8354 | updateFlags(FromD, ToD); | ||||||||||
8355 | // If we encounter a cycle during an import then we save the relevant part | ||||||||||
8356 | // of the import path associated to the Decl. | ||||||||||
8357 | if (ImportPath.hasCycleAtBack()) | ||||||||||
8358 | SavedImportPaths[FromD].push_back(ImportPath.copyCycleAtBack()); | ||||||||||
8359 | return ToD; | ||||||||||
8360 | } | ||||||||||
8361 | |||||||||||
8362 | // Import the declaration. | ||||||||||
8363 | ExpectedDecl ToDOrErr = ImportImpl(FromD); | ||||||||||
8364 | if (!ToDOrErr) { | ||||||||||
8365 | // Failed to import. | ||||||||||
8366 | |||||||||||
8367 | auto Pos = ImportedDecls.find(FromD); | ||||||||||
8368 | if (Pos != ImportedDecls.end()) { | ||||||||||
8369 | // Import failed after the object was created. | ||||||||||
8370 | // Remove all references to it. | ||||||||||
8371 | auto *ToD = Pos->second; | ||||||||||
8372 | ImportedDecls.erase(Pos); | ||||||||||
8373 | |||||||||||
8374 | // ImportedDecls and ImportedFromDecls are not symmetric. It may happen | ||||||||||
8375 | // (e.g. with namespaces) that several decls from the 'from' context are | ||||||||||
8376 | // mapped to the same decl in the 'to' context. If we removed entries | ||||||||||
8377 | // from the LookupTable here then we may end up removing them multiple | ||||||||||
8378 | // times. | ||||||||||
8379 | |||||||||||
8380 | // The Lookuptable contains decls only which are in the 'to' context. | ||||||||||
8381 | // Remove from the Lookuptable only if it is *imported* into the 'to' | ||||||||||
8382 | // context (and do not remove it if it was added during the initial | ||||||||||
8383 | // traverse of the 'to' context). | ||||||||||
8384 | auto PosF = ImportedFromDecls.find(ToD); | ||||||||||
8385 | if (PosF != ImportedFromDecls.end()) { | ||||||||||
8386 | SharedState->removeDeclFromLookup(ToD); | ||||||||||
8387 | ImportedFromDecls.erase(PosF); | ||||||||||
8388 | } | ||||||||||
8389 | |||||||||||
8390 | // FIXME: AST may contain remaining references to the failed object. | ||||||||||
8391 | // However, the ImportDeclErrors in the shared state contains all the | ||||||||||
8392 | // failed objects together with their error. | ||||||||||
8393 | } | ||||||||||
8394 | |||||||||||
8395 | // Error encountered for the first time. | ||||||||||
8396 | // After takeError the error is not usable any more in ToDOrErr. | ||||||||||
8397 | // Get a copy of the error object (any more simple solution for this?). | ||||||||||
8398 | ImportError ErrOut; | ||||||||||
8399 | handleAllErrors(ToDOrErr.takeError(), | ||||||||||
8400 | [&ErrOut](const ImportError &E) { ErrOut = E; }); | ||||||||||
8401 | setImportDeclError(FromD, ErrOut); | ||||||||||
8402 | // Set the error for the mapped to Decl, which is in the "to" context. | ||||||||||
8403 | if (Pos != ImportedDecls.end()) | ||||||||||
8404 | SharedState->setImportDeclError(Pos->second, ErrOut); | ||||||||||
8405 | |||||||||||
8406 | // Set the error for all nodes which have been created before we | ||||||||||
8407 | // recognized the error. | ||||||||||
8408 | for (const auto &Path : SavedImportPaths[FromD]) | ||||||||||
8409 | for (Decl *FromDi : Path) { | ||||||||||
8410 | setImportDeclError(FromDi, ErrOut); | ||||||||||
8411 | //FIXME Should we remove these Decls from ImportedDecls? | ||||||||||
8412 | // Set the error for the mapped to Decl, which is in the "to" context. | ||||||||||
8413 | auto Ii = ImportedDecls.find(FromDi); | ||||||||||
8414 | if (Ii != ImportedDecls.end()) | ||||||||||
8415 | SharedState->setImportDeclError(Ii->second, ErrOut); | ||||||||||
8416 | // FIXME Should we remove these Decls from the LookupTable, | ||||||||||
8417 | // and from ImportedFromDecls? | ||||||||||
8418 | } | ||||||||||
8419 | SavedImportPaths.erase(FromD); | ||||||||||
8420 | |||||||||||
8421 | // Do not return ToDOrErr, error was taken out of it. | ||||||||||
8422 | return make_error<ImportError>(ErrOut); | ||||||||||
8423 | } | ||||||||||
8424 | |||||||||||
8425 | ToD = *ToDOrErr; | ||||||||||
8426 | |||||||||||
8427 | // FIXME: Handle the "already imported with error" case. We can get here | ||||||||||
8428 | // nullptr only if GetImportedOrCreateDecl returned nullptr (after a | ||||||||||
8429 | // previously failed create was requested). | ||||||||||
8430 | // Later GetImportedOrCreateDecl can be updated to return the error. | ||||||||||
8431 | if (!ToD) { | ||||||||||
8432 | auto Err = getImportDeclErrorIfAny(FromD); | ||||||||||
8433 | assert(Err)((Err) ? static_cast<void> (0) : __assert_fail ("Err", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8433, __PRETTY_FUNCTION__)); | ||||||||||
8434 | return make_error<ImportError>(*Err); | ||||||||||
8435 | } | ||||||||||
8436 | |||||||||||
8437 | // We could import from the current TU without error. But previously we | ||||||||||
8438 | // already had imported a Decl as `ToD` from another TU (with another | ||||||||||
8439 | // ASTImporter object) and with an error. | ||||||||||
8440 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { | ||||||||||
8441 | setImportDeclError(FromD, *Error); | ||||||||||
8442 | return make_error<ImportError>(*Error); | ||||||||||
8443 | } | ||||||||||
8444 | |||||||||||
8445 | // Make sure that ImportImpl registered the imported decl. | ||||||||||
8446 | assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?")((ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?" ) ? static_cast<void> (0) : __assert_fail ("ImportedDecls.count(FromD) != 0 && \"Missing call to MapImported?\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8446, __PRETTY_FUNCTION__)); | ||||||||||
8447 | |||||||||||
8448 | if (FromD->hasAttrs()) | ||||||||||
8449 | for (const Attr *FromAttr : FromD->getAttrs()) { | ||||||||||
8450 | auto ToAttrOrErr = Import(FromAttr); | ||||||||||
8451 | if (ToAttrOrErr) | ||||||||||
8452 | ToD->addAttr(*ToAttrOrErr); | ||||||||||
8453 | else | ||||||||||
8454 | return ToAttrOrErr.takeError(); | ||||||||||
8455 | } | ||||||||||
8456 | |||||||||||
8457 | // Notify subclasses. | ||||||||||
8458 | Imported(FromD, ToD); | ||||||||||
8459 | |||||||||||
8460 | updateFlags(FromD, ToD); | ||||||||||
8461 | SavedImportPaths.erase(FromD); | ||||||||||
8462 | return ToDOrErr; | ||||||||||
8463 | } | ||||||||||
8464 | |||||||||||
8465 | Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) { | ||||||||||
8466 | if (!FromDC) | ||||||||||
8467 | return FromDC; | ||||||||||
8468 | |||||||||||
8469 | ExpectedDecl ToDCOrErr = Import(cast<Decl>(FromDC)); | ||||||||||
8470 | if (!ToDCOrErr) | ||||||||||
8471 | return ToDCOrErr.takeError(); | ||||||||||
8472 | auto *ToDC = cast<DeclContext>(*ToDCOrErr); | ||||||||||
8473 | |||||||||||
8474 | // When we're using a record/enum/Objective-C class/protocol as a context, we | ||||||||||
8475 | // need it to have a definition. | ||||||||||
8476 | if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) { | ||||||||||
8477 | auto *FromRecord = cast<RecordDecl>(FromDC); | ||||||||||
8478 | if (ToRecord->isCompleteDefinition()) | ||||||||||
8479 | return ToDC; | ||||||||||
8480 | |||||||||||
8481 | // If FromRecord is not defined we need to force it to be. | ||||||||||
8482 | // Simply calling CompleteDecl(...) for a RecordDecl will break some cases | ||||||||||
8483 | // it will start the definition but we never finish it. | ||||||||||
8484 | // If there are base classes they won't be imported and we will | ||||||||||
8485 | // be missing anything that we inherit from those bases. | ||||||||||
8486 | if (FromRecord->getASTContext().getExternalSource() && | ||||||||||
8487 | !FromRecord->isCompleteDefinition()) | ||||||||||
8488 | FromRecord->getASTContext().getExternalSource()->CompleteType(FromRecord); | ||||||||||
8489 | |||||||||||
8490 | if (FromRecord->isCompleteDefinition()) | ||||||||||
8491 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( | ||||||||||
8492 | FromRecord, ToRecord, ASTNodeImporter::IDK_Basic)) | ||||||||||
8493 | return std::move(Err); | ||||||||||
8494 | } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) { | ||||||||||
8495 | auto *FromEnum = cast<EnumDecl>(FromDC); | ||||||||||
8496 | if (ToEnum->isCompleteDefinition()) { | ||||||||||
8497 | // Do nothing. | ||||||||||
8498 | } else if (FromEnum->isCompleteDefinition()) { | ||||||||||
8499 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( | ||||||||||
8500 | FromEnum, ToEnum, ASTNodeImporter::IDK_Basic)) | ||||||||||
8501 | return std::move(Err); | ||||||||||
8502 | } else { | ||||||||||
8503 | CompleteDecl(ToEnum); | ||||||||||
8504 | } | ||||||||||
8505 | } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { | ||||||||||
8506 | auto *FromClass = cast<ObjCInterfaceDecl>(FromDC); | ||||||||||
8507 | if (ToClass->getDefinition()) { | ||||||||||
8508 | // Do nothing. | ||||||||||
8509 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { | ||||||||||
8510 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( | ||||||||||
8511 | FromDef, ToClass, ASTNodeImporter::IDK_Basic)) | ||||||||||
8512 | return std::move(Err); | ||||||||||
8513 | } else { | ||||||||||
8514 | CompleteDecl(ToClass); | ||||||||||
8515 | } | ||||||||||
8516 | } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { | ||||||||||
8517 | auto *FromProto = cast<ObjCProtocolDecl>(FromDC); | ||||||||||
8518 | if (ToProto->getDefinition()) { | ||||||||||
8519 | // Do nothing. | ||||||||||
8520 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { | ||||||||||
8521 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( | ||||||||||
8522 | FromDef, ToProto, ASTNodeImporter::IDK_Basic)) | ||||||||||
8523 | return std::move(Err); | ||||||||||
8524 | } else { | ||||||||||
8525 | CompleteDecl(ToProto); | ||||||||||
8526 | } | ||||||||||
8527 | } | ||||||||||
8528 | |||||||||||
8529 | return ToDC; | ||||||||||
8530 | } | ||||||||||
8531 | |||||||||||
8532 | Expected<Expr *> ASTImporter::Import(Expr *FromE) { | ||||||||||
8533 | if (ExpectedStmt ToSOrErr = Import(cast_or_null<Stmt>(FromE))) | ||||||||||
8534 | return cast_or_null<Expr>(*ToSOrErr); | ||||||||||
8535 | else | ||||||||||
8536 | return ToSOrErr.takeError(); | ||||||||||
8537 | } | ||||||||||
8538 | |||||||||||
8539 | Expected<Stmt *> ASTImporter::Import(Stmt *FromS) { | ||||||||||
8540 | if (!FromS) | ||||||||||
8541 | return nullptr; | ||||||||||
8542 | |||||||||||
8543 | // Check whether we've already imported this statement. | ||||||||||
8544 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); | ||||||||||
8545 | if (Pos != ImportedStmts.end()) | ||||||||||
8546 | return Pos->second; | ||||||||||
8547 | |||||||||||
8548 | // Import the statement. | ||||||||||
8549 | ASTNodeImporter Importer(*this); | ||||||||||
8550 | ExpectedStmt ToSOrErr = Importer.Visit(FromS); | ||||||||||
8551 | if (!ToSOrErr) | ||||||||||
8552 | return ToSOrErr; | ||||||||||
8553 | |||||||||||
8554 | if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) { | ||||||||||
8555 | auto *FromE = cast<Expr>(FromS); | ||||||||||
8556 | // Copy ExprBitfields, which may not be handled in Expr subclasses | ||||||||||
8557 | // constructors. | ||||||||||
8558 | ToE->setValueKind(FromE->getValueKind()); | ||||||||||
8559 | ToE->setObjectKind(FromE->getObjectKind()); | ||||||||||
8560 | ToE->setDependence(FromE->getDependence()); | ||||||||||
8561 | } | ||||||||||
8562 | |||||||||||
8563 | // Record the imported statement object. | ||||||||||
8564 | ImportedStmts[FromS] = *ToSOrErr; | ||||||||||
8565 | return ToSOrErr; | ||||||||||
8566 | } | ||||||||||
8567 | |||||||||||
8568 | Expected<NestedNameSpecifier *> | ||||||||||
8569 | ASTImporter::Import(NestedNameSpecifier *FromNNS) { | ||||||||||
8570 | if (!FromNNS) | ||||||||||
8571 | return nullptr; | ||||||||||
8572 | |||||||||||
8573 | NestedNameSpecifier *Prefix = nullptr; | ||||||||||
8574 | if (Error Err = importInto(Prefix, FromNNS->getPrefix())) | ||||||||||
8575 | return std::move(Err); | ||||||||||
8576 | |||||||||||
8577 | switch (FromNNS->getKind()) { | ||||||||||
8578 | case NestedNameSpecifier::Identifier: | ||||||||||
8579 | assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.")((FromNNS->getAsIdentifier() && "NNS should contain identifier." ) ? static_cast<void> (0) : __assert_fail ("FromNNS->getAsIdentifier() && \"NNS should contain identifier.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8579, __PRETTY_FUNCTION__)); | ||||||||||
8580 | return NestedNameSpecifier::Create(ToContext, Prefix, | ||||||||||
8581 | Import(FromNNS->getAsIdentifier())); | ||||||||||
8582 | |||||||||||
8583 | case NestedNameSpecifier::Namespace: | ||||||||||
8584 | if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) { | ||||||||||
8585 | return NestedNameSpecifier::Create(ToContext, Prefix, | ||||||||||
8586 | cast<NamespaceDecl>(*NSOrErr)); | ||||||||||
8587 | } else | ||||||||||
8588 | return NSOrErr.takeError(); | ||||||||||
8589 | |||||||||||
8590 | case NestedNameSpecifier::NamespaceAlias: | ||||||||||
8591 | if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias())) | ||||||||||
8592 | return NestedNameSpecifier::Create(ToContext, Prefix, | ||||||||||
8593 | cast<NamespaceAliasDecl>(*NSADOrErr)); | ||||||||||
8594 | else | ||||||||||
8595 | return NSADOrErr.takeError(); | ||||||||||
8596 | |||||||||||
8597 | case NestedNameSpecifier::Global: | ||||||||||
8598 | return NestedNameSpecifier::GlobalSpecifier(ToContext); | ||||||||||
8599 | |||||||||||
8600 | case NestedNameSpecifier::Super: | ||||||||||
8601 | if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl())) | ||||||||||
8602 | return NestedNameSpecifier::SuperSpecifier(ToContext, | ||||||||||
8603 | cast<CXXRecordDecl>(*RDOrErr)); | ||||||||||
8604 | else | ||||||||||
8605 | return RDOrErr.takeError(); | ||||||||||
8606 | |||||||||||
8607 | case NestedNameSpecifier::TypeSpec: | ||||||||||
8608 | case NestedNameSpecifier::TypeSpecWithTemplate: | ||||||||||
8609 | if (Expected<QualType> TyOrErr = | ||||||||||
8610 | Import(QualType(FromNNS->getAsType(), 0u))) { | ||||||||||
8611 | bool TSTemplate = | ||||||||||
8612 | FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate; | ||||||||||
8613 | return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate, | ||||||||||
8614 | TyOrErr->getTypePtr()); | ||||||||||
8615 | } else { | ||||||||||
8616 | return TyOrErr.takeError(); | ||||||||||
8617 | } | ||||||||||
8618 | } | ||||||||||
8619 | |||||||||||
8620 | llvm_unreachable("Invalid nested name specifier kind")::llvm::llvm_unreachable_internal("Invalid nested name specifier kind" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8620); | ||||||||||
8621 | } | ||||||||||
8622 | |||||||||||
8623 | Expected<NestedNameSpecifierLoc> | ||||||||||
8624 | ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { | ||||||||||
8625 | // Copied from NestedNameSpecifier mostly. | ||||||||||
8626 | SmallVector<NestedNameSpecifierLoc , 8> NestedNames; | ||||||||||
8627 | NestedNameSpecifierLoc NNS = FromNNS; | ||||||||||
8628 | |||||||||||
8629 | // Push each of the nested-name-specifiers's onto a stack for | ||||||||||
8630 | // serialization in reverse order. | ||||||||||
8631 | while (NNS) { | ||||||||||
8632 | NestedNames.push_back(NNS); | ||||||||||
8633 | NNS = NNS.getPrefix(); | ||||||||||
8634 | } | ||||||||||
8635 | |||||||||||
8636 | NestedNameSpecifierLocBuilder Builder; | ||||||||||
8637 | |||||||||||
8638 | while (!NestedNames.empty()) { | ||||||||||
8639 | NNS = NestedNames.pop_back_val(); | ||||||||||
8640 | NestedNameSpecifier *Spec = nullptr; | ||||||||||
8641 | if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier())) | ||||||||||
8642 | return std::move(Err); | ||||||||||
8643 | |||||||||||
8644 | NestedNameSpecifier::SpecifierKind Kind = Spec->getKind(); | ||||||||||
8645 | |||||||||||
8646 | SourceLocation ToLocalBeginLoc, ToLocalEndLoc; | ||||||||||
8647 | if (Kind != NestedNameSpecifier::Super) { | ||||||||||
8648 | if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc())) | ||||||||||
8649 | return std::move(Err); | ||||||||||
8650 | |||||||||||
8651 | if (Kind != NestedNameSpecifier::Global) | ||||||||||
8652 | if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc())) | ||||||||||
8653 | return std::move(Err); | ||||||||||
8654 | } | ||||||||||
8655 | |||||||||||
8656 | switch (Kind) { | ||||||||||
8657 | case NestedNameSpecifier::Identifier: | ||||||||||
8658 | Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc, | ||||||||||
8659 | ToLocalEndLoc); | ||||||||||
8660 | break; | ||||||||||
8661 | |||||||||||
8662 | case NestedNameSpecifier::Namespace: | ||||||||||
8663 | Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc, | ||||||||||
8664 | ToLocalEndLoc); | ||||||||||
8665 | break; | ||||||||||
8666 | |||||||||||
8667 | case NestedNameSpecifier::NamespaceAlias: | ||||||||||
8668 | Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(), | ||||||||||
8669 | ToLocalBeginLoc, ToLocalEndLoc); | ||||||||||
8670 | break; | ||||||||||
8671 | |||||||||||
8672 | case NestedNameSpecifier::TypeSpec: | ||||||||||
8673 | case NestedNameSpecifier::TypeSpecWithTemplate: { | ||||||||||
8674 | SourceLocation ToTLoc; | ||||||||||
8675 | if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc())) | ||||||||||
8676 | return std::move(Err); | ||||||||||
8677 | TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( | ||||||||||
8678 | QualType(Spec->getAsType(), 0), ToTLoc); | ||||||||||
8679 | if (Kind == NestedNameSpecifier::TypeSpecWithTemplate) | ||||||||||
8680 | // ToLocalBeginLoc is here the location of the 'template' keyword. | ||||||||||
8681 | Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(), | ||||||||||
8682 | ToLocalEndLoc); | ||||||||||
8683 | else | ||||||||||
8684 | // No location for 'template' keyword here. | ||||||||||
8685 | Builder.Extend(getToContext(), SourceLocation{}, TSI->getTypeLoc(), | ||||||||||
8686 | ToLocalEndLoc); | ||||||||||
8687 | break; | ||||||||||
8688 | } | ||||||||||
8689 | |||||||||||
8690 | case NestedNameSpecifier::Global: | ||||||||||
8691 | Builder.MakeGlobal(getToContext(), ToLocalBeginLoc); | ||||||||||
8692 | break; | ||||||||||
8693 | |||||||||||
8694 | case NestedNameSpecifier::Super: { | ||||||||||
8695 | auto ToSourceRangeOrErr = Import(NNS.getSourceRange()); | ||||||||||
8696 | if (!ToSourceRangeOrErr) | ||||||||||
8697 | return ToSourceRangeOrErr.takeError(); | ||||||||||
8698 | |||||||||||
8699 | Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(), | ||||||||||
8700 | ToSourceRangeOrErr->getBegin(), | ||||||||||
8701 | ToSourceRangeOrErr->getEnd()); | ||||||||||
8702 | } | ||||||||||
8703 | } | ||||||||||
8704 | } | ||||||||||
8705 | |||||||||||
8706 | return Builder.getWithLocInContext(getToContext()); | ||||||||||
8707 | } | ||||||||||
8708 | |||||||||||
8709 | Expected<TemplateName> ASTImporter::Import(TemplateName From) { | ||||||||||
8710 | switch (From.getKind()) { | ||||||||||
8711 | case TemplateName::Template: | ||||||||||
8712 | if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl())) | ||||||||||
8713 | return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr)); | ||||||||||
8714 | else | ||||||||||
8715 | return ToTemplateOrErr.takeError(); | ||||||||||
8716 | |||||||||||
8717 | case TemplateName::OverloadedTemplate: { | ||||||||||
8718 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); | ||||||||||
8719 | UnresolvedSet<2> ToTemplates; | ||||||||||
8720 | for (auto *I : *FromStorage) { | ||||||||||
8721 | if (auto ToOrErr = Import(I)) | ||||||||||
8722 | ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr)); | ||||||||||
8723 | else | ||||||||||
8724 | return ToOrErr.takeError(); | ||||||||||
8725 | } | ||||||||||
8726 | return ToContext.getOverloadedTemplateName(ToTemplates.begin(), | ||||||||||
8727 | ToTemplates.end()); | ||||||||||
8728 | } | ||||||||||
8729 | |||||||||||
8730 | case TemplateName::AssumedTemplate: { | ||||||||||
8731 | AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName(); | ||||||||||
8732 | auto DeclNameOrErr = Import(FromStorage->getDeclName()); | ||||||||||
8733 | if (!DeclNameOrErr) | ||||||||||
8734 | return DeclNameOrErr.takeError(); | ||||||||||
8735 | return ToContext.getAssumedTemplateName(*DeclNameOrErr); | ||||||||||
8736 | } | ||||||||||
8737 | |||||||||||
8738 | case TemplateName::QualifiedTemplate: { | ||||||||||
8739 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); | ||||||||||
8740 | auto QualifierOrErr = Import(QTN->getQualifier()); | ||||||||||
8741 | if (!QualifierOrErr) | ||||||||||
8742 | return QualifierOrErr.takeError(); | ||||||||||
8743 | |||||||||||
8744 | if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl())) | ||||||||||
8745 | return ToContext.getQualifiedTemplateName( | ||||||||||
8746 | *QualifierOrErr, QTN->hasTemplateKeyword(), | ||||||||||
8747 | cast<TemplateDecl>(*ToTemplateOrErr)); | ||||||||||
8748 | else | ||||||||||
8749 | return ToTemplateOrErr.takeError(); | ||||||||||
8750 | } | ||||||||||
8751 | |||||||||||
8752 | case TemplateName::DependentTemplate: { | ||||||||||
8753 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); | ||||||||||
8754 | auto QualifierOrErr = Import(DTN->getQualifier()); | ||||||||||
8755 | if (!QualifierOrErr) | ||||||||||
8756 | return QualifierOrErr.takeError(); | ||||||||||
8757 | |||||||||||
8758 | if (DTN->isIdentifier()) { | ||||||||||
8759 | return ToContext.getDependentTemplateName(*QualifierOrErr, | ||||||||||
8760 | Import(DTN->getIdentifier())); | ||||||||||
8761 | } | ||||||||||
8762 | |||||||||||
8763 | return ToContext.getDependentTemplateName(*QualifierOrErr, | ||||||||||
8764 | DTN->getOperator()); | ||||||||||
8765 | } | ||||||||||
8766 | |||||||||||
8767 | case TemplateName::SubstTemplateTemplateParm: { | ||||||||||
8768 | SubstTemplateTemplateParmStorage *Subst = | ||||||||||
8769 | From.getAsSubstTemplateTemplateParm(); | ||||||||||
8770 | ExpectedDecl ParamOrErr = Import(Subst->getParameter()); | ||||||||||
8771 | if (!ParamOrErr) | ||||||||||
8772 | return ParamOrErr.takeError(); | ||||||||||
8773 | |||||||||||
8774 | auto ReplacementOrErr = Import(Subst->getReplacement()); | ||||||||||
8775 | if (!ReplacementOrErr) | ||||||||||
8776 | return ReplacementOrErr.takeError(); | ||||||||||
8777 | |||||||||||
8778 | return ToContext.getSubstTemplateTemplateParm( | ||||||||||
8779 | cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr); | ||||||||||
8780 | } | ||||||||||
8781 | |||||||||||
8782 | case TemplateName::SubstTemplateTemplateParmPack: { | ||||||||||
8783 | SubstTemplateTemplateParmPackStorage *SubstPack | ||||||||||
8784 | = From.getAsSubstTemplateTemplateParmPack(); | ||||||||||
8785 | ExpectedDecl ParamOrErr = Import(SubstPack->getParameterPack()); | ||||||||||
8786 | if (!ParamOrErr) | ||||||||||
8787 | return ParamOrErr.takeError(); | ||||||||||
8788 | |||||||||||
8789 | ASTNodeImporter Importer(*this); | ||||||||||
8790 | auto ArgPackOrErr = | ||||||||||
8791 | Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); | ||||||||||
8792 | if (!ArgPackOrErr) | ||||||||||
8793 | return ArgPackOrErr.takeError(); | ||||||||||
8794 | |||||||||||
8795 | return ToContext.getSubstTemplateTemplateParmPack( | ||||||||||
8796 | cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr); | ||||||||||
8797 | } | ||||||||||
8798 | } | ||||||||||
8799 | |||||||||||
8800 | llvm_unreachable("Invalid template name kind")::llvm::llvm_unreachable_internal("Invalid template name kind" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8800); | ||||||||||
8801 | } | ||||||||||
8802 | |||||||||||
8803 | Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) { | ||||||||||
8804 | if (FromLoc.isInvalid()) | ||||||||||
8805 | return SourceLocation{}; | ||||||||||
8806 | |||||||||||
8807 | SourceManager &FromSM = FromContext.getSourceManager(); | ||||||||||
8808 | bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc); | ||||||||||
8809 | |||||||||||
8810 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); | ||||||||||
8811 | Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin); | ||||||||||
8812 | if (!ToFileIDOrErr) | ||||||||||
8813 | return ToFileIDOrErr.takeError(); | ||||||||||
8814 | SourceManager &ToSM = ToContext.getSourceManager(); | ||||||||||
8815 | return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second); | ||||||||||
8816 | } | ||||||||||
8817 | |||||||||||
8818 | Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) { | ||||||||||
8819 | SourceLocation ToBegin, ToEnd; | ||||||||||
8820 | if (Error Err = importInto(ToBegin, FromRange.getBegin())) | ||||||||||
8821 | return std::move(Err); | ||||||||||
8822 | if (Error Err = importInto(ToEnd, FromRange.getEnd())) | ||||||||||
8823 | return std::move(Err); | ||||||||||
8824 | |||||||||||
8825 | return SourceRange(ToBegin, ToEnd); | ||||||||||
8826 | } | ||||||||||
8827 | |||||||||||
8828 | Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) { | ||||||||||
8829 | llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID); | ||||||||||
8830 | if (Pos != ImportedFileIDs.end()) | ||||||||||
8831 | return Pos->second; | ||||||||||
8832 | |||||||||||
8833 | SourceManager &FromSM = FromContext.getSourceManager(); | ||||||||||
8834 | SourceManager &ToSM = ToContext.getSourceManager(); | ||||||||||
8835 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); | ||||||||||
8836 | |||||||||||
8837 | // Map the FromID to the "to" source manager. | ||||||||||
8838 | FileID ToID; | ||||||||||
8839 | if (FromSLoc.isExpansion()) { | ||||||||||
8840 | const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion(); | ||||||||||
8841 | ExpectedSLoc ToSpLoc = Import(FromEx.getSpellingLoc()); | ||||||||||
8842 | if (!ToSpLoc) | ||||||||||
8843 | return ToSpLoc.takeError(); | ||||||||||
8844 | ExpectedSLoc ToExLocS = Import(FromEx.getExpansionLocStart()); | ||||||||||
8845 | if (!ToExLocS) | ||||||||||
8846 | return ToExLocS.takeError(); | ||||||||||
8847 | unsigned TokenLen = FromSM.getFileIDSize(FromID); | ||||||||||
8848 | SourceLocation MLoc; | ||||||||||
8849 | if (FromEx.isMacroArgExpansion()) { | ||||||||||
8850 | MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen); | ||||||||||
8851 | } else { | ||||||||||
8852 | if (ExpectedSLoc ToExLocE = Import(FromEx.getExpansionLocEnd())) | ||||||||||
8853 | MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen, | ||||||||||
8854 | FromEx.isExpansionTokenRange()); | ||||||||||
8855 | else | ||||||||||
8856 | return ToExLocE.takeError(); | ||||||||||
8857 | } | ||||||||||
8858 | ToID = ToSM.getFileID(MLoc); | ||||||||||
8859 | } else { | ||||||||||
8860 | const SrcMgr::ContentCache *Cache = &FromSLoc.getFile().getContentCache(); | ||||||||||
8861 | |||||||||||
8862 | if (!IsBuiltin && !Cache->BufferOverridden) { | ||||||||||
8863 | // Include location of this file. | ||||||||||
8864 | ExpectedSLoc ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); | ||||||||||
8865 | if (!ToIncludeLoc) | ||||||||||
8866 | return ToIncludeLoc.takeError(); | ||||||||||
8867 | |||||||||||
8868 | // Every FileID that is not the main FileID needs to have a valid include | ||||||||||
8869 | // location so that the include chain points to the main FileID. When | ||||||||||
8870 | // importing the main FileID (which has no include location), we need to | ||||||||||
8871 | // create a fake include location in the main file to keep this property | ||||||||||
8872 | // intact. | ||||||||||
8873 | SourceLocation ToIncludeLocOrFakeLoc = *ToIncludeLoc; | ||||||||||
8874 | if (FromID == FromSM.getMainFileID()) | ||||||||||
8875 | ToIncludeLocOrFakeLoc = ToSM.getLocForStartOfFile(ToSM.getMainFileID()); | ||||||||||
8876 | |||||||||||
8877 | if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { | ||||||||||
8878 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the | ||||||||||
8879 | // disk again | ||||||||||
8880 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather | ||||||||||
8881 | // than mmap the files several times. | ||||||||||
8882 | auto Entry = | ||||||||||
8883 | ToFileManager.getOptionalFileRef(Cache->OrigEntry->getName()); | ||||||||||
8884 | // FIXME: The filename may be a virtual name that does probably not | ||||||||||
8885 | // point to a valid file and we get no Entry here. In this case try with | ||||||||||
8886 | // the memory buffer below. | ||||||||||
8887 | if (Entry) | ||||||||||
8888 | ToID = ToSM.createFileID(*Entry, ToIncludeLocOrFakeLoc, | ||||||||||
8889 | FromSLoc.getFile().getFileCharacteristic()); | ||||||||||
8890 | } | ||||||||||
8891 | } | ||||||||||
8892 | |||||||||||
8893 | if (ToID.isInvalid() || IsBuiltin) { | ||||||||||
8894 | // FIXME: We want to re-use the existing MemoryBuffer! | ||||||||||
8895 | llvm::Optional<llvm::MemoryBufferRef> FromBuf = | ||||||||||
8896 | Cache->getBufferOrNone(FromContext.getDiagnostics(), | ||||||||||
8897 | FromSM.getFileManager(), SourceLocation{}); | ||||||||||
8898 | if (!FromBuf) | ||||||||||
8899 | return llvm::make_error<ImportError>(ImportError::Unknown); | ||||||||||
8900 | |||||||||||
8901 | std::unique_ptr<llvm::MemoryBuffer> ToBuf = | ||||||||||
8902 | llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), | ||||||||||
8903 | FromBuf->getBufferIdentifier()); | ||||||||||
8904 | ToID = ToSM.createFileID(std::move(ToBuf), | ||||||||||
8905 | FromSLoc.getFile().getFileCharacteristic()); | ||||||||||
8906 | } | ||||||||||
8907 | } | ||||||||||
8908 | |||||||||||
8909 | assert(ToID.isValid() && "Unexpected invalid fileID was created.")((ToID.isValid() && "Unexpected invalid fileID was created." ) ? static_cast<void> (0) : __assert_fail ("ToID.isValid() && \"Unexpected invalid fileID was created.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 8909, __PRETTY_FUNCTION__)); | ||||||||||
8910 | |||||||||||
8911 | ImportedFileIDs[FromID] = ToID; | ||||||||||
8912 | return ToID; | ||||||||||
8913 | } | ||||||||||
8914 | |||||||||||
8915 | Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) { | ||||||||||
8916 | ExpectedExpr ToExprOrErr = Import(From->getInit()); | ||||||||||
8917 | if (!ToExprOrErr) | ||||||||||
8918 | return ToExprOrErr.takeError(); | ||||||||||
8919 | |||||||||||
8920 | auto LParenLocOrErr = Import(From->getLParenLoc()); | ||||||||||
8921 | if (!LParenLocOrErr) | ||||||||||
8922 | return LParenLocOrErr.takeError(); | ||||||||||
8923 | |||||||||||
8924 | auto RParenLocOrErr = Import(From->getRParenLoc()); | ||||||||||
8925 | if (!RParenLocOrErr) | ||||||||||
8926 | return RParenLocOrErr.takeError(); | ||||||||||
8927 | |||||||||||
8928 | if (From->isBaseInitializer()) { | ||||||||||
8929 | auto ToTInfoOrErr = Import(From->getTypeSourceInfo()); | ||||||||||
8930 | if (!ToTInfoOrErr) | ||||||||||
8931 | return ToTInfoOrErr.takeError(); | ||||||||||
8932 | |||||||||||
8933 | SourceLocation EllipsisLoc; | ||||||||||
8934 | if (From->isPackExpansion()) | ||||||||||
8935 | if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc())) | ||||||||||
8936 | return std::move(Err); | ||||||||||
8937 | |||||||||||
8938 | return new (ToContext) CXXCtorInitializer( | ||||||||||
8939 | ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr, | ||||||||||
8940 | *ToExprOrErr, *RParenLocOrErr, EllipsisLoc); | ||||||||||
8941 | } else if (From->isMemberInitializer()) { | ||||||||||
8942 | ExpectedDecl ToFieldOrErr = Import(From->getMember()); | ||||||||||
8943 | if (!ToFieldOrErr) | ||||||||||
8944 | return ToFieldOrErr.takeError(); | ||||||||||
8945 | |||||||||||
8946 | auto MemberLocOrErr = Import(From->getMemberLocation()); | ||||||||||
8947 | if (!MemberLocOrErr) | ||||||||||
8948 | return MemberLocOrErr.takeError(); | ||||||||||
8949 | |||||||||||
8950 | return new (ToContext) CXXCtorInitializer( | ||||||||||
8951 | ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr, | ||||||||||
8952 | *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); | ||||||||||
8953 | } else if (From->isIndirectMemberInitializer()) { | ||||||||||
8954 | ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember()); | ||||||||||
8955 | if (!ToIFieldOrErr) | ||||||||||
8956 | return ToIFieldOrErr.takeError(); | ||||||||||
8957 | |||||||||||
8958 | auto MemberLocOrErr = Import(From->getMemberLocation()); | ||||||||||
8959 | if (!MemberLocOrErr) | ||||||||||
8960 | return MemberLocOrErr.takeError(); | ||||||||||
8961 | |||||||||||
8962 | return new (ToContext) CXXCtorInitializer( | ||||||||||
8963 | ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr), | ||||||||||
8964 | *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); | ||||||||||
8965 | } else if (From->isDelegatingInitializer()) { | ||||||||||
8966 | auto ToTInfoOrErr = Import(From->getTypeSourceInfo()); | ||||||||||
8967 | if (!ToTInfoOrErr) | ||||||||||
8968 | return ToTInfoOrErr.takeError(); | ||||||||||
8969 | |||||||||||
8970 | return new (ToContext) | ||||||||||
8971 | CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr, | ||||||||||
8972 | *ToExprOrErr, *RParenLocOrErr); | ||||||||||
8973 | } else { | ||||||||||
8974 | // FIXME: assert? | ||||||||||
8975 | return make_error<ImportError>(); | ||||||||||
8976 | } | ||||||||||
8977 | } | ||||||||||
8978 | |||||||||||
8979 | Expected<CXXBaseSpecifier *> | ||||||||||
8980 | ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) { | ||||||||||
8981 | auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec); | ||||||||||
8982 | if (Pos != ImportedCXXBaseSpecifiers.end()) | ||||||||||
8983 | return Pos->second; | ||||||||||
8984 | |||||||||||
8985 | Expected<SourceRange> ToSourceRange = Import(BaseSpec->getSourceRange()); | ||||||||||
8986 | if (!ToSourceRange) | ||||||||||
8987 | return ToSourceRange.takeError(); | ||||||||||
8988 | Expected<TypeSourceInfo *> ToTSI = Import(BaseSpec->getTypeSourceInfo()); | ||||||||||
8989 | if (!ToTSI) | ||||||||||
8990 | return ToTSI.takeError(); | ||||||||||
8991 | ExpectedSLoc ToEllipsisLoc = Import(BaseSpec->getEllipsisLoc()); | ||||||||||
8992 | if (!ToEllipsisLoc) | ||||||||||
8993 | return ToEllipsisLoc.takeError(); | ||||||||||
8994 | CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier( | ||||||||||
8995 | *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), | ||||||||||
8996 | BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc); | ||||||||||
8997 | ImportedCXXBaseSpecifiers[BaseSpec] = Imported; | ||||||||||
8998 | return Imported; | ||||||||||
8999 | } | ||||||||||
9000 | |||||||||||
9001 | llvm::Expected<APValue> ASTImporter::Import(const APValue &FromValue) { | ||||||||||
9002 | ASTNodeImporter Importer(*this); | ||||||||||
9003 | return Importer.ImportAPValue(FromValue); | ||||||||||
9004 | } | ||||||||||
9005 | |||||||||||
9006 | Error ASTImporter::ImportDefinition(Decl *From) { | ||||||||||
9007 | ExpectedDecl ToOrErr = Import(From); | ||||||||||
9008 | if (!ToOrErr) | ||||||||||
9009 | return ToOrErr.takeError(); | ||||||||||
9010 | Decl *To = *ToOrErr; | ||||||||||
9011 | |||||||||||
9012 | auto *FromDC = cast<DeclContext>(From); | ||||||||||
9013 | ASTNodeImporter Importer(*this); | ||||||||||
9014 | |||||||||||
9015 | if (auto *ToRecord = dyn_cast<RecordDecl>(To)) { | ||||||||||
9016 | if (!ToRecord->getDefinition()) { | ||||||||||
9017 | return Importer.ImportDefinition( | ||||||||||
9018 | cast<RecordDecl>(FromDC), ToRecord, | ||||||||||
9019 | ASTNodeImporter::IDK_Everything); | ||||||||||
9020 | } | ||||||||||
9021 | } | ||||||||||
9022 | |||||||||||
9023 | if (auto *ToEnum = dyn_cast<EnumDecl>(To)) { | ||||||||||
9024 | if (!ToEnum->getDefinition()) { | ||||||||||
9025 | return Importer.ImportDefinition( | ||||||||||
9026 | cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything); | ||||||||||
9027 | } | ||||||||||
9028 | } | ||||||||||
9029 | |||||||||||
9030 | if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { | ||||||||||
9031 | if (!ToIFace->getDefinition()) { | ||||||||||
9032 | return Importer.ImportDefinition( | ||||||||||
9033 | cast<ObjCInterfaceDecl>(FromDC), ToIFace, | ||||||||||
9034 | ASTNodeImporter::IDK_Everything); | ||||||||||
9035 | } | ||||||||||
9036 | } | ||||||||||
9037 | |||||||||||
9038 | if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { | ||||||||||
9039 | if (!ToProto->getDefinition()) { | ||||||||||
9040 | return Importer.ImportDefinition( | ||||||||||
9041 | cast<ObjCProtocolDecl>(FromDC), ToProto, | ||||||||||
9042 | ASTNodeImporter::IDK_Everything); | ||||||||||
9043 | } | ||||||||||
9044 | } | ||||||||||
9045 | |||||||||||
9046 | return Importer.ImportDeclContext(FromDC, true); | ||||||||||
9047 | } | ||||||||||
9048 | |||||||||||
9049 | Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) { | ||||||||||
9050 | if (!FromName) | ||||||||||
9051 | return DeclarationName{}; | ||||||||||
9052 | |||||||||||
9053 | switch (FromName.getNameKind()) { | ||||||||||
9054 | case DeclarationName::Identifier: | ||||||||||
9055 | return DeclarationName(Import(FromName.getAsIdentifierInfo())); | ||||||||||
9056 | |||||||||||
9057 | case DeclarationName::ObjCZeroArgSelector: | ||||||||||
9058 | case DeclarationName::ObjCOneArgSelector: | ||||||||||
9059 | case DeclarationName::ObjCMultiArgSelector: | ||||||||||
9060 | if (auto ToSelOrErr = Import(FromName.getObjCSelector())) | ||||||||||
9061 | return DeclarationName(*ToSelOrErr); | ||||||||||
9062 | else | ||||||||||
9063 | return ToSelOrErr.takeError(); | ||||||||||
9064 | |||||||||||
9065 | case DeclarationName::CXXConstructorName: { | ||||||||||
9066 | if (auto ToTyOrErr = Import(FromName.getCXXNameType())) | ||||||||||
9067 | return ToContext.DeclarationNames.getCXXConstructorName( | ||||||||||
9068 | ToContext.getCanonicalType(*ToTyOrErr)); | ||||||||||
9069 | else | ||||||||||
9070 | return ToTyOrErr.takeError(); | ||||||||||
9071 | } | ||||||||||
9072 | |||||||||||
9073 | case DeclarationName::CXXDestructorName: { | ||||||||||
9074 | if (auto ToTyOrErr = Import(FromName.getCXXNameType())) | ||||||||||
9075 | return ToContext.DeclarationNames.getCXXDestructorName( | ||||||||||
9076 | ToContext.getCanonicalType(*ToTyOrErr)); | ||||||||||
9077 | else | ||||||||||
9078 | return ToTyOrErr.takeError(); | ||||||||||
9079 | } | ||||||||||
9080 | |||||||||||
9081 | case DeclarationName::CXXDeductionGuideName: { | ||||||||||
9082 | if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate())) | ||||||||||
9083 | return ToContext.DeclarationNames.getCXXDeductionGuideName( | ||||||||||
9084 | cast<TemplateDecl>(*ToTemplateOrErr)); | ||||||||||
9085 | else | ||||||||||
9086 | return ToTemplateOrErr.takeError(); | ||||||||||
9087 | } | ||||||||||
9088 | |||||||||||
9089 | case DeclarationName::CXXConversionFunctionName: { | ||||||||||
9090 | if (auto ToTyOrErr = Import(FromName.getCXXNameType())) | ||||||||||
9091 | return ToContext.DeclarationNames.getCXXConversionFunctionName( | ||||||||||
9092 | ToContext.getCanonicalType(*ToTyOrErr)); | ||||||||||
9093 | else | ||||||||||
9094 | return ToTyOrErr.takeError(); | ||||||||||
9095 | } | ||||||||||
9096 | |||||||||||
9097 | case DeclarationName::CXXOperatorName: | ||||||||||
9098 | return ToContext.DeclarationNames.getCXXOperatorName( | ||||||||||
9099 | FromName.getCXXOverloadedOperator()); | ||||||||||
9100 | |||||||||||
9101 | case DeclarationName::CXXLiteralOperatorName: | ||||||||||
9102 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( | ||||||||||
9103 | Import(FromName.getCXXLiteralIdentifier())); | ||||||||||
9104 | |||||||||||
9105 | case DeclarationName::CXXUsingDirective: | ||||||||||
9106 | // FIXME: STATICS! | ||||||||||
9107 | return DeclarationName::getUsingDirectiveName(); | ||||||||||
9108 | } | ||||||||||
9109 | |||||||||||
9110 | llvm_unreachable("Invalid DeclarationName Kind!")::llvm::llvm_unreachable_internal("Invalid DeclarationName Kind!" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 9110); | ||||||||||
9111 | } | ||||||||||
9112 | |||||||||||
9113 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { | ||||||||||
9114 | if (!FromId) | ||||||||||
9115 | return nullptr; | ||||||||||
9116 | |||||||||||
9117 | IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName()); | ||||||||||
9118 | |||||||||||
9119 | if (!ToId->getBuiltinID() && FromId->getBuiltinID()) | ||||||||||
9120 | ToId->setBuiltinID(FromId->getBuiltinID()); | ||||||||||
9121 | |||||||||||
9122 | return ToId; | ||||||||||
9123 | } | ||||||||||
9124 | |||||||||||
9125 | Expected<Selector> ASTImporter::Import(Selector FromSel) { | ||||||||||
9126 | if (FromSel.isNull()) | ||||||||||
9127 | return Selector{}; | ||||||||||
9128 | |||||||||||
9129 | SmallVector<IdentifierInfo *, 4> Idents; | ||||||||||
9130 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); | ||||||||||
9131 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) | ||||||||||
9132 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); | ||||||||||
9133 | return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); | ||||||||||
9134 | } | ||||||||||
9135 | |||||||||||
9136 | llvm::Expected<APValue> | ||||||||||
9137 | ASTNodeImporter::ImportAPValue(const APValue &FromValue) { | ||||||||||
9138 | APValue Result; | ||||||||||
9139 | llvm::Error Err = llvm::Error::success(); | ||||||||||
9140 | auto ImportLoop = [&](const APValue *From, APValue *To, unsigned Size) { | ||||||||||
9141 | for (unsigned Idx = 0; Idx < Size; Idx++) { | ||||||||||
9142 | APValue Tmp = importChecked(Err, From[Idx]); | ||||||||||
9143 | To[Idx] = Tmp; | ||||||||||
9144 | } | ||||||||||
9145 | }; | ||||||||||
9146 | switch (FromValue.getKind()) { | ||||||||||
9147 | case APValue::None: | ||||||||||
9148 | case APValue::Indeterminate: | ||||||||||
9149 | case APValue::Int: | ||||||||||
9150 | case APValue::Float: | ||||||||||
9151 | case APValue::FixedPoint: | ||||||||||
9152 | case APValue::ComplexInt: | ||||||||||
9153 | case APValue::ComplexFloat: | ||||||||||
9154 | Result = FromValue; | ||||||||||
9155 | break; | ||||||||||
9156 | case APValue::Vector: { | ||||||||||
9157 | Result.MakeVector(); | ||||||||||
9158 | MutableArrayRef<APValue> Elts = | ||||||||||
9159 | Result.setVectorUninit(FromValue.getVectorLength()); | ||||||||||
9160 | ImportLoop(((const APValue::Vec *)(const char *)&FromValue.Data)->Elts, | ||||||||||
9161 | Elts.data(), FromValue.getVectorLength()); | ||||||||||
9162 | break; | ||||||||||
9163 | } | ||||||||||
9164 | case APValue::Array: | ||||||||||
9165 | Result.MakeArray(FromValue.getArrayInitializedElts(), | ||||||||||
9166 | FromValue.getArraySize()); | ||||||||||
9167 | ImportLoop(((const APValue::Arr *)(const char *)&FromValue.Data)->Elts, | ||||||||||
9168 | ((const APValue::Arr *)(const char *)&Result.Data)->Elts, | ||||||||||
9169 | FromValue.getArrayInitializedElts()); | ||||||||||
9170 | break; | ||||||||||
9171 | case APValue::Struct: | ||||||||||
9172 | Result.MakeStruct(FromValue.getStructNumBases(), | ||||||||||
9173 | FromValue.getStructNumFields()); | ||||||||||
9174 | ImportLoop( | ||||||||||
9175 | ((const APValue::StructData *)(const char *)&FromValue.Data)->Elts, | ||||||||||
9176 | ((const APValue::StructData *)(const char *)&Result.Data)->Elts, | ||||||||||
9177 | FromValue.getStructNumBases() + FromValue.getStructNumFields()); | ||||||||||
9178 | break; | ||||||||||
9179 | case APValue::Union: { | ||||||||||
9180 | Result.MakeUnion(); | ||||||||||
9181 | const Decl *ImpFDecl = importChecked(Err, FromValue.getUnionField()); | ||||||||||
9182 | APValue ImpValue = importChecked(Err, FromValue.getUnionValue()); | ||||||||||
9183 | if (Err) | ||||||||||
9184 | return std::move(Err); | ||||||||||
9185 | Result.setUnion(cast<FieldDecl>(ImpFDecl), ImpValue); | ||||||||||
9186 | break; | ||||||||||
9187 | } | ||||||||||
9188 | case APValue::AddrLabelDiff: { | ||||||||||
9189 | Result.MakeAddrLabelDiff(); | ||||||||||
9190 | const Expr *ImpLHS = importChecked(Err, FromValue.getAddrLabelDiffLHS()); | ||||||||||
9191 | const Expr *ImpRHS = importChecked(Err, FromValue.getAddrLabelDiffRHS()); | ||||||||||
9192 | if (Err) | ||||||||||
9193 | return std::move(Err); | ||||||||||
9194 | Result.setAddrLabelDiff(cast<AddrLabelExpr>(ImpLHS), | ||||||||||
9195 | cast<AddrLabelExpr>(ImpRHS)); | ||||||||||
9196 | break; | ||||||||||
9197 | } | ||||||||||
9198 | case APValue::MemberPointer: { | ||||||||||
9199 | const Decl *ImpMemPtrDecl = | ||||||||||
9200 | importChecked(Err, FromValue.getMemberPointerDecl()); | ||||||||||
9201 | if (Err) | ||||||||||
9202 | return std::move(Err); | ||||||||||
9203 | MutableArrayRef<const CXXRecordDecl *> ToPath = | ||||||||||
9204 | Result.setMemberPointerUninit( | ||||||||||
9205 | cast<const ValueDecl>(ImpMemPtrDecl), | ||||||||||
9206 | FromValue.isMemberPointerToDerivedMember(), | ||||||||||
9207 | FromValue.getMemberPointerPath().size()); | ||||||||||
9208 | llvm::ArrayRef<const CXXRecordDecl *> FromPath = | ||||||||||
9209 | Result.getMemberPointerPath(); | ||||||||||
9210 | for (unsigned Idx = 0; Idx < FromValue.getMemberPointerPath().size(); | ||||||||||
9211 | Idx++) { | ||||||||||
9212 | const Decl *ImpDecl = importChecked(Err, FromPath[Idx]); | ||||||||||
9213 | if (Err) | ||||||||||
9214 | return std::move(Err); | ||||||||||
9215 | ToPath[Idx] = cast<const CXXRecordDecl>(ImpDecl->getCanonicalDecl()); | ||||||||||
9216 | } | ||||||||||
9217 | break; | ||||||||||
9218 | } | ||||||||||
9219 | case APValue::LValue: | ||||||||||
9220 | APValue::LValueBase Base; | ||||||||||
9221 | QualType FromElemTy; | ||||||||||
9222 | if (FromValue.getLValueBase()) { | ||||||||||
9223 | assert(!FromValue.getLValueBase().is<DynamicAllocLValue>() &&((!FromValue.getLValueBase().is<DynamicAllocLValue>() && "in C++20 dynamic allocation are transient so they shouldn't " "appear in the AST") ? static_cast<void> (0) : __assert_fail ("!FromValue.getLValueBase().is<DynamicAllocLValue>() && \"in C++20 dynamic allocation are transient so they shouldn't \" \"appear in the AST\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 9225, __PRETTY_FUNCTION__)) | ||||||||||
9224 | "in C++20 dynamic allocation are transient so they shouldn't "((!FromValue.getLValueBase().is<DynamicAllocLValue>() && "in C++20 dynamic allocation are transient so they shouldn't " "appear in the AST") ? static_cast<void> (0) : __assert_fail ("!FromValue.getLValueBase().is<DynamicAllocLValue>() && \"in C++20 dynamic allocation are transient so they shouldn't \" \"appear in the AST\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 9225, __PRETTY_FUNCTION__)) | ||||||||||
9225 | "appear in the AST")((!FromValue.getLValueBase().is<DynamicAllocLValue>() && "in C++20 dynamic allocation are transient so they shouldn't " "appear in the AST") ? static_cast<void> (0) : __assert_fail ("!FromValue.getLValueBase().is<DynamicAllocLValue>() && \"in C++20 dynamic allocation are transient so they shouldn't \" \"appear in the AST\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 9225, __PRETTY_FUNCTION__)); | ||||||||||
9226 | if (!FromValue.getLValueBase().is<TypeInfoLValue>()) { | ||||||||||
9227 | if (const auto *E = | ||||||||||
9228 | FromValue.getLValueBase().dyn_cast<const Expr *>()) { | ||||||||||
9229 | FromElemTy = E->getType(); | ||||||||||
9230 | const Expr *ImpExpr = importChecked(Err, E); | ||||||||||
9231 | if (Err) | ||||||||||
9232 | return std::move(Err); | ||||||||||
9233 | Base = APValue::LValueBase(ImpExpr, | ||||||||||
9234 | FromValue.getLValueBase().getCallIndex(), | ||||||||||
9235 | FromValue.getLValueBase().getVersion()); | ||||||||||
9236 | } else { | ||||||||||
9237 | FromElemTy = | ||||||||||
9238 | FromValue.getLValueBase().get<const ValueDecl *>()->getType(); | ||||||||||
9239 | const Decl *ImpDecl = importChecked( | ||||||||||
9240 | Err, FromValue.getLValueBase().get<const ValueDecl *>()); | ||||||||||
9241 | if (Err) | ||||||||||
9242 | return std::move(Err); | ||||||||||
9243 | Base = APValue::LValueBase(cast<ValueDecl>(ImpDecl), | ||||||||||
9244 | FromValue.getLValueBase().getCallIndex(), | ||||||||||
9245 | FromValue.getLValueBase().getVersion()); | ||||||||||
9246 | } | ||||||||||
9247 | } else { | ||||||||||
9248 | FromElemTy = FromValue.getLValueBase().getTypeInfoType(); | ||||||||||
9249 | QualType ImpTypeInfo = importChecked( | ||||||||||
9250 | Err, | ||||||||||
9251 | QualType(FromValue.getLValueBase().get<TypeInfoLValue>().getType(), | ||||||||||
9252 | 0)); | ||||||||||
9253 | QualType ImpType = | ||||||||||
9254 | importChecked(Err, FromValue.getLValueBase().getTypeInfoType()); | ||||||||||
9255 | if (Err) | ||||||||||
9256 | return std::move(Err); | ||||||||||
9257 | Base = APValue::LValueBase::getTypeInfo( | ||||||||||
9258 | TypeInfoLValue(ImpTypeInfo.getTypePtr()), ImpType); | ||||||||||
9259 | } | ||||||||||
9260 | } | ||||||||||
9261 | CharUnits Offset = FromValue.getLValueOffset(); | ||||||||||
9262 | unsigned PathLength = FromValue.getLValuePath().size(); | ||||||||||
9263 | Result.MakeLValue(); | ||||||||||
9264 | if (FromValue.hasLValuePath()) { | ||||||||||
9265 | MutableArrayRef<APValue::LValuePathEntry> ToPath = Result.setLValueUninit( | ||||||||||
9266 | Base, Offset, PathLength, FromValue.isLValueOnePastTheEnd(), | ||||||||||
9267 | FromValue.isNullPointer()); | ||||||||||
9268 | llvm::ArrayRef<APValue::LValuePathEntry> FromPath = | ||||||||||
9269 | FromValue.getLValuePath(); | ||||||||||
9270 | for (unsigned LoopIdx = 0; LoopIdx < PathLength; LoopIdx++) { | ||||||||||
9271 | if (FromElemTy->isRecordType()) { | ||||||||||
9272 | const Decl *FromDecl = | ||||||||||
9273 | FromPath[LoopIdx].getAsBaseOrMember().getPointer(); | ||||||||||
9274 | const Decl *ImpDecl = importChecked(Err, FromDecl); | ||||||||||
9275 | if (Err) | ||||||||||
9276 | return std::move(Err); | ||||||||||
9277 | if (auto *RD = dyn_cast<CXXRecordDecl>(FromDecl)) | ||||||||||
9278 | FromElemTy = Importer.FromContext.getRecordType(RD); | ||||||||||
9279 | else | ||||||||||
9280 | FromElemTy = cast<ValueDecl>(FromDecl)->getType(); | ||||||||||
9281 | ToPath[LoopIdx] = APValue::LValuePathEntry(APValue::BaseOrMemberType( | ||||||||||
9282 | ImpDecl, FromPath[LoopIdx].getAsBaseOrMember().getInt())); | ||||||||||
9283 | } else { | ||||||||||
9284 | FromElemTy = | ||||||||||
9285 | Importer.FromContext.getAsArrayType(FromElemTy)->getElementType(); | ||||||||||
9286 | ToPath[LoopIdx] = APValue::LValuePathEntry::ArrayIndex( | ||||||||||
9287 | FromPath[LoopIdx].getAsArrayIndex()); | ||||||||||
9288 | } | ||||||||||
9289 | } | ||||||||||
9290 | } else | ||||||||||
9291 | Result.setLValue(Base, Offset, APValue::NoLValuePath{}, | ||||||||||
9292 | FromValue.isNullPointer()); | ||||||||||
9293 | } | ||||||||||
9294 | if (Err) | ||||||||||
9295 | return std::move(Err); | ||||||||||
9296 | return Result; | ||||||||||
9297 | } | ||||||||||
9298 | |||||||||||
9299 | Expected<DeclarationName> ASTImporter::HandleNameConflict(DeclarationName Name, | ||||||||||
9300 | DeclContext *DC, | ||||||||||
9301 | unsigned IDNS, | ||||||||||
9302 | NamedDecl **Decls, | ||||||||||
9303 | unsigned NumDecls) { | ||||||||||
9304 | if (ODRHandling == ODRHandlingType::Conservative) | ||||||||||
9305 | // Report error at any name conflict. | ||||||||||
9306 | return make_error<ImportError>(ImportError::NameConflict); | ||||||||||
9307 | else | ||||||||||
9308 | // Allow to create the new Decl with the same name. | ||||||||||
9309 | return Name; | ||||||||||
9310 | } | ||||||||||
9311 | |||||||||||
9312 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { | ||||||||||
9313 | if (LastDiagFromFrom) | ||||||||||
9314 | ToContext.getDiagnostics().notePriorDiagnosticFrom( | ||||||||||
9315 | FromContext.getDiagnostics()); | ||||||||||
9316 | LastDiagFromFrom = false; | ||||||||||
9317 | return ToContext.getDiagnostics().Report(Loc, DiagID); | ||||||||||
9318 | } | ||||||||||
9319 | |||||||||||
9320 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { | ||||||||||
9321 | if (!LastDiagFromFrom) | ||||||||||
9322 | FromContext.getDiagnostics().notePriorDiagnosticFrom( | ||||||||||
9323 | ToContext.getDiagnostics()); | ||||||||||
9324 | LastDiagFromFrom = true; | ||||||||||
9325 | return FromContext.getDiagnostics().Report(Loc, DiagID); | ||||||||||
9326 | } | ||||||||||
9327 | |||||||||||
9328 | void ASTImporter::CompleteDecl (Decl *D) { | ||||||||||
9329 | if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) { | ||||||||||
9330 | if (!ID->getDefinition()) | ||||||||||
9331 | ID->startDefinition(); | ||||||||||
9332 | } | ||||||||||
9333 | else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) { | ||||||||||
9334 | if (!PD->getDefinition()) | ||||||||||
9335 | PD->startDefinition(); | ||||||||||
9336 | } | ||||||||||
9337 | else if (auto *TD = dyn_cast<TagDecl>(D)) { | ||||||||||
9338 | if (!TD->getDefinition() && !TD->isBeingDefined()) { | ||||||||||
9339 | TD->startDefinition(); | ||||||||||
9340 | TD->setCompleteDefinition(true); | ||||||||||
9341 | } | ||||||||||
9342 | } | ||||||||||
9343 | else { | ||||||||||
9344 | assert(0 && "CompleteDecl called on a Decl that can't be completed")((0 && "CompleteDecl called on a Decl that can't be completed" ) ? static_cast<void> (0) : __assert_fail ("0 && \"CompleteDecl called on a Decl that can't be completed\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 9344, __PRETTY_FUNCTION__)); | ||||||||||
9345 | } | ||||||||||
9346 | } | ||||||||||
9347 | |||||||||||
9348 | Decl *ASTImporter::MapImported(Decl *From, Decl *To) { | ||||||||||
9349 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From); | ||||||||||
9350 | assert((Pos == ImportedDecls.end() || Pos->second == To) &&(((Pos == ImportedDecls.end() || Pos->second == To) && "Try to import an already imported Decl") ? static_cast<void > (0) : __assert_fail ("(Pos == ImportedDecls.end() || Pos->second == To) && \"Try to import an already imported Decl\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 9351, __PRETTY_FUNCTION__)) | ||||||||||
9351 | "Try to import an already imported Decl")(((Pos == ImportedDecls.end() || Pos->second == To) && "Try to import an already imported Decl") ? static_cast<void > (0) : __assert_fail ("(Pos == ImportedDecls.end() || Pos->second == To) && \"Try to import an already imported Decl\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 9351, __PRETTY_FUNCTION__)); | ||||||||||
9352 | if (Pos != ImportedDecls.end()) | ||||||||||
9353 | return Pos->second; | ||||||||||
9354 | ImportedDecls[From] = To; | ||||||||||
9355 | // This mapping should be maintained only in this function. Therefore do not | ||||||||||
9356 | // check for additional consistency. | ||||||||||
9357 | ImportedFromDecls[To] = From; | ||||||||||
9358 | // In the case of TypedefNameDecl we create the Decl first and only then we | ||||||||||
9359 | // import and set its DeclContext. So, the DC is still not set when we reach | ||||||||||
9360 | // here from GetImportedOrCreateDecl. | ||||||||||
9361 | if (To->getDeclContext()) | ||||||||||
9362 | AddToLookupTable(To); | ||||||||||
9363 | return To; | ||||||||||
9364 | } | ||||||||||
9365 | |||||||||||
9366 | llvm::Optional<ImportError> | ||||||||||
9367 | ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const { | ||||||||||
9368 | auto Pos = ImportDeclErrors.find(FromD); | ||||||||||
9369 | if (Pos != ImportDeclErrors.end()) | ||||||||||
9370 | return Pos->second; | ||||||||||
9371 | else | ||||||||||
9372 | return Optional<ImportError>(); | ||||||||||
9373 | } | ||||||||||
9374 | |||||||||||
9375 | void ASTImporter::setImportDeclError(Decl *From, ImportError Error) { | ||||||||||
9376 | auto InsertRes = ImportDeclErrors.insert({From, Error}); | ||||||||||
9377 | (void)InsertRes; | ||||||||||
9378 | // Either we set the error for the first time, or we already had set one and | ||||||||||
9379 | // now we want to set the same error. | ||||||||||
9380 | assert(InsertRes.second || InsertRes.first->second.Error == Error.Error)((InsertRes.second || InsertRes.first->second.Error == Error .Error) ? static_cast<void> (0) : __assert_fail ("InsertRes.second || InsertRes.first->second.Error == Error.Error" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/lib/AST/ASTImporter.cpp" , 9380, __PRETTY_FUNCTION__)); | ||||||||||
9381 | } | ||||||||||
9382 | |||||||||||
9383 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, | ||||||||||
9384 | bool Complain) { | ||||||||||
9385 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = | ||||||||||
9386 | ImportedTypes.find(From.getTypePtr()); | ||||||||||
9387 | if (Pos != ImportedTypes.end()) { | ||||||||||
9388 | if (ExpectedType ToFromOrErr = Import(From)) { | ||||||||||
9389 | if (ToContext.hasSameType(*ToFromOrErr, To)) | ||||||||||
9390 | return true; | ||||||||||
9391 | } else { | ||||||||||
9392 | llvm::consumeError(ToFromOrErr.takeError()); | ||||||||||
9393 | } | ||||||||||
9394 | } | ||||||||||
9395 | |||||||||||
9396 | StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, | ||||||||||
9397 | getStructuralEquivalenceKind(*this), false, | ||||||||||
9398 | Complain); | ||||||||||
9399 | return Ctx.IsEquivalent(From, To); | ||||||||||
9400 | } |
1 | //===- llvm/Support/Error.h - Recoverable error handling --------*- C++ -*-===// |
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 file defines an API used to report recoverable errors. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_SUPPORT_ERROR_H |
14 | #define LLVM_SUPPORT_ERROR_H |
15 | |
16 | #include "llvm-c/Error.h" |
17 | #include "llvm/ADT/STLExtras.h" |
18 | #include "llvm/ADT/SmallVector.h" |
19 | #include "llvm/ADT/StringExtras.h" |
20 | #include "llvm/ADT/Twine.h" |
21 | #include "llvm/Config/abi-breaking.h" |
22 | #include "llvm/Support/AlignOf.h" |
23 | #include "llvm/Support/Compiler.h" |
24 | #include "llvm/Support/Debug.h" |
25 | #include "llvm/Support/ErrorHandling.h" |
26 | #include "llvm/Support/ErrorOr.h" |
27 | #include "llvm/Support/Format.h" |
28 | #include "llvm/Support/raw_ostream.h" |
29 | #include <algorithm> |
30 | #include <cassert> |
31 | #include <cstdint> |
32 | #include <cstdlib> |
33 | #include <functional> |
34 | #include <memory> |
35 | #include <new> |
36 | #include <string> |
37 | #include <system_error> |
38 | #include <type_traits> |
39 | #include <utility> |
40 | #include <vector> |
41 | |
42 | namespace llvm { |
43 | |
44 | class ErrorSuccess; |
45 | |
46 | /// Base class for error info classes. Do not extend this directly: Extend |
47 | /// the ErrorInfo template subclass instead. |
48 | class ErrorInfoBase { |
49 | public: |
50 | virtual ~ErrorInfoBase() = default; |
51 | |
52 | /// Print an error message to an output stream. |
53 | virtual void log(raw_ostream &OS) const = 0; |
54 | |
55 | /// Return the error message as a string. |
56 | virtual std::string message() const { |
57 | std::string Msg; |
58 | raw_string_ostream OS(Msg); |
59 | log(OS); |
60 | return OS.str(); |
61 | } |
62 | |
63 | /// Convert this error to a std::error_code. |
64 | /// |
65 | /// This is a temporary crutch to enable interaction with code still |
66 | /// using std::error_code. It will be removed in the future. |
67 | virtual std::error_code convertToErrorCode() const = 0; |
68 | |
69 | // Returns the class ID for this type. |
70 | static const void *classID() { return &ID; } |
71 | |
72 | // Returns the class ID for the dynamic type of this ErrorInfoBase instance. |
73 | virtual const void *dynamicClassID() const = 0; |
74 | |
75 | // Check whether this instance is a subclass of the class identified by |
76 | // ClassID. |
77 | virtual bool isA(const void *const ClassID) const { |
78 | return ClassID == classID(); |
79 | } |
80 | |
81 | // Check whether this instance is a subclass of ErrorInfoT. |
82 | template <typename ErrorInfoT> bool isA() const { |
83 | return isA(ErrorInfoT::classID()); |
84 | } |
85 | |
86 | private: |
87 | virtual void anchor(); |
88 | |
89 | static char ID; |
90 | }; |
91 | |
92 | /// Lightweight error class with error context and mandatory checking. |
93 | /// |
94 | /// Instances of this class wrap a ErrorInfoBase pointer. Failure states |
95 | /// are represented by setting the pointer to a ErrorInfoBase subclass |
96 | /// instance containing information describing the failure. Success is |
97 | /// represented by a null pointer value. |
98 | /// |
99 | /// Instances of Error also contains a 'Checked' flag, which must be set |
100 | /// before the destructor is called, otherwise the destructor will trigger a |
101 | /// runtime error. This enforces at runtime the requirement that all Error |
102 | /// instances be checked or returned to the caller. |
103 | /// |
104 | /// There are two ways to set the checked flag, depending on what state the |
105 | /// Error instance is in. For Error instances indicating success, it |
106 | /// is sufficient to invoke the boolean conversion operator. E.g.: |
107 | /// |
108 | /// @code{.cpp} |
109 | /// Error foo(<...>); |
110 | /// |
111 | /// if (auto E = foo(<...>)) |
112 | /// return E; // <- Return E if it is in the error state. |
113 | /// // We have verified that E was in the success state. It can now be safely |
114 | /// // destroyed. |
115 | /// @endcode |
116 | /// |
117 | /// A success value *can not* be dropped. For example, just calling 'foo(<...>)' |
118 | /// without testing the return value will raise a runtime error, even if foo |
119 | /// returns success. |
120 | /// |
121 | /// For Error instances representing failure, you must use either the |
122 | /// handleErrors or handleAllErrors function with a typed handler. E.g.: |
123 | /// |
124 | /// @code{.cpp} |
125 | /// class MyErrorInfo : public ErrorInfo<MyErrorInfo> { |
126 | /// // Custom error info. |
127 | /// }; |
128 | /// |
129 | /// Error foo(<...>) { return make_error<MyErrorInfo>(...); } |
130 | /// |
131 | /// auto E = foo(<...>); // <- foo returns failure with MyErrorInfo. |
132 | /// auto NewE = |
133 | /// handleErrors(E, |
134 | /// [](const MyErrorInfo &M) { |
135 | /// // Deal with the error. |
136 | /// }, |
137 | /// [](std::unique_ptr<OtherError> M) -> Error { |
138 | /// if (canHandle(*M)) { |
139 | /// // handle error. |
140 | /// return Error::success(); |
141 | /// } |
142 | /// // Couldn't handle this error instance. Pass it up the stack. |
143 | /// return Error(std::move(M)); |
144 | /// ); |
145 | /// // Note - we must check or return NewE in case any of the handlers |
146 | /// // returned a new error. |
147 | /// @endcode |
148 | /// |
149 | /// The handleAllErrors function is identical to handleErrors, except |
150 | /// that it has a void return type, and requires all errors to be handled and |
151 | /// no new errors be returned. It prevents errors (assuming they can all be |
152 | /// handled) from having to be bubbled all the way to the top-level. |
153 | /// |
154 | /// *All* Error instances must be checked before destruction, even if |
155 | /// they're moved-assigned or constructed from Success values that have already |
156 | /// been checked. This enforces checking through all levels of the call stack. |
157 | class LLVM_NODISCARD[[clang::warn_unused_result]] Error { |
158 | // ErrorList needs to be able to yank ErrorInfoBase pointers out of Errors |
159 | // to add to the error list. It can't rely on handleErrors for this, since |
160 | // handleErrors does not support ErrorList handlers. |
161 | friend class ErrorList; |
162 | |
163 | // handleErrors needs to be able to set the Checked flag. |
164 | template <typename... HandlerTs> |
165 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); |
166 | |
167 | // Expected<T> needs to be able to steal the payload when constructed from an |
168 | // error. |
169 | template <typename T> friend class Expected; |
170 | |
171 | // wrap needs to be able to steal the payload. |
172 | friend LLVMErrorRef wrap(Error); |
173 | |
174 | protected: |
175 | /// Create a success value. Prefer using 'Error::success()' for readability |
176 | Error() { |
177 | setPtr(nullptr); |
178 | setChecked(false); |
179 | } |
180 | |
181 | public: |
182 | /// Create a success value. |
183 | static ErrorSuccess success(); |
184 | |
185 | // Errors are not copy-constructable. |
186 | Error(const Error &Other) = delete; |
187 | |
188 | /// Move-construct an error value. The newly constructed error is considered |
189 | /// unchecked, even if the source error had been checked. The original error |
190 | /// becomes a checked Success value, regardless of its original state. |
191 | Error(Error &&Other) { |
192 | setChecked(true); |
193 | *this = std::move(Other); |
194 | } |
195 | |
196 | /// Create an error value. Prefer using the 'make_error' function, but |
197 | /// this constructor can be useful when "re-throwing" errors from handlers. |
198 | Error(std::unique_ptr<ErrorInfoBase> Payload) { |
199 | setPtr(Payload.release()); |
200 | setChecked(false); |
201 | } |
202 | |
203 | // Errors are not copy-assignable. |
204 | Error &operator=(const Error &Other) = delete; |
205 | |
206 | /// Move-assign an error value. The current error must represent success, you |
207 | /// you cannot overwrite an unhandled error. The current error is then |
208 | /// considered unchecked. The source error becomes a checked success value, |
209 | /// regardless of its original state. |
210 | Error &operator=(Error &&Other) { |
211 | // Don't allow overwriting of unchecked values. |
212 | assertIsChecked(); |
213 | setPtr(Other.getPtr()); |
214 | |
215 | // This Error is unchecked, even if the source error was checked. |
216 | setChecked(false); |
217 | |
218 | // Null out Other's payload and set its checked bit. |
219 | Other.setPtr(nullptr); |
220 | Other.setChecked(true); |
221 | |
222 | return *this; |
223 | } |
224 | |
225 | /// Destroy a Error. Fails with a call to abort() if the error is |
226 | /// unchecked. |
227 | ~Error() { |
228 | assertIsChecked(); |
229 | delete getPtr(); |
230 | } |
231 | |
232 | /// Bool conversion. Returns true if this Error is in a failure state, |
233 | /// and false if it is in an accept state. If the error is in a Success state |
234 | /// it will be considered checked. |
235 | explicit operator bool() { |
236 | setChecked(getPtr() == nullptr); |
237 | return getPtr() != nullptr; |
238 | } |
239 | |
240 | /// Check whether one error is a subclass of another. |
241 | template <typename ErrT> bool isA() const { |
242 | return getPtr() && getPtr()->isA(ErrT::classID()); |
243 | } |
244 | |
245 | /// Returns the dynamic class id of this error, or null if this is a success |
246 | /// value. |
247 | const void* dynamicClassID() const { |
248 | if (!getPtr()) |
249 | return nullptr; |
250 | return getPtr()->dynamicClassID(); |
251 | } |
252 | |
253 | private: |
254 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
255 | // assertIsChecked() happens very frequently, but under normal circumstances |
256 | // is supposed to be a no-op. So we want it to be inlined, but having a bunch |
257 | // of debug prints can cause the function to be too large for inlining. So |
258 | // it's important that we define this function out of line so that it can't be |
259 | // inlined. |
260 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) |
261 | void fatalUncheckedError() const; |
262 | #endif |
263 | |
264 | void assertIsChecked() { |
265 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
266 | if (LLVM_UNLIKELY(!getChecked() || getPtr())__builtin_expect((bool)(!getChecked() || getPtr()), false)) |
267 | fatalUncheckedError(); |
268 | #endif |
269 | } |
270 | |
271 | ErrorInfoBase *getPtr() const { |
272 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
273 | return reinterpret_cast<ErrorInfoBase*>( |
274 | reinterpret_cast<uintptr_t>(Payload) & |
275 | ~static_cast<uintptr_t>(0x1)); |
276 | #else |
277 | return Payload; |
278 | #endif |
279 | } |
280 | |
281 | void setPtr(ErrorInfoBase *EI) { |
282 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
283 | Payload = reinterpret_cast<ErrorInfoBase*>( |
284 | (reinterpret_cast<uintptr_t>(EI) & |
285 | ~static_cast<uintptr_t>(0x1)) | |
286 | (reinterpret_cast<uintptr_t>(Payload) & 0x1)); |
287 | #else |
288 | Payload = EI; |
289 | #endif |
290 | } |
291 | |
292 | bool getChecked() const { |
293 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
294 | return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0; |
295 | #else |
296 | return true; |
297 | #endif |
298 | } |
299 | |
300 | void setChecked(bool V) { |
301 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
302 | Payload = reinterpret_cast<ErrorInfoBase*>( |
303 | (reinterpret_cast<uintptr_t>(Payload) & |
304 | ~static_cast<uintptr_t>(0x1)) | |
305 | (V ? 0 : 1)); |
306 | #endif |
307 | } |
308 | |
309 | std::unique_ptr<ErrorInfoBase> takePayload() { |
310 | std::unique_ptr<ErrorInfoBase> Tmp(getPtr()); |
311 | setPtr(nullptr); |
312 | setChecked(true); |
313 | return Tmp; |
314 | } |
315 | |
316 | friend raw_ostream &operator<<(raw_ostream &OS, const Error &E) { |
317 | if (auto P = E.getPtr()) |
318 | P->log(OS); |
319 | else |
320 | OS << "success"; |
321 | return OS; |
322 | } |
323 | |
324 | ErrorInfoBase *Payload = nullptr; |
325 | }; |
326 | |
327 | /// Subclass of Error for the sole purpose of identifying the success path in |
328 | /// the type system. This allows to catch invalid conversion to Expected<T> at |
329 | /// compile time. |
330 | class ErrorSuccess final : public Error {}; |
331 | |
332 | inline ErrorSuccess Error::success() { return ErrorSuccess(); } |
333 | |
334 | /// Make a Error instance representing failure using the given error info |
335 | /// type. |
336 | template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) { |
337 | return Error(std::make_unique<ErrT>(std::forward<ArgTs>(Args)...)); |
338 | } |
339 | |
340 | /// Base class for user error types. Users should declare their error types |
341 | /// like: |
342 | /// |
343 | /// class MyError : public ErrorInfo<MyError> { |
344 | /// .... |
345 | /// }; |
346 | /// |
347 | /// This class provides an implementation of the ErrorInfoBase::kind |
348 | /// method, which is used by the Error RTTI system. |
349 | template <typename ThisErrT, typename ParentErrT = ErrorInfoBase> |
350 | class ErrorInfo : public ParentErrT { |
351 | public: |
352 | using ParentErrT::ParentErrT; // inherit constructors |
353 | |
354 | static const void *classID() { return &ThisErrT::ID; } |
355 | |
356 | const void *dynamicClassID() const override { return &ThisErrT::ID; } |
357 | |
358 | bool isA(const void *const ClassID) const override { |
359 | return ClassID == classID() || ParentErrT::isA(ClassID); |
360 | } |
361 | }; |
362 | |
363 | /// Special ErrorInfo subclass representing a list of ErrorInfos. |
364 | /// Instances of this class are constructed by joinError. |
365 | class ErrorList final : public ErrorInfo<ErrorList> { |
366 | // handleErrors needs to be able to iterate the payload list of an |
367 | // ErrorList. |
368 | template <typename... HandlerTs> |
369 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); |
370 | |
371 | // joinErrors is implemented in terms of join. |
372 | friend Error joinErrors(Error, Error); |
373 | |
374 | public: |
375 | void log(raw_ostream &OS) const override { |
376 | OS << "Multiple errors:\n"; |
377 | for (auto &ErrPayload : Payloads) { |
378 | ErrPayload->log(OS); |
379 | OS << "\n"; |
380 | } |
381 | } |
382 | |
383 | std::error_code convertToErrorCode() const override; |
384 | |
385 | // Used by ErrorInfo::classID. |
386 | static char ID; |
387 | |
388 | private: |
389 | ErrorList(std::unique_ptr<ErrorInfoBase> Payload1, |
390 | std::unique_ptr<ErrorInfoBase> Payload2) { |
391 | assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() &&((!Payload1->isA<ErrorList>() && !Payload2-> isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors" ) ? static_cast<void> (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 392, __PRETTY_FUNCTION__)) |
392 | "ErrorList constructor payloads should be singleton errors")((!Payload1->isA<ErrorList>() && !Payload2-> isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors" ) ? static_cast<void> (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 392, __PRETTY_FUNCTION__)); |
393 | Payloads.push_back(std::move(Payload1)); |
394 | Payloads.push_back(std::move(Payload2)); |
395 | } |
396 | |
397 | static Error join(Error E1, Error E2) { |
398 | if (!E1) |
399 | return E2; |
400 | if (!E2) |
401 | return E1; |
402 | if (E1.isA<ErrorList>()) { |
403 | auto &E1List = static_cast<ErrorList &>(*E1.getPtr()); |
404 | if (E2.isA<ErrorList>()) { |
405 | auto E2Payload = E2.takePayload(); |
406 | auto &E2List = static_cast<ErrorList &>(*E2Payload); |
407 | for (auto &Payload : E2List.Payloads) |
408 | E1List.Payloads.push_back(std::move(Payload)); |
409 | } else |
410 | E1List.Payloads.push_back(E2.takePayload()); |
411 | |
412 | return E1; |
413 | } |
414 | if (E2.isA<ErrorList>()) { |
415 | auto &E2List = static_cast<ErrorList &>(*E2.getPtr()); |
416 | E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload()); |
417 | return E2; |
418 | } |
419 | return Error(std::unique_ptr<ErrorList>( |
420 | new ErrorList(E1.takePayload(), E2.takePayload()))); |
421 | } |
422 | |
423 | std::vector<std::unique_ptr<ErrorInfoBase>> Payloads; |
424 | }; |
425 | |
426 | /// Concatenate errors. The resulting Error is unchecked, and contains the |
427 | /// ErrorInfo(s), if any, contained in E1, followed by the |
428 | /// ErrorInfo(s), if any, contained in E2. |
429 | inline Error joinErrors(Error E1, Error E2) { |
430 | return ErrorList::join(std::move(E1), std::move(E2)); |
431 | } |
432 | |
433 | /// Tagged union holding either a T or a Error. |
434 | /// |
435 | /// This class parallels ErrorOr, but replaces error_code with Error. Since |
436 | /// Error cannot be copied, this class replaces getError() with |
437 | /// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the |
438 | /// error class type. |
439 | template <class T> class LLVM_NODISCARD[[clang::warn_unused_result]] Expected { |
440 | template <class T1> friend class ExpectedAsOutParameter; |
441 | template <class OtherT> friend class Expected; |
442 | |
443 | static constexpr bool isRef = std::is_reference<T>::value; |
444 | |
445 | using wrap = std::reference_wrapper<std::remove_reference_t<T>>; |
446 | |
447 | using error_type = std::unique_ptr<ErrorInfoBase>; |
448 | |
449 | public: |
450 | using storage_type = std::conditional_t<isRef, wrap, T>; |
451 | using value_type = T; |
452 | |
453 | private: |
454 | using reference = std::remove_reference_t<T> &; |
455 | using const_reference = const std::remove_reference_t<T> &; |
456 | using pointer = std::remove_reference_t<T> *; |
457 | using const_pointer = const std::remove_reference_t<T> *; |
458 | |
459 | public: |
460 | /// Create an Expected<T> error value from the given Error. |
461 | Expected(Error Err) |
462 | : HasError(true) |
463 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
464 | // Expected is unchecked upon construction in Debug builds. |
465 | , Unchecked(true) |
466 | #endif |
467 | { |
468 | assert(Err && "Cannot create Expected<T> from Error success value.")((Err && "Cannot create Expected<T> from Error success value." ) ? static_cast<void> (0) : __assert_fail ("Err && \"Cannot create Expected<T> from Error success value.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 468, __PRETTY_FUNCTION__)); |
469 | new (getErrorStorage()) error_type(Err.takePayload()); |
470 | } |
471 | |
472 | /// Forbid to convert from Error::success() implicitly, this avoids having |
473 | /// Expected<T> foo() { return Error::success(); } which compiles otherwise |
474 | /// but triggers the assertion above. |
475 | Expected(ErrorSuccess) = delete; |
476 | |
477 | /// Create an Expected<T> success value from the given OtherT value, which |
478 | /// must be convertible to T. |
479 | template <typename OtherT> |
480 | Expected(OtherT &&Val, |
481 | std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) |
482 | : HasError(false) |
483 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
484 | // Expected is unchecked upon construction in Debug builds. |
485 | , |
486 | Unchecked(true) |
487 | #endif |
488 | { |
489 | new (getStorage()) storage_type(std::forward<OtherT>(Val)); |
490 | } |
491 | |
492 | /// Move construct an Expected<T> value. |
493 | Expected(Expected &&Other) { moveConstruct(std::move(Other)); } |
494 | |
495 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT |
496 | /// must be convertible to T. |
497 | template <class OtherT> |
498 | Expected( |
499 | Expected<OtherT> &&Other, |
500 | std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) { |
501 | moveConstruct(std::move(Other)); |
502 | } |
503 | |
504 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT |
505 | /// isn't convertible to T. |
506 | template <class OtherT> |
507 | explicit Expected( |
508 | Expected<OtherT> &&Other, |
509 | std::enable_if_t<!std::is_convertible<OtherT, T>::value> * = nullptr) { |
510 | moveConstruct(std::move(Other)); |
511 | } |
512 | |
513 | /// Move-assign from another Expected<T>. |
514 | Expected &operator=(Expected &&Other) { |
515 | moveAssign(std::move(Other)); |
516 | return *this; |
517 | } |
518 | |
519 | /// Destroy an Expected<T>. |
520 | ~Expected() { |
521 | assertIsChecked(); |
522 | if (!HasError) |
523 | getStorage()->~storage_type(); |
524 | else |
525 | getErrorStorage()->~error_type(); |
526 | } |
527 | |
528 | /// Return false if there is an error. |
529 | explicit operator bool() { |
530 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
531 | Unchecked = HasError; |
532 | #endif |
533 | return !HasError; |
534 | } |
535 | |
536 | /// Returns a reference to the stored T value. |
537 | reference get() { |
538 | assertIsChecked(); |
539 | return *getStorage(); |
540 | } |
541 | |
542 | /// Returns a const reference to the stored T value. |
543 | const_reference get() const { |
544 | assertIsChecked(); |
545 | return const_cast<Expected<T> *>(this)->get(); |
546 | } |
547 | |
548 | /// Check that this Expected<T> is an error of type ErrT. |
549 | template <typename ErrT> bool errorIsA() const { |
550 | return HasError && (*getErrorStorage())->template isA<ErrT>(); |
551 | } |
552 | |
553 | /// Take ownership of the stored error. |
554 | /// After calling this the Expected<T> is in an indeterminate state that can |
555 | /// only be safely destructed. No further calls (beside the destructor) should |
556 | /// be made on the Expected<T> value. |
557 | Error takeError() { |
558 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
559 | Unchecked = false; |
560 | #endif |
561 | return HasError ? Error(std::move(*getErrorStorage())) : Error::success(); |
562 | } |
563 | |
564 | /// Returns a pointer to the stored T value. |
565 | pointer operator->() { |
566 | assertIsChecked(); |
567 | return toPointer(getStorage()); |
568 | } |
569 | |
570 | /// Returns a const pointer to the stored T value. |
571 | const_pointer operator->() const { |
572 | assertIsChecked(); |
573 | return toPointer(getStorage()); |
574 | } |
575 | |
576 | /// Returns a reference to the stored T value. |
577 | reference operator*() { |
578 | assertIsChecked(); |
579 | return *getStorage(); |
580 | } |
581 | |
582 | /// Returns a const reference to the stored T value. |
583 | const_reference operator*() const { |
584 | assertIsChecked(); |
585 | return *getStorage(); |
586 | } |
587 | |
588 | private: |
589 | template <class T1> |
590 | static bool compareThisIfSameType(const T1 &a, const T1 &b) { |
591 | return &a == &b; |
592 | } |
593 | |
594 | template <class T1, class T2> |
595 | static bool compareThisIfSameType(const T1 &, const T2 &) { |
596 | return false; |
597 | } |
598 | |
599 | template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) { |
600 | HasError = Other.HasError; |
601 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
602 | Unchecked = true; |
603 | Other.Unchecked = false; |
604 | #endif |
605 | |
606 | if (!HasError) |
607 | new (getStorage()) storage_type(std::move(*Other.getStorage())); |
608 | else |
609 | new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage())); |
610 | } |
611 | |
612 | template <class OtherT> void moveAssign(Expected<OtherT> &&Other) { |
613 | assertIsChecked(); |
614 | |
615 | if (compareThisIfSameType(*this, Other)) |
616 | return; |
617 | |
618 | this->~Expected(); |
619 | new (this) Expected(std::move(Other)); |
620 | } |
621 | |
622 | pointer toPointer(pointer Val) { return Val; } |
623 | |
624 | const_pointer toPointer(const_pointer Val) const { return Val; } |
625 | |
626 | pointer toPointer(wrap *Val) { return &Val->get(); } |
627 | |
628 | const_pointer toPointer(const wrap *Val) const { return &Val->get(); } |
629 | |
630 | storage_type *getStorage() { |
631 | assert(!HasError && "Cannot get value when an error exists!")((!HasError && "Cannot get value when an error exists!" ) ? static_cast<void> (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 631, __PRETTY_FUNCTION__)); |
632 | return reinterpret_cast<storage_type *>(&TStorage); |
633 | } |
634 | |
635 | const storage_type *getStorage() const { |
636 | assert(!HasError && "Cannot get value when an error exists!")((!HasError && "Cannot get value when an error exists!" ) ? static_cast<void> (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 636, __PRETTY_FUNCTION__)); |
637 | return reinterpret_cast<const storage_type *>(&TStorage); |
638 | } |
639 | |
640 | error_type *getErrorStorage() { |
641 | assert(HasError && "Cannot get error when a value exists!")((HasError && "Cannot get error when a value exists!" ) ? static_cast<void> (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 641, __PRETTY_FUNCTION__)); |
642 | return reinterpret_cast<error_type *>(&ErrorStorage); |
643 | } |
644 | |
645 | const error_type *getErrorStorage() const { |
646 | assert(HasError && "Cannot get error when a value exists!")((HasError && "Cannot get error when a value exists!" ) ? static_cast<void> (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 646, __PRETTY_FUNCTION__)); |
647 | return reinterpret_cast<const error_type *>(&ErrorStorage); |
648 | } |
649 | |
650 | // Used by ExpectedAsOutParameter to reset the checked flag. |
651 | void setUnchecked() { |
652 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
653 | Unchecked = true; |
654 | #endif |
655 | } |
656 | |
657 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
658 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) |
659 | LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) |
660 | void fatalUncheckedExpected() const { |
661 | dbgs() << "Expected<T> must be checked before access or destruction.\n"; |
662 | if (HasError) { |
663 | dbgs() << "Unchecked Expected<T> contained error:\n"; |
664 | (*getErrorStorage())->log(dbgs()); |
665 | } else |
666 | dbgs() << "Expected<T> value was in success state. (Note: Expected<T> " |
667 | "values in success mode must still be checked prior to being " |
668 | "destroyed).\n"; |
669 | abort(); |
670 | } |
671 | #endif |
672 | |
673 | void assertIsChecked() { |
674 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
675 | if (LLVM_UNLIKELY(Unchecked)__builtin_expect((bool)(Unchecked), false)) |
676 | fatalUncheckedExpected(); |
677 | #endif |
678 | } |
679 | |
680 | union { |
681 | AlignedCharArrayUnion<storage_type> TStorage; |
682 | AlignedCharArrayUnion<error_type> ErrorStorage; |
683 | }; |
684 | bool HasError : 1; |
685 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
686 | bool Unchecked : 1; |
687 | #endif |
688 | }; |
689 | |
690 | /// Report a serious error, calling any installed error handler. See |
691 | /// ErrorHandling.h. |
692 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) void report_fatal_error(Error Err, |
693 | bool gen_crash_diag = true); |
694 | |
695 | /// Report a fatal error if Err is a failure value. |
696 | /// |
697 | /// This function can be used to wrap calls to fallible functions ONLY when it |
698 | /// is known that the Error will always be a success value. E.g. |
699 | /// |
700 | /// @code{.cpp} |
701 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
702 | /// // true. If DoFallibleOperation is false then foo always returns |
703 | /// // Error::success(). |
704 | /// Error foo(bool DoFallibleOperation); |
705 | /// |
706 | /// cantFail(foo(false)); |
707 | /// @endcode |
708 | inline void cantFail(Error Err, const char *Msg = nullptr) { |
709 | if (Err) { |
710 | if (!Msg) |
711 | Msg = "Failure value returned from cantFail wrapped call"; |
712 | #ifndef NDEBUG |
713 | std::string Str; |
714 | raw_string_ostream OS(Str); |
715 | OS << Msg << "\n" << Err; |
716 | Msg = OS.str().c_str(); |
717 | #endif |
718 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 718); |
719 | } |
720 | } |
721 | |
722 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and |
723 | /// returns the contained value. |
724 | /// |
725 | /// This function can be used to wrap calls to fallible functions ONLY when it |
726 | /// is known that the Error will always be a success value. E.g. |
727 | /// |
728 | /// @code{.cpp} |
729 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
730 | /// // true. If DoFallibleOperation is false then foo always returns an int. |
731 | /// Expected<int> foo(bool DoFallibleOperation); |
732 | /// |
733 | /// int X = cantFail(foo(false)); |
734 | /// @endcode |
735 | template <typename T> |
736 | T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) { |
737 | if (ValOrErr) |
738 | return std::move(*ValOrErr); |
739 | else { |
740 | if (!Msg) |
741 | Msg = "Failure value returned from cantFail wrapped call"; |
742 | #ifndef NDEBUG |
743 | std::string Str; |
744 | raw_string_ostream OS(Str); |
745 | auto E = ValOrErr.takeError(); |
746 | OS << Msg << "\n" << E; |
747 | Msg = OS.str().c_str(); |
748 | #endif |
749 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 749); |
750 | } |
751 | } |
752 | |
753 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and |
754 | /// returns the contained reference. |
755 | /// |
756 | /// This function can be used to wrap calls to fallible functions ONLY when it |
757 | /// is known that the Error will always be a success value. E.g. |
758 | /// |
759 | /// @code{.cpp} |
760 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
761 | /// // true. If DoFallibleOperation is false then foo always returns a Bar&. |
762 | /// Expected<Bar&> foo(bool DoFallibleOperation); |
763 | /// |
764 | /// Bar &X = cantFail(foo(false)); |
765 | /// @endcode |
766 | template <typename T> |
767 | T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) { |
768 | if (ValOrErr) |
769 | return *ValOrErr; |
770 | else { |
771 | if (!Msg) |
772 | Msg = "Failure value returned from cantFail wrapped call"; |
773 | #ifndef NDEBUG |
774 | std::string Str; |
775 | raw_string_ostream OS(Str); |
776 | auto E = ValOrErr.takeError(); |
777 | OS << Msg << "\n" << E; |
778 | Msg = OS.str().c_str(); |
779 | #endif |
780 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 780); |
781 | } |
782 | } |
783 | |
784 | /// Helper for testing applicability of, and applying, handlers for |
785 | /// ErrorInfo types. |
786 | template <typename HandlerT> |
787 | class ErrorHandlerTraits |
788 | : public ErrorHandlerTraits<decltype( |
789 | &std::remove_reference<HandlerT>::type::operator())> {}; |
790 | |
791 | // Specialization functions of the form 'Error (const ErrT&)'. |
792 | template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> { |
793 | public: |
794 | static bool appliesTo(const ErrorInfoBase &E) { |
795 | return E.template isA<ErrT>(); |
796 | } |
797 | |
798 | template <typename HandlerT> |
799 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
800 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 800, __PRETTY_FUNCTION__)); |
801 | return H(static_cast<ErrT &>(*E)); |
802 | } |
803 | }; |
804 | |
805 | // Specialization functions of the form 'void (const ErrT&)'. |
806 | template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> { |
807 | public: |
808 | static bool appliesTo(const ErrorInfoBase &E) { |
809 | return E.template isA<ErrT>(); |
810 | } |
811 | |
812 | template <typename HandlerT> |
813 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
814 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 814, __PRETTY_FUNCTION__)); |
815 | H(static_cast<ErrT &>(*E)); |
816 | return Error::success(); |
817 | } |
818 | }; |
819 | |
820 | /// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'. |
821 | template <typename ErrT> |
822 | class ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> { |
823 | public: |
824 | static bool appliesTo(const ErrorInfoBase &E) { |
825 | return E.template isA<ErrT>(); |
826 | } |
827 | |
828 | template <typename HandlerT> |
829 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
830 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 830, __PRETTY_FUNCTION__)); |
831 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); |
832 | return H(std::move(SubE)); |
833 | } |
834 | }; |
835 | |
836 | /// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'. |
837 | template <typename ErrT> |
838 | class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> { |
839 | public: |
840 | static bool appliesTo(const ErrorInfoBase &E) { |
841 | return E.template isA<ErrT>(); |
842 | } |
843 | |
844 | template <typename HandlerT> |
845 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
846 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 846, __PRETTY_FUNCTION__)); |
847 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); |
848 | H(std::move(SubE)); |
849 | return Error::success(); |
850 | } |
851 | }; |
852 | |
853 | // Specialization for member functions of the form 'RetT (const ErrT&)'. |
854 | template <typename C, typename RetT, typename ErrT> |
855 | class ErrorHandlerTraits<RetT (C::*)(ErrT &)> |
856 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
857 | |
858 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. |
859 | template <typename C, typename RetT, typename ErrT> |
860 | class ErrorHandlerTraits<RetT (C::*)(ErrT &) const> |
861 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
862 | |
863 | // Specialization for member functions of the form 'RetT (const ErrT&)'. |
864 | template <typename C, typename RetT, typename ErrT> |
865 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &)> |
866 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
867 | |
868 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. |
869 | template <typename C, typename RetT, typename ErrT> |
870 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const> |
871 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
872 | |
873 | /// Specialization for member functions of the form |
874 | /// 'RetT (std::unique_ptr<ErrT>)'. |
875 | template <typename C, typename RetT, typename ErrT> |
876 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)> |
877 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; |
878 | |
879 | /// Specialization for member functions of the form |
880 | /// 'RetT (std::unique_ptr<ErrT>) const'. |
881 | template <typename C, typename RetT, typename ErrT> |
882 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const> |
883 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; |
884 | |
885 | inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) { |
886 | return Error(std::move(Payload)); |
887 | } |
888 | |
889 | template <typename HandlerT, typename... HandlerTs> |
890 | Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload, |
891 | HandlerT &&Handler, HandlerTs &&... Handlers) { |
892 | if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload)) |
893 | return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler), |
894 | std::move(Payload)); |
895 | return handleErrorImpl(std::move(Payload), |
896 | std::forward<HandlerTs>(Handlers)...); |
897 | } |
898 | |
899 | /// Pass the ErrorInfo(s) contained in E to their respective handlers. Any |
900 | /// unhandled errors (or Errors returned by handlers) are re-concatenated and |
901 | /// returned. |
902 | /// Because this function returns an error, its result must also be checked |
903 | /// or returned. If you intend to handle all errors use handleAllErrors |
904 | /// (which returns void, and will abort() on unhandled errors) instead. |
905 | template <typename... HandlerTs> |
906 | Error handleErrors(Error E, HandlerTs &&... Hs) { |
907 | if (!E) |
908 | return Error::success(); |
909 | |
910 | std::unique_ptr<ErrorInfoBase> Payload = E.takePayload(); |
911 | |
912 | if (Payload->isA<ErrorList>()) { |
913 | ErrorList &List = static_cast<ErrorList &>(*Payload); |
914 | Error R; |
915 | for (auto &P : List.Payloads) |
916 | R = ErrorList::join( |
917 | std::move(R), |
918 | handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...)); |
919 | return R; |
920 | } |
921 | |
922 | return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...); |
923 | } |
924 | |
925 | /// Behaves the same as handleErrors, except that by contract all errors |
926 | /// *must* be handled by the given handlers (i.e. there must be no remaining |
927 | /// errors after running the handlers, or llvm_unreachable is called). |
928 | template <typename... HandlerTs> |
929 | void handleAllErrors(Error E, HandlerTs &&... Handlers) { |
930 | cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...)); |
931 | } |
932 | |
933 | /// Check that E is a non-error, then drop it. |
934 | /// If E is an error, llvm_unreachable will be called. |
935 | inline void handleAllErrors(Error E) { |
936 | cantFail(std::move(E)); |
937 | } |
938 | |
939 | /// Handle any errors (if present) in an Expected<T>, then try a recovery path. |
940 | /// |
941 | /// If the incoming value is a success value it is returned unmodified. If it |
942 | /// is a failure value then it the contained error is passed to handleErrors. |
943 | /// If handleErrors is able to handle the error then the RecoveryPath functor |
944 | /// is called to supply the final result. If handleErrors is not able to |
945 | /// handle all errors then the unhandled errors are returned. |
946 | /// |
947 | /// This utility enables the follow pattern: |
948 | /// |
949 | /// @code{.cpp} |
950 | /// enum FooStrategy { Aggressive, Conservative }; |
951 | /// Expected<Foo> foo(FooStrategy S); |
952 | /// |
953 | /// auto ResultOrErr = |
954 | /// handleExpected( |
955 | /// foo(Aggressive), |
956 | /// []() { return foo(Conservative); }, |
957 | /// [](AggressiveStrategyError&) { |
958 | /// // Implicitly conusme this - we'll recover by using a conservative |
959 | /// // strategy. |
960 | /// }); |
961 | /// |
962 | /// @endcode |
963 | template <typename T, typename RecoveryFtor, typename... HandlerTs> |
964 | Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath, |
965 | HandlerTs &&... Handlers) { |
966 | if (ValOrErr) |
967 | return ValOrErr; |
968 | |
969 | if (auto Err = handleErrors(ValOrErr.takeError(), |
970 | std::forward<HandlerTs>(Handlers)...)) |
971 | return std::move(Err); |
972 | |
973 | return RecoveryPath(); |
974 | } |
975 | |
976 | /// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner |
977 | /// will be printed before the first one is logged. A newline will be printed |
978 | /// after each error. |
979 | /// |
980 | /// This function is compatible with the helpers from Support/WithColor.h. You |
981 | /// can pass any of them as the OS. Please consider using them instead of |
982 | /// including 'error: ' in the ErrorBanner. |
983 | /// |
984 | /// This is useful in the base level of your program to allow clean termination |
985 | /// (allowing clean deallocation of resources, etc.), while reporting error |
986 | /// information to the user. |
987 | void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {}); |
988 | |
989 | /// Write all error messages (if any) in E to a string. The newline character |
990 | /// is used to separate error messages. |
991 | inline std::string toString(Error E) { |
992 | SmallVector<std::string, 2> Errors; |
993 | handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) { |
994 | Errors.push_back(EI.message()); |
995 | }); |
996 | return join(Errors.begin(), Errors.end(), "\n"); |
997 | } |
998 | |
999 | /// Consume a Error without doing anything. This method should be used |
1000 | /// only where an error can be considered a reasonable and expected return |
1001 | /// value. |
1002 | /// |
1003 | /// Uses of this method are potentially indicative of design problems: If it's |
1004 | /// legitimate to do nothing while processing an "error", the error-producer |
1005 | /// might be more clearly refactored to return an Optional<T>. |
1006 | inline void consumeError(Error Err) { |
1007 | handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {}); |
1008 | } |
1009 | |
1010 | /// Convert an Expected to an Optional without doing anything. This method |
1011 | /// should be used only where an error can be considered a reasonable and |
1012 | /// expected return value. |
1013 | /// |
1014 | /// Uses of this method are potentially indicative of problems: perhaps the |
1015 | /// error should be propagated further, or the error-producer should just |
1016 | /// return an Optional in the first place. |
1017 | template <typename T> Optional<T> expectedToOptional(Expected<T> &&E) { |
1018 | if (E) |
1019 | return std::move(*E); |
1020 | consumeError(E.takeError()); |
1021 | return None; |
1022 | } |
1023 | |
1024 | /// Helper for converting an Error to a bool. |
1025 | /// |
1026 | /// This method returns true if Err is in an error state, or false if it is |
1027 | /// in a success state. Puts Err in a checked state in both cases (unlike |
1028 | /// Error::operator bool(), which only does this for success states). |
1029 | inline bool errorToBool(Error Err) { |
1030 | bool IsError = static_cast<bool>(Err); |
1031 | if (IsError) |
1032 | consumeError(std::move(Err)); |
1033 | return IsError; |
1034 | } |
1035 | |
1036 | /// Helper for Errors used as out-parameters. |
1037 | /// |
1038 | /// This helper is for use with the Error-as-out-parameter idiom, where an error |
1039 | /// is passed to a function or method by reference, rather than being returned. |
1040 | /// In such cases it is helpful to set the checked bit on entry to the function |
1041 | /// so that the error can be written to (unchecked Errors abort on assignment) |
1042 | /// and clear the checked bit on exit so that clients cannot accidentally forget |
1043 | /// to check the result. This helper performs these actions automatically using |
1044 | /// RAII: |
1045 | /// |
1046 | /// @code{.cpp} |
1047 | /// Result foo(Error &Err) { |
1048 | /// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set |
1049 | /// // <body of foo> |
1050 | /// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed. |
1051 | /// } |
1052 | /// @endcode |
1053 | /// |
1054 | /// ErrorAsOutParameter takes an Error* rather than Error& so that it can be |
1055 | /// used with optional Errors (Error pointers that are allowed to be null). If |
1056 | /// ErrorAsOutParameter took an Error reference, an instance would have to be |
1057 | /// created inside every condition that verified that Error was non-null. By |
1058 | /// taking an Error pointer we can just create one instance at the top of the |
1059 | /// function. |
1060 | class ErrorAsOutParameter { |
1061 | public: |
1062 | ErrorAsOutParameter(Error *Err) : Err(Err) { |
1063 | // Raise the checked bit if Err is success. |
1064 | if (Err) |
1065 | (void)!!*Err; |
1066 | } |
1067 | |
1068 | ~ErrorAsOutParameter() { |
1069 | // Clear the checked bit. |
1070 | if (Err && !*Err) |
1071 | *Err = Error::success(); |
1072 | } |
1073 | |
1074 | private: |
1075 | Error *Err; |
1076 | }; |
1077 | |
1078 | /// Helper for Expected<T>s used as out-parameters. |
1079 | /// |
1080 | /// See ErrorAsOutParameter. |
1081 | template <typename T> |
1082 | class ExpectedAsOutParameter { |
1083 | public: |
1084 | ExpectedAsOutParameter(Expected<T> *ValOrErr) |
1085 | : ValOrErr(ValOrErr) { |
1086 | if (ValOrErr) |
1087 | (void)!!*ValOrErr; |
1088 | } |
1089 | |
1090 | ~ExpectedAsOutParameter() { |
1091 | if (ValOrErr) |
1092 | ValOrErr->setUnchecked(); |
1093 | } |
1094 | |
1095 | private: |
1096 | Expected<T> *ValOrErr; |
1097 | }; |
1098 | |
1099 | /// This class wraps a std::error_code in a Error. |
1100 | /// |
1101 | /// This is useful if you're writing an interface that returns a Error |
1102 | /// (or Expected) and you want to call code that still returns |
1103 | /// std::error_codes. |
1104 | class ECError : public ErrorInfo<ECError> { |
1105 | friend Error errorCodeToError(std::error_code); |
1106 | |
1107 | virtual void anchor() override; |
1108 | |
1109 | public: |
1110 | void setErrorCode(std::error_code EC) { this->EC = EC; } |
1111 | std::error_code convertToErrorCode() const override { return EC; } |
1112 | void log(raw_ostream &OS) const override { OS << EC.message(); } |
1113 | |
1114 | // Used by ErrorInfo::classID. |
1115 | static char ID; |
1116 | |
1117 | protected: |
1118 | ECError() = default; |
1119 | ECError(std::error_code EC) : EC(EC) {} |
1120 | |
1121 | std::error_code EC; |
1122 | }; |
1123 | |
1124 | /// The value returned by this function can be returned from convertToErrorCode |
1125 | /// for Error values where no sensible translation to std::error_code exists. |
1126 | /// It should only be used in this situation, and should never be used where a |
1127 | /// sensible conversion to std::error_code is available, as attempts to convert |
1128 | /// to/from this error will result in a fatal error. (i.e. it is a programmatic |
1129 | ///error to try to convert such a value). |
1130 | std::error_code inconvertibleErrorCode(); |
1131 | |
1132 | /// Helper for converting an std::error_code to a Error. |
1133 | Error errorCodeToError(std::error_code EC); |
1134 | |
1135 | /// Helper for converting an ECError to a std::error_code. |
1136 | /// |
1137 | /// This method requires that Err be Error() or an ECError, otherwise it |
1138 | /// will trigger a call to abort(). |
1139 | std::error_code errorToErrorCode(Error Err); |
1140 | |
1141 | /// Convert an ErrorOr<T> to an Expected<T>. |
1142 | template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) { |
1143 | if (auto EC = EO.getError()) |
1144 | return errorCodeToError(EC); |
1145 | return std::move(*EO); |
1146 | } |
1147 | |
1148 | /// Convert an Expected<T> to an ErrorOr<T>. |
1149 | template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> &&E) { |
1150 | if (auto Err = E.takeError()) |
1151 | return errorToErrorCode(std::move(Err)); |
1152 | return std::move(*E); |
1153 | } |
1154 | |
1155 | /// This class wraps a string in an Error. |
1156 | /// |
1157 | /// StringError is useful in cases where the client is not expected to be able |
1158 | /// to consume the specific error message programmatically (for example, if the |
1159 | /// error message is to be presented to the user). |
1160 | /// |
1161 | /// StringError can also be used when additional information is to be printed |
1162 | /// along with a error_code message. Depending on the constructor called, this |
1163 | /// class can either display: |
1164 | /// 1. the error_code message (ECError behavior) |
1165 | /// 2. a string |
1166 | /// 3. the error_code message and a string |
1167 | /// |
1168 | /// These behaviors are useful when subtyping is required; for example, when a |
1169 | /// specific library needs an explicit error type. In the example below, |
1170 | /// PDBError is derived from StringError: |
1171 | /// |
1172 | /// @code{.cpp} |
1173 | /// Expected<int> foo() { |
1174 | /// return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading, |
1175 | /// "Additional information"); |
1176 | /// } |
1177 | /// @endcode |
1178 | /// |
1179 | class StringError : public ErrorInfo<StringError> { |
1180 | public: |
1181 | static char ID; |
1182 | |
1183 | // Prints EC + S and converts to EC |
1184 | StringError(std::error_code EC, const Twine &S = Twine()); |
1185 | |
1186 | // Prints S and converts to EC |
1187 | StringError(const Twine &S, std::error_code EC); |
1188 | |
1189 | void log(raw_ostream &OS) const override; |
1190 | std::error_code convertToErrorCode() const override; |
1191 | |
1192 | const std::string &getMessage() const { return Msg; } |
1193 | |
1194 | private: |
1195 | std::string Msg; |
1196 | std::error_code EC; |
1197 | const bool PrintMsgOnly = false; |
1198 | }; |
1199 | |
1200 | /// Create formatted StringError object. |
1201 | template <typename... Ts> |
1202 | inline Error createStringError(std::error_code EC, char const *Fmt, |
1203 | const Ts &... Vals) { |
1204 | std::string Buffer; |
1205 | raw_string_ostream Stream(Buffer); |
1206 | Stream << format(Fmt, Vals...); |
1207 | return make_error<StringError>(Stream.str(), EC); |
1208 | } |
1209 | |
1210 | Error createStringError(std::error_code EC, char const *Msg); |
1211 | |
1212 | inline Error createStringError(std::error_code EC, const Twine &S) { |
1213 | return createStringError(EC, S.str().c_str()); |
1214 | } |
1215 | |
1216 | template <typename... Ts> |
1217 | inline Error createStringError(std::errc EC, char const *Fmt, |
1218 | const Ts &... Vals) { |
1219 | return createStringError(std::make_error_code(EC), Fmt, Vals...); |
1220 | } |
1221 | |
1222 | /// This class wraps a filename and another Error. |
1223 | /// |
1224 | /// In some cases, an error needs to live along a 'source' name, in order to |
1225 | /// show more detailed information to the user. |
1226 | class FileError final : public ErrorInfo<FileError> { |
1227 | |
1228 | friend Error createFileError(const Twine &, Error); |
1229 | friend Error createFileError(const Twine &, size_t, Error); |
1230 | |
1231 | public: |
1232 | void log(raw_ostream &OS) const override { |
1233 | assert(Err && !FileName.empty() && "Trying to log after takeError().")((Err && !FileName.empty() && "Trying to log after takeError()." ) ? static_cast<void> (0) : __assert_fail ("Err && !FileName.empty() && \"Trying to log after takeError().\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 1233, __PRETTY_FUNCTION__)); |
1234 | OS << "'" << FileName << "': "; |
1235 | if (Line.hasValue()) |
1236 | OS << "line " << Line.getValue() << ": "; |
1237 | Err->log(OS); |
1238 | } |
1239 | |
1240 | StringRef getFileName() { return FileName; } |
1241 | |
1242 | Error takeError() { return Error(std::move(Err)); } |
1243 | |
1244 | std::error_code convertToErrorCode() const override; |
1245 | |
1246 | // Used by ErrorInfo::classID. |
1247 | static char ID; |
1248 | |
1249 | private: |
1250 | FileError(const Twine &F, Optional<size_t> LineNum, |
1251 | std::unique_ptr<ErrorInfoBase> E) { |
1252 | assert(E && "Cannot create FileError from Error success value.")((E && "Cannot create FileError from Error success value." ) ? static_cast<void> (0) : __assert_fail ("E && \"Cannot create FileError from Error success value.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 1252, __PRETTY_FUNCTION__)); |
1253 | assert(!F.isTriviallyEmpty() &&((!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty." ) ? static_cast<void> (0) : __assert_fail ("!F.isTriviallyEmpty() && \"The file name provided to FileError must not be empty.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 1254, __PRETTY_FUNCTION__)) |
1254 | "The file name provided to FileError must not be empty.")((!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty." ) ? static_cast<void> (0) : __assert_fail ("!F.isTriviallyEmpty() && \"The file name provided to FileError must not be empty.\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/Support/Error.h" , 1254, __PRETTY_FUNCTION__)); |
1255 | FileName = F.str(); |
1256 | Err = std::move(E); |
1257 | Line = std::move(LineNum); |
1258 | } |
1259 | |
1260 | static Error build(const Twine &F, Optional<size_t> Line, Error E) { |
1261 | std::unique_ptr<ErrorInfoBase> Payload; |
1262 | handleAllErrors(std::move(E), |
1263 | [&](std::unique_ptr<ErrorInfoBase> EIB) -> Error { |
1264 | Payload = std::move(EIB); |
1265 | return Error::success(); |
1266 | }); |
1267 | return Error( |
1268 | std::unique_ptr<FileError>(new FileError(F, Line, std::move(Payload)))); |
1269 | } |
1270 | |
1271 | std::string FileName; |
1272 | Optional<size_t> Line; |
1273 | std::unique_ptr<ErrorInfoBase> Err; |
1274 | }; |
1275 | |
1276 | /// Concatenate a source file path and/or name with an Error. The resulting |
1277 | /// Error is unchecked. |
1278 | inline Error createFileError(const Twine &F, Error E) { |
1279 | return FileError::build(F, Optional<size_t>(), std::move(E)); |
1280 | } |
1281 | |
1282 | /// Concatenate a source file path and/or name with line number and an Error. |
1283 | /// The resulting Error is unchecked. |
1284 | inline Error createFileError(const Twine &F, size_t Line, Error E) { |
1285 | return FileError::build(F, Optional<size_t>(Line), std::move(E)); |
1286 | } |
1287 | |
1288 | /// Concatenate a source file path and/or name with a std::error_code |
1289 | /// to form an Error object. |
1290 | inline Error createFileError(const Twine &F, std::error_code EC) { |
1291 | return createFileError(F, errorCodeToError(EC)); |
1292 | } |
1293 | |
1294 | /// Concatenate a source file path and/or name with line number and |
1295 | /// std::error_code to form an Error object. |
1296 | inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) { |
1297 | return createFileError(F, Line, errorCodeToError(EC)); |
1298 | } |
1299 | |
1300 | Error createFileError(const Twine &F, ErrorSuccess) = delete; |
1301 | |
1302 | /// Helper for check-and-exit error handling. |
1303 | /// |
1304 | /// For tool use only. NOT FOR USE IN LIBRARY CODE. |
1305 | /// |
1306 | class ExitOnError { |
1307 | public: |
1308 | /// Create an error on exit helper. |
1309 | ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1) |
1310 | : Banner(std::move(Banner)), |
1311 | GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {} |
1312 | |
1313 | /// Set the banner string for any errors caught by operator(). |
1314 | void setBanner(std::string Banner) { this->Banner = std::move(Banner); } |
1315 | |
1316 | /// Set the exit-code mapper function. |
1317 | void setExitCodeMapper(std::function<int(const Error &)> GetExitCode) { |
1318 | this->GetExitCode = std::move(GetExitCode); |
1319 | } |
1320 | |
1321 | /// Check Err. If it's in a failure state log the error(s) and exit. |
1322 | void operator()(Error Err) const { checkError(std::move(Err)); } |
1323 | |
1324 | /// Check E. If it's in a success state then return the contained value. If |
1325 | /// it's in a failure state log the error(s) and exit. |
1326 | template <typename T> T operator()(Expected<T> &&E) const { |
1327 | checkError(E.takeError()); |
1328 | return std::move(*E); |
1329 | } |
1330 | |
1331 | /// Check E. If it's in a success state then return the contained reference. If |
1332 | /// it's in a failure state log the error(s) and exit. |
1333 | template <typename T> T& operator()(Expected<T&> &&E) const { |
1334 | checkError(E.takeError()); |
1335 | return *E; |
1336 | } |
1337 | |
1338 | private: |
1339 | void checkError(Error Err) const { |
1340 | if (Err) { |
1341 | int ExitCode = GetExitCode(Err); |
1342 | logAllUnhandledErrors(std::move(Err), errs(), Banner); |
1343 | exit(ExitCode); |
1344 | } |
1345 | } |
1346 | |
1347 | std::string Banner; |
1348 | std::function<int(const Error &)> GetExitCode; |
1349 | }; |
1350 | |
1351 | /// Conversion from Error to LLVMErrorRef for C error bindings. |
1352 | inline LLVMErrorRef wrap(Error Err) { |
1353 | return reinterpret_cast<LLVMErrorRef>(Err.takePayload().release()); |
1354 | } |
1355 | |
1356 | /// Conversion from LLVMErrorRef to Error for C error bindings. |
1357 | inline Error unwrap(LLVMErrorRef ErrRef) { |
1358 | return Error(std::unique_ptr<ErrorInfoBase>( |
1359 | reinterpret_cast<ErrorInfoBase *>(ErrRef))); |
1360 | } |
1361 | |
1362 | } // end namespace llvm |
1363 | |
1364 | #endif // LLVM_SUPPORT_ERROR_H |
1 | //===- DeclarationName.h - Representation of declaration names --*- C++ -*-===// |
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 file declares the DeclarationName and DeclarationNameTable classes. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_AST_DECLARATIONNAME_H |
14 | #define LLVM_CLANG_AST_DECLARATIONNAME_H |
15 | |
16 | #include "clang/AST/Type.h" |
17 | #include "clang/Basic/Diagnostic.h" |
18 | #include "clang/Basic/IdentifierTable.h" |
19 | #include "clang/Basic/OperatorKinds.h" |
20 | #include "clang/Basic/PartialDiagnostic.h" |
21 | #include "clang/Basic/SourceLocation.h" |
22 | #include "llvm/ADT/DenseMapInfo.h" |
23 | #include "llvm/ADT/FoldingSet.h" |
24 | #include "llvm/Support/Compiler.h" |
25 | #include "llvm/Support/type_traits.h" |
26 | #include <cassert> |
27 | #include <cstdint> |
28 | #include <cstring> |
29 | #include <string> |
30 | |
31 | namespace clang { |
32 | |
33 | class ASTContext; |
34 | template <typename> class CanQual; |
35 | class DeclarationName; |
36 | class DeclarationNameTable; |
37 | class MultiKeywordSelector; |
38 | struct PrintingPolicy; |
39 | class TemplateDecl; |
40 | class TypeSourceInfo; |
41 | class UsingDirectiveDecl; |
42 | |
43 | using CanQualType = CanQual<Type>; |
44 | |
45 | namespace detail { |
46 | |
47 | /// CXXSpecialNameExtra records the type associated with one of the "special" |
48 | /// kinds of declaration names in C++, e.g., constructors, destructors, and |
49 | /// conversion functions. Note that CXXSpecialName is used for C++ constructor, |
50 | /// destructor and conversion functions, but the actual kind is not stored in |
51 | /// CXXSpecialName. Instead we use three different FoldingSet<CXXSpecialName> |
52 | /// in DeclarationNameTable. |
53 | class alignas(IdentifierInfoAlignment) CXXSpecialNameExtra |
54 | : public llvm::FoldingSetNode { |
55 | friend class clang::DeclarationName; |
56 | friend class clang::DeclarationNameTable; |
57 | |
58 | /// The type associated with this declaration name. |
59 | QualType Type; |
60 | |
61 | /// Extra information associated with this declaration name that |
62 | /// can be used by the front end. All bits are really needed |
63 | /// so it is not possible to stash something in the low order bits. |
64 | void *FETokenInfo; |
65 | |
66 | CXXSpecialNameExtra(QualType QT) : Type(QT), FETokenInfo(nullptr) {} |
67 | |
68 | public: |
69 | void Profile(llvm::FoldingSetNodeID &ID) { |
70 | ID.AddPointer(Type.getAsOpaquePtr()); |
71 | } |
72 | }; |
73 | |
74 | /// Contains extra information for the name of a C++ deduction guide. |
75 | class alignas(IdentifierInfoAlignment) CXXDeductionGuideNameExtra |
76 | : public detail::DeclarationNameExtra, |
77 | public llvm::FoldingSetNode { |
78 | friend class clang::DeclarationName; |
79 | friend class clang::DeclarationNameTable; |
80 | |
81 | /// The template named by the deduction guide. |
82 | TemplateDecl *Template; |
83 | |
84 | /// Extra information associated with this operator name that |
85 | /// can be used by the front end. All bits are really needed |
86 | /// so it is not possible to stash something in the low order bits. |
87 | void *FETokenInfo; |
88 | |
89 | CXXDeductionGuideNameExtra(TemplateDecl *TD) |
90 | : DeclarationNameExtra(CXXDeductionGuideName), Template(TD), |
91 | FETokenInfo(nullptr) {} |
92 | |
93 | public: |
94 | void Profile(llvm::FoldingSetNodeID &ID) { ID.AddPointer(Template); } |
95 | }; |
96 | |
97 | /// Contains extra information for the name of an overloaded operator |
98 | /// in C++, such as "operator+. This do not includes literal or conversion |
99 | /// operators. For literal operators see CXXLiteralOperatorIdName and for |
100 | /// conversion operators see CXXSpecialNameExtra. |
101 | class alignas(IdentifierInfoAlignment) CXXOperatorIdName { |
102 | friend class clang::DeclarationName; |
103 | friend class clang::DeclarationNameTable; |
104 | |
105 | /// The kind of this operator. |
106 | OverloadedOperatorKind Kind = OO_None; |
107 | |
108 | /// Extra information associated with this operator name that |
109 | /// can be used by the front end. All bits are really needed |
110 | /// so it is not possible to stash something in the low order bits. |
111 | void *FETokenInfo = nullptr; |
112 | }; |
113 | |
114 | /// Contains the actual identifier that makes up the |
115 | /// name of a C++ literal operator. |
116 | class alignas(IdentifierInfoAlignment) CXXLiteralOperatorIdName |
117 | : public detail::DeclarationNameExtra, |
118 | public llvm::FoldingSetNode { |
119 | friend class clang::DeclarationName; |
120 | friend class clang::DeclarationNameTable; |
121 | |
122 | IdentifierInfo *ID; |
123 | |
124 | /// Extra information associated with this operator name that |
125 | /// can be used by the front end. All bits are really needed |
126 | /// so it is not possible to stash something in the low order bits. |
127 | void *FETokenInfo; |
128 | |
129 | CXXLiteralOperatorIdName(IdentifierInfo *II) |
130 | : DeclarationNameExtra(CXXLiteralOperatorName), ID(II), |
131 | FETokenInfo(nullptr) {} |
132 | |
133 | public: |
134 | void Profile(llvm::FoldingSetNodeID &FSID) { FSID.AddPointer(ID); } |
135 | }; |
136 | |
137 | } // namespace detail |
138 | |
139 | /// The name of a declaration. In the common case, this just stores |
140 | /// an IdentifierInfo pointer to a normal name. However, it also provides |
141 | /// encodings for Objective-C selectors (optimizing zero- and one-argument |
142 | /// selectors, which make up 78% percent of all selectors in Cocoa.h), |
143 | /// special C++ names for constructors, destructors, and conversion functions, |
144 | /// and C++ overloaded operators. |
145 | class DeclarationName { |
146 | friend class DeclarationNameTable; |
147 | friend class NamedDecl; |
148 | |
149 | /// StoredNameKind represent the kind of name that is actually stored in the |
150 | /// upper bits of the Ptr field. This is only used internally. |
151 | /// |
152 | /// NameKind, StoredNameKind, and DeclarationNameExtra::ExtraKind |
153 | /// must satisfy the following properties. These properties enable |
154 | /// efficient conversion between the various kinds. |
155 | /// |
156 | /// * The first seven enumerators of StoredNameKind must have the same |
157 | /// numerical value as the first seven enumerators of NameKind. |
158 | /// This enable efficient conversion between the two enumerations |
159 | /// in the usual case. |
160 | /// |
161 | /// * The enumerations values of DeclarationNameExtra::ExtraKind must start |
162 | /// at zero, and correspond to the numerical value of the first non-inline |
163 | /// enumeration values of NameKind minus an offset. This makes conversion |
164 | /// between DeclarationNameExtra::ExtraKind and NameKind possible with |
165 | /// a single addition/substraction. |
166 | /// |
167 | /// * The enumeration values of Selector::IdentifierInfoFlag must correspond |
168 | /// to the relevant enumeration values of StoredNameKind. |
169 | /// More specifically: |
170 | /// * ZeroArg == StoredObjCZeroArgSelector, |
171 | /// * OneArg == StoredObjCOneArgSelector, |
172 | /// * MultiArg == StoredDeclarationNameExtra |
173 | /// |
174 | /// * PtrMask must mask the low 3 bits of Ptr. |
175 | enum StoredNameKind { |
176 | StoredIdentifier = 0, |
177 | StoredObjCZeroArgSelector = Selector::ZeroArg, |
178 | StoredObjCOneArgSelector = Selector::OneArg, |
179 | StoredCXXConstructorName = 3, |
180 | StoredCXXDestructorName = 4, |
181 | StoredCXXConversionFunctionName = 5, |
182 | StoredCXXOperatorName = 6, |
183 | StoredDeclarationNameExtra = Selector::MultiArg, |
184 | PtrMask = 7, |
185 | UncommonNameKindOffset = 8 |
186 | }; |
187 | |
188 | static_assert(alignof(IdentifierInfo) >= 8 && |
189 | alignof(detail::DeclarationNameExtra) >= 8 && |
190 | alignof(detail::CXXSpecialNameExtra) >= 8 && |
191 | alignof(detail::CXXOperatorIdName) >= 8 && |
192 | alignof(detail::CXXDeductionGuideNameExtra) >= 8 && |
193 | alignof(detail::CXXLiteralOperatorIdName) >= 8, |
194 | "The various classes that DeclarationName::Ptr can point to" |
195 | " must be at least aligned to 8 bytes!"); |
196 | |
197 | public: |
198 | /// The kind of the name stored in this DeclarationName. |
199 | /// The first 7 enumeration values are stored inline and correspond |
200 | /// to frequently used kinds. The rest is stored in DeclarationNameExtra |
201 | /// and correspond to infrequently used kinds. |
202 | enum NameKind { |
203 | Identifier = StoredIdentifier, |
204 | ObjCZeroArgSelector = StoredObjCZeroArgSelector, |
205 | ObjCOneArgSelector = StoredObjCOneArgSelector, |
206 | CXXConstructorName = StoredCXXConstructorName, |
207 | CXXDestructorName = StoredCXXDestructorName, |
208 | CXXConversionFunctionName = StoredCXXConversionFunctionName, |
209 | CXXOperatorName = StoredCXXOperatorName, |
210 | CXXDeductionGuideName = UncommonNameKindOffset + |
211 | detail::DeclarationNameExtra::CXXDeductionGuideName, |
212 | CXXLiteralOperatorName = |
213 | UncommonNameKindOffset + |
214 | detail::DeclarationNameExtra::CXXLiteralOperatorName, |
215 | CXXUsingDirective = UncommonNameKindOffset + |
216 | detail::DeclarationNameExtra::CXXUsingDirective, |
217 | ObjCMultiArgSelector = UncommonNameKindOffset + |
218 | detail::DeclarationNameExtra::ObjCMultiArgSelector |
219 | }; |
220 | |
221 | private: |
222 | /// The lowest three bits of Ptr are used to express what kind of name |
223 | /// we're actually storing, using the values of StoredNameKind. Depending |
224 | /// on the kind of name this is, the upper bits of Ptr may have one |
225 | /// of several different meanings: |
226 | /// |
227 | /// StoredIdentifier - The name is a normal identifier, and Ptr is |
228 | /// a normal IdentifierInfo pointer. |
229 | /// |
230 | /// StoredObjCZeroArgSelector - The name is an Objective-C |
231 | /// selector with zero arguments, and Ptr is an IdentifierInfo |
232 | /// pointer pointing to the selector name. |
233 | /// |
234 | /// StoredObjCOneArgSelector - The name is an Objective-C selector |
235 | /// with one argument, and Ptr is an IdentifierInfo pointer |
236 | /// pointing to the selector name. |
237 | /// |
238 | /// StoredCXXConstructorName - The name of a C++ constructor, |
239 | /// Ptr points to a CXXSpecialNameExtra. |
240 | /// |
241 | /// StoredCXXDestructorName - The name of a C++ destructor, |
242 | /// Ptr points to a CXXSpecialNameExtra. |
243 | /// |
244 | /// StoredCXXConversionFunctionName - The name of a C++ conversion function, |
245 | /// Ptr points to a CXXSpecialNameExtra. |
246 | /// |
247 | /// StoredCXXOperatorName - The name of an overloaded C++ operator, |
248 | /// Ptr points to a CXXOperatorIdName. |
249 | /// |
250 | /// StoredDeclarationNameExtra - Ptr is actually a pointer to a |
251 | /// DeclarationNameExtra structure, whose first value will tell us |
252 | /// whether this is an Objective-C selector, C++ deduction guide, |
253 | /// C++ literal operator, or C++ using directive. |
254 | uintptr_t Ptr = 0; |
255 | |
256 | StoredNameKind getStoredNameKind() const { |
257 | return static_cast<StoredNameKind>(Ptr & PtrMask); |
258 | } |
259 | |
260 | void *getPtr() const { return reinterpret_cast<void *>(Ptr & ~PtrMask); } |
261 | |
262 | void setPtrAndKind(const void *P, StoredNameKind Kind) { |
263 | uintptr_t PAsInteger = reinterpret_cast<uintptr_t>(P); |
264 | assert((Kind & ~PtrMask) == 0 &&(((Kind & ~PtrMask) == 0 && "Invalid StoredNameKind in setPtrAndKind!" ) ? static_cast<void> (0) : __assert_fail ("(Kind & ~PtrMask) == 0 && \"Invalid StoredNameKind in setPtrAndKind!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 265, __PRETTY_FUNCTION__)) |
265 | "Invalid StoredNameKind in setPtrAndKind!")(((Kind & ~PtrMask) == 0 && "Invalid StoredNameKind in setPtrAndKind!" ) ? static_cast<void> (0) : __assert_fail ("(Kind & ~PtrMask) == 0 && \"Invalid StoredNameKind in setPtrAndKind!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 265, __PRETTY_FUNCTION__)); |
266 | assert((PAsInteger & PtrMask) == 0 &&(((PAsInteger & PtrMask) == 0 && "Improperly aligned pointer in setPtrAndKind!" ) ? static_cast<void> (0) : __assert_fail ("(PAsInteger & PtrMask) == 0 && \"Improperly aligned pointer in setPtrAndKind!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 267, __PRETTY_FUNCTION__)) |
267 | "Improperly aligned pointer in setPtrAndKind!")(((PAsInteger & PtrMask) == 0 && "Improperly aligned pointer in setPtrAndKind!" ) ? static_cast<void> (0) : __assert_fail ("(PAsInteger & PtrMask) == 0 && \"Improperly aligned pointer in setPtrAndKind!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 267, __PRETTY_FUNCTION__)); |
268 | Ptr = PAsInteger | Kind; |
269 | } |
270 | |
271 | /// Construct a declaration name from a DeclarationNameExtra. |
272 | DeclarationName(detail::DeclarationNameExtra *Name) { |
273 | setPtrAndKind(Name, StoredDeclarationNameExtra); |
274 | } |
275 | |
276 | /// Construct a declaration name from a CXXSpecialNameExtra. |
277 | DeclarationName(detail::CXXSpecialNameExtra *Name, |
278 | StoredNameKind StoredKind) { |
279 | assert((StoredKind == StoredCXXConstructorName ||(((StoredKind == StoredCXXConstructorName || StoredKind == StoredCXXDestructorName || StoredKind == StoredCXXConversionFunctionName) && "Invalid StoredNameKind when constructing a DeclarationName" " from a CXXSpecialNameExtra!") ? static_cast<void> (0 ) : __assert_fail ("(StoredKind == StoredCXXConstructorName || StoredKind == StoredCXXDestructorName || StoredKind == StoredCXXConversionFunctionName) && \"Invalid StoredNameKind when constructing a DeclarationName\" \" from a CXXSpecialNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 283, __PRETTY_FUNCTION__)) |
280 | StoredKind == StoredCXXDestructorName ||(((StoredKind == StoredCXXConstructorName || StoredKind == StoredCXXDestructorName || StoredKind == StoredCXXConversionFunctionName) && "Invalid StoredNameKind when constructing a DeclarationName" " from a CXXSpecialNameExtra!") ? static_cast<void> (0 ) : __assert_fail ("(StoredKind == StoredCXXConstructorName || StoredKind == StoredCXXDestructorName || StoredKind == StoredCXXConversionFunctionName) && \"Invalid StoredNameKind when constructing a DeclarationName\" \" from a CXXSpecialNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 283, __PRETTY_FUNCTION__)) |
281 | StoredKind == StoredCXXConversionFunctionName) &&(((StoredKind == StoredCXXConstructorName || StoredKind == StoredCXXDestructorName || StoredKind == StoredCXXConversionFunctionName) && "Invalid StoredNameKind when constructing a DeclarationName" " from a CXXSpecialNameExtra!") ? static_cast<void> (0 ) : __assert_fail ("(StoredKind == StoredCXXConstructorName || StoredKind == StoredCXXDestructorName || StoredKind == StoredCXXConversionFunctionName) && \"Invalid StoredNameKind when constructing a DeclarationName\" \" from a CXXSpecialNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 283, __PRETTY_FUNCTION__)) |
282 | "Invalid StoredNameKind when constructing a DeclarationName"(((StoredKind == StoredCXXConstructorName || StoredKind == StoredCXXDestructorName || StoredKind == StoredCXXConversionFunctionName) && "Invalid StoredNameKind when constructing a DeclarationName" " from a CXXSpecialNameExtra!") ? static_cast<void> (0 ) : __assert_fail ("(StoredKind == StoredCXXConstructorName || StoredKind == StoredCXXDestructorName || StoredKind == StoredCXXConversionFunctionName) && \"Invalid StoredNameKind when constructing a DeclarationName\" \" from a CXXSpecialNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 283, __PRETTY_FUNCTION__)) |
283 | " from a CXXSpecialNameExtra!")(((StoredKind == StoredCXXConstructorName || StoredKind == StoredCXXDestructorName || StoredKind == StoredCXXConversionFunctionName) && "Invalid StoredNameKind when constructing a DeclarationName" " from a CXXSpecialNameExtra!") ? static_cast<void> (0 ) : __assert_fail ("(StoredKind == StoredCXXConstructorName || StoredKind == StoredCXXDestructorName || StoredKind == StoredCXXConversionFunctionName) && \"Invalid StoredNameKind when constructing a DeclarationName\" \" from a CXXSpecialNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 283, __PRETTY_FUNCTION__)); |
284 | setPtrAndKind(Name, StoredKind); |
285 | } |
286 | |
287 | /// Construct a DeclarationName from a CXXOperatorIdName. |
288 | DeclarationName(detail::CXXOperatorIdName *Name) { |
289 | setPtrAndKind(Name, StoredCXXOperatorName); |
290 | } |
291 | |
292 | /// Assert that the stored pointer points to an IdentifierInfo and return it. |
293 | IdentifierInfo *castAsIdentifierInfo() const { |
294 | assert((getStoredNameKind() == StoredIdentifier) &&(((getStoredNameKind() == StoredIdentifier) && "DeclarationName does not store an IdentifierInfo!" ) ? static_cast<void> (0) : __assert_fail ("(getStoredNameKind() == StoredIdentifier) && \"DeclarationName does not store an IdentifierInfo!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 295, __PRETTY_FUNCTION__)) |
295 | "DeclarationName does not store an IdentifierInfo!")(((getStoredNameKind() == StoredIdentifier) && "DeclarationName does not store an IdentifierInfo!" ) ? static_cast<void> (0) : __assert_fail ("(getStoredNameKind() == StoredIdentifier) && \"DeclarationName does not store an IdentifierInfo!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 295, __PRETTY_FUNCTION__)); |
296 | return static_cast<IdentifierInfo *>(getPtr()); |
297 | } |
298 | |
299 | /// Assert that the stored pointer points to a DeclarationNameExtra |
300 | /// and return it. |
301 | detail::DeclarationNameExtra *castAsExtra() const { |
302 | assert((getStoredNameKind() == StoredDeclarationNameExtra) &&(((getStoredNameKind() == StoredDeclarationNameExtra) && "DeclarationName does not store an Extra structure!") ? static_cast <void> (0) : __assert_fail ("(getStoredNameKind() == StoredDeclarationNameExtra) && \"DeclarationName does not store an Extra structure!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 303, __PRETTY_FUNCTION__)) |
303 | "DeclarationName does not store an Extra structure!")(((getStoredNameKind() == StoredDeclarationNameExtra) && "DeclarationName does not store an Extra structure!") ? static_cast <void> (0) : __assert_fail ("(getStoredNameKind() == StoredDeclarationNameExtra) && \"DeclarationName does not store an Extra structure!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 303, __PRETTY_FUNCTION__)); |
304 | return static_cast<detail::DeclarationNameExtra *>(getPtr()); |
305 | } |
306 | |
307 | /// Assert that the stored pointer points to a CXXSpecialNameExtra |
308 | /// and return it. |
309 | detail::CXXSpecialNameExtra *castAsCXXSpecialNameExtra() const { |
310 | assert((getStoredNameKind() == StoredCXXConstructorName ||(((getStoredNameKind() == StoredCXXConstructorName || getStoredNameKind () == StoredCXXDestructorName || getStoredNameKind() == StoredCXXConversionFunctionName ) && "DeclarationName does not store a CXXSpecialNameExtra!" ) ? static_cast<void> (0) : __assert_fail ("(getStoredNameKind() == StoredCXXConstructorName || getStoredNameKind() == StoredCXXDestructorName || getStoredNameKind() == StoredCXXConversionFunctionName) && \"DeclarationName does not store a CXXSpecialNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 313, __PRETTY_FUNCTION__)) |
311 | getStoredNameKind() == StoredCXXDestructorName ||(((getStoredNameKind() == StoredCXXConstructorName || getStoredNameKind () == StoredCXXDestructorName || getStoredNameKind() == StoredCXXConversionFunctionName ) && "DeclarationName does not store a CXXSpecialNameExtra!" ) ? static_cast<void> (0) : __assert_fail ("(getStoredNameKind() == StoredCXXConstructorName || getStoredNameKind() == StoredCXXDestructorName || getStoredNameKind() == StoredCXXConversionFunctionName) && \"DeclarationName does not store a CXXSpecialNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 313, __PRETTY_FUNCTION__)) |
312 | getStoredNameKind() == StoredCXXConversionFunctionName) &&(((getStoredNameKind() == StoredCXXConstructorName || getStoredNameKind () == StoredCXXDestructorName || getStoredNameKind() == StoredCXXConversionFunctionName ) && "DeclarationName does not store a CXXSpecialNameExtra!" ) ? static_cast<void> (0) : __assert_fail ("(getStoredNameKind() == StoredCXXConstructorName || getStoredNameKind() == StoredCXXDestructorName || getStoredNameKind() == StoredCXXConversionFunctionName) && \"DeclarationName does not store a CXXSpecialNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 313, __PRETTY_FUNCTION__)) |
313 | "DeclarationName does not store a CXXSpecialNameExtra!")(((getStoredNameKind() == StoredCXXConstructorName || getStoredNameKind () == StoredCXXDestructorName || getStoredNameKind() == StoredCXXConversionFunctionName ) && "DeclarationName does not store a CXXSpecialNameExtra!" ) ? static_cast<void> (0) : __assert_fail ("(getStoredNameKind() == StoredCXXConstructorName || getStoredNameKind() == StoredCXXDestructorName || getStoredNameKind() == StoredCXXConversionFunctionName) && \"DeclarationName does not store a CXXSpecialNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 313, __PRETTY_FUNCTION__)); |
314 | return static_cast<detail::CXXSpecialNameExtra *>(getPtr()); |
315 | } |
316 | |
317 | /// Assert that the stored pointer points to a CXXOperatorIdName |
318 | /// and return it. |
319 | detail::CXXOperatorIdName *castAsCXXOperatorIdName() const { |
320 | assert((getStoredNameKind() == StoredCXXOperatorName) &&(((getStoredNameKind() == StoredCXXOperatorName) && "DeclarationName does not store a CXXOperatorIdName!" ) ? static_cast<void> (0) : __assert_fail ("(getStoredNameKind() == StoredCXXOperatorName) && \"DeclarationName does not store a CXXOperatorIdName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 321, __PRETTY_FUNCTION__)) |
321 | "DeclarationName does not store a CXXOperatorIdName!")(((getStoredNameKind() == StoredCXXOperatorName) && "DeclarationName does not store a CXXOperatorIdName!" ) ? static_cast<void> (0) : __assert_fail ("(getStoredNameKind() == StoredCXXOperatorName) && \"DeclarationName does not store a CXXOperatorIdName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 321, __PRETTY_FUNCTION__)); |
322 | return static_cast<detail::CXXOperatorIdName *>(getPtr()); |
323 | } |
324 | |
325 | /// Assert that the stored pointer points to a CXXDeductionGuideNameExtra |
326 | /// and return it. |
327 | detail::CXXDeductionGuideNameExtra *castAsCXXDeductionGuideNameExtra() const { |
328 | assert(getNameKind() == CXXDeductionGuideName &&((getNameKind() == CXXDeductionGuideName && "DeclarationName does not store a CXXDeductionGuideNameExtra!" ) ? static_cast<void> (0) : __assert_fail ("getNameKind() == CXXDeductionGuideName && \"DeclarationName does not store a CXXDeductionGuideNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 329, __PRETTY_FUNCTION__)) |
329 | "DeclarationName does not store a CXXDeductionGuideNameExtra!")((getNameKind() == CXXDeductionGuideName && "DeclarationName does not store a CXXDeductionGuideNameExtra!" ) ? static_cast<void> (0) : __assert_fail ("getNameKind() == CXXDeductionGuideName && \"DeclarationName does not store a CXXDeductionGuideNameExtra!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 329, __PRETTY_FUNCTION__)); |
330 | return static_cast<detail::CXXDeductionGuideNameExtra *>(getPtr()); |
331 | } |
332 | |
333 | /// Assert that the stored pointer points to a CXXLiteralOperatorIdName |
334 | /// and return it. |
335 | detail::CXXLiteralOperatorIdName *castAsCXXLiteralOperatorIdName() const { |
336 | assert(getNameKind() == CXXLiteralOperatorName &&((getNameKind() == CXXLiteralOperatorName && "DeclarationName does not store a CXXLiteralOperatorIdName!" ) ? static_cast<void> (0) : __assert_fail ("getNameKind() == CXXLiteralOperatorName && \"DeclarationName does not store a CXXLiteralOperatorIdName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 337, __PRETTY_FUNCTION__)) |
337 | "DeclarationName does not store a CXXLiteralOperatorIdName!")((getNameKind() == CXXLiteralOperatorName && "DeclarationName does not store a CXXLiteralOperatorIdName!" ) ? static_cast<void> (0) : __assert_fail ("getNameKind() == CXXLiteralOperatorName && \"DeclarationName does not store a CXXLiteralOperatorIdName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 337, __PRETTY_FUNCTION__)); |
338 | return static_cast<detail::CXXLiteralOperatorIdName *>(getPtr()); |
339 | } |
340 | |
341 | /// Get and set the FETokenInfo in the less common cases where the |
342 | /// declaration name do not point to an identifier. |
343 | void *getFETokenInfoSlow() const; |
344 | void setFETokenInfoSlow(void *T); |
345 | |
346 | public: |
347 | /// Construct an empty declaration name. |
348 | DeclarationName() { setPtrAndKind(nullptr, StoredIdentifier); } |
349 | |
350 | /// Construct a declaration name from an IdentifierInfo *. |
351 | DeclarationName(const IdentifierInfo *II) { |
352 | setPtrAndKind(II, StoredIdentifier); |
353 | } |
354 | |
355 | /// Construct a declaration name from an Objective-C selector. |
356 | DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) {} |
357 | |
358 | /// Returns the name for all C++ using-directives. |
359 | static DeclarationName getUsingDirectiveName() { |
360 | // Single instance of DeclarationNameExtra for using-directive |
361 | static detail::DeclarationNameExtra UDirExtra( |
362 | detail::DeclarationNameExtra::CXXUsingDirective); |
363 | return DeclarationName(&UDirExtra); |
364 | } |
365 | |
366 | /// Evaluates true when this declaration name is non-empty. |
367 | explicit operator bool() const { |
368 | return getPtr() || (getStoredNameKind() != StoredIdentifier); |
369 | } |
370 | |
371 | /// Evaluates true when this declaration name is empty. |
372 | bool isEmpty() const { return !*this; } |
373 | |
374 | /// Predicate functions for querying what type of name this is. |
375 | bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; } |
376 | bool isObjCZeroArgSelector() const { |
377 | return getStoredNameKind() == StoredObjCZeroArgSelector; |
378 | } |
379 | bool isObjCOneArgSelector() const { |
380 | return getStoredNameKind() == StoredObjCOneArgSelector; |
381 | } |
382 | |
383 | /// Determine what kind of name this is. |
384 | NameKind getNameKind() const { |
385 | // We rely on the fact that the first 7 NameKind and StoredNameKind |
386 | // have the same numerical value. This makes the usual case efficient. |
387 | StoredNameKind StoredKind = getStoredNameKind(); |
388 | if (StoredKind != StoredDeclarationNameExtra) |
389 | return static_cast<NameKind>(StoredKind); |
390 | // We have to consult DeclarationNameExtra. We rely on the fact that the |
391 | // enumeration values of ExtraKind correspond to the enumeration values of |
392 | // NameKind minus an offset of UncommonNameKindOffset. |
393 | unsigned ExtraKind = castAsExtra()->getKind(); |
394 | return static_cast<NameKind>(UncommonNameKindOffset + ExtraKind); |
395 | } |
396 | |
397 | /// Determines whether the name itself is dependent, e.g., because it |
398 | /// involves a C++ type that is itself dependent. |
399 | /// |
400 | /// Note that this does not capture all of the notions of "dependent name", |
401 | /// because an identifier can be a dependent name if it is used as the |
402 | /// callee in a call expression with dependent arguments. |
403 | bool isDependentName() const; |
404 | |
405 | /// Retrieve the human-readable string for this name. |
406 | std::string getAsString() const; |
407 | |
408 | /// Retrieve the IdentifierInfo * stored in this declaration name, |
409 | /// or null if this declaration name isn't a simple identifier. |
410 | IdentifierInfo *getAsIdentifierInfo() const { |
411 | if (isIdentifier()) |
412 | return castAsIdentifierInfo(); |
413 | return nullptr; |
414 | } |
415 | |
416 | /// Get the representation of this declaration name as an opaque integer. |
417 | uintptr_t getAsOpaqueInteger() const { return Ptr; } |
418 | |
419 | /// Get the representation of this declaration name as an opaque pointer. |
420 | void *getAsOpaquePtr() const { return reinterpret_cast<void *>(Ptr); } |
421 | |
422 | /// Get a declaration name from an opaque pointer returned by getAsOpaquePtr. |
423 | static DeclarationName getFromOpaquePtr(void *P) { |
424 | DeclarationName N; |
425 | N.Ptr = reinterpret_cast<uintptr_t>(P); |
426 | return N; |
427 | } |
428 | |
429 | /// Get a declaration name from an opaque integer |
430 | /// returned by getAsOpaqueInteger. |
431 | static DeclarationName getFromOpaqueInteger(uintptr_t P) { |
432 | DeclarationName N; |
433 | N.Ptr = P; |
434 | return N; |
435 | } |
436 | |
437 | /// If this name is one of the C++ names (of a constructor, destructor, |
438 | /// or conversion function), return the type associated with that name. |
439 | QualType getCXXNameType() const { |
440 | if (getStoredNameKind() == StoredCXXConstructorName || |
441 | getStoredNameKind() == StoredCXXDestructorName || |
442 | getStoredNameKind() == StoredCXXConversionFunctionName) { |
443 | assert(getPtr() && "getCXXNameType on a null DeclarationName!")((getPtr() && "getCXXNameType on a null DeclarationName!" ) ? static_cast<void> (0) : __assert_fail ("getPtr() && \"getCXXNameType on a null DeclarationName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 443, __PRETTY_FUNCTION__)); |
444 | return castAsCXXSpecialNameExtra()->Type; |
445 | } |
446 | return QualType(); |
447 | } |
448 | |
449 | /// If this name is the name of a C++ deduction guide, return the |
450 | /// template associated with that name. |
451 | TemplateDecl *getCXXDeductionGuideTemplate() const { |
452 | if (getNameKind() == CXXDeductionGuideName) { |
453 | assert(getPtr() &&((getPtr() && "getCXXDeductionGuideTemplate on a null DeclarationName!" ) ? static_cast<void> (0) : __assert_fail ("getPtr() && \"getCXXDeductionGuideTemplate on a null DeclarationName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 454, __PRETTY_FUNCTION__)) |
454 | "getCXXDeductionGuideTemplate on a null DeclarationName!")((getPtr() && "getCXXDeductionGuideTemplate on a null DeclarationName!" ) ? static_cast<void> (0) : __assert_fail ("getPtr() && \"getCXXDeductionGuideTemplate on a null DeclarationName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 454, __PRETTY_FUNCTION__)); |
455 | return castAsCXXDeductionGuideNameExtra()->Template; |
456 | } |
457 | return nullptr; |
458 | } |
459 | |
460 | /// If this name is the name of an overloadable operator in C++ |
461 | /// (e.g., @c operator+), retrieve the kind of overloaded operator. |
462 | OverloadedOperatorKind getCXXOverloadedOperator() const { |
463 | if (getStoredNameKind() == StoredCXXOperatorName) { |
464 | assert(getPtr() && "getCXXOverloadedOperator on a null DeclarationName!")((getPtr() && "getCXXOverloadedOperator on a null DeclarationName!" ) ? static_cast<void> (0) : __assert_fail ("getPtr() && \"getCXXOverloadedOperator on a null DeclarationName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 464, __PRETTY_FUNCTION__)); |
465 | return castAsCXXOperatorIdName()->Kind; |
466 | } |
467 | return OO_None; |
468 | } |
469 | |
470 | /// If this name is the name of a literal operator, |
471 | /// retrieve the identifier associated with it. |
472 | IdentifierInfo *getCXXLiteralIdentifier() const { |
473 | if (getNameKind() == CXXLiteralOperatorName) { |
474 | assert(getPtr() && "getCXXLiteralIdentifier on a null DeclarationName!")((getPtr() && "getCXXLiteralIdentifier on a null DeclarationName!" ) ? static_cast<void> (0) : __assert_fail ("getPtr() && \"getCXXLiteralIdentifier on a null DeclarationName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 474, __PRETTY_FUNCTION__)); |
475 | return castAsCXXLiteralOperatorIdName()->ID; |
476 | } |
477 | return nullptr; |
478 | } |
479 | |
480 | /// Get the Objective-C selector stored in this declaration name. |
481 | Selector getObjCSelector() const { |
482 | assert((getNameKind() == ObjCZeroArgSelector ||(((getNameKind() == ObjCZeroArgSelector || getNameKind() == ObjCOneArgSelector || getNameKind() == ObjCMultiArgSelector || !getPtr()) && "Not a selector!") ? static_cast<void> (0) : __assert_fail ("(getNameKind() == ObjCZeroArgSelector || getNameKind() == ObjCOneArgSelector || getNameKind() == ObjCMultiArgSelector || !getPtr()) && \"Not a selector!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 485, __PRETTY_FUNCTION__)) |
483 | getNameKind() == ObjCOneArgSelector ||(((getNameKind() == ObjCZeroArgSelector || getNameKind() == ObjCOneArgSelector || getNameKind() == ObjCMultiArgSelector || !getPtr()) && "Not a selector!") ? static_cast<void> (0) : __assert_fail ("(getNameKind() == ObjCZeroArgSelector || getNameKind() == ObjCOneArgSelector || getNameKind() == ObjCMultiArgSelector || !getPtr()) && \"Not a selector!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 485, __PRETTY_FUNCTION__)) |
484 | getNameKind() == ObjCMultiArgSelector || !getPtr()) &&(((getNameKind() == ObjCZeroArgSelector || getNameKind() == ObjCOneArgSelector || getNameKind() == ObjCMultiArgSelector || !getPtr()) && "Not a selector!") ? static_cast<void> (0) : __assert_fail ("(getNameKind() == ObjCZeroArgSelector || getNameKind() == ObjCOneArgSelector || getNameKind() == ObjCMultiArgSelector || !getPtr()) && \"Not a selector!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 485, __PRETTY_FUNCTION__)) |
485 | "Not a selector!")(((getNameKind() == ObjCZeroArgSelector || getNameKind() == ObjCOneArgSelector || getNameKind() == ObjCMultiArgSelector || !getPtr()) && "Not a selector!") ? static_cast<void> (0) : __assert_fail ("(getNameKind() == ObjCZeroArgSelector || getNameKind() == ObjCOneArgSelector || getNameKind() == ObjCMultiArgSelector || !getPtr()) && \"Not a selector!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 485, __PRETTY_FUNCTION__)); |
486 | return Selector(Ptr); |
487 | } |
488 | |
489 | /// Get and set FETokenInfo. The language front-end is allowed to associate |
490 | /// arbitrary metadata with some kinds of declaration names, including normal |
491 | /// identifiers and C++ constructors, destructors, and conversion functions. |
492 | void *getFETokenInfo() const { |
493 | assert(getPtr() && "getFETokenInfo on an empty DeclarationName!")((getPtr() && "getFETokenInfo on an empty DeclarationName!" ) ? static_cast<void> (0) : __assert_fail ("getPtr() && \"getFETokenInfo on an empty DeclarationName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 493, __PRETTY_FUNCTION__)); |
494 | if (getStoredNameKind() == StoredIdentifier) |
495 | return castAsIdentifierInfo()->getFETokenInfo(); |
496 | return getFETokenInfoSlow(); |
497 | } |
498 | |
499 | void setFETokenInfo(void *T) { |
500 | assert(getPtr() && "setFETokenInfo on an empty DeclarationName!")((getPtr() && "setFETokenInfo on an empty DeclarationName!" ) ? static_cast<void> (0) : __assert_fail ("getPtr() && \"setFETokenInfo on an empty DeclarationName!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 500, __PRETTY_FUNCTION__)); |
501 | if (getStoredNameKind() == StoredIdentifier) |
502 | castAsIdentifierInfo()->setFETokenInfo(T); |
503 | else |
504 | setFETokenInfoSlow(T); |
505 | } |
506 | |
507 | /// Determine whether the specified names are identical. |
508 | friend bool operator==(DeclarationName LHS, DeclarationName RHS) { |
509 | return LHS.Ptr == RHS.Ptr; |
510 | } |
511 | |
512 | /// Determine whether the specified names are different. |
513 | friend bool operator!=(DeclarationName LHS, DeclarationName RHS) { |
514 | return LHS.Ptr != RHS.Ptr; |
515 | } |
516 | |
517 | static DeclarationName getEmptyMarker() { |
518 | DeclarationName Name; |
519 | Name.Ptr = uintptr_t(-1); |
520 | return Name; |
521 | } |
522 | |
523 | static DeclarationName getTombstoneMarker() { |
524 | DeclarationName Name; |
525 | Name.Ptr = uintptr_t(-2); |
526 | return Name; |
527 | } |
528 | |
529 | static int compare(DeclarationName LHS, DeclarationName RHS); |
530 | |
531 | void print(raw_ostream &OS, const PrintingPolicy &Policy) const; |
532 | |
533 | void dump() const; |
534 | }; |
535 | |
536 | raw_ostream &operator<<(raw_ostream &OS, DeclarationName N); |
537 | |
538 | /// Ordering on two declaration names. If both names are identifiers, |
539 | /// this provides a lexicographical ordering. |
540 | inline bool operator<(DeclarationName LHS, DeclarationName RHS) { |
541 | return DeclarationName::compare(LHS, RHS) < 0; |
542 | } |
543 | |
544 | /// Ordering on two declaration names. If both names are identifiers, |
545 | /// this provides a lexicographical ordering. |
546 | inline bool operator>(DeclarationName LHS, DeclarationName RHS) { |
547 | return DeclarationName::compare(LHS, RHS) > 0; |
548 | } |
549 | |
550 | /// Ordering on two declaration names. If both names are identifiers, |
551 | /// this provides a lexicographical ordering. |
552 | inline bool operator<=(DeclarationName LHS, DeclarationName RHS) { |
553 | return DeclarationName::compare(LHS, RHS) <= 0; |
554 | } |
555 | |
556 | /// Ordering on two declaration names. If both names are identifiers, |
557 | /// this provides a lexicographical ordering. |
558 | inline bool operator>=(DeclarationName LHS, DeclarationName RHS) { |
559 | return DeclarationName::compare(LHS, RHS) >= 0; |
560 | } |
561 | |
562 | /// DeclarationNameTable is used to store and retrieve DeclarationName |
563 | /// instances for the various kinds of declaration names, e.g., normal |
564 | /// identifiers, C++ constructor names, etc. This class contains |
565 | /// uniqued versions of each of the C++ special names, which can be |
566 | /// retrieved using its member functions (e.g., getCXXConstructorName). |
567 | class DeclarationNameTable { |
568 | /// Used to allocate elements in the FoldingSets below. |
569 | const ASTContext &Ctx; |
570 | |
571 | /// Manage the uniqued CXXSpecialNameExtra representing C++ constructors. |
572 | /// getCXXConstructorName and getCXXSpecialName can be used to obtain |
573 | /// a DeclarationName from the corresponding type of the constructor. |
574 | llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConstructorNames; |
575 | |
576 | /// Manage the uniqued CXXSpecialNameExtra representing C++ destructors. |
577 | /// getCXXDestructorName and getCXXSpecialName can be used to obtain |
578 | /// a DeclarationName from the corresponding type of the destructor. |
579 | llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXDestructorNames; |
580 | |
581 | /// Manage the uniqued CXXSpecialNameExtra representing C++ conversion |
582 | /// functions. getCXXConversionFunctionName and getCXXSpecialName can be |
583 | /// used to obtain a DeclarationName from the corresponding type of the |
584 | /// conversion function. |
585 | llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConversionFunctionNames; |
586 | |
587 | /// Manage the uniqued CXXOperatorIdName, which contain extra information |
588 | /// for the name of overloaded C++ operators. getCXXOperatorName |
589 | /// can be used to obtain a DeclarationName from the operator kind. |
590 | detail::CXXOperatorIdName CXXOperatorNames[NUM_OVERLOADED_OPERATORS]; |
591 | |
592 | /// Manage the uniqued CXXLiteralOperatorIdName, which contain extra |
593 | /// information for the name of C++ literal operators. |
594 | /// getCXXLiteralOperatorName can be used to obtain a DeclarationName |
595 | /// from the corresponding IdentifierInfo. |
596 | llvm::FoldingSet<detail::CXXLiteralOperatorIdName> CXXLiteralOperatorNames; |
597 | |
598 | /// Manage the uniqued CXXDeductionGuideNameExtra, which contain |
599 | /// extra information for the name of a C++ deduction guide. |
600 | /// getCXXDeductionGuideName can be used to obtain a DeclarationName |
601 | /// from the corresponding template declaration. |
602 | llvm::FoldingSet<detail::CXXDeductionGuideNameExtra> CXXDeductionGuideNames; |
603 | |
604 | public: |
605 | DeclarationNameTable(const ASTContext &C); |
606 | DeclarationNameTable(const DeclarationNameTable &) = delete; |
607 | DeclarationNameTable &operator=(const DeclarationNameTable &) = delete; |
608 | DeclarationNameTable(DeclarationNameTable &&) = delete; |
609 | DeclarationNameTable &operator=(DeclarationNameTable &&) = delete; |
610 | ~DeclarationNameTable() = default; |
611 | |
612 | /// Create a declaration name that is a simple identifier. |
613 | DeclarationName getIdentifier(const IdentifierInfo *ID) { |
614 | return DeclarationName(ID); |
615 | } |
616 | |
617 | /// Returns the name of a C++ constructor for the given Type. |
618 | DeclarationName getCXXConstructorName(CanQualType Ty); |
619 | |
620 | /// Returns the name of a C++ destructor for the given Type. |
621 | DeclarationName getCXXDestructorName(CanQualType Ty); |
622 | |
623 | /// Returns the name of a C++ deduction guide for the given template. |
624 | DeclarationName getCXXDeductionGuideName(TemplateDecl *TD); |
625 | |
626 | /// Returns the name of a C++ conversion function for the given Type. |
627 | DeclarationName getCXXConversionFunctionName(CanQualType Ty); |
628 | |
629 | /// Returns a declaration name for special kind of C++ name, |
630 | /// e.g., for a constructor, destructor, or conversion function. |
631 | /// Kind must be one of: |
632 | /// * DeclarationName::CXXConstructorName, |
633 | /// * DeclarationName::CXXDestructorName or |
634 | /// * DeclarationName::CXXConversionFunctionName |
635 | DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind, |
636 | CanQualType Ty); |
637 | |
638 | /// Get the name of the overloadable C++ operator corresponding to Op. |
639 | DeclarationName getCXXOperatorName(OverloadedOperatorKind Op) { |
640 | return DeclarationName(&CXXOperatorNames[Op]); |
641 | } |
642 | |
643 | /// Get the name of the literal operator function with II as the identifier. |
644 | DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II); |
645 | }; |
646 | |
647 | /// DeclarationNameLoc - Additional source/type location info |
648 | /// for a declaration name. Needs a DeclarationName in order |
649 | /// to be interpreted correctly. |
650 | class DeclarationNameLoc { |
651 | // The source location for identifier stored elsewhere. |
652 | // struct {} Identifier; |
653 | |
654 | // Type info for constructors, destructors and conversion functions. |
655 | // Locations (if any) for the tilde (destructor) or operator keyword |
656 | // (conversion) are stored elsewhere. |
657 | struct NT { |
658 | TypeSourceInfo *TInfo; |
659 | }; |
660 | |
661 | // The location (if any) of the operator keyword is stored elsewhere. |
662 | struct CXXOpName { |
663 | unsigned BeginOpNameLoc; |
664 | unsigned EndOpNameLoc; |
665 | }; |
666 | |
667 | // The location (if any) of the operator keyword is stored elsewhere. |
668 | struct CXXLitOpName { |
669 | unsigned OpNameLoc; |
670 | }; |
671 | |
672 | // struct {} CXXUsingDirective; |
673 | // struct {} ObjCZeroArgSelector; |
674 | // struct {} ObjCOneArgSelector; |
675 | // struct {} ObjCMultiArgSelector; |
676 | union { |
677 | struct NT NamedType; |
678 | struct CXXOpName CXXOperatorName; |
679 | struct CXXLitOpName CXXLiteralOperatorName; |
680 | }; |
681 | |
682 | void setNamedTypeLoc(TypeSourceInfo *TInfo) { NamedType.TInfo = TInfo; } |
683 | |
684 | void setCXXOperatorNameRange(SourceRange Range) { |
685 | CXXOperatorName.BeginOpNameLoc = Range.getBegin().getRawEncoding(); |
686 | CXXOperatorName.EndOpNameLoc = Range.getEnd().getRawEncoding(); |
687 | } |
688 | |
689 | void setCXXLiteralOperatorNameLoc(SourceLocation Loc) { |
690 | CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding(); |
691 | } |
692 | |
693 | public: |
694 | DeclarationNameLoc(DeclarationName Name); |
695 | // FIXME: this should go away once all DNLocs are properly initialized. |
696 | DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); } |
697 | |
698 | /// Returns the source type info. Assumes that the object stores location |
699 | /// information of a constructor, destructor or conversion operator. |
700 | TypeSourceInfo *getNamedTypeInfo() const { return NamedType.TInfo; } |
701 | |
702 | /// Return the beginning location of the getCXXOperatorNameRange() range. |
703 | SourceLocation getCXXOperatorNameBeginLoc() const { |
704 | return SourceLocation::getFromRawEncoding(CXXOperatorName.BeginOpNameLoc); |
705 | } |
706 | |
707 | /// Return the end location of the getCXXOperatorNameRange() range. |
708 | SourceLocation getCXXOperatorNameEndLoc() const { |
709 | return SourceLocation::getFromRawEncoding(CXXOperatorName.EndOpNameLoc); |
710 | } |
711 | |
712 | /// Return the range of the operator name (without the operator keyword). |
713 | /// Assumes that the object stores location information of a (non-literal) |
714 | /// operator. |
715 | SourceRange getCXXOperatorNameRange() const { |
716 | return SourceRange(getCXXOperatorNameBeginLoc(), |
717 | getCXXOperatorNameEndLoc()); |
718 | } |
719 | |
720 | /// Return the location of the literal operator name (without the operator |
721 | /// keyword). Assumes that the object stores location information of a literal |
722 | /// operator. |
723 | SourceLocation getCXXLiteralOperatorNameLoc() const { |
724 | return SourceLocation::getFromRawEncoding(CXXLiteralOperatorName.OpNameLoc); |
725 | } |
726 | |
727 | /// Construct location information for a constructor, destructor or conversion |
728 | /// operator. |
729 | static DeclarationNameLoc makeNamedTypeLoc(TypeSourceInfo *TInfo) { |
730 | DeclarationNameLoc DNL; |
731 | DNL.setNamedTypeLoc(TInfo); |
732 | return DNL; |
733 | } |
734 | |
735 | /// Construct location information for a non-literal C++ operator. |
736 | static DeclarationNameLoc makeCXXOperatorNameLoc(SourceLocation BeginLoc, |
737 | SourceLocation EndLoc) { |
738 | return makeCXXOperatorNameLoc(SourceRange(BeginLoc, EndLoc)); |
739 | } |
740 | |
741 | /// Construct location information for a non-literal C++ operator. |
742 | static DeclarationNameLoc makeCXXOperatorNameLoc(SourceRange Range) { |
743 | DeclarationNameLoc DNL; |
744 | DNL.setCXXOperatorNameRange(Range); |
745 | return DNL; |
746 | } |
747 | |
748 | /// Construct location information for a literal C++ operator. |
749 | static DeclarationNameLoc makeCXXLiteralOperatorNameLoc(SourceLocation Loc) { |
750 | DeclarationNameLoc DNL; |
751 | DNL.setCXXLiteralOperatorNameLoc(Loc); |
752 | return DNL; |
753 | } |
754 | }; |
755 | |
756 | /// DeclarationNameInfo - A collector data type for bundling together |
757 | /// a DeclarationName and the correspnding source/type location info. |
758 | struct DeclarationNameInfo { |
759 | private: |
760 | /// Name - The declaration name, also encoding name kind. |
761 | DeclarationName Name; |
762 | |
763 | /// Loc - The main source location for the declaration name. |
764 | SourceLocation NameLoc; |
765 | |
766 | /// Info - Further source/type location info for special kinds of names. |
767 | DeclarationNameLoc LocInfo; |
768 | |
769 | public: |
770 | // FIXME: remove it. |
771 | DeclarationNameInfo() = default; |
772 | |
773 | DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc) |
774 | : Name(Name), NameLoc(NameLoc), LocInfo(Name) {} |
775 | |
776 | DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc, |
777 | DeclarationNameLoc LocInfo) |
778 | : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {} |
779 | |
780 | /// getName - Returns the embedded declaration name. |
781 | DeclarationName getName() const { return Name; } |
782 | |
783 | /// setName - Sets the embedded declaration name. |
784 | void setName(DeclarationName N) { Name = N; } |
785 | |
786 | /// getLoc - Returns the main location of the declaration name. |
787 | SourceLocation getLoc() const { return NameLoc; } |
788 | |
789 | /// setLoc - Sets the main location of the declaration name. |
790 | void setLoc(SourceLocation L) { NameLoc = L; } |
791 | |
792 | const DeclarationNameLoc &getInfo() const { return LocInfo; } |
793 | void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; } |
794 | |
795 | /// getNamedTypeInfo - Returns the source type info associated to |
796 | /// the name. Assumes it is a constructor, destructor or conversion. |
797 | TypeSourceInfo *getNamedTypeInfo() const { |
798 | if (Name.getNameKind() != DeclarationName::CXXConstructorName && |
799 | Name.getNameKind() != DeclarationName::CXXDestructorName && |
800 | Name.getNameKind() != DeclarationName::CXXConversionFunctionName) |
801 | return nullptr; |
802 | return LocInfo.getNamedTypeInfo(); |
803 | } |
804 | |
805 | /// setNamedTypeInfo - Sets the source type info associated to |
806 | /// the name. Assumes it is a constructor, destructor or conversion. |
807 | void setNamedTypeInfo(TypeSourceInfo *TInfo) { |
808 | assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||((Name.getNameKind() == DeclarationName::CXXConstructorName || Name.getNameKind() == DeclarationName::CXXDestructorName || Name .getNameKind() == DeclarationName::CXXConversionFunctionName) ? static_cast<void> (0) : __assert_fail ("Name.getNameKind() == DeclarationName::CXXConstructorName || Name.getNameKind() == DeclarationName::CXXDestructorName || Name.getNameKind() == DeclarationName::CXXConversionFunctionName" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 810, __PRETTY_FUNCTION__)) |
809 | Name.getNameKind() == DeclarationName::CXXDestructorName ||((Name.getNameKind() == DeclarationName::CXXConstructorName || Name.getNameKind() == DeclarationName::CXXDestructorName || Name .getNameKind() == DeclarationName::CXXConversionFunctionName) ? static_cast<void> (0) : __assert_fail ("Name.getNameKind() == DeclarationName::CXXConstructorName || Name.getNameKind() == DeclarationName::CXXDestructorName || Name.getNameKind() == DeclarationName::CXXConversionFunctionName" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 810, __PRETTY_FUNCTION__)) |
810 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName)((Name.getNameKind() == DeclarationName::CXXConstructorName || Name.getNameKind() == DeclarationName::CXXDestructorName || Name .getNameKind() == DeclarationName::CXXConversionFunctionName) ? static_cast<void> (0) : __assert_fail ("Name.getNameKind() == DeclarationName::CXXConstructorName || Name.getNameKind() == DeclarationName::CXXDestructorName || Name.getNameKind() == DeclarationName::CXXConversionFunctionName" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 810, __PRETTY_FUNCTION__)); |
811 | LocInfo = DeclarationNameLoc::makeNamedTypeLoc(TInfo); |
812 | } |
813 | |
814 | /// getCXXOperatorNameRange - Gets the range of the operator name |
815 | /// (without the operator keyword). Assumes it is a (non-literal) operator. |
816 | SourceRange getCXXOperatorNameRange() const { |
817 | if (Name.getNameKind() != DeclarationName::CXXOperatorName) |
818 | return SourceRange(); |
819 | return LocInfo.getCXXOperatorNameRange(); |
820 | } |
821 | |
822 | /// setCXXOperatorNameRange - Sets the range of the operator name |
823 | /// (without the operator keyword). Assumes it is a C++ operator. |
824 | void setCXXOperatorNameRange(SourceRange R) { |
825 | assert(Name.getNameKind() == DeclarationName::CXXOperatorName)((Name.getNameKind() == DeclarationName::CXXOperatorName) ? static_cast <void> (0) : __assert_fail ("Name.getNameKind() == DeclarationName::CXXOperatorName" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 825, __PRETTY_FUNCTION__)); |
826 | LocInfo = DeclarationNameLoc::makeCXXOperatorNameLoc(R); |
827 | } |
828 | |
829 | /// getCXXLiteralOperatorNameLoc - Returns the location of the literal |
830 | /// operator name (not the operator keyword). |
831 | /// Assumes it is a literal operator. |
832 | SourceLocation getCXXLiteralOperatorNameLoc() const { |
833 | if (Name.getNameKind() != DeclarationName::CXXLiteralOperatorName) |
834 | return SourceLocation(); |
835 | return LocInfo.getCXXLiteralOperatorNameLoc(); |
836 | } |
837 | |
838 | /// setCXXLiteralOperatorNameLoc - Sets the location of the literal |
839 | /// operator name (not the operator keyword). |
840 | /// Assumes it is a literal operator. |
841 | void setCXXLiteralOperatorNameLoc(SourceLocation Loc) { |
842 | assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName)((Name.getNameKind() == DeclarationName::CXXLiteralOperatorName ) ? static_cast<void> (0) : __assert_fail ("Name.getNameKind() == DeclarationName::CXXLiteralOperatorName" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/DeclarationName.h" , 842, __PRETTY_FUNCTION__)); |
843 | LocInfo = DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(Loc); |
844 | } |
845 | |
846 | /// Determine whether this name involves a template parameter. |
847 | bool isInstantiationDependent() const; |
848 | |
849 | /// Determine whether this name contains an unexpanded |
850 | /// parameter pack. |
851 | bool containsUnexpandedParameterPack() const; |
852 | |
853 | /// getAsString - Retrieve the human-readable string for this name. |
854 | std::string getAsString() const; |
855 | |
856 | /// printName - Print the human-readable name to a stream. |
857 | void printName(raw_ostream &OS, PrintingPolicy Policy) const; |
858 | |
859 | /// getBeginLoc - Retrieve the location of the first token. |
860 | SourceLocation getBeginLoc() const { return NameLoc; } |
861 | |
862 | /// getSourceRange - The range of the declaration name. |
863 | SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__)) { |
864 | return SourceRange(getBeginLoc(), getEndLoc()); |
865 | } |
866 | |
867 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { |
868 | SourceLocation EndLoc = getEndLocPrivate(); |
869 | return EndLoc.isValid() ? EndLoc : getBeginLoc(); |
870 | } |
871 | |
872 | private: |
873 | SourceLocation getEndLocPrivate() const; |
874 | }; |
875 | |
876 | /// Insertion operator for partial diagnostics. This allows binding |
877 | /// DeclarationName's into a partial diagnostic with <<. |
878 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
879 | DeclarationName N) { |
880 | PD.AddTaggedVal(N.getAsOpaqueInteger(), |
881 | DiagnosticsEngine::ak_declarationname); |
882 | return PD; |
883 | } |
884 | |
885 | raw_ostream &operator<<(raw_ostream &OS, DeclarationNameInfo DNInfo); |
886 | |
887 | } // namespace clang |
888 | |
889 | namespace llvm { |
890 | |
891 | /// Define DenseMapInfo so that DeclarationNames can be used as keys |
892 | /// in DenseMap and DenseSets. |
893 | template<> |
894 | struct DenseMapInfo<clang::DeclarationName> { |
895 | static inline clang::DeclarationName getEmptyKey() { |
896 | return clang::DeclarationName::getEmptyMarker(); |
897 | } |
898 | |
899 | static inline clang::DeclarationName getTombstoneKey() { |
900 | return clang::DeclarationName::getTombstoneMarker(); |
901 | } |
902 | |
903 | static unsigned getHashValue(clang::DeclarationName Name) { |
904 | return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr()); |
905 | } |
906 | |
907 | static inline bool |
908 | isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) { |
909 | return LHS == RHS; |
910 | } |
911 | }; |
912 | |
913 | template <> struct PointerLikeTypeTraits<clang::DeclarationName> { |
914 | static inline void *getAsVoidPointer(clang::DeclarationName P) { |
915 | return P.getAsOpaquePtr(); |
916 | } |
917 | static inline clang::DeclarationName getFromVoidPointer(void *P) { |
918 | return clang::DeclarationName::getFromOpaquePtr(P); |
919 | } |
920 | static constexpr int NumLowBitsAvailable = 0; |
921 | }; |
922 | |
923 | } // namespace llvm |
924 | |
925 | // The definition of AssumedTemplateStorage is factored out of TemplateName to |
926 | // resolve a cyclic dependency between it and DeclarationName (via Type). |
927 | namespace clang { |
928 | |
929 | /// A structure for storing the information associated with a name that has |
930 | /// been assumed to be a template name (despite finding no TemplateDecls). |
931 | class AssumedTemplateStorage : public UncommonTemplateNameStorage { |
932 | friend class ASTContext; |
933 | |
934 | AssumedTemplateStorage(DeclarationName Name) |
935 | : UncommonTemplateNameStorage(Assumed, 0), Name(Name) {} |
936 | DeclarationName Name; |
937 | |
938 | public: |
939 | /// Get the name of the template. |
940 | DeclarationName getDeclName() const { return Name; } |
941 | }; |
942 | |
943 | } // namespace clang |
944 | |
945 | #endif // LLVM_CLANG_AST_DECLARATIONNAME_H |
1 | //===- Decl.h - Classes for representing declarations -----------*- C++ -*-===// |
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 file defines the Decl subclasses. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_AST_DECL_H |
14 | #define LLVM_CLANG_AST_DECL_H |
15 | |
16 | #include "clang/AST/APValue.h" |
17 | #include "clang/AST/ASTContextAllocate.h" |
18 | #include "clang/AST/DeclAccessPair.h" |
19 | #include "clang/AST/DeclBase.h" |
20 | #include "clang/AST/DeclarationName.h" |
21 | #include "clang/AST/ExternalASTSource.h" |
22 | #include "clang/AST/NestedNameSpecifier.h" |
23 | #include "clang/AST/Redeclarable.h" |
24 | #include "clang/AST/Type.h" |
25 | #include "clang/Basic/AddressSpaces.h" |
26 | #include "clang/Basic/Diagnostic.h" |
27 | #include "clang/Basic/IdentifierTable.h" |
28 | #include "clang/Basic/LLVM.h" |
29 | #include "clang/Basic/Linkage.h" |
30 | #include "clang/Basic/OperatorKinds.h" |
31 | #include "clang/Basic/PartialDiagnostic.h" |
32 | #include "clang/Basic/PragmaKinds.h" |
33 | #include "clang/Basic/SourceLocation.h" |
34 | #include "clang/Basic/Specifiers.h" |
35 | #include "clang/Basic/Visibility.h" |
36 | #include "llvm/ADT/APSInt.h" |
37 | #include "llvm/ADT/ArrayRef.h" |
38 | #include "llvm/ADT/Optional.h" |
39 | #include "llvm/ADT/PointerIntPair.h" |
40 | #include "llvm/ADT/PointerUnion.h" |
41 | #include "llvm/ADT/StringRef.h" |
42 | #include "llvm/ADT/iterator_range.h" |
43 | #include "llvm/Support/Casting.h" |
44 | #include "llvm/Support/Compiler.h" |
45 | #include "llvm/Support/TrailingObjects.h" |
46 | #include <cassert> |
47 | #include <cstddef> |
48 | #include <cstdint> |
49 | #include <string> |
50 | #include <utility> |
51 | |
52 | namespace clang { |
53 | |
54 | class ASTContext; |
55 | struct ASTTemplateArgumentListInfo; |
56 | class Attr; |
57 | class CompoundStmt; |
58 | class DependentFunctionTemplateSpecializationInfo; |
59 | class EnumDecl; |
60 | class Expr; |
61 | class FunctionTemplateDecl; |
62 | class FunctionTemplateSpecializationInfo; |
63 | class FunctionTypeLoc; |
64 | class LabelStmt; |
65 | class MemberSpecializationInfo; |
66 | class Module; |
67 | class NamespaceDecl; |
68 | class ParmVarDecl; |
69 | class RecordDecl; |
70 | class Stmt; |
71 | class StringLiteral; |
72 | class TagDecl; |
73 | class TemplateArgumentList; |
74 | class TemplateArgumentListInfo; |
75 | class TemplateParameterList; |
76 | class TypeAliasTemplateDecl; |
77 | class TypeLoc; |
78 | class UnresolvedSetImpl; |
79 | class VarTemplateDecl; |
80 | |
81 | /// The top declaration context. |
82 | class TranslationUnitDecl : public Decl, public DeclContext { |
83 | ASTContext &Ctx; |
84 | |
85 | /// The (most recently entered) anonymous namespace for this |
86 | /// translation unit, if one has been created. |
87 | NamespaceDecl *AnonymousNamespace = nullptr; |
88 | |
89 | explicit TranslationUnitDecl(ASTContext &ctx); |
90 | |
91 | virtual void anchor(); |
92 | |
93 | public: |
94 | ASTContext &getASTContext() const { return Ctx; } |
95 | |
96 | NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; } |
97 | void setAnonymousNamespace(NamespaceDecl *D) { AnonymousNamespace = D; } |
98 | |
99 | static TranslationUnitDecl *Create(ASTContext &C); |
100 | |
101 | // Implement isa/cast/dyncast/etc. |
102 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
103 | static bool classofKind(Kind K) { return K == TranslationUnit; } |
104 | static DeclContext *castToDeclContext(const TranslationUnitDecl *D) { |
105 | return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D)); |
106 | } |
107 | static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) { |
108 | return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC)); |
109 | } |
110 | }; |
111 | |
112 | /// Represents a `#pragma comment` line. Always a child of |
113 | /// TranslationUnitDecl. |
114 | class PragmaCommentDecl final |
115 | : public Decl, |
116 | private llvm::TrailingObjects<PragmaCommentDecl, char> { |
117 | friend class ASTDeclReader; |
118 | friend class ASTDeclWriter; |
119 | friend TrailingObjects; |
120 | |
121 | PragmaMSCommentKind CommentKind; |
122 | |
123 | PragmaCommentDecl(TranslationUnitDecl *TU, SourceLocation CommentLoc, |
124 | PragmaMSCommentKind CommentKind) |
125 | : Decl(PragmaComment, TU, CommentLoc), CommentKind(CommentKind) {} |
126 | |
127 | virtual void anchor(); |
128 | |
129 | public: |
130 | static PragmaCommentDecl *Create(const ASTContext &C, TranslationUnitDecl *DC, |
131 | SourceLocation CommentLoc, |
132 | PragmaMSCommentKind CommentKind, |
133 | StringRef Arg); |
134 | static PragmaCommentDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
135 | unsigned ArgSize); |
136 | |
137 | PragmaMSCommentKind getCommentKind() const { return CommentKind; } |
138 | |
139 | StringRef getArg() const { return getTrailingObjects<char>(); } |
140 | |
141 | // Implement isa/cast/dyncast/etc. |
142 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
143 | static bool classofKind(Kind K) { return K == PragmaComment; } |
144 | }; |
145 | |
146 | /// Represents a `#pragma detect_mismatch` line. Always a child of |
147 | /// TranslationUnitDecl. |
148 | class PragmaDetectMismatchDecl final |
149 | : public Decl, |
150 | private llvm::TrailingObjects<PragmaDetectMismatchDecl, char> { |
151 | friend class ASTDeclReader; |
152 | friend class ASTDeclWriter; |
153 | friend TrailingObjects; |
154 | |
155 | size_t ValueStart; |
156 | |
157 | PragmaDetectMismatchDecl(TranslationUnitDecl *TU, SourceLocation Loc, |
158 | size_t ValueStart) |
159 | : Decl(PragmaDetectMismatch, TU, Loc), ValueStart(ValueStart) {} |
160 | |
161 | virtual void anchor(); |
162 | |
163 | public: |
164 | static PragmaDetectMismatchDecl *Create(const ASTContext &C, |
165 | TranslationUnitDecl *DC, |
166 | SourceLocation Loc, StringRef Name, |
167 | StringRef Value); |
168 | static PragmaDetectMismatchDecl * |
169 | CreateDeserialized(ASTContext &C, unsigned ID, unsigned NameValueSize); |
170 | |
171 | StringRef getName() const { return getTrailingObjects<char>(); } |
172 | StringRef getValue() const { return getTrailingObjects<char>() + ValueStart; } |
173 | |
174 | // Implement isa/cast/dyncast/etc. |
175 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
176 | static bool classofKind(Kind K) { return K == PragmaDetectMismatch; } |
177 | }; |
178 | |
179 | /// Declaration context for names declared as extern "C" in C++. This |
180 | /// is neither the semantic nor lexical context for such declarations, but is |
181 | /// used to check for conflicts with other extern "C" declarations. Example: |
182 | /// |
183 | /// \code |
184 | /// namespace N { extern "C" void f(); } // #1 |
185 | /// void N::f() {} // #2 |
186 | /// namespace M { extern "C" void f(); } // #3 |
187 | /// \endcode |
188 | /// |
189 | /// The semantic context of #1 is namespace N and its lexical context is the |
190 | /// LinkageSpecDecl; the semantic context of #2 is namespace N and its lexical |
191 | /// context is the TU. However, both declarations are also visible in the |
192 | /// extern "C" context. |
193 | /// |
194 | /// The declaration at #3 finds it is a redeclaration of \c N::f through |
195 | /// lookup in the extern "C" context. |
196 | class ExternCContextDecl : public Decl, public DeclContext { |
197 | explicit ExternCContextDecl(TranslationUnitDecl *TU) |
198 | : Decl(ExternCContext, TU, SourceLocation()), |
199 | DeclContext(ExternCContext) {} |
200 | |
201 | virtual void anchor(); |
202 | |
203 | public: |
204 | static ExternCContextDecl *Create(const ASTContext &C, |
205 | TranslationUnitDecl *TU); |
206 | |
207 | // Implement isa/cast/dyncast/etc. |
208 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
209 | static bool classofKind(Kind K) { return K == ExternCContext; } |
210 | static DeclContext *castToDeclContext(const ExternCContextDecl *D) { |
211 | return static_cast<DeclContext *>(const_cast<ExternCContextDecl*>(D)); |
212 | } |
213 | static ExternCContextDecl *castFromDeclContext(const DeclContext *DC) { |
214 | return static_cast<ExternCContextDecl *>(const_cast<DeclContext*>(DC)); |
215 | } |
216 | }; |
217 | |
218 | /// This represents a decl that may have a name. Many decls have names such |
219 | /// as ObjCMethodDecl, but not \@class, etc. |
220 | /// |
221 | /// Note that not every NamedDecl is actually named (e.g., a struct might |
222 | /// be anonymous), and not every name is an identifier. |
223 | class NamedDecl : public Decl { |
224 | /// The name of this declaration, which is typically a normal |
225 | /// identifier but may also be a special kind of name (C++ |
226 | /// constructor, Objective-C selector, etc.) |
227 | DeclarationName Name; |
228 | |
229 | virtual void anchor(); |
230 | |
231 | private: |
232 | NamedDecl *getUnderlyingDeclImpl() LLVM_READONLY__attribute__((__pure__)); |
233 | |
234 | protected: |
235 | NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N) |
236 | : Decl(DK, DC, L), Name(N) {} |
237 | |
238 | public: |
239 | /// Get the identifier that names this declaration, if there is one. |
240 | /// |
241 | /// This will return NULL if this declaration has no name (e.g., for |
242 | /// an unnamed class) or if the name is a special name (C++ constructor, |
243 | /// Objective-C selector, etc.). |
244 | IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); } |
245 | |
246 | /// Get the name of identifier for this declaration as a StringRef. |
247 | /// |
248 | /// This requires that the declaration have a name and that it be a simple |
249 | /// identifier. |
250 | StringRef getName() const { |
251 | assert(Name.isIdentifier() && "Name is not a simple identifier")((Name.isIdentifier() && "Name is not a simple identifier" ) ? static_cast<void> (0) : __assert_fail ("Name.isIdentifier() && \"Name is not a simple identifier\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 251, __PRETTY_FUNCTION__)); |
252 | return getIdentifier() ? getIdentifier()->getName() : ""; |
253 | } |
254 | |
255 | /// Get a human-readable name for the declaration, even if it is one of the |
256 | /// special kinds of names (C++ constructor, Objective-C selector, etc). |
257 | /// |
258 | /// Creating this name requires expensive string manipulation, so it should |
259 | /// be called only when performance doesn't matter. For simple declarations, |
260 | /// getNameAsCString() should suffice. |
261 | // |
262 | // FIXME: This function should be renamed to indicate that it is not just an |
263 | // alternate form of getName(), and clients should move as appropriate. |
264 | // |
265 | // FIXME: Deprecated, move clients to getName(). |
266 | std::string getNameAsString() const { return Name.getAsString(); } |
267 | |
268 | /// Pretty-print the unqualified name of this declaration. Can be overloaded |
269 | /// by derived classes to provide a more user-friendly name when appropriate. |
270 | virtual void printName(raw_ostream &os) const; |
271 | |
272 | /// Get the actual, stored name of the declaration, which may be a special |
273 | /// name. |
274 | /// |
275 | /// Note that generally in diagnostics, the non-null \p NamedDecl* itself |
276 | /// should be sent into the diagnostic instead of using the result of |
277 | /// \p getDeclName(). |
278 | /// |
279 | /// A \p DeclarationName in a diagnostic will just be streamed to the output, |
280 | /// which will directly result in a call to \p DeclarationName::print. |
281 | /// |
282 | /// A \p NamedDecl* in a diagnostic will also ultimately result in a call to |
283 | /// \p DeclarationName::print, but with two customisation points along the |
284 | /// way (\p getNameForDiagnostic and \p printName). These are used to print |
285 | /// the template arguments if any, and to provide a user-friendly name for |
286 | /// some entities (such as unnamed variables and anonymous records). |
287 | DeclarationName getDeclName() const { return Name; } |
288 | |
289 | /// Set the name of this declaration. |
290 | void setDeclName(DeclarationName N) { Name = N; } |
291 | |
292 | /// Returns a human-readable qualified name for this declaration, like |
293 | /// A::B::i, for i being member of namespace A::B. |
294 | /// |
295 | /// If the declaration is not a member of context which can be named (record, |
296 | /// namespace), it will return the same result as printName(). |
297 | /// |
298 | /// Creating this name is expensive, so it should be called only when |
299 | /// performance doesn't matter. |
300 | void printQualifiedName(raw_ostream &OS) const; |
301 | void printQualifiedName(raw_ostream &OS, const PrintingPolicy &Policy) const; |
302 | |
303 | /// Print only the nested name specifier part of a fully-qualified name, |
304 | /// including the '::' at the end. E.g. |
305 | /// when `printQualifiedName(D)` prints "A::B::i", |
306 | /// this function prints "A::B::". |
307 | void printNestedNameSpecifier(raw_ostream &OS) const; |
308 | void printNestedNameSpecifier(raw_ostream &OS, |
309 | const PrintingPolicy &Policy) const; |
310 | |
311 | // FIXME: Remove string version. |
312 | std::string getQualifiedNameAsString() const; |
313 | |
314 | /// Appends a human-readable name for this declaration into the given stream. |
315 | /// |
316 | /// This is the method invoked by Sema when displaying a NamedDecl |
317 | /// in a diagnostic. It does not necessarily produce the same |
318 | /// result as printName(); for example, class template |
319 | /// specializations are printed with their template arguments. |
320 | virtual void getNameForDiagnostic(raw_ostream &OS, |
321 | const PrintingPolicy &Policy, |
322 | bool Qualified) const; |
323 | |
324 | /// Determine whether this declaration, if known to be well-formed within |
325 | /// its context, will replace the declaration OldD if introduced into scope. |
326 | /// |
327 | /// A declaration will replace another declaration if, for example, it is |
328 | /// a redeclaration of the same variable or function, but not if it is a |
329 | /// declaration of a different kind (function vs. class) or an overloaded |
330 | /// function. |
331 | /// |
332 | /// \param IsKnownNewer \c true if this declaration is known to be newer |
333 | /// than \p OldD (for instance, if this declaration is newly-created). |
334 | bool declarationReplaces(NamedDecl *OldD, bool IsKnownNewer = true) const; |
335 | |
336 | /// Determine whether this declaration has linkage. |
337 | bool hasLinkage() const; |
338 | |
339 | using Decl::isModulePrivate; |
340 | using Decl::setModulePrivate; |
341 | |
342 | /// Determine whether this declaration is a C++ class member. |
343 | bool isCXXClassMember() const { |
344 | const DeclContext *DC = getDeclContext(); |
345 | |
346 | // C++0x [class.mem]p1: |
347 | // The enumerators of an unscoped enumeration defined in |
348 | // the class are members of the class. |
349 | if (isa<EnumDecl>(DC)) |
350 | DC = DC->getRedeclContext(); |
351 | |
352 | return DC->isRecord(); |
353 | } |
354 | |
355 | /// Determine whether the given declaration is an instance member of |
356 | /// a C++ class. |
357 | bool isCXXInstanceMember() const; |
358 | |
359 | /// Determine what kind of linkage this entity has. |
360 | /// |
361 | /// This is not the linkage as defined by the standard or the codegen notion |
362 | /// of linkage. It is just an implementation detail that is used to compute |
363 | /// those. |
364 | Linkage getLinkageInternal() const; |
365 | |
366 | /// Get the linkage from a semantic point of view. Entities in |
367 | /// anonymous namespaces are external (in c++98). |
368 | Linkage getFormalLinkage() const { |
369 | return clang::getFormalLinkage(getLinkageInternal()); |
370 | } |
371 | |
372 | /// True if this decl has external linkage. |
373 | bool hasExternalFormalLinkage() const { |
374 | return isExternalFormalLinkage(getLinkageInternal()); |
375 | } |
376 | |
377 | bool isExternallyVisible() const { |
378 | return clang::isExternallyVisible(getLinkageInternal()); |
379 | } |
380 | |
381 | /// Determine whether this declaration can be redeclared in a |
382 | /// different translation unit. |
383 | bool isExternallyDeclarable() const { |
384 | return isExternallyVisible() && !getOwningModuleForLinkage(); |
385 | } |
386 | |
387 | /// Determines the visibility of this entity. |
388 | Visibility getVisibility() const { |
389 | return getLinkageAndVisibility().getVisibility(); |
390 | } |
391 | |
392 | /// Determines the linkage and visibility of this entity. |
393 | LinkageInfo getLinkageAndVisibility() const; |
394 | |
395 | /// Kinds of explicit visibility. |
396 | enum ExplicitVisibilityKind { |
397 | /// Do an LV computation for, ultimately, a type. |
398 | /// Visibility may be restricted by type visibility settings and |
399 | /// the visibility of template arguments. |
400 | VisibilityForType, |
401 | |
402 | /// Do an LV computation for, ultimately, a non-type declaration. |
403 | /// Visibility may be restricted by value visibility settings and |
404 | /// the visibility of template arguments. |
405 | VisibilityForValue |
406 | }; |
407 | |
408 | /// If visibility was explicitly specified for this |
409 | /// declaration, return that visibility. |
410 | Optional<Visibility> |
411 | getExplicitVisibility(ExplicitVisibilityKind kind) const; |
412 | |
413 | /// True if the computed linkage is valid. Used for consistency |
414 | /// checking. Should always return true. |
415 | bool isLinkageValid() const; |
416 | |
417 | /// True if something has required us to compute the linkage |
418 | /// of this declaration. |
419 | /// |
420 | /// Language features which can retroactively change linkage (like a |
421 | /// typedef name for linkage purposes) may need to consider this, |
422 | /// but hopefully only in transitory ways during parsing. |
423 | bool hasLinkageBeenComputed() const { |
424 | return hasCachedLinkage(); |
425 | } |
426 | |
427 | /// Looks through UsingDecls and ObjCCompatibleAliasDecls for |
428 | /// the underlying named decl. |
429 | NamedDecl *getUnderlyingDecl() { |
430 | // Fast-path the common case. |
431 | if (this->getKind() != UsingShadow && |
432 | this->getKind() != ConstructorUsingShadow && |
433 | this->getKind() != ObjCCompatibleAlias && |
434 | this->getKind() != NamespaceAlias) |
435 | return this; |
436 | |
437 | return getUnderlyingDeclImpl(); |
438 | } |
439 | const NamedDecl *getUnderlyingDecl() const { |
440 | return const_cast<NamedDecl*>(this)->getUnderlyingDecl(); |
441 | } |
442 | |
443 | NamedDecl *getMostRecentDecl() { |
444 | return cast<NamedDecl>(static_cast<Decl *>(this)->getMostRecentDecl()); |
445 | } |
446 | const NamedDecl *getMostRecentDecl() const { |
447 | return const_cast<NamedDecl*>(this)->getMostRecentDecl(); |
448 | } |
449 | |
450 | ObjCStringFormatFamily getObjCFStringFormattingFamily() const; |
451 | |
452 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
453 | static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; } |
454 | }; |
455 | |
456 | inline raw_ostream &operator<<(raw_ostream &OS, const NamedDecl &ND) { |
457 | ND.printName(OS); |
458 | return OS; |
459 | } |
460 | |
461 | /// Represents the declaration of a label. Labels also have a |
462 | /// corresponding LabelStmt, which indicates the position that the label was |
463 | /// defined at. For normal labels, the location of the decl is the same as the |
464 | /// location of the statement. For GNU local labels (__label__), the decl |
465 | /// location is where the __label__ is. |
466 | class LabelDecl : public NamedDecl { |
467 | LabelStmt *TheStmt; |
468 | StringRef MSAsmName; |
469 | bool MSAsmNameResolved = false; |
470 | |
471 | /// For normal labels, this is the same as the main declaration |
472 | /// label, i.e., the location of the identifier; for GNU local labels, |
473 | /// this is the location of the __label__ keyword. |
474 | SourceLocation LocStart; |
475 | |
476 | LabelDecl(DeclContext *DC, SourceLocation IdentL, IdentifierInfo *II, |
477 | LabelStmt *S, SourceLocation StartL) |
478 | : NamedDecl(Label, DC, IdentL, II), TheStmt(S), LocStart(StartL) {} |
479 | |
480 | void anchor() override; |
481 | |
482 | public: |
483 | static LabelDecl *Create(ASTContext &C, DeclContext *DC, |
484 | SourceLocation IdentL, IdentifierInfo *II); |
485 | static LabelDecl *Create(ASTContext &C, DeclContext *DC, |
486 | SourceLocation IdentL, IdentifierInfo *II, |
487 | SourceLocation GnuLabelL); |
488 | static LabelDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
489 | |
490 | LabelStmt *getStmt() const { return TheStmt; } |
491 | void setStmt(LabelStmt *T) { TheStmt = T; } |
492 | |
493 | bool isGnuLocal() const { return LocStart != getLocation(); } |
494 | void setLocStart(SourceLocation L) { LocStart = L; } |
495 | |
496 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
497 | return SourceRange(LocStart, getLocation()); |
498 | } |
499 | |
500 | bool isMSAsmLabel() const { return !MSAsmName.empty(); } |
501 | bool isResolvedMSAsmLabel() const { return isMSAsmLabel() && MSAsmNameResolved; } |
502 | void setMSAsmLabel(StringRef Name); |
503 | StringRef getMSAsmLabel() const { return MSAsmName; } |
504 | void setMSAsmLabelResolved() { MSAsmNameResolved = true; } |
505 | |
506 | // Implement isa/cast/dyncast/etc. |
507 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
508 | static bool classofKind(Kind K) { return K == Label; } |
509 | }; |
510 | |
511 | /// Represent a C++ namespace. |
512 | class NamespaceDecl : public NamedDecl, public DeclContext, |
513 | public Redeclarable<NamespaceDecl> |
514 | { |
515 | /// The starting location of the source range, pointing |
516 | /// to either the namespace or the inline keyword. |
517 | SourceLocation LocStart; |
518 | |
519 | /// The ending location of the source range. |
520 | SourceLocation RBraceLoc; |
521 | |
522 | /// A pointer to either the anonymous namespace that lives just inside |
523 | /// this namespace or to the first namespace in the chain (the latter case |
524 | /// only when this is not the first in the chain), along with a |
525 | /// boolean value indicating whether this is an inline namespace. |
526 | llvm::PointerIntPair<NamespaceDecl *, 1, bool> AnonOrFirstNamespaceAndInline; |
527 | |
528 | NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline, |
529 | SourceLocation StartLoc, SourceLocation IdLoc, |
530 | IdentifierInfo *Id, NamespaceDecl *PrevDecl); |
531 | |
532 | using redeclarable_base = Redeclarable<NamespaceDecl>; |
533 | |
534 | NamespaceDecl *getNextRedeclarationImpl() override; |
535 | NamespaceDecl *getPreviousDeclImpl() override; |
536 | NamespaceDecl *getMostRecentDeclImpl() override; |
537 | |
538 | public: |
539 | friend class ASTDeclReader; |
540 | friend class ASTDeclWriter; |
541 | |
542 | static NamespaceDecl *Create(ASTContext &C, DeclContext *DC, |
543 | bool Inline, SourceLocation StartLoc, |
544 | SourceLocation IdLoc, IdentifierInfo *Id, |
545 | NamespaceDecl *PrevDecl); |
546 | |
547 | static NamespaceDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
548 | |
549 | using redecl_range = redeclarable_base::redecl_range; |
550 | using redecl_iterator = redeclarable_base::redecl_iterator; |
551 | |
552 | using redeclarable_base::redecls_begin; |
553 | using redeclarable_base::redecls_end; |
554 | using redeclarable_base::redecls; |
555 | using redeclarable_base::getPreviousDecl; |
556 | using redeclarable_base::getMostRecentDecl; |
557 | using redeclarable_base::isFirstDecl; |
558 | |
559 | /// Returns true if this is an anonymous namespace declaration. |
560 | /// |
561 | /// For example: |
562 | /// \code |
563 | /// namespace { |
564 | /// ... |
565 | /// }; |
566 | /// \endcode |
567 | /// q.v. C++ [namespace.unnamed] |
568 | bool isAnonymousNamespace() const { |
569 | return !getIdentifier(); |
570 | } |
571 | |
572 | /// Returns true if this is an inline namespace declaration. |
573 | bool isInline() const { |
574 | return AnonOrFirstNamespaceAndInline.getInt(); |
575 | } |
576 | |
577 | /// Set whether this is an inline namespace declaration. |
578 | void setInline(bool Inline) { |
579 | AnonOrFirstNamespaceAndInline.setInt(Inline); |
580 | } |
581 | |
582 | /// Returns true if the inline qualifier for \c Name is redundant. |
583 | bool isRedundantInlineQualifierFor(DeclarationName Name) const { |
584 | if (!isInline()) |
585 | return false; |
586 | auto X = lookup(Name); |
587 | auto Y = getParent()->lookup(Name); |
588 | return std::distance(X.begin(), X.end()) == |
589 | std::distance(Y.begin(), Y.end()); |
590 | } |
591 | |
592 | /// Get the original (first) namespace declaration. |
593 | NamespaceDecl *getOriginalNamespace(); |
594 | |
595 | /// Get the original (first) namespace declaration. |
596 | const NamespaceDecl *getOriginalNamespace() const; |
597 | |
598 | /// Return true if this declaration is an original (first) declaration |
599 | /// of the namespace. This is false for non-original (subsequent) namespace |
600 | /// declarations and anonymous namespaces. |
601 | bool isOriginalNamespace() const; |
602 | |
603 | /// Retrieve the anonymous namespace nested inside this namespace, |
604 | /// if any. |
605 | NamespaceDecl *getAnonymousNamespace() const { |
606 | return getOriginalNamespace()->AnonOrFirstNamespaceAndInline.getPointer(); |
607 | } |
608 | |
609 | void setAnonymousNamespace(NamespaceDecl *D) { |
610 | getOriginalNamespace()->AnonOrFirstNamespaceAndInline.setPointer(D); |
611 | } |
612 | |
613 | /// Retrieves the canonical declaration of this namespace. |
614 | NamespaceDecl *getCanonicalDecl() override { |
615 | return getOriginalNamespace(); |
616 | } |
617 | const NamespaceDecl *getCanonicalDecl() const { |
618 | return getOriginalNamespace(); |
619 | } |
620 | |
621 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
622 | return SourceRange(LocStart, RBraceLoc); |
623 | } |
624 | |
625 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return LocStart; } |
626 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
627 | void setLocStart(SourceLocation L) { LocStart = L; } |
628 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
629 | |
630 | // Implement isa/cast/dyncast/etc. |
631 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
632 | static bool classofKind(Kind K) { return K == Namespace; } |
633 | static DeclContext *castToDeclContext(const NamespaceDecl *D) { |
634 | return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D)); |
635 | } |
636 | static NamespaceDecl *castFromDeclContext(const DeclContext *DC) { |
637 | return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC)); |
638 | } |
639 | }; |
640 | |
641 | /// Represent the declaration of a variable (in which case it is |
642 | /// an lvalue) a function (in which case it is a function designator) or |
643 | /// an enum constant. |
644 | class ValueDecl : public NamedDecl { |
645 | QualType DeclType; |
646 | |
647 | void anchor() override; |
648 | |
649 | protected: |
650 | ValueDecl(Kind DK, DeclContext *DC, SourceLocation L, |
651 | DeclarationName N, QualType T) |
652 | : NamedDecl(DK, DC, L, N), DeclType(T) {} |
653 | |
654 | public: |
655 | QualType getType() const { return DeclType; } |
656 | void setType(QualType newType) { DeclType = newType; } |
657 | |
658 | /// Determine whether this symbol is weakly-imported, |
659 | /// or declared with the weak or weak-ref attr. |
660 | bool isWeak() const; |
661 | |
662 | // Implement isa/cast/dyncast/etc. |
663 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
664 | static bool classofKind(Kind K) { return K >= firstValue && K <= lastValue; } |
665 | }; |
666 | |
667 | /// A struct with extended info about a syntactic |
668 | /// name qualifier, to be used for the case of out-of-line declarations. |
669 | struct QualifierInfo { |
670 | NestedNameSpecifierLoc QualifierLoc; |
671 | |
672 | /// The number of "outer" template parameter lists. |
673 | /// The count includes all of the template parameter lists that were matched |
674 | /// against the template-ids occurring into the NNS and possibly (in the |
675 | /// case of an explicit specialization) a final "template <>". |
676 | unsigned NumTemplParamLists = 0; |
677 | |
678 | /// A new-allocated array of size NumTemplParamLists, |
679 | /// containing pointers to the "outer" template parameter lists. |
680 | /// It includes all of the template parameter lists that were matched |
681 | /// against the template-ids occurring into the NNS and possibly (in the |
682 | /// case of an explicit specialization) a final "template <>". |
683 | TemplateParameterList** TemplParamLists = nullptr; |
684 | |
685 | QualifierInfo() = default; |
686 | QualifierInfo(const QualifierInfo &) = delete; |
687 | QualifierInfo& operator=(const QualifierInfo &) = delete; |
688 | |
689 | /// Sets info about "outer" template parameter lists. |
690 | void setTemplateParameterListsInfo(ASTContext &Context, |
691 | ArrayRef<TemplateParameterList *> TPLists); |
692 | }; |
693 | |
694 | /// Represents a ValueDecl that came out of a declarator. |
695 | /// Contains type source information through TypeSourceInfo. |
696 | class DeclaratorDecl : public ValueDecl { |
697 | // A struct representing a TInfo, a trailing requires-clause and a syntactic |
698 | // qualifier, to be used for the (uncommon) case of out-of-line declarations |
699 | // and constrained function decls. |
700 | struct ExtInfo : public QualifierInfo { |
701 | TypeSourceInfo *TInfo; |
702 | Expr *TrailingRequiresClause = nullptr; |
703 | }; |
704 | |
705 | llvm::PointerUnion<TypeSourceInfo *, ExtInfo *> DeclInfo; |
706 | |
707 | /// The start of the source range for this declaration, |
708 | /// ignoring outer template declarations. |
709 | SourceLocation InnerLocStart; |
710 | |
711 | bool hasExtInfo() const { return DeclInfo.is<ExtInfo*>(); } |
712 | ExtInfo *getExtInfo() { return DeclInfo.get<ExtInfo*>(); } |
713 | const ExtInfo *getExtInfo() const { return DeclInfo.get<ExtInfo*>(); } |
714 | |
715 | protected: |
716 | DeclaratorDecl(Kind DK, DeclContext *DC, SourceLocation L, |
717 | DeclarationName N, QualType T, TypeSourceInfo *TInfo, |
718 | SourceLocation StartL) |
719 | : ValueDecl(DK, DC, L, N, T), DeclInfo(TInfo), InnerLocStart(StartL) {} |
720 | |
721 | public: |
722 | friend class ASTDeclReader; |
723 | friend class ASTDeclWriter; |
724 | |
725 | TypeSourceInfo *getTypeSourceInfo() const { |
726 | return hasExtInfo() |
727 | ? getExtInfo()->TInfo |
728 | : DeclInfo.get<TypeSourceInfo*>(); |
729 | } |
730 | |
731 | void setTypeSourceInfo(TypeSourceInfo *TI) { |
732 | if (hasExtInfo()) |
733 | getExtInfo()->TInfo = TI; |
734 | else |
735 | DeclInfo = TI; |
736 | } |
737 | |
738 | /// Return start of source range ignoring outer template declarations. |
739 | SourceLocation getInnerLocStart() const { return InnerLocStart; } |
740 | void setInnerLocStart(SourceLocation L) { InnerLocStart = L; } |
741 | |
742 | /// Return start of source range taking into account any outer template |
743 | /// declarations. |
744 | SourceLocation getOuterLocStart() const; |
745 | |
746 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
747 | |
748 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { |
749 | return getOuterLocStart(); |
750 | } |
751 | |
752 | /// Retrieve the nested-name-specifier that qualifies the name of this |
753 | /// declaration, if it was present in the source. |
754 | NestedNameSpecifier *getQualifier() const { |
755 | return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() |
756 | : nullptr; |
757 | } |
758 | |
759 | /// Retrieve the nested-name-specifier (with source-location |
760 | /// information) that qualifies the name of this declaration, if it was |
761 | /// present in the source. |
762 | NestedNameSpecifierLoc getQualifierLoc() const { |
763 | return hasExtInfo() ? getExtInfo()->QualifierLoc |
764 | : NestedNameSpecifierLoc(); |
765 | } |
766 | |
767 | void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); |
768 | |
769 | /// \brief Get the constraint-expression introduced by the trailing |
770 | /// requires-clause in the function/member declaration, or null if no |
771 | /// requires-clause was provided. |
772 | Expr *getTrailingRequiresClause() { |
773 | return hasExtInfo() ? getExtInfo()->TrailingRequiresClause |
774 | : nullptr; |
775 | } |
776 | |
777 | const Expr *getTrailingRequiresClause() const { |
778 | return hasExtInfo() ? getExtInfo()->TrailingRequiresClause |
779 | : nullptr; |
780 | } |
781 | |
782 | void setTrailingRequiresClause(Expr *TrailingRequiresClause); |
783 | |
784 | unsigned getNumTemplateParameterLists() const { |
785 | return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; |
786 | } |
787 | |
788 | TemplateParameterList *getTemplateParameterList(unsigned index) const { |
789 | assert(index < getNumTemplateParameterLists())((index < getNumTemplateParameterLists()) ? static_cast< void> (0) : __assert_fail ("index < getNumTemplateParameterLists()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 789, __PRETTY_FUNCTION__)); |
790 | return getExtInfo()->TemplParamLists[index]; |
791 | } |
792 | |
793 | void setTemplateParameterListsInfo(ASTContext &Context, |
794 | ArrayRef<TemplateParameterList *> TPLists); |
795 | |
796 | SourceLocation getTypeSpecStartLoc() const; |
797 | SourceLocation getTypeSpecEndLoc() const; |
798 | |
799 | // Implement isa/cast/dyncast/etc. |
800 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
801 | static bool classofKind(Kind K) { |
802 | return K >= firstDeclarator && K <= lastDeclarator; |
803 | } |
804 | }; |
805 | |
806 | /// Structure used to store a statement, the constant value to |
807 | /// which it was evaluated (if any), and whether or not the statement |
808 | /// is an integral constant expression (if known). |
809 | struct EvaluatedStmt { |
810 | /// Whether this statement was already evaluated. |
811 | bool WasEvaluated : 1; |
812 | |
813 | /// Whether this statement is being evaluated. |
814 | bool IsEvaluating : 1; |
815 | |
816 | /// Whether this variable is known to have constant initialization. This is |
817 | /// currently only computed in C++, for static / thread storage duration |
818 | /// variables that might have constant initialization and for variables that |
819 | /// are usable in constant expressions. |
820 | bool HasConstantInitialization : 1; |
821 | |
822 | /// Whether this variable is known to have constant destruction. That is, |
823 | /// whether running the destructor on the initial value is a side-effect |
824 | /// (and doesn't inspect any state that might have changed during program |
825 | /// execution). This is currently only computed if the destructor is |
826 | /// non-trivial. |
827 | bool HasConstantDestruction : 1; |
828 | |
829 | /// In C++98, whether the initializer is an ICE. This affects whether the |
830 | /// variable is usable in constant expressions. |
831 | bool HasICEInit : 1; |
832 | bool CheckedForICEInit : 1; |
833 | |
834 | Stmt *Value; |
835 | APValue Evaluated; |
836 | |
837 | EvaluatedStmt() |
838 | : WasEvaluated(false), IsEvaluating(false), |
839 | HasConstantInitialization(false), HasConstantDestruction(false), |
840 | HasICEInit(false), CheckedForICEInit(false) {} |
841 | }; |
842 | |
843 | /// Represents a variable declaration or definition. |
844 | class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> { |
845 | public: |
846 | /// Initialization styles. |
847 | enum InitializationStyle { |
848 | /// C-style initialization with assignment |
849 | CInit, |
850 | |
851 | /// Call-style initialization (C++98) |
852 | CallInit, |
853 | |
854 | /// Direct list-initialization (C++11) |
855 | ListInit |
856 | }; |
857 | |
858 | /// Kinds of thread-local storage. |
859 | enum TLSKind { |
860 | /// Not a TLS variable. |
861 | TLS_None, |
862 | |
863 | /// TLS with a known-constant initializer. |
864 | TLS_Static, |
865 | |
866 | /// TLS with a dynamic initializer. |
867 | TLS_Dynamic |
868 | }; |
869 | |
870 | /// Return the string used to specify the storage class \p SC. |
871 | /// |
872 | /// It is illegal to call this function with SC == None. |
873 | static const char *getStorageClassSpecifierString(StorageClass SC); |
874 | |
875 | protected: |
876 | // A pointer union of Stmt * and EvaluatedStmt *. When an EvaluatedStmt, we |
877 | // have allocated the auxiliary struct of information there. |
878 | // |
879 | // TODO: It is a bit unfortunate to use a PointerUnion inside the VarDecl for |
880 | // this as *many* VarDecls are ParmVarDecls that don't have default |
881 | // arguments. We could save some space by moving this pointer union to be |
882 | // allocated in trailing space when necessary. |
883 | using InitType = llvm::PointerUnion<Stmt *, EvaluatedStmt *>; |
884 | |
885 | /// The initializer for this variable or, for a ParmVarDecl, the |
886 | /// C++ default argument. |
887 | mutable InitType Init; |
888 | |
889 | private: |
890 | friend class ASTDeclReader; |
891 | friend class ASTNodeImporter; |
892 | friend class StmtIteratorBase; |
893 | |
894 | class VarDeclBitfields { |
895 | friend class ASTDeclReader; |
896 | friend class VarDecl; |
897 | |
898 | unsigned SClass : 3; |
899 | unsigned TSCSpec : 2; |
900 | unsigned InitStyle : 2; |
901 | |
902 | /// Whether this variable is an ARC pseudo-__strong variable; see |
903 | /// isARCPseudoStrong() for details. |
904 | unsigned ARCPseudoStrong : 1; |
905 | }; |
906 | enum { NumVarDeclBits = 8 }; |
907 | |
908 | protected: |
909 | enum { NumParameterIndexBits = 8 }; |
910 | |
911 | enum DefaultArgKind { |
912 | DAK_None, |
913 | DAK_Unparsed, |
914 | DAK_Uninstantiated, |
915 | DAK_Normal |
916 | }; |
917 | |
918 | enum { NumScopeDepthOrObjCQualsBits = 7 }; |
919 | |
920 | class ParmVarDeclBitfields { |
921 | friend class ASTDeclReader; |
922 | friend class ParmVarDecl; |
923 | |
924 | unsigned : NumVarDeclBits; |
925 | |
926 | /// Whether this parameter inherits a default argument from a |
927 | /// prior declaration. |
928 | unsigned HasInheritedDefaultArg : 1; |
929 | |
930 | /// Describes the kind of default argument for this parameter. By default |
931 | /// this is none. If this is normal, then the default argument is stored in |
932 | /// the \c VarDecl initializer expression unless we were unable to parse |
933 | /// (even an invalid) expression for the default argument. |
934 | unsigned DefaultArgKind : 2; |
935 | |
936 | /// Whether this parameter undergoes K&R argument promotion. |
937 | unsigned IsKNRPromoted : 1; |
938 | |
939 | /// Whether this parameter is an ObjC method parameter or not. |
940 | unsigned IsObjCMethodParam : 1; |
941 | |
942 | /// If IsObjCMethodParam, a Decl::ObjCDeclQualifier. |
943 | /// Otherwise, the number of function parameter scopes enclosing |
944 | /// the function parameter scope in which this parameter was |
945 | /// declared. |
946 | unsigned ScopeDepthOrObjCQuals : NumScopeDepthOrObjCQualsBits; |
947 | |
948 | /// The number of parameters preceding this parameter in the |
949 | /// function parameter scope in which it was declared. |
950 | unsigned ParameterIndex : NumParameterIndexBits; |
951 | }; |
952 | |
953 | class NonParmVarDeclBitfields { |
954 | friend class ASTDeclReader; |
955 | friend class ImplicitParamDecl; |
956 | friend class VarDecl; |
957 | |
958 | unsigned : NumVarDeclBits; |
959 | |
960 | // FIXME: We need something similar to CXXRecordDecl::DefinitionData. |
961 | /// Whether this variable is a definition which was demoted due to |
962 | /// module merge. |
963 | unsigned IsThisDeclarationADemotedDefinition : 1; |
964 | |
965 | /// Whether this variable is the exception variable in a C++ catch |
966 | /// or an Objective-C @catch statement. |
967 | unsigned ExceptionVar : 1; |
968 | |
969 | /// Whether this local variable could be allocated in the return |
970 | /// slot of its function, enabling the named return value optimization |
971 | /// (NRVO). |
972 | unsigned NRVOVariable : 1; |
973 | |
974 | /// Whether this variable is the for-range-declaration in a C++0x |
975 | /// for-range statement. |
976 | unsigned CXXForRangeDecl : 1; |
977 | |
978 | /// Whether this variable is the for-in loop declaration in Objective-C. |
979 | unsigned ObjCForDecl : 1; |
980 | |
981 | /// Whether this variable is (C++1z) inline. |
982 | unsigned IsInline : 1; |
983 | |
984 | /// Whether this variable has (C++1z) inline explicitly specified. |
985 | unsigned IsInlineSpecified : 1; |
986 | |
987 | /// Whether this variable is (C++0x) constexpr. |
988 | unsigned IsConstexpr : 1; |
989 | |
990 | /// Whether this variable is the implicit variable for a lambda |
991 | /// init-capture. |
992 | unsigned IsInitCapture : 1; |
993 | |
994 | /// Whether this local extern variable's previous declaration was |
995 | /// declared in the same block scope. This controls whether we should merge |
996 | /// the type of this declaration with its previous declaration. |
997 | unsigned PreviousDeclInSameBlockScope : 1; |
998 | |
999 | /// Defines kind of the ImplicitParamDecl: 'this', 'self', 'vtt', '_cmd' or |
1000 | /// something else. |
1001 | unsigned ImplicitParamKind : 3; |
1002 | |
1003 | unsigned EscapingByref : 1; |
1004 | }; |
1005 | |
1006 | union { |
1007 | unsigned AllBits; |
1008 | VarDeclBitfields VarDeclBits; |
1009 | ParmVarDeclBitfields ParmVarDeclBits; |
1010 | NonParmVarDeclBitfields NonParmVarDeclBits; |
1011 | }; |
1012 | |
1013 | VarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1014 | SourceLocation IdLoc, IdentifierInfo *Id, QualType T, |
1015 | TypeSourceInfo *TInfo, StorageClass SC); |
1016 | |
1017 | using redeclarable_base = Redeclarable<VarDecl>; |
1018 | |
1019 | VarDecl *getNextRedeclarationImpl() override { |
1020 | return getNextRedeclaration(); |
1021 | } |
1022 | |
1023 | VarDecl *getPreviousDeclImpl() override { |
1024 | return getPreviousDecl(); |
1025 | } |
1026 | |
1027 | VarDecl *getMostRecentDeclImpl() override { |
1028 | return getMostRecentDecl(); |
1029 | } |
1030 | |
1031 | public: |
1032 | using redecl_range = redeclarable_base::redecl_range; |
1033 | using redecl_iterator = redeclarable_base::redecl_iterator; |
1034 | |
1035 | using redeclarable_base::redecls_begin; |
1036 | using redeclarable_base::redecls_end; |
1037 | using redeclarable_base::redecls; |
1038 | using redeclarable_base::getPreviousDecl; |
1039 | using redeclarable_base::getMostRecentDecl; |
1040 | using redeclarable_base::isFirstDecl; |
1041 | |
1042 | static VarDecl *Create(ASTContext &C, DeclContext *DC, |
1043 | SourceLocation StartLoc, SourceLocation IdLoc, |
1044 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
1045 | StorageClass S); |
1046 | |
1047 | static VarDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1048 | |
1049 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
1050 | |
1051 | /// Returns the storage class as written in the source. For the |
1052 | /// computed linkage of symbol, see getLinkage. |
1053 | StorageClass getStorageClass() const { |
1054 | return (StorageClass) VarDeclBits.SClass; |
1055 | } |
1056 | void setStorageClass(StorageClass SC); |
1057 | |
1058 | void setTSCSpec(ThreadStorageClassSpecifier TSC) { |
1059 | VarDeclBits.TSCSpec = TSC; |
1060 | assert(VarDeclBits.TSCSpec == TSC && "truncation")((VarDeclBits.TSCSpec == TSC && "truncation") ? static_cast <void> (0) : __assert_fail ("VarDeclBits.TSCSpec == TSC && \"truncation\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1060, __PRETTY_FUNCTION__)); |
1061 | } |
1062 | ThreadStorageClassSpecifier getTSCSpec() const { |
1063 | return static_cast<ThreadStorageClassSpecifier>(VarDeclBits.TSCSpec); |
1064 | } |
1065 | TLSKind getTLSKind() const; |
1066 | |
1067 | /// Returns true if a variable with function scope is a non-static local |
1068 | /// variable. |
1069 | bool hasLocalStorage() const { |
1070 | if (getStorageClass() == SC_None) { |
1071 | // OpenCL v1.2 s6.5.3: The __constant or constant address space name is |
1072 | // used to describe variables allocated in global memory and which are |
1073 | // accessed inside a kernel(s) as read-only variables. As such, variables |
1074 | // in constant address space cannot have local storage. |
1075 | if (getType().getAddressSpace() == LangAS::opencl_constant) |
1076 | return false; |
1077 | // Second check is for C++11 [dcl.stc]p4. |
1078 | return !isFileVarDecl() && getTSCSpec() == TSCS_unspecified; |
1079 | } |
1080 | |
1081 | // Global Named Register (GNU extension) |
1082 | if (getStorageClass() == SC_Register && !isLocalVarDeclOrParm()) |
1083 | return false; |
1084 | |
1085 | // Return true for: Auto, Register. |
1086 | // Return false for: Extern, Static, PrivateExtern, OpenCLWorkGroupLocal. |
1087 | |
1088 | return getStorageClass() >= SC_Auto; |
1089 | } |
1090 | |
1091 | /// Returns true if a variable with function scope is a static local |
1092 | /// variable. |
1093 | bool isStaticLocal() const { |
1094 | return (getStorageClass() == SC_Static || |
1095 | // C++11 [dcl.stc]p4 |
1096 | (getStorageClass() == SC_None && getTSCSpec() == TSCS_thread_local)) |
1097 | && !isFileVarDecl(); |
1098 | } |
1099 | |
1100 | /// Returns true if a variable has extern or __private_extern__ |
1101 | /// storage. |
1102 | bool hasExternalStorage() const { |
1103 | return getStorageClass() == SC_Extern || |
1104 | getStorageClass() == SC_PrivateExtern; |
1105 | } |
1106 | |
1107 | /// Returns true for all variables that do not have local storage. |
1108 | /// |
1109 | /// This includes all global variables as well as static variables declared |
1110 | /// within a function. |
1111 | bool hasGlobalStorage() const { return !hasLocalStorage(); } |
1112 | |
1113 | /// Get the storage duration of this variable, per C++ [basic.stc]. |
1114 | StorageDuration getStorageDuration() const { |
1115 | return hasLocalStorage() ? SD_Automatic : |
1116 | getTSCSpec() ? SD_Thread : SD_Static; |
1117 | } |
1118 | |
1119 | /// Compute the language linkage. |
1120 | LanguageLinkage getLanguageLinkage() const; |
1121 | |
1122 | /// Determines whether this variable is a variable with external, C linkage. |
1123 | bool isExternC() const; |
1124 | |
1125 | /// Determines whether this variable's context is, or is nested within, |
1126 | /// a C++ extern "C" linkage spec. |
1127 | bool isInExternCContext() const; |
1128 | |
1129 | /// Determines whether this variable's context is, or is nested within, |
1130 | /// a C++ extern "C++" linkage spec. |
1131 | bool isInExternCXXContext() const; |
1132 | |
1133 | /// Returns true for local variable declarations other than parameters. |
1134 | /// Note that this includes static variables inside of functions. It also |
1135 | /// includes variables inside blocks. |
1136 | /// |
1137 | /// void foo() { int x; static int y; extern int z; } |
1138 | bool isLocalVarDecl() const { |
1139 | if (getKind() != Decl::Var && getKind() != Decl::Decomposition) |
1140 | return false; |
1141 | if (const DeclContext *DC = getLexicalDeclContext()) |
1142 | return DC->getRedeclContext()->isFunctionOrMethod(); |
1143 | return false; |
1144 | } |
1145 | |
1146 | /// Similar to isLocalVarDecl but also includes parameters. |
1147 | bool isLocalVarDeclOrParm() const { |
1148 | return isLocalVarDecl() || getKind() == Decl::ParmVar; |
1149 | } |
1150 | |
1151 | /// Similar to isLocalVarDecl, but excludes variables declared in blocks. |
1152 | bool isFunctionOrMethodVarDecl() const { |
1153 | if (getKind() != Decl::Var && getKind() != Decl::Decomposition) |
1154 | return false; |
1155 | const DeclContext *DC = getLexicalDeclContext()->getRedeclContext(); |
1156 | return DC->isFunctionOrMethod() && DC->getDeclKind() != Decl::Block; |
1157 | } |
1158 | |
1159 | /// Determines whether this is a static data member. |
1160 | /// |
1161 | /// This will only be true in C++, and applies to, e.g., the |
1162 | /// variable 'x' in: |
1163 | /// \code |
1164 | /// struct S { |
1165 | /// static int x; |
1166 | /// }; |
1167 | /// \endcode |
1168 | bool isStaticDataMember() const { |
1169 | // If it wasn't static, it would be a FieldDecl. |
1170 | return getKind() != Decl::ParmVar && getDeclContext()->isRecord(); |
1171 | } |
1172 | |
1173 | VarDecl *getCanonicalDecl() override; |
1174 | const VarDecl *getCanonicalDecl() const { |
1175 | return const_cast<VarDecl*>(this)->getCanonicalDecl(); |
1176 | } |
1177 | |
1178 | enum DefinitionKind { |
1179 | /// This declaration is only a declaration. |
1180 | DeclarationOnly, |
1181 | |
1182 | /// This declaration is a tentative definition. |
1183 | TentativeDefinition, |
1184 | |
1185 | /// This declaration is definitely a definition. |
1186 | Definition |
1187 | }; |
1188 | |
1189 | /// Check whether this declaration is a definition. If this could be |
1190 | /// a tentative definition (in C), don't check whether there's an overriding |
1191 | /// definition. |
1192 | DefinitionKind isThisDeclarationADefinition(ASTContext &) const; |
1193 | DefinitionKind isThisDeclarationADefinition() const { |
1194 | return isThisDeclarationADefinition(getASTContext()); |
1195 | } |
1196 | |
1197 | /// Check whether this variable is defined in this translation unit. |
1198 | DefinitionKind hasDefinition(ASTContext &) const; |
1199 | DefinitionKind hasDefinition() const { |
1200 | return hasDefinition(getASTContext()); |
1201 | } |
1202 | |
1203 | /// Get the tentative definition that acts as the real definition in a TU. |
1204 | /// Returns null if there is a proper definition available. |
1205 | VarDecl *getActingDefinition(); |
1206 | const VarDecl *getActingDefinition() const { |
1207 | return const_cast<VarDecl*>(this)->getActingDefinition(); |
1208 | } |
1209 | |
1210 | /// Get the real (not just tentative) definition for this declaration. |
1211 | VarDecl *getDefinition(ASTContext &); |
1212 | const VarDecl *getDefinition(ASTContext &C) const { |
1213 | return const_cast<VarDecl*>(this)->getDefinition(C); |
1214 | } |
1215 | VarDecl *getDefinition() { |
1216 | return getDefinition(getASTContext()); |
1217 | } |
1218 | const VarDecl *getDefinition() const { |
1219 | return const_cast<VarDecl*>(this)->getDefinition(); |
1220 | } |
1221 | |
1222 | /// Determine whether this is or was instantiated from an out-of-line |
1223 | /// definition of a static data member. |
1224 | bool isOutOfLine() const override; |
1225 | |
1226 | /// Returns true for file scoped variable declaration. |
1227 | bool isFileVarDecl() const { |
1228 | Kind K = getKind(); |
1229 | if (K == ParmVar || K == ImplicitParam) |
1230 | return false; |
1231 | |
1232 | if (getLexicalDeclContext()->getRedeclContext()->isFileContext()) |
1233 | return true; |
1234 | |
1235 | if (isStaticDataMember()) |
1236 | return true; |
1237 | |
1238 | return false; |
1239 | } |
1240 | |
1241 | /// Get the initializer for this variable, no matter which |
1242 | /// declaration it is attached to. |
1243 | const Expr *getAnyInitializer() const { |
1244 | const VarDecl *D; |
1245 | return getAnyInitializer(D); |
1246 | } |
1247 | |
1248 | /// Get the initializer for this variable, no matter which |
1249 | /// declaration it is attached to. Also get that declaration. |
1250 | const Expr *getAnyInitializer(const VarDecl *&D) const; |
1251 | |
1252 | bool hasInit() const; |
1253 | const Expr *getInit() const { |
1254 | return const_cast<VarDecl *>(this)->getInit(); |
1255 | } |
1256 | Expr *getInit(); |
1257 | |
1258 | /// Retrieve the address of the initializer expression. |
1259 | Stmt **getInitAddress(); |
1260 | |
1261 | void setInit(Expr *I); |
1262 | |
1263 | /// Get the initializing declaration of this variable, if any. This is |
1264 | /// usually the definition, except that for a static data member it can be |
1265 | /// the in-class declaration. |
1266 | VarDecl *getInitializingDeclaration(); |
1267 | const VarDecl *getInitializingDeclaration() const { |
1268 | return const_cast<VarDecl *>(this)->getInitializingDeclaration(); |
1269 | } |
1270 | |
1271 | /// Determine whether this variable's value might be usable in a |
1272 | /// constant expression, according to the relevant language standard. |
1273 | /// This only checks properties of the declaration, and does not check |
1274 | /// whether the initializer is in fact a constant expression. |
1275 | /// |
1276 | /// This corresponds to C++20 [expr.const]p3's notion of a |
1277 | /// "potentially-constant" variable. |
1278 | bool mightBeUsableInConstantExpressions(const ASTContext &C) const; |
1279 | |
1280 | /// Determine whether this variable's value can be used in a |
1281 | /// constant expression, according to the relevant language standard, |
1282 | /// including checking whether it was initialized by a constant expression. |
1283 | bool isUsableInConstantExpressions(const ASTContext &C) const; |
1284 | |
1285 | EvaluatedStmt *ensureEvaluatedStmt() const; |
1286 | EvaluatedStmt *getEvaluatedStmt() const; |
1287 | |
1288 | /// Attempt to evaluate the value of the initializer attached to this |
1289 | /// declaration, and produce notes explaining why it cannot be evaluated. |
1290 | /// Returns a pointer to the value if evaluation succeeded, 0 otherwise. |
1291 | APValue *evaluateValue() const; |
1292 | |
1293 | private: |
1294 | APValue *evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes, |
1295 | bool IsConstantInitialization) const; |
1296 | |
1297 | public: |
1298 | /// Return the already-evaluated value of this variable's |
1299 | /// initializer, or NULL if the value is not yet known. Returns pointer |
1300 | /// to untyped APValue if the value could not be evaluated. |
1301 | APValue *getEvaluatedValue() const; |
1302 | |
1303 | /// Evaluate the destruction of this variable to determine if it constitutes |
1304 | /// constant destruction. |
1305 | /// |
1306 | /// \pre hasConstantInitialization() |
1307 | /// \return \c true if this variable has constant destruction, \c false if |
1308 | /// not. |
1309 | bool evaluateDestruction(SmallVectorImpl<PartialDiagnosticAt> &Notes) const; |
1310 | |
1311 | /// Determine whether this variable has constant initialization. |
1312 | /// |
1313 | /// This is only set in two cases: when the language semantics require |
1314 | /// constant initialization (globals in C and some globals in C++), and when |
1315 | /// the variable is usable in constant expressions (constexpr, const int, and |
1316 | /// reference variables in C++). |
1317 | bool hasConstantInitialization() const; |
1318 | |
1319 | /// Determine whether the initializer of this variable is an integer constant |
1320 | /// expression. For use in C++98, where this affects whether the variable is |
1321 | /// usable in constant expressions. |
1322 | bool hasICEInitializer(const ASTContext &Context) const; |
1323 | |
1324 | /// Evaluate the initializer of this variable to determine whether it's a |
1325 | /// constant initializer. Should only be called once, after completing the |
1326 | /// definition of the variable. |
1327 | bool checkForConstantInitialization( |
1328 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const; |
1329 | |
1330 | void setInitStyle(InitializationStyle Style) { |
1331 | VarDeclBits.InitStyle = Style; |
1332 | } |
1333 | |
1334 | /// The style of initialization for this declaration. |
1335 | /// |
1336 | /// C-style initialization is "int x = 1;". Call-style initialization is |
1337 | /// a C++98 direct-initializer, e.g. "int x(1);". The Init expression will be |
1338 | /// the expression inside the parens or a "ClassType(a,b,c)" class constructor |
1339 | /// expression for class types. List-style initialization is C++11 syntax, |
1340 | /// e.g. "int x{1};". Clients can distinguish between different forms of |
1341 | /// initialization by checking this value. In particular, "int x = {1};" is |
1342 | /// C-style, "int x({1})" is call-style, and "int x{1};" is list-style; the |
1343 | /// Init expression in all three cases is an InitListExpr. |
1344 | InitializationStyle getInitStyle() const { |
1345 | return static_cast<InitializationStyle>(VarDeclBits.InitStyle); |
1346 | } |
1347 | |
1348 | /// Whether the initializer is a direct-initializer (list or call). |
1349 | bool isDirectInit() const { |
1350 | return getInitStyle() != CInit; |
1351 | } |
1352 | |
1353 | /// If this definition should pretend to be a declaration. |
1354 | bool isThisDeclarationADemotedDefinition() const { |
1355 | return isa<ParmVarDecl>(this) ? false : |
1356 | NonParmVarDeclBits.IsThisDeclarationADemotedDefinition; |
1357 | } |
1358 | |
1359 | /// This is a definition which should be demoted to a declaration. |
1360 | /// |
1361 | /// In some cases (mostly module merging) we can end up with two visible |
1362 | /// definitions one of which needs to be demoted to a declaration to keep |
1363 | /// the AST invariants. |
1364 | void demoteThisDefinitionToDeclaration() { |
1365 | assert(isThisDeclarationADefinition() && "Not a definition!")((isThisDeclarationADefinition() && "Not a definition!" ) ? static_cast<void> (0) : __assert_fail ("isThisDeclarationADefinition() && \"Not a definition!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1365, __PRETTY_FUNCTION__)); |
1366 | assert(!isa<ParmVarDecl>(this) && "Cannot demote ParmVarDecls!")((!isa<ParmVarDecl>(this) && "Cannot demote ParmVarDecls!" ) ? static_cast<void> (0) : __assert_fail ("!isa<ParmVarDecl>(this) && \"Cannot demote ParmVarDecls!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1366, __PRETTY_FUNCTION__)); |
1367 | NonParmVarDeclBits.IsThisDeclarationADemotedDefinition = 1; |
1368 | } |
1369 | |
1370 | /// Determine whether this variable is the exception variable in a |
1371 | /// C++ catch statememt or an Objective-C \@catch statement. |
1372 | bool isExceptionVariable() const { |
1373 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.ExceptionVar; |
1374 | } |
1375 | void setExceptionVariable(bool EV) { |
1376 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1376, __PRETTY_FUNCTION__)); |
1377 | NonParmVarDeclBits.ExceptionVar = EV; |
1378 | } |
1379 | |
1380 | /// Determine whether this local variable can be used with the named |
1381 | /// return value optimization (NRVO). |
1382 | /// |
1383 | /// The named return value optimization (NRVO) works by marking certain |
1384 | /// non-volatile local variables of class type as NRVO objects. These |
1385 | /// locals can be allocated within the return slot of their containing |
1386 | /// function, in which case there is no need to copy the object to the |
1387 | /// return slot when returning from the function. Within the function body, |
1388 | /// each return that returns the NRVO object will have this variable as its |
1389 | /// NRVO candidate. |
1390 | bool isNRVOVariable() const { |
1391 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.NRVOVariable; |
1392 | } |
1393 | void setNRVOVariable(bool NRVO) { |
1394 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1394, __PRETTY_FUNCTION__)); |
1395 | NonParmVarDeclBits.NRVOVariable = NRVO; |
1396 | } |
1397 | |
1398 | /// Determine whether this variable is the for-range-declaration in |
1399 | /// a C++0x for-range statement. |
1400 | bool isCXXForRangeDecl() const { |
1401 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.CXXForRangeDecl; |
1402 | } |
1403 | void setCXXForRangeDecl(bool FRD) { |
1404 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1404, __PRETTY_FUNCTION__)); |
1405 | NonParmVarDeclBits.CXXForRangeDecl = FRD; |
1406 | } |
1407 | |
1408 | /// Determine whether this variable is a for-loop declaration for a |
1409 | /// for-in statement in Objective-C. |
1410 | bool isObjCForDecl() const { |
1411 | return NonParmVarDeclBits.ObjCForDecl; |
1412 | } |
1413 | |
1414 | void setObjCForDecl(bool FRD) { |
1415 | NonParmVarDeclBits.ObjCForDecl = FRD; |
1416 | } |
1417 | |
1418 | /// Determine whether this variable is an ARC pseudo-__strong variable. A |
1419 | /// pseudo-__strong variable has a __strong-qualified type but does not |
1420 | /// actually retain the object written into it. Generally such variables are |
1421 | /// also 'const' for safety. There are 3 cases where this will be set, 1) if |
1422 | /// the variable is annotated with the objc_externally_retained attribute, 2) |
1423 | /// if its 'self' in a non-init method, or 3) if its the variable in an for-in |
1424 | /// loop. |
1425 | bool isARCPseudoStrong() const { return VarDeclBits.ARCPseudoStrong; } |
1426 | void setARCPseudoStrong(bool PS) { VarDeclBits.ARCPseudoStrong = PS; } |
1427 | |
1428 | /// Whether this variable is (C++1z) inline. |
1429 | bool isInline() const { |
1430 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInline; |
1431 | } |
1432 | bool isInlineSpecified() const { |
1433 | return isa<ParmVarDecl>(this) ? false |
1434 | : NonParmVarDeclBits.IsInlineSpecified; |
1435 | } |
1436 | void setInlineSpecified() { |
1437 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1437, __PRETTY_FUNCTION__)); |
1438 | NonParmVarDeclBits.IsInline = true; |
1439 | NonParmVarDeclBits.IsInlineSpecified = true; |
1440 | } |
1441 | void setImplicitlyInline() { |
1442 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1442, __PRETTY_FUNCTION__)); |
1443 | NonParmVarDeclBits.IsInline = true; |
1444 | } |
1445 | |
1446 | /// Whether this variable is (C++11) constexpr. |
1447 | bool isConstexpr() const { |
1448 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsConstexpr; |
1449 | } |
1450 | void setConstexpr(bool IC) { |
1451 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1451, __PRETTY_FUNCTION__)); |
1452 | NonParmVarDeclBits.IsConstexpr = IC; |
1453 | } |
1454 | |
1455 | /// Whether this variable is the implicit variable for a lambda init-capture. |
1456 | bool isInitCapture() const { |
1457 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInitCapture; |
1458 | } |
1459 | void setInitCapture(bool IC) { |
1460 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1460, __PRETTY_FUNCTION__)); |
1461 | NonParmVarDeclBits.IsInitCapture = IC; |
1462 | } |
1463 | |
1464 | /// Determine whether this variable is actually a function parameter pack or |
1465 | /// init-capture pack. |
1466 | bool isParameterPack() const; |
1467 | |
1468 | /// Whether this local extern variable declaration's previous declaration |
1469 | /// was declared in the same block scope. Only correct in C++. |
1470 | bool isPreviousDeclInSameBlockScope() const { |
1471 | return isa<ParmVarDecl>(this) |
1472 | ? false |
1473 | : NonParmVarDeclBits.PreviousDeclInSameBlockScope; |
1474 | } |
1475 | void setPreviousDeclInSameBlockScope(bool Same) { |
1476 | assert(!isa<ParmVarDecl>(this))((!isa<ParmVarDecl>(this)) ? static_cast<void> (0 ) : __assert_fail ("!isa<ParmVarDecl>(this)", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1476, __PRETTY_FUNCTION__)); |
1477 | NonParmVarDeclBits.PreviousDeclInSameBlockScope = Same; |
1478 | } |
1479 | |
1480 | /// Indicates the capture is a __block variable that is captured by a block |
1481 | /// that can potentially escape (a block for which BlockDecl::doesNotEscape |
1482 | /// returns false). |
1483 | bool isEscapingByref() const; |
1484 | |
1485 | /// Indicates the capture is a __block variable that is never captured by an |
1486 | /// escaping block. |
1487 | bool isNonEscapingByref() const; |
1488 | |
1489 | void setEscapingByref() { |
1490 | NonParmVarDeclBits.EscapingByref = true; |
1491 | } |
1492 | |
1493 | /// Retrieve the variable declaration from which this variable could |
1494 | /// be instantiated, if it is an instantiation (rather than a non-template). |
1495 | VarDecl *getTemplateInstantiationPattern() const; |
1496 | |
1497 | /// If this variable is an instantiated static data member of a |
1498 | /// class template specialization, returns the templated static data member |
1499 | /// from which it was instantiated. |
1500 | VarDecl *getInstantiatedFromStaticDataMember() const; |
1501 | |
1502 | /// If this variable is an instantiation of a variable template or a |
1503 | /// static data member of a class template, determine what kind of |
1504 | /// template specialization or instantiation this is. |
1505 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
1506 | |
1507 | /// Get the template specialization kind of this variable for the purposes of |
1508 | /// template instantiation. This differs from getTemplateSpecializationKind() |
1509 | /// for an instantiation of a class-scope explicit specialization. |
1510 | TemplateSpecializationKind |
1511 | getTemplateSpecializationKindForInstantiation() const; |
1512 | |
1513 | /// If this variable is an instantiation of a variable template or a |
1514 | /// static data member of a class template, determine its point of |
1515 | /// instantiation. |
1516 | SourceLocation getPointOfInstantiation() const; |
1517 | |
1518 | /// If this variable is an instantiation of a static data member of a |
1519 | /// class template specialization, retrieves the member specialization |
1520 | /// information. |
1521 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
1522 | |
1523 | /// For a static data member that was instantiated from a static |
1524 | /// data member of a class template, set the template specialiation kind. |
1525 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
1526 | SourceLocation PointOfInstantiation = SourceLocation()); |
1527 | |
1528 | /// Specify that this variable is an instantiation of the |
1529 | /// static data member VD. |
1530 | void setInstantiationOfStaticDataMember(VarDecl *VD, |
1531 | TemplateSpecializationKind TSK); |
1532 | |
1533 | /// Retrieves the variable template that is described by this |
1534 | /// variable declaration. |
1535 | /// |
1536 | /// Every variable template is represented as a VarTemplateDecl and a |
1537 | /// VarDecl. The former contains template properties (such as |
1538 | /// the template parameter lists) while the latter contains the |
1539 | /// actual description of the template's |
1540 | /// contents. VarTemplateDecl::getTemplatedDecl() retrieves the |
1541 | /// VarDecl that from a VarTemplateDecl, while |
1542 | /// getDescribedVarTemplate() retrieves the VarTemplateDecl from |
1543 | /// a VarDecl. |
1544 | VarTemplateDecl *getDescribedVarTemplate() const; |
1545 | |
1546 | void setDescribedVarTemplate(VarTemplateDecl *Template); |
1547 | |
1548 | // Is this variable known to have a definition somewhere in the complete |
1549 | // program? This may be true even if the declaration has internal linkage and |
1550 | // has no definition within this source file. |
1551 | bool isKnownToBeDefined() const; |
1552 | |
1553 | /// Is destruction of this variable entirely suppressed? If so, the variable |
1554 | /// need not have a usable destructor at all. |
1555 | bool isNoDestroy(const ASTContext &) const; |
1556 | |
1557 | /// Would the destruction of this variable have any effect, and if so, what |
1558 | /// kind? |
1559 | QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const; |
1560 | |
1561 | // Implement isa/cast/dyncast/etc. |
1562 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1563 | static bool classofKind(Kind K) { return K >= firstVar && K <= lastVar; } |
1564 | }; |
1565 | |
1566 | class ImplicitParamDecl : public VarDecl { |
1567 | void anchor() override; |
1568 | |
1569 | public: |
1570 | /// Defines the kind of the implicit parameter: is this an implicit parameter |
1571 | /// with pointer to 'this', 'self', '_cmd', virtual table pointers, captured |
1572 | /// context or something else. |
1573 | enum ImplicitParamKind : unsigned { |
1574 | /// Parameter for Objective-C 'self' argument |
1575 | ObjCSelf, |
1576 | |
1577 | /// Parameter for Objective-C '_cmd' argument |
1578 | ObjCCmd, |
1579 | |
1580 | /// Parameter for C++ 'this' argument |
1581 | CXXThis, |
1582 | |
1583 | /// Parameter for C++ virtual table pointers |
1584 | CXXVTT, |
1585 | |
1586 | /// Parameter for captured context |
1587 | CapturedContext, |
1588 | |
1589 | /// Other implicit parameter |
1590 | Other, |
1591 | }; |
1592 | |
1593 | /// Create implicit parameter. |
1594 | static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC, |
1595 | SourceLocation IdLoc, IdentifierInfo *Id, |
1596 | QualType T, ImplicitParamKind ParamKind); |
1597 | static ImplicitParamDecl *Create(ASTContext &C, QualType T, |
1598 | ImplicitParamKind ParamKind); |
1599 | |
1600 | static ImplicitParamDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1601 | |
1602 | ImplicitParamDecl(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, |
1603 | IdentifierInfo *Id, QualType Type, |
1604 | ImplicitParamKind ParamKind) |
1605 | : VarDecl(ImplicitParam, C, DC, IdLoc, IdLoc, Id, Type, |
1606 | /*TInfo=*/nullptr, SC_None) { |
1607 | NonParmVarDeclBits.ImplicitParamKind = ParamKind; |
1608 | setImplicit(); |
1609 | } |
1610 | |
1611 | ImplicitParamDecl(ASTContext &C, QualType Type, ImplicitParamKind ParamKind) |
1612 | : VarDecl(ImplicitParam, C, /*DC=*/nullptr, SourceLocation(), |
1613 | SourceLocation(), /*Id=*/nullptr, Type, |
1614 | /*TInfo=*/nullptr, SC_None) { |
1615 | NonParmVarDeclBits.ImplicitParamKind = ParamKind; |
1616 | setImplicit(); |
1617 | } |
1618 | |
1619 | /// Returns the implicit parameter kind. |
1620 | ImplicitParamKind getParameterKind() const { |
1621 | return static_cast<ImplicitParamKind>(NonParmVarDeclBits.ImplicitParamKind); |
1622 | } |
1623 | |
1624 | // Implement isa/cast/dyncast/etc. |
1625 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1626 | static bool classofKind(Kind K) { return K == ImplicitParam; } |
1627 | }; |
1628 | |
1629 | /// Represents a parameter to a function. |
1630 | class ParmVarDecl : public VarDecl { |
1631 | public: |
1632 | enum { MaxFunctionScopeDepth = 255 }; |
1633 | enum { MaxFunctionScopeIndex = 255 }; |
1634 | |
1635 | protected: |
1636 | ParmVarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1637 | SourceLocation IdLoc, IdentifierInfo *Id, QualType T, |
1638 | TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg) |
1639 | : VarDecl(DK, C, DC, StartLoc, IdLoc, Id, T, TInfo, S) { |
1640 | assert(ParmVarDeclBits.HasInheritedDefaultArg == false)((ParmVarDeclBits.HasInheritedDefaultArg == false) ? static_cast <void> (0) : __assert_fail ("ParmVarDeclBits.HasInheritedDefaultArg == false" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1640, __PRETTY_FUNCTION__)); |
1641 | assert(ParmVarDeclBits.DefaultArgKind == DAK_None)((ParmVarDeclBits.DefaultArgKind == DAK_None) ? static_cast< void> (0) : __assert_fail ("ParmVarDeclBits.DefaultArgKind == DAK_None" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1641, __PRETTY_FUNCTION__)); |
1642 | assert(ParmVarDeclBits.IsKNRPromoted == false)((ParmVarDeclBits.IsKNRPromoted == false) ? static_cast<void > (0) : __assert_fail ("ParmVarDeclBits.IsKNRPromoted == false" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1642, __PRETTY_FUNCTION__)); |
1643 | assert(ParmVarDeclBits.IsObjCMethodParam == false)((ParmVarDeclBits.IsObjCMethodParam == false) ? static_cast< void> (0) : __assert_fail ("ParmVarDeclBits.IsObjCMethodParam == false" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1643, __PRETTY_FUNCTION__)); |
1644 | setDefaultArg(DefArg); |
1645 | } |
1646 | |
1647 | public: |
1648 | static ParmVarDecl *Create(ASTContext &C, DeclContext *DC, |
1649 | SourceLocation StartLoc, |
1650 | SourceLocation IdLoc, IdentifierInfo *Id, |
1651 | QualType T, TypeSourceInfo *TInfo, |
1652 | StorageClass S, Expr *DefArg); |
1653 | |
1654 | static ParmVarDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1655 | |
1656 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
1657 | |
1658 | void setObjCMethodScopeInfo(unsigned parameterIndex) { |
1659 | ParmVarDeclBits.IsObjCMethodParam = true; |
1660 | setParameterIndex(parameterIndex); |
1661 | } |
1662 | |
1663 | void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex) { |
1664 | assert(!ParmVarDeclBits.IsObjCMethodParam)((!ParmVarDeclBits.IsObjCMethodParam) ? static_cast<void> (0) : __assert_fail ("!ParmVarDeclBits.IsObjCMethodParam", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1664, __PRETTY_FUNCTION__)); |
1665 | |
1666 | ParmVarDeclBits.ScopeDepthOrObjCQuals = scopeDepth; |
1667 | assert(ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth((ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth && "truncation!") ? static_cast<void> (0) : __assert_fail ("ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth && \"truncation!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1668, __PRETTY_FUNCTION__)) |
1668 | && "truncation!")((ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth && "truncation!") ? static_cast<void> (0) : __assert_fail ("ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth && \"truncation!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1668, __PRETTY_FUNCTION__)); |
1669 | |
1670 | setParameterIndex(parameterIndex); |
1671 | } |
1672 | |
1673 | bool isObjCMethodParameter() const { |
1674 | return ParmVarDeclBits.IsObjCMethodParam; |
1675 | } |
1676 | |
1677 | /// Determines whether this parameter is destroyed in the callee function. |
1678 | bool isDestroyedInCallee() const; |
1679 | |
1680 | unsigned getFunctionScopeDepth() const { |
1681 | if (ParmVarDeclBits.IsObjCMethodParam) return 0; |
1682 | return ParmVarDeclBits.ScopeDepthOrObjCQuals; |
1683 | } |
1684 | |
1685 | static constexpr unsigned getMaxFunctionScopeDepth() { |
1686 | return (1u << NumScopeDepthOrObjCQualsBits) - 1; |
1687 | } |
1688 | |
1689 | /// Returns the index of this parameter in its prototype or method scope. |
1690 | unsigned getFunctionScopeIndex() const { |
1691 | return getParameterIndex(); |
1692 | } |
1693 | |
1694 | ObjCDeclQualifier getObjCDeclQualifier() const { |
1695 | if (!ParmVarDeclBits.IsObjCMethodParam) return OBJC_TQ_None; |
1696 | return ObjCDeclQualifier(ParmVarDeclBits.ScopeDepthOrObjCQuals); |
1697 | } |
1698 | void setObjCDeclQualifier(ObjCDeclQualifier QTVal) { |
1699 | assert(ParmVarDeclBits.IsObjCMethodParam)((ParmVarDeclBits.IsObjCMethodParam) ? static_cast<void> (0) : __assert_fail ("ParmVarDeclBits.IsObjCMethodParam", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1699, __PRETTY_FUNCTION__)); |
1700 | ParmVarDeclBits.ScopeDepthOrObjCQuals = QTVal; |
1701 | } |
1702 | |
1703 | /// True if the value passed to this parameter must undergo |
1704 | /// K&R-style default argument promotion: |
1705 | /// |
1706 | /// C99 6.5.2.2. |
1707 | /// If the expression that denotes the called function has a type |
1708 | /// that does not include a prototype, the integer promotions are |
1709 | /// performed on each argument, and arguments that have type float |
1710 | /// are promoted to double. |
1711 | bool isKNRPromoted() const { |
1712 | return ParmVarDeclBits.IsKNRPromoted; |
1713 | } |
1714 | void setKNRPromoted(bool promoted) { |
1715 | ParmVarDeclBits.IsKNRPromoted = promoted; |
1716 | } |
1717 | |
1718 | Expr *getDefaultArg(); |
1719 | const Expr *getDefaultArg() const { |
1720 | return const_cast<ParmVarDecl *>(this)->getDefaultArg(); |
1721 | } |
1722 | |
1723 | void setDefaultArg(Expr *defarg); |
1724 | |
1725 | /// Retrieve the source range that covers the entire default |
1726 | /// argument. |
1727 | SourceRange getDefaultArgRange() const; |
1728 | void setUninstantiatedDefaultArg(Expr *arg); |
1729 | Expr *getUninstantiatedDefaultArg(); |
1730 | const Expr *getUninstantiatedDefaultArg() const { |
1731 | return const_cast<ParmVarDecl *>(this)->getUninstantiatedDefaultArg(); |
1732 | } |
1733 | |
1734 | /// Determines whether this parameter has a default argument, |
1735 | /// either parsed or not. |
1736 | bool hasDefaultArg() const; |
1737 | |
1738 | /// Determines whether this parameter has a default argument that has not |
1739 | /// yet been parsed. This will occur during the processing of a C++ class |
1740 | /// whose member functions have default arguments, e.g., |
1741 | /// @code |
1742 | /// class X { |
1743 | /// public: |
1744 | /// void f(int x = 17); // x has an unparsed default argument now |
1745 | /// }; // x has a regular default argument now |
1746 | /// @endcode |
1747 | bool hasUnparsedDefaultArg() const { |
1748 | return ParmVarDeclBits.DefaultArgKind == DAK_Unparsed; |
1749 | } |
1750 | |
1751 | bool hasUninstantiatedDefaultArg() const { |
1752 | return ParmVarDeclBits.DefaultArgKind == DAK_Uninstantiated; |
1753 | } |
1754 | |
1755 | /// Specify that this parameter has an unparsed default argument. |
1756 | /// The argument will be replaced with a real default argument via |
1757 | /// setDefaultArg when the class definition enclosing the function |
1758 | /// declaration that owns this default argument is completed. |
1759 | void setUnparsedDefaultArg() { |
1760 | ParmVarDeclBits.DefaultArgKind = DAK_Unparsed; |
1761 | } |
1762 | |
1763 | bool hasInheritedDefaultArg() const { |
1764 | return ParmVarDeclBits.HasInheritedDefaultArg; |
1765 | } |
1766 | |
1767 | void setHasInheritedDefaultArg(bool I = true) { |
1768 | ParmVarDeclBits.HasInheritedDefaultArg = I; |
1769 | } |
1770 | |
1771 | QualType getOriginalType() const; |
1772 | |
1773 | /// Sets the function declaration that owns this |
1774 | /// ParmVarDecl. Since ParmVarDecls are often created before the |
1775 | /// FunctionDecls that own them, this routine is required to update |
1776 | /// the DeclContext appropriately. |
1777 | void setOwningFunction(DeclContext *FD) { setDeclContext(FD); } |
1778 | |
1779 | // Implement isa/cast/dyncast/etc. |
1780 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1781 | static bool classofKind(Kind K) { return K == ParmVar; } |
1782 | |
1783 | private: |
1784 | enum { ParameterIndexSentinel = (1 << NumParameterIndexBits) - 1 }; |
1785 | |
1786 | void setParameterIndex(unsigned parameterIndex) { |
1787 | if (parameterIndex >= ParameterIndexSentinel) { |
1788 | setParameterIndexLarge(parameterIndex); |
1789 | return; |
1790 | } |
1791 | |
1792 | ParmVarDeclBits.ParameterIndex = parameterIndex; |
1793 | assert(ParmVarDeclBits.ParameterIndex == parameterIndex && "truncation!")((ParmVarDeclBits.ParameterIndex == parameterIndex && "truncation!") ? static_cast<void> (0) : __assert_fail ("ParmVarDeclBits.ParameterIndex == parameterIndex && \"truncation!\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 1793, __PRETTY_FUNCTION__)); |
1794 | } |
1795 | unsigned getParameterIndex() const { |
1796 | unsigned d = ParmVarDeclBits.ParameterIndex; |
1797 | return d == ParameterIndexSentinel ? getParameterIndexLarge() : d; |
1798 | } |
1799 | |
1800 | void setParameterIndexLarge(unsigned parameterIndex); |
1801 | unsigned getParameterIndexLarge() const; |
1802 | }; |
1803 | |
1804 | enum class MultiVersionKind { |
1805 | None, |
1806 | Target, |
1807 | CPUSpecific, |
1808 | CPUDispatch |
1809 | }; |
1810 | |
1811 | /// Represents a function declaration or definition. |
1812 | /// |
1813 | /// Since a given function can be declared several times in a program, |
1814 | /// there may be several FunctionDecls that correspond to that |
1815 | /// function. Only one of those FunctionDecls will be found when |
1816 | /// traversing the list of declarations in the context of the |
1817 | /// FunctionDecl (e.g., the translation unit); this FunctionDecl |
1818 | /// contains all of the information known about the function. Other, |
1819 | /// previous declarations of the function are available via the |
1820 | /// getPreviousDecl() chain. |
1821 | class FunctionDecl : public DeclaratorDecl, |
1822 | public DeclContext, |
1823 | public Redeclarable<FunctionDecl> { |
1824 | // This class stores some data in DeclContext::FunctionDeclBits |
1825 | // to save some space. Use the provided accessors to access it. |
1826 | public: |
1827 | /// The kind of templated function a FunctionDecl can be. |
1828 | enum TemplatedKind { |
1829 | // Not templated. |
1830 | TK_NonTemplate, |
1831 | // The pattern in a function template declaration. |
1832 | TK_FunctionTemplate, |
1833 | // A non-template function that is an instantiation or explicit |
1834 | // specialization of a member of a templated class. |
1835 | TK_MemberSpecialization, |
1836 | // An instantiation or explicit specialization of a function template. |
1837 | // Note: this might have been instantiated from a templated class if it |
1838 | // is a class-scope explicit specialization. |
1839 | TK_FunctionTemplateSpecialization, |
1840 | // A function template specialization that hasn't yet been resolved to a |
1841 | // particular specialized function template. |
1842 | TK_DependentFunctionTemplateSpecialization |
1843 | }; |
1844 | |
1845 | /// Stashed information about a defaulted function definition whose body has |
1846 | /// not yet been lazily generated. |
1847 | class DefaultedFunctionInfo final |
1848 | : llvm::TrailingObjects<DefaultedFunctionInfo, DeclAccessPair> { |
1849 | friend TrailingObjects; |
1850 | unsigned NumLookups; |
1851 | |
1852 | public: |
1853 | static DefaultedFunctionInfo *Create(ASTContext &Context, |
1854 | ArrayRef<DeclAccessPair> Lookups); |
1855 | /// Get the unqualified lookup results that should be used in this |
1856 | /// defaulted function definition. |
1857 | ArrayRef<DeclAccessPair> getUnqualifiedLookups() const { |
1858 | return {getTrailingObjects<DeclAccessPair>(), NumLookups}; |
1859 | } |
1860 | }; |
1861 | |
1862 | private: |
1863 | /// A new[]'d array of pointers to VarDecls for the formal |
1864 | /// parameters of this function. This is null if a prototype or if there are |
1865 | /// no formals. |
1866 | ParmVarDecl **ParamInfo = nullptr; |
1867 | |
1868 | /// The active member of this union is determined by |
1869 | /// FunctionDeclBits.HasDefaultedFunctionInfo. |
1870 | union { |
1871 | /// The body of the function. |
1872 | LazyDeclStmtPtr Body; |
1873 | /// Information about a future defaulted function definition. |
1874 | DefaultedFunctionInfo *DefaultedInfo; |
1875 | }; |
1876 | |
1877 | unsigned ODRHash; |
1878 | |
1879 | /// End part of this FunctionDecl's source range. |
1880 | /// |
1881 | /// We could compute the full range in getSourceRange(). However, when we're |
1882 | /// dealing with a function definition deserialized from a PCH/AST file, |
1883 | /// we can only compute the full range once the function body has been |
1884 | /// de-serialized, so it's far better to have the (sometimes-redundant) |
1885 | /// EndRangeLoc. |
1886 | SourceLocation EndRangeLoc; |
1887 | |
1888 | /// The template or declaration that this declaration |
1889 | /// describes or was instantiated from, respectively. |
1890 | /// |
1891 | /// For non-templates, this value will be NULL. For function |
1892 | /// declarations that describe a function template, this will be a |
1893 | /// pointer to a FunctionTemplateDecl. For member functions |
1894 | /// of class template specializations, this will be a MemberSpecializationInfo |
1895 | /// pointer containing information about the specialization. |
1896 | /// For function template specializations, this will be a |
1897 | /// FunctionTemplateSpecializationInfo, which contains information about |
1898 | /// the template being specialized and the template arguments involved in |
1899 | /// that specialization. |
1900 | llvm::PointerUnion<FunctionTemplateDecl *, |
1901 | MemberSpecializationInfo *, |
1902 | FunctionTemplateSpecializationInfo *, |
1903 | DependentFunctionTemplateSpecializationInfo *> |
1904 | TemplateOrSpecialization; |
1905 | |
1906 | /// Provides source/type location info for the declaration name embedded in |
1907 | /// the DeclaratorDecl base class. |
1908 | DeclarationNameLoc DNLoc; |
1909 | |
1910 | /// Specify that this function declaration is actually a function |
1911 | /// template specialization. |
1912 | /// |
1913 | /// \param C the ASTContext. |
1914 | /// |
1915 | /// \param Template the function template that this function template |
1916 | /// specialization specializes. |
1917 | /// |
1918 | /// \param TemplateArgs the template arguments that produced this |
1919 | /// function template specialization from the template. |
1920 | /// |
1921 | /// \param InsertPos If non-NULL, the position in the function template |
1922 | /// specialization set where the function template specialization data will |
1923 | /// be inserted. |
1924 | /// |
1925 | /// \param TSK the kind of template specialization this is. |
1926 | /// |
1927 | /// \param TemplateArgsAsWritten location info of template arguments. |
1928 | /// |
1929 | /// \param PointOfInstantiation point at which the function template |
1930 | /// specialization was first instantiated. |
1931 | void setFunctionTemplateSpecialization(ASTContext &C, |
1932 | FunctionTemplateDecl *Template, |
1933 | const TemplateArgumentList *TemplateArgs, |
1934 | void *InsertPos, |
1935 | TemplateSpecializationKind TSK, |
1936 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
1937 | SourceLocation PointOfInstantiation); |
1938 | |
1939 | /// Specify that this record is an instantiation of the |
1940 | /// member function FD. |
1941 | void setInstantiationOfMemberFunction(ASTContext &C, FunctionDecl *FD, |
1942 | TemplateSpecializationKind TSK); |
1943 | |
1944 | void setParams(ASTContext &C, ArrayRef<ParmVarDecl *> NewParamInfo); |
1945 | |
1946 | // This is unfortunately needed because ASTDeclWriter::VisitFunctionDecl |
1947 | // need to access this bit but we want to avoid making ASTDeclWriter |
1948 | // a friend of FunctionDeclBitfields just for this. |
1949 | bool isDeletedBit() const { return FunctionDeclBits.IsDeleted; } |
1950 | |
1951 | /// Whether an ODRHash has been stored. |
1952 | bool hasODRHash() const { return FunctionDeclBits.HasODRHash; } |
1953 | |
1954 | /// State that an ODRHash has been stored. |
1955 | void setHasODRHash(bool B = true) { FunctionDeclBits.HasODRHash = B; } |
1956 | |
1957 | protected: |
1958 | FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1959 | const DeclarationNameInfo &NameInfo, QualType T, |
1960 | TypeSourceInfo *TInfo, StorageClass S, bool isInlineSpecified, |
1961 | ConstexprSpecKind ConstexprKind, |
1962 | Expr *TrailingRequiresClause = nullptr); |
1963 | |
1964 | using redeclarable_base = Redeclarable<FunctionDecl>; |
1965 | |
1966 | FunctionDecl *getNextRedeclarationImpl() override { |
1967 | return getNextRedeclaration(); |
1968 | } |
1969 | |
1970 | FunctionDecl *getPreviousDeclImpl() override { |
1971 | return getPreviousDecl(); |
1972 | } |
1973 | |
1974 | FunctionDecl *getMostRecentDeclImpl() override { |
1975 | return getMostRecentDecl(); |
1976 | } |
1977 | |
1978 | public: |
1979 | friend class ASTDeclReader; |
1980 | friend class ASTDeclWriter; |
1981 | |
1982 | using redecl_range = redeclarable_base::redecl_range; |
1983 | using redecl_iterator = redeclarable_base::redecl_iterator; |
1984 | |
1985 | using redeclarable_base::redecls_begin; |
1986 | using redeclarable_base::redecls_end; |
1987 | using redeclarable_base::redecls; |
1988 | using redeclarable_base::getPreviousDecl; |
1989 | using redeclarable_base::getMostRecentDecl; |
1990 | using redeclarable_base::isFirstDecl; |
1991 | |
1992 | static FunctionDecl * |
1993 | Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1994 | SourceLocation NLoc, DeclarationName N, QualType T, |
1995 | TypeSourceInfo *TInfo, StorageClass SC, bool isInlineSpecified = false, |
1996 | bool hasWrittenPrototype = true, |
1997 | ConstexprSpecKind ConstexprKind = ConstexprSpecKind::Unspecified, |
1998 | Expr *TrailingRequiresClause = nullptr) { |
1999 | DeclarationNameInfo NameInfo(N, NLoc); |
2000 | return FunctionDecl::Create(C, DC, StartLoc, NameInfo, T, TInfo, SC, |
2001 | isInlineSpecified, hasWrittenPrototype, |
2002 | ConstexprKind, TrailingRequiresClause); |
2003 | } |
2004 | |
2005 | static FunctionDecl *Create(ASTContext &C, DeclContext *DC, |
2006 | SourceLocation StartLoc, |
2007 | const DeclarationNameInfo &NameInfo, QualType T, |
2008 | TypeSourceInfo *TInfo, StorageClass SC, |
2009 | bool isInlineSpecified, bool hasWrittenPrototype, |
2010 | ConstexprSpecKind ConstexprKind, |
2011 | Expr *TrailingRequiresClause); |
2012 | |
2013 | static FunctionDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2014 | |
2015 | DeclarationNameInfo getNameInfo() const { |
2016 | return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); |
2017 | } |
2018 | |
2019 | void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, |
2020 | bool Qualified) const override; |
2021 | |
2022 | void setRangeEnd(SourceLocation E) { EndRangeLoc = E; } |
2023 | |
2024 | /// Returns the location of the ellipsis of a variadic function. |
2025 | SourceLocation getEllipsisLoc() const { |
2026 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
2027 | if (FPT && FPT->isVariadic()) |
2028 | return FPT->getEllipsisLoc(); |
2029 | return SourceLocation(); |
2030 | } |
2031 | |
2032 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
2033 | |
2034 | // Function definitions. |
2035 | // |
2036 | // A function declaration may be: |
2037 | // - a non defining declaration, |
2038 | // - a definition. A function may be defined because: |
2039 | // - it has a body, or will have it in the case of late parsing. |
2040 | // - it has an uninstantiated body. The body does not exist because the |
2041 | // function is not used yet, but the declaration is considered a |
2042 | // definition and does not allow other definition of this function. |
2043 | // - it does not have a user specified body, but it does not allow |
2044 | // redefinition, because it is deleted/defaulted or is defined through |
2045 | // some other mechanism (alias, ifunc). |
2046 | |
2047 | /// Returns true if the function has a body. |
2048 | /// |
2049 | /// The function body might be in any of the (re-)declarations of this |
2050 | /// function. The variant that accepts a FunctionDecl pointer will set that |
2051 | /// function declaration to the actual declaration containing the body (if |
2052 | /// there is one). |
2053 | bool hasBody(const FunctionDecl *&Definition) const; |
2054 | |
2055 | bool hasBody() const override { |
2056 | const FunctionDecl* Definition; |
2057 | return hasBody(Definition); |
2058 | } |
2059 | |
2060 | /// Returns whether the function has a trivial body that does not require any |
2061 | /// specific codegen. |
2062 | bool hasTrivialBody() const; |
2063 | |
2064 | /// Returns true if the function has a definition that does not need to be |
2065 | /// instantiated. |
2066 | /// |
2067 | /// The variant that accepts a FunctionDecl pointer will set that function |
2068 | /// declaration to the declaration that is a definition (if there is one). |
2069 | /// |
2070 | /// \param CheckForPendingFriendDefinition If \c true, also check for friend |
2071 | /// declarations that were instantiataed from function definitions. |
2072 | /// Such a declaration behaves as if it is a definition for the |
2073 | /// purpose of redefinition checking, but isn't actually a "real" |
2074 | /// definition until its body is instantiated. |
2075 | bool isDefined(const FunctionDecl *&Definition, |
2076 | bool CheckForPendingFriendDefinition = false) const; |
2077 | |
2078 | bool isDefined() const { |
2079 | const FunctionDecl* Definition; |
2080 | return isDefined(Definition); |
2081 | } |
2082 | |
2083 | /// Get the definition for this declaration. |
2084 | FunctionDecl *getDefinition() { |
2085 | const FunctionDecl *Definition; |
2086 | if (isDefined(Definition)) |
2087 | return const_cast<FunctionDecl *>(Definition); |
2088 | return nullptr; |
2089 | } |
2090 | const FunctionDecl *getDefinition() const { |
2091 | return const_cast<FunctionDecl *>(this)->getDefinition(); |
2092 | } |
2093 | |
2094 | /// Retrieve the body (definition) of the function. The function body might be |
2095 | /// in any of the (re-)declarations of this function. The variant that accepts |
2096 | /// a FunctionDecl pointer will set that function declaration to the actual |
2097 | /// declaration containing the body (if there is one). |
2098 | /// NOTE: For checking if there is a body, use hasBody() instead, to avoid |
2099 | /// unnecessary AST de-serialization of the body. |
2100 | Stmt *getBody(const FunctionDecl *&Definition) const; |
2101 | |
2102 | Stmt *getBody() const override { |
2103 | const FunctionDecl* Definition; |
2104 | return getBody(Definition); |
2105 | } |
2106 | |
2107 | /// Returns whether this specific declaration of the function is also a |
2108 | /// definition that does not contain uninstantiated body. |
2109 | /// |
2110 | /// This does not determine whether the function has been defined (e.g., in a |
2111 | /// previous definition); for that information, use isDefined. |
2112 | /// |
2113 | /// Note: the function declaration does not become a definition until the |
2114 | /// parser reaches the definition, if called before, this function will return |
2115 | /// `false`. |
2116 | bool isThisDeclarationADefinition() const { |
2117 | return isDeletedAsWritten() || isDefaulted() || |
2118 | doesThisDeclarationHaveABody() || hasSkippedBody() || |
2119 | willHaveBody() || hasDefiningAttr(); |
2120 | } |
2121 | |
2122 | /// Determine whether this specific declaration of the function is a friend |
2123 | /// declaration that was instantiated from a function definition. Such |
2124 | /// declarations behave like definitions in some contexts. |
2125 | bool isThisDeclarationInstantiatedFromAFriendDefinition() const; |
2126 | |
2127 | /// Returns whether this specific declaration of the function has a body. |
2128 | bool doesThisDeclarationHaveABody() const { |
2129 | return (!FunctionDeclBits.HasDefaultedFunctionInfo && Body) || |
2130 | isLateTemplateParsed(); |
2131 | } |
2132 | |
2133 | void setBody(Stmt *B); |
2134 | void setLazyBody(uint64_t Offset) { |
2135 | FunctionDeclBits.HasDefaultedFunctionInfo = false; |
2136 | Body = LazyDeclStmtPtr(Offset); |
2137 | } |
2138 | |
2139 | void setDefaultedFunctionInfo(DefaultedFunctionInfo *Info); |
2140 | DefaultedFunctionInfo *getDefaultedFunctionInfo() const; |
2141 | |
2142 | /// Whether this function is variadic. |
2143 | bool isVariadic() const; |
2144 | |
2145 | /// Whether this function is marked as virtual explicitly. |
2146 | bool isVirtualAsWritten() const { |
2147 | return FunctionDeclBits.IsVirtualAsWritten; |
2148 | } |
2149 | |
2150 | /// State that this function is marked as virtual explicitly. |
2151 | void setVirtualAsWritten(bool V) { FunctionDeclBits.IsVirtualAsWritten = V; } |
2152 | |
2153 | /// Whether this virtual function is pure, i.e. makes the containing class |
2154 | /// abstract. |
2155 | bool isPure() const { return FunctionDeclBits.IsPure; } |
2156 | void setPure(bool P = true); |
2157 | |
2158 | /// Whether this templated function will be late parsed. |
2159 | bool isLateTemplateParsed() const { |
2160 | return FunctionDeclBits.IsLateTemplateParsed; |
2161 | } |
2162 | |
2163 | /// State that this templated function will be late parsed. |
2164 | void setLateTemplateParsed(bool ILT = true) { |
2165 | FunctionDeclBits.IsLateTemplateParsed = ILT; |
2166 | } |
2167 | |
2168 | /// Whether this function is "trivial" in some specialized C++ senses. |
2169 | /// Can only be true for default constructors, copy constructors, |
2170 | /// copy assignment operators, and destructors. Not meaningful until |
2171 | /// the class has been fully built by Sema. |
2172 | bool isTrivial() const { return FunctionDeclBits.IsTrivial; } |
2173 | void setTrivial(bool IT) { FunctionDeclBits.IsTrivial = IT; } |
2174 | |
2175 | bool isTrivialForCall() const { return FunctionDeclBits.IsTrivialForCall; } |
2176 | void setTrivialForCall(bool IT) { FunctionDeclBits.IsTrivialForCall = IT; } |
2177 | |
2178 | /// Whether this function is defaulted. Valid for e.g. |
2179 | /// special member functions, defaulted comparisions (not methods!). |
2180 | bool isDefaulted() const { return FunctionDeclBits.IsDefaulted; } |
2181 | void setDefaulted(bool D = true) { FunctionDeclBits.IsDefaulted = D; } |
2182 | |
2183 | /// Whether this function is explicitly defaulted. |
2184 | bool isExplicitlyDefaulted() const { |
2185 | return FunctionDeclBits.IsExplicitlyDefaulted; |
2186 | } |
2187 | |
2188 | /// State that this function is explicitly defaulted. |
2189 | void setExplicitlyDefaulted(bool ED = true) { |
2190 | FunctionDeclBits.IsExplicitlyDefaulted = ED; |
2191 | } |
2192 | |
2193 | /// True if this method is user-declared and was not |
2194 | /// deleted or defaulted on its first declaration. |
2195 | bool isUserProvided() const { |
2196 | auto *DeclAsWritten = this; |
2197 | if (FunctionDecl *Pattern = getTemplateInstantiationPattern()) |
2198 | DeclAsWritten = Pattern; |
2199 | return !(DeclAsWritten->isDeleted() || |
2200 | DeclAsWritten->getCanonicalDecl()->isDefaulted()); |
2201 | } |
2202 | |
2203 | /// Whether falling off this function implicitly returns null/zero. |
2204 | /// If a more specific implicit return value is required, front-ends |
2205 | /// should synthesize the appropriate return statements. |
2206 | bool hasImplicitReturnZero() const { |
2207 | return FunctionDeclBits.HasImplicitReturnZero; |
2208 | } |
2209 | |
2210 | /// State that falling off this function implicitly returns null/zero. |
2211 | /// If a more specific implicit return value is required, front-ends |
2212 | /// should synthesize the appropriate return statements. |
2213 | void setHasImplicitReturnZero(bool IRZ) { |
2214 | FunctionDeclBits.HasImplicitReturnZero = IRZ; |
2215 | } |
2216 | |
2217 | /// Whether this function has a prototype, either because one |
2218 | /// was explicitly written or because it was "inherited" by merging |
2219 | /// a declaration without a prototype with a declaration that has a |
2220 | /// prototype. |
2221 | bool hasPrototype() const { |
2222 | return hasWrittenPrototype() || hasInheritedPrototype(); |
2223 | } |
2224 | |
2225 | /// Whether this function has a written prototype. |
2226 | bool hasWrittenPrototype() const { |
2227 | return FunctionDeclBits.HasWrittenPrototype; |
2228 | } |
2229 | |
2230 | /// State that this function has a written prototype. |
2231 | void setHasWrittenPrototype(bool P = true) { |
2232 | FunctionDeclBits.HasWrittenPrototype = P; |
2233 | } |
2234 | |
2235 | /// Whether this function inherited its prototype from a |
2236 | /// previous declaration. |
2237 | bool hasInheritedPrototype() const { |
2238 | return FunctionDeclBits.HasInheritedPrototype; |
2239 | } |
2240 | |
2241 | /// State that this function inherited its prototype from a |
2242 | /// previous declaration. |
2243 | void setHasInheritedPrototype(bool P = true) { |
2244 | FunctionDeclBits.HasInheritedPrototype = P; |
2245 | } |
2246 | |
2247 | /// Whether this is a (C++11) constexpr function or constexpr constructor. |
2248 | bool isConstexpr() const { |
2249 | return getConstexprKind() != ConstexprSpecKind::Unspecified; |
2250 | } |
2251 | void setConstexprKind(ConstexprSpecKind CSK) { |
2252 | FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(CSK); |
2253 | } |
2254 | ConstexprSpecKind getConstexprKind() const { |
2255 | return static_cast<ConstexprSpecKind>(FunctionDeclBits.ConstexprKind); |
2256 | } |
2257 | bool isConstexprSpecified() const { |
2258 | return getConstexprKind() == ConstexprSpecKind::Constexpr; |
2259 | } |
2260 | bool isConsteval() const { |
2261 | return getConstexprKind() == ConstexprSpecKind::Consteval; |
2262 | } |
2263 | |
2264 | /// Whether the instantiation of this function is pending. |
2265 | /// This bit is set when the decision to instantiate this function is made |
2266 | /// and unset if and when the function body is created. That leaves out |
2267 | /// cases where instantiation did not happen because the template definition |
2268 | /// was not seen in this TU. This bit remains set in those cases, under the |
2269 | /// assumption that the instantiation will happen in some other TU. |
2270 | bool instantiationIsPending() const { |
2271 | return FunctionDeclBits.InstantiationIsPending; |
2272 | } |
2273 | |
2274 | /// State that the instantiation of this function is pending. |
2275 | /// (see instantiationIsPending) |
2276 | void setInstantiationIsPending(bool IC) { |
2277 | FunctionDeclBits.InstantiationIsPending = IC; |
2278 | } |
2279 | |
2280 | /// Indicates the function uses __try. |
2281 | bool usesSEHTry() const { return FunctionDeclBits.UsesSEHTry; } |
2282 | void setUsesSEHTry(bool UST) { FunctionDeclBits.UsesSEHTry = UST; } |
2283 | |
2284 | /// Whether this function has been deleted. |
2285 | /// |
2286 | /// A function that is "deleted" (via the C++0x "= delete" syntax) |
2287 | /// acts like a normal function, except that it cannot actually be |
2288 | /// called or have its address taken. Deleted functions are |
2289 | /// typically used in C++ overload resolution to attract arguments |
2290 | /// whose type or lvalue/rvalue-ness would permit the use of a |
2291 | /// different overload that would behave incorrectly. For example, |
2292 | /// one might use deleted functions to ban implicit conversion from |
2293 | /// a floating-point number to an Integer type: |
2294 | /// |
2295 | /// @code |
2296 | /// struct Integer { |
2297 | /// Integer(long); // construct from a long |
2298 | /// Integer(double) = delete; // no construction from float or double |
2299 | /// Integer(long double) = delete; // no construction from long double |
2300 | /// }; |
2301 | /// @endcode |
2302 | // If a function is deleted, its first declaration must be. |
2303 | bool isDeleted() const { |
2304 | return getCanonicalDecl()->FunctionDeclBits.IsDeleted; |
2305 | } |
2306 | |
2307 | bool isDeletedAsWritten() const { |
2308 | return FunctionDeclBits.IsDeleted && !isDefaulted(); |
2309 | } |
2310 | |
2311 | void setDeletedAsWritten(bool D = true) { FunctionDeclBits.IsDeleted = D; } |
2312 | |
2313 | /// Determines whether this function is "main", which is the |
2314 | /// entry point into an executable program. |
2315 | bool isMain() const; |
2316 | |
2317 | /// Determines whether this function is a MSVCRT user defined entry |
2318 | /// point. |
2319 | bool isMSVCRTEntryPoint() const; |
2320 | |
2321 | /// Determines whether this operator new or delete is one |
2322 | /// of the reserved global placement operators: |
2323 | /// void *operator new(size_t, void *); |
2324 | /// void *operator new[](size_t, void *); |
2325 | /// void operator delete(void *, void *); |
2326 | /// void operator delete[](void *, void *); |
2327 | /// These functions have special behavior under [new.delete.placement]: |
2328 | /// These functions are reserved, a C++ program may not define |
2329 | /// functions that displace the versions in the Standard C++ library. |
2330 | /// The provisions of [basic.stc.dynamic] do not apply to these |
2331 | /// reserved placement forms of operator new and operator delete. |
2332 | /// |
2333 | /// This function must be an allocation or deallocation function. |
2334 | bool isReservedGlobalPlacementOperator() const; |
2335 | |
2336 | /// Determines whether this function is one of the replaceable |
2337 | /// global allocation functions: |
2338 | /// void *operator new(size_t); |
2339 | /// void *operator new(size_t, const std::nothrow_t &) noexcept; |
2340 | /// void *operator new[](size_t); |
2341 | /// void *operator new[](size_t, const std::nothrow_t &) noexcept; |
2342 | /// void operator delete(void *) noexcept; |
2343 | /// void operator delete(void *, std::size_t) noexcept; [C++1y] |
2344 | /// void operator delete(void *, const std::nothrow_t &) noexcept; |
2345 | /// void operator delete[](void *) noexcept; |
2346 | /// void operator delete[](void *, std::size_t) noexcept; [C++1y] |
2347 | /// void operator delete[](void *, const std::nothrow_t &) noexcept; |
2348 | /// These functions have special behavior under C++1y [expr.new]: |
2349 | /// An implementation is allowed to omit a call to a replaceable global |
2350 | /// allocation function. [...] |
2351 | /// |
2352 | /// If this function is an aligned allocation/deallocation function, return |
2353 | /// the parameter number of the requested alignment through AlignmentParam. |
2354 | /// |
2355 | /// If this function is an allocation/deallocation function that takes |
2356 | /// the `std::nothrow_t` tag, return true through IsNothrow, |
2357 | bool isReplaceableGlobalAllocationFunction( |
2358 | Optional<unsigned> *AlignmentParam = nullptr, |
2359 | bool *IsNothrow = nullptr) const; |
2360 | |
2361 | /// Determine if this function provides an inline implementation of a builtin. |
2362 | bool isInlineBuiltinDeclaration() const; |
2363 | |
2364 | /// Determine whether this is a destroying operator delete. |
2365 | bool isDestroyingOperatorDelete() const; |
2366 | |
2367 | /// Compute the language linkage. |
2368 | LanguageLinkage getLanguageLinkage() const; |
2369 | |
2370 | /// Determines whether this function is a function with |
2371 | /// external, C linkage. |
2372 | bool isExternC() const; |
2373 | |
2374 | /// Determines whether this function's context is, or is nested within, |
2375 | /// a C++ extern "C" linkage spec. |
2376 | bool isInExternCContext() const; |
2377 | |
2378 | /// Determines whether this function's context is, or is nested within, |
2379 | /// a C++ extern "C++" linkage spec. |
2380 | bool isInExternCXXContext() const; |
2381 | |
2382 | /// Determines whether this is a global function. |
2383 | bool isGlobal() const; |
2384 | |
2385 | /// Determines whether this function is known to be 'noreturn', through |
2386 | /// an attribute on its declaration or its type. |
2387 | bool isNoReturn() const; |
2388 | |
2389 | /// True if the function was a definition but its body was skipped. |
2390 | bool hasSkippedBody() const { return FunctionDeclBits.HasSkippedBody; } |
2391 | void setHasSkippedBody(bool Skipped = true) { |
2392 | FunctionDeclBits.HasSkippedBody = Skipped; |
2393 | } |
2394 | |
2395 | /// True if this function will eventually have a body, once it's fully parsed. |
2396 | bool willHaveBody() const { return FunctionDeclBits.WillHaveBody; } |
2397 | void setWillHaveBody(bool V = true) { FunctionDeclBits.WillHaveBody = V; } |
2398 | |
2399 | /// True if this function is considered a multiversioned function. |
2400 | bool isMultiVersion() const { |
2401 | return getCanonicalDecl()->FunctionDeclBits.IsMultiVersion; |
2402 | } |
2403 | |
2404 | /// Sets the multiversion state for this declaration and all of its |
2405 | /// redeclarations. |
2406 | void setIsMultiVersion(bool V = true) { |
2407 | getCanonicalDecl()->FunctionDeclBits.IsMultiVersion = V; |
2408 | } |
2409 | |
2410 | /// Gets the kind of multiversioning attribute this declaration has. Note that |
2411 | /// this can return a value even if the function is not multiversion, such as |
2412 | /// the case of 'target'. |
2413 | MultiVersionKind getMultiVersionKind() const; |
2414 | |
2415 | |
2416 | /// True if this function is a multiversioned dispatch function as a part of |
2417 | /// the cpu_specific/cpu_dispatch functionality. |
2418 | bool isCPUDispatchMultiVersion() const; |
2419 | /// True if this function is a multiversioned processor specific function as a |
2420 | /// part of the cpu_specific/cpu_dispatch functionality. |
2421 | bool isCPUSpecificMultiVersion() const; |
2422 | |
2423 | /// True if this function is a multiversioned dispatch function as a part of |
2424 | /// the target functionality. |
2425 | bool isTargetMultiVersion() const; |
2426 | |
2427 | /// \brief Get the associated-constraints of this function declaration. |
2428 | /// Currently, this will either be a vector of size 1 containing the |
2429 | /// trailing-requires-clause or an empty vector. |
2430 | /// |
2431 | /// Use this instead of getTrailingRequiresClause for concepts APIs that |
2432 | /// accept an ArrayRef of constraint expressions. |
2433 | void getAssociatedConstraints(SmallVectorImpl<const Expr *> &AC) const { |
2434 | if (auto *TRC = getTrailingRequiresClause()) |
2435 | AC.push_back(TRC); |
2436 | } |
2437 | |
2438 | void setPreviousDeclaration(FunctionDecl * PrevDecl); |
2439 | |
2440 | FunctionDecl *getCanonicalDecl() override; |
2441 | const FunctionDecl *getCanonicalDecl() const { |
2442 | return const_cast<FunctionDecl*>(this)->getCanonicalDecl(); |
2443 | } |
2444 | |
2445 | unsigned getBuiltinID(bool ConsiderWrapperFunctions = false) const; |
2446 | |
2447 | // ArrayRef interface to parameters. |
2448 | ArrayRef<ParmVarDecl *> parameters() const { |
2449 | return {ParamInfo, getNumParams()}; |
2450 | } |
2451 | MutableArrayRef<ParmVarDecl *> parameters() { |
2452 | return {ParamInfo, getNumParams()}; |
2453 | } |
2454 | |
2455 | // Iterator access to formal parameters. |
2456 | using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator; |
2457 | using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator; |
2458 | |
2459 | bool param_empty() const { return parameters().empty(); } |
2460 | param_iterator param_begin() { return parameters().begin(); } |
2461 | param_iterator param_end() { return parameters().end(); } |
2462 | param_const_iterator param_begin() const { return parameters().begin(); } |
2463 | param_const_iterator param_end() const { return parameters().end(); } |
2464 | size_t param_size() const { return parameters().size(); } |
2465 | |
2466 | /// Return the number of parameters this function must have based on its |
2467 | /// FunctionType. This is the length of the ParamInfo array after it has been |
2468 | /// created. |
2469 | unsigned getNumParams() const; |
2470 | |
2471 | const ParmVarDecl *getParamDecl(unsigned i) const { |
2472 | assert(i < getNumParams() && "Illegal param #")((i < getNumParams() && "Illegal param #") ? static_cast <void> (0) : __assert_fail ("i < getNumParams() && \"Illegal param #\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 2472, __PRETTY_FUNCTION__)); |
2473 | return ParamInfo[i]; |
2474 | } |
2475 | ParmVarDecl *getParamDecl(unsigned i) { |
2476 | assert(i < getNumParams() && "Illegal param #")((i < getNumParams() && "Illegal param #") ? static_cast <void> (0) : __assert_fail ("i < getNumParams() && \"Illegal param #\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 2476, __PRETTY_FUNCTION__)); |
2477 | return ParamInfo[i]; |
2478 | } |
2479 | void setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
2480 | setParams(getASTContext(), NewParamInfo); |
2481 | } |
2482 | |
2483 | /// Returns the minimum number of arguments needed to call this function. This |
2484 | /// may be fewer than the number of function parameters, if some of the |
2485 | /// parameters have default arguments (in C++). |
2486 | unsigned getMinRequiredArguments() const; |
2487 | |
2488 | /// Determine whether this function has a single parameter, or multiple |
2489 | /// parameters where all but the first have default arguments. |
2490 | /// |
2491 | /// This notion is used in the definition of copy/move constructors and |
2492 | /// initializer list constructors. Note that, unlike getMinRequiredArguments, |
2493 | /// parameter packs are not treated specially here. |
2494 | bool hasOneParamOrDefaultArgs() const; |
2495 | |
2496 | /// Find the source location information for how the type of this function |
2497 | /// was written. May be absent (for example if the function was declared via |
2498 | /// a typedef) and may contain a different type from that of the function |
2499 | /// (for example if the function type was adjusted by an attribute). |
2500 | FunctionTypeLoc getFunctionTypeLoc() const; |
2501 | |
2502 | QualType getReturnType() const { |
2503 | return getType()->castAs<FunctionType>()->getReturnType(); |
2504 | } |
2505 | |
2506 | /// Attempt to compute an informative source range covering the |
2507 | /// function return type. This may omit qualifiers and other information with |
2508 | /// limited representation in the AST. |
2509 | SourceRange getReturnTypeSourceRange() const; |
2510 | |
2511 | /// Attempt to compute an informative source range covering the |
2512 | /// function parameters, including the ellipsis of a variadic function. |
2513 | /// The source range excludes the parentheses, and is invalid if there are |
2514 | /// no parameters and no ellipsis. |
2515 | SourceRange getParametersSourceRange() const; |
2516 | |
2517 | /// Get the declared return type, which may differ from the actual return |
2518 | /// type if the return type is deduced. |
2519 | QualType getDeclaredReturnType() const { |
2520 | auto *TSI = getTypeSourceInfo(); |
2521 | QualType T = TSI ? TSI->getType() : getType(); |
2522 | return T->castAs<FunctionType>()->getReturnType(); |
2523 | } |
2524 | |
2525 | /// Gets the ExceptionSpecificationType as declared. |
2526 | ExceptionSpecificationType getExceptionSpecType() const { |
2527 | auto *TSI = getTypeSourceInfo(); |
2528 | QualType T = TSI ? TSI->getType() : getType(); |
2529 | const auto *FPT = T->getAs<FunctionProtoType>(); |
2530 | return FPT ? FPT->getExceptionSpecType() : EST_None; |
2531 | } |
2532 | |
2533 | /// Attempt to compute an informative source range covering the |
2534 | /// function exception specification, if any. |
2535 | SourceRange getExceptionSpecSourceRange() const; |
2536 | |
2537 | /// Determine the type of an expression that calls this function. |
2538 | QualType getCallResultType() const { |
2539 | return getType()->castAs<FunctionType>()->getCallResultType( |
2540 | getASTContext()); |
2541 | } |
2542 | |
2543 | /// Returns the storage class as written in the source. For the |
2544 | /// computed linkage of symbol, see getLinkage. |
2545 | StorageClass getStorageClass() const { |
2546 | return static_cast<StorageClass>(FunctionDeclBits.SClass); |
2547 | } |
2548 | |
2549 | /// Sets the storage class as written in the source. |
2550 | void setStorageClass(StorageClass SClass) { |
2551 | FunctionDeclBits.SClass = SClass; |
2552 | } |
2553 | |
2554 | /// Determine whether the "inline" keyword was specified for this |
2555 | /// function. |
2556 | bool isInlineSpecified() const { return FunctionDeclBits.IsInlineSpecified; } |
2557 | |
2558 | /// Set whether the "inline" keyword was specified for this function. |
2559 | void setInlineSpecified(bool I) { |
2560 | FunctionDeclBits.IsInlineSpecified = I; |
2561 | FunctionDeclBits.IsInline = I; |
2562 | } |
2563 | |
2564 | /// Flag that this function is implicitly inline. |
2565 | void setImplicitlyInline(bool I = true) { FunctionDeclBits.IsInline = I; } |
2566 | |
2567 | /// Determine whether this function should be inlined, because it is |
2568 | /// either marked "inline" or "constexpr" or is a member function of a class |
2569 | /// that was defined in the class body. |
2570 | bool isInlined() const { return FunctionDeclBits.IsInline; } |
2571 | |
2572 | bool isInlineDefinitionExternallyVisible() const; |
2573 | |
2574 | bool isMSExternInline() const; |
2575 | |
2576 | bool doesDeclarationForceExternallyVisibleDefinition() const; |
2577 | |
2578 | bool isStatic() const { return getStorageClass() == SC_Static; } |
2579 | |
2580 | /// Whether this function declaration represents an C++ overloaded |
2581 | /// operator, e.g., "operator+". |
2582 | bool isOverloadedOperator() const { |
2583 | return getOverloadedOperator() != OO_None; |
2584 | } |
2585 | |
2586 | OverloadedOperatorKind getOverloadedOperator() const; |
2587 | |
2588 | const IdentifierInfo *getLiteralIdentifier() const; |
2589 | |
2590 | /// If this function is an instantiation of a member function |
2591 | /// of a class template specialization, retrieves the function from |
2592 | /// which it was instantiated. |
2593 | /// |
2594 | /// This routine will return non-NULL for (non-templated) member |
2595 | /// functions of class templates and for instantiations of function |
2596 | /// templates. For example, given: |
2597 | /// |
2598 | /// \code |
2599 | /// template<typename T> |
2600 | /// struct X { |
2601 | /// void f(T); |
2602 | /// }; |
2603 | /// \endcode |
2604 | /// |
2605 | /// The declaration for X<int>::f is a (non-templated) FunctionDecl |
2606 | /// whose parent is the class template specialization X<int>. For |
2607 | /// this declaration, getInstantiatedFromFunction() will return |
2608 | /// the FunctionDecl X<T>::A. When a complete definition of |
2609 | /// X<int>::A is required, it will be instantiated from the |
2610 | /// declaration returned by getInstantiatedFromMemberFunction(). |
2611 | FunctionDecl *getInstantiatedFromMemberFunction() const; |
2612 | |
2613 | /// What kind of templated function this is. |
2614 | TemplatedKind getTemplatedKind() const; |
2615 | |
2616 | /// If this function is an instantiation of a member function of a |
2617 | /// class template specialization, retrieves the member specialization |
2618 | /// information. |
2619 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
2620 | |
2621 | /// Specify that this record is an instantiation of the |
2622 | /// member function FD. |
2623 | void setInstantiationOfMemberFunction(FunctionDecl *FD, |
2624 | TemplateSpecializationKind TSK) { |
2625 | setInstantiationOfMemberFunction(getASTContext(), FD, TSK); |
2626 | } |
2627 | |
2628 | /// Retrieves the function template that is described by this |
2629 | /// function declaration. |
2630 | /// |
2631 | /// Every function template is represented as a FunctionTemplateDecl |
2632 | /// and a FunctionDecl (or something derived from FunctionDecl). The |
2633 | /// former contains template properties (such as the template |
2634 | /// parameter lists) while the latter contains the actual |
2635 | /// description of the template's |
2636 | /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the |
2637 | /// FunctionDecl that describes the function template, |
2638 | /// getDescribedFunctionTemplate() retrieves the |
2639 | /// FunctionTemplateDecl from a FunctionDecl. |
2640 | FunctionTemplateDecl *getDescribedFunctionTemplate() const; |
2641 | |
2642 | void setDescribedFunctionTemplate(FunctionTemplateDecl *Template); |
2643 | |
2644 | /// Determine whether this function is a function template |
2645 | /// specialization. |
2646 | bool isFunctionTemplateSpecialization() const { |
2647 | return getPrimaryTemplate() != nullptr; |
2648 | } |
2649 | |
2650 | /// If this function is actually a function template specialization, |
2651 | /// retrieve information about this function template specialization. |
2652 | /// Otherwise, returns NULL. |
2653 | FunctionTemplateSpecializationInfo *getTemplateSpecializationInfo() const; |
2654 | |
2655 | /// Determines whether this function is a function template |
2656 | /// specialization or a member of a class template specialization that can |
2657 | /// be implicitly instantiated. |
2658 | bool isImplicitlyInstantiable() const; |
2659 | |
2660 | /// Determines if the given function was instantiated from a |
2661 | /// function template. |
2662 | bool isTemplateInstantiation() const; |
2663 | |
2664 | /// Retrieve the function declaration from which this function could |
2665 | /// be instantiated, if it is an instantiation (rather than a non-template |
2666 | /// or a specialization, for example). |
2667 | /// |
2668 | /// If \p ForDefinition is \c false, explicit specializations will be treated |
2669 | /// as if they were implicit instantiations. This will then find the pattern |
2670 | /// corresponding to non-definition portions of the declaration, such as |
2671 | /// default arguments and the exception specification. |
2672 | FunctionDecl * |
2673 | getTemplateInstantiationPattern(bool ForDefinition = true) const; |
2674 | |
2675 | /// Retrieve the primary template that this function template |
2676 | /// specialization either specializes or was instantiated from. |
2677 | /// |
2678 | /// If this function declaration is not a function template specialization, |
2679 | /// returns NULL. |
2680 | FunctionTemplateDecl *getPrimaryTemplate() const; |
2681 | |
2682 | /// Retrieve the template arguments used to produce this function |
2683 | /// template specialization from the primary template. |
2684 | /// |
2685 | /// If this function declaration is not a function template specialization, |
2686 | /// returns NULL. |
2687 | const TemplateArgumentList *getTemplateSpecializationArgs() const; |
2688 | |
2689 | /// Retrieve the template argument list as written in the sources, |
2690 | /// if any. |
2691 | /// |
2692 | /// If this function declaration is not a function template specialization |
2693 | /// or if it had no explicit template argument list, returns NULL. |
2694 | /// Note that it an explicit template argument list may be written empty, |
2695 | /// e.g., template<> void foo<>(char* s); |
2696 | const ASTTemplateArgumentListInfo* |
2697 | getTemplateSpecializationArgsAsWritten() const; |
2698 | |
2699 | /// Specify that this function declaration is actually a function |
2700 | /// template specialization. |
2701 | /// |
2702 | /// \param Template the function template that this function template |
2703 | /// specialization specializes. |
2704 | /// |
2705 | /// \param TemplateArgs the template arguments that produced this |
2706 | /// function template specialization from the template. |
2707 | /// |
2708 | /// \param InsertPos If non-NULL, the position in the function template |
2709 | /// specialization set where the function template specialization data will |
2710 | /// be inserted. |
2711 | /// |
2712 | /// \param TSK the kind of template specialization this is. |
2713 | /// |
2714 | /// \param TemplateArgsAsWritten location info of template arguments. |
2715 | /// |
2716 | /// \param PointOfInstantiation point at which the function template |
2717 | /// specialization was first instantiated. |
2718 | void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template, |
2719 | const TemplateArgumentList *TemplateArgs, |
2720 | void *InsertPos, |
2721 | TemplateSpecializationKind TSK = TSK_ImplicitInstantiation, |
2722 | const TemplateArgumentListInfo *TemplateArgsAsWritten = nullptr, |
2723 | SourceLocation PointOfInstantiation = SourceLocation()) { |
2724 | setFunctionTemplateSpecialization(getASTContext(), Template, TemplateArgs, |
2725 | InsertPos, TSK, TemplateArgsAsWritten, |
2726 | PointOfInstantiation); |
2727 | } |
2728 | |
2729 | /// Specifies that this function declaration is actually a |
2730 | /// dependent function template specialization. |
2731 | void setDependentTemplateSpecialization(ASTContext &Context, |
2732 | const UnresolvedSetImpl &Templates, |
2733 | const TemplateArgumentListInfo &TemplateArgs); |
2734 | |
2735 | DependentFunctionTemplateSpecializationInfo * |
2736 | getDependentSpecializationInfo() const; |
2737 | |
2738 | /// Determine what kind of template instantiation this function |
2739 | /// represents. |
2740 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
2741 | |
2742 | /// Determine the kind of template specialization this function represents |
2743 | /// for the purpose of template instantiation. |
2744 | TemplateSpecializationKind |
2745 | getTemplateSpecializationKindForInstantiation() const; |
2746 | |
2747 | /// Determine what kind of template instantiation this function |
2748 | /// represents. |
2749 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
2750 | SourceLocation PointOfInstantiation = SourceLocation()); |
2751 | |
2752 | /// Retrieve the (first) point of instantiation of a function template |
2753 | /// specialization or a member of a class template specialization. |
2754 | /// |
2755 | /// \returns the first point of instantiation, if this function was |
2756 | /// instantiated from a template; otherwise, returns an invalid source |
2757 | /// location. |
2758 | SourceLocation getPointOfInstantiation() const; |
2759 | |
2760 | /// Determine whether this is or was instantiated from an out-of-line |
2761 | /// definition of a member function. |
2762 | bool isOutOfLine() const override; |
2763 | |
2764 | /// Identify a memory copying or setting function. |
2765 | /// If the given function is a memory copy or setting function, returns |
2766 | /// the corresponding Builtin ID. If the function is not a memory function, |
2767 | /// returns 0. |
2768 | unsigned getMemoryFunctionKind() const; |
2769 | |
2770 | /// Returns ODRHash of the function. This value is calculated and |
2771 | /// stored on first call, then the stored value returned on the other calls. |
2772 | unsigned getODRHash(); |
2773 | |
2774 | /// Returns cached ODRHash of the function. This must have been previously |
2775 | /// computed and stored. |
2776 | unsigned getODRHash() const; |
2777 | |
2778 | // Implement isa/cast/dyncast/etc. |
2779 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
2780 | static bool classofKind(Kind K) { |
2781 | return K >= firstFunction && K <= lastFunction; |
2782 | } |
2783 | static DeclContext *castToDeclContext(const FunctionDecl *D) { |
2784 | return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D)); |
2785 | } |
2786 | static FunctionDecl *castFromDeclContext(const DeclContext *DC) { |
2787 | return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC)); |
2788 | } |
2789 | }; |
2790 | |
2791 | /// Represents a member of a struct/union/class. |
2792 | class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> { |
2793 | unsigned BitField : 1; |
2794 | unsigned Mutable : 1; |
2795 | mutable unsigned CachedFieldIndex : 30; |
2796 | |
2797 | /// The kinds of value we can store in InitializerOrBitWidth. |
2798 | /// |
2799 | /// Note that this is compatible with InClassInitStyle except for |
2800 | /// ISK_CapturedVLAType. |
2801 | enum InitStorageKind { |
2802 | /// If the pointer is null, there's nothing special. Otherwise, |
2803 | /// this is a bitfield and the pointer is the Expr* storing the |
2804 | /// bit-width. |
2805 | ISK_NoInit = (unsigned) ICIS_NoInit, |
2806 | |
2807 | /// The pointer is an (optional due to delayed parsing) Expr* |
2808 | /// holding the copy-initializer. |
2809 | ISK_InClassCopyInit = (unsigned) ICIS_CopyInit, |
2810 | |
2811 | /// The pointer is an (optional due to delayed parsing) Expr* |
2812 | /// holding the list-initializer. |
2813 | ISK_InClassListInit = (unsigned) ICIS_ListInit, |
2814 | |
2815 | /// The pointer is a VariableArrayType* that's been captured; |
2816 | /// the enclosing context is a lambda or captured statement. |
2817 | ISK_CapturedVLAType, |
2818 | }; |
2819 | |
2820 | /// If this is a bitfield with a default member initializer, this |
2821 | /// structure is used to represent the two expressions. |
2822 | struct InitAndBitWidth { |
2823 | Expr *Init; |
2824 | Expr *BitWidth; |
2825 | }; |
2826 | |
2827 | /// Storage for either the bit-width, the in-class initializer, or |
2828 | /// both (via InitAndBitWidth), or the captured variable length array bound. |
2829 | /// |
2830 | /// If the storage kind is ISK_InClassCopyInit or |
2831 | /// ISK_InClassListInit, but the initializer is null, then this |
2832 | /// field has an in-class initializer that has not yet been parsed |
2833 | /// and attached. |
2834 | // FIXME: Tail-allocate this to reduce the size of FieldDecl in the |
2835 | // overwhelmingly common case that we have none of these things. |
2836 | llvm::PointerIntPair<void *, 2, InitStorageKind> InitStorage; |
2837 | |
2838 | protected: |
2839 | FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc, |
2840 | SourceLocation IdLoc, IdentifierInfo *Id, |
2841 | QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
2842 | InClassInitStyle InitStyle) |
2843 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
2844 | BitField(false), Mutable(Mutable), CachedFieldIndex(0), |
2845 | InitStorage(nullptr, (InitStorageKind) InitStyle) { |
2846 | if (BW) |
2847 | setBitWidth(BW); |
2848 | } |
2849 | |
2850 | public: |
2851 | friend class ASTDeclReader; |
2852 | friend class ASTDeclWriter; |
2853 | |
2854 | static FieldDecl *Create(const ASTContext &C, DeclContext *DC, |
2855 | SourceLocation StartLoc, SourceLocation IdLoc, |
2856 | IdentifierInfo *Id, QualType T, |
2857 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
2858 | InClassInitStyle InitStyle); |
2859 | |
2860 | static FieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2861 | |
2862 | /// Returns the index of this field within its record, |
2863 | /// as appropriate for passing to ASTRecordLayout::getFieldOffset. |
2864 | unsigned getFieldIndex() const; |
2865 | |
2866 | /// Determines whether this field is mutable (C++ only). |
2867 | bool isMutable() const { return Mutable; } |
2868 | |
2869 | /// Determines whether this field is a bitfield. |
2870 | bool isBitField() const { return BitField; } |
2871 | |
2872 | /// Determines whether this is an unnamed bitfield. |
2873 | bool isUnnamedBitfield() const { return isBitField() && !getDeclName(); } |
2874 | |
2875 | /// Determines whether this field is a |
2876 | /// representative for an anonymous struct or union. Such fields are |
2877 | /// unnamed and are implicitly generated by the implementation to |
2878 | /// store the data for the anonymous union or struct. |
2879 | bool isAnonymousStructOrUnion() const; |
2880 | |
2881 | Expr *getBitWidth() const { |
2882 | if (!BitField) |
2883 | return nullptr; |
2884 | void *Ptr = InitStorage.getPointer(); |
2885 | if (getInClassInitStyle()) |
2886 | return static_cast<InitAndBitWidth*>(Ptr)->BitWidth; |
2887 | return static_cast<Expr*>(Ptr); |
2888 | } |
2889 | |
2890 | unsigned getBitWidthValue(const ASTContext &Ctx) const; |
2891 | |
2892 | /// Set the bit-field width for this member. |
2893 | // Note: used by some clients (i.e., do not remove it). |
2894 | void setBitWidth(Expr *Width) { |
2895 | assert(!hasCapturedVLAType() && !BitField &&((!hasCapturedVLAType() && !BitField && "bit width or captured type already set" ) ? static_cast<void> (0) : __assert_fail ("!hasCapturedVLAType() && !BitField && \"bit width or captured type already set\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 2896, __PRETTY_FUNCTION__)) |
2896 | "bit width or captured type already set")((!hasCapturedVLAType() && !BitField && "bit width or captured type already set" ) ? static_cast<void> (0) : __assert_fail ("!hasCapturedVLAType() && !BitField && \"bit width or captured type already set\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 2896, __PRETTY_FUNCTION__)); |
2897 | assert(Width && "no bit width specified")((Width && "no bit width specified") ? static_cast< void> (0) : __assert_fail ("Width && \"no bit width specified\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 2897, __PRETTY_FUNCTION__)); |
2898 | InitStorage.setPointer( |
2899 | InitStorage.getInt() |
2900 | ? new (getASTContext()) |
2901 | InitAndBitWidth{getInClassInitializer(), Width} |
2902 | : static_cast<void*>(Width)); |
2903 | BitField = true; |
2904 | } |
2905 | |
2906 | /// Remove the bit-field width from this member. |
2907 | // Note: used by some clients (i.e., do not remove it). |
2908 | void removeBitWidth() { |
2909 | assert(isBitField() && "no bitfield width to remove")((isBitField() && "no bitfield width to remove") ? static_cast <void> (0) : __assert_fail ("isBitField() && \"no bitfield width to remove\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 2909, __PRETTY_FUNCTION__)); |
2910 | InitStorage.setPointer(getInClassInitializer()); |
2911 | BitField = false; |
2912 | } |
2913 | |
2914 | /// Is this a zero-length bit-field? Such bit-fields aren't really bit-fields |
2915 | /// at all and instead act as a separator between contiguous runs of other |
2916 | /// bit-fields. |
2917 | bool isZeroLengthBitField(const ASTContext &Ctx) const; |
2918 | |
2919 | /// Determine if this field is a subobject of zero size, that is, either a |
2920 | /// zero-length bit-field or a field of empty class type with the |
2921 | /// [[no_unique_address]] attribute. |
2922 | bool isZeroSize(const ASTContext &Ctx) const; |
2923 | |
2924 | /// Get the kind of (C++11) default member initializer that this field has. |
2925 | InClassInitStyle getInClassInitStyle() const { |
2926 | InitStorageKind storageKind = InitStorage.getInt(); |
2927 | return (storageKind == ISK_CapturedVLAType |
2928 | ? ICIS_NoInit : (InClassInitStyle) storageKind); |
2929 | } |
2930 | |
2931 | /// Determine whether this member has a C++11 default member initializer. |
2932 | bool hasInClassInitializer() const { |
2933 | return getInClassInitStyle() != ICIS_NoInit; |
2934 | } |
2935 | |
2936 | /// Get the C++11 default member initializer for this member, or null if one |
2937 | /// has not been set. If a valid declaration has a default member initializer, |
2938 | /// but this returns null, then we have not parsed and attached it yet. |
2939 | Expr *getInClassInitializer() const { |
2940 | if (!hasInClassInitializer()) |
2941 | return nullptr; |
2942 | void *Ptr = InitStorage.getPointer(); |
2943 | if (BitField) |
2944 | return static_cast<InitAndBitWidth*>(Ptr)->Init; |
2945 | return static_cast<Expr*>(Ptr); |
2946 | } |
2947 | |
2948 | /// Set the C++11 in-class initializer for this member. |
2949 | void setInClassInitializer(Expr *Init) { |
2950 | assert(hasInClassInitializer() && !getInClassInitializer())((hasInClassInitializer() && !getInClassInitializer() ) ? static_cast<void> (0) : __assert_fail ("hasInClassInitializer() && !getInClassInitializer()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 2950, __PRETTY_FUNCTION__)); |
2951 | if (BitField) |
2952 | static_cast<InitAndBitWidth*>(InitStorage.getPointer())->Init = Init; |
2953 | else |
2954 | InitStorage.setPointer(Init); |
2955 | } |
2956 | |
2957 | /// Remove the C++11 in-class initializer from this member. |
2958 | void removeInClassInitializer() { |
2959 | assert(hasInClassInitializer() && "no initializer to remove")((hasInClassInitializer() && "no initializer to remove" ) ? static_cast<void> (0) : __assert_fail ("hasInClassInitializer() && \"no initializer to remove\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 2959, __PRETTY_FUNCTION__)); |
2960 | InitStorage.setPointerAndInt(getBitWidth(), ISK_NoInit); |
2961 | } |
2962 | |
2963 | /// Determine whether this member captures the variable length array |
2964 | /// type. |
2965 | bool hasCapturedVLAType() const { |
2966 | return InitStorage.getInt() == ISK_CapturedVLAType; |
2967 | } |
2968 | |
2969 | /// Get the captured variable length array type. |
2970 | const VariableArrayType *getCapturedVLAType() const { |
2971 | return hasCapturedVLAType() ? static_cast<const VariableArrayType *>( |
2972 | InitStorage.getPointer()) |
2973 | : nullptr; |
2974 | } |
2975 | |
2976 | /// Set the captured variable length array type for this field. |
2977 | void setCapturedVLAType(const VariableArrayType *VLAType); |
2978 | |
2979 | /// Returns the parent of this field declaration, which |
2980 | /// is the struct in which this field is defined. |
2981 | /// |
2982 | /// Returns null if this is not a normal class/struct field declaration, e.g. |
2983 | /// ObjCAtDefsFieldDecl, ObjCIvarDecl. |
2984 | const RecordDecl *getParent() const { |
2985 | return dyn_cast<RecordDecl>(getDeclContext()); |
2986 | } |
2987 | |
2988 | RecordDecl *getParent() { |
2989 | return dyn_cast<RecordDecl>(getDeclContext()); |
2990 | } |
2991 | |
2992 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
2993 | |
2994 | /// Retrieves the canonical declaration of this field. |
2995 | FieldDecl *getCanonicalDecl() override { return getFirstDecl(); } |
2996 | const FieldDecl *getCanonicalDecl() const { return getFirstDecl(); } |
2997 | |
2998 | // Implement isa/cast/dyncast/etc. |
2999 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3000 | static bool classofKind(Kind K) { return K >= firstField && K <= lastField; } |
3001 | }; |
3002 | |
3003 | /// An instance of this object exists for each enum constant |
3004 | /// that is defined. For example, in "enum X {a,b}", each of a/b are |
3005 | /// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a |
3006 | /// TagType for the X EnumDecl. |
3007 | class EnumConstantDecl : public ValueDecl, public Mergeable<EnumConstantDecl> { |
3008 | Stmt *Init; // an integer constant expression |
3009 | llvm::APSInt Val; // The value. |
3010 | |
3011 | protected: |
3012 | EnumConstantDecl(DeclContext *DC, SourceLocation L, |
3013 | IdentifierInfo *Id, QualType T, Expr *E, |
3014 | const llvm::APSInt &V) |
3015 | : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {} |
3016 | |
3017 | public: |
3018 | friend class StmtIteratorBase; |
3019 | |
3020 | static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC, |
3021 | SourceLocation L, IdentifierInfo *Id, |
3022 | QualType T, Expr *E, |
3023 | const llvm::APSInt &V); |
3024 | static EnumConstantDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3025 | |
3026 | const Expr *getInitExpr() const { return (const Expr*) Init; } |
3027 | Expr *getInitExpr() { return (Expr*) Init; } |
3028 | const llvm::APSInt &getInitVal() const { return Val; } |
3029 | |
3030 | void setInitExpr(Expr *E) { Init = (Stmt*) E; } |
3031 | void setInitVal(const llvm::APSInt &V) { Val = V; } |
3032 | |
3033 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
3034 | |
3035 | /// Retrieves the canonical declaration of this enumerator. |
3036 | EnumConstantDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3037 | const EnumConstantDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3038 | |
3039 | // Implement isa/cast/dyncast/etc. |
3040 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3041 | static bool classofKind(Kind K) { return K == EnumConstant; } |
3042 | }; |
3043 | |
3044 | /// Represents a field injected from an anonymous union/struct into the parent |
3045 | /// scope. These are always implicit. |
3046 | class IndirectFieldDecl : public ValueDecl, |
3047 | public Mergeable<IndirectFieldDecl> { |
3048 | NamedDecl **Chaining; |
3049 | unsigned ChainingSize; |
3050 | |
3051 | IndirectFieldDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
3052 | DeclarationName N, QualType T, |
3053 | MutableArrayRef<NamedDecl *> CH); |
3054 | |
3055 | void anchor() override; |
3056 | |
3057 | public: |
3058 | friend class ASTDeclReader; |
3059 | |
3060 | static IndirectFieldDecl *Create(ASTContext &C, DeclContext *DC, |
3061 | SourceLocation L, IdentifierInfo *Id, |
3062 | QualType T, llvm::MutableArrayRef<NamedDecl *> CH); |
3063 | |
3064 | static IndirectFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3065 | |
3066 | using chain_iterator = ArrayRef<NamedDecl *>::const_iterator; |
3067 | |
3068 | ArrayRef<NamedDecl *> chain() const { |
3069 | return llvm::makeArrayRef(Chaining, ChainingSize); |
3070 | } |
3071 | chain_iterator chain_begin() const { return chain().begin(); } |
3072 | chain_iterator chain_end() const { return chain().end(); } |
3073 | |
3074 | unsigned getChainingSize() const { return ChainingSize; } |
3075 | |
3076 | FieldDecl *getAnonField() const { |
3077 | assert(chain().size() >= 2)((chain().size() >= 2) ? static_cast<void> (0) : __assert_fail ("chain().size() >= 2", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 3077, __PRETTY_FUNCTION__)); |
3078 | return cast<FieldDecl>(chain().back()); |
3079 | } |
3080 | |
3081 | VarDecl *getVarDecl() const { |
3082 | assert(chain().size() >= 2)((chain().size() >= 2) ? static_cast<void> (0) : __assert_fail ("chain().size() >= 2", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 3082, __PRETTY_FUNCTION__)); |
3083 | return dyn_cast<VarDecl>(chain().front()); |
3084 | } |
3085 | |
3086 | IndirectFieldDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3087 | const IndirectFieldDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3088 | |
3089 | // Implement isa/cast/dyncast/etc. |
3090 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3091 | static bool classofKind(Kind K) { return K == IndirectField; } |
3092 | }; |
3093 | |
3094 | /// Represents a declaration of a type. |
3095 | class TypeDecl : public NamedDecl { |
3096 | friend class ASTContext; |
3097 | |
3098 | /// This indicates the Type object that represents |
3099 | /// this TypeDecl. It is a cache maintained by |
3100 | /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and |
3101 | /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl. |
3102 | mutable const Type *TypeForDecl = nullptr; |
3103 | |
3104 | /// The start of the source range for this declaration. |
3105 | SourceLocation LocStart; |
3106 | |
3107 | void anchor() override; |
3108 | |
3109 | protected: |
3110 | TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id, |
3111 | SourceLocation StartL = SourceLocation()) |
3112 | : NamedDecl(DK, DC, L, Id), LocStart(StartL) {} |
3113 | |
3114 | public: |
3115 | // Low-level accessor. If you just want the type defined by this node, |
3116 | // check out ASTContext::getTypeDeclType or one of |
3117 | // ASTContext::getTypedefType, ASTContext::getRecordType, etc. if you |
3118 | // already know the specific kind of node this is. |
3119 | const Type *getTypeForDecl() const { return TypeForDecl; } |
3120 | void setTypeForDecl(const Type *TD) { TypeForDecl = TD; } |
3121 | |
3122 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return LocStart; } |
3123 | void setLocStart(SourceLocation L) { LocStart = L; } |
3124 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
3125 | if (LocStart.isValid()) |
3126 | return SourceRange(LocStart, getLocation()); |
3127 | else |
3128 | return SourceRange(getLocation()); |
3129 | } |
3130 | |
3131 | // Implement isa/cast/dyncast/etc. |
3132 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3133 | static bool classofKind(Kind K) { return K >= firstType && K <= lastType; } |
3134 | }; |
3135 | |
3136 | /// Base class for declarations which introduce a typedef-name. |
3137 | class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> { |
3138 | struct alignas(8) ModedTInfo { |
3139 | TypeSourceInfo *first; |
3140 | QualType second; |
3141 | }; |
3142 | |
3143 | /// If int part is 0, we have not computed IsTransparentTag. |
3144 | /// Otherwise, IsTransparentTag is (getInt() >> 1). |
3145 | mutable llvm::PointerIntPair< |
3146 | llvm::PointerUnion<TypeSourceInfo *, ModedTInfo *>, 2> |
3147 | MaybeModedTInfo; |
3148 | |
3149 | void anchor() override; |
3150 | |
3151 | protected: |
3152 | TypedefNameDecl(Kind DK, ASTContext &C, DeclContext *DC, |
3153 | SourceLocation StartLoc, SourceLocation IdLoc, |
3154 | IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3155 | : TypeDecl(DK, DC, IdLoc, Id, StartLoc), redeclarable_base(C), |
3156 | MaybeModedTInfo(TInfo, 0) {} |
3157 | |
3158 | using redeclarable_base = Redeclarable<TypedefNameDecl>; |
3159 | |
3160 | TypedefNameDecl *getNextRedeclarationImpl() override { |
3161 | return getNextRedeclaration(); |
3162 | } |
3163 | |
3164 | TypedefNameDecl *getPreviousDeclImpl() override { |
3165 | return getPreviousDecl(); |
3166 | } |
3167 | |
3168 | TypedefNameDecl *getMostRecentDeclImpl() override { |
3169 | return getMostRecentDecl(); |
3170 | } |
3171 | |
3172 | public: |
3173 | using redecl_range = redeclarable_base::redecl_range; |
3174 | using redecl_iterator = redeclarable_base::redecl_iterator; |
3175 | |
3176 | using redeclarable_base::redecls_begin; |
3177 | using redeclarable_base::redecls_end; |
3178 | using redeclarable_base::redecls; |
3179 | using redeclarable_base::getPreviousDecl; |
3180 | using redeclarable_base::getMostRecentDecl; |
3181 | using redeclarable_base::isFirstDecl; |
3182 | |
3183 | bool isModed() const { |
3184 | return MaybeModedTInfo.getPointer().is<ModedTInfo *>(); |
3185 | } |
3186 | |
3187 | TypeSourceInfo *getTypeSourceInfo() const { |
3188 | return isModed() ? MaybeModedTInfo.getPointer().get<ModedTInfo *>()->first |
3189 | : MaybeModedTInfo.getPointer().get<TypeSourceInfo *>(); |
3190 | } |
3191 | |
3192 | QualType getUnderlyingType() const { |
3193 | return isModed() ? MaybeModedTInfo.getPointer().get<ModedTInfo *>()->second |
3194 | : MaybeModedTInfo.getPointer() |
3195 | .get<TypeSourceInfo *>() |
3196 | ->getType(); |
3197 | } |
3198 | |
3199 | void setTypeSourceInfo(TypeSourceInfo *newType) { |
3200 | MaybeModedTInfo.setPointer(newType); |
3201 | } |
3202 | |
3203 | void setModedTypeSourceInfo(TypeSourceInfo *unmodedTSI, QualType modedTy) { |
3204 | MaybeModedTInfo.setPointer(new (getASTContext(), 8) |
3205 | ModedTInfo({unmodedTSI, modedTy})); |
3206 | } |
3207 | |
3208 | /// Retrieves the canonical declaration of this typedef-name. |
3209 | TypedefNameDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3210 | const TypedefNameDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3211 | |
3212 | /// Retrieves the tag declaration for which this is the typedef name for |
3213 | /// linkage purposes, if any. |
3214 | /// |
3215 | /// \param AnyRedecl Look for the tag declaration in any redeclaration of |
3216 | /// this typedef declaration. |
3217 | TagDecl *getAnonDeclWithTypedefName(bool AnyRedecl = false) const; |
3218 | |
3219 | /// Determines if this typedef shares a name and spelling location with its |
3220 | /// underlying tag type, as is the case with the NS_ENUM macro. |
3221 | bool isTransparentTag() const { |
3222 | if (MaybeModedTInfo.getInt()) |
3223 | return MaybeModedTInfo.getInt() & 0x2; |
3224 | return isTransparentTagSlow(); |
3225 | } |
3226 | |
3227 | // Implement isa/cast/dyncast/etc. |
3228 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3229 | static bool classofKind(Kind K) { |
3230 | return K >= firstTypedefName && K <= lastTypedefName; |
3231 | } |
3232 | |
3233 | private: |
3234 | bool isTransparentTagSlow() const; |
3235 | }; |
3236 | |
3237 | /// Represents the declaration of a typedef-name via the 'typedef' |
3238 | /// type specifier. |
3239 | class TypedefDecl : public TypedefNameDecl { |
3240 | TypedefDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3241 | SourceLocation IdLoc, IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3242 | : TypedefNameDecl(Typedef, C, DC, StartLoc, IdLoc, Id, TInfo) {} |
3243 | |
3244 | public: |
3245 | static TypedefDecl *Create(ASTContext &C, DeclContext *DC, |
3246 | SourceLocation StartLoc, SourceLocation IdLoc, |
3247 | IdentifierInfo *Id, TypeSourceInfo *TInfo); |
3248 | static TypedefDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3249 | |
3250 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
3251 | |
3252 | // Implement isa/cast/dyncast/etc. |
3253 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3254 | static bool classofKind(Kind K) { return K == Typedef; } |
3255 | }; |
3256 | |
3257 | /// Represents the declaration of a typedef-name via a C++11 |
3258 | /// alias-declaration. |
3259 | class TypeAliasDecl : public TypedefNameDecl { |
3260 | /// The template for which this is the pattern, if any. |
3261 | TypeAliasTemplateDecl *Template; |
3262 | |
3263 | TypeAliasDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3264 | SourceLocation IdLoc, IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3265 | : TypedefNameDecl(TypeAlias, C, DC, StartLoc, IdLoc, Id, TInfo), |
3266 | Template(nullptr) {} |
3267 | |
3268 | public: |
3269 | static TypeAliasDecl *Create(ASTContext &C, DeclContext *DC, |
3270 | SourceLocation StartLoc, SourceLocation IdLoc, |
3271 | IdentifierInfo *Id, TypeSourceInfo *TInfo); |
3272 | static TypeAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3273 | |
3274 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
3275 | |
3276 | TypeAliasTemplateDecl *getDescribedAliasTemplate() const { return Template; } |
3277 | void setDescribedAliasTemplate(TypeAliasTemplateDecl *TAT) { Template = TAT; } |
3278 | |
3279 | // Implement isa/cast/dyncast/etc. |
3280 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3281 | static bool classofKind(Kind K) { return K == TypeAlias; } |
3282 | }; |
3283 | |
3284 | /// Represents the declaration of a struct/union/class/enum. |
3285 | class TagDecl : public TypeDecl, |
3286 | public DeclContext, |
3287 | public Redeclarable<TagDecl> { |
3288 | // This class stores some data in DeclContext::TagDeclBits |
3289 | // to save some space. Use the provided accessors to access it. |
3290 | public: |
3291 | // This is really ugly. |
3292 | using TagKind = TagTypeKind; |
3293 | |
3294 | private: |
3295 | SourceRange BraceRange; |
3296 | |
3297 | // A struct representing syntactic qualifier info, |
3298 | // to be used for the (uncommon) case of out-of-line declarations. |
3299 | using ExtInfo = QualifierInfo; |
3300 | |
3301 | /// If the (out-of-line) tag declaration name |
3302 | /// is qualified, it points to the qualifier info (nns and range); |
3303 | /// otherwise, if the tag declaration is anonymous and it is part of |
3304 | /// a typedef or alias, it points to the TypedefNameDecl (used for mangling); |
3305 | /// otherwise, if the tag declaration is anonymous and it is used as a |
3306 | /// declaration specifier for variables, it points to the first VarDecl (used |
3307 | /// for mangling); |
3308 | /// otherwise, it is a null (TypedefNameDecl) pointer. |
3309 | llvm::PointerUnion<TypedefNameDecl *, ExtInfo *> TypedefNameDeclOrQualifier; |
3310 | |
3311 | bool hasExtInfo() const { return TypedefNameDeclOrQualifier.is<ExtInfo *>(); } |
3312 | ExtInfo *getExtInfo() { return TypedefNameDeclOrQualifier.get<ExtInfo *>(); } |
3313 | const ExtInfo *getExtInfo() const { |
3314 | return TypedefNameDeclOrQualifier.get<ExtInfo *>(); |
3315 | } |
3316 | |
3317 | protected: |
3318 | TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
3319 | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
3320 | SourceLocation StartL); |
3321 | |
3322 | using redeclarable_base = Redeclarable<TagDecl>; |
3323 | |
3324 | TagDecl *getNextRedeclarationImpl() override { |
3325 | return getNextRedeclaration(); |
3326 | } |
3327 | |
3328 | TagDecl *getPreviousDeclImpl() override { |
3329 | return getPreviousDecl(); |
3330 | } |
3331 | |
3332 | TagDecl *getMostRecentDeclImpl() override { |
3333 | return getMostRecentDecl(); |
3334 | } |
3335 | |
3336 | /// Completes the definition of this tag declaration. |
3337 | /// |
3338 | /// This is a helper function for derived classes. |
3339 | void completeDefinition(); |
3340 | |
3341 | /// True if this decl is currently being defined. |
3342 | void setBeingDefined(bool V = true) { TagDeclBits.IsBeingDefined = V; } |
3343 | |
3344 | /// Indicates whether it is possible for declarations of this kind |
3345 | /// to have an out-of-date definition. |
3346 | /// |
3347 | /// This option is only enabled when modules are enabled. |
3348 | void setMayHaveOutOfDateDef(bool V = true) { |
3349 | TagDeclBits.MayHaveOutOfDateDef = V; |
3350 | } |
3351 | |
3352 | public: |
3353 | friend class ASTDeclReader; |
3354 | friend class ASTDeclWriter; |
3355 | |
3356 | using redecl_range = redeclarable_base::redecl_range; |
3357 | using redecl_iterator = redeclarable_base::redecl_iterator; |
3358 | |
3359 | using redeclarable_base::redecls_begin; |
3360 | using redeclarable_base::redecls_end; |
3361 | using redeclarable_base::redecls; |
3362 | using redeclarable_base::getPreviousDecl; |
3363 | using redeclarable_base::getMostRecentDecl; |
3364 | using redeclarable_base::isFirstDecl; |
3365 | |
3366 | SourceRange getBraceRange() const { return BraceRange; } |
3367 | void setBraceRange(SourceRange R) { BraceRange = R; } |
3368 | |
3369 | /// Return SourceLocation representing start of source |
3370 | /// range ignoring outer template declarations. |
3371 | SourceLocation getInnerLocStart() const { return getBeginLoc(); } |
3372 | |
3373 | /// Return SourceLocation representing start of source |
3374 | /// range taking into account any outer template declarations. |
3375 | SourceLocation getOuterLocStart() const; |
3376 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
3377 | |
3378 | TagDecl *getCanonicalDecl() override; |
3379 | const TagDecl *getCanonicalDecl() const { |
3380 | return const_cast<TagDecl*>(this)->getCanonicalDecl(); |
3381 | } |
3382 | |
3383 | /// Return true if this declaration is a completion definition of the type. |
3384 | /// Provided for consistency. |
3385 | bool isThisDeclarationADefinition() const { |
3386 | return isCompleteDefinition(); |
3387 | } |
3388 | |
3389 | /// Return true if this decl has its body fully specified. |
3390 | bool isCompleteDefinition() const { return TagDeclBits.IsCompleteDefinition; } |
3391 | |
3392 | /// True if this decl has its body fully specified. |
3393 | void setCompleteDefinition(bool V = true) { |
3394 | TagDeclBits.IsCompleteDefinition = V; |
3395 | } |
3396 | |
3397 | /// Return true if this complete decl is |
3398 | /// required to be complete for some existing use. |
3399 | bool isCompleteDefinitionRequired() const { |
3400 | return TagDeclBits.IsCompleteDefinitionRequired; |
3401 | } |
3402 | |
3403 | /// True if this complete decl is |
3404 | /// required to be complete for some existing use. |
3405 | void setCompleteDefinitionRequired(bool V = true) { |
3406 | TagDeclBits.IsCompleteDefinitionRequired = V; |
3407 | } |
3408 | |
3409 | /// Return true if this decl is currently being defined. |
3410 | bool isBeingDefined() const { return TagDeclBits.IsBeingDefined; } |
3411 | |
3412 | /// True if this tag declaration is "embedded" (i.e., defined or declared |
3413 | /// for the very first time) in the syntax of a declarator. |
3414 | bool isEmbeddedInDeclarator() const { |
3415 | return TagDeclBits.IsEmbeddedInDeclarator; |
3416 | } |
3417 | |
3418 | /// True if this tag declaration is "embedded" (i.e., defined or declared |
3419 | /// for the very first time) in the syntax of a declarator. |
3420 | void setEmbeddedInDeclarator(bool isInDeclarator) { |
3421 | TagDeclBits.IsEmbeddedInDeclarator = isInDeclarator; |
3422 | } |
3423 | |
3424 | /// True if this tag is free standing, e.g. "struct foo;". |
3425 | bool isFreeStanding() const { return TagDeclBits.IsFreeStanding; } |
3426 | |
3427 | /// True if this tag is free standing, e.g. "struct foo;". |
3428 | void setFreeStanding(bool isFreeStanding = true) { |
3429 | TagDeclBits.IsFreeStanding = isFreeStanding; |
3430 | } |
3431 | |
3432 | /// Indicates whether it is possible for declarations of this kind |
3433 | /// to have an out-of-date definition. |
3434 | /// |
3435 | /// This option is only enabled when modules are enabled. |
3436 | bool mayHaveOutOfDateDef() const { return TagDeclBits.MayHaveOutOfDateDef; } |
3437 | |
3438 | /// Whether this declaration declares a type that is |
3439 | /// dependent, i.e., a type that somehow depends on template |
3440 | /// parameters. |
3441 | bool isDependentType() const { return isDependentContext(); } |
3442 | |
3443 | /// Starts the definition of this tag declaration. |
3444 | /// |
3445 | /// This method should be invoked at the beginning of the definition |
3446 | /// of this tag declaration. It will set the tag type into a state |
3447 | /// where it is in the process of being defined. |
3448 | void startDefinition(); |
3449 | |
3450 | /// Returns the TagDecl that actually defines this |
3451 | /// struct/union/class/enum. When determining whether or not a |
3452 | /// struct/union/class/enum has a definition, one should use this |
3453 | /// method as opposed to 'isDefinition'. 'isDefinition' indicates |
3454 | /// whether or not a specific TagDecl is defining declaration, not |
3455 | /// whether or not the struct/union/class/enum type is defined. |
3456 | /// This method returns NULL if there is no TagDecl that defines |
3457 | /// the struct/union/class/enum. |
3458 | TagDecl *getDefinition() const; |
3459 | |
3460 | StringRef getKindName() const { |
3461 | return TypeWithKeyword::getTagTypeKindName(getTagKind()); |
3462 | } |
3463 | |
3464 | TagKind getTagKind() const { |
3465 | return static_cast<TagKind>(TagDeclBits.TagDeclKind); |
3466 | } |
3467 | |
3468 | void setTagKind(TagKind TK) { TagDeclBits.TagDeclKind = TK; } |
3469 | |
3470 | bool isStruct() const { return getTagKind() == TTK_Struct; } |
3471 | bool isInterface() const { return getTagKind() == TTK_Interface; } |
3472 | bool isClass() const { return getTagKind() == TTK_Class; } |
3473 | bool isUnion() const { return getTagKind() == TTK_Union; } |
3474 | bool isEnum() const { return getTagKind() == TTK_Enum; } |
3475 | |
3476 | /// Is this tag type named, either directly or via being defined in |
3477 | /// a typedef of this type? |
3478 | /// |
3479 | /// C++11 [basic.link]p8: |
3480 | /// A type is said to have linkage if and only if: |
3481 | /// - it is a class or enumeration type that is named (or has a |
3482 | /// name for linkage purposes) and the name has linkage; ... |
3483 | /// C++11 [dcl.typedef]p9: |
3484 | /// If the typedef declaration defines an unnamed class (or enum), |
3485 | /// the first typedef-name declared by the declaration to be that |
3486 | /// class type (or enum type) is used to denote the class type (or |
3487 | /// enum type) for linkage purposes only. |
3488 | /// |
3489 | /// C does not have an analogous rule, but the same concept is |
3490 | /// nonetheless useful in some places. |
3491 | bool hasNameForLinkage() const { |
3492 | return (getDeclName() || getTypedefNameForAnonDecl()); |
3493 | } |
3494 | |
3495 | TypedefNameDecl *getTypedefNameForAnonDecl() const { |
3496 | return hasExtInfo() ? nullptr |
3497 | : TypedefNameDeclOrQualifier.get<TypedefNameDecl *>(); |
3498 | } |
3499 | |
3500 | void setTypedefNameForAnonDecl(TypedefNameDecl *TDD); |
3501 | |
3502 | /// Retrieve the nested-name-specifier that qualifies the name of this |
3503 | /// declaration, if it was present in the source. |
3504 | NestedNameSpecifier *getQualifier() const { |
3505 | return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() |
3506 | : nullptr; |
3507 | } |
3508 | |
3509 | /// Retrieve the nested-name-specifier (with source-location |
3510 | /// information) that qualifies the name of this declaration, if it was |
3511 | /// present in the source. |
3512 | NestedNameSpecifierLoc getQualifierLoc() const { |
3513 | return hasExtInfo() ? getExtInfo()->QualifierLoc |
3514 | : NestedNameSpecifierLoc(); |
3515 | } |
3516 | |
3517 | void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); |
3518 | |
3519 | unsigned getNumTemplateParameterLists() const { |
3520 | return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; |
3521 | } |
3522 | |
3523 | TemplateParameterList *getTemplateParameterList(unsigned i) const { |
3524 | assert(i < getNumTemplateParameterLists())((i < getNumTemplateParameterLists()) ? static_cast<void > (0) : __assert_fail ("i < getNumTemplateParameterLists()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 3524, __PRETTY_FUNCTION__)); |
3525 | return getExtInfo()->TemplParamLists[i]; |
3526 | } |
3527 | |
3528 | void setTemplateParameterListsInfo(ASTContext &Context, |
3529 | ArrayRef<TemplateParameterList *> TPLists); |
3530 | |
3531 | // Implement isa/cast/dyncast/etc. |
3532 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3533 | static bool classofKind(Kind K) { return K >= firstTag && K <= lastTag; } |
3534 | |
3535 | static DeclContext *castToDeclContext(const TagDecl *D) { |
3536 | return static_cast<DeclContext *>(const_cast<TagDecl*>(D)); |
3537 | } |
3538 | |
3539 | static TagDecl *castFromDeclContext(const DeclContext *DC) { |
3540 | return static_cast<TagDecl *>(const_cast<DeclContext*>(DC)); |
3541 | } |
3542 | }; |
3543 | |
3544 | /// Represents an enum. In C++11, enums can be forward-declared |
3545 | /// with a fixed underlying type, and in C we allow them to be forward-declared |
3546 | /// with no underlying type as an extension. |
3547 | class EnumDecl : public TagDecl { |
3548 | // This class stores some data in DeclContext::EnumDeclBits |
3549 | // to save some space. Use the provided accessors to access it. |
3550 | |
3551 | /// This represent the integer type that the enum corresponds |
3552 | /// to for code generation purposes. Note that the enumerator constants may |
3553 | /// have a different type than this does. |
3554 | /// |
3555 | /// If the underlying integer type was explicitly stated in the source |
3556 | /// code, this is a TypeSourceInfo* for that type. Otherwise this type |
3557 | /// was automatically deduced somehow, and this is a Type*. |
3558 | /// |
3559 | /// Normally if IsFixed(), this would contain a TypeSourceInfo*, but in |
3560 | /// some cases it won't. |
3561 | /// |
3562 | /// The underlying type of an enumeration never has any qualifiers, so |
3563 | /// we can get away with just storing a raw Type*, and thus save an |
3564 | /// extra pointer when TypeSourceInfo is needed. |
3565 | llvm::PointerUnion<const Type *, TypeSourceInfo *> IntegerType; |
3566 | |
3567 | /// The integer type that values of this type should |
3568 | /// promote to. In C, enumerators are generally of an integer type |
3569 | /// directly, but gcc-style large enumerators (and all enumerators |
3570 | /// in C++) are of the enum type instead. |
3571 | QualType PromotionType; |
3572 | |
3573 | /// If this enumeration is an instantiation of a member enumeration |
3574 | /// of a class template specialization, this is the member specialization |
3575 | /// information. |
3576 | MemberSpecializationInfo *SpecializationInfo = nullptr; |
3577 | |
3578 | /// Store the ODRHash after first calculation. |
3579 | /// The corresponding flag HasODRHash is in EnumDeclBits |
3580 | /// and can be accessed with the provided accessors. |
3581 | unsigned ODRHash; |
3582 | |
3583 | EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3584 | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
3585 | bool Scoped, bool ScopedUsingClassTag, bool Fixed); |
3586 | |
3587 | void anchor() override; |
3588 | |
3589 | void setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
3590 | TemplateSpecializationKind TSK); |
3591 | |
3592 | /// Sets the width in bits required to store all the |
3593 | /// non-negative enumerators of this enum. |
3594 | void setNumPositiveBits(unsigned Num) { |
3595 | EnumDeclBits.NumPositiveBits = Num; |
3596 | assert(EnumDeclBits.NumPositiveBits == Num && "can't store this bitcount")((EnumDeclBits.NumPositiveBits == Num && "can't store this bitcount" ) ? static_cast<void> (0) : __assert_fail ("EnumDeclBits.NumPositiveBits == Num && \"can't store this bitcount\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 3596, __PRETTY_FUNCTION__)); |
3597 | } |
3598 | |
3599 | /// Returns the width in bits required to store all the |
3600 | /// negative enumerators of this enum. (see getNumNegativeBits) |
3601 | void setNumNegativeBits(unsigned Num) { EnumDeclBits.NumNegativeBits = Num; } |
3602 | |
3603 | public: |
3604 | /// True if this tag declaration is a scoped enumeration. Only |
3605 | /// possible in C++11 mode. |
3606 | void setScoped(bool Scoped = true) { EnumDeclBits.IsScoped = Scoped; } |
3607 | |
3608 | /// If this tag declaration is a scoped enum, |
3609 | /// then this is true if the scoped enum was declared using the class |
3610 | /// tag, false if it was declared with the struct tag. No meaning is |
3611 | /// associated if this tag declaration is not a scoped enum. |
3612 | void setScopedUsingClassTag(bool ScopedUCT = true) { |
3613 | EnumDeclBits.IsScopedUsingClassTag = ScopedUCT; |
3614 | } |
3615 | |
3616 | /// True if this is an Objective-C, C++11, or |
3617 | /// Microsoft-style enumeration with a fixed underlying type. |
3618 | void setFixed(bool Fixed = true) { EnumDeclBits.IsFixed = Fixed; } |
3619 | |
3620 | private: |
3621 | /// True if a valid hash is stored in ODRHash. |
3622 | bool hasODRHash() const { return EnumDeclBits.HasODRHash; } |
3623 | void setHasODRHash(bool Hash = true) { EnumDeclBits.HasODRHash = Hash; } |
3624 | |
3625 | public: |
3626 | friend class ASTDeclReader; |
3627 | |
3628 | EnumDecl *getCanonicalDecl() override { |
3629 | return cast<EnumDecl>(TagDecl::getCanonicalDecl()); |
3630 | } |
3631 | const EnumDecl *getCanonicalDecl() const { |
3632 | return const_cast<EnumDecl*>(this)->getCanonicalDecl(); |
3633 | } |
3634 | |
3635 | EnumDecl *getPreviousDecl() { |
3636 | return cast_or_null<EnumDecl>( |
3637 | static_cast<TagDecl *>(this)->getPreviousDecl()); |
3638 | } |
3639 | const EnumDecl *getPreviousDecl() const { |
3640 | return const_cast<EnumDecl*>(this)->getPreviousDecl(); |
3641 | } |
3642 | |
3643 | EnumDecl *getMostRecentDecl() { |
3644 | return cast<EnumDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl()); |
3645 | } |
3646 | const EnumDecl *getMostRecentDecl() const { |
3647 | return const_cast<EnumDecl*>(this)->getMostRecentDecl(); |
3648 | } |
3649 | |
3650 | EnumDecl *getDefinition() const { |
3651 | return cast_or_null<EnumDecl>(TagDecl::getDefinition()); |
3652 | } |
3653 | |
3654 | static EnumDecl *Create(ASTContext &C, DeclContext *DC, |
3655 | SourceLocation StartLoc, SourceLocation IdLoc, |
3656 | IdentifierInfo *Id, EnumDecl *PrevDecl, |
3657 | bool IsScoped, bool IsScopedUsingClassTag, |
3658 | bool IsFixed); |
3659 | static EnumDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3660 | |
3661 | /// When created, the EnumDecl corresponds to a |
3662 | /// forward-declared enum. This method is used to mark the |
3663 | /// declaration as being defined; its enumerators have already been |
3664 | /// added (via DeclContext::addDecl). NewType is the new underlying |
3665 | /// type of the enumeration type. |
3666 | void completeDefinition(QualType NewType, |
3667 | QualType PromotionType, |
3668 | unsigned NumPositiveBits, |
3669 | unsigned NumNegativeBits); |
3670 | |
3671 | // Iterates through the enumerators of this enumeration. |
3672 | using enumerator_iterator = specific_decl_iterator<EnumConstantDecl>; |
3673 | using enumerator_range = |
3674 | llvm::iterator_range<specific_decl_iterator<EnumConstantDecl>>; |
3675 | |
3676 | enumerator_range enumerators() const { |
3677 | return enumerator_range(enumerator_begin(), enumerator_end()); |
3678 | } |
3679 | |
3680 | enumerator_iterator enumerator_begin() const { |
3681 | const EnumDecl *E = getDefinition(); |
3682 | if (!E) |
3683 | E = this; |
3684 | return enumerator_iterator(E->decls_begin()); |
3685 | } |
3686 | |
3687 | enumerator_iterator enumerator_end() const { |
3688 | const EnumDecl *E = getDefinition(); |
3689 | if (!E) |
3690 | E = this; |
3691 | return enumerator_iterator(E->decls_end()); |
3692 | } |
3693 | |
3694 | /// Return the integer type that enumerators should promote to. |
3695 | QualType getPromotionType() const { return PromotionType; } |
3696 | |
3697 | /// Set the promotion type. |
3698 | void setPromotionType(QualType T) { PromotionType = T; } |
3699 | |
3700 | /// Return the integer type this enum decl corresponds to. |
3701 | /// This returns a null QualType for an enum forward definition with no fixed |
3702 | /// underlying type. |
3703 | QualType getIntegerType() const { |
3704 | if (!IntegerType) |
3705 | return QualType(); |
3706 | if (const Type *T = IntegerType.dyn_cast<const Type*>()) |
3707 | return QualType(T, 0); |
3708 | return IntegerType.get<TypeSourceInfo*>()->getType().getUnqualifiedType(); |
3709 | } |
3710 | |
3711 | /// Set the underlying integer type. |
3712 | void setIntegerType(QualType T) { IntegerType = T.getTypePtrOrNull(); } |
3713 | |
3714 | /// Set the underlying integer type source info. |
3715 | void setIntegerTypeSourceInfo(TypeSourceInfo *TInfo) { IntegerType = TInfo; } |
3716 | |
3717 | /// Return the type source info for the underlying integer type, |
3718 | /// if no type source info exists, return 0. |
3719 | TypeSourceInfo *getIntegerTypeSourceInfo() const { |
3720 | return IntegerType.dyn_cast<TypeSourceInfo*>(); |
3721 | } |
3722 | |
3723 | /// Retrieve the source range that covers the underlying type if |
3724 | /// specified. |
3725 | SourceRange getIntegerTypeRange() const LLVM_READONLY__attribute__((__pure__)); |
3726 | |
3727 | /// Returns the width in bits required to store all the |
3728 | /// non-negative enumerators of this enum. |
3729 | unsigned getNumPositiveBits() const { return EnumDeclBits.NumPositiveBits; } |
3730 | |
3731 | /// Returns the width in bits required to store all the |
3732 | /// negative enumerators of this enum. These widths include |
3733 | /// the rightmost leading 1; that is: |
3734 | /// |
3735 | /// MOST NEGATIVE ENUMERATOR PATTERN NUM NEGATIVE BITS |
3736 | /// ------------------------ ------- ----------------- |
3737 | /// -1 1111111 1 |
3738 | /// -10 1110110 5 |
3739 | /// -101 1001011 8 |
3740 | unsigned getNumNegativeBits() const { return EnumDeclBits.NumNegativeBits; } |
3741 | |
3742 | /// Returns true if this is a C++11 scoped enumeration. |
3743 | bool isScoped() const { return EnumDeclBits.IsScoped; } |
3744 | |
3745 | /// Returns true if this is a C++11 scoped enumeration. |
3746 | bool isScopedUsingClassTag() const { |
3747 | return EnumDeclBits.IsScopedUsingClassTag; |
3748 | } |
3749 | |
3750 | /// Returns true if this is an Objective-C, C++11, or |
3751 | /// Microsoft-style enumeration with a fixed underlying type. |
3752 | bool isFixed() const { return EnumDeclBits.IsFixed; } |
3753 | |
3754 | unsigned getODRHash(); |
3755 | |
3756 | /// Returns true if this can be considered a complete type. |
3757 | bool isComplete() const { |
3758 | // IntegerType is set for fixed type enums and non-fixed but implicitly |
3759 | // int-sized Microsoft enums. |
3760 | return isCompleteDefinition() || IntegerType; |
3761 | } |
3762 | |
3763 | /// Returns true if this enum is either annotated with |
3764 | /// enum_extensibility(closed) or isn't annotated with enum_extensibility. |
3765 | bool isClosed() const; |
3766 | |
3767 | /// Returns true if this enum is annotated with flag_enum and isn't annotated |
3768 | /// with enum_extensibility(open). |
3769 | bool isClosedFlag() const; |
3770 | |
3771 | /// Returns true if this enum is annotated with neither flag_enum nor |
3772 | /// enum_extensibility(open). |
3773 | bool isClosedNonFlag() const; |
3774 | |
3775 | /// Retrieve the enum definition from which this enumeration could |
3776 | /// be instantiated, if it is an instantiation (rather than a non-template). |
3777 | EnumDecl *getTemplateInstantiationPattern() const; |
3778 | |
3779 | /// Returns the enumeration (declared within the template) |
3780 | /// from which this enumeration type was instantiated, or NULL if |
3781 | /// this enumeration was not instantiated from any template. |
3782 | EnumDecl *getInstantiatedFromMemberEnum() const; |
3783 | |
3784 | /// If this enumeration is a member of a specialization of a |
3785 | /// templated class, determine what kind of template specialization |
3786 | /// or instantiation this is. |
3787 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
3788 | |
3789 | /// For an enumeration member that was instantiated from a member |
3790 | /// enumeration of a templated class, set the template specialiation kind. |
3791 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
3792 | SourceLocation PointOfInstantiation = SourceLocation()); |
3793 | |
3794 | /// If this enumeration is an instantiation of a member enumeration of |
3795 | /// a class template specialization, retrieves the member specialization |
3796 | /// information. |
3797 | MemberSpecializationInfo *getMemberSpecializationInfo() const { |
3798 | return SpecializationInfo; |
3799 | } |
3800 | |
3801 | /// Specify that this enumeration is an instantiation of the |
3802 | /// member enumeration ED. |
3803 | void setInstantiationOfMemberEnum(EnumDecl *ED, |
3804 | TemplateSpecializationKind TSK) { |
3805 | setInstantiationOfMemberEnum(getASTContext(), ED, TSK); |
3806 | } |
3807 | |
3808 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3809 | static bool classofKind(Kind K) { return K == Enum; } |
3810 | }; |
3811 | |
3812 | /// Represents a struct/union/class. For example: |
3813 | /// struct X; // Forward declaration, no "body". |
3814 | /// union Y { int A, B; }; // Has body with members A and B (FieldDecls). |
3815 | /// This decl will be marked invalid if *any* members are invalid. |
3816 | class RecordDecl : public TagDecl { |
3817 | // This class stores some data in DeclContext::RecordDeclBits |
3818 | // to save some space. Use the provided accessors to access it. |
3819 | public: |
3820 | friend class DeclContext; |
3821 | /// Enum that represents the different ways arguments are passed to and |
3822 | /// returned from function calls. This takes into account the target-specific |
3823 | /// and version-specific rules along with the rules determined by the |
3824 | /// language. |
3825 | enum ArgPassingKind : unsigned { |
3826 | /// The argument of this type can be passed directly in registers. |
3827 | APK_CanPassInRegs, |
3828 | |
3829 | /// The argument of this type cannot be passed directly in registers. |
3830 | /// Records containing this type as a subobject are not forced to be passed |
3831 | /// indirectly. This value is used only in C++. This value is required by |
3832 | /// C++ because, in uncommon situations, it is possible for a class to have |
3833 | /// only trivial copy/move constructors even when one of its subobjects has |
3834 | /// a non-trivial copy/move constructor (if e.g. the corresponding copy/move |
3835 | /// constructor in the derived class is deleted). |
3836 | APK_CannotPassInRegs, |
3837 | |
3838 | /// The argument of this type cannot be passed directly in registers. |
3839 | /// Records containing this type as a subobject are forced to be passed |
3840 | /// indirectly. |
3841 | APK_CanNeverPassInRegs |
3842 | }; |
3843 | |
3844 | protected: |
3845 | RecordDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
3846 | SourceLocation StartLoc, SourceLocation IdLoc, |
3847 | IdentifierInfo *Id, RecordDecl *PrevDecl); |
3848 | |
3849 | public: |
3850 | static RecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
3851 | SourceLocation StartLoc, SourceLocation IdLoc, |
3852 | IdentifierInfo *Id, RecordDecl* PrevDecl = nullptr); |
3853 | static RecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID); |
3854 | |
3855 | RecordDecl *getPreviousDecl() { |
3856 | return cast_or_null<RecordDecl>( |
3857 | static_cast<TagDecl *>(this)->getPreviousDecl()); |
3858 | } |
3859 | const RecordDecl *getPreviousDecl() const { |
3860 | return const_cast<RecordDecl*>(this)->getPreviousDecl(); |
3861 | } |
3862 | |
3863 | RecordDecl *getMostRecentDecl() { |
3864 | return cast<RecordDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl()); |
3865 | } |
3866 | const RecordDecl *getMostRecentDecl() const { |
3867 | return const_cast<RecordDecl*>(this)->getMostRecentDecl(); |
3868 | } |
3869 | |
3870 | bool hasFlexibleArrayMember() const { |
3871 | return RecordDeclBits.HasFlexibleArrayMember; |
3872 | } |
3873 | |
3874 | void setHasFlexibleArrayMember(bool V) { |
3875 | RecordDeclBits.HasFlexibleArrayMember = V; |
3876 | } |
3877 | |
3878 | /// Whether this is an anonymous struct or union. To be an anonymous |
3879 | /// struct or union, it must have been declared without a name and |
3880 | /// there must be no objects of this type declared, e.g., |
3881 | /// @code |
3882 | /// union { int i; float f; }; |
3883 | /// @endcode |
3884 | /// is an anonymous union but neither of the following are: |
3885 | /// @code |
3886 | /// union X { int i; float f; }; |
3887 | /// union { int i; float f; } obj; |
3888 | /// @endcode |
3889 | bool isAnonymousStructOrUnion() const { |
3890 | return RecordDeclBits.AnonymousStructOrUnion; |
3891 | } |
3892 | |
3893 | void setAnonymousStructOrUnion(bool Anon) { |
3894 | RecordDeclBits.AnonymousStructOrUnion = Anon; |
3895 | } |
3896 | |
3897 | bool hasObjectMember() const { return RecordDeclBits.HasObjectMember; } |
3898 | void setHasObjectMember(bool val) { RecordDeclBits.HasObjectMember = val; } |
3899 | |
3900 | bool hasVolatileMember() const { return RecordDeclBits.HasVolatileMember; } |
3901 | |
3902 | void setHasVolatileMember(bool val) { |
3903 | RecordDeclBits.HasVolatileMember = val; |
3904 | } |
3905 | |
3906 | bool hasLoadedFieldsFromExternalStorage() const { |
3907 | return RecordDeclBits.LoadedFieldsFromExternalStorage; |
3908 | } |
3909 | |
3910 | void setHasLoadedFieldsFromExternalStorage(bool val) const { |
3911 | RecordDeclBits.LoadedFieldsFromExternalStorage = val; |
3912 | } |
3913 | |
3914 | /// Functions to query basic properties of non-trivial C structs. |
3915 | bool isNonTrivialToPrimitiveDefaultInitialize() const { |
3916 | return RecordDeclBits.NonTrivialToPrimitiveDefaultInitialize; |
3917 | } |
3918 | |
3919 | void setNonTrivialToPrimitiveDefaultInitialize(bool V) { |
3920 | RecordDeclBits.NonTrivialToPrimitiveDefaultInitialize = V; |
3921 | } |
3922 | |
3923 | bool isNonTrivialToPrimitiveCopy() const { |
3924 | return RecordDeclBits.NonTrivialToPrimitiveCopy; |
3925 | } |
3926 | |
3927 | void setNonTrivialToPrimitiveCopy(bool V) { |
3928 | RecordDeclBits.NonTrivialToPrimitiveCopy = V; |
3929 | } |
3930 | |
3931 | bool isNonTrivialToPrimitiveDestroy() const { |
3932 | return RecordDeclBits.NonTrivialToPrimitiveDestroy; |
3933 | } |
3934 | |
3935 | void setNonTrivialToPrimitiveDestroy(bool V) { |
3936 | RecordDeclBits.NonTrivialToPrimitiveDestroy = V; |
3937 | } |
3938 | |
3939 | bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { |
3940 | return RecordDeclBits.HasNonTrivialToPrimitiveDefaultInitializeCUnion; |
3941 | } |
3942 | |
3943 | void setHasNonTrivialToPrimitiveDefaultInitializeCUnion(bool V) { |
3944 | RecordDeclBits.HasNonTrivialToPrimitiveDefaultInitializeCUnion = V; |
3945 | } |
3946 | |
3947 | bool hasNonTrivialToPrimitiveDestructCUnion() const { |
3948 | return RecordDeclBits.HasNonTrivialToPrimitiveDestructCUnion; |
3949 | } |
3950 | |
3951 | void setHasNonTrivialToPrimitiveDestructCUnion(bool V) { |
3952 | RecordDeclBits.HasNonTrivialToPrimitiveDestructCUnion = V; |
3953 | } |
3954 | |
3955 | bool hasNonTrivialToPrimitiveCopyCUnion() const { |
3956 | return RecordDeclBits.HasNonTrivialToPrimitiveCopyCUnion; |
3957 | } |
3958 | |
3959 | void setHasNonTrivialToPrimitiveCopyCUnion(bool V) { |
3960 | RecordDeclBits.HasNonTrivialToPrimitiveCopyCUnion = V; |
3961 | } |
3962 | |
3963 | /// Determine whether this class can be passed in registers. In C++ mode, |
3964 | /// it must have at least one trivial, non-deleted copy or move constructor. |
3965 | /// FIXME: This should be set as part of completeDefinition. |
3966 | bool canPassInRegisters() const { |
3967 | return getArgPassingRestrictions() == APK_CanPassInRegs; |
3968 | } |
3969 | |
3970 | ArgPassingKind getArgPassingRestrictions() const { |
3971 | return static_cast<ArgPassingKind>(RecordDeclBits.ArgPassingRestrictions); |
3972 | } |
3973 | |
3974 | void setArgPassingRestrictions(ArgPassingKind Kind) { |
3975 | RecordDeclBits.ArgPassingRestrictions = Kind; |
3976 | } |
3977 | |
3978 | bool isParamDestroyedInCallee() const { |
3979 | return RecordDeclBits.ParamDestroyedInCallee; |
3980 | } |
3981 | |
3982 | void setParamDestroyedInCallee(bool V) { |
3983 | RecordDeclBits.ParamDestroyedInCallee = V; |
3984 | } |
3985 | |
3986 | /// Determines whether this declaration represents the |
3987 | /// injected class name. |
3988 | /// |
3989 | /// The injected class name in C++ is the name of the class that |
3990 | /// appears inside the class itself. For example: |
3991 | /// |
3992 | /// \code |
3993 | /// struct C { |
3994 | /// // C is implicitly declared here as a synonym for the class name. |
3995 | /// }; |
3996 | /// |
3997 | /// C::C c; // same as "C c;" |
3998 | /// \endcode |
3999 | bool isInjectedClassName() const; |
4000 | |
4001 | /// Determine whether this record is a class describing a lambda |
4002 | /// function object. |
4003 | bool isLambda() const; |
4004 | |
4005 | /// Determine whether this record is a record for captured variables in |
4006 | /// CapturedStmt construct. |
4007 | bool isCapturedRecord() const; |
4008 | |
4009 | /// Mark the record as a record for captured variables in CapturedStmt |
4010 | /// construct. |
4011 | void setCapturedRecord(); |
4012 | |
4013 | /// Returns the RecordDecl that actually defines |
4014 | /// this struct/union/class. When determining whether or not a |
4015 | /// struct/union/class is completely defined, one should use this |
4016 | /// method as opposed to 'isCompleteDefinition'. |
4017 | /// 'isCompleteDefinition' indicates whether or not a specific |
4018 | /// RecordDecl is a completed definition, not whether or not the |
4019 | /// record type is defined. This method returns NULL if there is |
4020 | /// no RecordDecl that defines the struct/union/tag. |
4021 | RecordDecl *getDefinition() const { |
4022 | return cast_or_null<RecordDecl>(TagDecl::getDefinition()); |
4023 | } |
4024 | |
4025 | /// Returns whether this record is a union, or contains (at any nesting level) |
4026 | /// a union member. This is used by CMSE to warn about possible information |
4027 | /// leaks. |
4028 | bool isOrContainsUnion() const; |
4029 | |
4030 | // Iterator access to field members. The field iterator only visits |
4031 | // the non-static data members of this class, ignoring any static |
4032 | // data members, functions, constructors, destructors, etc. |
4033 | using field_iterator = specific_decl_iterator<FieldDecl>; |
4034 | using field_range = llvm::iterator_range<specific_decl_iterator<FieldDecl>>; |
4035 | |
4036 | field_range fields() const { return field_range(field_begin(), field_end()); } |
4037 | field_iterator field_begin() const; |
4038 | |
4039 | field_iterator field_end() const { |
4040 | return field_iterator(decl_iterator()); |
4041 | } |
4042 | |
4043 | // Whether there are any fields (non-static data members) in this record. |
4044 | bool field_empty() const { |
4045 | return field_begin() == field_end(); |
4046 | } |
4047 | |
4048 | /// Note that the definition of this type is now complete. |
4049 | virtual void completeDefinition(); |
4050 | |
4051 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4052 | static bool classofKind(Kind K) { |
4053 | return K >= firstRecord && K <= lastRecord; |
4054 | } |
4055 | |
4056 | /// Get whether or not this is an ms_struct which can |
4057 | /// be turned on with an attribute, pragma, or -mms-bitfields |
4058 | /// commandline option. |
4059 | bool isMsStruct(const ASTContext &C) const; |
4060 | |
4061 | /// Whether we are allowed to insert extra padding between fields. |
4062 | /// These padding are added to help AddressSanitizer detect |
4063 | /// intra-object-overflow bugs. |
4064 | bool mayInsertExtraPadding(bool EmitRemark = false) const; |
4065 | |
4066 | /// Finds the first data member which has a name. |
4067 | /// nullptr is returned if no named data member exists. |
4068 | const FieldDecl *findFirstNamedDataMember() const; |
4069 | |
4070 | private: |
4071 | /// Deserialize just the fields. |
4072 | void LoadFieldsFromExternalStorage() const; |
4073 | }; |
4074 | |
4075 | class FileScopeAsmDecl : public Decl { |
4076 | StringLiteral *AsmString; |
4077 | SourceLocation RParenLoc; |
4078 | |
4079 | FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring, |
4080 | SourceLocation StartL, SourceLocation EndL) |
4081 | : Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {} |
4082 | |
4083 | virtual void anchor(); |
4084 | |
4085 | public: |
4086 | static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC, |
4087 | StringLiteral *Str, SourceLocation AsmLoc, |
4088 | SourceLocation RParenLoc); |
4089 | |
4090 | static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4091 | |
4092 | SourceLocation getAsmLoc() const { return getLocation(); } |
4093 | SourceLocation getRParenLoc() const { return RParenLoc; } |
4094 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
4095 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
4096 | return SourceRange(getAsmLoc(), getRParenLoc()); |
4097 | } |
4098 | |
4099 | const StringLiteral *getAsmString() const { return AsmString; } |
4100 | StringLiteral *getAsmString() { return AsmString; } |
4101 | void setAsmString(StringLiteral *Asm) { AsmString = Asm; } |
4102 | |
4103 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4104 | static bool classofKind(Kind K) { return K == FileScopeAsm; } |
4105 | }; |
4106 | |
4107 | /// Represents a block literal declaration, which is like an |
4108 | /// unnamed FunctionDecl. For example: |
4109 | /// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } |
4110 | class BlockDecl : public Decl, public DeclContext { |
4111 | // This class stores some data in DeclContext::BlockDeclBits |
4112 | // to save some space. Use the provided accessors to access it. |
4113 | public: |
4114 | /// A class which contains all the information about a particular |
4115 | /// captured value. |
4116 | class Capture { |
4117 | enum { |
4118 | flag_isByRef = 0x1, |
4119 | flag_isNested = 0x2 |
4120 | }; |
4121 | |
4122 | /// The variable being captured. |
4123 | llvm::PointerIntPair<VarDecl*, 2> VariableAndFlags; |
4124 | |
4125 | /// The copy expression, expressed in terms of a DeclRef (or |
4126 | /// BlockDeclRef) to the captured variable. Only required if the |
4127 | /// variable has a C++ class type. |
4128 | Expr *CopyExpr; |
4129 | |
4130 | public: |
4131 | Capture(VarDecl *variable, bool byRef, bool nested, Expr *copy) |
4132 | : VariableAndFlags(variable, |
4133 | (byRef ? flag_isByRef : 0) | (nested ? flag_isNested : 0)), |
4134 | CopyExpr(copy) {} |
4135 | |
4136 | /// The variable being captured. |
4137 | VarDecl *getVariable() const { return VariableAndFlags.getPointer(); } |
4138 | |
4139 | /// Whether this is a "by ref" capture, i.e. a capture of a __block |
4140 | /// variable. |
4141 | bool isByRef() const { return VariableAndFlags.getInt() & flag_isByRef; } |
4142 | |
4143 | bool isEscapingByref() const { |
4144 | return getVariable()->isEscapingByref(); |
4145 | } |
4146 | |
4147 | bool isNonEscapingByref() const { |
4148 | return getVariable()->isNonEscapingByref(); |
4149 | } |
4150 | |
4151 | /// Whether this is a nested capture, i.e. the variable captured |
4152 | /// is not from outside the immediately enclosing function/block. |
4153 | bool isNested() const { return VariableAndFlags.getInt() & flag_isNested; } |
4154 | |
4155 | bool hasCopyExpr() const { return CopyExpr != nullptr; } |
4156 | Expr *getCopyExpr() const { return CopyExpr; } |
4157 | void setCopyExpr(Expr *e) { CopyExpr = e; } |
4158 | }; |
4159 | |
4160 | private: |
4161 | /// A new[]'d array of pointers to ParmVarDecls for the formal |
4162 | /// parameters of this function. This is null if a prototype or if there are |
4163 | /// no formals. |
4164 | ParmVarDecl **ParamInfo = nullptr; |
4165 | unsigned NumParams = 0; |
4166 | |
4167 | Stmt *Body = nullptr; |
4168 | TypeSourceInfo *SignatureAsWritten = nullptr; |
4169 | |
4170 | const Capture *Captures = nullptr; |
4171 | unsigned NumCaptures = 0; |
4172 | |
4173 | unsigned ManglingNumber = 0; |
4174 | Decl *ManglingContextDecl = nullptr; |
4175 | |
4176 | protected: |
4177 | BlockDecl(DeclContext *DC, SourceLocation CaretLoc); |
4178 | |
4179 | public: |
4180 | static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L); |
4181 | static BlockDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4182 | |
4183 | SourceLocation getCaretLocation() const { return getLocation(); } |
4184 | |
4185 | bool isVariadic() const { return BlockDeclBits.IsVariadic; } |
4186 | void setIsVariadic(bool value) { BlockDeclBits.IsVariadic = value; } |
4187 | |
4188 | CompoundStmt *getCompoundBody() const { return (CompoundStmt*) Body; } |
4189 | Stmt *getBody() const override { return (Stmt*) Body; } |
4190 | void setBody(CompoundStmt *B) { Body = (Stmt*) B; } |
4191 | |
4192 | void setSignatureAsWritten(TypeSourceInfo *Sig) { SignatureAsWritten = Sig; } |
4193 | TypeSourceInfo *getSignatureAsWritten() const { return SignatureAsWritten; } |
4194 | |
4195 | // ArrayRef access to formal parameters. |
4196 | ArrayRef<ParmVarDecl *> parameters() const { |
4197 | return {ParamInfo, getNumParams()}; |
4198 | } |
4199 | MutableArrayRef<ParmVarDecl *> parameters() { |
4200 | return {ParamInfo, getNumParams()}; |
4201 | } |
4202 | |
4203 | // Iterator access to formal parameters. |
4204 | using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator; |
4205 | using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator; |
4206 | |
4207 | bool param_empty() const { return parameters().empty(); } |
4208 | param_iterator param_begin() { return parameters().begin(); } |
4209 | param_iterator param_end() { return parameters().end(); } |
4210 | param_const_iterator param_begin() const { return parameters().begin(); } |
4211 | param_const_iterator param_end() const { return parameters().end(); } |
4212 | size_t param_size() const { return parameters().size(); } |
4213 | |
4214 | unsigned getNumParams() const { return NumParams; } |
4215 | |
4216 | const ParmVarDecl *getParamDecl(unsigned i) const { |
4217 | assert(i < getNumParams() && "Illegal param #")((i < getNumParams() && "Illegal param #") ? static_cast <void> (0) : __assert_fail ("i < getNumParams() && \"Illegal param #\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4217, __PRETTY_FUNCTION__)); |
4218 | return ParamInfo[i]; |
4219 | } |
4220 | ParmVarDecl *getParamDecl(unsigned i) { |
4221 | assert(i < getNumParams() && "Illegal param #")((i < getNumParams() && "Illegal param #") ? static_cast <void> (0) : __assert_fail ("i < getNumParams() && \"Illegal param #\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4221, __PRETTY_FUNCTION__)); |
4222 | return ParamInfo[i]; |
4223 | } |
4224 | |
4225 | void setParams(ArrayRef<ParmVarDecl *> NewParamInfo); |
4226 | |
4227 | /// True if this block (or its nested blocks) captures |
4228 | /// anything of local storage from its enclosing scopes. |
4229 | bool hasCaptures() const { return NumCaptures || capturesCXXThis(); } |
4230 | |
4231 | /// Returns the number of captured variables. |
4232 | /// Does not include an entry for 'this'. |
4233 | unsigned getNumCaptures() const { return NumCaptures; } |
4234 | |
4235 | using capture_const_iterator = ArrayRef<Capture>::const_iterator; |
4236 | |
4237 | ArrayRef<Capture> captures() const { return {Captures, NumCaptures}; } |
4238 | |
4239 | capture_const_iterator capture_begin() const { return captures().begin(); } |
4240 | capture_const_iterator capture_end() const { return captures().end(); } |
4241 | |
4242 | bool capturesCXXThis() const { return BlockDeclBits.CapturesCXXThis; } |
4243 | void setCapturesCXXThis(bool B = true) { BlockDeclBits.CapturesCXXThis = B; } |
4244 | |
4245 | bool blockMissingReturnType() const { |
4246 | return BlockDeclBits.BlockMissingReturnType; |
4247 | } |
4248 | |
4249 | void setBlockMissingReturnType(bool val = true) { |
4250 | BlockDeclBits.BlockMissingReturnType = val; |
4251 | } |
4252 | |
4253 | bool isConversionFromLambda() const { |
4254 | return BlockDeclBits.IsConversionFromLambda; |
4255 | } |
4256 | |
4257 | void setIsConversionFromLambda(bool val = true) { |
4258 | BlockDeclBits.IsConversionFromLambda = val; |
4259 | } |
4260 | |
4261 | bool doesNotEscape() const { return BlockDeclBits.DoesNotEscape; } |
4262 | void setDoesNotEscape(bool B = true) { BlockDeclBits.DoesNotEscape = B; } |
4263 | |
4264 | bool canAvoidCopyToHeap() const { |
4265 | return BlockDeclBits.CanAvoidCopyToHeap; |
4266 | } |
4267 | void setCanAvoidCopyToHeap(bool B = true) { |
4268 | BlockDeclBits.CanAvoidCopyToHeap = B; |
4269 | } |
4270 | |
4271 | bool capturesVariable(const VarDecl *var) const; |
4272 | |
4273 | void setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
4274 | bool CapturesCXXThis); |
4275 | |
4276 | unsigned getBlockManglingNumber() const { return ManglingNumber; } |
4277 | |
4278 | Decl *getBlockManglingContextDecl() const { return ManglingContextDecl; } |
4279 | |
4280 | void setBlockMangling(unsigned Number, Decl *Ctx) { |
4281 | ManglingNumber = Number; |
4282 | ManglingContextDecl = Ctx; |
4283 | } |
4284 | |
4285 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
4286 | |
4287 | // Implement isa/cast/dyncast/etc. |
4288 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4289 | static bool classofKind(Kind K) { return K == Block; } |
4290 | static DeclContext *castToDeclContext(const BlockDecl *D) { |
4291 | return static_cast<DeclContext *>(const_cast<BlockDecl*>(D)); |
4292 | } |
4293 | static BlockDecl *castFromDeclContext(const DeclContext *DC) { |
4294 | return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC)); |
4295 | } |
4296 | }; |
4297 | |
4298 | /// Represents the body of a CapturedStmt, and serves as its DeclContext. |
4299 | class CapturedDecl final |
4300 | : public Decl, |
4301 | public DeclContext, |
4302 | private llvm::TrailingObjects<CapturedDecl, ImplicitParamDecl *> { |
4303 | protected: |
4304 | size_t numTrailingObjects(OverloadToken<ImplicitParamDecl>) { |
4305 | return NumParams; |
4306 | } |
4307 | |
4308 | private: |
4309 | /// The number of parameters to the outlined function. |
4310 | unsigned NumParams; |
4311 | |
4312 | /// The position of context parameter in list of parameters. |
4313 | unsigned ContextParam; |
4314 | |
4315 | /// The body of the outlined function. |
4316 | llvm::PointerIntPair<Stmt *, 1, bool> BodyAndNothrow; |
4317 | |
4318 | explicit CapturedDecl(DeclContext *DC, unsigned NumParams); |
4319 | |
4320 | ImplicitParamDecl *const *getParams() const { |
4321 | return getTrailingObjects<ImplicitParamDecl *>(); |
4322 | } |
4323 | |
4324 | ImplicitParamDecl **getParams() { |
4325 | return getTrailingObjects<ImplicitParamDecl *>(); |
4326 | } |
4327 | |
4328 | public: |
4329 | friend class ASTDeclReader; |
4330 | friend class ASTDeclWriter; |
4331 | friend TrailingObjects; |
4332 | |
4333 | static CapturedDecl *Create(ASTContext &C, DeclContext *DC, |
4334 | unsigned NumParams); |
4335 | static CapturedDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
4336 | unsigned NumParams); |
4337 | |
4338 | Stmt *getBody() const override; |
4339 | void setBody(Stmt *B); |
4340 | |
4341 | bool isNothrow() const; |
4342 | void setNothrow(bool Nothrow = true); |
4343 | |
4344 | unsigned getNumParams() const { return NumParams; } |
4345 | |
4346 | ImplicitParamDecl *getParam(unsigned i) const { |
4347 | assert(i < NumParams)((i < NumParams) ? static_cast<void> (0) : __assert_fail ("i < NumParams", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4347, __PRETTY_FUNCTION__)); |
4348 | return getParams()[i]; |
4349 | } |
4350 | void setParam(unsigned i, ImplicitParamDecl *P) { |
4351 | assert(i < NumParams)((i < NumParams) ? static_cast<void> (0) : __assert_fail ("i < NumParams", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4351, __PRETTY_FUNCTION__)); |
4352 | getParams()[i] = P; |
4353 | } |
4354 | |
4355 | // ArrayRef interface to parameters. |
4356 | ArrayRef<ImplicitParamDecl *> parameters() const { |
4357 | return {getParams(), getNumParams()}; |
4358 | } |
4359 | MutableArrayRef<ImplicitParamDecl *> parameters() { |
4360 | return {getParams(), getNumParams()}; |
4361 | } |
4362 | |
4363 | /// Retrieve the parameter containing captured variables. |
4364 | ImplicitParamDecl *getContextParam() const { |
4365 | assert(ContextParam < NumParams)((ContextParam < NumParams) ? static_cast<void> (0) : __assert_fail ("ContextParam < NumParams", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4365, __PRETTY_FUNCTION__)); |
4366 | return getParam(ContextParam); |
4367 | } |
4368 | void setContextParam(unsigned i, ImplicitParamDecl *P) { |
4369 | assert(i < NumParams)((i < NumParams) ? static_cast<void> (0) : __assert_fail ("i < NumParams", "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4369, __PRETTY_FUNCTION__)); |
4370 | ContextParam = i; |
4371 | setParam(i, P); |
4372 | } |
4373 | unsigned getContextParamPosition() const { return ContextParam; } |
4374 | |
4375 | using param_iterator = ImplicitParamDecl *const *; |
4376 | using param_range = llvm::iterator_range<param_iterator>; |
4377 | |
4378 | /// Retrieve an iterator pointing to the first parameter decl. |
4379 | param_iterator param_begin() const { return getParams(); } |
4380 | /// Retrieve an iterator one past the last parameter decl. |
4381 | param_iterator param_end() const { return getParams() + NumParams; } |
4382 | |
4383 | // Implement isa/cast/dyncast/etc. |
4384 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4385 | static bool classofKind(Kind K) { return K == Captured; } |
4386 | static DeclContext *castToDeclContext(const CapturedDecl *D) { |
4387 | return static_cast<DeclContext *>(const_cast<CapturedDecl *>(D)); |
4388 | } |
4389 | static CapturedDecl *castFromDeclContext(const DeclContext *DC) { |
4390 | return static_cast<CapturedDecl *>(const_cast<DeclContext *>(DC)); |
4391 | } |
4392 | }; |
4393 | |
4394 | /// Describes a module import declaration, which makes the contents |
4395 | /// of the named module visible in the current translation unit. |
4396 | /// |
4397 | /// An import declaration imports the named module (or submodule). For example: |
4398 | /// \code |
4399 | /// @import std.vector; |
4400 | /// \endcode |
4401 | /// |
4402 | /// Import declarations can also be implicitly generated from |
4403 | /// \#include/\#import directives. |
4404 | class ImportDecl final : public Decl, |
4405 | llvm::TrailingObjects<ImportDecl, SourceLocation> { |
4406 | friend class ASTContext; |
4407 | friend class ASTDeclReader; |
4408 | friend class ASTReader; |
4409 | friend TrailingObjects; |
4410 | |
4411 | /// The imported module. |
4412 | Module *ImportedModule = nullptr; |
4413 | |
4414 | /// The next import in the list of imports local to the translation |
4415 | /// unit being parsed (not loaded from an AST file). |
4416 | /// |
4417 | /// Includes a bit that indicates whether we have source-location information |
4418 | /// for each identifier in the module name. |
4419 | /// |
4420 | /// When the bit is false, we only have a single source location for the |
4421 | /// end of the import declaration. |
4422 | llvm::PointerIntPair<ImportDecl *, 1, bool> NextLocalImportAndComplete; |
4423 | |
4424 | ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, |
4425 | ArrayRef<SourceLocation> IdentifierLocs); |
4426 | |
4427 | ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, |
4428 | SourceLocation EndLoc); |
4429 | |
4430 | ImportDecl(EmptyShell Empty) : Decl(Import, Empty) {} |
4431 | |
4432 | bool isImportComplete() const { return NextLocalImportAndComplete.getInt(); } |
4433 | |
4434 | void setImportComplete(bool C) { NextLocalImportAndComplete.setInt(C); } |
4435 | |
4436 | /// The next import in the list of imports local to the translation |
4437 | /// unit being parsed (not loaded from an AST file). |
4438 | ImportDecl *getNextLocalImport() const { |
4439 | return NextLocalImportAndComplete.getPointer(); |
4440 | } |
4441 | |
4442 | void setNextLocalImport(ImportDecl *Import) { |
4443 | NextLocalImportAndComplete.setPointer(Import); |
4444 | } |
4445 | |
4446 | public: |
4447 | /// Create a new module import declaration. |
4448 | static ImportDecl *Create(ASTContext &C, DeclContext *DC, |
4449 | SourceLocation StartLoc, Module *Imported, |
4450 | ArrayRef<SourceLocation> IdentifierLocs); |
4451 | |
4452 | /// Create a new module import declaration for an implicitly-generated |
4453 | /// import. |
4454 | static ImportDecl *CreateImplicit(ASTContext &C, DeclContext *DC, |
4455 | SourceLocation StartLoc, Module *Imported, |
4456 | SourceLocation EndLoc); |
4457 | |
4458 | /// Create a new, deserialized module import declaration. |
4459 | static ImportDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
4460 | unsigned NumLocations); |
4461 | |
4462 | /// Retrieve the module that was imported by the import declaration. |
4463 | Module *getImportedModule() const { return ImportedModule; } |
4464 | |
4465 | /// Retrieves the locations of each of the identifiers that make up |
4466 | /// the complete module name in the import declaration. |
4467 | /// |
4468 | /// This will return an empty array if the locations of the individual |
4469 | /// identifiers aren't available. |
4470 | ArrayRef<SourceLocation> getIdentifierLocs() const; |
4471 | |
4472 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)); |
4473 | |
4474 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4475 | static bool classofKind(Kind K) { return K == Import; } |
4476 | }; |
4477 | |
4478 | /// Represents a C++ Modules TS module export declaration. |
4479 | /// |
4480 | /// For example: |
4481 | /// \code |
4482 | /// export void foo(); |
4483 | /// \endcode |
4484 | class ExportDecl final : public Decl, public DeclContext { |
4485 | virtual void anchor(); |
4486 | |
4487 | private: |
4488 | friend class ASTDeclReader; |
4489 | |
4490 | /// The source location for the right brace (if valid). |
4491 | SourceLocation RBraceLoc; |
4492 | |
4493 | ExportDecl(DeclContext *DC, SourceLocation ExportLoc) |
4494 | : Decl(Export, DC, ExportLoc), DeclContext(Export), |
4495 | RBraceLoc(SourceLocation()) {} |
4496 | |
4497 | public: |
4498 | static ExportDecl *Create(ASTContext &C, DeclContext *DC, |
4499 | SourceLocation ExportLoc); |
4500 | static ExportDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4501 | |
4502 | SourceLocation getExportLoc() const { return getLocation(); } |
4503 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
4504 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
4505 | |
4506 | bool hasBraces() const { return RBraceLoc.isValid(); } |
4507 | |
4508 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { |
4509 | if (hasBraces()) |
4510 | return RBraceLoc; |
4511 | // No braces: get the end location of the (only) declaration in context |
4512 | // (if present). |
4513 | return decls_empty() ? getLocation() : decls_begin()->getEndLoc(); |
4514 | } |
4515 | |
4516 | SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) { |
4517 | return SourceRange(getLocation(), getEndLoc()); |
4518 | } |
4519 | |
4520 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4521 | static bool classofKind(Kind K) { return K == Export; } |
4522 | static DeclContext *castToDeclContext(const ExportDecl *D) { |
4523 | return static_cast<DeclContext *>(const_cast<ExportDecl*>(D)); |
4524 | } |
4525 | static ExportDecl *castFromDeclContext(const DeclContext *DC) { |
4526 | return static_cast<ExportDecl *>(const_cast<DeclContext*>(DC)); |
4527 | } |
4528 | }; |
4529 | |
4530 | /// Represents an empty-declaration. |
4531 | class EmptyDecl : public Decl { |
4532 | EmptyDecl(DeclContext *DC, SourceLocation L) : Decl(Empty, DC, L) {} |
4533 | |
4534 | virtual void anchor(); |
4535 | |
4536 | public: |
4537 | static EmptyDecl *Create(ASTContext &C, DeclContext *DC, |
4538 | SourceLocation L); |
4539 | static EmptyDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4540 | |
4541 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4542 | static bool classofKind(Kind K) { return K == Empty; } |
4543 | }; |
4544 | |
4545 | /// Insertion operator for diagnostics. This allows sending NamedDecl's |
4546 | /// into a diagnostic with <<. |
4547 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
4548 | const NamedDecl *ND) { |
4549 | PD.AddTaggedVal(reinterpret_cast<intptr_t>(ND), |
4550 | DiagnosticsEngine::ak_nameddecl); |
4551 | return PD; |
4552 | } |
4553 | |
4554 | template<typename decl_type> |
4555 | void Redeclarable<decl_type>::setPreviousDecl(decl_type *PrevDecl) { |
4556 | // Note: This routine is implemented here because we need both NamedDecl |
4557 | // and Redeclarable to be defined. |
4558 | assert(RedeclLink.isFirst() &&((RedeclLink.isFirst() && "setPreviousDecl on a decl already in a redeclaration chain" ) ? static_cast<void> (0) : __assert_fail ("RedeclLink.isFirst() && \"setPreviousDecl on a decl already in a redeclaration chain\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4559, __PRETTY_FUNCTION__)) |
4559 | "setPreviousDecl on a decl already in a redeclaration chain")((RedeclLink.isFirst() && "setPreviousDecl on a decl already in a redeclaration chain" ) ? static_cast<void> (0) : __assert_fail ("RedeclLink.isFirst() && \"setPreviousDecl on a decl already in a redeclaration chain\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4559, __PRETTY_FUNCTION__)); |
4560 | |
4561 | if (PrevDecl) { |
4562 | // Point to previous. Make sure that this is actually the most recent |
4563 | // redeclaration, or we can build invalid chains. If the most recent |
4564 | // redeclaration is invalid, it won't be PrevDecl, but we want it anyway. |
4565 | First = PrevDecl->getFirstDecl(); |
4566 | assert(First->RedeclLink.isFirst() && "Expected first")((First->RedeclLink.isFirst() && "Expected first") ? static_cast<void> (0) : __assert_fail ("First->RedeclLink.isFirst() && \"Expected first\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4566, __PRETTY_FUNCTION__)); |
4567 | decl_type *MostRecent = First->getNextRedeclaration(); |
4568 | RedeclLink = PreviousDeclLink(cast<decl_type>(MostRecent)); |
4569 | |
4570 | // If the declaration was previously visible, a redeclaration of it remains |
4571 | // visible even if it wouldn't be visible by itself. |
4572 | static_cast<decl_type*>(this)->IdentifierNamespace |= |
4573 | MostRecent->getIdentifierNamespace() & |
4574 | (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type); |
4575 | } else { |
4576 | // Make this first. |
4577 | First = static_cast<decl_type*>(this); |
4578 | } |
4579 | |
4580 | // First one will point to this one as latest. |
4581 | First->RedeclLink.setLatest(static_cast<decl_type*>(this)); |
4582 | |
4583 | assert(!isa<NamedDecl>(static_cast<decl_type*>(this)) ||((!isa<NamedDecl>(static_cast<decl_type*>(this)) || cast<NamedDecl>(static_cast<decl_type*>(this))-> isLinkageValid()) ? static_cast<void> (0) : __assert_fail ("!isa<NamedDecl>(static_cast<decl_type*>(this)) || cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4584, __PRETTY_FUNCTION__)) |
4584 | cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid())((!isa<NamedDecl>(static_cast<decl_type*>(this)) || cast<NamedDecl>(static_cast<decl_type*>(this))-> isLinkageValid()) ? static_cast<void> (0) : __assert_fail ("!isa<NamedDecl>(static_cast<decl_type*>(this)) || cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid()" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/clang/include/clang/AST/Decl.h" , 4584, __PRETTY_FUNCTION__)); |
4585 | } |
4586 | |
4587 | // Inline function definitions. |
4588 | |
4589 | /// Check if the given decl is complete. |
4590 | /// |
4591 | /// We use this function to break a cycle between the inline definitions in |
4592 | /// Type.h and Decl.h. |
4593 | inline bool IsEnumDeclComplete(EnumDecl *ED) { |
4594 | return ED->isComplete(); |
4595 | } |
4596 | |
4597 | /// Check if the given decl is scoped. |
4598 | /// |
4599 | /// We use this function to break a cycle between the inline definitions in |
4600 | /// Type.h and Decl.h. |
4601 | inline bool IsEnumDeclScoped(EnumDecl *ED) { |
4602 | return ED->isScoped(); |
4603 | } |
4604 | |
4605 | /// OpenMP variants are mangled early based on their OpenMP context selector. |
4606 | /// The new name looks likes this: |
4607 | /// <name> + OpenMPVariantManglingSeparatorStr + <mangled OpenMP context> |
4608 | static constexpr StringRef getOpenMPVariantManglingSeparatorStr() { |
4609 | return "$ompvariant"; |
4610 | } |
4611 | |
4612 | } // namespace clang |
4613 | |
4614 | #endif // LLVM_CLANG_AST_DECL_H |
1 | //===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===// |
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 file defines the PointerUnion class, which is a discriminated union of |
10 | // pointer types. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_ADT_POINTERUNION_H |
15 | #define LLVM_ADT_POINTERUNION_H |
16 | |
17 | #include "llvm/ADT/DenseMapInfo.h" |
18 | #include "llvm/ADT/PointerIntPair.h" |
19 | #include "llvm/Support/PointerLikeTypeTraits.h" |
20 | #include <cassert> |
21 | #include <cstddef> |
22 | #include <cstdint> |
23 | |
24 | namespace llvm { |
25 | |
26 | template <typename T> struct PointerUnionTypeSelectorReturn { |
27 | using Return = T; |
28 | }; |
29 | |
30 | /// Get a type based on whether two types are the same or not. |
31 | /// |
32 | /// For: |
33 | /// |
34 | /// \code |
35 | /// using Ret = typename PointerUnionTypeSelector<T1, T2, EQ, NE>::Return; |
36 | /// \endcode |
37 | /// |
38 | /// Ret will be EQ type if T1 is same as T2 or NE type otherwise. |
39 | template <typename T1, typename T2, typename RET_EQ, typename RET_NE> |
40 | struct PointerUnionTypeSelector { |
41 | using Return = typename PointerUnionTypeSelectorReturn<RET_NE>::Return; |
42 | }; |
43 | |
44 | template <typename T, typename RET_EQ, typename RET_NE> |
45 | struct PointerUnionTypeSelector<T, T, RET_EQ, RET_NE> { |
46 | using Return = typename PointerUnionTypeSelectorReturn<RET_EQ>::Return; |
47 | }; |
48 | |
49 | template <typename T1, typename T2, typename RET_EQ, typename RET_NE> |
50 | struct PointerUnionTypeSelectorReturn< |
51 | PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>> { |
52 | using Return = |
53 | typename PointerUnionTypeSelector<T1, T2, RET_EQ, RET_NE>::Return; |
54 | }; |
55 | |
56 | namespace pointer_union_detail { |
57 | /// Determine the number of bits required to store integers with values < n. |
58 | /// This is ceil(log2(n)). |
59 | constexpr int bitsRequired(unsigned n) { |
60 | return n > 1 ? 1 + bitsRequired((n + 1) / 2) : 0; |
61 | } |
62 | |
63 | template <typename... Ts> constexpr int lowBitsAvailable() { |
64 | return std::min<int>({PointerLikeTypeTraits<Ts>::NumLowBitsAvailable...}); |
65 | } |
66 | |
67 | /// Find the index of a type in a list of types. TypeIndex<T, Us...>::Index |
68 | /// is the index of T in Us, or sizeof...(Us) if T does not appear in the |
69 | /// list. |
70 | template <typename T, typename ...Us> struct TypeIndex; |
71 | template <typename T, typename ...Us> struct TypeIndex<T, T, Us...> { |
72 | static constexpr int Index = 0; |
73 | }; |
74 | template <typename T, typename U, typename... Us> |
75 | struct TypeIndex<T, U, Us...> { |
76 | static constexpr int Index = 1 + TypeIndex<T, Us...>::Index; |
77 | }; |
78 | template <typename T> struct TypeIndex<T> { |
79 | static constexpr int Index = 0; |
80 | }; |
81 | |
82 | /// Find the first type in a list of types. |
83 | template <typename T, typename...> struct GetFirstType { |
84 | using type = T; |
85 | }; |
86 | |
87 | /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion |
88 | /// for the template arguments. |
89 | template <typename ...PTs> class PointerUnionUIntTraits { |
90 | public: |
91 | static inline void *getAsVoidPointer(void *P) { return P; } |
92 | static inline void *getFromVoidPointer(void *P) { return P; } |
93 | static constexpr int NumLowBitsAvailable = lowBitsAvailable<PTs...>(); |
94 | }; |
95 | |
96 | template <typename Derived, typename ValTy, int I, typename ...Types> |
97 | class PointerUnionMembers; |
98 | |
99 | template <typename Derived, typename ValTy, int I> |
100 | class PointerUnionMembers<Derived, ValTy, I> { |
101 | protected: |
102 | ValTy Val; |
103 | PointerUnionMembers() = default; |
104 | PointerUnionMembers(ValTy Val) : Val(Val) {} |
105 | |
106 | friend struct PointerLikeTypeTraits<Derived>; |
107 | }; |
108 | |
109 | template <typename Derived, typename ValTy, int I, typename Type, |
110 | typename ...Types> |
111 | class PointerUnionMembers<Derived, ValTy, I, Type, Types...> |
112 | : public PointerUnionMembers<Derived, ValTy, I + 1, Types...> { |
113 | using Base = PointerUnionMembers<Derived, ValTy, I + 1, Types...>; |
114 | public: |
115 | using Base::Base; |
116 | PointerUnionMembers() = default; |
117 | PointerUnionMembers(Type V) |
118 | : Base(ValTy(const_cast<void *>( |
119 | PointerLikeTypeTraits<Type>::getAsVoidPointer(V)), |
120 | I)) {} |
121 | |
122 | using Base::operator=; |
123 | Derived &operator=(Type V) { |
124 | this->Val = ValTy( |
125 | const_cast<void *>(PointerLikeTypeTraits<Type>::getAsVoidPointer(V)), |
126 | I); |
127 | return static_cast<Derived &>(*this); |
128 | }; |
129 | }; |
130 | } |
131 | |
132 | /// A discriminated union of two or more pointer types, with the discriminator |
133 | /// in the low bit of the pointer. |
134 | /// |
135 | /// This implementation is extremely efficient in space due to leveraging the |
136 | /// low bits of the pointer, while exposing a natural and type-safe API. |
137 | /// |
138 | /// Common use patterns would be something like this: |
139 | /// PointerUnion<int*, float*> P; |
140 | /// P = (int*)0; |
141 | /// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0" |
142 | /// X = P.get<int*>(); // ok. |
143 | /// Y = P.get<float*>(); // runtime assertion failure. |
144 | /// Z = P.get<double*>(); // compile time failure. |
145 | /// P = (float*)0; |
146 | /// Y = P.get<float*>(); // ok. |
147 | /// X = P.get<int*>(); // runtime assertion failure. |
148 | template <typename... PTs> |
149 | class PointerUnion |
150 | : public pointer_union_detail::PointerUnionMembers< |
151 | PointerUnion<PTs...>, |
152 | PointerIntPair< |
153 | void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int, |
154 | pointer_union_detail::PointerUnionUIntTraits<PTs...>>, |
155 | 0, PTs...> { |
156 | // The first type is special because we want to directly cast a pointer to a |
157 | // default-initialized union to a pointer to the first type. But we don't |
158 | // want PointerUnion to be a 'template <typename First, typename ...Rest>' |
159 | // because it's much more convenient to have a name for the whole pack. So |
160 | // split off the first type here. |
161 | using First = typename pointer_union_detail::GetFirstType<PTs...>::type; |
162 | using Base = typename PointerUnion::PointerUnionMembers; |
163 | |
164 | public: |
165 | PointerUnion() = default; |
166 | |
167 | PointerUnion(std::nullptr_t) : PointerUnion() {} |
168 | using Base::Base; |
169 | |
170 | /// Test if the pointer held in the union is null, regardless of |
171 | /// which type it is. |
172 | bool isNull() const { return !this->Val.getPointer(); } |
173 | |
174 | explicit operator bool() const { return !isNull(); } |
175 | |
176 | /// Test if the Union currently holds the type matching T. |
177 | template <typename T> bool is() const { |
178 | constexpr int Index = pointer_union_detail::TypeIndex<T, PTs...>::Index; |
179 | static_assert(Index < sizeof...(PTs), |
180 | "PointerUnion::is<T> given type not in the union"); |
181 | return this->Val.getInt() == Index; |
182 | } |
183 | |
184 | /// Returns the value of the specified pointer type. |
185 | /// |
186 | /// If the specified pointer type is incorrect, assert. |
187 | template <typename T> T get() const { |
188 | assert(is<T>() && "Invalid accessor called")((is<T>() && "Invalid accessor called") ? static_cast <void> (0) : __assert_fail ("is<T>() && \"Invalid accessor called\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 188, __PRETTY_FUNCTION__)); |
189 | return PointerLikeTypeTraits<T>::getFromVoidPointer(this->Val.getPointer()); |
190 | } |
191 | |
192 | /// Returns the current pointer if it is of the specified pointer type, |
193 | /// otherwise returns null. |
194 | template <typename T> T dyn_cast() const { |
195 | if (is<T>()) |
196 | return get<T>(); |
197 | return T(); |
198 | } |
199 | |
200 | /// If the union is set to the first pointer type get an address pointing to |
201 | /// it. |
202 | First const *getAddrOfPtr1() const { |
203 | return const_cast<PointerUnion *>(this)->getAddrOfPtr1(); |
204 | } |
205 | |
206 | /// If the union is set to the first pointer type get an address pointing to |
207 | /// it. |
208 | First *getAddrOfPtr1() { |
209 | assert(is<First>() && "Val is not the first pointer")((is<First>() && "Val is not the first pointer" ) ? static_cast<void> (0) : __assert_fail ("is<First>() && \"Val is not the first pointer\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 209, __PRETTY_FUNCTION__)); |
210 | assert(((PointerLikeTypeTraits<First>::getAsVoidPointer(get< First>()) == this->Val.getPointer() && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? static_cast<void> (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 213, __PRETTY_FUNCTION__)) |
211 | PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) ==((PointerLikeTypeTraits<First>::getAsVoidPointer(get< First>()) == this->Val.getPointer() && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? static_cast<void> (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 213, __PRETTY_FUNCTION__)) |
212 | this->Val.getPointer() &&((PointerLikeTypeTraits<First>::getAsVoidPointer(get< First>()) == this->Val.getPointer() && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? static_cast<void> (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 213, __PRETTY_FUNCTION__)) |
213 | "Can't get the address because PointerLikeTypeTraits changes the ptr")((PointerLikeTypeTraits<First>::getAsVoidPointer(get< First>()) == this->Val.getPointer() && "Can't get the address because PointerLikeTypeTraits changes the ptr" ) ? static_cast<void> (0) : __assert_fail ("PointerLikeTypeTraits<First>::getAsVoidPointer(get<First>()) == this->Val.getPointer() && \"Can't get the address because PointerLikeTypeTraits changes the ptr\"" , "/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include/llvm/ADT/PointerUnion.h" , 213, __PRETTY_FUNCTION__)); |
214 | return const_cast<First *>( |
215 | reinterpret_cast<const First *>(this->Val.getAddrOfPointer())); |
216 | } |
217 | |
218 | /// Assignment from nullptr which just clears the union. |
219 | const PointerUnion &operator=(std::nullptr_t) { |
220 | this->Val.initWithPointer(nullptr); |
221 | return *this; |
222 | } |
223 | |
224 | /// Assignment from elements of the union. |
225 | using Base::operator=; |
226 | |
227 | void *getOpaqueValue() const { return this->Val.getOpaqueValue(); } |
228 | static inline PointerUnion getFromOpaqueValue(void *VP) { |
229 | PointerUnion V; |
230 | V.Val = decltype(V.Val)::getFromOpaqueValue(VP); |
231 | return V; |
232 | } |
233 | }; |
234 | |
235 | template <typename ...PTs> |
236 | bool operator==(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) { |
237 | return lhs.getOpaqueValue() == rhs.getOpaqueValue(); |
238 | } |
239 | |
240 | template <typename ...PTs> |
241 | bool operator!=(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) { |
242 | return lhs.getOpaqueValue() != rhs.getOpaqueValue(); |
243 | } |
244 | |
245 | template <typename ...PTs> |
246 | bool operator<(PointerUnion<PTs...> lhs, PointerUnion<PTs...> rhs) { |
247 | return lhs.getOpaqueValue() < rhs.getOpaqueValue(); |
248 | } |
249 | |
250 | // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has |
251 | // # low bits available = min(PT1bits,PT2bits)-1. |
252 | template <typename ...PTs> |
253 | struct PointerLikeTypeTraits<PointerUnion<PTs...>> { |
254 | static inline void *getAsVoidPointer(const PointerUnion<PTs...> &P) { |
255 | return P.getOpaqueValue(); |
256 | } |
257 | |
258 | static inline PointerUnion<PTs...> getFromVoidPointer(void *P) { |
259 | return PointerUnion<PTs...>::getFromOpaqueValue(P); |
260 | } |
261 | |
262 | // The number of bits available are the min of the pointer types minus the |
263 | // bits needed for the discriminator. |
264 | static constexpr int NumLowBitsAvailable = PointerLikeTypeTraits<decltype( |
265 | PointerUnion<PTs...>::Val)>::NumLowBitsAvailable; |
266 | }; |
267 | |
268 | // Teach DenseMap how to use PointerUnions as keys. |
269 | template <typename ...PTs> struct DenseMapInfo<PointerUnion<PTs...>> { |
270 | using Union = PointerUnion<PTs...>; |
271 | using FirstInfo = |
272 | DenseMapInfo<typename pointer_union_detail::GetFirstType<PTs...>::type>; |
273 | |
274 | static inline Union getEmptyKey() { return Union(FirstInfo::getEmptyKey()); } |
275 | |
276 | static inline Union getTombstoneKey() { |
277 | return Union(FirstInfo::getTombstoneKey()); |
278 | } |
279 | |
280 | static unsigned getHashValue(const Union &UnionVal) { |
281 | intptr_t key = (intptr_t)UnionVal.getOpaqueValue(); |
282 | return DenseMapInfo<intptr_t>::getHashValue(key); |
283 | } |
284 | |
285 | static bool isEqual(const Union &LHS, const Union &RHS) { |
286 | return LHS == RHS; |
287 | } |
288 | }; |
289 | |
290 | } // end namespace llvm |
291 | |
292 | #endif // LLVM_ADT_POINTERUNION_H |