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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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~++20210405022414+5f57793c4fe4/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 | |||||