File: | clang/lib/AST/ASTImporter.cpp |
Warning: | line 3446, column 53 1st function call argument is an uninitialized value |
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 = dyn_cast<CXXRecordDecl>(D)) { | ||||
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
| ||||
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
| ||||
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
| ||||
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 |