File: | build/source/clang/include/clang/AST/Expr.h |
Warning: | line 4238, column 5 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file defines the ASTImporter class which imports AST nodes from one | |||
10 | // context into another context. | |||
11 | // | |||
12 | //===----------------------------------------------------------------------===// | |||
13 | ||||
14 | #include "clang/AST/ASTImporter.h" | |||
15 | #include "clang/AST/ASTContext.h" | |||
16 | #include "clang/AST/ASTDiagnostic.h" | |||
17 | #include "clang/AST/ASTImporterSharedState.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/STLExtras.h" | |||
60 | #include "llvm/ADT/ScopeExit.h" | |||
61 | #include "llvm/ADT/SmallVector.h" | |||
62 | #include "llvm/Support/Casting.h" | |||
63 | #include "llvm/Support/ErrorHandling.h" | |||
64 | #include "llvm/Support/MemoryBuffer.h" | |||
65 | #include <algorithm> | |||
66 | #include <cassert> | |||
67 | #include <cstddef> | |||
68 | #include <memory> | |||
69 | #include <optional> | |||
70 | #include <type_traits> | |||
71 | #include <utility> | |||
72 | ||||
73 | namespace clang { | |||
74 | ||||
75 | using llvm::make_error; | |||
76 | using llvm::Error; | |||
77 | using llvm::Expected; | |||
78 | using ExpectedTypePtr = llvm::Expected<const Type *>; | |||
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 ASTImportError::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.", "clang/lib/AST/ASTImporter.cpp" , 96); | |||
97 | return "Invalid error code."; | |||
98 | } | |||
99 | ||||
100 | void ASTImportError::log(raw_ostream &OS) const { OS << toString(); } | |||
101 | ||||
102 | std::error_code ASTImportError::convertToErrorCode() const { | |||
103 | llvm_unreachable("Function not implemented.")::llvm::llvm_unreachable_internal("Function not implemented." , "clang/lib/AST/ASTImporter.cpp", 103); | |||
104 | } | |||
105 | ||||
106 | char ASTImportError::ID; | |||
107 | ||||
108 | template <class T> | |||
109 | SmallVector<Decl *, 2> | |||
110 | getCanonicalForwardRedeclChain(Redeclarable<T>* D) { | |||
111 | SmallVector<Decl *, 2> Redecls; | |||
112 | for (auto *R : D->getFirstDecl()->redecls()) { | |||
113 | if (R != D->getFirstDecl()) | |||
114 | Redecls.push_back(R); | |||
115 | } | |||
116 | Redecls.push_back(D->getFirstDecl()); | |||
117 | std::reverse(Redecls.begin(), Redecls.end()); | |||
118 | return Redecls; | |||
119 | } | |||
120 | ||||
121 | SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) { | |||
122 | if (auto *FD = dyn_cast<FunctionDecl>(D)) | |||
123 | return getCanonicalForwardRedeclChain<FunctionDecl>(FD); | |||
124 | if (auto *VD = dyn_cast<VarDecl>(D)) | |||
125 | return getCanonicalForwardRedeclChain<VarDecl>(VD); | |||
126 | if (auto *TD = dyn_cast<TagDecl>(D)) | |||
127 | return getCanonicalForwardRedeclChain<TagDecl>(TD); | |||
128 | llvm_unreachable("Bad declaration kind")::llvm::llvm_unreachable_internal("Bad declaration kind", "clang/lib/AST/ASTImporter.cpp" , 128); | |||
129 | } | |||
130 | ||||
131 | void updateFlags(const Decl *From, Decl *To) { | |||
132 | // Check if some flags or attrs are new in 'From' and copy into 'To'. | |||
133 | // FIXME: Other flags or attrs? | |||
134 | if (From->isUsed(false) && !To->isUsed(false)) | |||
135 | To->setIsUsed(); | |||
136 | } | |||
137 | ||||
138 | /// How to handle import errors that occur when import of a child declaration | |||
139 | /// of a DeclContext fails. | |||
140 | class ChildErrorHandlingStrategy { | |||
141 | /// This context is imported (in the 'from' domain). | |||
142 | /// It is nullptr if a non-DeclContext is imported. | |||
143 | const DeclContext *const FromDC; | |||
144 | /// Ignore import errors of the children. | |||
145 | /// If true, the context can be imported successfully if a child | |||
146 | /// of it failed to import. Otherwise the import errors of the child nodes | |||
147 | /// are accumulated (joined) into the import error object of the parent. | |||
148 | /// (Import of a parent can fail in other ways.) | |||
149 | bool const IgnoreChildErrors; | |||
150 | ||||
151 | public: | |||
152 | ChildErrorHandlingStrategy(const DeclContext *FromDC) | |||
153 | : FromDC(FromDC), IgnoreChildErrors(!isa<TagDecl>(FromDC)) {} | |||
154 | ChildErrorHandlingStrategy(const Decl *FromD) | |||
155 | : FromDC(dyn_cast<DeclContext>(FromD)), | |||
156 | IgnoreChildErrors(!isa<TagDecl>(FromD)) {} | |||
157 | ||||
158 | /// Process the import result of a child (of the current declaration). | |||
159 | /// \param ResultErr The import error that can be used as result of | |||
160 | /// importing the parent. This may be changed by the function. | |||
161 | /// \param ChildErr Result of importing a child. Can be success or error. | |||
162 | void handleChildImportResult(Error &ResultErr, Error &&ChildErr) { | |||
163 | if (ChildErr && !IgnoreChildErrors) | |||
164 | ResultErr = joinErrors(std::move(ResultErr), std::move(ChildErr)); | |||
165 | else | |||
166 | consumeError(std::move(ChildErr)); | |||
167 | } | |||
168 | ||||
169 | /// Determine if import failure of a child does not cause import failure of | |||
170 | /// its parent. | |||
171 | bool ignoreChildErrorOnParent(Decl *FromChildD) const { | |||
172 | if (!IgnoreChildErrors || !FromDC) | |||
173 | return false; | |||
174 | return FromDC->containsDecl(FromChildD); | |||
175 | } | |||
176 | }; | |||
177 | ||||
178 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>, | |||
179 | public DeclVisitor<ASTNodeImporter, ExpectedDecl>, | |||
180 | public StmtVisitor<ASTNodeImporter, ExpectedStmt> { | |||
181 | ASTImporter &Importer; | |||
182 | ||||
183 | // Use this instead of Importer.importInto . | |||
184 | template <typename ImportT> | |||
185 | [[nodiscard]] Error importInto(ImportT &To, const ImportT &From) { | |||
186 | return Importer.importInto(To, From); | |||
187 | } | |||
188 | ||||
189 | // Use this to import pointers of specific type. | |||
190 | template <typename ImportT> | |||
191 | [[nodiscard]] Error importInto(ImportT *&To, ImportT *From) { | |||
192 | auto ToOrErr = Importer.Import(From); | |||
193 | if (ToOrErr) | |||
194 | To = cast_or_null<ImportT>(*ToOrErr); | |||
195 | return ToOrErr.takeError(); | |||
196 | } | |||
197 | ||||
198 | // Call the import function of ASTImporter for a baseclass of type `T` and | |||
199 | // cast the return value to `T`. | |||
200 | template <typename T> | |||
201 | auto import(T *From) | |||
202 | -> std::conditional_t<std::is_base_of_v<Type, T>, Expected<const T *>, | |||
203 | Expected<T *>> { | |||
204 | auto ToOrErr = Importer.Import(From); | |||
205 | if (!ToOrErr) | |||
206 | return ToOrErr.takeError(); | |||
207 | return cast_or_null<T>(*ToOrErr); | |||
208 | } | |||
209 | ||||
210 | template <typename T> | |||
211 | auto import(const T *From) { | |||
212 | return import(const_cast<T *>(From)); | |||
213 | } | |||
214 | ||||
215 | // Call the import function of ASTImporter for type `T`. | |||
216 | template <typename T> | |||
217 | Expected<T> import(const T &From) { | |||
218 | return Importer.Import(From); | |||
219 | } | |||
220 | ||||
221 | // Import an std::optional<T> by importing the contained T, if any. | |||
222 | template <typename T> | |||
223 | Expected<std::optional<T>> import(std::optional<T> From) { | |||
224 | if (!From) | |||
225 | return std::nullopt; | |||
226 | return import(*From); | |||
227 | } | |||
228 | ||||
229 | ExplicitSpecifier importExplicitSpecifier(Error &Err, | |||
230 | ExplicitSpecifier ESpec); | |||
231 | ||||
232 | // Wrapper for an overload set. | |||
233 | template <typename ToDeclT> struct CallOverloadedCreateFun { | |||
234 | template <typename... Args> decltype(auto) operator()(Args &&... args) { | |||
235 | return ToDeclT::Create(std::forward<Args>(args)...); | |||
236 | } | |||
237 | }; | |||
238 | ||||
239 | // Always use these functions to create a Decl during import. There are | |||
240 | // certain tasks which must be done after the Decl was created, e.g. we | |||
241 | // must immediately register that as an imported Decl. The parameter `ToD` | |||
242 | // will be set to the newly created Decl or if had been imported before | |||
243 | // then to the already imported Decl. Returns a bool value set to true if | |||
244 | // the `FromD` had been imported before. | |||
245 | template <typename ToDeclT, typename FromDeclT, typename... Args> | |||
246 | [[nodiscard]] bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, | |||
247 | Args &&...args) { | |||
248 | // There may be several overloads of ToDeclT::Create. We must make sure | |||
249 | // to call the one which would be chosen by the arguments, thus we use a | |||
250 | // wrapper for the overload set. | |||
251 | CallOverloadedCreateFun<ToDeclT> OC; | |||
252 | return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, | |||
253 | std::forward<Args>(args)...); | |||
254 | } | |||
255 | // Use this overload if a special Type is needed to be created. E.g if we | |||
256 | // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl` | |||
257 | // then: | |||
258 | // TypedefNameDecl *ToTypedef; | |||
259 | // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...); | |||
260 | template <typename NewDeclT, typename ToDeclT, typename FromDeclT, | |||
261 | typename... Args> | |||
262 | [[nodiscard]] bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD, | |||
263 | Args &&...args) { | |||
264 | CallOverloadedCreateFun<NewDeclT> OC; | |||
265 | return GetImportedOrCreateSpecialDecl(ToD, OC, FromD, | |||
266 | std::forward<Args>(args)...); | |||
267 | } | |||
268 | // Use this version if a special create function must be | |||
269 | // used, e.g. CXXRecordDecl::CreateLambda . | |||
270 | template <typename ToDeclT, typename CreateFunT, typename FromDeclT, | |||
271 | typename... Args> | |||
272 | [[nodiscard]] bool | |||
273 | GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun, | |||
274 | FromDeclT *FromD, Args &&...args) { | |||
275 | if (Importer.getImportDeclErrorIfAny(FromD)) { | |||
276 | ToD = nullptr; | |||
277 | return true; // Already imported but with error. | |||
278 | } | |||
279 | ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD)); | |||
280 | if (ToD) | |||
281 | return true; // Already imported. | |||
282 | ToD = CreateFun(std::forward<Args>(args)...); | |||
283 | // Keep track of imported Decls. | |||
284 | Importer.RegisterImportedDecl(FromD, ToD); | |||
285 | Importer.SharedState->markAsNewDecl(ToD); | |||
286 | InitializeImportedDecl(FromD, ToD); | |||
287 | return false; // A new Decl is created. | |||
288 | } | |||
289 | ||||
290 | void InitializeImportedDecl(Decl *FromD, Decl *ToD) { | |||
291 | ToD->IdentifierNamespace = FromD->IdentifierNamespace; | |||
292 | if (FromD->isUsed()) | |||
293 | ToD->setIsUsed(); | |||
294 | if (FromD->isImplicit()) | |||
295 | ToD->setImplicit(); | |||
296 | } | |||
297 | ||||
298 | // Check if we have found an existing definition. Returns with that | |||
299 | // definition if yes, otherwise returns null. | |||
300 | Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) { | |||
301 | const FunctionDecl *Definition = nullptr; | |||
302 | if (D->doesThisDeclarationHaveABody() && | |||
303 | FoundFunction->hasBody(Definition)) | |||
304 | return Importer.MapImported(D, const_cast<FunctionDecl *>(Definition)); | |||
305 | return nullptr; | |||
306 | } | |||
307 | ||||
308 | void addDeclToContexts(Decl *FromD, Decl *ToD) { | |||
309 | if (Importer.isMinimalImport()) { | |||
310 | // In minimal import case the decl must be added even if it is not | |||
311 | // contained in original context, for LLDB compatibility. | |||
312 | // FIXME: Check if a better solution is possible. | |||
313 | if (!FromD->getDescribedTemplate() && | |||
314 | FromD->getFriendObjectKind() == Decl::FOK_None) | |||
315 | ToD->getLexicalDeclContext()->addDeclInternal(ToD); | |||
316 | return; | |||
317 | } | |||
318 | ||||
319 | DeclContext *FromDC = FromD->getDeclContext(); | |||
320 | DeclContext *FromLexicalDC = FromD->getLexicalDeclContext(); | |||
321 | DeclContext *ToDC = ToD->getDeclContext(); | |||
322 | DeclContext *ToLexicalDC = ToD->getLexicalDeclContext(); | |||
323 | ||||
324 | bool Visible = false; | |||
325 | if (FromDC->containsDeclAndLoad(FromD)) { | |||
326 | ToDC->addDeclInternal(ToD); | |||
327 | Visible = true; | |||
328 | } | |||
329 | if (ToDC != ToLexicalDC && FromLexicalDC->containsDeclAndLoad(FromD)) { | |||
330 | ToLexicalDC->addDeclInternal(ToD); | |||
331 | Visible = true; | |||
332 | } | |||
333 | ||||
334 | // If the Decl was added to any context, it was made already visible. | |||
335 | // Otherwise it is still possible that it should be visible. | |||
336 | if (!Visible) { | |||
337 | if (auto *FromNamed = dyn_cast<NamedDecl>(FromD)) { | |||
338 | auto *ToNamed = cast<NamedDecl>(ToD); | |||
339 | DeclContextLookupResult FromLookup = | |||
340 | FromDC->lookup(FromNamed->getDeclName()); | |||
341 | if (llvm::is_contained(FromLookup, FromNamed)) | |||
342 | ToDC->makeDeclVisibleInContext(ToNamed); | |||
343 | } | |||
344 | } | |||
345 | } | |||
346 | ||||
347 | void updateLookupTableForTemplateParameters(TemplateParameterList &Params, | |||
348 | DeclContext *OldDC) { | |||
349 | ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable(); | |||
350 | if (!LT) | |||
351 | return; | |||
352 | ||||
353 | for (NamedDecl *TP : Params) | |||
354 | LT->update(TP, OldDC); | |||
355 | } | |||
356 | ||||
357 | void updateLookupTableForTemplateParameters(TemplateParameterList &Params) { | |||
358 | updateLookupTableForTemplateParameters( | |||
359 | Params, Importer.getToContext().getTranslationUnitDecl()); | |||
360 | } | |||
361 | ||||
362 | public: | |||
363 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {} | |||
364 | ||||
365 | using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit; | |||
366 | using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit; | |||
367 | using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit; | |||
368 | ||||
369 | // Importing types | |||
370 | ExpectedType VisitType(const Type *T); | |||
371 | ExpectedType VisitAtomicType(const AtomicType *T); | |||
372 | ExpectedType VisitBuiltinType(const BuiltinType *T); | |||
373 | ExpectedType VisitDecayedType(const DecayedType *T); | |||
374 | ExpectedType VisitComplexType(const ComplexType *T); | |||
375 | ExpectedType VisitPointerType(const PointerType *T); | |||
376 | ExpectedType VisitBlockPointerType(const BlockPointerType *T); | |||
377 | ExpectedType VisitLValueReferenceType(const LValueReferenceType *T); | |||
378 | ExpectedType VisitRValueReferenceType(const RValueReferenceType *T); | |||
379 | ExpectedType VisitMemberPointerType(const MemberPointerType *T); | |||
380 | ExpectedType VisitConstantArrayType(const ConstantArrayType *T); | |||
381 | ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T); | |||
382 | ExpectedType VisitVariableArrayType(const VariableArrayType *T); | |||
383 | ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T); | |||
384 | // FIXME: DependentSizedExtVectorType | |||
385 | ExpectedType VisitVectorType(const VectorType *T); | |||
386 | ExpectedType VisitExtVectorType(const ExtVectorType *T); | |||
387 | ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T); | |||
388 | ExpectedType VisitFunctionProtoType(const FunctionProtoType *T); | |||
389 | ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T); | |||
390 | ExpectedType VisitParenType(const ParenType *T); | |||
391 | ExpectedType VisitTypedefType(const TypedefType *T); | |||
392 | ExpectedType VisitTypeOfExprType(const TypeOfExprType *T); | |||
393 | // FIXME: DependentTypeOfExprType | |||
394 | ExpectedType VisitTypeOfType(const TypeOfType *T); | |||
395 | ExpectedType VisitUsingType(const UsingType *T); | |||
396 | ExpectedType VisitDecltypeType(const DecltypeType *T); | |||
397 | ExpectedType VisitUnaryTransformType(const UnaryTransformType *T); | |||
398 | ExpectedType VisitAutoType(const AutoType *T); | |||
399 | ExpectedType VisitDeducedTemplateSpecializationType( | |||
400 | const DeducedTemplateSpecializationType *T); | |||
401 | ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T); | |||
402 | // FIXME: DependentDecltypeType | |||
403 | ExpectedType VisitRecordType(const RecordType *T); | |||
404 | ExpectedType VisitEnumType(const EnumType *T); | |||
405 | ExpectedType VisitAttributedType(const AttributedType *T); | |||
406 | ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T); | |||
407 | ExpectedType VisitSubstTemplateTypeParmType( | |||
408 | const SubstTemplateTypeParmType *T); | |||
409 | ExpectedType | |||
410 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T); | |||
411 | ExpectedType VisitTemplateSpecializationType( | |||
412 | const TemplateSpecializationType *T); | |||
413 | ExpectedType VisitElaboratedType(const ElaboratedType *T); | |||
414 | ExpectedType VisitDependentNameType(const DependentNameType *T); | |||
415 | ExpectedType VisitPackExpansionType(const PackExpansionType *T); | |||
416 | ExpectedType VisitDependentTemplateSpecializationType( | |||
417 | const DependentTemplateSpecializationType *T); | |||
418 | ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T); | |||
419 | ExpectedType VisitObjCObjectType(const ObjCObjectType *T); | |||
420 | ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); | |||
421 | ||||
422 | // Importing declarations | |||
423 | Error ImportDeclParts(NamedDecl *D, DeclarationName &Name, NamedDecl *&ToD, | |||
424 | SourceLocation &Loc); | |||
425 | Error ImportDeclParts( | |||
426 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, | |||
427 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc); | |||
428 | Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); | |||
429 | Error ImportDeclarationNameLoc( | |||
430 | const DeclarationNameInfo &From, DeclarationNameInfo &To); | |||
431 | Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); | |||
432 | Error ImportDeclContext( | |||
433 | Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC); | |||
434 | Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To); | |||
435 | ||||
436 | Expected<CXXCastPath> ImportCastPath(CastExpr *E); | |||
437 | Expected<APValue> ImportAPValue(const APValue &FromValue); | |||
438 | ||||
439 | using Designator = DesignatedInitExpr::Designator; | |||
440 | ||||
441 | /// What we should import from the definition. | |||
442 | enum ImportDefinitionKind { | |||
443 | /// Import the default subset of the definition, which might be | |||
444 | /// nothing (if minimal import is set) or might be everything (if minimal | |||
445 | /// import is not set). | |||
446 | IDK_Default, | |||
447 | /// Import everything. | |||
448 | IDK_Everything, | |||
449 | /// Import only the bare bones needed to establish a valid | |||
450 | /// DeclContext. | |||
451 | IDK_Basic | |||
452 | }; | |||
453 | ||||
454 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { | |||
455 | return IDK == IDK_Everything || | |||
456 | (IDK == IDK_Default && !Importer.isMinimalImport()); | |||
457 | } | |||
458 | ||||
459 | Error ImportInitializer(VarDecl *From, VarDecl *To); | |||
460 | Error ImportDefinition( | |||
461 | RecordDecl *From, RecordDecl *To, | |||
462 | ImportDefinitionKind Kind = IDK_Default); | |||
463 | Error ImportDefinition( | |||
464 | EnumDecl *From, EnumDecl *To, | |||
465 | ImportDefinitionKind Kind = IDK_Default); | |||
466 | Error ImportDefinition( | |||
467 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, | |||
468 | ImportDefinitionKind Kind = IDK_Default); | |||
469 | Error ImportDefinition( | |||
470 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, | |||
471 | ImportDefinitionKind Kind = IDK_Default); | |||
472 | Error ImportTemplateArguments(ArrayRef<TemplateArgument> FromArgs, | |||
473 | SmallVectorImpl<TemplateArgument> &ToArgs); | |||
474 | Expected<TemplateArgument> | |||
475 | ImportTemplateArgument(const TemplateArgument &From); | |||
476 | ||||
477 | template <typename InContainerTy> | |||
478 | Error ImportTemplateArgumentListInfo( | |||
479 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo); | |||
480 | ||||
481 | template<typename InContainerTy> | |||
482 | Error ImportTemplateArgumentListInfo( | |||
483 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, | |||
484 | const InContainerTy &Container, TemplateArgumentListInfo &Result); | |||
485 | ||||
486 | using TemplateArgsTy = SmallVector<TemplateArgument, 8>; | |||
487 | using FunctionTemplateAndArgsTy = | |||
488 | std::tuple<FunctionTemplateDecl *, TemplateArgsTy>; | |||
489 | Expected<FunctionTemplateAndArgsTy> | |||
490 | ImportFunctionTemplateWithTemplateArgsFromSpecialization( | |||
491 | FunctionDecl *FromFD); | |||
492 | Error ImportTemplateParameterLists(const DeclaratorDecl *FromD, | |||
493 | DeclaratorDecl *ToD); | |||
494 | ||||
495 | Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD); | |||
496 | ||||
497 | Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD); | |||
498 | ||||
499 | Error ImportDefaultArgOfParmVarDecl(const ParmVarDecl *FromParam, | |||
500 | ParmVarDecl *ToParam); | |||
501 | ||||
502 | Expected<InheritedConstructor> | |||
503 | ImportInheritedConstructor(const InheritedConstructor &From); | |||
504 | ||||
505 | template <typename T> | |||
506 | bool hasSameVisibilityContextAndLinkage(T *Found, T *From); | |||
507 | ||||
508 | bool IsStructuralMatch(Decl *From, Decl *To, bool Complain = true); | |||
509 | ExpectedDecl VisitDecl(Decl *D); | |||
510 | ExpectedDecl VisitImportDecl(ImportDecl *D); | |||
511 | ExpectedDecl VisitEmptyDecl(EmptyDecl *D); | |||
512 | ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D); | |||
513 | ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D); | |||
514 | ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D); | |||
515 | ExpectedDecl VisitBindingDecl(BindingDecl *D); | |||
516 | ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D); | |||
517 | ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D); | |||
518 | ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); | |||
519 | ExpectedDecl VisitTypedefDecl(TypedefDecl *D); | |||
520 | ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D); | |||
521 | ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); | |||
522 | ExpectedDecl VisitLabelDecl(LabelDecl *D); | |||
523 | ExpectedDecl VisitEnumDecl(EnumDecl *D); | |||
524 | ExpectedDecl VisitRecordDecl(RecordDecl *D); | |||
525 | ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D); | |||
526 | ExpectedDecl VisitFunctionDecl(FunctionDecl *D); | |||
527 | ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D); | |||
528 | ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D); | |||
529 | ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D); | |||
530 | ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D); | |||
531 | ExpectedDecl VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D); | |||
532 | ExpectedDecl VisitFieldDecl(FieldDecl *D); | |||
533 | ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D); | |||
534 | ExpectedDecl VisitFriendDecl(FriendDecl *D); | |||
535 | ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D); | |||
536 | ExpectedDecl VisitVarDecl(VarDecl *D); | |||
537 | ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D); | |||
538 | ExpectedDecl VisitParmVarDecl(ParmVarDecl *D); | |||
539 | ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D); | |||
540 | ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); | |||
541 | ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D); | |||
542 | ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D); | |||
543 | ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D); | |||
544 | ExpectedDecl VisitUsingDecl(UsingDecl *D); | |||
545 | ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D); | |||
546 | ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D); | |||
547 | ExpectedDecl VisitUsingPackDecl(UsingPackDecl *D); | |||
548 | ExpectedDecl ImportUsingShadowDecls(BaseUsingDecl *D, BaseUsingDecl *ToSI); | |||
549 | ExpectedDecl VisitUsingEnumDecl(UsingEnumDecl *D); | |||
550 | ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); | |||
551 | ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); | |||
552 | ExpectedDecl VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D); | |||
553 | ExpectedDecl | |||
554 | VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D); | |||
555 | ||||
556 | Expected<ObjCTypeParamList *> | |||
557 | ImportObjCTypeParamList(ObjCTypeParamList *list); | |||
558 | ||||
559 | ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); | |||
560 | ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); | |||
561 | ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D); | |||
562 | ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D); | |||
563 | ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); | |||
564 | ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); | |||
565 | ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); | |||
566 | ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); | |||
567 | ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D); | |||
568 | ExpectedDecl VisitClassTemplateSpecializationDecl( | |||
569 | ClassTemplateSpecializationDecl *D); | |||
570 | ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D); | |||
571 | ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); | |||
572 | ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D); | |||
573 | ||||
574 | // Importing statements | |||
575 | ExpectedStmt VisitStmt(Stmt *S); | |||
576 | ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S); | |||
577 | ExpectedStmt VisitDeclStmt(DeclStmt *S); | |||
578 | ExpectedStmt VisitNullStmt(NullStmt *S); | |||
579 | ExpectedStmt VisitCompoundStmt(CompoundStmt *S); | |||
580 | ExpectedStmt VisitCaseStmt(CaseStmt *S); | |||
581 | ExpectedStmt VisitDefaultStmt(DefaultStmt *S); | |||
582 | ExpectedStmt VisitLabelStmt(LabelStmt *S); | |||
583 | ExpectedStmt VisitAttributedStmt(AttributedStmt *S); | |||
584 | ExpectedStmt VisitIfStmt(IfStmt *S); | |||
585 | ExpectedStmt VisitSwitchStmt(SwitchStmt *S); | |||
586 | ExpectedStmt VisitWhileStmt(WhileStmt *S); | |||
587 | ExpectedStmt VisitDoStmt(DoStmt *S); | |||
588 | ExpectedStmt VisitForStmt(ForStmt *S); | |||
589 | ExpectedStmt VisitGotoStmt(GotoStmt *S); | |||
590 | ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S); | |||
591 | ExpectedStmt VisitContinueStmt(ContinueStmt *S); | |||
592 | ExpectedStmt VisitBreakStmt(BreakStmt *S); | |||
593 | ExpectedStmt VisitReturnStmt(ReturnStmt *S); | |||
594 | // FIXME: MSAsmStmt | |||
595 | // FIXME: SEHExceptStmt | |||
596 | // FIXME: SEHFinallyStmt | |||
597 | // FIXME: SEHTryStmt | |||
598 | // FIXME: SEHLeaveStmt | |||
599 | // FIXME: CapturedStmt | |||
600 | ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S); | |||
601 | ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S); | |||
602 | ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S); | |||
603 | // FIXME: MSDependentExistsStmt | |||
604 | ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); | |||
605 | ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); | |||
606 | ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); | |||
607 | ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S); | |||
608 | ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); | |||
609 | ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); | |||
610 | ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); | |||
611 | ||||
612 | // Importing expressions | |||
613 | ExpectedStmt VisitExpr(Expr *E); | |||
614 | ExpectedStmt VisitSourceLocExpr(SourceLocExpr *E); | |||
615 | ExpectedStmt VisitVAArgExpr(VAArgExpr *E); | |||
616 | ExpectedStmt VisitChooseExpr(ChooseExpr *E); | |||
617 | ExpectedStmt VisitShuffleVectorExpr(ShuffleVectorExpr *E); | |||
618 | ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E); | |||
619 | ExpectedStmt VisitGenericSelectionExpr(GenericSelectionExpr *E); | |||
620 | ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E); | |||
621 | ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E); | |||
622 | ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); | |||
623 | ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E); | |||
624 | ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); | |||
625 | ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E); | |||
626 | ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E); | |||
627 | ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E); | |||
628 | ExpectedStmt VisitFixedPointLiteral(FixedPointLiteral *E); | |||
629 | ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E); | |||
630 | ExpectedStmt VisitStringLiteral(StringLiteral *E); | |||
631 | ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E); | |||
632 | ExpectedStmt VisitAtomicExpr(AtomicExpr *E); | |||
633 | ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E); | |||
634 | ExpectedStmt VisitConstantExpr(ConstantExpr *E); | |||
635 | ExpectedStmt VisitParenExpr(ParenExpr *E); | |||
636 | ExpectedStmt VisitParenListExpr(ParenListExpr *E); | |||
637 | ExpectedStmt VisitStmtExpr(StmtExpr *E); | |||
638 | ExpectedStmt VisitUnaryOperator(UnaryOperator *E); | |||
639 | ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); | |||
640 | ExpectedStmt VisitBinaryOperator(BinaryOperator *E); | |||
641 | ExpectedStmt VisitConditionalOperator(ConditionalOperator *E); | |||
642 | ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E); | |||
643 | ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E); | |||
644 | ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); | |||
645 | ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E); | |||
646 | ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E); | |||
647 | ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E); | |||
648 | ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E); | |||
649 | ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E); | |||
650 | ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE); | |||
651 | ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E); | |||
652 | ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E); | |||
653 | ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); | |||
654 | ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); | |||
655 | ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); | |||
656 | ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); | |||
657 | ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E); | |||
658 | ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E); | |||
659 | ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E); | |||
660 | ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E); | |||
661 | ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E); | |||
662 | ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E); | |||
663 | ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E); | |||
664 | ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); | |||
665 | ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); | |||
666 | ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); | |||
667 | ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); | |||
668 | ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E); | |||
669 | ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E); | |||
670 | ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E); | |||
671 | ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); | |||
672 | ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); | |||
673 | ExpectedStmt VisitMemberExpr(MemberExpr *E); | |||
674 | ExpectedStmt VisitCallExpr(CallExpr *E); | |||
675 | ExpectedStmt VisitLambdaExpr(LambdaExpr *LE); | |||
676 | ExpectedStmt VisitInitListExpr(InitListExpr *E); | |||
677 | ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E); | |||
678 | ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E); | |||
679 | ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E); | |||
680 | ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E); | |||
681 | ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); | |||
682 | ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E); | |||
683 | ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E); | |||
684 | ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E); | |||
685 | ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E); | |||
686 | ExpectedStmt VisitCXXFoldExpr(CXXFoldExpr *E); | |||
687 | ||||
688 | // Helper for chaining together multiple imports. If an error is detected, | |||
689 | // subsequent imports will return default constructed nodes, so that failure | |||
690 | // can be detected with a single conditional branch after a sequence of | |||
691 | // imports. | |||
692 | template <typename T> T importChecked(Error &Err, const T &From) { | |||
693 | // Don't attempt to import nodes if we hit an error earlier. | |||
694 | if (Err) | |||
695 | return T{}; | |||
696 | Expected<T> MaybeVal = import(From); | |||
697 | if (!MaybeVal) { | |||
698 | Err = MaybeVal.takeError(); | |||
699 | return T{}; | |||
700 | } | |||
701 | return *MaybeVal; | |||
702 | } | |||
703 | ||||
704 | template<typename IIter, typename OIter> | |||
705 | Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { | |||
706 | using ItemT = std::remove_reference_t<decltype(*Obegin)>; | |||
707 | for (; Ibegin != Iend; ++Ibegin, ++Obegin) { | |||
708 | Expected<ItemT> ToOrErr = import(*Ibegin); | |||
709 | if (!ToOrErr) | |||
710 | return ToOrErr.takeError(); | |||
711 | *Obegin = *ToOrErr; | |||
712 | } | |||
713 | return Error::success(); | |||
714 | } | |||
715 | ||||
716 | // Import every item from a container structure into an output container. | |||
717 | // If error occurs, stops at first error and returns the error. | |||
718 | // The output container should have space for all needed elements (it is not | |||
719 | // expanded, new items are put into from the beginning). | |||
720 | template<typename InContainerTy, typename OutContainerTy> | |||
721 | Error ImportContainerChecked( | |||
722 | const InContainerTy &InContainer, OutContainerTy &OutContainer) { | |||
723 | return ImportArrayChecked( | |||
724 | InContainer.begin(), InContainer.end(), OutContainer.begin()); | |||
725 | } | |||
726 | ||||
727 | template<typename InContainerTy, typename OIter> | |||
728 | Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) { | |||
729 | return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin); | |||
730 | } | |||
731 | ||||
732 | Error ImportOverriddenMethods(CXXMethodDecl *ToMethod, | |||
733 | CXXMethodDecl *FromMethod); | |||
734 | ||||
735 | Expected<FunctionDecl *> FindFunctionTemplateSpecialization( | |||
736 | FunctionDecl *FromFD); | |||
737 | ||||
738 | // Returns true if the given function has a placeholder return type and | |||
739 | // that type is declared inside the body of the function. | |||
740 | // E.g. auto f() { struct X{}; return X(); } | |||
741 | bool hasAutoReturnTypeDeclaredInside(FunctionDecl *D); | |||
742 | }; | |||
743 | ||||
744 | template <typename InContainerTy> | |||
745 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( | |||
746 | SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc, | |||
747 | const InContainerTy &Container, TemplateArgumentListInfo &Result) { | |||
748 | auto ToLAngleLocOrErr = import(FromLAngleLoc); | |||
749 | if (!ToLAngleLocOrErr) | |||
750 | return ToLAngleLocOrErr.takeError(); | |||
751 | auto ToRAngleLocOrErr = import(FromRAngleLoc); | |||
752 | if (!ToRAngleLocOrErr) | |||
753 | return ToRAngleLocOrErr.takeError(); | |||
754 | ||||
755 | TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr); | |||
756 | if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo)) | |||
757 | return Err; | |||
758 | Result = ToTAInfo; | |||
759 | return Error::success(); | |||
760 | } | |||
761 | ||||
762 | template <> | |||
763 | Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>( | |||
764 | const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) { | |||
765 | return ImportTemplateArgumentListInfo( | |||
766 | From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result); | |||
767 | } | |||
768 | ||||
769 | template <> | |||
770 | Error ASTNodeImporter::ImportTemplateArgumentListInfo< | |||
771 | ASTTemplateArgumentListInfo>( | |||
772 | const ASTTemplateArgumentListInfo &From, | |||
773 | TemplateArgumentListInfo &Result) { | |||
774 | return ImportTemplateArgumentListInfo( | |||
775 | From.LAngleLoc, From.RAngleLoc, From.arguments(), Result); | |||
776 | } | |||
777 | ||||
778 | Expected<ASTNodeImporter::FunctionTemplateAndArgsTy> | |||
779 | ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization( | |||
780 | FunctionDecl *FromFD) { | |||
781 | assert(FromFD->getTemplatedKind() ==(static_cast <bool> (FromFD->getTemplatedKind() == FunctionDecl ::TK_FunctionTemplateSpecialization) ? void (0) : __assert_fail ("FromFD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplateSpecialization" , "clang/lib/AST/ASTImporter.cpp", 782, __extension__ __PRETTY_FUNCTION__ )) | |||
782 | FunctionDecl::TK_FunctionTemplateSpecialization)(static_cast <bool> (FromFD->getTemplatedKind() == FunctionDecl ::TK_FunctionTemplateSpecialization) ? void (0) : __assert_fail ("FromFD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplateSpecialization" , "clang/lib/AST/ASTImporter.cpp", 782, __extension__ __PRETTY_FUNCTION__ )); | |||
783 | ||||
784 | FunctionTemplateAndArgsTy Result; | |||
785 | ||||
786 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); | |||
787 | if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate())) | |||
788 | return std::move(Err); | |||
789 | ||||
790 | // Import template arguments. | |||
791 | if (Error Err = ImportTemplateArguments(FTSInfo->TemplateArguments->asArray(), | |||
792 | std::get<1>(Result))) | |||
793 | return std::move(Err); | |||
794 | ||||
795 | return Result; | |||
796 | } | |||
797 | ||||
798 | template <> | |||
799 | Expected<TemplateParameterList *> | |||
800 | ASTNodeImporter::import(TemplateParameterList *From) { | |||
801 | SmallVector<NamedDecl *, 4> To(From->size()); | |||
802 | if (Error Err = ImportContainerChecked(*From, To)) | |||
803 | return std::move(Err); | |||
804 | ||||
805 | ExpectedExpr ToRequiresClause = import(From->getRequiresClause()); | |||
806 | if (!ToRequiresClause) | |||
807 | return ToRequiresClause.takeError(); | |||
808 | ||||
809 | auto ToTemplateLocOrErr = import(From->getTemplateLoc()); | |||
810 | if (!ToTemplateLocOrErr) | |||
811 | return ToTemplateLocOrErr.takeError(); | |||
812 | auto ToLAngleLocOrErr = import(From->getLAngleLoc()); | |||
813 | if (!ToLAngleLocOrErr) | |||
814 | return ToLAngleLocOrErr.takeError(); | |||
815 | auto ToRAngleLocOrErr = import(From->getRAngleLoc()); | |||
816 | if (!ToRAngleLocOrErr) | |||
817 | return ToRAngleLocOrErr.takeError(); | |||
818 | ||||
819 | return TemplateParameterList::Create( | |||
820 | Importer.getToContext(), | |||
821 | *ToTemplateLocOrErr, | |||
822 | *ToLAngleLocOrErr, | |||
823 | To, | |||
824 | *ToRAngleLocOrErr, | |||
825 | *ToRequiresClause); | |||
826 | } | |||
827 | ||||
828 | template <> | |||
829 | Expected<TemplateArgument> | |||
830 | ASTNodeImporter::import(const TemplateArgument &From) { | |||
831 | switch (From.getKind()) { | |||
832 | case TemplateArgument::Null: | |||
833 | return TemplateArgument(); | |||
834 | ||||
835 | case TemplateArgument::Type: { | |||
836 | ExpectedType ToTypeOrErr = import(From.getAsType()); | |||
837 | if (!ToTypeOrErr) | |||
838 | return ToTypeOrErr.takeError(); | |||
839 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/ false, | |||
840 | From.getIsDefaulted()); | |||
841 | } | |||
842 | ||||
843 | case TemplateArgument::Integral: { | |||
844 | ExpectedType ToTypeOrErr = import(From.getIntegralType()); | |||
845 | if (!ToTypeOrErr) | |||
846 | return ToTypeOrErr.takeError(); | |||
847 | return TemplateArgument(From, *ToTypeOrErr); | |||
848 | } | |||
849 | ||||
850 | case TemplateArgument::Declaration: { | |||
851 | Expected<ValueDecl *> ToOrErr = import(From.getAsDecl()); | |||
852 | if (!ToOrErr) | |||
853 | return ToOrErr.takeError(); | |||
854 | ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl()); | |||
855 | if (!ToTypeOrErr) | |||
856 | return ToTypeOrErr.takeError(); | |||
857 | return TemplateArgument(*ToOrErr, *ToTypeOrErr, From.getIsDefaulted()); | |||
858 | } | |||
859 | ||||
860 | case TemplateArgument::NullPtr: { | |||
861 | ExpectedType ToTypeOrErr = import(From.getNullPtrType()); | |||
862 | if (!ToTypeOrErr) | |||
863 | return ToTypeOrErr.takeError(); | |||
864 | return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/ true, | |||
865 | From.getIsDefaulted()); | |||
866 | } | |||
867 | ||||
868 | case TemplateArgument::Template: { | |||
869 | Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate()); | |||
870 | if (!ToTemplateOrErr) | |||
871 | return ToTemplateOrErr.takeError(); | |||
872 | ||||
873 | return TemplateArgument(*ToTemplateOrErr, From.getIsDefaulted()); | |||
874 | } | |||
875 | ||||
876 | case TemplateArgument::TemplateExpansion: { | |||
877 | Expected<TemplateName> ToTemplateOrErr = | |||
878 | import(From.getAsTemplateOrTemplatePattern()); | |||
879 | if (!ToTemplateOrErr) | |||
880 | return ToTemplateOrErr.takeError(); | |||
881 | ||||
882 | return TemplateArgument(*ToTemplateOrErr, From.getNumTemplateExpansions(), | |||
883 | From.getIsDefaulted()); | |||
884 | } | |||
885 | ||||
886 | case TemplateArgument::Expression: | |||
887 | if (ExpectedExpr ToExpr = import(From.getAsExpr())) | |||
888 | return TemplateArgument(*ToExpr, From.getIsDefaulted()); | |||
889 | else | |||
890 | return ToExpr.takeError(); | |||
891 | ||||
892 | case TemplateArgument::Pack: { | |||
893 | SmallVector<TemplateArgument, 2> ToPack; | |||
894 | ToPack.reserve(From.pack_size()); | |||
895 | if (Error Err = ImportTemplateArguments(From.pack_elements(), ToPack)) | |||
896 | return std::move(Err); | |||
897 | ||||
898 | return TemplateArgument( | |||
899 | llvm::ArrayRef(ToPack).copy(Importer.getToContext())); | |||
900 | } | |||
901 | } | |||
902 | ||||
903 | llvm_unreachable("Invalid template argument kind")::llvm::llvm_unreachable_internal("Invalid template argument kind" , "clang/lib/AST/ASTImporter.cpp", 903); | |||
904 | } | |||
905 | ||||
906 | template <> | |||
907 | Expected<TemplateArgumentLoc> | |||
908 | ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) { | |||
909 | Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument()); | |||
910 | if (!ArgOrErr) | |||
911 | return ArgOrErr.takeError(); | |||
912 | TemplateArgument Arg = *ArgOrErr; | |||
913 | ||||
914 | TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo(); | |||
915 | ||||
916 | TemplateArgumentLocInfo ToInfo; | |||
917 | if (Arg.getKind() == TemplateArgument::Expression) { | |||
918 | ExpectedExpr E = import(FromInfo.getAsExpr()); | |||
919 | if (!E) | |||
920 | return E.takeError(); | |||
921 | ToInfo = TemplateArgumentLocInfo(*E); | |||
922 | } else if (Arg.getKind() == TemplateArgument::Type) { | |||
923 | if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo())) | |||
924 | ToInfo = TemplateArgumentLocInfo(*TSIOrErr); | |||
925 | else | |||
926 | return TSIOrErr.takeError(); | |||
927 | } else { | |||
928 | auto ToTemplateQualifierLocOrErr = | |||
929 | import(FromInfo.getTemplateQualifierLoc()); | |||
930 | if (!ToTemplateQualifierLocOrErr) | |||
931 | return ToTemplateQualifierLocOrErr.takeError(); | |||
932 | auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc()); | |||
933 | if (!ToTemplateNameLocOrErr) | |||
934 | return ToTemplateNameLocOrErr.takeError(); | |||
935 | auto ToTemplateEllipsisLocOrErr = | |||
936 | import(FromInfo.getTemplateEllipsisLoc()); | |||
937 | if (!ToTemplateEllipsisLocOrErr) | |||
938 | return ToTemplateEllipsisLocOrErr.takeError(); | |||
939 | ToInfo = TemplateArgumentLocInfo( | |||
940 | Importer.getToContext(), *ToTemplateQualifierLocOrErr, | |||
941 | *ToTemplateNameLocOrErr, *ToTemplateEllipsisLocOrErr); | |||
942 | } | |||
943 | ||||
944 | return TemplateArgumentLoc(Arg, ToInfo); | |||
945 | } | |||
946 | ||||
947 | template <> | |||
948 | Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) { | |||
949 | if (DG.isNull()) | |||
950 | return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0); | |||
951 | size_t NumDecls = DG.end() - DG.begin(); | |||
952 | SmallVector<Decl *, 1> ToDecls; | |||
953 | ToDecls.reserve(NumDecls); | |||
954 | for (Decl *FromD : DG) { | |||
955 | if (auto ToDOrErr = import(FromD)) | |||
956 | ToDecls.push_back(*ToDOrErr); | |||
957 | else | |||
958 | return ToDOrErr.takeError(); | |||
959 | } | |||
960 | return DeclGroupRef::Create(Importer.getToContext(), | |||
961 | ToDecls.begin(), | |||
962 | NumDecls); | |||
963 | } | |||
964 | ||||
965 | template <> | |||
966 | Expected<ASTNodeImporter::Designator> | |||
967 | ASTNodeImporter::import(const Designator &D) { | |||
968 | if (D.isFieldDesignator()) { | |||
969 | IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName()); | |||
970 | ||||
971 | ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc()); | |||
972 | if (!ToDotLocOrErr) | |||
973 | return ToDotLocOrErr.takeError(); | |||
974 | ||||
975 | ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc()); | |||
976 | if (!ToFieldLocOrErr) | |||
977 | return ToFieldLocOrErr.takeError(); | |||
978 | ||||
979 | return DesignatedInitExpr::Designator::CreateFieldDesignator( | |||
980 | ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr); | |||
981 | } | |||
982 | ||||
983 | ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc()); | |||
984 | if (!ToLBracketLocOrErr) | |||
985 | return ToLBracketLocOrErr.takeError(); | |||
986 | ||||
987 | ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc()); | |||
988 | if (!ToRBracketLocOrErr) | |||
989 | return ToRBracketLocOrErr.takeError(); | |||
990 | ||||
991 | if (D.isArrayDesignator()) | |||
992 | return Designator::CreateArrayDesignator(D.getArrayIndex(), | |||
993 | *ToLBracketLocOrErr, | |||
994 | *ToRBracketLocOrErr); | |||
995 | ||||
996 | ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc()); | |||
997 | if (!ToEllipsisLocOrErr) | |||
998 | return ToEllipsisLocOrErr.takeError(); | |||
999 | ||||
1000 | assert(D.isArrayRangeDesignator())(static_cast <bool> (D.isArrayRangeDesignator()) ? void (0) : __assert_fail ("D.isArrayRangeDesignator()", "clang/lib/AST/ASTImporter.cpp" , 1000, __extension__ __PRETTY_FUNCTION__)); | |||
1001 | return Designator::CreateArrayRangeDesignator( | |||
1002 | D.getArrayIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr, | |||
1003 | *ToRBracketLocOrErr); | |||
1004 | } | |||
1005 | ||||
1006 | template <> | |||
1007 | Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) { | |||
1008 | ValueDecl *Var = nullptr; | |||
1009 | if (From.capturesVariable()) { | |||
1010 | if (auto VarOrErr = import(From.getCapturedVar())) | |||
1011 | Var = *VarOrErr; | |||
1012 | else | |||
1013 | return VarOrErr.takeError(); | |||
1014 | } | |||
1015 | ||||
1016 | auto LocationOrErr = import(From.getLocation()); | |||
1017 | if (!LocationOrErr) | |||
1018 | return LocationOrErr.takeError(); | |||
1019 | ||||
1020 | SourceLocation EllipsisLoc; | |||
1021 | if (From.isPackExpansion()) | |||
1022 | if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc())) | |||
1023 | return std::move(Err); | |||
1024 | ||||
1025 | return LambdaCapture( | |||
1026 | *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var, | |||
1027 | EllipsisLoc); | |||
1028 | } | |||
1029 | ||||
1030 | template <typename T> | |||
1031 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(T *Found, T *From) { | |||
1032 | if (Found->getLinkageInternal() != From->getLinkageInternal()) | |||
1033 | return false; | |||
1034 | ||||
1035 | if (From->hasExternalFormalLinkage()) | |||
1036 | return Found->hasExternalFormalLinkage(); | |||
1037 | if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl()) | |||
1038 | return false; | |||
1039 | if (From->isInAnonymousNamespace()) | |||
1040 | return Found->isInAnonymousNamespace(); | |||
1041 | else | |||
1042 | return !Found->isInAnonymousNamespace() && | |||
1043 | !Found->hasExternalFormalLinkage(); | |||
1044 | } | |||
1045 | ||||
1046 | template <> | |||
1047 | bool ASTNodeImporter::hasSameVisibilityContextAndLinkage(TypedefNameDecl *Found, | |||
1048 | TypedefNameDecl *From) { | |||
1049 | if (Found->getLinkageInternal() != From->getLinkageInternal()) | |||
1050 | return false; | |||
1051 | ||||
1052 | if (From->isInAnonymousNamespace() && Found->isInAnonymousNamespace()) | |||
1053 | return Importer.GetFromTU(Found) == From->getTranslationUnitDecl(); | |||
1054 | return From->isInAnonymousNamespace() == Found->isInAnonymousNamespace(); | |||
1055 | } | |||
1056 | ||||
1057 | } // namespace clang | |||
1058 | ||||
1059 | //---------------------------------------------------------------------------- | |||
1060 | // Import Types | |||
1061 | //---------------------------------------------------------------------------- | |||
1062 | ||||
1063 | using namespace clang; | |||
1064 | ||||
1065 | ExpectedType ASTNodeImporter::VisitType(const Type *T) { | |||
1066 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) | |||
1067 | << T->getTypeClassName(); | |||
1068 | return make_error<ASTImportError>(ASTImportError::UnsupportedConstruct); | |||
1069 | } | |||
1070 | ||||
1071 | ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){ | |||
1072 | ExpectedType UnderlyingTypeOrErr = import(T->getValueType()); | |||
1073 | if (!UnderlyingTypeOrErr) | |||
1074 | return UnderlyingTypeOrErr.takeError(); | |||
1075 | ||||
1076 | return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr); | |||
1077 | } | |||
1078 | ||||
1079 | ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { | |||
1080 | switch (T->getKind()) { | |||
1081 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | |||
1082 | case BuiltinType::Id: \ | |||
1083 | return Importer.getToContext().SingletonId; | |||
1084 | #include "clang/Basic/OpenCLImageTypes.def" | |||
1085 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | |||
1086 | case BuiltinType::Id: \ | |||
1087 | return Importer.getToContext().Id##Ty; | |||
1088 | #include "clang/Basic/OpenCLExtensionTypes.def" | |||
1089 | #define SVE_TYPE(Name, Id, SingletonId) \ | |||
1090 | case BuiltinType::Id: \ | |||
1091 | return Importer.getToContext().SingletonId; | |||
1092 | #include "clang/Basic/AArch64SVEACLETypes.def" | |||
1093 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ | |||
1094 | case BuiltinType::Id: \ | |||
1095 | return Importer.getToContext().Id##Ty; | |||
1096 | #include "clang/Basic/PPCTypes.def" | |||
1097 | #define RVV_TYPE(Name, Id, SingletonId) \ | |||
1098 | case BuiltinType::Id: \ | |||
1099 | return Importer.getToContext().SingletonId; | |||
1100 | #include "clang/Basic/RISCVVTypes.def" | |||
1101 | #define WASM_TYPE(Name, Id, SingletonId) \ | |||
1102 | case BuiltinType::Id: \ | |||
1103 | return Importer.getToContext().SingletonId; | |||
1104 | #include "clang/Basic/WebAssemblyReferenceTypes.def" | |||
1105 | #define SHARED_SINGLETON_TYPE(Expansion) | |||
1106 | #define BUILTIN_TYPE(Id, SingletonId) \ | |||
1107 | case BuiltinType::Id: return Importer.getToContext().SingletonId; | |||
1108 | #include "clang/AST/BuiltinTypes.def" | |||
1109 | ||||
1110 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" | |||
1111 | // context supports C++. | |||
1112 | ||||
1113 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" | |||
1114 | // context supports ObjC. | |||
1115 | ||||
1116 | case BuiltinType::Char_U: | |||
1117 | // The context we're importing from has an unsigned 'char'. If we're | |||
1118 | // importing into a context with a signed 'char', translate to | |||
1119 | // 'unsigned char' instead. | |||
1120 | if (Importer.getToContext().getLangOpts().CharIsSigned) | |||
1121 | return Importer.getToContext().UnsignedCharTy; | |||
1122 | ||||
1123 | return Importer.getToContext().CharTy; | |||
1124 | ||||
1125 | case BuiltinType::Char_S: | |||
1126 | // The context we're importing from has an unsigned 'char'. If we're | |||
1127 | // importing into a context with a signed 'char', translate to | |||
1128 | // 'unsigned char' instead. | |||
1129 | if (!Importer.getToContext().getLangOpts().CharIsSigned) | |||
1130 | return Importer.getToContext().SignedCharTy; | |||
1131 | ||||
1132 | return Importer.getToContext().CharTy; | |||
1133 | ||||
1134 | case BuiltinType::WChar_S: | |||
1135 | case BuiltinType::WChar_U: | |||
1136 | // FIXME: If not in C++, shall we translate to the C equivalent of | |||
1137 | // wchar_t? | |||
1138 | return Importer.getToContext().WCharTy; | |||
1139 | } | |||
1140 | ||||
1141 | llvm_unreachable("Invalid BuiltinType Kind!")::llvm::llvm_unreachable_internal("Invalid BuiltinType Kind!" , "clang/lib/AST/ASTImporter.cpp", 1141); | |||
1142 | } | |||
1143 | ||||
1144 | ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) { | |||
1145 | ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType()); | |||
1146 | if (!ToOriginalTypeOrErr) | |||
1147 | return ToOriginalTypeOrErr.takeError(); | |||
1148 | ||||
1149 | return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr); | |||
1150 | } | |||
1151 | ||||
1152 | ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) { | |||
1153 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); | |||
1154 | if (!ToElementTypeOrErr) | |||
1155 | return ToElementTypeOrErr.takeError(); | |||
1156 | ||||
1157 | return Importer.getToContext().getComplexType(*ToElementTypeOrErr); | |||
1158 | } | |||
1159 | ||||
1160 | ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) { | |||
1161 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType()); | |||
1162 | if (!ToPointeeTypeOrErr) | |||
1163 | return ToPointeeTypeOrErr.takeError(); | |||
1164 | ||||
1165 | return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr); | |||
1166 | } | |||
1167 | ||||
1168 | ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { | |||
1169 | // FIXME: Check for blocks support in "to" context. | |||
1170 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType()); | |||
1171 | if (!ToPointeeTypeOrErr) | |||
1172 | return ToPointeeTypeOrErr.takeError(); | |||
1173 | ||||
1174 | return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr); | |||
1175 | } | |||
1176 | ||||
1177 | ExpectedType | |||
1178 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { | |||
1179 | // FIXME: Check for C++ support in "to" context. | |||
1180 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten()); | |||
1181 | if (!ToPointeeTypeOrErr) | |||
1182 | return ToPointeeTypeOrErr.takeError(); | |||
1183 | ||||
1184 | return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr); | |||
1185 | } | |||
1186 | ||||
1187 | ExpectedType | |||
1188 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { | |||
1189 | // FIXME: Check for C++0x support in "to" context. | |||
1190 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten()); | |||
1191 | if (!ToPointeeTypeOrErr) | |||
1192 | return ToPointeeTypeOrErr.takeError(); | |||
1193 | ||||
1194 | return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr); | |||
1195 | } | |||
1196 | ||||
1197 | ExpectedType | |||
1198 | ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { | |||
1199 | // FIXME: Check for C++ support in "to" context. | |||
1200 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType()); | |||
1201 | if (!ToPointeeTypeOrErr) | |||
1202 | return ToPointeeTypeOrErr.takeError(); | |||
1203 | ||||
1204 | ExpectedTypePtr ClassTypeOrErr = import(T->getClass()); | |||
1205 | if (!ClassTypeOrErr) | |||
1206 | return ClassTypeOrErr.takeError(); | |||
1207 | ||||
1208 | return Importer.getToContext().getMemberPointerType(*ToPointeeTypeOrErr, | |||
1209 | *ClassTypeOrErr); | |||
1210 | } | |||
1211 | ||||
1212 | ExpectedType | |||
1213 | ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { | |||
1214 | Error Err = Error::success(); | |||
1215 | auto ToElementType = importChecked(Err, T->getElementType()); | |||
1216 | auto ToSizeExpr = importChecked(Err, T->getSizeExpr()); | |||
1217 | if (Err) | |||
1218 | return std::move(Err); | |||
1219 | ||||
1220 | return Importer.getToContext().getConstantArrayType( | |||
1221 | ToElementType, T->getSize(), ToSizeExpr, T->getSizeModifier(), | |||
1222 | T->getIndexTypeCVRQualifiers()); | |||
1223 | } | |||
1224 | ||||
1225 | ExpectedType | |||
1226 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { | |||
1227 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); | |||
1228 | if (!ToElementTypeOrErr) | |||
1229 | return ToElementTypeOrErr.takeError(); | |||
1230 | ||||
1231 | return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr, | |||
1232 | T->getSizeModifier(), | |||
1233 | T->getIndexTypeCVRQualifiers()); | |||
1234 | } | |||
1235 | ||||
1236 | ExpectedType | |||
1237 | ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { | |||
1238 | Error Err = Error::success(); | |||
1239 | QualType ToElementType = importChecked(Err, T->getElementType()); | |||
1240 | Expr *ToSizeExpr = importChecked(Err, T->getSizeExpr()); | |||
1241 | SourceRange ToBracketsRange = importChecked(Err, T->getBracketsRange()); | |||
1242 | if (Err) | |||
1243 | return std::move(Err); | |||
1244 | return Importer.getToContext().getVariableArrayType( | |||
1245 | ToElementType, ToSizeExpr, T->getSizeModifier(), | |||
1246 | T->getIndexTypeCVRQualifiers(), ToBracketsRange); | |||
1247 | } | |||
1248 | ||||
1249 | ExpectedType ASTNodeImporter::VisitDependentSizedArrayType( | |||
1250 | const DependentSizedArrayType *T) { | |||
1251 | Error Err = Error::success(); | |||
1252 | QualType ToElementType = importChecked(Err, T->getElementType()); | |||
1253 | Expr *ToSizeExpr = importChecked(Err, T->getSizeExpr()); | |||
1254 | SourceRange ToBracketsRange = importChecked(Err, T->getBracketsRange()); | |||
1255 | if (Err) | |||
1256 | return std::move(Err); | |||
1257 | // SizeExpr may be null if size is not specified directly. | |||
1258 | // For example, 'int a[]'. | |||
1259 | ||||
1260 | return Importer.getToContext().getDependentSizedArrayType( | |||
1261 | ToElementType, ToSizeExpr, T->getSizeModifier(), | |||
1262 | T->getIndexTypeCVRQualifiers(), ToBracketsRange); | |||
1263 | } | |||
1264 | ||||
1265 | ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) { | |||
1266 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); | |||
1267 | if (!ToElementTypeOrErr) | |||
1268 | return ToElementTypeOrErr.takeError(); | |||
1269 | ||||
1270 | return Importer.getToContext().getVectorType(*ToElementTypeOrErr, | |||
1271 | T->getNumElements(), | |||
1272 | T->getVectorKind()); | |||
1273 | } | |||
1274 | ||||
1275 | ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { | |||
1276 | ExpectedType ToElementTypeOrErr = import(T->getElementType()); | |||
1277 | if (!ToElementTypeOrErr) | |||
1278 | return ToElementTypeOrErr.takeError(); | |||
1279 | ||||
1280 | return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr, | |||
1281 | T->getNumElements()); | |||
1282 | } | |||
1283 | ||||
1284 | ExpectedType | |||
1285 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { | |||
1286 | // FIXME: What happens if we're importing a function without a prototype | |||
1287 | // into C++? Should we make it variadic? | |||
1288 | ExpectedType ToReturnTypeOrErr = import(T->getReturnType()); | |||
1289 | if (!ToReturnTypeOrErr) | |||
1290 | return ToReturnTypeOrErr.takeError(); | |||
1291 | ||||
1292 | return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr, | |||
1293 | T->getExtInfo()); | |||
1294 | } | |||
1295 | ||||
1296 | ExpectedType | |||
1297 | ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { | |||
1298 | ExpectedType ToReturnTypeOrErr = import(T->getReturnType()); | |||
1299 | if (!ToReturnTypeOrErr) | |||
1300 | return ToReturnTypeOrErr.takeError(); | |||
1301 | ||||
1302 | // Import argument types | |||
1303 | SmallVector<QualType, 4> ArgTypes; | |||
1304 | for (const auto &A : T->param_types()) { | |||
1305 | ExpectedType TyOrErr = import(A); | |||
1306 | if (!TyOrErr) | |||
1307 | return TyOrErr.takeError(); | |||
1308 | ArgTypes.push_back(*TyOrErr); | |||
1309 | } | |||
1310 | ||||
1311 | // Import exception types | |||
1312 | SmallVector<QualType, 4> ExceptionTypes; | |||
1313 | for (const auto &E : T->exceptions()) { | |||
1314 | ExpectedType TyOrErr = import(E); | |||
1315 | if (!TyOrErr) | |||
1316 | return TyOrErr.takeError(); | |||
1317 | ExceptionTypes.push_back(*TyOrErr); | |||
1318 | } | |||
1319 | ||||
1320 | FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); | |||
1321 | Error Err = Error::success(); | |||
1322 | FunctionProtoType::ExtProtoInfo ToEPI; | |||
1323 | ToEPI.ExtInfo = FromEPI.ExtInfo; | |||
1324 | ToEPI.Variadic = FromEPI.Variadic; | |||
1325 | ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; | |||
1326 | ToEPI.TypeQuals = FromEPI.TypeQuals; | |||
1327 | ToEPI.RefQualifier = FromEPI.RefQualifier; | |||
1328 | ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; | |||
1329 | ToEPI.ExceptionSpec.NoexceptExpr = | |||
1330 | importChecked(Err, FromEPI.ExceptionSpec.NoexceptExpr); | |||
1331 | ToEPI.ExceptionSpec.SourceDecl = | |||
1332 | importChecked(Err, FromEPI.ExceptionSpec.SourceDecl); | |||
1333 | ToEPI.ExceptionSpec.SourceTemplate = | |||
1334 | importChecked(Err, FromEPI.ExceptionSpec.SourceTemplate); | |||
1335 | ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; | |||
1336 | ||||
1337 | if (Err) | |||
1338 | return std::move(Err); | |||
1339 | ||||
1340 | return Importer.getToContext().getFunctionType( | |||
1341 | *ToReturnTypeOrErr, ArgTypes, ToEPI); | |||
1342 | } | |||
1343 | ||||
1344 | ExpectedType ASTNodeImporter::VisitUnresolvedUsingType( | |||
1345 | const UnresolvedUsingType *T) { | |||
1346 | Error Err = Error::success(); | |||
1347 | auto ToD = importChecked(Err, T->getDecl()); | |||
1348 | auto ToPrevD = importChecked(Err, T->getDecl()->getPreviousDecl()); | |||
1349 | if (Err) | |||
1350 | return std::move(Err); | |||
1351 | ||||
1352 | return Importer.getToContext().getTypeDeclType( | |||
1353 | ToD, cast_or_null<TypeDecl>(ToPrevD)); | |||
1354 | } | |||
1355 | ||||
1356 | ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) { | |||
1357 | ExpectedType ToInnerTypeOrErr = import(T->getInnerType()); | |||
1358 | if (!ToInnerTypeOrErr) | |||
1359 | return ToInnerTypeOrErr.takeError(); | |||
1360 | ||||
1361 | return Importer.getToContext().getParenType(*ToInnerTypeOrErr); | |||
1362 | } | |||
1363 | ||||
1364 | ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { | |||
1365 | Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl()); | |||
1366 | if (!ToDeclOrErr) | |||
1367 | return ToDeclOrErr.takeError(); | |||
1368 | ||||
1369 | TypedefNameDecl *ToDecl = *ToDeclOrErr; | |||
1370 | if (ToDecl->getTypeForDecl()) | |||
1371 | return QualType(ToDecl->getTypeForDecl(), 0); | |||
1372 | ||||
1373 | ExpectedType ToUnderlyingTypeOrErr = import(T->desugar()); | |||
1374 | if (!ToUnderlyingTypeOrErr) | |||
1375 | return ToUnderlyingTypeOrErr.takeError(); | |||
1376 | ||||
1377 | return Importer.getToContext().getTypedefType(ToDecl, *ToUnderlyingTypeOrErr); | |||
1378 | } | |||
1379 | ||||
1380 | ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { | |||
1381 | ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr()); | |||
1382 | if (!ToExprOrErr) | |||
1383 | return ToExprOrErr.takeError(); | |||
1384 | return Importer.getToContext().getTypeOfExprType(*ToExprOrErr, T->getKind()); | |||
1385 | } | |||
1386 | ||||
1387 | ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { | |||
1388 | ExpectedType ToUnderlyingTypeOrErr = import(T->getUnmodifiedType()); | |||
1389 | if (!ToUnderlyingTypeOrErr) | |||
1390 | return ToUnderlyingTypeOrErr.takeError(); | |||
1391 | return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr, | |||
1392 | T->getKind()); | |||
1393 | } | |||
1394 | ||||
1395 | ExpectedType ASTNodeImporter::VisitUsingType(const UsingType *T) { | |||
1396 | Expected<UsingShadowDecl *> FoundOrErr = import(T->getFoundDecl()); | |||
1397 | if (!FoundOrErr) | |||
1398 | return FoundOrErr.takeError(); | |||
1399 | Expected<QualType> UnderlyingOrErr = import(T->getUnderlyingType()); | |||
1400 | if (!UnderlyingOrErr) | |||
1401 | return UnderlyingOrErr.takeError(); | |||
1402 | ||||
1403 | return Importer.getToContext().getUsingType(*FoundOrErr, *UnderlyingOrErr); | |||
1404 | } | |||
1405 | ||||
1406 | ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { | |||
1407 | // FIXME: Make sure that the "to" context supports C++0x! | |||
1408 | ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr()); | |||
1409 | if (!ToExprOrErr) | |||
1410 | return ToExprOrErr.takeError(); | |||
1411 | ||||
1412 | ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType()); | |||
1413 | if (!ToUnderlyingTypeOrErr) | |||
1414 | return ToUnderlyingTypeOrErr.takeError(); | |||
1415 | ||||
1416 | return Importer.getToContext().getDecltypeType( | |||
1417 | *ToExprOrErr, *ToUnderlyingTypeOrErr); | |||
1418 | } | |||
1419 | ||||
1420 | ExpectedType | |||
1421 | ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { | |||
1422 | ExpectedType ToBaseTypeOrErr = import(T->getBaseType()); | |||
1423 | if (!ToBaseTypeOrErr) | |||
1424 | return ToBaseTypeOrErr.takeError(); | |||
1425 | ||||
1426 | ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType()); | |||
1427 | if (!ToUnderlyingTypeOrErr) | |||
1428 | return ToUnderlyingTypeOrErr.takeError(); | |||
1429 | ||||
1430 | return Importer.getToContext().getUnaryTransformType( | |||
1431 | *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind()); | |||
1432 | } | |||
1433 | ||||
1434 | ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) { | |||
1435 | // FIXME: Make sure that the "to" context supports C++11! | |||
1436 | ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType()); | |||
1437 | if (!ToDeducedTypeOrErr) | |||
1438 | return ToDeducedTypeOrErr.takeError(); | |||
1439 | ||||
1440 | ExpectedDecl ToTypeConstraintConcept = import(T->getTypeConstraintConcept()); | |||
1441 | if (!ToTypeConstraintConcept) | |||
1442 | return ToTypeConstraintConcept.takeError(); | |||
1443 | ||||
1444 | SmallVector<TemplateArgument, 2> ToTemplateArgs; | |||
1445 | if (Error Err = ImportTemplateArguments(T->getTypeConstraintArguments(), | |||
1446 | ToTemplateArgs)) | |||
1447 | return std::move(Err); | |||
1448 | ||||
1449 | return Importer.getToContext().getAutoType( | |||
1450 | *ToDeducedTypeOrErr, T->getKeyword(), /*IsDependent*/false, | |||
1451 | /*IsPack=*/false, cast_or_null<ConceptDecl>(*ToTypeConstraintConcept), | |||
1452 | ToTemplateArgs); | |||
1453 | } | |||
1454 | ||||
1455 | ExpectedType ASTNodeImporter::VisitDeducedTemplateSpecializationType( | |||
1456 | const DeducedTemplateSpecializationType *T) { | |||
1457 | // FIXME: Make sure that the "to" context supports C++17! | |||
1458 | Expected<TemplateName> ToTemplateNameOrErr = import(T->getTemplateName()); | |||
1459 | if (!ToTemplateNameOrErr) | |||
1460 | return ToTemplateNameOrErr.takeError(); | |||
1461 | ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType()); | |||
1462 | if (!ToDeducedTypeOrErr) | |||
1463 | return ToDeducedTypeOrErr.takeError(); | |||
1464 | ||||
1465 | return Importer.getToContext().getDeducedTemplateSpecializationType( | |||
1466 | *ToTemplateNameOrErr, *ToDeducedTypeOrErr, T->isDependentType()); | |||
1467 | } | |||
1468 | ||||
1469 | ExpectedType ASTNodeImporter::VisitInjectedClassNameType( | |||
1470 | const InjectedClassNameType *T) { | |||
1471 | Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl()); | |||
1472 | if (!ToDeclOrErr) | |||
1473 | return ToDeclOrErr.takeError(); | |||
1474 | ||||
1475 | // The InjectedClassNameType is created in VisitRecordDecl when the | |||
1476 | // T->getDecl() is imported. Here we can return the existing type. | |||
1477 | const Type *Ty = (*ToDeclOrErr)->getTypeForDecl(); | |||
1478 | assert(Ty && isa<InjectedClassNameType>(Ty))(static_cast <bool> (Ty && isa<InjectedClassNameType >(Ty)) ? void (0) : __assert_fail ("Ty && isa<InjectedClassNameType>(Ty)" , "clang/lib/AST/ASTImporter.cpp", 1478, __extension__ __PRETTY_FUNCTION__ )); | |||
1479 | return QualType(Ty, 0); | |||
1480 | } | |||
1481 | ||||
1482 | ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) { | |||
1483 | Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl()); | |||
1484 | if (!ToDeclOrErr) | |||
1485 | return ToDeclOrErr.takeError(); | |||
1486 | ||||
1487 | return Importer.getToContext().getTagDeclType(*ToDeclOrErr); | |||
1488 | } | |||
1489 | ||||
1490 | ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) { | |||
1491 | Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl()); | |||
1492 | if (!ToDeclOrErr) | |||
1493 | return ToDeclOrErr.takeError(); | |||
1494 | ||||
1495 | return Importer.getToContext().getTagDeclType(*ToDeclOrErr); | |||
1496 | } | |||
1497 | ||||
1498 | ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { | |||
1499 | ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType()); | |||
1500 | if (!ToModifiedTypeOrErr) | |||
1501 | return ToModifiedTypeOrErr.takeError(); | |||
1502 | ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType()); | |||
1503 | if (!ToEquivalentTypeOrErr) | |||
1504 | return ToEquivalentTypeOrErr.takeError(); | |||
1505 | ||||
1506 | return Importer.getToContext().getAttributedType(T->getAttrKind(), | |||
1507 | *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr); | |||
1508 | } | |||
1509 | ||||
1510 | ExpectedType ASTNodeImporter::VisitTemplateTypeParmType( | |||
1511 | const TemplateTypeParmType *T) { | |||
1512 | Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl()); | |||
1513 | if (!ToDeclOrErr) | |||
1514 | return ToDeclOrErr.takeError(); | |||
1515 | ||||
1516 | return Importer.getToContext().getTemplateTypeParmType( | |||
1517 | T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr); | |||
1518 | } | |||
1519 | ||||
1520 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType( | |||
1521 | const SubstTemplateTypeParmType *T) { | |||
1522 | Expected<Decl *> ReplacedOrErr = import(T->getAssociatedDecl()); | |||
1523 | if (!ReplacedOrErr) | |||
1524 | return ReplacedOrErr.takeError(); | |||
1525 | ||||
1526 | ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType()); | |||
1527 | if (!ToReplacementTypeOrErr) | |||
1528 | return ToReplacementTypeOrErr.takeError(); | |||
1529 | ||||
1530 | return Importer.getToContext().getSubstTemplateTypeParmType( | |||
1531 | *ToReplacementTypeOrErr, *ReplacedOrErr, T->getIndex(), | |||
1532 | T->getPackIndex()); | |||
1533 | } | |||
1534 | ||||
1535 | ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmPackType( | |||
1536 | const SubstTemplateTypeParmPackType *T) { | |||
1537 | Expected<Decl *> ReplacedOrErr = import(T->getAssociatedDecl()); | |||
1538 | if (!ReplacedOrErr) | |||
1539 | return ReplacedOrErr.takeError(); | |||
1540 | ||||
1541 | Expected<TemplateArgument> ToArgumentPack = import(T->getArgumentPack()); | |||
1542 | if (!ToArgumentPack) | |||
1543 | return ToArgumentPack.takeError(); | |||
1544 | ||||
1545 | return Importer.getToContext().getSubstTemplateTypeParmPackType( | |||
1546 | *ReplacedOrErr, T->getIndex(), T->getFinal(), *ToArgumentPack); | |||
1547 | } | |||
1548 | ||||
1549 | ExpectedType ASTNodeImporter::VisitTemplateSpecializationType( | |||
1550 | const TemplateSpecializationType *T) { | |||
1551 | auto ToTemplateOrErr = import(T->getTemplateName()); | |||
1552 | if (!ToTemplateOrErr) | |||
1553 | return ToTemplateOrErr.takeError(); | |||
1554 | ||||
1555 | SmallVector<TemplateArgument, 2> ToTemplateArgs; | |||
1556 | if (Error Err = | |||
1557 | ImportTemplateArguments(T->template_arguments(), ToTemplateArgs)) | |||
1558 | return std::move(Err); | |||
1559 | ||||
1560 | QualType ToCanonType; | |||
1561 | if (!T->isCanonicalUnqualified()) { | |||
1562 | QualType FromCanonType | |||
1563 | = Importer.getFromContext().getCanonicalType(QualType(T, 0)); | |||
1564 | if (ExpectedType TyOrErr = import(FromCanonType)) | |||
1565 | ToCanonType = *TyOrErr; | |||
1566 | else | |||
1567 | return TyOrErr.takeError(); | |||
1568 | } | |||
1569 | return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr, | |||
1570 | ToTemplateArgs, | |||
1571 | ToCanonType); | |||
1572 | } | |||
1573 | ||||
1574 | ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { | |||
1575 | // Note: the qualifier in an ElaboratedType is optional. | |||
1576 | auto ToQualifierOrErr = import(T->getQualifier()); | |||
1577 | if (!ToQualifierOrErr) | |||
1578 | return ToQualifierOrErr.takeError(); | |||
1579 | ||||
1580 | ExpectedType ToNamedTypeOrErr = import(T->getNamedType()); | |||
1581 | if (!ToNamedTypeOrErr) | |||
1582 | return ToNamedTypeOrErr.takeError(); | |||
1583 | ||||
1584 | Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl()); | |||
1585 | if (!ToOwnedTagDeclOrErr) | |||
1586 | return ToOwnedTagDeclOrErr.takeError(); | |||
1587 | ||||
1588 | return Importer.getToContext().getElaboratedType(T->getKeyword(), | |||
1589 | *ToQualifierOrErr, | |||
1590 | *ToNamedTypeOrErr, | |||
1591 | *ToOwnedTagDeclOrErr); | |||
1592 | } | |||
1593 | ||||
1594 | ExpectedType | |||
1595 | ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) { | |||
1596 | ExpectedType ToPatternOrErr = import(T->getPattern()); | |||
1597 | if (!ToPatternOrErr) | |||
1598 | return ToPatternOrErr.takeError(); | |||
1599 | ||||
1600 | return Importer.getToContext().getPackExpansionType(*ToPatternOrErr, | |||
1601 | T->getNumExpansions(), | |||
1602 | /*ExpactPack=*/false); | |||
1603 | } | |||
1604 | ||||
1605 | ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType( | |||
1606 | const DependentTemplateSpecializationType *T) { | |||
1607 | auto ToQualifierOrErr = import(T->getQualifier()); | |||
1608 | if (!ToQualifierOrErr) | |||
1609 | return ToQualifierOrErr.takeError(); | |||
1610 | ||||
1611 | IdentifierInfo *ToName = Importer.Import(T->getIdentifier()); | |||
1612 | ||||
1613 | SmallVector<TemplateArgument, 2> ToPack; | |||
1614 | ToPack.reserve(T->template_arguments().size()); | |||
1615 | if (Error Err = ImportTemplateArguments(T->template_arguments(), ToPack)) | |||
1616 | return std::move(Err); | |||
1617 | ||||
1618 | return Importer.getToContext().getDependentTemplateSpecializationType( | |||
1619 | T->getKeyword(), *ToQualifierOrErr, ToName, ToPack); | |||
1620 | } | |||
1621 | ||||
1622 | ExpectedType | |||
1623 | ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) { | |||
1624 | auto ToQualifierOrErr = import(T->getQualifier()); | |||
1625 | if (!ToQualifierOrErr) | |||
1626 | return ToQualifierOrErr.takeError(); | |||
1627 | ||||
1628 | IdentifierInfo *Name = Importer.Import(T->getIdentifier()); | |||
1629 | ||||
1630 | QualType Canon; | |||
1631 | if (T != T->getCanonicalTypeInternal().getTypePtr()) { | |||
1632 | if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal())) | |||
1633 | Canon = (*TyOrErr).getCanonicalType(); | |||
1634 | else | |||
1635 | return TyOrErr.takeError(); | |||
1636 | } | |||
1637 | ||||
1638 | return Importer.getToContext().getDependentNameType(T->getKeyword(), | |||
1639 | *ToQualifierOrErr, | |||
1640 | Name, Canon); | |||
1641 | } | |||
1642 | ||||
1643 | ExpectedType | |||
1644 | ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { | |||
1645 | Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl()); | |||
1646 | if (!ToDeclOrErr) | |||
1647 | return ToDeclOrErr.takeError(); | |||
1648 | ||||
1649 | return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr); | |||
1650 | } | |||
1651 | ||||
1652 | ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { | |||
1653 | ExpectedType ToBaseTypeOrErr = import(T->getBaseType()); | |||
1654 | if (!ToBaseTypeOrErr) | |||
1655 | return ToBaseTypeOrErr.takeError(); | |||
1656 | ||||
1657 | SmallVector<QualType, 4> TypeArgs; | |||
1658 | for (auto TypeArg : T->getTypeArgsAsWritten()) { | |||
1659 | if (ExpectedType TyOrErr = import(TypeArg)) | |||
1660 | TypeArgs.push_back(*TyOrErr); | |||
1661 | else | |||
1662 | return TyOrErr.takeError(); | |||
1663 | } | |||
1664 | ||||
1665 | SmallVector<ObjCProtocolDecl *, 4> Protocols; | |||
1666 | for (auto *P : T->quals()) { | |||
1667 | if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P)) | |||
1668 | Protocols.push_back(*ProtocolOrErr); | |||
1669 | else | |||
1670 | return ProtocolOrErr.takeError(); | |||
1671 | ||||
1672 | } | |||
1673 | ||||
1674 | return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs, | |||
1675 | Protocols, | |||
1676 | T->isKindOfTypeAsWritten()); | |||
1677 | } | |||
1678 | ||||
1679 | ExpectedType | |||
1680 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { | |||
1681 | ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType()); | |||
1682 | if (!ToPointeeTypeOrErr) | |||
1683 | return ToPointeeTypeOrErr.takeError(); | |||
1684 | ||||
1685 | return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr); | |||
1686 | } | |||
1687 | ||||
1688 | //---------------------------------------------------------------------------- | |||
1689 | // Import Declarations | |||
1690 | //---------------------------------------------------------------------------- | |||
1691 | Error ASTNodeImporter::ImportDeclParts( | |||
1692 | NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, | |||
1693 | DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) { | |||
1694 | // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop. | |||
1695 | // example: int struct_in_proto(struct data_t{int a;int b;} *d); | |||
1696 | // FIXME: We could support these constructs by importing a different type of | |||
1697 | // this parameter and by importing the original type of the parameter only | |||
1698 | // after the FunctionDecl is created. See | |||
1699 | // VisitFunctionDecl::UsedDifferentProtoType. | |||
1700 | DeclContext *OrigDC = D->getDeclContext(); | |||
1701 | FunctionDecl *FunDecl; | |||
1702 | if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) && | |||
1703 | FunDecl->hasBody()) { | |||
1704 | auto getLeafPointeeType = [](const Type *T) { | |||
1705 | while (T->isPointerType() || T->isArrayType()) { | |||
1706 | T = T->getPointeeOrArrayElementType(); | |||
1707 | } | |||
1708 | return T; | |||
1709 | }; | |||
1710 | for (const ParmVarDecl *P : FunDecl->parameters()) { | |||
1711 | const Type *LeafT = | |||
1712 | getLeafPointeeType(P->getType().getCanonicalType().getTypePtr()); | |||
1713 | auto *RT = dyn_cast<RecordType>(LeafT); | |||
1714 | if (RT && RT->getDecl() == D) { | |||
1715 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) | |||
1716 | << D->getDeclKindName(); | |||
1717 | return make_error<ASTImportError>(ASTImportError::UnsupportedConstruct); | |||
1718 | } | |||
1719 | } | |||
1720 | } | |||
1721 | ||||
1722 | // Import the context of this declaration. | |||
1723 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | |||
1724 | return Err; | |||
1725 | ||||
1726 | // Import the name of this declaration. | |||
1727 | if (Error Err = importInto(Name, D->getDeclName())) | |||
1728 | return Err; | |||
1729 | ||||
1730 | // Import the location of this declaration. | |||
1731 | if (Error Err = importInto(Loc, D->getLocation())) | |||
1732 | return Err; | |||
1733 | ||||
1734 | ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); | |||
1735 | if (ToD) | |||
1736 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD)) | |||
1737 | return Err; | |||
1738 | ||||
1739 | return Error::success(); | |||
1740 | } | |||
1741 | ||||
1742 | Error ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclarationName &Name, | |||
1743 | NamedDecl *&ToD, SourceLocation &Loc) { | |||
1744 | ||||
1745 | // Import the name of this declaration. | |||
1746 | if (Error Err = importInto(Name, D->getDeclName())) | |||
1747 | return Err; | |||
1748 | ||||
1749 | // Import the location of this declaration. | |||
1750 | if (Error Err = importInto(Loc, D->getLocation())) | |||
1751 | return Err; | |||
1752 | ||||
1753 | ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D)); | |||
1754 | if (ToD) | |||
1755 | if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD)) | |||
1756 | return Err; | |||
1757 | ||||
1758 | return Error::success(); | |||
1759 | } | |||
1760 | ||||
1761 | Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { | |||
1762 | if (!FromD) | |||
1763 | return Error::success(); | |||
1764 | ||||
1765 | if (!ToD) | |||
1766 | if (Error Err = importInto(ToD, FromD)) | |||
1767 | return Err; | |||
1768 | ||||
1769 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) { | |||
1770 | if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) { | |||
1771 | if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && | |||
1772 | !ToRecord->getDefinition()) { | |||
1773 | if (Error Err = ImportDefinition(FromRecord, ToRecord)) | |||
1774 | return Err; | |||
1775 | } | |||
1776 | } | |||
1777 | return Error::success(); | |||
1778 | } | |||
1779 | ||||
1780 | if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) { | |||
1781 | if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) { | |||
1782 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { | |||
1783 | if (Error Err = ImportDefinition(FromEnum, ToEnum)) | |||
1784 | return Err; | |||
1785 | } | |||
1786 | } | |||
1787 | return Error::success(); | |||
1788 | } | |||
1789 | ||||
1790 | return Error::success(); | |||
1791 | } | |||
1792 | ||||
1793 | Error | |||
1794 | ASTNodeImporter::ImportDeclarationNameLoc( | |||
1795 | const DeclarationNameInfo &From, DeclarationNameInfo& To) { | |||
1796 | // NOTE: To.Name and To.Loc are already imported. | |||
1797 | // We only have to import To.LocInfo. | |||
1798 | switch (To.getName().getNameKind()) { | |||
1799 | case DeclarationName::Identifier: | |||
1800 | case DeclarationName::ObjCZeroArgSelector: | |||
1801 | case DeclarationName::ObjCOneArgSelector: | |||
1802 | case DeclarationName::ObjCMultiArgSelector: | |||
1803 | case DeclarationName::CXXUsingDirective: | |||
1804 | case DeclarationName::CXXDeductionGuideName: | |||
1805 | return Error::success(); | |||
1806 | ||||
1807 | case DeclarationName::CXXOperatorName: { | |||
1808 | if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange())) | |||
1809 | To.setCXXOperatorNameRange(*ToRangeOrErr); | |||
1810 | else | |||
1811 | return ToRangeOrErr.takeError(); | |||
1812 | return Error::success(); | |||
1813 | } | |||
1814 | case DeclarationName::CXXLiteralOperatorName: { | |||
1815 | if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc())) | |||
1816 | To.setCXXLiteralOperatorNameLoc(*LocOrErr); | |||
1817 | else | |||
1818 | return LocOrErr.takeError(); | |||
1819 | return Error::success(); | |||
1820 | } | |||
1821 | case DeclarationName::CXXConstructorName: | |||
1822 | case DeclarationName::CXXDestructorName: | |||
1823 | case DeclarationName::CXXConversionFunctionName: { | |||
1824 | if (auto ToTInfoOrErr = import(From.getNamedTypeInfo())) | |||
1825 | To.setNamedTypeInfo(*ToTInfoOrErr); | |||
1826 | else | |||
1827 | return ToTInfoOrErr.takeError(); | |||
1828 | return Error::success(); | |||
1829 | } | |||
1830 | } | |||
1831 | llvm_unreachable("Unknown name kind.")::llvm::llvm_unreachable_internal("Unknown name kind.", "clang/lib/AST/ASTImporter.cpp" , 1831); | |||
1832 | } | |||
1833 | ||||
1834 | Error | |||
1835 | ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { | |||
1836 | if (Importer.isMinimalImport() && !ForceImport) { | |||
1837 | auto ToDCOrErr = Importer.ImportContext(FromDC); | |||
1838 | return ToDCOrErr.takeError(); | |||
1839 | } | |||
1840 | ||||
1841 | // We use strict error handling in case of records and enums, but not | |||
1842 | // with e.g. namespaces. | |||
1843 | // | |||
1844 | // FIXME Clients of the ASTImporter should be able to choose an | |||
1845 | // appropriate error handling strategy for their needs. For instance, | |||
1846 | // they may not want to mark an entire namespace as erroneous merely | |||
1847 | // because there is an ODR error with two typedefs. As another example, | |||
1848 | // the client may allow EnumConstantDecls with same names but with | |||
1849 | // different values in two distinct translation units. | |||
1850 | ChildErrorHandlingStrategy HandleChildErrors(FromDC); | |||
1851 | ||||
1852 | Error ChildErrors = Error::success(); | |||
1853 | for (auto *From : FromDC->decls()) { | |||
1854 | ExpectedDecl ImportedOrErr = import(From); | |||
1855 | ||||
1856 | // If we are in the process of ImportDefinition(...) for a RecordDecl we | |||
1857 | // want to make sure that we are also completing each FieldDecl. There | |||
1858 | // are currently cases where this does not happen and this is correctness | |||
1859 | // fix since operations such as code generation will expect this to be so. | |||
1860 | if (ImportedOrErr) { | |||
1861 | FieldDecl *FieldFrom = dyn_cast_or_null<FieldDecl>(From); | |||
1862 | Decl *ImportedDecl = *ImportedOrErr; | |||
1863 | FieldDecl *FieldTo = dyn_cast_or_null<FieldDecl>(ImportedDecl); | |||
1864 | if (FieldFrom && FieldTo) { | |||
1865 | RecordDecl *FromRecordDecl = nullptr; | |||
1866 | RecordDecl *ToRecordDecl = nullptr; | |||
1867 | // If we have a field that is an ArrayType we need to check if the array | |||
1868 | // element is a RecordDecl and if so we need to import the definition. | |||
1869 | if (FieldFrom->getType()->isArrayType()) { | |||
1870 | // getBaseElementTypeUnsafe(...) handles multi-dimensonal arrays for us. | |||
1871 | FromRecordDecl = FieldFrom->getType()->getBaseElementTypeUnsafe()->getAsRecordDecl(); | |||
1872 | ToRecordDecl = FieldTo->getType()->getBaseElementTypeUnsafe()->getAsRecordDecl(); | |||
1873 | } | |||
1874 | ||||
1875 | if (!FromRecordDecl || !ToRecordDecl) { | |||
1876 | const RecordType *RecordFrom = | |||
1877 | FieldFrom->getType()->getAs<RecordType>(); | |||
1878 | const RecordType *RecordTo = FieldTo->getType()->getAs<RecordType>(); | |||
1879 | ||||
1880 | if (RecordFrom && RecordTo) { | |||
1881 | FromRecordDecl = RecordFrom->getDecl(); | |||
1882 | ToRecordDecl = RecordTo->getDecl(); | |||
1883 | } | |||
1884 | } | |||
1885 | ||||
1886 | if (FromRecordDecl && ToRecordDecl) { | |||
1887 | if (FromRecordDecl->isCompleteDefinition() && | |||
1888 | !ToRecordDecl->isCompleteDefinition()) { | |||
1889 | Error Err = ImportDefinition(FromRecordDecl, ToRecordDecl); | |||
1890 | HandleChildErrors.handleChildImportResult(ChildErrors, | |||
1891 | std::move(Err)); | |||
1892 | } | |||
1893 | } | |||
1894 | } | |||
1895 | } else { | |||
1896 | HandleChildErrors.handleChildImportResult(ChildErrors, | |||
1897 | ImportedOrErr.takeError()); | |||
1898 | } | |||
1899 | } | |||
1900 | ||||
1901 | // We reorder declarations in RecordDecls because they may have another order | |||
1902 | // in the "to" context than they have in the "from" context. This may happen | |||
1903 | // e.g when we import a class like this: | |||
1904 | // struct declToImport { | |||
1905 | // int a = c + b; | |||
1906 | // int b = 1; | |||
1907 | // int c = 2; | |||
1908 | // }; | |||
1909 | // During the import of `a` we import first the dependencies in sequence, | |||
1910 | // thus the order would be `c`, `b`, `a`. We will get the normal order by | |||
1911 | // first removing the already imported members and then adding them in the | |||
1912 | // order as they apper in the "from" context. | |||
1913 | // | |||
1914 | // Keeping field order is vital because it determines structure layout. | |||
1915 | // | |||
1916 | // Here and below, we cannot call field_begin() method and its callers on | |||
1917 | // ToDC if it has an external storage. Calling field_begin() will | |||
1918 | // automatically load all the fields by calling | |||
1919 | // LoadFieldsFromExternalStorage(). LoadFieldsFromExternalStorage() would | |||
1920 | // call ASTImporter::Import(). This is because the ExternalASTSource | |||
1921 | // interface in LLDB is implemented by the means of the ASTImporter. However, | |||
1922 | // calling an import at this point would result in an uncontrolled import, we | |||
1923 | // must avoid that. | |||
1924 | const auto *FromRD = dyn_cast<RecordDecl>(FromDC); | |||
1925 | if (!FromRD) | |||
1926 | return ChildErrors; | |||
1927 | ||||
1928 | auto ToDCOrErr = Importer.ImportContext(FromDC); | |||
1929 | if (!ToDCOrErr) { | |||
1930 | consumeError(std::move(ChildErrors)); | |||
1931 | return ToDCOrErr.takeError(); | |||
1932 | } | |||
1933 | ||||
1934 | DeclContext *ToDC = *ToDCOrErr; | |||
1935 | // Remove all declarations, which may be in wrong order in the | |||
1936 | // lexical DeclContext and then add them in the proper order. | |||
1937 | for (auto *D : FromRD->decls()) { | |||
1938 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<FriendDecl>(D)) { | |||
1939 | assert(D && "DC contains a null decl")(static_cast <bool> (D && "DC contains a null decl" ) ? void (0) : __assert_fail ("D && \"DC contains a null decl\"" , "clang/lib/AST/ASTImporter.cpp", 1939, __extension__ __PRETTY_FUNCTION__ )); | |||
1940 | Decl *ToD = Importer.GetAlreadyImportedOrNull(D); | |||
1941 | // Remove only the decls which we successfully imported. | |||
1942 | if (ToD) { | |||
1943 | assert(ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD))(static_cast <bool> (ToDC == ToD->getLexicalDeclContext () && ToDC->containsDecl(ToD)) ? void (0) : __assert_fail ("ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD)" , "clang/lib/AST/ASTImporter.cpp", 1943, __extension__ __PRETTY_FUNCTION__ )); | |||
1944 | // Remove the decl from its wrong place in the linked list. | |||
1945 | ToDC->removeDecl(ToD); | |||
1946 | // Add the decl to the end of the linked list. | |||
1947 | // This time it will be at the proper place because the enclosing for | |||
1948 | // loop iterates in the original (good) order of the decls. | |||
1949 | ToDC->addDeclInternal(ToD); | |||
1950 | } | |||
1951 | } | |||
1952 | } | |||
1953 | ||||
1954 | return ChildErrors; | |||
1955 | } | |||
1956 | ||||
1957 | Error ASTNodeImporter::ImportDeclContext( | |||
1958 | Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) { | |||
1959 | auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext()); | |||
1960 | if (!ToDCOrErr) | |||
1961 | return ToDCOrErr.takeError(); | |||
1962 | ToDC = *ToDCOrErr; | |||
1963 | ||||
1964 | if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) { | |||
1965 | auto ToLexicalDCOrErr = Importer.ImportContext( | |||
1966 | FromD->getLexicalDeclContext()); | |||
1967 | if (!ToLexicalDCOrErr) | |||
1968 | return ToLexicalDCOrErr.takeError(); | |||
1969 | ToLexicalDC = *ToLexicalDCOrErr; | |||
1970 | } else | |||
1971 | ToLexicalDC = ToDC; | |||
1972 | ||||
1973 | return Error::success(); | |||
1974 | } | |||
1975 | ||||
1976 | Error ASTNodeImporter::ImportImplicitMethods( | |||
1977 | const CXXRecordDecl *From, CXXRecordDecl *To) { | |||
1978 | assert(From->isCompleteDefinition() && To->getDefinition() == To &&(static_cast <bool> (From->isCompleteDefinition() && To->getDefinition() == To && "Import implicit methods to or from non-definition" ) ? void (0) : __assert_fail ("From->isCompleteDefinition() && To->getDefinition() == To && \"Import implicit methods to or from non-definition\"" , "clang/lib/AST/ASTImporter.cpp", 1979, __extension__ __PRETTY_FUNCTION__ )) | |||
1979 | "Import implicit methods to or from non-definition")(static_cast <bool> (From->isCompleteDefinition() && To->getDefinition() == To && "Import implicit methods to or from non-definition" ) ? void (0) : __assert_fail ("From->isCompleteDefinition() && To->getDefinition() == To && \"Import implicit methods to or from non-definition\"" , "clang/lib/AST/ASTImporter.cpp", 1979, __extension__ __PRETTY_FUNCTION__ )); | |||
1980 | ||||
1981 | for (CXXMethodDecl *FromM : From->methods()) | |||
1982 | if (FromM->isImplicit()) { | |||
1983 | Expected<CXXMethodDecl *> ToMOrErr = import(FromM); | |||
1984 | if (!ToMOrErr) | |||
1985 | return ToMOrErr.takeError(); | |||
1986 | } | |||
1987 | ||||
1988 | return Error::success(); | |||
1989 | } | |||
1990 | ||||
1991 | static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To, | |||
1992 | ASTImporter &Importer) { | |||
1993 | if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) { | |||
1994 | if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef)) | |||
1995 | To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr)); | |||
1996 | else | |||
1997 | return ToTypedefOrErr.takeError(); | |||
1998 | } | |||
1999 | return Error::success(); | |||
2000 | } | |||
2001 | ||||
2002 | Error ASTNodeImporter::ImportDefinition( | |||
2003 | RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) { | |||
2004 | auto DefinitionCompleter = [To]() { | |||
2005 | // There are cases in LLDB when we first import a class without its | |||
2006 | // members. The class will have DefinitionData, but no members. Then, | |||
2007 | // importDefinition is called from LLDB, which tries to get the members, so | |||
2008 | // when we get here, the class already has the DefinitionData set, so we | |||
2009 | // must unset the CompleteDefinition here to be able to complete again the | |||
2010 | // definition. | |||
2011 | To->setCompleteDefinition(false); | |||
2012 | To->completeDefinition(); | |||
2013 | }; | |||
2014 | ||||
2015 | if (To->getDefinition() || To->isBeingDefined()) { | |||
2016 | if (Kind == IDK_Everything || | |||
2017 | // In case of lambdas, the class already has a definition ptr set, but | |||
2018 | // the contained decls are not imported yet. Also, isBeingDefined was | |||
2019 | // set in CXXRecordDecl::CreateLambda. We must import the contained | |||
2020 | // decls here and finish the definition. | |||
2021 | (To->isLambda() && shouldForceImportDeclContext(Kind))) { | |||
2022 | if (To->isLambda()) { | |||
2023 | auto *FromCXXRD = cast<CXXRecordDecl>(From); | |||
2024 | SmallVector<LambdaCapture, 8> ToCaptures; | |||
2025 | ToCaptures.reserve(FromCXXRD->capture_size()); | |||
2026 | for (const auto &FromCapture : FromCXXRD->captures()) { | |||
2027 | if (auto ToCaptureOrErr = import(FromCapture)) | |||
2028 | ToCaptures.push_back(*ToCaptureOrErr); | |||
2029 | else | |||
2030 | return ToCaptureOrErr.takeError(); | |||
2031 | } | |||
2032 | cast<CXXRecordDecl>(To)->setCaptures(Importer.getToContext(), | |||
2033 | ToCaptures); | |||
2034 | } | |||
2035 | ||||
2036 | Error Result = ImportDeclContext(From, /*ForceImport=*/true); | |||
2037 | // Finish the definition of the lambda, set isBeingDefined to false. | |||
2038 | if (To->isLambda()) | |||
2039 | DefinitionCompleter(); | |||
2040 | return Result; | |||
2041 | } | |||
2042 | ||||
2043 | return Error::success(); | |||
2044 | } | |||
2045 | ||||
2046 | To->startDefinition(); | |||
2047 | // Set the definition to complete even if it is really not complete during | |||
2048 | // import. Some AST constructs (expressions) require the record layout | |||
2049 | // to be calculated (see 'clang::computeDependence') at the time they are | |||
2050 | // constructed. Import of such AST node is possible during import of the | |||
2051 | // same record, there is no way to have a completely defined record (all | |||
2052 | // fields imported) at that time without multiple AST import passes. | |||
2053 | if (!Importer.isMinimalImport()) | |||
2054 | To->setCompleteDefinition(true); | |||
2055 | // Complete the definition even if error is returned. | |||
2056 | // The RecordDecl may be already part of the AST so it is better to | |||
2057 | // have it in complete state even if something is wrong with it. | |||
2058 | auto DefinitionCompleterScopeExit = | |||
2059 | llvm::make_scope_exit(DefinitionCompleter); | |||
2060 | ||||
2061 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) | |||
2062 | return Err; | |||
2063 | ||||
2064 | // Add base classes. | |||
2065 | auto *ToCXX = dyn_cast<CXXRecordDecl>(To); | |||
2066 | auto *FromCXX = dyn_cast<CXXRecordDecl>(From); | |||
2067 | if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) { | |||
2068 | ||||
2069 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); | |||
2070 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); | |||
2071 | ||||
2072 | #define FIELD(Name, Width, Merge) \ | |||
2073 | ToData.Name = FromData.Name; | |||
2074 | #include "clang/AST/CXXRecordDeclDefinitionBits.def" | |||
2075 | ||||
2076 | // Copy over the data stored in RecordDeclBits | |||
2077 | ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions()); | |||
2078 | ||||
2079 | SmallVector<CXXBaseSpecifier *, 4> Bases; | |||
2080 | for (const auto &Base1 : FromCXX->bases()) { | |||
2081 | ExpectedType TyOrErr = import(Base1.getType()); | |||
2082 | if (!TyOrErr) | |||
2083 | return TyOrErr.takeError(); | |||
2084 | ||||
2085 | SourceLocation EllipsisLoc; | |||
2086 | if (Base1.isPackExpansion()) { | |||
2087 | if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc())) | |||
2088 | EllipsisLoc = *LocOrErr; | |||
2089 | else | |||
2090 | return LocOrErr.takeError(); | |||
2091 | } | |||
2092 | ||||
2093 | // Ensure that we have a definition for the base. | |||
2094 | if (Error Err = | |||
2095 | ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl())) | |||
2096 | return Err; | |||
2097 | ||||
2098 | auto RangeOrErr = import(Base1.getSourceRange()); | |||
2099 | if (!RangeOrErr) | |||
2100 | return RangeOrErr.takeError(); | |||
2101 | ||||
2102 | auto TSIOrErr = import(Base1.getTypeSourceInfo()); | |||
2103 | if (!TSIOrErr) | |||
2104 | return TSIOrErr.takeError(); | |||
2105 | ||||
2106 | Bases.push_back( | |||
2107 | new (Importer.getToContext()) CXXBaseSpecifier( | |||
2108 | *RangeOrErr, | |||
2109 | Base1.isVirtual(), | |||
2110 | Base1.isBaseOfClass(), | |||
2111 | Base1.getAccessSpecifierAsWritten(), | |||
2112 | *TSIOrErr, | |||
2113 | EllipsisLoc)); | |||
2114 | } | |||
2115 | if (!Bases.empty()) | |||
2116 | ToCXX->setBases(Bases.data(), Bases.size()); | |||
2117 | } | |||
2118 | ||||
2119 | if (shouldForceImportDeclContext(Kind)) { | |||
2120 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) | |||
2121 | return Err; | |||
2122 | } | |||
2123 | ||||
2124 | return Error::success(); | |||
2125 | } | |||
2126 | ||||
2127 | Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) { | |||
2128 | if (To->getAnyInitializer()) | |||
2129 | return Error::success(); | |||
2130 | ||||
2131 | Expr *FromInit = From->getInit(); | |||
2132 | if (!FromInit) | |||
2133 | return Error::success(); | |||
2134 | ||||
2135 | ExpectedExpr ToInitOrErr = import(FromInit); | |||
2136 | if (!ToInitOrErr) | |||
2137 | return ToInitOrErr.takeError(); | |||
2138 | ||||
2139 | To->setInit(*ToInitOrErr); | |||
2140 | if (EvaluatedStmt *FromEval = From->getEvaluatedStmt()) { | |||
2141 | EvaluatedStmt *ToEval = To->ensureEvaluatedStmt(); | |||
2142 | ToEval->HasConstantInitialization = FromEval->HasConstantInitialization; | |||
2143 | ToEval->HasConstantDestruction = FromEval->HasConstantDestruction; | |||
2144 | // FIXME: Also import the initializer value. | |||
2145 | } | |||
2146 | ||||
2147 | // FIXME: Other bits to merge? | |||
2148 | return Error::success(); | |||
2149 | } | |||
2150 | ||||
2151 | Error ASTNodeImporter::ImportDefinition( | |||
2152 | EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) { | |||
2153 | if (To->getDefinition() || To->isBeingDefined()) { | |||
2154 | if (Kind == IDK_Everything) | |||
2155 | return ImportDeclContext(From, /*ForceImport=*/true); | |||
2156 | return Error::success(); | |||
2157 | } | |||
2158 | ||||
2159 | To->startDefinition(); | |||
2160 | ||||
2161 | if (Error Err = setTypedefNameForAnonDecl(From, To, Importer)) | |||
2162 | return Err; | |||
2163 | ||||
2164 | ExpectedType ToTypeOrErr = | |||
2165 | import(Importer.getFromContext().getTypeDeclType(From)); | |||
2166 | if (!ToTypeOrErr) | |||
2167 | return ToTypeOrErr.takeError(); | |||
2168 | ||||
2169 | ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType()); | |||
2170 | if (!ToPromotionTypeOrErr) | |||
2171 | return ToPromotionTypeOrErr.takeError(); | |||
2172 | ||||
2173 | if (shouldForceImportDeclContext(Kind)) | |||
2174 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) | |||
2175 | return Err; | |||
2176 | ||||
2177 | // FIXME: we might need to merge the number of positive or negative bits | |||
2178 | // if the enumerator lists don't match. | |||
2179 | To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr, | |||
2180 | From->getNumPositiveBits(), | |||
2181 | From->getNumNegativeBits()); | |||
2182 | return Error::success(); | |||
2183 | } | |||
2184 | ||||
2185 | Error ASTNodeImporter::ImportTemplateArguments( | |||
2186 | ArrayRef<TemplateArgument> FromArgs, | |||
2187 | SmallVectorImpl<TemplateArgument> &ToArgs) { | |||
2188 | for (const auto &Arg : FromArgs) { | |||
2189 | if (auto ToOrErr = import(Arg)) | |||
2190 | ToArgs.push_back(*ToOrErr); | |||
2191 | else | |||
2192 | return ToOrErr.takeError(); | |||
2193 | } | |||
2194 | ||||
2195 | return Error::success(); | |||
2196 | } | |||
2197 | ||||
2198 | // FIXME: Do not forget to remove this and use only 'import'. | |||
2199 | Expected<TemplateArgument> | |||
2200 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { | |||
2201 | return import(From); | |||
2202 | } | |||
2203 | ||||
2204 | template <typename InContainerTy> | |||
2205 | Error ASTNodeImporter::ImportTemplateArgumentListInfo( | |||
2206 | const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) { | |||
2207 | for (const auto &FromLoc : Container) { | |||
2208 | if (auto ToLocOrErr = import(FromLoc)) | |||
2209 | ToTAInfo.addArgument(*ToLocOrErr); | |||
2210 | else | |||
2211 | return ToLocOrErr.takeError(); | |||
2212 | } | |||
2213 | return Error::success(); | |||
2214 | } | |||
2215 | ||||
2216 | static StructuralEquivalenceKind | |||
2217 | getStructuralEquivalenceKind(const ASTImporter &Importer) { | |||
2218 | return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal | |||
2219 | : StructuralEquivalenceKind::Default; | |||
2220 | } | |||
2221 | ||||
2222 | bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) { | |||
2223 | // Eliminate a potential failure point where we attempt to re-import | |||
2224 | // something we're trying to import while completing ToRecord. | |||
2225 | Decl *ToOrigin = Importer.GetOriginalDecl(To); | |||
2226 | if (ToOrigin) { | |||
2227 | To = ToOrigin; | |||
2228 | } | |||
2229 | ||||
2230 | StructuralEquivalenceContext Ctx( | |||
2231 | Importer.getFromContext(), Importer.getToContext(), | |||
2232 | Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer), | |||
2233 | false, Complain); | |||
2234 | return Ctx.IsEquivalent(From, To); | |||
2235 | } | |||
2236 | ||||
2237 | ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) { | |||
2238 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) | |||
2239 | << D->getDeclKindName(); | |||
2240 | return make_error<ASTImportError>(ASTImportError::UnsupportedConstruct); | |||
2241 | } | |||
2242 | ||||
2243 | ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) { | |||
2244 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) | |||
2245 | << D->getDeclKindName(); | |||
2246 | return make_error<ASTImportError>(ASTImportError::UnsupportedConstruct); | |||
2247 | } | |||
2248 | ||||
2249 | ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) { | |||
2250 | // Import the context of this declaration. | |||
2251 | DeclContext *DC, *LexicalDC; | |||
2252 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | |||
2253 | return std::move(Err); | |||
2254 | ||||
2255 | // Import the location of this declaration. | |||
2256 | ExpectedSLoc LocOrErr = import(D->getLocation()); | |||
2257 | if (!LocOrErr) | |||
2258 | return LocOrErr.takeError(); | |||
2259 | ||||
2260 | EmptyDecl *ToD; | |||
2261 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr)) | |||
2262 | return ToD; | |||
2263 | ||||
2264 | ToD->setLexicalDeclContext(LexicalDC); | |||
2265 | LexicalDC->addDeclInternal(ToD); | |||
2266 | return ToD; | |||
2267 | } | |||
2268 | ||||
2269 | ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { | |||
2270 | TranslationUnitDecl *ToD = | |||
2271 | Importer.getToContext().getTranslationUnitDecl(); | |||
2272 | ||||
2273 | Importer.MapImported(D, ToD); | |||
2274 | ||||
2275 | return ToD; | |||
2276 | } | |||
2277 | ||||
2278 | ExpectedDecl ASTNodeImporter::VisitBindingDecl(BindingDecl *D) { | |||
2279 | DeclContext *DC, *LexicalDC; | |||
2280 | DeclarationName Name; | |||
2281 | SourceLocation Loc; | |||
2282 | NamedDecl *ToND; | |||
2283 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToND, Loc)) | |||
2284 | return std::move(Err); | |||
2285 | if (ToND) | |||
2286 | return ToND; | |||
2287 | ||||
2288 | BindingDecl *ToD; | |||
2289 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, Loc, | |||
2290 | Name.getAsIdentifierInfo())) | |||
2291 | return ToD; | |||
2292 | ||||
2293 | Error Err = Error::success(); | |||
2294 | QualType ToType = importChecked(Err, D->getType()); | |||
2295 | Expr *ToBinding = importChecked(Err, D->getBinding()); | |||
2296 | ValueDecl *ToDecomposedDecl = importChecked(Err, D->getDecomposedDecl()); | |||
2297 | if (Err) | |||
2298 | return std::move(Err); | |||
2299 | ||||
2300 | ToD->setBinding(ToType, ToBinding); | |||
2301 | ToD->setDecomposedDecl(ToDecomposedDecl); | |||
2302 | addDeclToContexts(D, ToD); | |||
2303 | ||||
2304 | return ToD; | |||
2305 | } | |||
2306 | ||||
2307 | ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { | |||
2308 | ExpectedSLoc LocOrErr = import(D->getLocation()); | |||
2309 | if (!LocOrErr) | |||
2310 | return LocOrErr.takeError(); | |||
2311 | auto ColonLocOrErr = import(D->getColonLoc()); | |||
2312 | if (!ColonLocOrErr) | |||
2313 | return ColonLocOrErr.takeError(); | |||
2314 | ||||
2315 | // Import the context of this declaration. | |||
2316 | auto DCOrErr = Importer.ImportContext(D->getDeclContext()); | |||
2317 | if (!DCOrErr) | |||
2318 | return DCOrErr.takeError(); | |||
2319 | DeclContext *DC = *DCOrErr; | |||
2320 | ||||
2321 | AccessSpecDecl *ToD; | |||
2322 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(), | |||
2323 | DC, *LocOrErr, *ColonLocOrErr)) | |||
2324 | return ToD; | |||
2325 | ||||
2326 | // Lexical DeclContext and Semantic DeclContext | |||
2327 | // is always the same for the accessSpec. | |||
2328 | ToD->setLexicalDeclContext(DC); | |||
2329 | DC->addDeclInternal(ToD); | |||
2330 | ||||
2331 | return ToD; | |||
2332 | } | |||
2333 | ||||
2334 | ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) { | |||
2335 | auto DCOrErr = Importer.ImportContext(D->getDeclContext()); | |||
2336 | if (!DCOrErr) | |||
2337 | return DCOrErr.takeError(); | |||
2338 | DeclContext *DC = *DCOrErr; | |||
2339 | DeclContext *LexicalDC = DC; | |||
2340 | ||||
2341 | Error Err = Error::success(); | |||
2342 | auto ToLocation = importChecked(Err, D->getLocation()); | |||
2343 | auto ToRParenLoc = importChecked(Err, D->getRParenLoc()); | |||
2344 | auto ToAssertExpr = importChecked(Err, D->getAssertExpr()); | |||
2345 | auto ToMessage = importChecked(Err, D->getMessage()); | |||
2346 | if (Err) | |||
2347 | return std::move(Err); | |||
2348 | ||||
2349 | StaticAssertDecl *ToD; | |||
2350 | if (GetImportedOrCreateDecl( | |||
2351 | ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage, | |||
2352 | ToRParenLoc, D->isFailed())) | |||
2353 | return ToD; | |||
2354 | ||||
2355 | ToD->setLexicalDeclContext(LexicalDC); | |||
2356 | LexicalDC->addDeclInternal(ToD); | |||
2357 | return ToD; | |||
2358 | } | |||
2359 | ||||
2360 | ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { | |||
2361 | // Import the major distinguishing characteristics of this namespace. | |||
2362 | DeclContext *DC, *LexicalDC; | |||
2363 | DeclarationName Name; | |||
2364 | SourceLocation Loc; | |||
2365 | NamedDecl *ToD; | |||
2366 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
2367 | return std::move(Err); | |||
2368 | if (ToD) | |||
2369 | return ToD; | |||
2370 | ||||
2371 | NamespaceDecl *MergeWithNamespace = nullptr; | |||
2372 | if (!Name) { | |||
2373 | // This is an anonymous namespace. Adopt an existing anonymous | |||
2374 | // namespace if we can. | |||
2375 | // FIXME: Not testable. | |||
2376 | if (auto *TU = dyn_cast<TranslationUnitDecl>(DC)) | |||
2377 | MergeWithNamespace = TU->getAnonymousNamespace(); | |||
2378 | else | |||
2379 | MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); | |||
2380 | } else { | |||
2381 | SmallVector<NamedDecl *, 4> ConflictingDecls; | |||
2382 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
2383 | for (auto *FoundDecl : FoundDecls) { | |||
2384 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace)) | |||
2385 | continue; | |||
2386 | ||||
2387 | if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) { | |||
2388 | MergeWithNamespace = FoundNS; | |||
2389 | ConflictingDecls.clear(); | |||
2390 | break; | |||
2391 | } | |||
2392 | ||||
2393 | ConflictingDecls.push_back(FoundDecl); | |||
2394 | } | |||
2395 | ||||
2396 | if (!ConflictingDecls.empty()) { | |||
2397 | ExpectedName NameOrErr = Importer.HandleNameConflict( | |||
2398 | Name, DC, Decl::IDNS_Namespace, ConflictingDecls.data(), | |||
2399 | ConflictingDecls.size()); | |||
2400 | if (NameOrErr) | |||
2401 | Name = NameOrErr.get(); | |||
2402 | else | |||
2403 | return NameOrErr.takeError(); | |||
2404 | } | |||
2405 | } | |||
2406 | ||||
2407 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | |||
2408 | if (!BeginLocOrErr) | |||
2409 | return BeginLocOrErr.takeError(); | |||
2410 | ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc()); | |||
2411 | if (!RBraceLocOrErr) | |||
2412 | return RBraceLocOrErr.takeError(); | |||
2413 | ||||
2414 | // Create the "to" namespace, if needed. | |||
2415 | NamespaceDecl *ToNamespace = MergeWithNamespace; | |||
2416 | if (!ToNamespace) { | |||
2417 | if (GetImportedOrCreateDecl(ToNamespace, D, Importer.getToContext(), DC, | |||
2418 | D->isInline(), *BeginLocOrErr, Loc, | |||
2419 | Name.getAsIdentifierInfo(), | |||
2420 | /*PrevDecl=*/nullptr, D->isNested())) | |||
2421 | return ToNamespace; | |||
2422 | ToNamespace->setRBraceLoc(*RBraceLocOrErr); | |||
2423 | ToNamespace->setLexicalDeclContext(LexicalDC); | |||
2424 | LexicalDC->addDeclInternal(ToNamespace); | |||
2425 | ||||
2426 | // If this is an anonymous namespace, register it as the anonymous | |||
2427 | // namespace within its context. | |||
2428 | if (!Name) { | |||
2429 | if (auto *TU = dyn_cast<TranslationUnitDecl>(DC)) | |||
2430 | TU->setAnonymousNamespace(ToNamespace); | |||
2431 | else | |||
2432 | cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); | |||
2433 | } | |||
2434 | } | |||
2435 | Importer.MapImported(D, ToNamespace); | |||
2436 | ||||
2437 | if (Error Err = ImportDeclContext(D)) | |||
2438 | return std::move(Err); | |||
2439 | ||||
2440 | return ToNamespace; | |||
2441 | } | |||
2442 | ||||
2443 | ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { | |||
2444 | // Import the major distinguishing characteristics of this namespace. | |||
2445 | DeclContext *DC, *LexicalDC; | |||
2446 | DeclarationName Name; | |||
2447 | SourceLocation Loc; | |||
2448 | NamedDecl *LookupD; | |||
2449 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc)) | |||
2450 | return std::move(Err); | |||
2451 | if (LookupD) | |||
2452 | return LookupD; | |||
2453 | ||||
2454 | // NOTE: No conflict resolution is done for namespace aliases now. | |||
2455 | ||||
2456 | Error Err = Error::success(); | |||
2457 | auto ToNamespaceLoc = importChecked(Err, D->getNamespaceLoc()); | |||
2458 | auto ToAliasLoc = importChecked(Err, D->getAliasLoc()); | |||
2459 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | |||
2460 | auto ToTargetNameLoc = importChecked(Err, D->getTargetNameLoc()); | |||
2461 | auto ToNamespace = importChecked(Err, D->getNamespace()); | |||
2462 | if (Err) | |||
2463 | return std::move(Err); | |||
2464 | ||||
2465 | IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier()); | |||
2466 | ||||
2467 | NamespaceAliasDecl *ToD; | |||
2468 | if (GetImportedOrCreateDecl( | |||
2469 | ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc, | |||
2470 | ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace)) | |||
2471 | return ToD; | |||
2472 | ||||
2473 | ToD->setLexicalDeclContext(LexicalDC); | |||
2474 | LexicalDC->addDeclInternal(ToD); | |||
2475 | ||||
2476 | return ToD; | |||
2477 | } | |||
2478 | ||||
2479 | ExpectedDecl | |||
2480 | ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { | |||
2481 | // Import the major distinguishing characteristics of this typedef. | |||
2482 | DeclarationName Name; | |||
2483 | SourceLocation Loc; | |||
2484 | NamedDecl *ToD; | |||
2485 | // Do not import the DeclContext, we will import it once the TypedefNameDecl | |||
2486 | // is created. | |||
2487 | if (Error Err = ImportDeclParts(D, Name, ToD, Loc)) | |||
2488 | return std::move(Err); | |||
2489 | if (ToD) | |||
2490 | return ToD; | |||
2491 | ||||
2492 | DeclContext *DC = cast_or_null<DeclContext>( | |||
2493 | Importer.GetAlreadyImportedOrNull(cast<Decl>(D->getDeclContext()))); | |||
2494 | DeclContext *LexicalDC = | |||
2495 | cast_or_null<DeclContext>(Importer.GetAlreadyImportedOrNull( | |||
2496 | cast<Decl>(D->getLexicalDeclContext()))); | |||
2497 | ||||
2498 | // If this typedef is not in block scope, determine whether we've | |||
2499 | // seen a typedef with the same name (that we can merge with) or any | |||
2500 | // other entity by that name (which name lookup could conflict with). | |||
2501 | // Note: Repeated typedefs are not valid in C99: | |||
2502 | // 'typedef int T; typedef int T;' is invalid | |||
2503 | // We do not care about this now. | |||
2504 | if (DC && !DC->isFunctionOrMethod()) { | |||
2505 | SmallVector<NamedDecl *, 4> ConflictingDecls; | |||
2506 | unsigned IDNS = Decl::IDNS_Ordinary; | |||
2507 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
2508 | for (auto *FoundDecl : FoundDecls) { | |||
2509 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | |||
2510 | continue; | |||
2511 | if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) { | |||
2512 | if (!hasSameVisibilityContextAndLinkage(FoundTypedef, D)) | |||
2513 | continue; | |||
2514 | ||||
2515 | QualType FromUT = D->getUnderlyingType(); | |||
2516 | QualType FoundUT = FoundTypedef->getUnderlyingType(); | |||
2517 | if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) { | |||
2518 | // If the underlying declarations are unnamed records these can be | |||
2519 | // imported as different types. We should create a distinct typedef | |||
2520 | // node in this case. | |||
2521 | // If we found an existing underlying type with a record in a | |||
2522 | // different context (than the imported), this is already reason for | |||
2523 | // having distinct typedef nodes for these. | |||
2524 | // Again this can create situation like | |||
2525 | // 'typedef int T; typedef int T;' but this is hard to avoid without | |||
2526 | // a rename strategy at import. | |||
2527 | if (!FromUT.isNull() && !FoundUT.isNull()) { | |||
2528 | RecordDecl *FromR = FromUT->getAsRecordDecl(); | |||
2529 | RecordDecl *FoundR = FoundUT->getAsRecordDecl(); | |||
2530 | if (FromR && FoundR && | |||
2531 | !hasSameVisibilityContextAndLinkage(FoundR, FromR)) | |||
2532 | continue; | |||
2533 | } | |||
2534 | // If the "From" context has a complete underlying type but we | |||
2535 | // already have a complete underlying type then return with that. | |||
2536 | if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType()) | |||
2537 | return Importer.MapImported(D, FoundTypedef); | |||
2538 | // FIXME Handle redecl chain. When you do that make consistent changes | |||
2539 | // in ASTImporterLookupTable too. | |||
2540 | } else { | |||
2541 | ConflictingDecls.push_back(FoundDecl); | |||
2542 | } | |||
2543 | } | |||
2544 | } | |||
2545 | ||||
2546 | if (!ConflictingDecls.empty()) { | |||
2547 | ExpectedName NameOrErr = Importer.HandleNameConflict( | |||
2548 | Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); | |||
2549 | if (NameOrErr) | |||
2550 | Name = NameOrErr.get(); | |||
2551 | else | |||
2552 | return NameOrErr.takeError(); | |||
2553 | } | |||
2554 | } | |||
2555 | ||||
2556 | Error Err = Error::success(); | |||
2557 | auto ToUnderlyingType = importChecked(Err, D->getUnderlyingType()); | |||
2558 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | |||
2559 | auto ToBeginLoc = importChecked(Err, D->getBeginLoc()); | |||
2560 | if (Err) | |||
2561 | return std::move(Err); | |||
2562 | ||||
2563 | // Create the new typedef node. | |||
2564 | // FIXME: ToUnderlyingType is not used. | |||
2565 | (void)ToUnderlyingType; | |||
2566 | TypedefNameDecl *ToTypedef; | |||
2567 | if (IsAlias) { | |||
2568 | if (GetImportedOrCreateDecl<TypeAliasDecl>( | |||
2569 | ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc, | |||
2570 | Name.getAsIdentifierInfo(), ToTypeSourceInfo)) | |||
2571 | return ToTypedef; | |||
2572 | } else if (GetImportedOrCreateDecl<TypedefDecl>( | |||
2573 | ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc, | |||
2574 | Name.getAsIdentifierInfo(), ToTypeSourceInfo)) | |||
2575 | return ToTypedef; | |||
2576 | ||||
2577 | // Import the DeclContext and set it to the Typedef. | |||
2578 | if ((Err = ImportDeclContext(D, DC, LexicalDC))) | |||
2579 | return std::move(Err); | |||
2580 | ToTypedef->setDeclContext(DC); | |||
2581 | ToTypedef->setLexicalDeclContext(LexicalDC); | |||
2582 | // Add to the lookupTable because we could not do that in MapImported. | |||
2583 | Importer.AddToLookupTable(ToTypedef); | |||
2584 | ||||
2585 | ToTypedef->setAccess(D->getAccess()); | |||
2586 | ||||
2587 | // Templated declarations should not appear in DeclContext. | |||
2588 | TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr; | |||
2589 | if (!FromAlias || !FromAlias->getDescribedAliasTemplate()) | |||
2590 | LexicalDC->addDeclInternal(ToTypedef); | |||
2591 | ||||
2592 | return ToTypedef; | |||
2593 | } | |||
2594 | ||||
2595 | ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { | |||
2596 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); | |||
2597 | } | |||
2598 | ||||
2599 | ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { | |||
2600 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); | |||
2601 | } | |||
2602 | ||||
2603 | ExpectedDecl | |||
2604 | ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { | |||
2605 | // Import the major distinguishing characteristics of this typedef. | |||
2606 | DeclContext *DC, *LexicalDC; | |||
2607 | DeclarationName Name; | |||
2608 | SourceLocation Loc; | |||
2609 | NamedDecl *FoundD; | |||
2610 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc)) | |||
2611 | return std::move(Err); | |||
2612 | if (FoundD) | |||
2613 | return FoundD; | |||
2614 | ||||
2615 | // If this typedef is not in block scope, determine whether we've | |||
2616 | // seen a typedef with the same name (that we can merge with) or any | |||
2617 | // other entity by that name (which name lookup could conflict with). | |||
2618 | if (!DC->isFunctionOrMethod()) { | |||
2619 | SmallVector<NamedDecl *, 4> ConflictingDecls; | |||
2620 | unsigned IDNS = Decl::IDNS_Ordinary; | |||
2621 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
2622 | for (auto *FoundDecl : FoundDecls) { | |||
2623 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | |||
2624 | continue; | |||
2625 | if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl)) | |||
2626 | return Importer.MapImported(D, FoundAlias); | |||
2627 | ConflictingDecls.push_back(FoundDecl); | |||
2628 | } | |||
2629 | ||||
2630 | if (!ConflictingDecls.empty()) { | |||
2631 | ExpectedName NameOrErr = Importer.HandleNameConflict( | |||
2632 | Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); | |||
2633 | if (NameOrErr) | |||
2634 | Name = NameOrErr.get(); | |||
2635 | else | |||
2636 | return NameOrErr.takeError(); | |||
2637 | } | |||
2638 | } | |||
2639 | ||||
2640 | Error Err = Error::success(); | |||
2641 | auto ToTemplateParameters = importChecked(Err, D->getTemplateParameters()); | |||
2642 | auto ToTemplatedDecl = importChecked(Err, D->getTemplatedDecl()); | |||
2643 | if (Err) | |||
2644 | return std::move(Err); | |||
2645 | ||||
2646 | TypeAliasTemplateDecl *ToAlias; | |||
2647 | if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc, | |||
2648 | Name, ToTemplateParameters, ToTemplatedDecl)) | |||
2649 | return ToAlias; | |||
2650 | ||||
2651 | ToTemplatedDecl->setDescribedAliasTemplate(ToAlias); | |||
2652 | ||||
2653 | ToAlias->setAccess(D->getAccess()); | |||
2654 | ToAlias->setLexicalDeclContext(LexicalDC); | |||
2655 | LexicalDC->addDeclInternal(ToAlias); | |||
2656 | if (DC != Importer.getToContext().getTranslationUnitDecl()) | |||
2657 | updateLookupTableForTemplateParameters(*ToTemplateParameters); | |||
2658 | return ToAlias; | |||
2659 | } | |||
2660 | ||||
2661 | ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { | |||
2662 | // Import the major distinguishing characteristics of this label. | |||
2663 | DeclContext *DC, *LexicalDC; | |||
2664 | DeclarationName Name; | |||
2665 | SourceLocation Loc; | |||
2666 | NamedDecl *ToD; | |||
2667 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
2668 | return std::move(Err); | |||
2669 | if (ToD) | |||
2670 | return ToD; | |||
2671 | ||||
2672 | assert(LexicalDC->isFunctionOrMethod())(static_cast <bool> (LexicalDC->isFunctionOrMethod() ) ? void (0) : __assert_fail ("LexicalDC->isFunctionOrMethod()" , "clang/lib/AST/ASTImporter.cpp", 2672, __extension__ __PRETTY_FUNCTION__ )); | |||
2673 | ||||
2674 | LabelDecl *ToLabel; | |||
2675 | if (D->isGnuLocal()) { | |||
2676 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | |||
2677 | if (!BeginLocOrErr) | |||
2678 | return BeginLocOrErr.takeError(); | |||
2679 | if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc, | |||
2680 | Name.getAsIdentifierInfo(), *BeginLocOrErr)) | |||
2681 | return ToLabel; | |||
2682 | ||||
2683 | } else { | |||
2684 | if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc, | |||
2685 | Name.getAsIdentifierInfo())) | |||
2686 | return ToLabel; | |||
2687 | ||||
2688 | } | |||
2689 | ||||
2690 | Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt()); | |||
2691 | if (!ToStmtOrErr) | |||
2692 | return ToStmtOrErr.takeError(); | |||
2693 | ||||
2694 | ToLabel->setStmt(*ToStmtOrErr); | |||
2695 | ToLabel->setLexicalDeclContext(LexicalDC); | |||
2696 | LexicalDC->addDeclInternal(ToLabel); | |||
2697 | return ToLabel; | |||
2698 | } | |||
2699 | ||||
2700 | ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { | |||
2701 | // Import the major distinguishing characteristics of this enum. | |||
2702 | DeclContext *DC, *LexicalDC; | |||
2703 | DeclarationName Name; | |||
2704 | SourceLocation Loc; | |||
2705 | NamedDecl *ToD; | |||
2706 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
2707 | return std::move(Err); | |||
2708 | if (ToD) | |||
2709 | return ToD; | |||
2710 | ||||
2711 | // Figure out what enum name we're looking for. | |||
2712 | unsigned IDNS = Decl::IDNS_Tag; | |||
2713 | DeclarationName SearchName = Name; | |||
2714 | if (!SearchName && D->getTypedefNameForAnonDecl()) { | |||
2715 | if (Error Err = importInto( | |||
2716 | SearchName, D->getTypedefNameForAnonDecl()->getDeclName())) | |||
2717 | return std::move(Err); | |||
2718 | IDNS = Decl::IDNS_Ordinary; | |||
2719 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) | |||
2720 | IDNS |= Decl::IDNS_Ordinary; | |||
2721 | ||||
2722 | // We may already have an enum of the same name; try to find and match it. | |||
2723 | EnumDecl *PrevDecl = nullptr; | |||
2724 | if (!DC->isFunctionOrMethod() && SearchName) { | |||
2725 | SmallVector<NamedDecl *, 4> ConflictingDecls; | |||
2726 | auto FoundDecls = | |||
2727 | Importer.findDeclsInToCtx(DC, SearchName); | |||
2728 | for (auto *FoundDecl : FoundDecls) { | |||
2729 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | |||
2730 | continue; | |||
2731 | ||||
2732 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) { | |||
2733 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) | |||
2734 | FoundDecl = Tag->getDecl(); | |||
2735 | } | |||
2736 | ||||
2737 | if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) { | |||
2738 | if (!hasSameVisibilityContextAndLinkage(FoundEnum, D)) | |||
2739 | continue; | |||
2740 | if (IsStructuralMatch(D, FoundEnum)) { | |||
2741 | EnumDecl *FoundDef = FoundEnum->getDefinition(); | |||
2742 | if (D->isThisDeclarationADefinition() && FoundDef) | |||
2743 | return Importer.MapImported(D, FoundDef); | |||
2744 | PrevDecl = FoundEnum->getMostRecentDecl(); | |||
2745 | break; | |||
2746 | } | |||
2747 | ConflictingDecls.push_back(FoundDecl); | |||
2748 | } | |||
2749 | } | |||
2750 | ||||
2751 | if (!ConflictingDecls.empty()) { | |||
2752 | ExpectedName NameOrErr = Importer.HandleNameConflict( | |||
2753 | SearchName, DC, IDNS, ConflictingDecls.data(), | |||
2754 | ConflictingDecls.size()); | |||
2755 | if (NameOrErr) | |||
2756 | Name = NameOrErr.get(); | |||
2757 | else | |||
2758 | return NameOrErr.takeError(); | |||
2759 | } | |||
2760 | } | |||
2761 | ||||
2762 | Error Err = Error::success(); | |||
2763 | auto ToBeginLoc = importChecked(Err, D->getBeginLoc()); | |||
2764 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | |||
2765 | auto ToIntegerType = importChecked(Err, D->getIntegerType()); | |||
2766 | auto ToBraceRange = importChecked(Err, D->getBraceRange()); | |||
2767 | if (Err) | |||
2768 | return std::move(Err); | |||
2769 | ||||
2770 | // Create the enum declaration. | |||
2771 | EnumDecl *D2; | |||
2772 | if (GetImportedOrCreateDecl( | |||
2773 | D2, D, Importer.getToContext(), DC, ToBeginLoc, | |||
2774 | Loc, Name.getAsIdentifierInfo(), PrevDecl, D->isScoped(), | |||
2775 | D->isScopedUsingClassTag(), D->isFixed())) | |||
2776 | return D2; | |||
2777 | ||||
2778 | D2->setQualifierInfo(ToQualifierLoc); | |||
2779 | D2->setIntegerType(ToIntegerType); | |||
2780 | D2->setBraceRange(ToBraceRange); | |||
2781 | D2->setAccess(D->getAccess()); | |||
2782 | D2->setLexicalDeclContext(LexicalDC); | |||
2783 | addDeclToContexts(D, D2); | |||
2784 | ||||
2785 | if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) { | |||
2786 | TemplateSpecializationKind SK = MemberInfo->getTemplateSpecializationKind(); | |||
2787 | EnumDecl *FromInst = D->getInstantiatedFromMemberEnum(); | |||
2788 | if (Expected<EnumDecl *> ToInstOrErr = import(FromInst)) | |||
2789 | D2->setInstantiationOfMemberEnum(*ToInstOrErr, SK); | |||
2790 | else | |||
2791 | return ToInstOrErr.takeError(); | |||
2792 | if (ExpectedSLoc POIOrErr = import(MemberInfo->getPointOfInstantiation())) | |||
2793 | D2->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); | |||
2794 | else | |||
2795 | return POIOrErr.takeError(); | |||
2796 | } | |||
2797 | ||||
2798 | // Import the definition | |||
2799 | if (D->isCompleteDefinition()) | |||
2800 | if (Error Err = ImportDefinition(D, D2)) | |||
2801 | return std::move(Err); | |||
2802 | ||||
2803 | return D2; | |||
2804 | } | |||
2805 | ||||
2806 | ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { | |||
2807 | bool IsFriendTemplate = false; | |||
2808 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) { | |||
2809 | IsFriendTemplate = | |||
2810 | DCXX->getDescribedClassTemplate() && | |||
2811 | DCXX->getDescribedClassTemplate()->getFriendObjectKind() != | |||
2812 | Decl::FOK_None; | |||
2813 | } | |||
2814 | ||||
2815 | // Import the major distinguishing characteristics of this record. | |||
2816 | DeclContext *DC = nullptr, *LexicalDC = nullptr; | |||
2817 | DeclarationName Name; | |||
2818 | SourceLocation Loc; | |||
2819 | NamedDecl *ToD = nullptr; | |||
2820 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
2821 | return std::move(Err); | |||
2822 | if (ToD) | |||
2823 | return ToD; | |||
2824 | ||||
2825 | // Figure out what structure name we're looking for. | |||
2826 | unsigned IDNS = Decl::IDNS_Tag; | |||
2827 | DeclarationName SearchName = Name; | |||
2828 | if (!SearchName && D->getTypedefNameForAnonDecl()) { | |||
2829 | if (Error Err = importInto( | |||
2830 | SearchName, D->getTypedefNameForAnonDecl()->getDeclName())) | |||
2831 | return std::move(Err); | |||
2832 | IDNS = Decl::IDNS_Ordinary; | |||
2833 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) | |||
2834 | IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend; | |||
2835 | ||||
2836 | // We may already have a record of the same name; try to find and match it. | |||
2837 | RecordDecl *PrevDecl = nullptr; | |||
2838 | if (!DC->isFunctionOrMethod() && !D->isLambda()) { | |||
2839 | SmallVector<NamedDecl *, 4> ConflictingDecls; | |||
2840 | auto FoundDecls = | |||
2841 | Importer.findDeclsInToCtx(DC, SearchName); | |||
2842 | if (!FoundDecls.empty()) { | |||
2843 | // We're going to have to compare D against potentially conflicting Decls, | |||
2844 | // so complete it. | |||
2845 | if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition()) | |||
2846 | D->getASTContext().getExternalSource()->CompleteType(D); | |||
2847 | } | |||
2848 | ||||
2849 | for (auto *FoundDecl : FoundDecls) { | |||
2850 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | |||
2851 | continue; | |||
2852 | ||||
2853 | Decl *Found = FoundDecl; | |||
2854 | if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) { | |||
2855 | if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) | |||
2856 | Found = Tag->getDecl(); | |||
2857 | } | |||
2858 | ||||
2859 | if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) { | |||
2860 | // Do not emit false positive diagnostic in case of unnamed | |||
2861 | // struct/union and in case of anonymous structs. Would be false | |||
2862 | // because there may be several anonymous/unnamed structs in a class. | |||
2863 | // E.g. these are both valid: | |||
2864 | // struct A { // unnamed structs | |||
2865 | // struct { struct A *next; } entry0; | |||
2866 | // struct { struct A *next; } entry1; | |||
2867 | // }; | |||
2868 | // struct X { struct { int a; }; struct { int b; }; }; // anon structs | |||
2869 | if (!SearchName) | |||
2870 | if (!IsStructuralMatch(D, FoundRecord, false)) | |||
2871 | continue; | |||
2872 | ||||
2873 | if (!hasSameVisibilityContextAndLinkage(FoundRecord, D)) | |||
2874 | continue; | |||
2875 | ||||
2876 | if (IsStructuralMatch(D, FoundRecord)) { | |||
2877 | RecordDecl *FoundDef = FoundRecord->getDefinition(); | |||
2878 | if (D->isThisDeclarationADefinition() && FoundDef) { | |||
2879 | // FIXME: Structural equivalence check should check for same | |||
2880 | // user-defined methods. | |||
2881 | Importer.MapImported(D, FoundDef); | |||
2882 | if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) { | |||
2883 | auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef); | |||
2884 | assert(FoundCXX && "Record type mismatch")(static_cast <bool> (FoundCXX && "Record type mismatch" ) ? void (0) : __assert_fail ("FoundCXX && \"Record type mismatch\"" , "clang/lib/AST/ASTImporter.cpp", 2884, __extension__ __PRETTY_FUNCTION__ )); | |||
2885 | ||||
2886 | if (!Importer.isMinimalImport()) | |||
2887 | // FoundDef may not have every implicit method that D has | |||
2888 | // because implicit methods are created only if they are used. | |||
2889 | if (Error Err = ImportImplicitMethods(DCXX, FoundCXX)) | |||
2890 | return std::move(Err); | |||
2891 | } | |||
2892 | } | |||
2893 | PrevDecl = FoundRecord->getMostRecentDecl(); | |||
2894 | break; | |||
2895 | } | |||
2896 | ConflictingDecls.push_back(FoundDecl); | |||
2897 | } // kind is RecordDecl | |||
2898 | } // for | |||
2899 | ||||
2900 | if (!ConflictingDecls.empty() && SearchName) { | |||
2901 | ExpectedName NameOrErr = Importer.HandleNameConflict( | |||
2902 | SearchName, DC, IDNS, ConflictingDecls.data(), | |||
2903 | ConflictingDecls.size()); | |||
2904 | if (NameOrErr) | |||
2905 | Name = NameOrErr.get(); | |||
2906 | else | |||
2907 | return NameOrErr.takeError(); | |||
2908 | } | |||
2909 | } | |||
2910 | ||||
2911 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | |||
2912 | if (!BeginLocOrErr) | |||
2913 | return BeginLocOrErr.takeError(); | |||
2914 | ||||
2915 | // Create the record declaration. | |||
2916 | RecordDecl *D2 = nullptr; | |||
2917 | CXXRecordDecl *D2CXX = nullptr; | |||
2918 | if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) { | |||
2919 | if (DCXX->isLambda()) { | |||
2920 | auto TInfoOrErr = import(DCXX->getLambdaTypeInfo()); | |||
2921 | if (!TInfoOrErr) | |||
2922 | return TInfoOrErr.takeError(); | |||
2923 | if (GetImportedOrCreateSpecialDecl( | |||
2924 | D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(), | |||
2925 | DC, *TInfoOrErr, Loc, DCXX->getLambdaDependencyKind(), | |||
2926 | DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault())) | |||
2927 | return D2CXX; | |||
2928 | CXXRecordDecl::LambdaNumbering Numbering = DCXX->getLambdaNumbering(); | |||
2929 | ExpectedDecl CDeclOrErr = import(Numbering.ContextDecl); | |||
2930 | if (!CDeclOrErr) | |||
2931 | return CDeclOrErr.takeError(); | |||
2932 | Numbering.ContextDecl = *CDeclOrErr; | |||
2933 | D2CXX->setLambdaNumbering(Numbering); | |||
2934 | } else if (DCXX->isInjectedClassName()) { | |||
2935 | // We have to be careful to do a similar dance to the one in | |||
2936 | // Sema::ActOnStartCXXMemberDeclarations | |||
2937 | const bool DelayTypeCreation = true; | |||
2938 | if (GetImportedOrCreateDecl( | |||
2939 | D2CXX, D, Importer.getToContext(), D->getTagKind(), DC, | |||
2940 | *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(), | |||
2941 | cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation)) | |||
2942 | return D2CXX; | |||
2943 | Importer.getToContext().getTypeDeclType( | |||
2944 | D2CXX, dyn_cast<CXXRecordDecl>(DC)); | |||
2945 | } else { | |||
2946 | if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(), | |||
2947 | D->getTagKind(), DC, *BeginLocOrErr, Loc, | |||
2948 | Name.getAsIdentifierInfo(), | |||
2949 | cast_or_null<CXXRecordDecl>(PrevDecl))) | |||
2950 | return D2CXX; | |||
2951 | } | |||
2952 | ||||
2953 | D2 = D2CXX; | |||
2954 | D2->setAccess(D->getAccess()); | |||
2955 | D2->setLexicalDeclContext(LexicalDC); | |||
2956 | addDeclToContexts(D, D2); | |||
2957 | ||||
2958 | if (ClassTemplateDecl *FromDescribed = | |||
2959 | DCXX->getDescribedClassTemplate()) { | |||
2960 | ClassTemplateDecl *ToDescribed; | |||
2961 | if (Error Err = importInto(ToDescribed, FromDescribed)) | |||
2962 | return std::move(Err); | |||
2963 | D2CXX->setDescribedClassTemplate(ToDescribed); | |||
2964 | if (!DCXX->isInjectedClassName() && !IsFriendTemplate) { | |||
2965 | // In a record describing a template the type should be an | |||
2966 | // InjectedClassNameType (see Sema::CheckClassTemplate). Update the | |||
2967 | // previously set type to the correct value here (ToDescribed is not | |||
2968 | // available at record create). | |||
2969 | CXXRecordDecl *Injected = nullptr; | |||
2970 | for (NamedDecl *Found : D2CXX->noload_lookup(Name)) { | |||
2971 | auto *Record = dyn_cast<CXXRecordDecl>(Found); | |||
2972 | if (Record && Record->isInjectedClassName()) { | |||
2973 | Injected = Record; | |||
2974 | break; | |||
2975 | } | |||
2976 | } | |||
2977 | // Create an injected type for the whole redecl chain. | |||
2978 | // The chain may contain an already existing injected type at the start, | |||
2979 | // if yes this should be reused. We must ensure that only one type | |||
2980 | // object exists for the injected type (including the injected record | |||
2981 | // declaration), ASTContext does not check it. | |||
2982 | SmallVector<Decl *, 2> Redecls = | |||
2983 | getCanonicalForwardRedeclChain(D2CXX); | |||
2984 | const Type *FrontTy = | |||
2985 | cast<CXXRecordDecl>(Redecls.front())->getTypeForDecl(); | |||
2986 | QualType InjSpec; | |||
2987 | if (auto *InjTy = FrontTy->getAs<InjectedClassNameType>()) | |||
2988 | InjSpec = InjTy->getInjectedSpecializationType(); | |||
2989 | else | |||
2990 | InjSpec = ToDescribed->getInjectedClassNameSpecialization(); | |||
2991 | for (auto *R : Redecls) { | |||
2992 | auto *RI = cast<CXXRecordDecl>(R); | |||
2993 | if (R != Redecls.front() || | |||
2994 | !isa<InjectedClassNameType>(RI->getTypeForDecl())) | |||
2995 | RI->setTypeForDecl(nullptr); | |||
2996 | // This function tries to get the injected type from getTypeForDecl, | |||
2997 | // then from the previous declaration if possible. If not, it creates | |||
2998 | // a new type. | |||
2999 | Importer.getToContext().getInjectedClassNameType(RI, InjSpec); | |||
3000 | } | |||
3001 | // Set the new type for the injected decl too. | |||
3002 | if (Injected) { | |||
3003 | Injected->setTypeForDecl(nullptr); | |||
3004 | // This function will copy the injected type from D2CXX into Injected. | |||
3005 | // The injected decl does not have a previous decl to copy from. | |||
3006 | Importer.getToContext().getTypeDeclType(Injected, D2CXX); | |||
3007 | } | |||
3008 | } | |||
3009 | } else if (MemberSpecializationInfo *MemberInfo = | |||
3010 | DCXX->getMemberSpecializationInfo()) { | |||
3011 | TemplateSpecializationKind SK = | |||
3012 | MemberInfo->getTemplateSpecializationKind(); | |||
3013 | CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass(); | |||
3014 | ||||
3015 | if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst)) | |||
3016 | D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK); | |||
3017 | else | |||
3018 | return ToInstOrErr.takeError(); | |||
3019 | ||||
3020 | if (ExpectedSLoc POIOrErr = | |||
3021 | import(MemberInfo->getPointOfInstantiation())) | |||
3022 | D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation( | |||
3023 | *POIOrErr); | |||
3024 | else | |||
3025 | return POIOrErr.takeError(); | |||
3026 | } | |||
3027 | ||||
3028 | } else { | |||
3029 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), | |||
3030 | D->getTagKind(), DC, *BeginLocOrErr, Loc, | |||
3031 | Name.getAsIdentifierInfo(), PrevDecl)) | |||
3032 | return D2; | |||
3033 | D2->setLexicalDeclContext(LexicalDC); | |||
3034 | addDeclToContexts(D, D2); | |||
3035 | } | |||
3036 | ||||
3037 | if (auto BraceRangeOrErr = import(D->getBraceRange())) | |||
3038 | D2->setBraceRange(*BraceRangeOrErr); | |||
3039 | else | |||
3040 | return BraceRangeOrErr.takeError(); | |||
3041 | if (auto QualifierLocOrErr = import(D->getQualifierLoc())) | |||
3042 | D2->setQualifierInfo(*QualifierLocOrErr); | |||
3043 | else | |||
3044 | return QualifierLocOrErr.takeError(); | |||
3045 | ||||
3046 | if (D->isAnonymousStructOrUnion()) | |||
3047 | D2->setAnonymousStructOrUnion(true); | |||
3048 | ||||
3049 | if (D->isCompleteDefinition()) | |||
3050 | if (Error Err = ImportDefinition(D, D2, IDK_Default)) | |||
3051 | return std::move(Err); | |||
3052 | ||||
3053 | return D2; | |||
3054 | } | |||
3055 | ||||
3056 | ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { | |||
3057 | // Import the major distinguishing characteristics of this enumerator. | |||
3058 | DeclContext *DC, *LexicalDC; | |||
3059 | DeclarationName Name; | |||
3060 | SourceLocation Loc; | |||
3061 | NamedDecl *ToD; | |||
3062 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
3063 | return std::move(Err); | |||
3064 | if (ToD) | |||
3065 | return ToD; | |||
3066 | ||||
3067 | // Determine whether there are any other declarations with the same name and | |||
3068 | // in the same context. | |||
3069 | if (!LexicalDC->isFunctionOrMethod()) { | |||
3070 | SmallVector<NamedDecl *, 4> ConflictingDecls; | |||
3071 | unsigned IDNS = Decl::IDNS_Ordinary; | |||
3072 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
3073 | for (auto *FoundDecl : FoundDecls) { | |||
3074 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | |||
3075 | continue; | |||
3076 | ||||
3077 | if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) { | |||
3078 | if (IsStructuralMatch(D, FoundEnumConstant)) | |||
3079 | return Importer.MapImported(D, FoundEnumConstant); | |||
3080 | ConflictingDecls.push_back(FoundDecl); | |||
3081 | } | |||
3082 | } | |||
3083 | ||||
3084 | if (!ConflictingDecls.empty()) { | |||
3085 | ExpectedName NameOrErr = Importer.HandleNameConflict( | |||
3086 | Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); | |||
3087 | if (NameOrErr) | |||
3088 | Name = NameOrErr.get(); | |||
3089 | else | |||
3090 | return NameOrErr.takeError(); | |||
3091 | } | |||
3092 | } | |||
3093 | ||||
3094 | ExpectedType TypeOrErr = import(D->getType()); | |||
3095 | if (!TypeOrErr) | |||
3096 | return TypeOrErr.takeError(); | |||
3097 | ||||
3098 | ExpectedExpr InitOrErr = import(D->getInitExpr()); | |||
3099 | if (!InitOrErr) | |||
3100 | return InitOrErr.takeError(); | |||
3101 | ||||
3102 | EnumConstantDecl *ToEnumerator; | |||
3103 | if (GetImportedOrCreateDecl( | |||
3104 | ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc, | |||
3105 | Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal())) | |||
3106 | return ToEnumerator; | |||
3107 | ||||
3108 | ToEnumerator->setAccess(D->getAccess()); | |||
3109 | ToEnumerator->setLexicalDeclContext(LexicalDC); | |||
3110 | LexicalDC->addDeclInternal(ToEnumerator); | |||
3111 | return ToEnumerator; | |||
3112 | } | |||
3113 | ||||
3114 | Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD, | |||
3115 | DeclaratorDecl *ToD) { | |||
3116 | unsigned int Num = FromD->getNumTemplateParameterLists(); | |||
3117 | if (Num == 0) | |||
3118 | return Error::success(); | |||
3119 | SmallVector<TemplateParameterList *, 2> ToTPLists(Num); | |||
3120 | for (unsigned int I = 0; I < Num; ++I) | |||
3121 | if (Expected<TemplateParameterList *> ToTPListOrErr = | |||
3122 | import(FromD->getTemplateParameterList(I))) | |||
3123 | ToTPLists[I] = *ToTPListOrErr; | |||
3124 | else | |||
3125 | return ToTPListOrErr.takeError(); | |||
3126 | ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists); | |||
3127 | return Error::success(); | |||
3128 | } | |||
3129 | ||||
3130 | Error ASTNodeImporter::ImportTemplateInformation( | |||
3131 | FunctionDecl *FromFD, FunctionDecl *ToFD) { | |||
3132 | switch (FromFD->getTemplatedKind()) { | |||
3133 | case FunctionDecl::TK_NonTemplate: | |||
3134 | case FunctionDecl::TK_FunctionTemplate: | |||
3135 | return Error::success(); | |||
3136 | ||||
3137 | case FunctionDecl::TK_DependentNonTemplate: | |||
3138 | if (Expected<FunctionDecl *> InstFDOrErr = | |||
3139 | import(FromFD->getInstantiatedFromDecl())) | |||
3140 | ToFD->setInstantiatedFromDecl(*InstFDOrErr); | |||
3141 | return Error::success(); | |||
3142 | case FunctionDecl::TK_MemberSpecialization: { | |||
3143 | TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind(); | |||
3144 | ||||
3145 | if (Expected<FunctionDecl *> InstFDOrErr = | |||
3146 | import(FromFD->getInstantiatedFromMemberFunction())) | |||
3147 | ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK); | |||
3148 | else | |||
3149 | return InstFDOrErr.takeError(); | |||
3150 | ||||
3151 | if (ExpectedSLoc POIOrErr = import( | |||
3152 | FromFD->getMemberSpecializationInfo()->getPointOfInstantiation())) | |||
3153 | ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr); | |||
3154 | else | |||
3155 | return POIOrErr.takeError(); | |||
3156 | ||||
3157 | return Error::success(); | |||
3158 | } | |||
3159 | ||||
3160 | case FunctionDecl::TK_FunctionTemplateSpecialization: { | |||
3161 | auto FunctionAndArgsOrErr = | |||
3162 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); | |||
3163 | if (!FunctionAndArgsOrErr) | |||
3164 | return FunctionAndArgsOrErr.takeError(); | |||
3165 | ||||
3166 | TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy( | |||
3167 | Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr)); | |||
3168 | ||||
3169 | auto *FTSInfo = FromFD->getTemplateSpecializationInfo(); | |||
3170 | TemplateArgumentListInfo ToTAInfo; | |||
3171 | const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten; | |||
3172 | if (FromTAArgsAsWritten) | |||
3173 | if (Error Err = ImportTemplateArgumentListInfo( | |||
3174 | *FromTAArgsAsWritten, ToTAInfo)) | |||
3175 | return Err; | |||
3176 | ||||
3177 | ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation()); | |||
3178 | if (!POIOrErr) | |||
3179 | return POIOrErr.takeError(); | |||
3180 | ||||
3181 | if (Error Err = ImportTemplateParameterLists(FromFD, ToFD)) | |||
3182 | return Err; | |||
3183 | ||||
3184 | TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind(); | |||
3185 | ToFD->setFunctionTemplateSpecialization( | |||
3186 | std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr, | |||
3187 | TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr); | |||
3188 | return Error::success(); | |||
3189 | } | |||
3190 | ||||
3191 | case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { | |||
3192 | auto *FromInfo = FromFD->getDependentSpecializationInfo(); | |||
3193 | UnresolvedSet<8> TemplDecls; | |||
3194 | unsigned NumTemplates = FromInfo->getNumTemplates(); | |||
3195 | for (unsigned I = 0; I < NumTemplates; I++) { | |||
3196 | if (Expected<FunctionTemplateDecl *> ToFTDOrErr = | |||
3197 | import(FromInfo->getTemplate(I))) | |||
3198 | TemplDecls.addDecl(*ToFTDOrErr); | |||
3199 | else | |||
3200 | return ToFTDOrErr.takeError(); | |||
3201 | } | |||
3202 | ||||
3203 | // Import TemplateArgumentListInfo. | |||
3204 | TemplateArgumentListInfo ToTAInfo; | |||
3205 | if (Error Err = ImportTemplateArgumentListInfo( | |||
3206 | FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(), | |||
3207 | llvm::ArrayRef(FromInfo->getTemplateArgs(), | |||
3208 | FromInfo->getNumTemplateArgs()), | |||
3209 | ToTAInfo)) | |||
3210 | return Err; | |||
3211 | ||||
3212 | ToFD->setDependentTemplateSpecialization(Importer.getToContext(), | |||
3213 | TemplDecls, ToTAInfo); | |||
3214 | return Error::success(); | |||
3215 | } | |||
3216 | } | |||
3217 | llvm_unreachable("All cases should be covered!")::llvm::llvm_unreachable_internal("All cases should be covered!" , "clang/lib/AST/ASTImporter.cpp", 3217); | |||
3218 | } | |||
3219 | ||||
3220 | Expected<FunctionDecl *> | |||
3221 | ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) { | |||
3222 | auto FunctionAndArgsOrErr = | |||
3223 | ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD); | |||
3224 | if (!FunctionAndArgsOrErr) | |||
3225 | return FunctionAndArgsOrErr.takeError(); | |||
3226 | ||||
3227 | FunctionTemplateDecl *Template; | |||
3228 | TemplateArgsTy ToTemplArgs; | |||
3229 | std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr; | |||
3230 | void *InsertPos = nullptr; | |||
3231 | auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos); | |||
3232 | return FoundSpec; | |||
3233 | } | |||
3234 | ||||
3235 | Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD, | |||
3236 | FunctionDecl *ToFD) { | |||
3237 | if (Stmt *FromBody = FromFD->getBody()) { | |||
3238 | if (ExpectedStmt ToBodyOrErr = import(FromBody)) | |||
3239 | ToFD->setBody(*ToBodyOrErr); | |||
3240 | else | |||
3241 | return ToBodyOrErr.takeError(); | |||
3242 | } | |||
3243 | return Error::success(); | |||
3244 | } | |||
3245 | ||||
3246 | // Returns true if the given D has a DeclContext up to the TranslationUnitDecl | |||
3247 | // which is equal to the given DC, or D is equal to DC. | |||
3248 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Decl *D) { | |||
3249 | const DeclContext *DCi = dyn_cast<DeclContext>(D); | |||
3250 | if (!DCi) | |||
3251 | DCi = D->getDeclContext(); | |||
3252 | assert(DCi && "Declaration should have a context")(static_cast <bool> (DCi && "Declaration should have a context" ) ? void (0) : __assert_fail ("DCi && \"Declaration should have a context\"" , "clang/lib/AST/ASTImporter.cpp", 3252, __extension__ __PRETTY_FUNCTION__ )); | |||
3253 | while (DCi != D->getTranslationUnitDecl()) { | |||
3254 | if (DCi == DC) | |||
3255 | return true; | |||
3256 | DCi = DCi->getParent(); | |||
3257 | } | |||
3258 | return false; | |||
3259 | } | |||
3260 | ||||
3261 | // Check if there is a declaration that has 'DC' as parent context and is | |||
3262 | // referenced from statement 'S' or one of its children. The search is done in | |||
3263 | // BFS order through children of 'S'. | |||
3264 | static bool isAncestorDeclContextOf(const DeclContext *DC, const Stmt *S) { | |||
3265 | SmallVector<const Stmt *> ToProcess; | |||
3266 | ToProcess.push_back(S); | |||
3267 | while (!ToProcess.empty()) { | |||
3268 | const Stmt *CurrentS = ToProcess.pop_back_val(); | |||
3269 | ToProcess.append(CurrentS->child_begin(), CurrentS->child_end()); | |||
3270 | if (const auto *DeclRef = dyn_cast<DeclRefExpr>(CurrentS)) | |||
3271 | if (const Decl *D = DeclRef->getDecl()) | |||
3272 | if (isAncestorDeclContextOf(DC, D)) | |||
3273 | return true; | |||
3274 | } | |||
3275 | return false; | |||
3276 | } | |||
3277 | ||||
3278 | namespace { | |||
3279 | /// Check if a type has any reference to a declaration that is inside the body | |||
3280 | /// of a function. | |||
3281 | /// The \c CheckType(QualType) function should be used to determine | |||
3282 | /// this property. | |||
3283 | /// | |||
3284 | /// The type visitor visits one type object only (not recursive). | |||
3285 | /// To find all referenced declarations we must discover all type objects until | |||
3286 | /// the canonical type is reached (walk over typedef and similar objects). This | |||
3287 | /// is done by loop over all "sugar" type objects. For every such type we must | |||
3288 | /// check all declarations that are referenced from it. For this check the | |||
3289 | /// visitor is used. In the visit functions all referenced declarations except | |||
3290 | /// the one that follows in the sugar chain (if any) must be checked. For this | |||
3291 | /// check the same visitor is re-used (it has no state-dependent data). | |||
3292 | /// | |||
3293 | /// The visit functions have 3 possible return values: | |||
3294 | /// - True, found a declaration inside \c ParentDC. | |||
3295 | /// - False, found declarations only outside \c ParentDC and it is not possible | |||
3296 | /// to find more declarations (the "sugar" chain does not continue). | |||
3297 | /// - Empty optional value, found no declarations or only outside \c ParentDC, | |||
3298 | /// but it is possible to find more declarations in the type "sugar" chain. | |||
3299 | /// The loop over the "sugar" types can be implemented by using type visit | |||
3300 | /// functions only (call \c CheckType with the desugared type). With the current | |||
3301 | /// solution no visit function is needed if the type has only a desugared type | |||
3302 | /// as data. | |||
3303 | class IsTypeDeclaredInsideVisitor | |||
3304 | : public TypeVisitor<IsTypeDeclaredInsideVisitor, std::optional<bool>> { | |||
3305 | public: | |||
3306 | IsTypeDeclaredInsideVisitor(const FunctionDecl *ParentDC) | |||
3307 | : ParentDC(ParentDC) {} | |||
3308 | ||||
3309 | bool CheckType(QualType T) { | |||
3310 | // Check the chain of "sugar" types. | |||
3311 | // The "sugar" types are typedef or similar types that have the same | |||
3312 | // canonical type. | |||
3313 | if (std::optional<bool> Res = Visit(T.getTypePtr())) | |||
3314 | return *Res; | |||
3315 | QualType DsT = | |||
3316 | T.getSingleStepDesugaredType(ParentDC->getParentASTContext()); | |||
3317 | while (DsT != T) { | |||
3318 | if (std::optional<bool> Res = Visit(DsT.getTypePtr())) | |||
3319 | return *Res; | |||
3320 | T = DsT; | |||
3321 | DsT = T.getSingleStepDesugaredType(ParentDC->getParentASTContext()); | |||
3322 | } | |||
3323 | return false; | |||
3324 | } | |||
3325 | ||||
3326 | std::optional<bool> VisitTagType(const TagType *T) { | |||
3327 | if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) | |||
3328 | for (const auto &Arg : Spec->getTemplateArgs().asArray()) | |||
3329 | if (checkTemplateArgument(Arg)) | |||
3330 | return true; | |||
3331 | return isAncestorDeclContextOf(ParentDC, T->getDecl()); | |||
3332 | } | |||
3333 | ||||
3334 | std::optional<bool> VisitPointerType(const PointerType *T) { | |||
3335 | return CheckType(T->getPointeeType()); | |||
3336 | } | |||
3337 | ||||
3338 | std::optional<bool> VisitReferenceType(const ReferenceType *T) { | |||
3339 | return CheckType(T->getPointeeTypeAsWritten()); | |||
3340 | } | |||
3341 | ||||
3342 | std::optional<bool> VisitTypedefType(const TypedefType *T) { | |||
3343 | const TypedefNameDecl *TD = T->getDecl(); | |||
3344 | assert(TD)(static_cast <bool> (TD) ? void (0) : __assert_fail ("TD" , "clang/lib/AST/ASTImporter.cpp", 3344, __extension__ __PRETTY_FUNCTION__ )); | |||
3345 | return isAncestorDeclContextOf(ParentDC, TD); | |||
3346 | } | |||
3347 | ||||
3348 | std::optional<bool> VisitUsingType(const UsingType *T) { | |||
3349 | if (T->getFoundDecl() && | |||
3350 | isAncestorDeclContextOf(ParentDC, T->getFoundDecl())) | |||
3351 | return true; | |||
3352 | ||||
3353 | return {}; | |||
3354 | } | |||
3355 | ||||
3356 | std::optional<bool> | |||
3357 | VisitTemplateSpecializationType(const TemplateSpecializationType *T) { | |||
3358 | for (const auto &Arg : T->template_arguments()) | |||
3359 | if (checkTemplateArgument(Arg)) | |||
3360 | return true; | |||
3361 | // This type is a "sugar" to a record type, it can have a desugared type. | |||
3362 | return {}; | |||
3363 | } | |||
3364 | ||||
3365 | std::optional<bool> VisitConstantArrayType(const ConstantArrayType *T) { | |||
3366 | if (T->getSizeExpr() && isAncestorDeclContextOf(ParentDC, T->getSizeExpr())) | |||
3367 | return true; | |||
3368 | ||||
3369 | return CheckType(T->getElementType()); | |||
3370 | } | |||
3371 | ||||
3372 | std::optional<bool> VisitVariableArrayType(const VariableArrayType *T) { | |||
3373 | llvm_unreachable(::llvm::llvm_unreachable_internal("Variable array should not occur in deduced return type of a function" , "clang/lib/AST/ASTImporter.cpp", 3374) | |||
3374 | "Variable array should not occur in deduced return type of a function")::llvm::llvm_unreachable_internal("Variable array should not occur in deduced return type of a function" , "clang/lib/AST/ASTImporter.cpp", 3374); | |||
3375 | } | |||
3376 | ||||
3377 | std::optional<bool> VisitIncompleteArrayType(const IncompleteArrayType *T) { | |||
3378 | llvm_unreachable("Incomplete array should not occur in deduced return type "::llvm::llvm_unreachable_internal("Incomplete array should not occur in deduced return type " "of a function", "clang/lib/AST/ASTImporter.cpp", 3379) | |||
3379 | "of a function")::llvm::llvm_unreachable_internal("Incomplete array should not occur in deduced return type " "of a function", "clang/lib/AST/ASTImporter.cpp", 3379); | |||
3380 | } | |||
3381 | ||||
3382 | std::optional<bool> VisitDependentArrayType(const IncompleteArrayType *T) { | |||
3383 | llvm_unreachable("Dependent array should not occur in deduced return type "::llvm::llvm_unreachable_internal("Dependent array should not occur in deduced return type " "of a function", "clang/lib/AST/ASTImporter.cpp", 3384) | |||
3384 | "of a function")::llvm::llvm_unreachable_internal("Dependent array should not occur in deduced return type " "of a function", "clang/lib/AST/ASTImporter.cpp", 3384); | |||
3385 | } | |||
3386 | ||||
3387 | private: | |||
3388 | const DeclContext *const ParentDC; | |||
3389 | ||||
3390 | bool checkTemplateArgument(const TemplateArgument &Arg) { | |||
3391 | switch (Arg.getKind()) { | |||
3392 | case TemplateArgument::Null: | |||
3393 | return false; | |||
3394 | case TemplateArgument::Integral: | |||
3395 | return CheckType(Arg.getIntegralType()); | |||
3396 | case TemplateArgument::Type: | |||
3397 | return CheckType(Arg.getAsType()); | |||
3398 | case TemplateArgument::Expression: | |||
3399 | return isAncestorDeclContextOf(ParentDC, Arg.getAsExpr()); | |||
3400 | case TemplateArgument::Declaration: | |||
3401 | // FIXME: The declaration in this case is not allowed to be in a function? | |||
3402 | return isAncestorDeclContextOf(ParentDC, Arg.getAsDecl()); | |||
3403 | case TemplateArgument::NullPtr: | |||
3404 | // FIXME: The type is not allowed to be in the function? | |||
3405 | return CheckType(Arg.getNullPtrType()); | |||
3406 | case TemplateArgument::Pack: | |||
3407 | for (const auto &PackArg : Arg.getPackAsArray()) | |||
3408 | if (checkTemplateArgument(PackArg)) | |||
3409 | return true; | |||
3410 | return false; | |||
3411 | case TemplateArgument::Template: | |||
3412 | // Templates can not be defined locally in functions. | |||
3413 | // A template passed as argument can be not in ParentDC. | |||
3414 | return false; | |||
3415 | case TemplateArgument::TemplateExpansion: | |||
3416 | // Templates can not be defined locally in functions. | |||
3417 | // A template passed as argument can be not in ParentDC. | |||
3418 | return false; | |||
3419 | } | |||
3420 | llvm_unreachable("Unknown TemplateArgument::ArgKind enum")::llvm::llvm_unreachable_internal("Unknown TemplateArgument::ArgKind enum" , "clang/lib/AST/ASTImporter.cpp", 3420); | |||
3421 | }; | |||
3422 | }; | |||
3423 | } // namespace | |||
3424 | ||||
3425 | bool ASTNodeImporter::hasAutoReturnTypeDeclaredInside(FunctionDecl *D) { | |||
3426 | QualType FromTy = D->getType(); | |||
3427 | const auto *FromFPT = FromTy->getAs<FunctionProtoType>(); | |||
3428 | assert(FromFPT && "Must be called on FunctionProtoType")(static_cast <bool> (FromFPT && "Must be called on FunctionProtoType" ) ? void (0) : __assert_fail ("FromFPT && \"Must be called on FunctionProtoType\"" , "clang/lib/AST/ASTImporter.cpp", 3428, __extension__ __PRETTY_FUNCTION__ )); | |||
3429 | ||||
3430 | QualType RetT = FromFPT->getReturnType(); | |||
3431 | if (isa<AutoType>(RetT.getTypePtr())) { | |||
3432 | FunctionDecl *Def = D->getDefinition(); | |||
3433 | IsTypeDeclaredInsideVisitor Visitor(Def ? Def : D); | |||
3434 | return Visitor.CheckType(RetT); | |||
3435 | } | |||
3436 | ||||
3437 | return false; | |||
3438 | } | |||
3439 | ||||
3440 | ExplicitSpecifier | |||
3441 | ASTNodeImporter::importExplicitSpecifier(Error &Err, ExplicitSpecifier ESpec) { | |||
3442 | Expr *ExplicitExpr = ESpec.getExpr(); | |||
3443 | if (ExplicitExpr) | |||
3444 | ExplicitExpr = importChecked(Err, ESpec.getExpr()); | |||
3445 | return ExplicitSpecifier(ExplicitExpr, ESpec.getKind()); | |||
3446 | } | |||
3447 | ||||
3448 | ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { | |||
3449 | ||||
3450 | SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D); | |||
3451 | auto RedeclIt = Redecls.begin(); | |||
3452 | // Import the first part of the decl chain. I.e. import all previous | |||
3453 | // declarations starting from the canonical decl. | |||
3454 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { | |||
3455 | ExpectedDecl ToRedeclOrErr = import(*RedeclIt); | |||
3456 | if (!ToRedeclOrErr) | |||
3457 | return ToRedeclOrErr.takeError(); | |||
3458 | } | |||
3459 | assert(*RedeclIt == D)(static_cast <bool> (*RedeclIt == D) ? void (0) : __assert_fail ("*RedeclIt == D", "clang/lib/AST/ASTImporter.cpp", 3459, __extension__ __PRETTY_FUNCTION__)); | |||
3460 | ||||
3461 | // Import the major distinguishing characteristics of this function. | |||
3462 | DeclContext *DC, *LexicalDC; | |||
3463 | DeclarationName Name; | |||
3464 | SourceLocation Loc; | |||
3465 | NamedDecl *ToD; | |||
3466 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
3467 | return std::move(Err); | |||
3468 | if (ToD) | |||
3469 | return ToD; | |||
3470 | ||||
3471 | FunctionDecl *FoundByLookup = nullptr; | |||
3472 | FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate(); | |||
3473 | ||||
3474 | // If this is a function template specialization, then try to find the same | |||
3475 | // existing specialization in the "to" context. The lookup below will not | |||
3476 | // find any specialization, but would find the primary template; thus, we | |||
3477 | // have to skip normal lookup in case of specializations. | |||
3478 | // FIXME handle member function templates (TK_MemberSpecialization) similarly? | |||
3479 | if (D->getTemplatedKind() == | |||
3480 | FunctionDecl::TK_FunctionTemplateSpecialization) { | |||
3481 | auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D); | |||
3482 | if (!FoundFunctionOrErr) | |||
3483 | return FoundFunctionOrErr.takeError(); | |||
3484 | if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) { | |||
3485 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) | |||
3486 | return Def; | |||
3487 | FoundByLookup = FoundFunction; | |||
3488 | } | |||
3489 | } | |||
3490 | // Try to find a function in our own ("to") context with the same name, same | |||
3491 | // type, and in the same context as the function we're importing. | |||
3492 | else if (!LexicalDC->isFunctionOrMethod()) { | |||
3493 | SmallVector<NamedDecl *, 4> ConflictingDecls; | |||
3494 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; | |||
3495 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
3496 | for (auto *FoundDecl : FoundDecls) { | |||
3497 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | |||
3498 | continue; | |||
3499 | ||||
3500 | if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) { | |||
3501 | if (!hasSameVisibilityContextAndLinkage(FoundFunction, D)) | |||
3502 | continue; | |||
3503 | ||||
3504 | if (IsStructuralMatch(D, FoundFunction)) { | |||
3505 | if (Decl *Def = FindAndMapDefinition(D, FoundFunction)) | |||
3506 | return Def; | |||
3507 | FoundByLookup = FoundFunction; | |||
3508 | break; | |||
3509 | } | |||
3510 | // FIXME: Check for overloading more carefully, e.g., by boosting | |||
3511 | // Sema::IsOverload out to the AST library. | |||
3512 | ||||
3513 | // Function overloading is okay in C++. | |||
3514 | if (Importer.getToContext().getLangOpts().CPlusPlus) | |||
3515 | continue; | |||
3516 | ||||
3517 | // Complain about inconsistent function types. | |||
3518 | Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent) | |||
3519 | << Name << D->getType() << FoundFunction->getType(); | |||
3520 | Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here) | |||
3521 | << FoundFunction->getType(); | |||
3522 | ConflictingDecls.push_back(FoundDecl); | |||
3523 | } | |||
3524 | } | |||
3525 | ||||
3526 | if (!ConflictingDecls.empty()) { | |||
3527 | ExpectedName NameOrErr = Importer.HandleNameConflict( | |||
3528 | Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); | |||
3529 | if (NameOrErr) | |||
3530 | Name = NameOrErr.get(); | |||
3531 | else | |||
3532 | return NameOrErr.takeError(); | |||
3533 | } | |||
3534 | } | |||
3535 | ||||
3536 | // We do not allow more than one in-class declaration of a function. This is | |||
3537 | // because AST clients like VTableBuilder asserts on this. VTableBuilder | |||
3538 | // assumes there is only one in-class declaration. Building a redecl | |||
3539 | // chain would result in more than one in-class declaration for | |||
3540 | // overrides (even if they are part of the same redecl chain inside the | |||
3541 | // derived class.) | |||
3542 | if (FoundByLookup) { | |||
3543 | if (isa<CXXMethodDecl>(FoundByLookup)) { | |||
3544 | if (D->getLexicalDeclContext() == D->getDeclContext()) { | |||
3545 | if (!D->doesThisDeclarationHaveABody()) { | |||
3546 | if (FunctionTemplateDecl *DescribedD = | |||
3547 | D->getDescribedFunctionTemplate()) { | |||
3548 | // Handle a "templated" function together with its described | |||
3549 | // template. This avoids need for a similar check at import of the | |||
3550 | // described template. | |||
3551 | assert(FoundByLookup->getDescribedFunctionTemplate() &&(static_cast <bool> (FoundByLookup->getDescribedFunctionTemplate () && "Templated function mapped to non-templated?") ? void (0) : __assert_fail ("FoundByLookup->getDescribedFunctionTemplate() && \"Templated function mapped to non-templated?\"" , "clang/lib/AST/ASTImporter.cpp", 3552, __extension__ __PRETTY_FUNCTION__ )) | |||
3552 | "Templated function mapped to non-templated?")(static_cast <bool> (FoundByLookup->getDescribedFunctionTemplate () && "Templated function mapped to non-templated?") ? void (0) : __assert_fail ("FoundByLookup->getDescribedFunctionTemplate() && \"Templated function mapped to non-templated?\"" , "clang/lib/AST/ASTImporter.cpp", 3552, __extension__ __PRETTY_FUNCTION__ )); | |||
3553 | Importer.MapImported(DescribedD, | |||
3554 | FoundByLookup->getDescribedFunctionTemplate()); | |||
3555 | } | |||
3556 | return Importer.MapImported(D, FoundByLookup); | |||
3557 | } else { | |||
3558 | // Let's continue and build up the redecl chain in this case. | |||
3559 | // FIXME Merge the functions into one decl. | |||
3560 | } | |||
3561 | } | |||
3562 | } | |||
3563 | } | |||
3564 | ||||
3565 | DeclarationNameInfo NameInfo(Name, Loc); | |||
3566 | // Import additional name location/type info. | |||
3567 | if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo)) | |||
3568 | return std::move(Err); | |||
3569 | ||||
3570 | QualType FromTy = D->getType(); | |||
3571 | TypeSourceInfo *FromTSI = D->getTypeSourceInfo(); | |||
3572 | // Set to true if we do not import the type of the function as is. There are | |||
3573 | // cases when the original type would result in an infinite recursion during | |||
3574 | // the import. To avoid an infinite recursion when importing, we create the | |||
3575 | // FunctionDecl with a simplified function type and update it only after the | |||
3576 | // relevant AST nodes are already imported. | |||
3577 | // The type is related to TypeSourceInfo (it references the type), so we must | |||
3578 | // do the same with TypeSourceInfo. | |||
3579 | bool UsedDifferentProtoType = false; | |||
3580 | if (const auto *FromFPT = FromTy->getAs<FunctionProtoType>()) { | |||
3581 | QualType FromReturnTy = FromFPT->getReturnType(); | |||
3582 | // Functions with auto return type may define a struct inside their body | |||
3583 | // and the return type could refer to that struct. | |||
3584 | // E.g.: auto foo() { struct X{}; return X(); } | |||
3585 | // To avoid an infinite recursion when importing, create the FunctionDecl | |||
3586 | // with a simplified return type. | |||
3587 | if (hasAutoReturnTypeDeclaredInside(D)) { | |||
3588 | FromReturnTy = Importer.getFromContext().VoidTy; | |||
3589 | UsedDifferentProtoType = true; | |||
3590 | } | |||
3591 | FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); | |||
3592 | // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the | |||
3593 | // FunctionDecl that we are importing the FunctionProtoType for. | |||
3594 | // To avoid an infinite recursion when importing, create the FunctionDecl | |||
3595 | // with a simplified function type. | |||
3596 | if (FromEPI.ExceptionSpec.SourceDecl || | |||
3597 | FromEPI.ExceptionSpec.SourceTemplate || | |||
3598 | FromEPI.ExceptionSpec.NoexceptExpr) { | |||
3599 | FunctionProtoType::ExtProtoInfo DefaultEPI; | |||
3600 | FromEPI = DefaultEPI; | |||
3601 | UsedDifferentProtoType = true; | |||
3602 | } | |||
3603 | FromTy = Importer.getFromContext().getFunctionType( | |||
3604 | FromReturnTy, FromFPT->getParamTypes(), FromEPI); | |||
3605 | FromTSI = Importer.getFromContext().getTrivialTypeSourceInfo( | |||
3606 | FromTy, D->getBeginLoc()); | |||
3607 | } | |||
3608 | ||||
3609 | Error Err = Error::success(); | |||
3610 | auto T = importChecked(Err, FromTy); | |||
3611 | auto TInfo = importChecked(Err, FromTSI); | |||
3612 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | |||
3613 | auto ToEndLoc = importChecked(Err, D->getEndLoc()); | |||
3614 | auto ToDefaultLoc = importChecked(Err, D->getDefaultLoc()); | |||
3615 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | |||
3616 | auto TrailingRequiresClause = | |||
3617 | importChecked(Err, D->getTrailingRequiresClause()); | |||
3618 | if (Err) | |||
3619 | return std::move(Err); | |||
3620 | ||||
3621 | // Import the function parameters. | |||
3622 | SmallVector<ParmVarDecl *, 8> Parameters; | |||
3623 | for (auto *P : D->parameters()) { | |||
3624 | if (Expected<ParmVarDecl *> ToPOrErr = import(P)) | |||
3625 | Parameters.push_back(*ToPOrErr); | |||
3626 | else | |||
3627 | return ToPOrErr.takeError(); | |||
3628 | } | |||
3629 | ||||
3630 | // Create the imported function. | |||
3631 | FunctionDecl *ToFunction = nullptr; | |||
3632 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { | |||
3633 | ExplicitSpecifier ESpec = | |||
3634 | importExplicitSpecifier(Err, FromConstructor->getExplicitSpecifier()); | |||
3635 | if (Err) | |||
3636 | return std::move(Err); | |||
3637 | auto ToInheritedConstructor = InheritedConstructor(); | |||
3638 | if (FromConstructor->isInheritingConstructor()) { | |||
3639 | Expected<InheritedConstructor> ImportedInheritedCtor = | |||
3640 | import(FromConstructor->getInheritedConstructor()); | |||
3641 | if (!ImportedInheritedCtor) | |||
3642 | return ImportedInheritedCtor.takeError(); | |||
3643 | ToInheritedConstructor = *ImportedInheritedCtor; | |||
3644 | } | |||
3645 | if (GetImportedOrCreateDecl<CXXConstructorDecl>( | |||
3646 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), | |||
3647 | ToInnerLocStart, NameInfo, T, TInfo, ESpec, D->UsesFPIntrin(), | |||
3648 | D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind(), | |||
3649 | ToInheritedConstructor, TrailingRequiresClause)) | |||
3650 | return ToFunction; | |||
3651 | } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) { | |||
3652 | ||||
3653 | Error Err = Error::success(); | |||
3654 | auto ToOperatorDelete = importChecked( | |||
3655 | Err, const_cast<FunctionDecl *>(FromDtor->getOperatorDelete())); | |||
3656 | auto ToThisArg = importChecked(Err, FromDtor->getOperatorDeleteThisArg()); | |||
3657 | if (Err) | |||
3658 | return std::move(Err); | |||
3659 | ||||
3660 | if (GetImportedOrCreateDecl<CXXDestructorDecl>( | |||
3661 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), | |||
3662 | ToInnerLocStart, NameInfo, T, TInfo, D->UsesFPIntrin(), | |||
3663 | D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind(), | |||
3664 | TrailingRequiresClause)) | |||
3665 | return ToFunction; | |||
3666 | ||||
3667 | CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction); | |||
3668 | ||||
3669 | ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg); | |||
3670 | } else if (CXXConversionDecl *FromConversion = | |||
3671 | dyn_cast<CXXConversionDecl>(D)) { | |||
3672 | ExplicitSpecifier ESpec = | |||
3673 | importExplicitSpecifier(Err, FromConversion->getExplicitSpecifier()); | |||
3674 | if (Err) | |||
3675 | return std::move(Err); | |||
3676 | if (GetImportedOrCreateDecl<CXXConversionDecl>( | |||
3677 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), | |||
3678 | ToInnerLocStart, NameInfo, T, TInfo, D->UsesFPIntrin(), | |||
3679 | D->isInlineSpecified(), ESpec, D->getConstexprKind(), | |||
3680 | SourceLocation(), TrailingRequiresClause)) | |||
3681 | return ToFunction; | |||
3682 | } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) { | |||
3683 | if (GetImportedOrCreateDecl<CXXMethodDecl>( | |||
3684 | ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), | |||
3685 | ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(), | |||
3686 | Method->UsesFPIntrin(), Method->isInlineSpecified(), | |||
3687 | D->getConstexprKind(), SourceLocation(), TrailingRequiresClause)) | |||
3688 | return ToFunction; | |||
3689 | } else if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(D)) { | |||
3690 | ExplicitSpecifier ESpec = | |||
3691 | importExplicitSpecifier(Err, Guide->getExplicitSpecifier()); | |||
3692 | CXXConstructorDecl *Ctor = | |||
3693 | importChecked(Err, Guide->getCorrespondingConstructor()); | |||
3694 | if (Err) | |||
3695 | return std::move(Err); | |||
3696 | if (GetImportedOrCreateDecl<CXXDeductionGuideDecl>( | |||
3697 | ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart, ESpec, | |||
3698 | NameInfo, T, TInfo, ToEndLoc, Ctor)) | |||
3699 | return ToFunction; | |||
3700 | cast<CXXDeductionGuideDecl>(ToFunction) | |||
3701 | ->setIsCopyDeductionCandidate(Guide->isCopyDeductionCandidate()); | |||
3702 | } else { | |||
3703 | if (GetImportedOrCreateDecl( | |||
3704 | ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart, | |||
3705 | NameInfo, T, TInfo, D->getStorageClass(), D->UsesFPIntrin(), | |||
3706 | D->isInlineSpecified(), D->hasWrittenPrototype(), | |||
3707 | D->getConstexprKind(), TrailingRequiresClause)) | |||
3708 | return ToFunction; | |||
3709 | } | |||
3710 | ||||
3711 | // Connect the redecl chain. | |||
3712 | if (FoundByLookup) { | |||
3713 | auto *Recent = const_cast<FunctionDecl *>( | |||
3714 | FoundByLookup->getMostRecentDecl()); | |||
3715 | ToFunction->setPreviousDecl(Recent); | |||
3716 | // FIXME Probably we should merge exception specifications. E.g. In the | |||
3717 | // "To" context the existing function may have exception specification with | |||
3718 | // noexcept-unevaluated, while the newly imported function may have an | |||
3719 | // evaluated noexcept. A call to adjustExceptionSpec() on the imported | |||
3720 | // decl and its redeclarations may be required. | |||
3721 | } | |||
3722 | ||||
3723 | ToFunction->setQualifierInfo(ToQualifierLoc); | |||
3724 | ToFunction->setAccess(D->getAccess()); | |||
3725 | ToFunction->setLexicalDeclContext(LexicalDC); | |||
3726 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); | |||
3727 | ToFunction->setTrivial(D->isTrivial()); | |||
3728 | ToFunction->setPure(D->isPure()); | |||
3729 | ToFunction->setDefaulted(D->isDefaulted()); | |||
3730 | ToFunction->setExplicitlyDefaulted(D->isExplicitlyDefaulted()); | |||
3731 | ToFunction->setDeletedAsWritten(D->isDeletedAsWritten()); | |||
3732 | ToFunction->setFriendConstraintRefersToEnclosingTemplate( | |||
3733 | D->FriendConstraintRefersToEnclosingTemplate()); | |||
3734 | ToFunction->setRangeEnd(ToEndLoc); | |||
3735 | ToFunction->setDefaultLoc(ToDefaultLoc); | |||
3736 | ||||
3737 | // Set the parameters. | |||
3738 | for (auto *Param : Parameters) { | |||
3739 | Param->setOwningFunction(ToFunction); | |||
3740 | ToFunction->addDeclInternal(Param); | |||
3741 | if (ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable()) | |||
3742 | LT->update(Param, Importer.getToContext().getTranslationUnitDecl()); | |||
3743 | } | |||
3744 | ToFunction->setParams(Parameters); | |||
3745 | ||||
3746 | // We need to complete creation of FunctionProtoTypeLoc manually with setting | |||
3747 | // params it refers to. | |||
3748 | if (TInfo) { | |||
3749 | if (auto ProtoLoc = | |||
3750 | TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) { | |||
3751 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) | |||
3752 | ProtoLoc.setParam(I, Parameters[I]); | |||
3753 | } | |||
3754 | } | |||
3755 | ||||
3756 | // Import the describing template function, if any. | |||
3757 | if (FromFT) { | |||
3758 | auto ToFTOrErr = import(FromFT); | |||
3759 | if (!ToFTOrErr) | |||
3760 | return ToFTOrErr.takeError(); | |||
3761 | } | |||
3762 | ||||
3763 | // Import Ctor initializers. | |||
3764 | if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { | |||
3765 | if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { | |||
3766 | SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers); | |||
3767 | // Import first, then allocate memory and copy if there was no error. | |||
3768 | if (Error Err = ImportContainerChecked( | |||
3769 | FromConstructor->inits(), CtorInitializers)) | |||
3770 | return std::move(Err); | |||
3771 | auto **Memory = | |||
3772 | new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; | |||
3773 | std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory); | |||
3774 | auto *ToCtor = cast<CXXConstructorDecl>(ToFunction); | |||
3775 | ToCtor->setCtorInitializers(Memory); | |||
3776 | ToCtor->setNumCtorInitializers(NumInitializers); | |||
3777 | } | |||
3778 | } | |||
3779 | ||||
3780 | // If it is a template, import all related things. | |||
3781 | if (Error Err = ImportTemplateInformation(D, ToFunction)) | |||
3782 | return std::move(Err); | |||
3783 | ||||
3784 | if (D->doesThisDeclarationHaveABody()) { | |||
3785 | Error Err = ImportFunctionDeclBody(D, ToFunction); | |||
3786 | ||||
3787 | if (Err) | |||
3788 | return std::move(Err); | |||
3789 | } | |||
3790 | ||||
3791 | // Import and set the original type in case we used another type. | |||
3792 | if (UsedDifferentProtoType) { | |||
3793 | if (ExpectedType TyOrErr = import(D->getType())) | |||
3794 | ToFunction->setType(*TyOrErr); | |||
3795 | else | |||
3796 | return TyOrErr.takeError(); | |||
3797 | if (Expected<TypeSourceInfo *> TSIOrErr = import(D->getTypeSourceInfo())) | |||
3798 | ToFunction->setTypeSourceInfo(*TSIOrErr); | |||
3799 | else | |||
3800 | return TSIOrErr.takeError(); | |||
3801 | } | |||
3802 | ||||
3803 | // FIXME: Other bits to merge? | |||
3804 | ||||
3805 | addDeclToContexts(D, ToFunction); | |||
3806 | ||||
3807 | if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D)) | |||
3808 | if (Error Err = ImportOverriddenMethods(cast<CXXMethodDecl>(ToFunction), | |||
3809 | FromCXXMethod)) | |||
3810 | return std::move(Err); | |||
3811 | ||||
3812 | // Import the rest of the chain. I.e. import all subsequent declarations. | |||
3813 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { | |||
3814 | ExpectedDecl ToRedeclOrErr = import(*RedeclIt); | |||
3815 | if (!ToRedeclOrErr) | |||
3816 | return ToRedeclOrErr.takeError(); | |||
3817 | } | |||
3818 | ||||
3819 | return ToFunction; | |||
3820 | } | |||
3821 | ||||
3822 | ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { | |||
3823 | return VisitFunctionDecl(D); | |||
3824 | } | |||
3825 | ||||
3826 | ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { | |||
3827 | return VisitCXXMethodDecl(D); | |||
3828 | } | |||
3829 | ||||
3830 | ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { | |||
3831 | return VisitCXXMethodDecl(D); | |||
3832 | } | |||
3833 | ||||
3834 | ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { | |||
3835 | return VisitCXXMethodDecl(D); | |||
3836 | } | |||
3837 | ||||
3838 | ExpectedDecl | |||
3839 | ASTNodeImporter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { | |||
3840 | return VisitFunctionDecl(D); | |||
3841 | } | |||
3842 | ||||
3843 | ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { | |||
3844 | // Import the major distinguishing characteristics of a variable. | |||
3845 | DeclContext *DC, *LexicalDC; | |||
3846 | DeclarationName Name; | |||
3847 | SourceLocation Loc; | |||
3848 | NamedDecl *ToD; | |||
3849 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
3850 | return std::move(Err); | |||
3851 | if (ToD) | |||
3852 | return ToD; | |||
3853 | ||||
3854 | // Determine whether we've already imported this field. | |||
3855 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
3856 | for (auto *FoundDecl : FoundDecls) { | |||
3857 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) { | |||
3858 | // For anonymous fields, match up by index. | |||
3859 | if (!Name && | |||
3860 | ASTImporter::getFieldIndex(D) != | |||
3861 | ASTImporter::getFieldIndex(FoundField)) | |||
3862 | continue; | |||
3863 | ||||
3864 | if (Importer.IsStructurallyEquivalent(D->getType(), | |||
3865 | FoundField->getType())) { | |||
3866 | Importer.MapImported(D, FoundField); | |||
3867 | // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the | |||
3868 | // initializer of a FieldDecl might not had been instantiated in the | |||
3869 | // "To" context. However, the "From" context might instantiated that, | |||
3870 | // thus we have to merge that. | |||
3871 | // Note: `hasInClassInitializer()` is not the same as non-null | |||
3872 | // `getInClassInitializer()` value. | |||
3873 | if (Expr *FromInitializer = D->getInClassInitializer()) { | |||
3874 | if (ExpectedExpr ToInitializerOrErr = import(FromInitializer)) { | |||
3875 | // Import of the FromInitializer may result in the setting of | |||
3876 | // InClassInitializer. If not, set it here. | |||
3877 | assert(FoundField->hasInClassInitializer() &&(static_cast <bool> (FoundField->hasInClassInitializer () && "Field should have an in-class initializer if it has an " "expression for it.") ? void (0) : __assert_fail ("FoundField->hasInClassInitializer() && \"Field should have an in-class initializer if it has an \" \"expression for it.\"" , "clang/lib/AST/ASTImporter.cpp", 3879, __extension__ __PRETTY_FUNCTION__ )) | |||
3878 | "Field should have an in-class initializer if it has an "(static_cast <bool> (FoundField->hasInClassInitializer () && "Field should have an in-class initializer if it has an " "expression for it.") ? void (0) : __assert_fail ("FoundField->hasInClassInitializer() && \"Field should have an in-class initializer if it has an \" \"expression for it.\"" , "clang/lib/AST/ASTImporter.cpp", 3879, __extension__ __PRETTY_FUNCTION__ )) | |||
3879 | "expression for it.")(static_cast <bool> (FoundField->hasInClassInitializer () && "Field should have an in-class initializer if it has an " "expression for it.") ? void (0) : __assert_fail ("FoundField->hasInClassInitializer() && \"Field should have an in-class initializer if it has an \" \"expression for it.\"" , "clang/lib/AST/ASTImporter.cpp", 3879, __extension__ __PRETTY_FUNCTION__ )); | |||
3880 | if (!FoundField->getInClassInitializer()) | |||
3881 | FoundField->setInClassInitializer(*ToInitializerOrErr); | |||
3882 | } else { | |||
3883 | return ToInitializerOrErr.takeError(); | |||
3884 | } | |||
3885 | } | |||
3886 | return FoundField; | |||
3887 | } | |||
3888 | ||||
3889 | // FIXME: Why is this case not handled with calling HandleNameConflict? | |||
3890 | Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent) | |||
3891 | << Name << D->getType() << FoundField->getType(); | |||
3892 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) | |||
3893 | << FoundField->getType(); | |||
3894 | ||||
3895 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
3896 | } | |||
3897 | } | |||
3898 | ||||
3899 | Error Err = Error::success(); | |||
3900 | auto ToType = importChecked(Err, D->getType()); | |||
3901 | auto ToTInfo = importChecked(Err, D->getTypeSourceInfo()); | |||
3902 | auto ToBitWidth = importChecked(Err, D->getBitWidth()); | |||
3903 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | |||
3904 | auto ToInitializer = importChecked(Err, D->getInClassInitializer()); | |||
3905 | if (Err) | |||
3906 | return std::move(Err); | |||
3907 | const Type *ToCapturedVLAType = nullptr; | |||
3908 | if (Error Err = Importer.importInto( | |||
3909 | ToCapturedVLAType, cast_or_null<Type>(D->getCapturedVLAType()))) | |||
3910 | return std::move(Err); | |||
3911 | ||||
3912 | FieldDecl *ToField; | |||
3913 | if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC, | |||
3914 | ToInnerLocStart, Loc, Name.getAsIdentifierInfo(), | |||
3915 | ToType, ToTInfo, ToBitWidth, D->isMutable(), | |||
3916 | D->getInClassInitStyle())) | |||
3917 | return ToField; | |||
3918 | ||||
3919 | // We need [[no_unqiue_address]] attributes to be added to FieldDecl, before | |||
3920 | // we add fields in CXXRecordDecl::addedMember, otherwise record will be | |||
3921 | // marked as having non-zero size. | |||
3922 | Err = Importer.ImportAttrs(ToField, D); | |||
3923 | if (Err) | |||
3924 | return std::move(Err); | |||
3925 | ToField->setAccess(D->getAccess()); | |||
3926 | ToField->setLexicalDeclContext(LexicalDC); | |||
3927 | if (ToInitializer) | |||
3928 | ToField->setInClassInitializer(ToInitializer); | |||
3929 | ToField->setImplicit(D->isImplicit()); | |||
3930 | if (ToCapturedVLAType) | |||
3931 | ToField->setCapturedVLAType(cast<VariableArrayType>(ToCapturedVLAType)); | |||
3932 | LexicalDC->addDeclInternal(ToField); | |||
3933 | return ToField; | |||
3934 | } | |||
3935 | ||||
3936 | ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { | |||
3937 | // Import the major distinguishing characteristics of a variable. | |||
3938 | DeclContext *DC, *LexicalDC; | |||
3939 | DeclarationName Name; | |||
3940 | SourceLocation Loc; | |||
3941 | NamedDecl *ToD; | |||
3942 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
3943 | return std::move(Err); | |||
3944 | if (ToD) | |||
3945 | return ToD; | |||
3946 | ||||
3947 | // Determine whether we've already imported this field. | |||
3948 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
3949 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { | |||
3950 | if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { | |||
3951 | // For anonymous indirect fields, match up by index. | |||
3952 | if (!Name && | |||
3953 | ASTImporter::getFieldIndex(D) != | |||
3954 | ASTImporter::getFieldIndex(FoundField)) | |||
3955 | continue; | |||
3956 | ||||
3957 | if (Importer.IsStructurallyEquivalent(D->getType(), | |||
3958 | FoundField->getType(), | |||
3959 | !Name.isEmpty())) { | |||
3960 | Importer.MapImported(D, FoundField); | |||
3961 | return FoundField; | |||
3962 | } | |||
3963 | ||||
3964 | // If there are more anonymous fields to check, continue. | |||
3965 | if (!Name && I < N-1) | |||
3966 | continue; | |||
3967 | ||||
3968 | // FIXME: Why is this case not handled with calling HandleNameConflict? | |||
3969 | Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent) | |||
3970 | << Name << D->getType() << FoundField->getType(); | |||
3971 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) | |||
3972 | << FoundField->getType(); | |||
3973 | ||||
3974 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
3975 | } | |||
3976 | } | |||
3977 | ||||
3978 | // Import the type. | |||
3979 | auto TypeOrErr = import(D->getType()); | |||
3980 | if (!TypeOrErr) | |||
3981 | return TypeOrErr.takeError(); | |||
3982 | ||||
3983 | auto **NamedChain = | |||
3984 | new (Importer.getToContext()) NamedDecl*[D->getChainingSize()]; | |||
3985 | ||||
3986 | unsigned i = 0; | |||
3987 | for (auto *PI : D->chain()) | |||
3988 | if (Expected<NamedDecl *> ToD = import(PI)) | |||
3989 | NamedChain[i++] = *ToD; | |||
3990 | else | |||
3991 | return ToD.takeError(); | |||
3992 | ||||
3993 | llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()}; | |||
3994 | IndirectFieldDecl *ToIndirectField; | |||
3995 | if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC, | |||
3996 | Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH)) | |||
3997 | // FIXME here we leak `NamedChain` which is allocated before | |||
3998 | return ToIndirectField; | |||
3999 | ||||
4000 | ToIndirectField->setAccess(D->getAccess()); | |||
4001 | ToIndirectField->setLexicalDeclContext(LexicalDC); | |||
4002 | LexicalDC->addDeclInternal(ToIndirectField); | |||
4003 | return ToIndirectField; | |||
4004 | } | |||
4005 | ||||
4006 | /// Used as return type of getFriendCountAndPosition. | |||
4007 | struct FriendCountAndPosition { | |||
4008 | /// Number of similar looking friends. | |||
4009 | unsigned int TotalCount; | |||
4010 | /// Index of the specific FriendDecl. | |||
4011 | unsigned int IndexOfDecl; | |||
4012 | }; | |||
4013 | ||||
4014 | template <class T> | |||
4015 | static FriendCountAndPosition getFriendCountAndPosition( | |||
4016 | const FriendDecl *FD, | |||
4017 | llvm::function_ref<T(const FriendDecl *)> GetCanTypeOrDecl) { | |||
4018 | unsigned int FriendCount = 0; | |||
4019 | std::optional<unsigned int> FriendPosition; | |||
4020 | const auto *RD = cast<CXXRecordDecl>(FD->getLexicalDeclContext()); | |||
4021 | ||||
4022 | T TypeOrDecl = GetCanTypeOrDecl(FD); | |||
4023 | ||||
4024 | for (const FriendDecl *FoundFriend : RD->friends()) { | |||
4025 | if (FoundFriend == FD) { | |||
4026 | FriendPosition = FriendCount; | |||
4027 | ++FriendCount; | |||
4028 | } else if (!FoundFriend->getFriendDecl() == !FD->getFriendDecl() && | |||
4029 | GetCanTypeOrDecl(FoundFriend) == TypeOrDecl) { | |||
4030 | ++FriendCount; | |||
4031 | } | |||
4032 | } | |||
4033 | ||||
4034 | assert(FriendPosition && "Friend decl not found in own parent.")(static_cast <bool> (FriendPosition && "Friend decl not found in own parent." ) ? void (0) : __assert_fail ("FriendPosition && \"Friend decl not found in own parent.\"" , "clang/lib/AST/ASTImporter.cpp", 4034, __extension__ __PRETTY_FUNCTION__ )); | |||
4035 | ||||
4036 | return {FriendCount, *FriendPosition}; | |||
4037 | } | |||
4038 | ||||
4039 | static FriendCountAndPosition getFriendCountAndPosition(const FriendDecl *FD) { | |||
4040 | if (FD->getFriendType()) | |||
4041 | return getFriendCountAndPosition<QualType>(FD, [](const FriendDecl *F) { | |||
4042 | if (TypeSourceInfo *TSI = F->getFriendType()) | |||
4043 | return TSI->getType().getCanonicalType(); | |||
4044 | llvm_unreachable("Wrong friend object type.")::llvm::llvm_unreachable_internal("Wrong friend object type." , "clang/lib/AST/ASTImporter.cpp", 4044); | |||
4045 | }); | |||
4046 | else | |||
4047 | return getFriendCountAndPosition<Decl *>(FD, [](const FriendDecl *F) { | |||
4048 | if (Decl *D = F->getFriendDecl()) | |||
4049 | return D->getCanonicalDecl(); | |||
4050 | llvm_unreachable("Wrong friend object type.")::llvm::llvm_unreachable_internal("Wrong friend object type." , "clang/lib/AST/ASTImporter.cpp", 4050); | |||
4051 | }); | |||
4052 | } | |||
4053 | ||||
4054 | ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) { | |||
4055 | // Import the major distinguishing characteristics of a declaration. | |||
4056 | DeclContext *DC, *LexicalDC; | |||
4057 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | |||
4058 | return std::move(Err); | |||
4059 | ||||
4060 | // Determine whether we've already imported this decl. | |||
4061 | // FriendDecl is not a NamedDecl so we cannot use lookup. | |||
4062 | // We try to maintain order and count of redundant friend declarations. | |||
4063 | const auto *RD = cast<CXXRecordDecl>(DC); | |||
4064 | FriendDecl *ImportedFriend = RD->getFirstFriend(); | |||
4065 | SmallVector<FriendDecl *, 2> ImportedEquivalentFriends; | |||
4066 | ||||
4067 | while (ImportedFriend) { | |||
4068 | bool Match = false; | |||
4069 | if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) { | |||
4070 | Match = | |||
4071 | IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(), | |||
4072 | /*Complain=*/false); | |||
4073 | } else if (D->getFriendType() && ImportedFriend->getFriendType()) { | |||
4074 | Match = Importer.IsStructurallyEquivalent( | |||
4075 | D->getFriendType()->getType(), | |||
4076 | ImportedFriend->getFriendType()->getType(), /*Complain=*/false); | |||
4077 | } | |||
4078 | if (Match) | |||
4079 | ImportedEquivalentFriends.push_back(ImportedFriend); | |||
4080 | ||||
4081 | ImportedFriend = ImportedFriend->getNextFriend(); | |||
4082 | } | |||
4083 | FriendCountAndPosition CountAndPosition = getFriendCountAndPosition(D); | |||
4084 | ||||
4085 | assert(ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount &&(static_cast <bool> (ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && "Class with non-matching friends is imported, ODR check wrong?" ) ? void (0) : __assert_fail ("ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && \"Class with non-matching friends is imported, ODR check wrong?\"" , "clang/lib/AST/ASTImporter.cpp", 4086, __extension__ __PRETTY_FUNCTION__ )) | |||
4086 | "Class with non-matching friends is imported, ODR check wrong?")(static_cast <bool> (ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && "Class with non-matching friends is imported, ODR check wrong?" ) ? void (0) : __assert_fail ("ImportedEquivalentFriends.size() <= CountAndPosition.TotalCount && \"Class with non-matching friends is imported, ODR check wrong?\"" , "clang/lib/AST/ASTImporter.cpp", 4086, __extension__ __PRETTY_FUNCTION__ )); | |||
4087 | if (ImportedEquivalentFriends.size() == CountAndPosition.TotalCount) | |||
4088 | return Importer.MapImported( | |||
4089 | D, ImportedEquivalentFriends[CountAndPosition.IndexOfDecl]); | |||
4090 | ||||
4091 | // Not found. Create it. | |||
4092 | // The declarations will be put into order later by ImportDeclContext. | |||
4093 | FriendDecl::FriendUnion ToFU; | |||
4094 | if (NamedDecl *FriendD = D->getFriendDecl()) { | |||
4095 | NamedDecl *ToFriendD; | |||
4096 | if (Error Err = importInto(ToFriendD, FriendD)) | |||
4097 | return std::move(Err); | |||
4098 | ||||
4099 | if (FriendD->getFriendObjectKind() != Decl::FOK_None && | |||
4100 | !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator))) | |||
4101 | ToFriendD->setObjectOfFriendDecl(false); | |||
4102 | ||||
4103 | ToFU = ToFriendD; | |||
4104 | } else { // The friend is a type, not a decl. | |||
4105 | if (auto TSIOrErr = import(D->getFriendType())) | |||
4106 | ToFU = *TSIOrErr; | |||
4107 | else | |||
4108 | return TSIOrErr.takeError(); | |||
4109 | } | |||
4110 | ||||
4111 | SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists); | |||
4112 | auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>(); | |||
4113 | for (unsigned I = 0; I < D->NumTPLists; I++) { | |||
4114 | if (auto ListOrErr = import(FromTPLists[I])) | |||
4115 | ToTPLists[I] = *ListOrErr; | |||
4116 | else | |||
4117 | return ListOrErr.takeError(); | |||
4118 | } | |||
4119 | ||||
4120 | auto LocationOrErr = import(D->getLocation()); | |||
4121 | if (!LocationOrErr) | |||
4122 | return LocationOrErr.takeError(); | |||
4123 | auto FriendLocOrErr = import(D->getFriendLoc()); | |||
4124 | if (!FriendLocOrErr) | |||
4125 | return FriendLocOrErr.takeError(); | |||
4126 | ||||
4127 | FriendDecl *FrD; | |||
4128 | if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC, | |||
4129 | *LocationOrErr, ToFU, | |||
4130 | *FriendLocOrErr, ToTPLists)) | |||
4131 | return FrD; | |||
4132 | ||||
4133 | FrD->setAccess(D->getAccess()); | |||
4134 | FrD->setLexicalDeclContext(LexicalDC); | |||
4135 | LexicalDC->addDeclInternal(FrD); | |||
4136 | return FrD; | |||
4137 | } | |||
4138 | ||||
4139 | ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { | |||
4140 | // Import the major distinguishing characteristics of an ivar. | |||
4141 | DeclContext *DC, *LexicalDC; | |||
4142 | DeclarationName Name; | |||
4143 | SourceLocation Loc; | |||
4144 | NamedDecl *ToD; | |||
4145 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
4146 | return std::move(Err); | |||
4147 | if (ToD) | |||
4148 | return ToD; | |||
4149 | ||||
4150 | // Determine whether we've already imported this ivar | |||
4151 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
4152 | for (auto *FoundDecl : FoundDecls) { | |||
4153 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) { | |||
4154 | if (Importer.IsStructurallyEquivalent(D->getType(), | |||
4155 | FoundIvar->getType())) { | |||
4156 | Importer.MapImported(D, FoundIvar); | |||
4157 | return FoundIvar; | |||
4158 | } | |||
4159 | ||||
4160 | Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent) | |||
4161 | << Name << D->getType() << FoundIvar->getType(); | |||
4162 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) | |||
4163 | << FoundIvar->getType(); | |||
4164 | ||||
4165 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
4166 | } | |||
4167 | } | |||
4168 | ||||
4169 | Error Err = Error::success(); | |||
4170 | auto ToType = importChecked(Err, D->getType()); | |||
4171 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | |||
4172 | auto ToBitWidth = importChecked(Err, D->getBitWidth()); | |||
4173 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | |||
4174 | if (Err) | |||
4175 | return std::move(Err); | |||
4176 | ||||
4177 | ObjCIvarDecl *ToIvar; | |||
4178 | if (GetImportedOrCreateDecl( | |||
4179 | ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC), | |||
4180 | ToInnerLocStart, Loc, Name.getAsIdentifierInfo(), | |||
4181 | ToType, ToTypeSourceInfo, | |||
4182 | D->getAccessControl(),ToBitWidth, D->getSynthesize())) | |||
4183 | return ToIvar; | |||
4184 | ||||
4185 | ToIvar->setLexicalDeclContext(LexicalDC); | |||
4186 | LexicalDC->addDeclInternal(ToIvar); | |||
4187 | return ToIvar; | |||
4188 | } | |||
4189 | ||||
4190 | ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) { | |||
4191 | ||||
4192 | SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D); | |||
4193 | auto RedeclIt = Redecls.begin(); | |||
4194 | // Import the first part of the decl chain. I.e. import all previous | |||
4195 | // declarations starting from the canonical decl. | |||
4196 | for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) { | |||
4197 | ExpectedDecl RedeclOrErr = import(*RedeclIt); | |||
4198 | if (!RedeclOrErr) | |||
4199 | return RedeclOrErr.takeError(); | |||
4200 | } | |||
4201 | assert(*RedeclIt == D)(static_cast <bool> (*RedeclIt == D) ? void (0) : __assert_fail ("*RedeclIt == D", "clang/lib/AST/ASTImporter.cpp", 4201, __extension__ __PRETTY_FUNCTION__)); | |||
4202 | ||||
4203 | // Import the major distinguishing characteristics of a variable. | |||
4204 | DeclContext *DC, *LexicalDC; | |||
4205 | DeclarationName Name; | |||
4206 | SourceLocation Loc; | |||
4207 | NamedDecl *ToD; | |||
4208 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
4209 | return std::move(Err); | |||
4210 | if (ToD) | |||
4211 | return ToD; | |||
4212 | ||||
4213 | // Try to find a variable in our own ("to") context with the same name and | |||
4214 | // in the same context as the variable we're importing. | |||
4215 | VarDecl *FoundByLookup = nullptr; | |||
4216 | if (D->isFileVarDecl()) { | |||
4217 | SmallVector<NamedDecl *, 4> ConflictingDecls; | |||
4218 | unsigned IDNS = Decl::IDNS_Ordinary; | |||
4219 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
4220 | for (auto *FoundDecl : FoundDecls) { | |||
4221 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | |||
4222 | continue; | |||
4223 | ||||
4224 | if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) { | |||
4225 | if (!hasSameVisibilityContextAndLinkage(FoundVar, D)) | |||
4226 | continue; | |||
4227 | if (Importer.IsStructurallyEquivalent(D->getType(), | |||
4228 | FoundVar->getType())) { | |||
4229 | ||||
4230 | // The VarDecl in the "From" context has a definition, but in the | |||
4231 | // "To" context we already have a definition. | |||
4232 | VarDecl *FoundDef = FoundVar->getDefinition(); | |||
4233 | if (D->isThisDeclarationADefinition() && FoundDef) | |||
4234 | // FIXME Check for ODR error if the two definitions have | |||
4235 | // different initializers? | |||
4236 | return Importer.MapImported(D, FoundDef); | |||
4237 | ||||
4238 | // The VarDecl in the "From" context has an initializer, but in the | |||
4239 | // "To" context we already have an initializer. | |||
4240 | const VarDecl *FoundDInit = nullptr; | |||
4241 | if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit)) | |||
4242 | // FIXME Diagnose ODR error if the two initializers are different? | |||
4243 | return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit)); | |||
4244 | ||||
4245 | FoundByLookup = FoundVar; | |||
4246 | break; | |||
4247 | } | |||
4248 | ||||
4249 | const ArrayType *FoundArray | |||
4250 | = Importer.getToContext().getAsArrayType(FoundVar->getType()); | |||
4251 | const ArrayType *TArray | |||
4252 | = Importer.getToContext().getAsArrayType(D->getType()); | |||
4253 | if (FoundArray && TArray) { | |||
4254 | if (isa<IncompleteArrayType>(FoundArray) && | |||
4255 | isa<ConstantArrayType>(TArray)) { | |||
4256 | // Import the type. | |||
4257 | if (auto TyOrErr = import(D->getType())) | |||
4258 | FoundVar->setType(*TyOrErr); | |||
4259 | else | |||
4260 | return TyOrErr.takeError(); | |||
4261 | ||||
4262 | FoundByLookup = FoundVar; | |||
4263 | break; | |||
4264 | } else if (isa<IncompleteArrayType>(TArray) && | |||
4265 | isa<ConstantArrayType>(FoundArray)) { | |||
4266 | FoundByLookup = FoundVar; | |||
4267 | break; | |||
4268 | } | |||
4269 | } | |||
4270 | ||||
4271 | Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent) | |||
4272 | << Name << D->getType() << FoundVar->getType(); | |||
4273 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) | |||
4274 | << FoundVar->getType(); | |||
4275 | ConflictingDecls.push_back(FoundDecl); | |||
4276 | } | |||
4277 | } | |||
4278 | ||||
4279 | if (!ConflictingDecls.empty()) { | |||
4280 | ExpectedName NameOrErr = Importer.HandleNameConflict( | |||
4281 | Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); | |||
4282 | if (NameOrErr) | |||
4283 | Name = NameOrErr.get(); | |||
4284 | else | |||
4285 | return NameOrErr.takeError(); | |||
4286 | } | |||
4287 | } | |||
4288 | ||||
4289 | Error Err = Error::success(); | |||
4290 | auto ToType = importChecked(Err, D->getType()); | |||
4291 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | |||
4292 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | |||
4293 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | |||
4294 | if (Err) | |||
4295 | return std::move(Err); | |||
4296 | ||||
4297 | VarDecl *ToVar; | |||
4298 | if (auto *FromDecomp = dyn_cast<DecompositionDecl>(D)) { | |||
4299 | SmallVector<BindingDecl *> Bindings(FromDecomp->bindings().size()); | |||
4300 | if (Error Err = | |||
4301 | ImportArrayChecked(FromDecomp->bindings(), Bindings.begin())) | |||
4302 | return std::move(Err); | |||
4303 | DecompositionDecl *ToDecomp; | |||
4304 | if (GetImportedOrCreateDecl( | |||
4305 | ToDecomp, FromDecomp, Importer.getToContext(), DC, ToInnerLocStart, | |||
4306 | Loc, ToType, ToTypeSourceInfo, D->getStorageClass(), Bindings)) | |||
4307 | return ToDecomp; | |||
4308 | ToVar = ToDecomp; | |||
4309 | } else { | |||
4310 | // Create the imported variable. | |||
4311 | if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC, | |||
4312 | ToInnerLocStart, Loc, | |||
4313 | Name.getAsIdentifierInfo(), ToType, | |||
4314 | ToTypeSourceInfo, D->getStorageClass())) | |||
4315 | return ToVar; | |||
4316 | } | |||
4317 | ||||
4318 | ToVar->setTSCSpec(D->getTSCSpec()); | |||
4319 | ToVar->setQualifierInfo(ToQualifierLoc); | |||
4320 | ToVar->setAccess(D->getAccess()); | |||
4321 | ToVar->setLexicalDeclContext(LexicalDC); | |||
4322 | ||||
4323 | if (FoundByLookup) { | |||
4324 | auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl()); | |||
4325 | ToVar->setPreviousDecl(Recent); | |||
4326 | } | |||
4327 | ||||
4328 | // Import the described template, if any. | |||
4329 | if (D->getDescribedVarTemplate()) { | |||
4330 | auto ToVTOrErr = import(D->getDescribedVarTemplate()); | |||
4331 | if (!ToVTOrErr) | |||
4332 | return ToVTOrErr.takeError(); | |||
4333 | } | |||
4334 | ||||
4335 | if (Error Err = ImportInitializer(D, ToVar)) | |||
4336 | return std::move(Err); | |||
4337 | ||||
4338 | if (D->isConstexpr()) | |||
4339 | ToVar->setConstexpr(true); | |||
4340 | ||||
4341 | addDeclToContexts(D, ToVar); | |||
4342 | ||||
4343 | // Import the rest of the chain. I.e. import all subsequent declarations. | |||
4344 | for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { | |||
4345 | ExpectedDecl RedeclOrErr = import(*RedeclIt); | |||
4346 | if (!RedeclOrErr) | |||
4347 | return RedeclOrErr.takeError(); | |||
4348 | } | |||
4349 | ||||
4350 | return ToVar; | |||
4351 | } | |||
4352 | ||||
4353 | ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { | |||
4354 | // Parameters are created in the translation unit's context, then moved | |||
4355 | // into the function declaration's context afterward. | |||
4356 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); | |||
4357 | ||||
4358 | Error Err = Error::success(); | |||
4359 | auto ToDeclName = importChecked(Err, D->getDeclName()); | |||
4360 | auto ToLocation = importChecked(Err, D->getLocation()); | |||
4361 | auto ToType = importChecked(Err, D->getType()); | |||
4362 | if (Err) | |||
4363 | return std::move(Err); | |||
4364 | ||||
4365 | // Create the imported parameter. | |||
4366 | ImplicitParamDecl *ToParm = nullptr; | |||
4367 | if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, | |||
4368 | ToLocation, ToDeclName.getAsIdentifierInfo(), | |||
4369 | ToType, D->getParameterKind())) | |||
4370 | return ToParm; | |||
4371 | return ToParm; | |||
4372 | } | |||
4373 | ||||
4374 | Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl( | |||
4375 | const ParmVarDecl *FromParam, ParmVarDecl *ToParam) { | |||
4376 | ToParam->setHasInheritedDefaultArg(FromParam->hasInheritedDefaultArg()); | |||
4377 | ToParam->setKNRPromoted(FromParam->isKNRPromoted()); | |||
4378 | ||||
4379 | if (FromParam->hasUninstantiatedDefaultArg()) { | |||
4380 | if (auto ToDefArgOrErr = import(FromParam->getUninstantiatedDefaultArg())) | |||
4381 | ToParam->setUninstantiatedDefaultArg(*ToDefArgOrErr); | |||
4382 | else | |||
4383 | return ToDefArgOrErr.takeError(); | |||
4384 | } else if (FromParam->hasUnparsedDefaultArg()) { | |||
4385 | ToParam->setUnparsedDefaultArg(); | |||
4386 | } else if (FromParam->hasDefaultArg()) { | |||
4387 | if (auto ToDefArgOrErr = import(FromParam->getDefaultArg())) | |||
4388 | ToParam->setDefaultArg(*ToDefArgOrErr); | |||
4389 | else | |||
4390 | return ToDefArgOrErr.takeError(); | |||
4391 | } | |||
4392 | ||||
4393 | return Error::success(); | |||
4394 | } | |||
4395 | ||||
4396 | Expected<InheritedConstructor> | |||
4397 | ASTNodeImporter::ImportInheritedConstructor(const InheritedConstructor &From) { | |||
4398 | Error Err = Error::success(); | |||
4399 | CXXConstructorDecl *ToBaseCtor = importChecked(Err, From.getConstructor()); | |||
4400 | ConstructorUsingShadowDecl *ToShadow = | |||
4401 | importChecked(Err, From.getShadowDecl()); | |||
4402 | if (Err) | |||
4403 | return std::move(Err); | |||
4404 | return InheritedConstructor(ToShadow, ToBaseCtor); | |||
4405 | } | |||
4406 | ||||
4407 | ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { | |||
4408 | // Parameters are created in the translation unit's context, then moved | |||
4409 | // into the function declaration's context afterward. | |||
4410 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); | |||
4411 | ||||
4412 | Error Err = Error::success(); | |||
4413 | auto ToDeclName = importChecked(Err, D->getDeclName()); | |||
4414 | auto ToLocation = importChecked(Err, D->getLocation()); | |||
4415 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | |||
4416 | auto ToType = importChecked(Err, D->getType()); | |||
4417 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | |||
4418 | if (Err) | |||
4419 | return std::move(Err); | |||
4420 | ||||
4421 | ParmVarDecl *ToParm; | |||
4422 | if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, | |||
4423 | ToInnerLocStart, ToLocation, | |||
4424 | ToDeclName.getAsIdentifierInfo(), ToType, | |||
4425 | ToTypeSourceInfo, D->getStorageClass(), | |||
4426 | /*DefaultArg*/ nullptr)) | |||
4427 | return ToParm; | |||
4428 | ||||
4429 | // Set the default argument. It should be no problem if it was already done. | |||
4430 | // Do not import the default expression before GetImportedOrCreateDecl call | |||
4431 | // to avoid possible infinite import loop because circular dependency. | |||
4432 | if (Error Err = ImportDefaultArgOfParmVarDecl(D, ToParm)) | |||
4433 | return std::move(Err); | |||
4434 | ||||
4435 | if (D->isObjCMethodParameter()) { | |||
4436 | ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex()); | |||
4437 | ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier()); | |||
4438 | } else { | |||
4439 | ToParm->setScopeInfo(D->getFunctionScopeDepth(), | |||
4440 | D->getFunctionScopeIndex()); | |||
4441 | } | |||
4442 | ||||
4443 | return ToParm; | |||
4444 | } | |||
4445 | ||||
4446 | ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { | |||
4447 | // Import the major distinguishing characteristics of a method. | |||
4448 | DeclContext *DC, *LexicalDC; | |||
4449 | DeclarationName Name; | |||
4450 | SourceLocation Loc; | |||
4451 | NamedDecl *ToD; | |||
4452 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
4453 | return std::move(Err); | |||
4454 | if (ToD) | |||
4455 | return ToD; | |||
4456 | ||||
4457 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
4458 | for (auto *FoundDecl : FoundDecls) { | |||
4459 | if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) { | |||
4460 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) | |||
4461 | continue; | |||
4462 | ||||
4463 | // Check return types. | |||
4464 | if (!Importer.IsStructurallyEquivalent(D->getReturnType(), | |||
4465 | FoundMethod->getReturnType())) { | |||
4466 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent) | |||
4467 | << D->isInstanceMethod() << Name << D->getReturnType() | |||
4468 | << FoundMethod->getReturnType(); | |||
4469 | Importer.ToDiag(FoundMethod->getLocation(), | |||
4470 | diag::note_odr_objc_method_here) | |||
4471 | << D->isInstanceMethod() << Name; | |||
4472 | ||||
4473 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
4474 | } | |||
4475 | ||||
4476 | // Check the number of parameters. | |||
4477 | if (D->param_size() != FoundMethod->param_size()) { | |||
4478 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent) | |||
4479 | << D->isInstanceMethod() << Name | |||
4480 | << D->param_size() << FoundMethod->param_size(); | |||
4481 | Importer.ToDiag(FoundMethod->getLocation(), | |||
4482 | diag::note_odr_objc_method_here) | |||
4483 | << D->isInstanceMethod() << Name; | |||
4484 | ||||
4485 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
4486 | } | |||
4487 | ||||
4488 | // Check parameter types. | |||
4489 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), | |||
4490 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); | |||
4491 | P != PEnd; ++P, ++FoundP) { | |||
4492 | if (!Importer.IsStructurallyEquivalent((*P)->getType(), | |||
4493 | (*FoundP)->getType())) { | |||
4494 | Importer.FromDiag((*P)->getLocation(), | |||
4495 | diag::warn_odr_objc_method_param_type_inconsistent) | |||
4496 | << D->isInstanceMethod() << Name | |||
4497 | << (*P)->getType() << (*FoundP)->getType(); | |||
4498 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) | |||
4499 | << (*FoundP)->getType(); | |||
4500 | ||||
4501 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
4502 | } | |||
4503 | } | |||
4504 | ||||
4505 | // Check variadic/non-variadic. | |||
4506 | // Check the number of parameters. | |||
4507 | if (D->isVariadic() != FoundMethod->isVariadic()) { | |||
4508 | Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent) | |||
4509 | << D->isInstanceMethod() << Name; | |||
4510 | Importer.ToDiag(FoundMethod->getLocation(), | |||
4511 | diag::note_odr_objc_method_here) | |||
4512 | << D->isInstanceMethod() << Name; | |||
4513 | ||||
4514 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
4515 | } | |||
4516 | ||||
4517 | // FIXME: Any other bits we need to merge? | |||
4518 | return Importer.MapImported(D, FoundMethod); | |||
4519 | } | |||
4520 | } | |||
4521 | ||||
4522 | Error Err = Error::success(); | |||
4523 | auto ToEndLoc = importChecked(Err, D->getEndLoc()); | |||
4524 | auto ToReturnType = importChecked(Err, D->getReturnType()); | |||
4525 | auto ToReturnTypeSourceInfo = | |||
4526 | importChecked(Err, D->getReturnTypeSourceInfo()); | |||
4527 | if (Err) | |||
4528 | return std::move(Err); | |||
4529 | ||||
4530 | ObjCMethodDecl *ToMethod; | |||
4531 | if (GetImportedOrCreateDecl( | |||
4532 | ToMethod, D, Importer.getToContext(), Loc, ToEndLoc, | |||
4533 | Name.getObjCSelector(), ToReturnType, ToReturnTypeSourceInfo, DC, | |||
4534 | D->isInstanceMethod(), D->isVariadic(), D->isPropertyAccessor(), | |||
4535 | D->isSynthesizedAccessorStub(), D->isImplicit(), D->isDefined(), | |||
4536 | D->getImplementationControl(), D->hasRelatedResultType())) | |||
4537 | return ToMethod; | |||
4538 | ||||
4539 | // FIXME: When we decide to merge method definitions, we'll need to | |||
4540 | // deal with implicit parameters. | |||
4541 | ||||
4542 | // Import the parameters | |||
4543 | SmallVector<ParmVarDecl *, 5> ToParams; | |||
4544 | for (auto *FromP : D->parameters()) { | |||
4545 | if (Expected<ParmVarDecl *> ToPOrErr = import(FromP)) | |||
4546 | ToParams.push_back(*ToPOrErr); | |||
4547 | else | |||
4548 | return ToPOrErr.takeError(); | |||
4549 | } | |||
4550 | ||||
4551 | // Set the parameters. | |||
4552 | for (auto *ToParam : ToParams) { | |||
4553 | ToParam->setOwningFunction(ToMethod); | |||
4554 | ToMethod->addDeclInternal(ToParam); | |||
4555 | } | |||
4556 | ||||
4557 | SmallVector<SourceLocation, 12> FromSelLocs; | |||
4558 | D->getSelectorLocs(FromSelLocs); | |||
4559 | SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size()); | |||
4560 | if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs)) | |||
4561 | return std::move(Err); | |||
4562 | ||||
4563 | ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs); | |||
4564 | ||||
4565 | ToMethod->setLexicalDeclContext(LexicalDC); | |||
4566 | LexicalDC->addDeclInternal(ToMethod); | |||
4567 | ||||
4568 | // Implicit params are declared when Sema encounters the definition but this | |||
4569 | // never happens when the method is imported. Manually declare the implicit | |||
4570 | // params now that the MethodDecl knows its class interface. | |||
4571 | if (D->getSelfDecl()) | |||
4572 | ToMethod->createImplicitParams(Importer.getToContext(), | |||
4573 | ToMethod->getClassInterface()); | |||
4574 | ||||
4575 | return ToMethod; | |||
4576 | } | |||
4577 | ||||
4578 | ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { | |||
4579 | // Import the major distinguishing characteristics of a category. | |||
4580 | DeclContext *DC, *LexicalDC; | |||
4581 | DeclarationName Name; | |||
4582 | SourceLocation Loc; | |||
4583 | NamedDecl *ToD; | |||
4584 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
4585 | return std::move(Err); | |||
4586 | if (ToD) | |||
4587 | return ToD; | |||
4588 | ||||
4589 | Error Err = Error::success(); | |||
4590 | auto ToVarianceLoc = importChecked(Err, D->getVarianceLoc()); | |||
4591 | auto ToLocation = importChecked(Err, D->getLocation()); | |||
4592 | auto ToColonLoc = importChecked(Err, D->getColonLoc()); | |||
4593 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | |||
4594 | if (Err) | |||
4595 | return std::move(Err); | |||
4596 | ||||
4597 | ObjCTypeParamDecl *Result; | |||
4598 | if (GetImportedOrCreateDecl( | |||
4599 | Result, D, Importer.getToContext(), DC, D->getVariance(), | |||
4600 | ToVarianceLoc, D->getIndex(), | |||
4601 | ToLocation, Name.getAsIdentifierInfo(), | |||
4602 | ToColonLoc, ToTypeSourceInfo)) | |||
4603 | return Result; | |||
4604 | ||||
4605 | Result->setLexicalDeclContext(LexicalDC); | |||
4606 | return Result; | |||
4607 | } | |||
4608 | ||||
4609 | ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { | |||
4610 | // Import the major distinguishing characteristics of a category. | |||
4611 | DeclContext *DC, *LexicalDC; | |||
4612 | DeclarationName Name; | |||
4613 | SourceLocation Loc; | |||
4614 | NamedDecl *ToD; | |||
4615 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
4616 | return std::move(Err); | |||
4617 | if (ToD) | |||
4618 | return ToD; | |||
4619 | ||||
4620 | ObjCInterfaceDecl *ToInterface; | |||
4621 | if (Error Err = importInto(ToInterface, D->getClassInterface())) | |||
4622 | return std::move(Err); | |||
4623 | ||||
4624 | // Determine if we've already encountered this category. | |||
4625 | ObjCCategoryDecl *MergeWithCategory | |||
4626 | = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); | |||
4627 | ObjCCategoryDecl *ToCategory = MergeWithCategory; | |||
4628 | if (!ToCategory) { | |||
4629 | ||||
4630 | Error Err = Error::success(); | |||
4631 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); | |||
4632 | auto ToCategoryNameLoc = importChecked(Err, D->getCategoryNameLoc()); | |||
4633 | auto ToIvarLBraceLoc = importChecked(Err, D->getIvarLBraceLoc()); | |||
4634 | auto ToIvarRBraceLoc = importChecked(Err, D->getIvarRBraceLoc()); | |||
4635 | if (Err) | |||
4636 | return std::move(Err); | |||
4637 | ||||
4638 | if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC, | |||
4639 | ToAtStartLoc, Loc, | |||
4640 | ToCategoryNameLoc, | |||
4641 | Name.getAsIdentifierInfo(), ToInterface, | |||
4642 | /*TypeParamList=*/nullptr, | |||
4643 | ToIvarLBraceLoc, | |||
4644 | ToIvarRBraceLoc)) | |||
4645 | return ToCategory; | |||
4646 | ||||
4647 | ToCategory->setLexicalDeclContext(LexicalDC); | |||
4648 | LexicalDC->addDeclInternal(ToCategory); | |||
4649 | // Import the type parameter list after MapImported, to avoid | |||
4650 | // loops when bringing in their DeclContext. | |||
4651 | if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList())) | |||
4652 | ToCategory->setTypeParamList(*PListOrErr); | |||
4653 | else | |||
4654 | return PListOrErr.takeError(); | |||
4655 | ||||
4656 | // Import protocols | |||
4657 | SmallVector<ObjCProtocolDecl *, 4> Protocols; | |||
4658 | SmallVector<SourceLocation, 4> ProtocolLocs; | |||
4659 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc | |||
4660 | = D->protocol_loc_begin(); | |||
4661 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), | |||
4662 | FromProtoEnd = D->protocol_end(); | |||
4663 | FromProto != FromProtoEnd; | |||
4664 | ++FromProto, ++FromProtoLoc) { | |||
4665 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto)) | |||
4666 | Protocols.push_back(*ToProtoOrErr); | |||
4667 | else | |||
4668 | return ToProtoOrErr.takeError(); | |||
4669 | ||||
4670 | if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc)) | |||
4671 | ProtocolLocs.push_back(*ToProtoLocOrErr); | |||
4672 | else | |||
4673 | return ToProtoLocOrErr.takeError(); | |||
4674 | } | |||
4675 | ||||
4676 | // FIXME: If we're merging, make sure that the protocol list is the same. | |||
4677 | ToCategory->setProtocolList(Protocols.data(), Protocols.size(), | |||
4678 | ProtocolLocs.data(), Importer.getToContext()); | |||
4679 | ||||
4680 | } else { | |||
4681 | Importer.MapImported(D, ToCategory); | |||
4682 | } | |||
4683 | ||||
4684 | // Import all of the members of this category. | |||
4685 | if (Error Err = ImportDeclContext(D)) | |||
4686 | return std::move(Err); | |||
4687 | ||||
4688 | // If we have an implementation, import it as well. | |||
4689 | if (D->getImplementation()) { | |||
4690 | if (Expected<ObjCCategoryImplDecl *> ToImplOrErr = | |||
4691 | import(D->getImplementation())) | |||
4692 | ToCategory->setImplementation(*ToImplOrErr); | |||
4693 | else | |||
4694 | return ToImplOrErr.takeError(); | |||
4695 | } | |||
4696 | ||||
4697 | return ToCategory; | |||
4698 | } | |||
4699 | ||||
4700 | Error ASTNodeImporter::ImportDefinition( | |||
4701 | ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) { | |||
4702 | if (To->getDefinition()) { | |||
4703 | if (shouldForceImportDeclContext(Kind)) | |||
4704 | if (Error Err = ImportDeclContext(From)) | |||
4705 | return Err; | |||
4706 | return Error::success(); | |||
4707 | } | |||
4708 | ||||
4709 | // Start the protocol definition | |||
4710 | To->startDefinition(); | |||
4711 | ||||
4712 | // Import protocols | |||
4713 | SmallVector<ObjCProtocolDecl *, 4> Protocols; | |||
4714 | SmallVector<SourceLocation, 4> ProtocolLocs; | |||
4715 | ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc = | |||
4716 | From->protocol_loc_begin(); | |||
4717 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), | |||
4718 | FromProtoEnd = From->protocol_end(); | |||
4719 | FromProto != FromProtoEnd; | |||
4720 | ++FromProto, ++FromProtoLoc) { | |||
4721 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto)) | |||
4722 | Protocols.push_back(*ToProtoOrErr); | |||
4723 | else | |||
4724 | return ToProtoOrErr.takeError(); | |||
4725 | ||||
4726 | if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc)) | |||
4727 | ProtocolLocs.push_back(*ToProtoLocOrErr); | |||
4728 | else | |||
4729 | return ToProtoLocOrErr.takeError(); | |||
4730 | ||||
4731 | } | |||
4732 | ||||
4733 | // FIXME: If we're merging, make sure that the protocol list is the same. | |||
4734 | To->setProtocolList(Protocols.data(), Protocols.size(), | |||
4735 | ProtocolLocs.data(), Importer.getToContext()); | |||
4736 | ||||
4737 | if (shouldForceImportDeclContext(Kind)) { | |||
4738 | // Import all of the members of this protocol. | |||
4739 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) | |||
4740 | return Err; | |||
4741 | } | |||
4742 | return Error::success(); | |||
4743 | } | |||
4744 | ||||
4745 | ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { | |||
4746 | // If this protocol has a definition in the translation unit we're coming | |||
4747 | // from, but this particular declaration is not that definition, import the | |||
4748 | // definition and map to that. | |||
4749 | ObjCProtocolDecl *Definition = D->getDefinition(); | |||
4750 | if (Definition && Definition != D) { | |||
4751 | if (ExpectedDecl ImportedDefOrErr = import(Definition)) | |||
4752 | return Importer.MapImported(D, *ImportedDefOrErr); | |||
4753 | else | |||
4754 | return ImportedDefOrErr.takeError(); | |||
4755 | } | |||
4756 | ||||
4757 | // Import the major distinguishing characteristics of a protocol. | |||
4758 | DeclContext *DC, *LexicalDC; | |||
4759 | DeclarationName Name; | |||
4760 | SourceLocation Loc; | |||
4761 | NamedDecl *ToD; | |||
4762 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
4763 | return std::move(Err); | |||
4764 | if (ToD) | |||
4765 | return ToD; | |||
4766 | ||||
4767 | ObjCProtocolDecl *MergeWithProtocol = nullptr; | |||
4768 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
4769 | for (auto *FoundDecl : FoundDecls) { | |||
4770 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) | |||
4771 | continue; | |||
4772 | ||||
4773 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl))) | |||
4774 | break; | |||
4775 | } | |||
4776 | ||||
4777 | ObjCProtocolDecl *ToProto = MergeWithProtocol; | |||
4778 | if (!ToProto) { | |||
4779 | auto ToAtBeginLocOrErr = import(D->getAtStartLoc()); | |||
4780 | if (!ToAtBeginLocOrErr) | |||
4781 | return ToAtBeginLocOrErr.takeError(); | |||
4782 | ||||
4783 | if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC, | |||
4784 | Name.getAsIdentifierInfo(), Loc, | |||
4785 | *ToAtBeginLocOrErr, | |||
4786 | /*PrevDecl=*/nullptr)) | |||
4787 | return ToProto; | |||
4788 | ToProto->setLexicalDeclContext(LexicalDC); | |||
4789 | LexicalDC->addDeclInternal(ToProto); | |||
4790 | } | |||
4791 | ||||
4792 | Importer.MapImported(D, ToProto); | |||
4793 | ||||
4794 | if (D->isThisDeclarationADefinition()) | |||
4795 | if (Error Err = ImportDefinition(D, ToProto)) | |||
4796 | return std::move(Err); | |||
4797 | ||||
4798 | return ToProto; | |||
4799 | } | |||
4800 | ||||
4801 | ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { | |||
4802 | DeclContext *DC, *LexicalDC; | |||
4803 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | |||
4804 | return std::move(Err); | |||
4805 | ||||
4806 | ExpectedSLoc ExternLocOrErr = import(D->getExternLoc()); | |||
4807 | if (!ExternLocOrErr) | |||
4808 | return ExternLocOrErr.takeError(); | |||
4809 | ||||
4810 | ExpectedSLoc LangLocOrErr = import(D->getLocation()); | |||
4811 | if (!LangLocOrErr) | |||
4812 | return LangLocOrErr.takeError(); | |||
4813 | ||||
4814 | bool HasBraces = D->hasBraces(); | |||
4815 | ||||
4816 | LinkageSpecDecl *ToLinkageSpec; | |||
4817 | if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC, | |||
4818 | *ExternLocOrErr, *LangLocOrErr, | |||
4819 | D->getLanguage(), HasBraces)) | |||
4820 | return ToLinkageSpec; | |||
4821 | ||||
4822 | if (HasBraces) { | |||
4823 | ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc()); | |||
4824 | if (!RBraceLocOrErr) | |||
4825 | return RBraceLocOrErr.takeError(); | |||
4826 | ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr); | |||
4827 | } | |||
4828 | ||||
4829 | ToLinkageSpec->setLexicalDeclContext(LexicalDC); | |||
4830 | LexicalDC->addDeclInternal(ToLinkageSpec); | |||
4831 | ||||
4832 | return ToLinkageSpec; | |||
4833 | } | |||
4834 | ||||
4835 | ExpectedDecl ASTNodeImporter::ImportUsingShadowDecls(BaseUsingDecl *D, | |||
4836 | BaseUsingDecl *ToSI) { | |||
4837 | for (UsingShadowDecl *FromShadow : D->shadows()) { | |||
4838 | if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow)) | |||
4839 | ToSI->addShadowDecl(*ToShadowOrErr); | |||
4840 | else | |||
4841 | // FIXME: We return error here but the definition is already created | |||
4842 | // and available with lookups. How to fix this?.. | |||
4843 | return ToShadowOrErr.takeError(); | |||
4844 | } | |||
4845 | return ToSI; | |||
4846 | } | |||
4847 | ||||
4848 | ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) { | |||
4849 | DeclContext *DC, *LexicalDC; | |||
4850 | DeclarationName Name; | |||
4851 | SourceLocation Loc; | |||
4852 | NamedDecl *ToD = nullptr; | |||
4853 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
4854 | return std::move(Err); | |||
4855 | if (ToD) | |||
4856 | return ToD; | |||
4857 | ||||
4858 | Error Err = Error::success(); | |||
4859 | auto ToLoc = importChecked(Err, D->getNameInfo().getLoc()); | |||
4860 | auto ToUsingLoc = importChecked(Err, D->getUsingLoc()); | |||
4861 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | |||
4862 | if (Err) | |||
4863 | return std::move(Err); | |||
4864 | ||||
4865 | DeclarationNameInfo NameInfo(Name, ToLoc); | |||
4866 | if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo)) | |||
4867 | return std::move(Err); | |||
4868 | ||||
4869 | UsingDecl *ToUsing; | |||
4870 | if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC, | |||
4871 | ToUsingLoc, ToQualifierLoc, NameInfo, | |||
4872 | D->hasTypename())) | |||
4873 | return ToUsing; | |||
4874 | ||||
4875 | ToUsing->setLexicalDeclContext(LexicalDC); | |||
4876 | LexicalDC->addDeclInternal(ToUsing); | |||
4877 | ||||
4878 | if (NamedDecl *FromPattern = | |||
4879 | Importer.getFromContext().getInstantiatedFromUsingDecl(D)) { | |||
4880 | if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern)) | |||
4881 | Importer.getToContext().setInstantiatedFromUsingDecl( | |||
4882 | ToUsing, *ToPatternOrErr); | |||
4883 | else | |||
4884 | return ToPatternOrErr.takeError(); | |||
4885 | } | |||
4886 | ||||
4887 | return ImportUsingShadowDecls(D, ToUsing); | |||
4888 | } | |||
4889 | ||||
4890 | ExpectedDecl ASTNodeImporter::VisitUsingEnumDecl(UsingEnumDecl *D) { | |||
4891 | DeclContext *DC, *LexicalDC; | |||
4892 | DeclarationName Name; | |||
4893 | SourceLocation Loc; | |||
4894 | NamedDecl *ToD = nullptr; | |||
4895 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
4896 | return std::move(Err); | |||
4897 | if (ToD) | |||
4898 | return ToD; | |||
4899 | ||||
4900 | Error Err = Error::success(); | |||
4901 | auto ToUsingLoc = importChecked(Err, D->getUsingLoc()); | |||
4902 | auto ToEnumLoc = importChecked(Err, D->getEnumLoc()); | |||
4903 | auto ToNameLoc = importChecked(Err, D->getLocation()); | |||
4904 | auto *ToEnumType = importChecked(Err, D->getEnumType()); | |||
4905 | if (Err) | |||
4906 | return std::move(Err); | |||
4907 | ||||
4908 | UsingEnumDecl *ToUsingEnum; | |||
4909 | if (GetImportedOrCreateDecl(ToUsingEnum, D, Importer.getToContext(), DC, | |||
4910 | ToUsingLoc, ToEnumLoc, ToNameLoc, ToEnumType)) | |||
4911 | return ToUsingEnum; | |||
4912 | ||||
4913 | ToUsingEnum->setLexicalDeclContext(LexicalDC); | |||
4914 | LexicalDC->addDeclInternal(ToUsingEnum); | |||
4915 | ||||
4916 | if (UsingEnumDecl *FromPattern = | |||
4917 | Importer.getFromContext().getInstantiatedFromUsingEnumDecl(D)) { | |||
4918 | if (Expected<UsingEnumDecl *> ToPatternOrErr = import(FromPattern)) | |||
4919 | Importer.getToContext().setInstantiatedFromUsingEnumDecl(ToUsingEnum, | |||
4920 | *ToPatternOrErr); | |||
4921 | else | |||
4922 | return ToPatternOrErr.takeError(); | |||
4923 | } | |||
4924 | ||||
4925 | return ImportUsingShadowDecls(D, ToUsingEnum); | |||
4926 | } | |||
4927 | ||||
4928 | ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) { | |||
4929 | DeclContext *DC, *LexicalDC; | |||
4930 | DeclarationName Name; | |||
4931 | SourceLocation Loc; | |||
4932 | NamedDecl *ToD = nullptr; | |||
4933 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
4934 | return std::move(Err); | |||
4935 | if (ToD) | |||
4936 | return ToD; | |||
4937 | ||||
4938 | Expected<BaseUsingDecl *> ToIntroducerOrErr = import(D->getIntroducer()); | |||
4939 | if (!ToIntroducerOrErr) | |||
4940 | return ToIntroducerOrErr.takeError(); | |||
4941 | ||||
4942 | Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl()); | |||
4943 | if (!ToTargetOrErr) | |||
4944 | return ToTargetOrErr.takeError(); | |||
4945 | ||||
4946 | UsingShadowDecl *ToShadow; | |||
4947 | if (auto *FromConstructorUsingShadow = | |||
4948 | dyn_cast<ConstructorUsingShadowDecl>(D)) { | |||
4949 | Error Err = Error::success(); | |||
4950 | ConstructorUsingShadowDecl *Nominated = importChecked( | |||
4951 | Err, FromConstructorUsingShadow->getNominatedBaseClassShadowDecl()); | |||
4952 | if (Err) | |||
4953 | return std::move(Err); | |||
4954 | // The 'Target' parameter of ConstructorUsingShadowDecl constructor | |||
4955 | // is really the "NominatedBaseClassShadowDecl" value if it exists | |||
4956 | // (see code of ConstructorUsingShadowDecl::ConstructorUsingShadowDecl). | |||
4957 | // We should pass the NominatedBaseClassShadowDecl to it (if non-null) to | |||
4958 | // get the correct values. | |||
4959 | if (GetImportedOrCreateDecl<ConstructorUsingShadowDecl>( | |||
4960 | ToShadow, D, Importer.getToContext(), DC, Loc, | |||
4961 | cast<UsingDecl>(*ToIntroducerOrErr), | |||
4962 | Nominated ? Nominated : *ToTargetOrErr, | |||
4963 | FromConstructorUsingShadow->constructsVirtualBase())) | |||
4964 | return ToShadow; | |||
4965 | } else { | |||
4966 | if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc, | |||
4967 | Name, *ToIntroducerOrErr, *ToTargetOrErr)) | |||
4968 | return ToShadow; | |||
4969 | } | |||
4970 | ||||
4971 | ToShadow->setLexicalDeclContext(LexicalDC); | |||
4972 | ToShadow->setAccess(D->getAccess()); | |||
4973 | ||||
4974 | if (UsingShadowDecl *FromPattern = | |||
4975 | Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) { | |||
4976 | if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern)) | |||
4977 | Importer.getToContext().setInstantiatedFromUsingShadowDecl( | |||
4978 | ToShadow, *ToPatternOrErr); | |||
4979 | else | |||
4980 | // FIXME: We return error here but the definition is already created | |||
4981 | // and available with lookups. How to fix this?.. | |||
4982 | return ToPatternOrErr.takeError(); | |||
4983 | } | |||
4984 | ||||
4985 | LexicalDC->addDeclInternal(ToShadow); | |||
4986 | ||||
4987 | return ToShadow; | |||
4988 | } | |||
4989 | ||||
4990 | ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { | |||
4991 | DeclContext *DC, *LexicalDC; | |||
4992 | DeclarationName Name; | |||
4993 | SourceLocation Loc; | |||
4994 | NamedDecl *ToD = nullptr; | |||
4995 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
4996 | return std::move(Err); | |||
4997 | if (ToD) | |||
4998 | return ToD; | |||
4999 | ||||
5000 | auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor()); | |||
5001 | if (!ToComAncestorOrErr) | |||
5002 | return ToComAncestorOrErr.takeError(); | |||
5003 | ||||
5004 | Error Err = Error::success(); | |||
5005 | auto ToNominatedNamespace = importChecked(Err, D->getNominatedNamespace()); | |||
5006 | auto ToUsingLoc = importChecked(Err, D->getUsingLoc()); | |||
5007 | auto ToNamespaceKeyLocation = | |||
5008 | importChecked(Err, D->getNamespaceKeyLocation()); | |||
5009 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | |||
5010 | auto ToIdentLocation = importChecked(Err, D->getIdentLocation()); | |||
5011 | if (Err) | |||
5012 | return std::move(Err); | |||
5013 | ||||
5014 | UsingDirectiveDecl *ToUsingDir; | |||
5015 | if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC, | |||
5016 | ToUsingLoc, | |||
5017 | ToNamespaceKeyLocation, | |||
5018 | ToQualifierLoc, | |||
5019 | ToIdentLocation, | |||
5020 | ToNominatedNamespace, *ToComAncestorOrErr)) | |||
5021 | return ToUsingDir; | |||
5022 | ||||
5023 | ToUsingDir->setLexicalDeclContext(LexicalDC); | |||
5024 | LexicalDC->addDeclInternal(ToUsingDir); | |||
5025 | ||||
5026 | return ToUsingDir; | |||
5027 | } | |||
5028 | ||||
5029 | ExpectedDecl ASTNodeImporter::VisitUsingPackDecl(UsingPackDecl *D) { | |||
5030 | DeclContext *DC, *LexicalDC; | |||
5031 | DeclarationName Name; | |||
5032 | SourceLocation Loc; | |||
5033 | NamedDecl *ToD = nullptr; | |||
5034 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
5035 | return std::move(Err); | |||
5036 | if (ToD) | |||
5037 | return ToD; | |||
5038 | ||||
5039 | auto ToInstantiatedFromUsingOrErr = | |||
5040 | Importer.Import(D->getInstantiatedFromUsingDecl()); | |||
5041 | if (!ToInstantiatedFromUsingOrErr) | |||
5042 | return ToInstantiatedFromUsingOrErr.takeError(); | |||
5043 | SmallVector<NamedDecl *, 4> Expansions(D->expansions().size()); | |||
5044 | if (Error Err = ImportArrayChecked(D->expansions(), Expansions.begin())) | |||
5045 | return std::move(Err); | |||
5046 | ||||
5047 | UsingPackDecl *ToUsingPack; | |||
5048 | if (GetImportedOrCreateDecl(ToUsingPack, D, Importer.getToContext(), DC, | |||
5049 | cast<NamedDecl>(*ToInstantiatedFromUsingOrErr), | |||
5050 | Expansions)) | |||
5051 | return ToUsingPack; | |||
5052 | ||||
5053 | addDeclToContexts(D, ToUsingPack); | |||
5054 | ||||
5055 | return ToUsingPack; | |||
5056 | } | |||
5057 | ||||
5058 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl( | |||
5059 | UnresolvedUsingValueDecl *D) { | |||
5060 | DeclContext *DC, *LexicalDC; | |||
5061 | DeclarationName Name; | |||
5062 | SourceLocation Loc; | |||
5063 | NamedDecl *ToD = nullptr; | |||
5064 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
5065 | return std::move(Err); | |||
5066 | if (ToD) | |||
5067 | return ToD; | |||
5068 | ||||
5069 | Error Err = Error::success(); | |||
5070 | auto ToLoc = importChecked(Err, D->getNameInfo().getLoc()); | |||
5071 | auto ToUsingLoc = importChecked(Err, D->getUsingLoc()); | |||
5072 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | |||
5073 | auto ToEllipsisLoc = importChecked(Err, D->getEllipsisLoc()); | |||
5074 | if (Err) | |||
5075 | return std::move(Err); | |||
5076 | ||||
5077 | DeclarationNameInfo NameInfo(Name, ToLoc); | |||
5078 | if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo)) | |||
5079 | return std::move(Err); | |||
5080 | ||||
5081 | UnresolvedUsingValueDecl *ToUsingValue; | |||
5082 | if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC, | |||
5083 | ToUsingLoc, ToQualifierLoc, NameInfo, | |||
5084 | ToEllipsisLoc)) | |||
5085 | return ToUsingValue; | |||
5086 | ||||
5087 | ToUsingValue->setAccess(D->getAccess()); | |||
5088 | ToUsingValue->setLexicalDeclContext(LexicalDC); | |||
5089 | LexicalDC->addDeclInternal(ToUsingValue); | |||
5090 | ||||
5091 | return ToUsingValue; | |||
5092 | } | |||
5093 | ||||
5094 | ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl( | |||
5095 | UnresolvedUsingTypenameDecl *D) { | |||
5096 | DeclContext *DC, *LexicalDC; | |||
5097 | DeclarationName Name; | |||
5098 | SourceLocation Loc; | |||
5099 | NamedDecl *ToD = nullptr; | |||
5100 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
5101 | return std::move(Err); | |||
5102 | if (ToD) | |||
5103 | return ToD; | |||
5104 | ||||
5105 | Error Err = Error::success(); | |||
5106 | auto ToUsingLoc = importChecked(Err, D->getUsingLoc()); | |||
5107 | auto ToTypenameLoc = importChecked(Err, D->getTypenameLoc()); | |||
5108 | auto ToQualifierLoc = importChecked(Err, D->getQualifierLoc()); | |||
5109 | auto ToEllipsisLoc = importChecked(Err, D->getEllipsisLoc()); | |||
5110 | if (Err) | |||
5111 | return std::move(Err); | |||
5112 | ||||
5113 | UnresolvedUsingTypenameDecl *ToUsing; | |||
5114 | if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC, | |||
5115 | ToUsingLoc, ToTypenameLoc, | |||
5116 | ToQualifierLoc, Loc, Name, ToEllipsisLoc)) | |||
5117 | return ToUsing; | |||
5118 | ||||
5119 | ToUsing->setAccess(D->getAccess()); | |||
5120 | ToUsing->setLexicalDeclContext(LexicalDC); | |||
5121 | LexicalDC->addDeclInternal(ToUsing); | |||
5122 | ||||
5123 | return ToUsing; | |||
5124 | } | |||
5125 | ||||
5126 | ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { | |||
5127 | Decl* ToD = nullptr; | |||
5128 | switch (D->getBuiltinTemplateKind()) { | |||
5129 | case BuiltinTemplateKind::BTK__make_integer_seq: | |||
5130 | ToD = Importer.getToContext().getMakeIntegerSeqDecl(); | |||
5131 | break; | |||
5132 | case BuiltinTemplateKind::BTK__type_pack_element: | |||
5133 | ToD = Importer.getToContext().getTypePackElementDecl(); | |||
5134 | break; | |||
5135 | } | |||
5136 | assert(ToD && "BuiltinTemplateDecl of unsupported kind!")(static_cast <bool> (ToD && "BuiltinTemplateDecl of unsupported kind!" ) ? void (0) : __assert_fail ("ToD && \"BuiltinTemplateDecl of unsupported kind!\"" , "clang/lib/AST/ASTImporter.cpp", 5136, __extension__ __PRETTY_FUNCTION__ )); | |||
5137 | Importer.MapImported(D, ToD); | |||
5138 | return ToD; | |||
5139 | } | |||
5140 | ||||
5141 | Error ASTNodeImporter::ImportDefinition( | |||
5142 | ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) { | |||
5143 | if (To->getDefinition()) { | |||
5144 | // Check consistency of superclass. | |||
5145 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); | |||
5146 | if (FromSuper) { | |||
5147 | if (auto FromSuperOrErr = import(FromSuper)) | |||
5148 | FromSuper = *FromSuperOrErr; | |||
5149 | else | |||
5150 | return FromSuperOrErr.takeError(); | |||
5151 | } | |||
5152 | ||||
5153 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); | |||
5154 | if ((bool)FromSuper != (bool)ToSuper || | |||
5155 | (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { | |||
5156 | Importer.ToDiag(To->getLocation(), | |||
5157 | diag::warn_odr_objc_superclass_inconsistent) | |||
5158 | << To->getDeclName(); | |||
5159 | if (ToSuper) | |||
5160 | Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) | |||
5161 | << To->getSuperClass()->getDeclName(); | |||
5162 | else | |||
5163 | Importer.ToDiag(To->getLocation(), | |||
5164 | diag::note_odr_objc_missing_superclass); | |||
5165 | if (From->getSuperClass()) | |||
5166 | Importer.FromDiag(From->getSuperClassLoc(), | |||
5167 | diag::note_odr_objc_superclass) | |||
5168 | << From->getSuperClass()->getDeclName(); | |||
5169 | else | |||
5170 | Importer.FromDiag(From->getLocation(), | |||
5171 | diag::note_odr_objc_missing_superclass); | |||
5172 | } | |||
5173 | ||||
5174 | if (shouldForceImportDeclContext(Kind)) | |||
5175 | if (Error Err = ImportDeclContext(From)) | |||
5176 | return Err; | |||
5177 | return Error::success(); | |||
5178 | } | |||
5179 | ||||
5180 | // Start the definition. | |||
5181 | To->startDefinition(); | |||
5182 | ||||
5183 | // If this class has a superclass, import it. | |||
5184 | if (From->getSuperClass()) { | |||
5185 | if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo())) | |||
5186 | To->setSuperClass(*SuperTInfoOrErr); | |||
5187 | else | |||
5188 | return SuperTInfoOrErr.takeError(); | |||
5189 | } | |||
5190 | ||||
5191 | // Import protocols | |||
5192 | SmallVector<ObjCProtocolDecl *, 4> Protocols; | |||
5193 | SmallVector<SourceLocation, 4> ProtocolLocs; | |||
5194 | ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc = | |||
5195 | From->protocol_loc_begin(); | |||
5196 | ||||
5197 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), | |||
5198 | FromProtoEnd = From->protocol_end(); | |||
5199 | FromProto != FromProtoEnd; | |||
5200 | ++FromProto, ++FromProtoLoc) { | |||
5201 | if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto)) | |||
5202 | Protocols.push_back(*ToProtoOrErr); | |||
5203 | else | |||
5204 | return ToProtoOrErr.takeError(); | |||
5205 | ||||
5206 | if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc)) | |||
5207 | ProtocolLocs.push_back(*ToProtoLocOrErr); | |||
5208 | else | |||
5209 | return ToProtoLocOrErr.takeError(); | |||
5210 | ||||
5211 | } | |||
5212 | ||||
5213 | // FIXME: If we're merging, make sure that the protocol list is the same. | |||
5214 | To->setProtocolList(Protocols.data(), Protocols.size(), | |||
5215 | ProtocolLocs.data(), Importer.getToContext()); | |||
5216 | ||||
5217 | // Import categories. When the categories themselves are imported, they'll | |||
5218 | // hook themselves into this interface. | |||
5219 | for (auto *Cat : From->known_categories()) { | |||
5220 | auto ToCatOrErr = import(Cat); | |||
5221 | if (!ToCatOrErr) | |||
5222 | return ToCatOrErr.takeError(); | |||
5223 | } | |||
5224 | ||||
5225 | // If we have an @implementation, import it as well. | |||
5226 | if (From->getImplementation()) { | |||
5227 | if (Expected<ObjCImplementationDecl *> ToImplOrErr = | |||
5228 | import(From->getImplementation())) | |||
5229 | To->setImplementation(*ToImplOrErr); | |||
5230 | else | |||
5231 | return ToImplOrErr.takeError(); | |||
5232 | } | |||
5233 | ||||
5234 | // Import all of the members of this class. | |||
5235 | if (Error Err = ImportDeclContext(From, /*ForceImport=*/true)) | |||
5236 | return Err; | |||
5237 | ||||
5238 | return Error::success(); | |||
5239 | } | |||
5240 | ||||
5241 | Expected<ObjCTypeParamList *> | |||
5242 | ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { | |||
5243 | if (!list) | |||
5244 | return nullptr; | |||
5245 | ||||
5246 | SmallVector<ObjCTypeParamDecl *, 4> toTypeParams; | |||
5247 | for (auto *fromTypeParam : *list) { | |||
5248 | if (auto toTypeParamOrErr = import(fromTypeParam)) | |||
5249 | toTypeParams.push_back(*toTypeParamOrErr); | |||
5250 | else | |||
5251 | return toTypeParamOrErr.takeError(); | |||
5252 | } | |||
5253 | ||||
5254 | auto LAngleLocOrErr = import(list->getLAngleLoc()); | |||
5255 | if (!LAngleLocOrErr) | |||
5256 | return LAngleLocOrErr.takeError(); | |||
5257 | ||||
5258 | auto RAngleLocOrErr = import(list->getRAngleLoc()); | |||
5259 | if (!RAngleLocOrErr) | |||
5260 | return RAngleLocOrErr.takeError(); | |||
5261 | ||||
5262 | return ObjCTypeParamList::create(Importer.getToContext(), | |||
5263 | *LAngleLocOrErr, | |||
5264 | toTypeParams, | |||
5265 | *RAngleLocOrErr); | |||
5266 | } | |||
5267 | ||||
5268 | ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { | |||
5269 | // If this class has a definition in the translation unit we're coming from, | |||
5270 | // but this particular declaration is not that definition, import the | |||
5271 | // definition and map to that. | |||
5272 | ObjCInterfaceDecl *Definition = D->getDefinition(); | |||
5273 | if (Definition && Definition != D) { | |||
5274 | if (ExpectedDecl ImportedDefOrErr = import(Definition)) | |||
5275 | return Importer.MapImported(D, *ImportedDefOrErr); | |||
5276 | else | |||
5277 | return ImportedDefOrErr.takeError(); | |||
5278 | } | |||
5279 | ||||
5280 | // Import the major distinguishing characteristics of an @interface. | |||
5281 | DeclContext *DC, *LexicalDC; | |||
5282 | DeclarationName Name; | |||
5283 | SourceLocation Loc; | |||
5284 | NamedDecl *ToD; | |||
5285 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
5286 | return std::move(Err); | |||
5287 | if (ToD) | |||
5288 | return ToD; | |||
5289 | ||||
5290 | // Look for an existing interface with the same name. | |||
5291 | ObjCInterfaceDecl *MergeWithIface = nullptr; | |||
5292 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
5293 | for (auto *FoundDecl : FoundDecls) { | |||
5294 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) | |||
5295 | continue; | |||
5296 | ||||
5297 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl))) | |||
5298 | break; | |||
5299 | } | |||
5300 | ||||
5301 | // Create an interface declaration, if one does not already exist. | |||
5302 | ObjCInterfaceDecl *ToIface = MergeWithIface; | |||
5303 | if (!ToIface) { | |||
5304 | ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc()); | |||
5305 | if (!AtBeginLocOrErr) | |||
5306 | return AtBeginLocOrErr.takeError(); | |||
5307 | ||||
5308 | if (GetImportedOrCreateDecl( | |||
5309 | ToIface, D, Importer.getToContext(), DC, | |||
5310 | *AtBeginLocOrErr, Name.getAsIdentifierInfo(), | |||
5311 | /*TypeParamList=*/nullptr, | |||
5312 | /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl())) | |||
5313 | return ToIface; | |||
5314 | ToIface->setLexicalDeclContext(LexicalDC); | |||
5315 | LexicalDC->addDeclInternal(ToIface); | |||
5316 | } | |||
5317 | Importer.MapImported(D, ToIface); | |||
5318 | // Import the type parameter list after MapImported, to avoid | |||
5319 | // loops when bringing in their DeclContext. | |||
5320 | if (auto ToPListOrErr = | |||
5321 | ImportObjCTypeParamList(D->getTypeParamListAsWritten())) | |||
5322 | ToIface->setTypeParamList(*ToPListOrErr); | |||
5323 | else | |||
5324 | return ToPListOrErr.takeError(); | |||
5325 | ||||
5326 | if (D->isThisDeclarationADefinition()) | |||
5327 | if (Error Err = ImportDefinition(D, ToIface)) | |||
5328 | return std::move(Err); | |||
5329 | ||||
5330 | return ToIface; | |||
5331 | } | |||
5332 | ||||
5333 | ExpectedDecl | |||
5334 | ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { | |||
5335 | ObjCCategoryDecl *Category; | |||
5336 | if (Error Err = importInto(Category, D->getCategoryDecl())) | |||
5337 | return std::move(Err); | |||
5338 | ||||
5339 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); | |||
5340 | if (!ToImpl) { | |||
5341 | DeclContext *DC, *LexicalDC; | |||
5342 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | |||
5343 | return std::move(Err); | |||
5344 | ||||
5345 | Error Err = Error::success(); | |||
5346 | auto ToLocation = importChecked(Err, D->getLocation()); | |||
5347 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); | |||
5348 | auto ToCategoryNameLoc = importChecked(Err, D->getCategoryNameLoc()); | |||
5349 | if (Err) | |||
5350 | return std::move(Err); | |||
5351 | ||||
5352 | if (GetImportedOrCreateDecl( | |||
5353 | ToImpl, D, Importer.getToContext(), DC, | |||
5354 | Importer.Import(D->getIdentifier()), Category->getClassInterface(), | |||
5355 | ToLocation, ToAtStartLoc, ToCategoryNameLoc)) | |||
5356 | return ToImpl; | |||
5357 | ||||
5358 | ToImpl->setLexicalDeclContext(LexicalDC); | |||
5359 | LexicalDC->addDeclInternal(ToImpl); | |||
5360 | Category->setImplementation(ToImpl); | |||
5361 | } | |||
5362 | ||||
5363 | Importer.MapImported(D, ToImpl); | |||
5364 | if (Error Err = ImportDeclContext(D)) | |||
5365 | return std::move(Err); | |||
5366 | ||||
5367 | return ToImpl; | |||
5368 | } | |||
5369 | ||||
5370 | ExpectedDecl | |||
5371 | ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { | |||
5372 | // Find the corresponding interface. | |||
5373 | ObjCInterfaceDecl *Iface; | |||
5374 | if (Error Err = importInto(Iface, D->getClassInterface())) | |||
5375 | return std::move(Err); | |||
5376 | ||||
5377 | // Import the superclass, if any. | |||
5378 | ObjCInterfaceDecl *Super; | |||
5379 | if (Error Err = importInto(Super, D->getSuperClass())) | |||
5380 | return std::move(Err); | |||
5381 | ||||
5382 | ObjCImplementationDecl *Impl = Iface->getImplementation(); | |||
5383 | if (!Impl) { | |||
5384 | // We haven't imported an implementation yet. Create a new @implementation | |||
5385 | // now. | |||
5386 | DeclContext *DC, *LexicalDC; | |||
5387 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | |||
5388 | return std::move(Err); | |||
5389 | ||||
5390 | Error Err = Error::success(); | |||
5391 | auto ToLocation = importChecked(Err, D->getLocation()); | |||
5392 | auto ToAtStartLoc = importChecked(Err, D->getAtStartLoc()); | |||
5393 | auto ToSuperClassLoc = importChecked(Err, D->getSuperClassLoc()); | |||
5394 | auto ToIvarLBraceLoc = importChecked(Err, D->getIvarLBraceLoc()); | |||
5395 | auto ToIvarRBraceLoc = importChecked(Err, D->getIvarRBraceLoc()); | |||
5396 | if (Err) | |||
5397 | return std::move(Err); | |||
5398 | ||||
5399 | if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(), | |||
5400 | DC, Iface, Super, | |||
5401 | ToLocation, | |||
5402 | ToAtStartLoc, | |||
5403 | ToSuperClassLoc, | |||
5404 | ToIvarLBraceLoc, | |||
5405 | ToIvarRBraceLoc)) | |||
5406 | return Impl; | |||
5407 | ||||
5408 | Impl->setLexicalDeclContext(LexicalDC); | |||
5409 | ||||
5410 | // Associate the implementation with the class it implements. | |||
5411 | Iface->setImplementation(Impl); | |||
5412 | Importer.MapImported(D, Iface->getImplementation()); | |||
5413 | } else { | |||
5414 | Importer.MapImported(D, Iface->getImplementation()); | |||
5415 | ||||
5416 | // Verify that the existing @implementation has the same superclass. | |||
5417 | if ((Super && !Impl->getSuperClass()) || | |||
5418 | (!Super && Impl->getSuperClass()) || | |||
5419 | (Super && Impl->getSuperClass() && | |||
5420 | !declaresSameEntity(Super->getCanonicalDecl(), | |||
5421 | Impl->getSuperClass()))) { | |||
5422 | Importer.ToDiag(Impl->getLocation(), | |||
5423 | diag::warn_odr_objc_superclass_inconsistent) | |||
5424 | << Iface->getDeclName(); | |||
5425 | // FIXME: It would be nice to have the location of the superclass | |||
5426 | // below. | |||
5427 | if (Impl->getSuperClass()) | |||
5428 | Importer.ToDiag(Impl->getLocation(), | |||
5429 | diag::note_odr_objc_superclass) | |||
5430 | << Impl->getSuperClass()->getDeclName(); | |||
5431 | else | |||
5432 | Importer.ToDiag(Impl->getLocation(), | |||
5433 | diag::note_odr_objc_missing_superclass); | |||
5434 | if (D->getSuperClass()) | |||
5435 | Importer.FromDiag(D->getLocation(), | |||
5436 | diag::note_odr_objc_superclass) | |||
5437 | << D->getSuperClass()->getDeclName(); | |||
5438 | else | |||
5439 | Importer.FromDiag(D->getLocation(), | |||
5440 | diag::note_odr_objc_missing_superclass); | |||
5441 | ||||
5442 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
5443 | } | |||
5444 | } | |||
5445 | ||||
5446 | // Import all of the members of this @implementation. | |||
5447 | if (Error Err = ImportDeclContext(D)) | |||
5448 | return std::move(Err); | |||
5449 | ||||
5450 | return Impl; | |||
5451 | } | |||
5452 | ||||
5453 | ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { | |||
5454 | // Import the major distinguishing characteristics of an @property. | |||
5455 | DeclContext *DC, *LexicalDC; | |||
5456 | DeclarationName Name; | |||
5457 | SourceLocation Loc; | |||
5458 | NamedDecl *ToD; | |||
5459 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
5460 | return std::move(Err); | |||
5461 | if (ToD) | |||
5462 | return ToD; | |||
5463 | ||||
5464 | // Check whether we have already imported this property. | |||
5465 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
5466 | for (auto *FoundDecl : FoundDecls) { | |||
5467 | if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) { | |||
5468 | // Instance and class properties can share the same name but are different | |||
5469 | // declarations. | |||
5470 | if (FoundProp->isInstanceProperty() != D->isInstanceProperty()) | |||
5471 | continue; | |||
5472 | ||||
5473 | // Check property types. | |||
5474 | if (!Importer.IsStructurallyEquivalent(D->getType(), | |||
5475 | FoundProp->getType())) { | |||
5476 | Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent) | |||
5477 | << Name << D->getType() << FoundProp->getType(); | |||
5478 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) | |||
5479 | << FoundProp->getType(); | |||
5480 | ||||
5481 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
5482 | } | |||
5483 | ||||
5484 | // FIXME: Check property attributes, getters, setters, etc.? | |||
5485 | ||||
5486 | // Consider these properties to be equivalent. | |||
5487 | Importer.MapImported(D, FoundProp); | |||
5488 | return FoundProp; | |||
5489 | } | |||
5490 | } | |||
5491 | ||||
5492 | Error Err = Error::success(); | |||
5493 | auto ToType = importChecked(Err, D->getType()); | |||
5494 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | |||
5495 | auto ToAtLoc = importChecked(Err, D->getAtLoc()); | |||
5496 | auto ToLParenLoc = importChecked(Err, D->getLParenLoc()); | |||
5497 | if (Err) | |||
5498 | return std::move(Err); | |||
5499 | ||||
5500 | // Create the new property. | |||
5501 | ObjCPropertyDecl *ToProperty; | |||
5502 | if (GetImportedOrCreateDecl( | |||
5503 | ToProperty, D, Importer.getToContext(), DC, Loc, | |||
5504 | Name.getAsIdentifierInfo(), ToAtLoc, | |||
5505 | ToLParenLoc, ToType, | |||
5506 | ToTypeSourceInfo, D->getPropertyImplementation())) | |||
5507 | return ToProperty; | |||
5508 | ||||
5509 | auto ToGetterName = importChecked(Err, D->getGetterName()); | |||
5510 | auto ToSetterName = importChecked(Err, D->getSetterName()); | |||
5511 | auto ToGetterNameLoc = importChecked(Err, D->getGetterNameLoc()); | |||
5512 | auto ToSetterNameLoc = importChecked(Err, D->getSetterNameLoc()); | |||
5513 | auto ToGetterMethodDecl = importChecked(Err, D->getGetterMethodDecl()); | |||
5514 | auto ToSetterMethodDecl = importChecked(Err, D->getSetterMethodDecl()); | |||
5515 | auto ToPropertyIvarDecl = importChecked(Err, D->getPropertyIvarDecl()); | |||
5516 | if (Err) | |||
5517 | return std::move(Err); | |||
5518 | ||||
5519 | ToProperty->setLexicalDeclContext(LexicalDC); | |||
5520 | LexicalDC->addDeclInternal(ToProperty); | |||
5521 | ||||
5522 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); | |||
5523 | ToProperty->setPropertyAttributesAsWritten( | |||
5524 | D->getPropertyAttributesAsWritten()); | |||
5525 | ToProperty->setGetterName(ToGetterName, ToGetterNameLoc); | |||
5526 | ToProperty->setSetterName(ToSetterName, ToSetterNameLoc); | |||
5527 | ToProperty->setGetterMethodDecl(ToGetterMethodDecl); | |||
5528 | ToProperty->setSetterMethodDecl(ToSetterMethodDecl); | |||
5529 | ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl); | |||
5530 | return ToProperty; | |||
5531 | } | |||
5532 | ||||
5533 | ExpectedDecl | |||
5534 | ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { | |||
5535 | ObjCPropertyDecl *Property; | |||
5536 | if (Error Err = importInto(Property, D->getPropertyDecl())) | |||
5537 | return std::move(Err); | |||
5538 | ||||
5539 | DeclContext *DC, *LexicalDC; | |||
5540 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | |||
5541 | return std::move(Err); | |||
5542 | ||||
5543 | auto *InImpl = cast<ObjCImplDecl>(LexicalDC); | |||
5544 | ||||
5545 | // Import the ivar (for an @synthesize). | |||
5546 | ObjCIvarDecl *Ivar = nullptr; | |||
5547 | if (Error Err = importInto(Ivar, D->getPropertyIvarDecl())) | |||
5548 | return std::move(Err); | |||
5549 | ||||
5550 | ObjCPropertyImplDecl *ToImpl | |||
5551 | = InImpl->FindPropertyImplDecl(Property->getIdentifier(), | |||
5552 | Property->getQueryKind()); | |||
5553 | if (!ToImpl) { | |||
5554 | ||||
5555 | Error Err = Error::success(); | |||
5556 | auto ToBeginLoc = importChecked(Err, D->getBeginLoc()); | |||
5557 | auto ToLocation = importChecked(Err, D->getLocation()); | |||
5558 | auto ToPropertyIvarDeclLoc = | |||
5559 | importChecked(Err, D->getPropertyIvarDeclLoc()); | |||
5560 | if (Err) | |||
5561 | return std::move(Err); | |||
5562 | ||||
5563 | if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC, | |||
5564 | ToBeginLoc, | |||
5565 | ToLocation, Property, | |||
5566 | D->getPropertyImplementation(), Ivar, | |||
5567 | ToPropertyIvarDeclLoc)) | |||
5568 | return ToImpl; | |||
5569 | ||||
5570 | ToImpl->setLexicalDeclContext(LexicalDC); | |||
5571 | LexicalDC->addDeclInternal(ToImpl); | |||
5572 | } else { | |||
5573 | // Check that we have the same kind of property implementation (@synthesize | |||
5574 | // vs. @dynamic). | |||
5575 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { | |||
5576 | Importer.ToDiag(ToImpl->getLocation(), | |||
5577 | diag::warn_odr_objc_property_impl_kind_inconsistent) | |||
5578 | << Property->getDeclName() | |||
5579 | << (ToImpl->getPropertyImplementation() | |||
5580 | == ObjCPropertyImplDecl::Dynamic); | |||
5581 | Importer.FromDiag(D->getLocation(), | |||
5582 | diag::note_odr_objc_property_impl_kind) | |||
5583 | << D->getPropertyDecl()->getDeclName() | |||
5584 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); | |||
5585 | ||||
5586 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
5587 | } | |||
5588 | ||||
5589 | // For @synthesize, check that we have the same | |||
5590 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && | |||
5591 | Ivar != ToImpl->getPropertyIvarDecl()) { | |||
5592 | Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), | |||
5593 | diag::warn_odr_objc_synthesize_ivar_inconsistent) | |||
5594 | << Property->getDeclName() | |||
5595 | << ToImpl->getPropertyIvarDecl()->getDeclName() | |||
5596 | << Ivar->getDeclName(); | |||
5597 | Importer.FromDiag(D->getPropertyIvarDeclLoc(), | |||
5598 | diag::note_odr_objc_synthesize_ivar_here) | |||
5599 | << D->getPropertyIvarDecl()->getDeclName(); | |||
5600 | ||||
5601 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
5602 | } | |||
5603 | ||||
5604 | // Merge the existing implementation with the new implementation. | |||
5605 | Importer.MapImported(D, ToImpl); | |||
5606 | } | |||
5607 | ||||
5608 | return ToImpl; | |||
5609 | } | |||
5610 | ||||
5611 | ExpectedDecl | |||
5612 | ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { | |||
5613 | // For template arguments, we adopt the translation unit as our declaration | |||
5614 | // context. This context will be fixed when the actual template declaration | |||
5615 | // is created. | |||
5616 | ||||
5617 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | |||
5618 | if (!BeginLocOrErr) | |||
5619 | return BeginLocOrErr.takeError(); | |||
5620 | ||||
5621 | ExpectedSLoc LocationOrErr = import(D->getLocation()); | |||
5622 | if (!LocationOrErr) | |||
5623 | return LocationOrErr.takeError(); | |||
5624 | ||||
5625 | TemplateTypeParmDecl *ToD = nullptr; | |||
5626 | if (GetImportedOrCreateDecl( | |||
5627 | ToD, D, Importer.getToContext(), | |||
5628 | Importer.getToContext().getTranslationUnitDecl(), | |||
5629 | *BeginLocOrErr, *LocationOrErr, | |||
5630 | D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()), | |||
5631 | D->wasDeclaredWithTypename(), D->isParameterPack(), | |||
5632 | D->hasTypeConstraint())) | |||
5633 | return ToD; | |||
5634 | ||||
5635 | // Import the type-constraint | |||
5636 | if (const TypeConstraint *TC = D->getTypeConstraint()) { | |||
5637 | ||||
5638 | Error Err = Error::success(); | |||
5639 | auto ToNNS = importChecked(Err, TC->getNestedNameSpecifierLoc()); | |||
5640 | auto ToName = importChecked(Err, TC->getConceptNameInfo().getName()); | |||
5641 | auto ToNameLoc = importChecked(Err, TC->getConceptNameInfo().getLoc()); | |||
5642 | auto ToFoundDecl = importChecked(Err, TC->getFoundDecl()); | |||
5643 | auto ToNamedConcept = importChecked(Err, TC->getNamedConcept()); | |||
5644 | auto ToIDC = importChecked(Err, TC->getImmediatelyDeclaredConstraint()); | |||
5645 | if (Err) | |||
5646 | return std::move(Err); | |||
5647 | ||||
5648 | TemplateArgumentListInfo ToTAInfo; | |||
5649 | const auto *ASTTemplateArgs = TC->getTemplateArgsAsWritten(); | |||
5650 | if (ASTTemplateArgs) | |||
5651 | if (Error Err = ImportTemplateArgumentListInfo(*ASTTemplateArgs, | |||
5652 | ToTAInfo)) | |||
5653 | return std::move(Err); | |||
5654 | ||||
5655 | ToD->setTypeConstraint(ToNNS, DeclarationNameInfo(ToName, ToNameLoc), | |||
5656 | ToFoundDecl, ToNamedConcept, | |||
5657 | ASTTemplateArgs ? | |||
5658 | ASTTemplateArgumentListInfo::Create(Importer.getToContext(), | |||
5659 | ToTAInfo) : nullptr, | |||
5660 | ToIDC); | |||
5661 | } | |||
5662 | ||||
5663 | if (D->hasDefaultArgument()) { | |||
5664 | Expected<TypeSourceInfo *> ToDefaultArgOrErr = | |||
5665 | import(D->getDefaultArgumentInfo()); | |||
5666 | if (!ToDefaultArgOrErr) | |||
5667 | return ToDefaultArgOrErr.takeError(); | |||
5668 | ToD->setDefaultArgument(*ToDefaultArgOrErr); | |||
5669 | } | |||
5670 | ||||
5671 | return ToD; | |||
5672 | } | |||
5673 | ||||
5674 | ExpectedDecl | |||
5675 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { | |||
5676 | ||||
5677 | Error Err = Error::success(); | |||
5678 | auto ToDeclName = importChecked(Err, D->getDeclName()); | |||
5679 | auto ToLocation = importChecked(Err, D->getLocation()); | |||
5680 | auto ToType = importChecked(Err, D->getType()); | |||
5681 | auto ToTypeSourceInfo = importChecked(Err, D->getTypeSourceInfo()); | |||
5682 | auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart()); | |||
5683 | if (Err) | |||
5684 | return std::move(Err); | |||
5685 | ||||
5686 | NonTypeTemplateParmDecl *ToD = nullptr; | |||
5687 | if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), | |||
5688 | Importer.getToContext().getTranslationUnitDecl(), | |||
5689 | ToInnerLocStart, ToLocation, D->getDepth(), | |||
5690 | D->getPosition(), | |||
5691 | ToDeclName.getAsIdentifierInfo(), ToType, | |||
5692 | D->isParameterPack(), ToTypeSourceInfo)) | |||
5693 | return ToD; | |||
5694 | ||||
5695 | if (D->hasDefaultArgument()) { | |||
5696 | ExpectedExpr ToDefaultArgOrErr = import(D->getDefaultArgument()); | |||
5697 | if (!ToDefaultArgOrErr) | |||
5698 | return ToDefaultArgOrErr.takeError(); | |||
5699 | ToD->setDefaultArgument(*ToDefaultArgOrErr); | |||
5700 | } | |||
5701 | ||||
5702 | return ToD; | |||
5703 | } | |||
5704 | ||||
5705 | ExpectedDecl | |||
5706 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { | |||
5707 | // Import the name of this declaration. | |||
5708 | auto NameOrErr = import(D->getDeclName()); | |||
5709 | if (!NameOrErr) | |||
5710 | return NameOrErr.takeError(); | |||
5711 | ||||
5712 | // Import the location of this declaration. | |||
5713 | ExpectedSLoc LocationOrErr = import(D->getLocation()); | |||
5714 | if (!LocationOrErr) | |||
5715 | return LocationOrErr.takeError(); | |||
5716 | ||||
5717 | // Import template parameters. | |||
5718 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); | |||
5719 | if (!TemplateParamsOrErr) | |||
5720 | return TemplateParamsOrErr.takeError(); | |||
5721 | ||||
5722 | TemplateTemplateParmDecl *ToD = nullptr; | |||
5723 | if (GetImportedOrCreateDecl( | |||
5724 | ToD, D, Importer.getToContext(), | |||
5725 | Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr, | |||
5726 | D->getDepth(), D->getPosition(), D->isParameterPack(), | |||
5727 | (*NameOrErr).getAsIdentifierInfo(), *TemplateParamsOrErr)) | |||
5728 | return ToD; | |||
5729 | ||||
5730 | if (D->hasDefaultArgument()) { | |||
5731 | Expected<TemplateArgumentLoc> ToDefaultArgOrErr = | |||
5732 | import(D->getDefaultArgument()); | |||
5733 | if (!ToDefaultArgOrErr) | |||
5734 | return ToDefaultArgOrErr.takeError(); | |||
5735 | ToD->setDefaultArgument(Importer.getToContext(), *ToDefaultArgOrErr); | |||
5736 | } | |||
5737 | ||||
5738 | return ToD; | |||
5739 | } | |||
5740 | ||||
5741 | // Returns the definition for a (forward) declaration of a TemplateDecl, if | |||
5742 | // it has any definition in the redecl chain. | |||
5743 | template <typename T> static auto getTemplateDefinition(T *D) -> T * { | |||
5744 | assert(D->getTemplatedDecl() && "Should be called on templates only")(static_cast <bool> (D->getTemplatedDecl() && "Should be called on templates only") ? void (0) : __assert_fail ("D->getTemplatedDecl() && \"Should be called on templates only\"" , "clang/lib/AST/ASTImporter.cpp", 5744, __extension__ __PRETTY_FUNCTION__ )); | |||
5745 | auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition(); | |||
5746 | if (!ToTemplatedDef) | |||
5747 | return nullptr; | |||
5748 | auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate(); | |||
5749 | return cast_or_null<T>(TemplateWithDef); | |||
5750 | } | |||
5751 | ||||
5752 | ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { | |||
5753 | ||||
5754 | // Import the major distinguishing characteristics of this class template. | |||
5755 | DeclContext *DC, *LexicalDC; | |||
5756 | DeclarationName Name; | |||
5757 | SourceLocation Loc; | |||
5758 | NamedDecl *ToD; | |||
5759 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
5760 | return std::move(Err); | |||
5761 | if (ToD) | |||
5762 | return ToD; | |||
5763 | ||||
5764 | ClassTemplateDecl *FoundByLookup = nullptr; | |||
5765 | ||||
5766 | // We may already have a template of the same name; try to find and match it. | |||
5767 | if (!DC->isFunctionOrMethod()) { | |||
5768 | SmallVector<NamedDecl *, 4> ConflictingDecls; | |||
5769 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
5770 | for (auto *FoundDecl : FoundDecls) { | |||
5771 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary | | |||
5772 | Decl::IDNS_TagFriend)) | |||
5773 | continue; | |||
5774 | ||||
5775 | Decl *Found = FoundDecl; | |||
5776 | auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found); | |||
5777 | if (FoundTemplate) { | |||
5778 | if (!hasSameVisibilityContextAndLinkage(FoundTemplate, D)) | |||
5779 | continue; | |||
5780 | ||||
5781 | if (IsStructuralMatch(D, FoundTemplate)) { | |||
5782 | ClassTemplateDecl *TemplateWithDef = | |||
5783 | getTemplateDefinition(FoundTemplate); | |||
5784 | if (D->isThisDeclarationADefinition() && TemplateWithDef) | |||
5785 | return Importer.MapImported(D, TemplateWithDef); | |||
5786 | if (!FoundByLookup) | |||
5787 | FoundByLookup = FoundTemplate; | |||
5788 | // Search in all matches because there may be multiple decl chains, | |||
5789 | // see ASTTests test ImportExistingFriendClassTemplateDef. | |||
5790 | continue; | |||
5791 | } | |||
5792 | ConflictingDecls.push_back(FoundDecl); | |||
5793 | } | |||
5794 | } | |||
5795 | ||||
5796 | if (!ConflictingDecls.empty()) { | |||
5797 | ExpectedName NameOrErr = Importer.HandleNameConflict( | |||
5798 | Name, DC, Decl::IDNS_Ordinary, ConflictingDecls.data(), | |||
5799 | ConflictingDecls.size()); | |||
5800 | if (NameOrErr) | |||
5801 | Name = NameOrErr.get(); | |||
5802 | else | |||
5803 | return NameOrErr.takeError(); | |||
5804 | } | |||
5805 | } | |||
5806 | ||||
5807 | CXXRecordDecl *FromTemplated = D->getTemplatedDecl(); | |||
5808 | ||||
5809 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); | |||
5810 | if (!TemplateParamsOrErr) | |||
5811 | return TemplateParamsOrErr.takeError(); | |||
5812 | ||||
5813 | // Create the declaration that is being templated. | |||
5814 | CXXRecordDecl *ToTemplated; | |||
5815 | if (Error Err = importInto(ToTemplated, FromTemplated)) | |||
5816 | return std::move(Err); | |||
5817 | ||||
5818 | // Create the class template declaration itself. | |||
5819 | ClassTemplateDecl *D2; | |||
5820 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name, | |||
5821 | *TemplateParamsOrErr, ToTemplated)) | |||
5822 | return D2; | |||
5823 | ||||
5824 | ToTemplated->setDescribedClassTemplate(D2); | |||
5825 | ||||
5826 | D2->setAccess(D->getAccess()); | |||
5827 | D2->setLexicalDeclContext(LexicalDC); | |||
5828 | ||||
5829 | addDeclToContexts(D, D2); | |||
5830 | updateLookupTableForTemplateParameters(**TemplateParamsOrErr); | |||
5831 | ||||
5832 | if (FoundByLookup) { | |||
5833 | auto *Recent = | |||
5834 | const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl()); | |||
5835 | ||||
5836 | // It is possible that during the import of the class template definition | |||
5837 | // we start the import of a fwd friend decl of the very same class template | |||
5838 | // and we add the fwd friend decl to the lookup table. But the ToTemplated | |||
5839 | // had been created earlier and by that time the lookup could not find | |||
5840 | // anything existing, so it has no previous decl. Later, (still during the | |||
5841 | // import of the fwd friend decl) we start to import the definition again | |||
5842 | // and this time the lookup finds the previous fwd friend class template. | |||
5843 | // In this case we must set up the previous decl for the templated decl. | |||
5844 | if (!ToTemplated->getPreviousDecl()) { | |||
5845 | assert(FoundByLookup->getTemplatedDecl() &&(static_cast <bool> (FoundByLookup->getTemplatedDecl () && "Found decl must have its templated decl set") ? void (0) : __assert_fail ("FoundByLookup->getTemplatedDecl() && \"Found decl must have its templated decl set\"" , "clang/lib/AST/ASTImporter.cpp", 5846, __extension__ __PRETTY_FUNCTION__ )) | |||
5846 | "Found decl must have its templated decl set")(static_cast <bool> (FoundByLookup->getTemplatedDecl () && "Found decl must have its templated decl set") ? void (0) : __assert_fail ("FoundByLookup->getTemplatedDecl() && \"Found decl must have its templated decl set\"" , "clang/lib/AST/ASTImporter.cpp", 5846, __extension__ __PRETTY_FUNCTION__ )); | |||
5847 | CXXRecordDecl *PrevTemplated = | |||
5848 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); | |||
5849 | if (ToTemplated != PrevTemplated) | |||
5850 | ToTemplated->setPreviousDecl(PrevTemplated); | |||
5851 | } | |||
5852 | ||||
5853 | D2->setPreviousDecl(Recent); | |||
5854 | } | |||
5855 | ||||
5856 | return D2; | |||
5857 | } | |||
5858 | ||||
5859 | ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl( | |||
5860 | ClassTemplateSpecializationDecl *D) { | |||
5861 | ClassTemplateDecl *ClassTemplate; | |||
5862 | if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate())) | |||
5863 | return std::move(Err); | |||
5864 | ||||
5865 | // Import the context of this declaration. | |||
5866 | DeclContext *DC, *LexicalDC; | |||
5867 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | |||
5868 | return std::move(Err); | |||
5869 | ||||
5870 | // Import template arguments. | |||
5871 | SmallVector<TemplateArgument, 2> TemplateArgs; | |||
5872 | if (Error Err = | |||
5873 | ImportTemplateArguments(D->getTemplateArgs().asArray(), TemplateArgs)) | |||
5874 | return std::move(Err); | |||
5875 | // Try to find an existing specialization with these template arguments and | |||
5876 | // template parameter list. | |||
5877 | void *InsertPos = nullptr; | |||
5878 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; | |||
5879 | ClassTemplatePartialSpecializationDecl *PartialSpec = | |||
5880 | dyn_cast<ClassTemplatePartialSpecializationDecl>(D); | |||
5881 | ||||
5882 | // Import template parameters. | |||
5883 | TemplateParameterList *ToTPList = nullptr; | |||
5884 | ||||
5885 | if (PartialSpec) { | |||
5886 | auto ToTPListOrErr = import(PartialSpec->getTemplateParameters()); | |||
5887 | if (!ToTPListOrErr) | |||
5888 | return ToTPListOrErr.takeError(); | |||
5889 | ToTPList = *ToTPListOrErr; | |||
5890 | PrevDecl = ClassTemplate->findPartialSpecialization(TemplateArgs, | |||
5891 | *ToTPListOrErr, | |||
5892 | InsertPos); | |||
5893 | } else | |||
5894 | PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); | |||
5895 | ||||
5896 | if (PrevDecl) { | |||
5897 | if (IsStructuralMatch(D, PrevDecl)) { | |||
5898 | CXXRecordDecl *PrevDefinition = PrevDecl->getDefinition(); | |||
5899 | if (D->isThisDeclarationADefinition() && PrevDefinition) { | |||
5900 | Importer.MapImported(D, PrevDefinition); | |||
5901 | // Import those default field initializers which have been | |||
5902 | // instantiated in the "From" context, but not in the "To" context. | |||
5903 | for (auto *FromField : D->fields()) { | |||
5904 | auto ToOrErr = import(FromField); | |||
5905 | if (!ToOrErr) | |||
5906 | return ToOrErr.takeError(); | |||
5907 | } | |||
5908 | ||||
5909 | // Import those methods which have been instantiated in the | |||
5910 | // "From" context, but not in the "To" context. | |||
5911 | for (CXXMethodDecl *FromM : D->methods()) { | |||
5912 | auto ToOrErr = import(FromM); | |||
5913 | if (!ToOrErr) | |||
5914 | return ToOrErr.takeError(); | |||
5915 | } | |||
5916 | ||||
5917 | // TODO Import instantiated default arguments. | |||
5918 | // TODO Import instantiated exception specifications. | |||
5919 | // | |||
5920 | // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint | |||
5921 | // what else could be fused during an AST merge. | |||
5922 | return PrevDefinition; | |||
5923 | } | |||
5924 | } else { // ODR violation. | |||
5925 | // FIXME HandleNameConflict | |||
5926 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
5927 | } | |||
5928 | } | |||
5929 | ||||
5930 | // Import the location of this declaration. | |||
5931 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | |||
5932 | if (!BeginLocOrErr) | |||
5933 | return BeginLocOrErr.takeError(); | |||
5934 | ExpectedSLoc IdLocOrErr = import(D->getLocation()); | |||
5935 | if (!IdLocOrErr) | |||
5936 | return IdLocOrErr.takeError(); | |||
5937 | ||||
5938 | // Create the specialization. | |||
5939 | ClassTemplateSpecializationDecl *D2 = nullptr; | |||
5940 | if (PartialSpec) { | |||
5941 | // Import TemplateArgumentListInfo. | |||
5942 | TemplateArgumentListInfo ToTAInfo; | |||
5943 | const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten(); | |||
5944 | if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo)) | |||
5945 | return std::move(Err); | |||
5946 | ||||
5947 | QualType CanonInjType; | |||
5948 | if (Error Err = importInto( | |||
5949 | CanonInjType, PartialSpec->getInjectedSpecializationType())) | |||
5950 | return std::move(Err); | |||
5951 | CanonInjType = CanonInjType.getCanonicalType(); | |||
5952 | ||||
5953 | if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>( | |||
5954 | D2, D, Importer.getToContext(), D->getTagKind(), DC, *BeginLocOrErr, | |||
5955 | *IdLocOrErr, ToTPList, ClassTemplate, | |||
5956 | llvm::ArrayRef(TemplateArgs.data(), TemplateArgs.size()), ToTAInfo, | |||
5957 | CanonInjType, | |||
5958 | cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl))) | |||
5959 | return D2; | |||
5960 | ||||
5961 | // Update InsertPos, because preceding import calls may have invalidated | |||
5962 | // it by adding new specializations. | |||
5963 | auto *PartSpec2 = cast<ClassTemplatePartialSpecializationDecl>(D2); | |||
5964 | if (!ClassTemplate->findPartialSpecialization(TemplateArgs, ToTPList, | |||
5965 | InsertPos)) | |||
5966 | // Add this partial specialization to the class template. | |||
5967 | ClassTemplate->AddPartialSpecialization(PartSpec2, InsertPos); | |||
5968 | ||||
5969 | updateLookupTableForTemplateParameters(*ToTPList); | |||
5970 | } else { // Not a partial specialization. | |||
5971 | if (GetImportedOrCreateDecl( | |||
5972 | D2, D, Importer.getToContext(), D->getTagKind(), DC, | |||
5973 | *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs, | |||
5974 | PrevDecl)) | |||
5975 | return D2; | |||
5976 | ||||
5977 | // Update InsertPos, because preceding import calls may have invalidated | |||
5978 | // it by adding new specializations. | |||
5979 | if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos)) | |||
5980 | // Add this specialization to the class template. | |||
5981 | ClassTemplate->AddSpecialization(D2, InsertPos); | |||
5982 | } | |||
5983 | ||||
5984 | D2->setSpecializationKind(D->getSpecializationKind()); | |||
5985 | ||||
5986 | // Set the context of this specialization/instantiation. | |||
5987 | D2->setLexicalDeclContext(LexicalDC); | |||
5988 | ||||
5989 | // Add to the DC only if it was an explicit specialization/instantiation. | |||
5990 | if (D2->isExplicitInstantiationOrSpecialization()) { | |||
5991 | LexicalDC->addDeclInternal(D2); | |||
5992 | } | |||
5993 | ||||
5994 | if (auto BraceRangeOrErr = import(D->getBraceRange())) | |||
5995 | D2->setBraceRange(*BraceRangeOrErr); | |||
5996 | else | |||
5997 | return BraceRangeOrErr.takeError(); | |||
5998 | ||||
5999 | // Import the qualifier, if any. | |||
6000 | if (auto LocOrErr = import(D->getQualifierLoc())) | |||
6001 | D2->setQualifierInfo(*LocOrErr); | |||
6002 | else | |||
6003 | return LocOrErr.takeError(); | |||
6004 | ||||
6005 | if (auto *TSI = D->getTypeAsWritten()) { | |||
6006 | if (auto TInfoOrErr = import(TSI)) | |||
6007 | D2->setTypeAsWritten(*TInfoOrErr); | |||
6008 | else | |||
6009 | return TInfoOrErr.takeError(); | |||
6010 | ||||
6011 | if (auto LocOrErr = import(D->getTemplateKeywordLoc())) | |||
6012 | D2->setTemplateKeywordLoc(*LocOrErr); | |||
6013 | else | |||
6014 | return LocOrErr.takeError(); | |||
6015 | ||||
6016 | if (auto LocOrErr = import(D->getExternLoc())) | |||
6017 | D2->setExternLoc(*LocOrErr); | |||
6018 | else | |||
6019 | return LocOrErr.takeError(); | |||
6020 | } | |||
6021 | ||||
6022 | if (D->getPointOfInstantiation().isValid()) { | |||
6023 | if (auto POIOrErr = import(D->getPointOfInstantiation())) | |||
6024 | D2->setPointOfInstantiation(*POIOrErr); | |||
6025 | else | |||
6026 | return POIOrErr.takeError(); | |||
6027 | } | |||
6028 | ||||
6029 | D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind()); | |||
6030 | ||||
6031 | if (auto P = D->getInstantiatedFrom()) { | |||
6032 | if (auto *CTD = P.dyn_cast<ClassTemplateDecl *>()) { | |||
6033 | if (auto CTDorErr = import(CTD)) | |||
6034 | D2->setInstantiationOf(*CTDorErr); | |||
6035 | } else { | |||
6036 | auto *CTPSD = cast<ClassTemplatePartialSpecializationDecl *>(P); | |||
6037 | auto CTPSDOrErr = import(CTPSD); | |||
6038 | if (!CTPSDOrErr) | |||
6039 | return CTPSDOrErr.takeError(); | |||
6040 | const TemplateArgumentList &DArgs = D->getTemplateInstantiationArgs(); | |||
6041 | SmallVector<TemplateArgument, 2> D2ArgsVec(DArgs.size()); | |||
6042 | for (unsigned I = 0; I < DArgs.size(); ++I) { | |||
6043 | const TemplateArgument &DArg = DArgs[I]; | |||
6044 | if (auto ArgOrErr = import(DArg)) | |||
6045 | D2ArgsVec[I] = *ArgOrErr; | |||
6046 | else | |||
6047 | return ArgOrErr.takeError(); | |||
6048 | } | |||
6049 | D2->setInstantiationOf( | |||
6050 | *CTPSDOrErr, | |||
6051 | TemplateArgumentList::CreateCopy(Importer.getToContext(), D2ArgsVec)); | |||
6052 | } | |||
6053 | } | |||
6054 | ||||
6055 | if (D->isCompleteDefinition()) | |||
6056 | if (Error Err = ImportDefinition(D, D2)) | |||
6057 | return std::move(Err); | |||
6058 | ||||
6059 | return D2; | |||
6060 | } | |||
6061 | ||||
6062 | ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { | |||
6063 | // Import the major distinguishing characteristics of this variable template. | |||
6064 | DeclContext *DC, *LexicalDC; | |||
6065 | DeclarationName Name; | |||
6066 | SourceLocation Loc; | |||
6067 | NamedDecl *ToD; | |||
6068 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
6069 | return std::move(Err); | |||
6070 | if (ToD) | |||
6071 | return ToD; | |||
6072 | ||||
6073 | // We may already have a template of the same name; try to find and match it. | |||
6074 | assert(!DC->isFunctionOrMethod() &&(static_cast <bool> (!DC->isFunctionOrMethod() && "Variable templates cannot be declared at function scope") ? void (0) : __assert_fail ("!DC->isFunctionOrMethod() && \"Variable templates cannot be declared at function scope\"" , "clang/lib/AST/ASTImporter.cpp", 6075, __extension__ __PRETTY_FUNCTION__ )) | |||
6075 | "Variable templates cannot be declared at function scope")(static_cast <bool> (!DC->isFunctionOrMethod() && "Variable templates cannot be declared at function scope") ? void (0) : __assert_fail ("!DC->isFunctionOrMethod() && \"Variable templates cannot be declared at function scope\"" , "clang/lib/AST/ASTImporter.cpp", 6075, __extension__ __PRETTY_FUNCTION__ )); | |||
6076 | ||||
6077 | SmallVector<NamedDecl *, 4> ConflictingDecls; | |||
6078 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
6079 | VarTemplateDecl *FoundByLookup = nullptr; | |||
6080 | for (auto *FoundDecl : FoundDecls) { | |||
6081 | if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) | |||
6082 | continue; | |||
6083 | ||||
6084 | if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(FoundDecl)) { | |||
6085 | // Use the templated decl, some linkage flags are set only there. | |||
6086 | if (!hasSameVisibilityContextAndLinkage(FoundTemplate->getTemplatedDecl(), | |||
6087 | D->getTemplatedDecl())) | |||
6088 | continue; | |||
6089 | if (IsStructuralMatch(D, FoundTemplate)) { | |||
6090 | // The Decl in the "From" context has a definition, but in the | |||
6091 | // "To" context we already have a definition. | |||
6092 | VarTemplateDecl *FoundDef = getTemplateDefinition(FoundTemplate); | |||
6093 | if (D->isThisDeclarationADefinition() && FoundDef) | |||
6094 | // FIXME Check for ODR error if the two definitions have | |||
6095 | // different initializers? | |||
6096 | return Importer.MapImported(D, FoundDef); | |||
6097 | ||||
6098 | FoundByLookup = FoundTemplate; | |||
6099 | break; | |||
6100 | } | |||
6101 | ConflictingDecls.push_back(FoundDecl); | |||
6102 | } | |||
6103 | } | |||
6104 | ||||
6105 | if (!ConflictingDecls.empty()) { | |||
6106 | ExpectedName NameOrErr = Importer.HandleNameConflict( | |||
6107 | Name, DC, Decl::IDNS_Ordinary, ConflictingDecls.data(), | |||
6108 | ConflictingDecls.size()); | |||
6109 | if (NameOrErr) | |||
6110 | Name = NameOrErr.get(); | |||
6111 | else | |||
6112 | return NameOrErr.takeError(); | |||
6113 | } | |||
6114 | ||||
6115 | VarDecl *DTemplated = D->getTemplatedDecl(); | |||
6116 | ||||
6117 | // Import the type. | |||
6118 | // FIXME: Value not used? | |||
6119 | ExpectedType TypeOrErr = import(DTemplated->getType()); | |||
6120 | if (!TypeOrErr) | |||
6121 | return TypeOrErr.takeError(); | |||
6122 | ||||
6123 | // Create the declaration that is being templated. | |||
6124 | VarDecl *ToTemplated; | |||
6125 | if (Error Err = importInto(ToTemplated, DTemplated)) | |||
6126 | return std::move(Err); | |||
6127 | ||||
6128 | // Create the variable template declaration itself. | |||
6129 | auto TemplateParamsOrErr = import(D->getTemplateParameters()); | |||
6130 | if (!TemplateParamsOrErr) | |||
6131 | return TemplateParamsOrErr.takeError(); | |||
6132 | ||||
6133 | VarTemplateDecl *ToVarTD; | |||
6134 | if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc, | |||
6135 | Name, *TemplateParamsOrErr, ToTemplated)) | |||
6136 | return ToVarTD; | |||
6137 | ||||
6138 | ToTemplated->setDescribedVarTemplate(ToVarTD); | |||
6139 | ||||
6140 | ToVarTD->setAccess(D->getAccess()); | |||
6141 | ToVarTD->setLexicalDeclContext(LexicalDC); | |||
6142 | LexicalDC->addDeclInternal(ToVarTD); | |||
6143 | if (DC != Importer.getToContext().getTranslationUnitDecl()) | |||
6144 | updateLookupTableForTemplateParameters(**TemplateParamsOrErr); | |||
6145 | ||||
6146 | if (FoundByLookup) { | |||
6147 | auto *Recent = | |||
6148 | const_cast<VarTemplateDecl *>(FoundByLookup->getMostRecentDecl()); | |||
6149 | if (!ToTemplated->getPreviousDecl()) { | |||
6150 | auto *PrevTemplated = | |||
6151 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); | |||
6152 | if (ToTemplated != PrevTemplated) | |||
6153 | ToTemplated->setPreviousDecl(PrevTemplated); | |||
6154 | } | |||
6155 | ToVarTD->setPreviousDecl(Recent); | |||
6156 | } | |||
6157 | ||||
6158 | return ToVarTD; | |||
6159 | } | |||
6160 | ||||
6161 | ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl( | |||
6162 | VarTemplateSpecializationDecl *D) { | |||
6163 | // If this record has a definition in the translation unit we're coming from, | |||
6164 | // but this particular declaration is not that definition, import the | |||
6165 | // definition and map to that. | |||
6166 | VarDecl *Definition = D->getDefinition(); | |||
6167 | if (Definition && Definition != D) { | |||
6168 | if (ExpectedDecl ImportedDefOrErr = import(Definition)) | |||
6169 | return Importer.MapImported(D, *ImportedDefOrErr); | |||
6170 | else | |||
6171 | return ImportedDefOrErr.takeError(); | |||
6172 | } | |||
6173 | ||||
6174 | VarTemplateDecl *VarTemplate = nullptr; | |||
6175 | if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate())) | |||
6176 | return std::move(Err); | |||
6177 | ||||
6178 | // Import the context of this declaration. | |||
6179 | DeclContext *DC, *LexicalDC; | |||
6180 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | |||
6181 | return std::move(Err); | |||
6182 | ||||
6183 | // Import the location of this declaration. | |||
6184 | ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc()); | |||
6185 | if (!BeginLocOrErr) | |||
6186 | return BeginLocOrErr.takeError(); | |||
6187 | ||||
6188 | auto IdLocOrErr = import(D->getLocation()); | |||
6189 | if (!IdLocOrErr) | |||
6190 | return IdLocOrErr.takeError(); | |||
6191 | ||||
6192 | // Import template arguments. | |||
6193 | SmallVector<TemplateArgument, 2> TemplateArgs; | |||
6194 | if (Error Err = | |||
6195 | ImportTemplateArguments(D->getTemplateArgs().asArray(), TemplateArgs)) | |||
6196 | return std::move(Err); | |||
6197 | ||||
6198 | // Try to find an existing specialization with these template arguments. | |||
6199 | void *InsertPos = nullptr; | |||
6200 | VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( | |||
6201 | TemplateArgs, InsertPos); | |||
6202 | if (D2) { | |||
6203 | // We already have a variable template specialization with these template | |||
6204 | // arguments. | |||
6205 | ||||
6206 | // FIXME: Check for specialization vs. instantiation errors. | |||
6207 | ||||
6208 | if (VarDecl *FoundDef = D2->getDefinition()) { | |||
6209 | if (!D->isThisDeclarationADefinition() || | |||
6210 | IsStructuralMatch(D, FoundDef)) { | |||
6211 | // The record types structurally match, or the "from" translation | |||
6212 | // unit only had a forward declaration anyway; call it the same | |||
6213 | // variable. | |||
6214 | return Importer.MapImported(D, FoundDef); | |||
6215 | } | |||
6216 | } | |||
6217 | } else { | |||
6218 | TemplateArgumentListInfo ToTAInfo; | |||
6219 | if (const ASTTemplateArgumentListInfo *Args = D->getTemplateArgsInfo()) { | |||
6220 | if (Error Err = ImportTemplateArgumentListInfo(*Args, ToTAInfo)) | |||
6221 | return std::move(Err); | |||
6222 | } | |||
6223 | ||||
6224 | using PartVarSpecDecl = VarTemplatePartialSpecializationDecl; | |||
6225 | // Create a new specialization. | |||
6226 | if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) { | |||
6227 | // Import TemplateArgumentListInfo | |||
6228 | TemplateArgumentListInfo ArgInfos; | |||
6229 | const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten(); | |||
6230 | // NOTE: FromTAArgsAsWritten and template parameter list are non-null. | |||
6231 | if (Error Err = ImportTemplateArgumentListInfo( | |||
6232 | *FromTAArgsAsWritten, ArgInfos)) | |||
6233 | return std::move(Err); | |||
6234 | ||||
6235 | auto ToTPListOrErr = import(FromPartial->getTemplateParameters()); | |||
6236 | if (!ToTPListOrErr) | |||
6237 | return ToTPListOrErr.takeError(); | |||
6238 | ||||
6239 | PartVarSpecDecl *ToPartial; | |||
6240 | if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC, | |||
6241 | *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, | |||
6242 | VarTemplate, QualType(), nullptr, | |||
6243 | D->getStorageClass(), TemplateArgs, ArgInfos)) | |||
6244 | return ToPartial; | |||
6245 | ||||
6246 | if (Expected<PartVarSpecDecl *> ToInstOrErr = import( | |||
6247 | FromPartial->getInstantiatedFromMember())) | |||
6248 | ToPartial->setInstantiatedFromMember(*ToInstOrErr); | |||
6249 | else | |||
6250 | return ToInstOrErr.takeError(); | |||
6251 | ||||
6252 | if (FromPartial->isMemberSpecialization()) | |||
6253 | ToPartial->setMemberSpecialization(); | |||
6254 | ||||
6255 | D2 = ToPartial; | |||
6256 | ||||
6257 | // FIXME: Use this update if VarTemplatePartialSpecializationDecl is fixed | |||
6258 | // to adopt template parameters. | |||
6259 | // updateLookupTableForTemplateParameters(**ToTPListOrErr); | |||
6260 | } else { // Full specialization | |||
6261 | if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, | |||
6262 | *BeginLocOrErr, *IdLocOrErr, VarTemplate, | |||
6263 | QualType(), nullptr, D->getStorageClass(), | |||
6264 | TemplateArgs)) | |||
6265 | return D2; | |||
6266 | } | |||
6267 | ||||
6268 | QualType T; | |||
6269 | if (Error Err = importInto(T, D->getType())) | |||
6270 | return std::move(Err); | |||
6271 | D2->setType(T); | |||
6272 | ||||
6273 | auto TInfoOrErr = import(D->getTypeSourceInfo()); | |||
6274 | if (!TInfoOrErr) | |||
6275 | return TInfoOrErr.takeError(); | |||
6276 | D2->setTypeSourceInfo(*TInfoOrErr); | |||
6277 | ||||
6278 | if (D->getPointOfInstantiation().isValid()) { | |||
6279 | if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation())) | |||
6280 | D2->setPointOfInstantiation(*POIOrErr); | |||
6281 | else | |||
6282 | return POIOrErr.takeError(); | |||
6283 | } | |||
6284 | ||||
6285 | D2->setSpecializationKind(D->getSpecializationKind()); | |||
6286 | D2->setTemplateArgsInfo(ToTAInfo); | |||
6287 | ||||
6288 | // Add this specialization to the class template. | |||
6289 | VarTemplate->AddSpecialization(D2, InsertPos); | |||
6290 | ||||
6291 | // Import the qualifier, if any. | |||
6292 | if (auto LocOrErr = import(D->getQualifierLoc())) | |||
6293 | D2->setQualifierInfo(*LocOrErr); | |||
6294 | else | |||
6295 | return LocOrErr.takeError(); | |||
6296 | ||||
6297 | if (D->isConstexpr()) | |||
6298 | D2->setConstexpr(true); | |||
6299 | ||||
6300 | // Add the specialization to this context. | |||
6301 | D2->setLexicalDeclContext(LexicalDC); | |||
6302 | LexicalDC->addDeclInternal(D2); | |||
6303 | ||||
6304 | D2->setAccess(D->getAccess()); | |||
6305 | } | |||
6306 | ||||
6307 | if (Error Err = ImportInitializer(D, D2)) | |||
6308 | return std::move(Err); | |||
6309 | ||||
6310 | return D2; | |||
6311 | } | |||
6312 | ||||
6313 | ExpectedDecl | |||
6314 | ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { | |||
6315 | DeclContext *DC, *LexicalDC; | |||
6316 | DeclarationName Name; | |||
6317 | SourceLocation Loc; | |||
6318 | NamedDecl *ToD; | |||
6319 | ||||
6320 | if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) | |||
6321 | return std::move(Err); | |||
6322 | ||||
6323 | if (ToD) | |||
6324 | return ToD; | |||
6325 | ||||
6326 | const FunctionTemplateDecl *FoundByLookup = nullptr; | |||
6327 | ||||
6328 | // Try to find a function in our own ("to") context with the same name, same | |||
6329 | // type, and in the same context as the function we're importing. | |||
6330 | // FIXME Split this into a separate function. | |||
6331 | if (!LexicalDC->isFunctionOrMethod()) { | |||
6332 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend; | |||
6333 | auto FoundDecls = Importer.findDeclsInToCtx(DC, Name); | |||
6334 | for (auto *FoundDecl : FoundDecls) { | |||
6335 | if (!FoundDecl->isInIdentifierNamespace(IDNS)) | |||
6336 | continue; | |||
6337 | ||||
6338 | if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) { | |||
6339 | if (!hasSameVisibilityContextAndLinkage(FoundTemplate, D)) | |||
6340 | continue; | |||
6341 | if (IsStructuralMatch(D, FoundTemplate)) { | |||
6342 | FunctionTemplateDecl *TemplateWithDef = | |||
6343 | getTemplateDefinition(FoundTemplate); | |||
6344 | if (D->isThisDeclarationADefinition() && TemplateWithDef) | |||
6345 | return Importer.MapImported(D, TemplateWithDef); | |||
6346 | ||||
6347 | FoundByLookup = FoundTemplate; | |||
6348 | break; | |||
6349 | // TODO: handle conflicting names | |||
6350 | } | |||
6351 | } | |||
6352 | } | |||
6353 | } | |||
6354 | ||||
6355 | auto ParamsOrErr = import(D->getTemplateParameters()); | |||
6356 | if (!ParamsOrErr) | |||
6357 | return ParamsOrErr.takeError(); | |||
6358 | TemplateParameterList *Params = *ParamsOrErr; | |||
6359 | ||||
6360 | FunctionDecl *TemplatedFD; | |||
6361 | if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl())) | |||
6362 | return std::move(Err); | |||
6363 | ||||
6364 | // At creation of the template the template parameters are "adopted" | |||
6365 | // (DeclContext is changed). After this possible change the lookup table | |||
6366 | // must be updated. | |||
6367 | // At deduction guides the DeclContext of the template parameters may be | |||
6368 | // different from what we would expect, it may be the class template, or a | |||
6369 | // probably different CXXDeductionGuideDecl. This may come from the fact that | |||
6370 | // the template parameter objects may be shared between deduction guides or | |||
6371 | // the class template, and at creation of multiple FunctionTemplateDecl | |||
6372 | // objects (for deduction guides) the same parameters are re-used. The | |||
6373 | // "adoption" happens multiple times with different parent, even recursively | |||
6374 | // for TemplateTemplateParmDecl. The same happens at import when the | |||
6375 | // FunctionTemplateDecl objects are created, but in different order. | |||
6376 | // In this way the DeclContext of these template parameters is not necessarily | |||
6377 | // the same as in the "from" context. | |||
6378 | SmallVector<DeclContext *, 2> OldParamDC; | |||
6379 | OldParamDC.reserve(Params->size()); | |||
6380 | llvm::transform(*Params, std::back_inserter(OldParamDC), | |||
6381 | [](NamedDecl *ND) { return ND->getDeclContext(); }); | |||
6382 | ||||
6383 | FunctionTemplateDecl *ToFunc; | |||
6384 | if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name, | |||
6385 | Params, TemplatedFD)) | |||
6386 | return ToFunc; | |||
6387 | ||||
6388 | TemplatedFD->setDescribedFunctionTemplate(ToFunc); | |||
6389 | ||||
6390 | ToFunc->setAccess(D->getAccess()); | |||
6391 | ToFunc->setLexicalDeclContext(LexicalDC); | |||
6392 | LexicalDC->addDeclInternal(ToFunc); | |||
6393 | ||||
6394 | ASTImporterLookupTable *LT = Importer.SharedState->getLookupTable(); | |||
6395 | if (LT && !OldParamDC.empty()) { | |||
6396 | for (unsigned int I = 0; I < OldParamDC.size(); ++I) | |||
6397 | LT->updateForced(Params->getParam(I), OldParamDC[I]); | |||
6398 | } | |||
6399 | ||||
6400 | if (FoundByLookup) { | |||
6401 | auto *Recent = | |||
6402 | const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl()); | |||
6403 | if (!TemplatedFD->getPreviousDecl()) { | |||
6404 | assert(FoundByLookup->getTemplatedDecl() &&(static_cast <bool> (FoundByLookup->getTemplatedDecl () && "Found decl must have its templated decl set") ? void (0) : __assert_fail ("FoundByLookup->getTemplatedDecl() && \"Found decl must have its templated decl set\"" , "clang/lib/AST/ASTImporter.cpp", 6405, __extension__ __PRETTY_FUNCTION__ )) | |||
6405 | "Found decl must have its templated decl set")(static_cast <bool> (FoundByLookup->getTemplatedDecl () && "Found decl must have its templated decl set") ? void (0) : __assert_fail ("FoundByLookup->getTemplatedDecl() && \"Found decl must have its templated decl set\"" , "clang/lib/AST/ASTImporter.cpp", 6405, __extension__ __PRETTY_FUNCTION__ )); | |||
6406 | auto *PrevTemplated = | |||
6407 | FoundByLookup->getTemplatedDecl()->getMostRecentDecl(); | |||
6408 | if (TemplatedFD != PrevTemplated) | |||
6409 | TemplatedFD->setPreviousDecl(PrevTemplated); | |||
6410 | } | |||
6411 | ToFunc->setPreviousDecl(Recent); | |||
6412 | } | |||
6413 | ||||
6414 | return ToFunc; | |||
6415 | } | |||
6416 | ||||
6417 | //---------------------------------------------------------------------------- | |||
6418 | // Import Statements | |||
6419 | //---------------------------------------------------------------------------- | |||
6420 | ||||
6421 | ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) { | |||
6422 | Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node) | |||
6423 | << S->getStmtClassName(); | |||
6424 | return make_error<ASTImportError>(ASTImportError::UnsupportedConstruct); | |||
6425 | } | |||
6426 | ||||
6427 | ||||
6428 | ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) { | |||
6429 | if (Importer.returnWithErrorInTest()) | |||
6430 | return make_error<ASTImportError>(ASTImportError::UnsupportedConstruct); | |||
6431 | SmallVector<IdentifierInfo *, 4> Names; | |||
6432 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { | |||
6433 | IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I)); | |||
6434 | // ToII is nullptr when no symbolic name is given for output operand | |||
6435 | // see ParseStmtAsm::ParseAsmOperandsOpt | |||
6436 | Names.push_back(ToII); | |||
6437 | } | |||
6438 | ||||
6439 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { | |||
6440 | IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I)); | |||
6441 | // ToII is nullptr when no symbolic name is given for input operand | |||
6442 | // see ParseStmtAsm::ParseAsmOperandsOpt | |||
6443 | Names.push_back(ToII); | |||
6444 | } | |||
6445 | ||||
6446 | SmallVector<StringLiteral *, 4> Clobbers; | |||
6447 | for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) { | |||
6448 | if (auto ClobberOrErr = import(S->getClobberStringLiteral(I))) | |||
6449 | Clobbers.push_back(*ClobberOrErr); | |||
6450 | else | |||
6451 | return ClobberOrErr.takeError(); | |||
6452 | ||||
6453 | } | |||
6454 | ||||
6455 | SmallVector<StringLiteral *, 4> Constraints; | |||
6456 | for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) { | |||
6457 | if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I))) | |||
6458 | Constraints.push_back(*OutputOrErr); | |||
6459 | else | |||
6460 | return OutputOrErr.takeError(); | |||
6461 | } | |||
6462 | ||||
6463 | for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) { | |||
6464 | if (auto InputOrErr = import(S->getInputConstraintLiteral(I))) | |||
6465 | Constraints.push_back(*InputOrErr); | |||
6466 | else | |||
6467 | return InputOrErr.takeError(); | |||
6468 | } | |||
6469 | ||||
6470 | SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() + | |||
6471 | S->getNumLabels()); | |||
6472 | if (Error Err = ImportContainerChecked(S->outputs(), Exprs)) | |||
6473 | return std::move(Err); | |||
6474 | ||||
6475 | if (Error Err = | |||
6476 | ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs())) | |||
6477 | return std::move(Err); | |||
6478 | ||||
6479 | if (Error Err = ImportArrayChecked( | |||
6480 | S->labels(), Exprs.begin() + S->getNumOutputs() + S->getNumInputs())) | |||
6481 | return std::move(Err); | |||
6482 | ||||
6483 | ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc()); | |||
6484 | if (!AsmLocOrErr) | |||
6485 | return AsmLocOrErr.takeError(); | |||
6486 | auto AsmStrOrErr = import(S->getAsmString()); | |||
6487 | if (!AsmStrOrErr) | |||
6488 | return AsmStrOrErr.takeError(); | |||
6489 | ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc()); | |||
6490 | if (!RParenLocOrErr) | |||
6491 | return RParenLocOrErr.takeError(); | |||
6492 | ||||
6493 | return new (Importer.getToContext()) GCCAsmStmt( | |||
6494 | Importer.getToContext(), | |||
6495 | *AsmLocOrErr, | |||
6496 | S->isSimple(), | |||
6497 | S->isVolatile(), | |||
6498 | S->getNumOutputs(), | |||
6499 | S->getNumInputs(), | |||
6500 | Names.data(), | |||
6501 | Constraints.data(), | |||
6502 | Exprs.data(), | |||
6503 | *AsmStrOrErr, | |||
6504 | S->getNumClobbers(), | |||
6505 | Clobbers.data(), | |||
6506 | S->getNumLabels(), | |||
6507 | *RParenLocOrErr); | |||
6508 | } | |||
6509 | ||||
6510 | ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) { | |||
6511 | ||||
6512 | Error Err = Error::success(); | |||
6513 | auto ToDG = importChecked(Err, S->getDeclGroup()); | |||
6514 | auto ToBeginLoc = importChecked(Err, S->getBeginLoc()); | |||
6515 | auto ToEndLoc = importChecked(Err, S->getEndLoc()); | |||
6516 | if (Err) | |||
6517 | return std::move(Err); | |||
6518 | return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc); | |||
6519 | } | |||
6520 | ||||
6521 | ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) { | |||
6522 | ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc()); | |||
6523 | if (!ToSemiLocOrErr) | |||
6524 | return ToSemiLocOrErr.takeError(); | |||
6525 | return new (Importer.getToContext()) NullStmt( | |||
6526 | *ToSemiLocOrErr, S->hasLeadingEmptyMacro()); | |||
6527 | } | |||
6528 | ||||
6529 | ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) { | |||
6530 | SmallVector<Stmt *, 8> ToStmts(S->size()); | |||
6531 | ||||
6532 | if (Error Err = ImportContainerChecked(S->body(), ToStmts)) | |||
6533 | return std::move(Err); | |||
6534 | ||||
6535 | ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc()); | |||
6536 | if (!ToLBracLocOrErr) | |||
6537 | return ToLBracLocOrErr.takeError(); | |||
6538 | ||||
6539 | ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc()); | |||
6540 | if (!ToRBracLocOrErr) | |||
6541 | return ToRBracLocOrErr.takeError(); | |||
6542 | ||||
6543 | FPOptionsOverride FPO = | |||
6544 | S->hasStoredFPFeatures() ? S->getStoredFPFeatures() : FPOptionsOverride(); | |||
6545 | return CompoundStmt::Create(Importer.getToContext(), ToStmts, FPO, | |||
6546 | *ToLBracLocOrErr, *ToRBracLocOrErr); | |||
6547 | } | |||
6548 | ||||
6549 | ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) { | |||
6550 | ||||
6551 | Error Err = Error::success(); | |||
6552 | auto ToLHS = importChecked(Err, S->getLHS()); | |||
6553 | auto ToRHS = importChecked(Err, S->getRHS()); | |||
6554 | auto ToSubStmt = importChecked(Err, S->getSubStmt()); | |||
6555 | auto ToCaseLoc = importChecked(Err, S->getCaseLoc()); | |||
6556 | auto ToEllipsisLoc = importChecked(Err, S->getEllipsisLoc()); | |||
6557 | auto ToColonLoc = importChecked(Err, S->getColonLoc()); | |||
6558 | if (Err) | |||
6559 | return std::move(Err); | |||
6560 | ||||
6561 | auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS, | |||
6562 | ToCaseLoc, ToEllipsisLoc, ToColonLoc); | |||
6563 | ToStmt->setSubStmt(ToSubStmt); | |||
6564 | ||||
6565 | return ToStmt; | |||
6566 | } | |||
6567 | ||||
6568 | ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) { | |||
6569 | ||||
6570 | Error Err = Error::success(); | |||
6571 | auto ToDefaultLoc = importChecked(Err, S->getDefaultLoc()); | |||
6572 | auto ToColonLoc = importChecked(Err, S->getColonLoc()); | |||
6573 | auto ToSubStmt = importChecked(Err, S->getSubStmt()); | |||
6574 | if (Err) | |||
6575 | return std::move(Err); | |||
6576 | ||||
6577 | return new (Importer.getToContext()) DefaultStmt( | |||
6578 | ToDefaultLoc, ToColonLoc, ToSubStmt); | |||
6579 | } | |||
6580 | ||||
6581 | ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) { | |||
6582 | ||||
6583 | Error Err = Error::success(); | |||
6584 | auto ToIdentLoc = importChecked(Err, S->getIdentLoc()); | |||
6585 | auto ToLabelDecl = importChecked(Err, S->getDecl()); | |||
6586 | auto ToSubStmt = importChecked(Err, S->getSubStmt()); | |||
6587 | if (Err) | |||
6588 | return std::move(Err); | |||
6589 | ||||
6590 | return new (Importer.getToContext()) LabelStmt( | |||
6591 | ToIdentLoc, ToLabelDecl, ToSubStmt); | |||
6592 | } | |||
6593 | ||||
6594 | ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) { | |||
6595 | ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc()); | |||
6596 | if (!ToAttrLocOrErr) | |||
6597 | return ToAttrLocOrErr.takeError(); | |||
6598 | ArrayRef<const Attr*> FromAttrs(S->getAttrs()); | |||
6599 | SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size()); | |||
6600 | if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs)) | |||
6601 | return std::move(Err); | |||
6602 | ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt()); | |||
6603 | if (!ToSubStmtOrErr) | |||
6604 | return ToSubStmtOrErr.takeError(); | |||
6605 | ||||
6606 | return AttributedStmt::Create( | |||
6607 | Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr); | |||
6608 | } | |||
6609 | ||||
6610 | ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) { | |||
6611 | ||||
6612 | Error Err = Error::success(); | |||
6613 | auto ToIfLoc = importChecked(Err, S->getIfLoc()); | |||
6614 | auto ToInit = importChecked(Err, S->getInit()); | |||
6615 | auto ToConditionVariable = importChecked(Err, S->getConditionVariable()); | |||
6616 | auto ToCond = importChecked(Err, S->getCond()); | |||
6617 | auto ToLParenLoc = importChecked(Err, S->getLParenLoc()); | |||
6618 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | |||
6619 | auto ToThen = importChecked(Err, S->getThen()); | |||
6620 | auto ToElseLoc = importChecked(Err, S->getElseLoc()); | |||
6621 | auto ToElse = importChecked(Err, S->getElse()); | |||
6622 | if (Err) | |||
6623 | return std::move(Err); | |||
6624 | ||||
6625 | return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->getStatementKind(), | |||
6626 | ToInit, ToConditionVariable, ToCond, ToLParenLoc, | |||
6627 | ToRParenLoc, ToThen, ToElseLoc, ToElse); | |||
6628 | } | |||
6629 | ||||
6630 | ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) { | |||
6631 | ||||
6632 | Error Err = Error::success(); | |||
6633 | auto ToInit = importChecked(Err, S->getInit()); | |||
6634 | auto ToConditionVariable = importChecked(Err, S->getConditionVariable()); | |||
6635 | auto ToCond = importChecked(Err, S->getCond()); | |||
6636 | auto ToLParenLoc = importChecked(Err, S->getLParenLoc()); | |||
6637 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | |||
6638 | auto ToBody = importChecked(Err, S->getBody()); | |||
6639 | auto ToSwitchLoc = importChecked(Err, S->getSwitchLoc()); | |||
6640 | if (Err) | |||
6641 | return std::move(Err); | |||
6642 | ||||
6643 | auto *ToStmt = | |||
6644 | SwitchStmt::Create(Importer.getToContext(), ToInit, ToConditionVariable, | |||
6645 | ToCond, ToLParenLoc, ToRParenLoc); | |||
6646 | ToStmt->setBody(ToBody); | |||
6647 | ToStmt->setSwitchLoc(ToSwitchLoc); | |||
6648 | ||||
6649 | // Now we have to re-chain the cases. | |||
6650 | SwitchCase *LastChainedSwitchCase = nullptr; | |||
6651 | for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr; | |||
6652 | SC = SC->getNextSwitchCase()) { | |||
6653 | Expected<SwitchCase *> ToSCOrErr = import(SC); | |||
6654 | if (!ToSCOrErr) | |||
6655 | return ToSCOrErr.takeError(); | |||
6656 | if (LastChainedSwitchCase) | |||
6657 | LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr); | |||
6658 | else | |||
6659 | ToStmt->setSwitchCaseList(*ToSCOrErr); | |||
6660 | LastChainedSwitchCase = *ToSCOrErr; | |||
6661 | } | |||
6662 | ||||
6663 | return ToStmt; | |||
6664 | } | |||
6665 | ||||
6666 | ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) { | |||
6667 | ||||
6668 | Error Err = Error::success(); | |||
6669 | auto ToConditionVariable = importChecked(Err, S->getConditionVariable()); | |||
6670 | auto ToCond = importChecked(Err, S->getCond()); | |||
6671 | auto ToBody = importChecked(Err, S->getBody()); | |||
6672 | auto ToWhileLoc = importChecked(Err, S->getWhileLoc()); | |||
6673 | auto ToLParenLoc = importChecked(Err, S->getLParenLoc()); | |||
6674 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | |||
6675 | if (Err) | |||
6676 | return std::move(Err); | |||
6677 | ||||
6678 | return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond, | |||
6679 | ToBody, ToWhileLoc, ToLParenLoc, ToRParenLoc); | |||
6680 | } | |||
6681 | ||||
6682 | ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) { | |||
6683 | ||||
6684 | Error Err = Error::success(); | |||
6685 | auto ToBody = importChecked(Err, S->getBody()); | |||
6686 | auto ToCond = importChecked(Err, S->getCond()); | |||
6687 | auto ToDoLoc = importChecked(Err, S->getDoLoc()); | |||
6688 | auto ToWhileLoc = importChecked(Err, S->getWhileLoc()); | |||
6689 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | |||
6690 | if (Err) | |||
6691 | return std::move(Err); | |||
6692 | ||||
6693 | return new (Importer.getToContext()) DoStmt( | |||
6694 | ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc); | |||
6695 | } | |||
6696 | ||||
6697 | ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) { | |||
6698 | ||||
6699 | Error Err = Error::success(); | |||
6700 | auto ToInit = importChecked(Err, S->getInit()); | |||
6701 | auto ToCond = importChecked(Err, S->getCond()); | |||
6702 | auto ToConditionVariable = importChecked(Err, S->getConditionVariable()); | |||
6703 | auto ToInc = importChecked(Err, S->getInc()); | |||
6704 | auto ToBody = importChecked(Err, S->getBody()); | |||
6705 | auto ToForLoc = importChecked(Err, S->getForLoc()); | |||
6706 | auto ToLParenLoc = importChecked(Err, S->getLParenLoc()); | |||
6707 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | |||
6708 | if (Err) | |||
6709 | return std::move(Err); | |||
6710 | ||||
6711 | return new (Importer.getToContext()) ForStmt( | |||
6712 | Importer.getToContext(), | |||
6713 | ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc, | |||
6714 | ToRParenLoc); | |||
6715 | } | |||
6716 | ||||
6717 | ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) { | |||
6718 | ||||
6719 | Error Err = Error::success(); | |||
6720 | auto ToLabel = importChecked(Err, S->getLabel()); | |||
6721 | auto ToGotoLoc = importChecked(Err, S->getGotoLoc()); | |||
6722 | auto ToLabelLoc = importChecked(Err, S->getLabelLoc()); | |||
6723 | if (Err) | |||
6724 | return std::move(Err); | |||
6725 | ||||
6726 | return new (Importer.getToContext()) GotoStmt( | |||
6727 | ToLabel, ToGotoLoc, ToLabelLoc); | |||
6728 | } | |||
6729 | ||||
6730 | ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { | |||
6731 | ||||
6732 | Error Err = Error::success(); | |||
6733 | auto ToGotoLoc = importChecked(Err, S->getGotoLoc()); | |||
6734 | auto ToStarLoc = importChecked(Err, S->getStarLoc()); | |||
6735 | auto ToTarget = importChecked(Err, S->getTarget()); | |||
6736 | if (Err) | |||
6737 | return std::move(Err); | |||
6738 | ||||
6739 | return new (Importer.getToContext()) IndirectGotoStmt( | |||
6740 | ToGotoLoc, ToStarLoc, ToTarget); | |||
6741 | } | |||
6742 | ||||
6743 | ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) { | |||
6744 | ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc()); | |||
6745 | if (!ToContinueLocOrErr) | |||
6746 | return ToContinueLocOrErr.takeError(); | |||
6747 | return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr); | |||
6748 | } | |||
6749 | ||||
6750 | ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) { | |||
6751 | auto ToBreakLocOrErr = import(S->getBreakLoc()); | |||
6752 | if (!ToBreakLocOrErr) | |||
6753 | return ToBreakLocOrErr.takeError(); | |||
6754 | return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr); | |||
6755 | } | |||
6756 | ||||
6757 | ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) { | |||
6758 | ||||
6759 | Error Err = Error::success(); | |||
6760 | auto ToReturnLoc = importChecked(Err, S->getReturnLoc()); | |||
6761 | auto ToRetValue = importChecked(Err, S->getRetValue()); | |||
6762 | auto ToNRVOCandidate = importChecked(Err, S->getNRVOCandidate()); | |||
6763 | if (Err) | |||
6764 | return std::move(Err); | |||
6765 | ||||
6766 | return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue, | |||
6767 | ToNRVOCandidate); | |||
6768 | } | |||
6769 | ||||
6770 | ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) { | |||
6771 | ||||
6772 | Error Err = Error::success(); | |||
6773 | auto ToCatchLoc = importChecked(Err, S->getCatchLoc()); | |||
6774 | auto ToExceptionDecl = importChecked(Err, S->getExceptionDecl()); | |||
6775 | auto ToHandlerBlock = importChecked(Err, S->getHandlerBlock()); | |||
6776 | if (Err) | |||
6777 | return std::move(Err); | |||
6778 | ||||
6779 | return new (Importer.getToContext()) CXXCatchStmt ( | |||
6780 | ToCatchLoc, ToExceptionDecl, ToHandlerBlock); | |||
6781 | } | |||
6782 | ||||
6783 | ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) { | |||
6784 | ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc()); | |||
6785 | if (!ToTryLocOrErr) | |||
6786 | return ToTryLocOrErr.takeError(); | |||
6787 | ||||
6788 | ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock()); | |||
6789 | if (!ToTryBlockOrErr) | |||
6790 | return ToTryBlockOrErr.takeError(); | |||
6791 | ||||
6792 | SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers()); | |||
6793 | for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) { | |||
6794 | CXXCatchStmt *FromHandler = S->getHandler(HI); | |||
6795 | if (auto ToHandlerOrErr = import(FromHandler)) | |||
6796 | ToHandlers[HI] = *ToHandlerOrErr; | |||
6797 | else | |||
6798 | return ToHandlerOrErr.takeError(); | |||
6799 | } | |||
6800 | ||||
6801 | return CXXTryStmt::Create(Importer.getToContext(), *ToTryLocOrErr, | |||
6802 | cast<CompoundStmt>(*ToTryBlockOrErr), ToHandlers); | |||
6803 | } | |||
6804 | ||||
6805 | ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { | |||
6806 | ||||
6807 | Error Err = Error::success(); | |||
6808 | auto ToInit = importChecked(Err, S->getInit()); | |||
6809 | auto ToRangeStmt = importChecked(Err, S->getRangeStmt()); | |||
6810 | auto ToBeginStmt = importChecked(Err, S->getBeginStmt()); | |||
6811 | auto ToEndStmt = importChecked(Err, S->getEndStmt()); | |||
6812 | auto ToCond = importChecked(Err, S->getCond()); | |||
6813 | auto ToInc = importChecked(Err, S->getInc()); | |||
6814 | auto ToLoopVarStmt = importChecked(Err, S->getLoopVarStmt()); | |||
6815 | auto ToBody = importChecked(Err, S->getBody()); | |||
6816 | auto ToForLoc = importChecked(Err, S->getForLoc()); | |||
6817 | auto ToCoawaitLoc = importChecked(Err, S->getCoawaitLoc()); | |||
6818 | auto ToColonLoc = importChecked(Err, S->getColonLoc()); | |||
6819 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | |||
6820 | if (Err) | |||
6821 | return std::move(Err); | |||
6822 | ||||
6823 | return new (Importer.getToContext()) CXXForRangeStmt( | |||
6824 | ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt, | |||
6825 | ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc); | |||
6826 | } | |||
6827 | ||||
6828 | ExpectedStmt | |||
6829 | ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { | |||
6830 | Error Err = Error::success(); | |||
6831 | auto ToElement = importChecked(Err, S->getElement()); | |||
6832 | auto ToCollection = importChecked(Err, S->getCollection()); | |||
6833 | auto ToBody = importChecked(Err, S->getBody()); | |||
6834 | auto ToForLoc = importChecked(Err, S->getForLoc()); | |||
6835 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | |||
6836 | if (Err) | |||
6837 | return std::move(Err); | |||
6838 | ||||
6839 | return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement, | |||
6840 | ToCollection, | |||
6841 | ToBody, | |||
6842 | ToForLoc, | |||
6843 | ToRParenLoc); | |||
6844 | } | |||
6845 | ||||
6846 | ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { | |||
6847 | ||||
6848 | Error Err = Error::success(); | |||
6849 | auto ToAtCatchLoc = importChecked(Err, S->getAtCatchLoc()); | |||
6850 | auto ToRParenLoc = importChecked(Err, S->getRParenLoc()); | |||
6851 | auto ToCatchParamDecl = importChecked(Err, S->getCatchParamDecl()); | |||
6852 | auto ToCatchBody = importChecked(Err, S->getCatchBody()); | |||
6853 | if (Err) | |||
6854 | return std::move(Err); | |||
6855 | ||||
6856 | return new (Importer.getToContext()) ObjCAtCatchStmt ( | |||
6857 | ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody); | |||
6858 | } | |||
6859 | ||||
6860 | ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { | |||
6861 | ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc()); | |||
6862 | if (!ToAtFinallyLocOrErr) | |||
6863 | return ToAtFinallyLocOrErr.takeError(); | |||
6864 | ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody()); | |||
6865 | if (!ToAtFinallyStmtOrErr) | |||
6866 | return ToAtFinallyStmtOrErr.takeError(); | |||
6867 | return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr, | |||
6868 | *ToAtFinallyStmtOrErr); | |||
6869 | } | |||
6870 | ||||
6871 | ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { | |||
6872 | ||||
6873 | Error Err = Error::success(); | |||
6874 | auto ToAtTryLoc = importChecked(Err, S->getAtTryLoc()); | |||
6875 | auto ToTryBody = importChecked(Err, S->getTryBody()); | |||
6876 | auto ToFinallyStmt = importChecked(Err, S->getFinallyStmt()); | |||
6877 | if (Err) | |||
6878 | return std::move(Err); | |||
6879 | ||||
6880 | SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts()); | |||
6881 | for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) { | |||
6882 | ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI); | |||
6883 | if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt)) | |||
6884 | ToCatchStmts[CI] = *ToCatchStmtOrErr; | |||
6885 | else | |||
6886 | return ToCatchStmtOrErr.takeError(); | |||
6887 | } | |||
6888 | ||||
6889 | return ObjCAtTryStmt::Create(Importer.getToContext(), | |||
6890 | ToAtTryLoc, ToTryBody, | |||
6891 | ToCatchStmts.begin(), ToCatchStmts.size(), | |||
6892 | ToFinallyStmt); | |||
6893 | } | |||
6894 | ||||
6895 | ExpectedStmt | |||
6896 | ASTNodeImporter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { | |||
6897 | ||||
6898 | Error Err = Error::success(); | |||
6899 | auto ToAtSynchronizedLoc = importChecked(Err, S->getAtSynchronizedLoc()); | |||
6900 | auto ToSynchExpr = importChecked(Err, S->getSynchExpr()); | |||
6901 | auto ToSynchBody = importChecked(Err, S->getSynchBody()); | |||
6902 | if (Err) | |||
6903 | return std::move(Err); | |||
6904 | ||||
6905 | return new (Importer.getToContext()) ObjCAtSynchronizedStmt( | |||
6906 | ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody); | |||
6907 | } | |||
6908 | ||||
6909 | ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { | |||
6910 | ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc()); | |||
6911 | if (!ToThrowLocOrErr) | |||
6912 | return ToThrowLocOrErr.takeError(); | |||
6913 | ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr()); | |||
6914 | if (!ToThrowExprOrErr) | |||
6915 | return ToThrowExprOrErr.takeError(); | |||
6916 | return new (Importer.getToContext()) ObjCAtThrowStmt( | |||
6917 | *ToThrowLocOrErr, *ToThrowExprOrErr); | |||
6918 | } | |||
6919 | ||||
6920 | ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt( | |||
6921 | ObjCAutoreleasePoolStmt *S) { | |||
6922 | ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc()); | |||
6923 | if (!ToAtLocOrErr) | |||
6924 | return ToAtLocOrErr.takeError(); | |||
6925 | ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt()); | |||
6926 | if (!ToSubStmtOrErr) | |||
6927 | return ToSubStmtOrErr.takeError(); | |||
6928 | return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr, | |||
6929 | *ToSubStmtOrErr); | |||
6930 | } | |||
6931 | ||||
6932 | //---------------------------------------------------------------------------- | |||
6933 | // Import Expressions | |||
6934 | //---------------------------------------------------------------------------- | |||
6935 | ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) { | |||
6936 | Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node) | |||
6937 | << E->getStmtClassName(); | |||
6938 | return make_error<ASTImportError>(ASTImportError::UnsupportedConstruct); | |||
6939 | } | |||
6940 | ||||
6941 | ExpectedStmt ASTNodeImporter::VisitSourceLocExpr(SourceLocExpr *E) { | |||
6942 | Error Err = Error::success(); | |||
6943 | auto ToType = importChecked(Err, E->getType()); | |||
6944 | auto BLoc = importChecked(Err, E->getBeginLoc()); | |||
6945 | auto RParenLoc = importChecked(Err, E->getEndLoc()); | |||
6946 | if (Err) | |||
6947 | return std::move(Err); | |||
6948 | auto ParentContextOrErr = Importer.ImportContext(E->getParentContext()); | |||
6949 | if (!ParentContextOrErr) | |||
6950 | return ParentContextOrErr.takeError(); | |||
6951 | ||||
6952 | return new (Importer.getToContext()) | |||
6953 | SourceLocExpr(Importer.getToContext(), E->getIdentKind(), ToType, BLoc, | |||
6954 | RParenLoc, *ParentContextOrErr); | |||
6955 | } | |||
6956 | ||||
6957 | ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) { | |||
6958 | ||||
6959 | Error Err = Error::success(); | |||
6960 | auto ToBuiltinLoc = importChecked(Err, E->getBuiltinLoc()); | |||
6961 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | |||
6962 | auto ToWrittenTypeInfo = importChecked(Err, E->getWrittenTypeInfo()); | |||
6963 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
6964 | auto ToType = importChecked(Err, E->getType()); | |||
6965 | if (Err) | |||
6966 | return std::move(Err); | |||
6967 | ||||
6968 | return new (Importer.getToContext()) VAArgExpr( | |||
6969 | ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType, | |||
6970 | E->isMicrosoftABI()); | |||
6971 | } | |||
6972 | ||||
6973 | ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) { | |||
6974 | ||||
6975 | Error Err = Error::success(); | |||
6976 | auto ToCond = importChecked(Err, E->getCond()); | |||
6977 | auto ToLHS = importChecked(Err, E->getLHS()); | |||
6978 | auto ToRHS = importChecked(Err, E->getRHS()); | |||
6979 | auto ToBuiltinLoc = importChecked(Err, E->getBuiltinLoc()); | |||
6980 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
6981 | auto ToType = importChecked(Err, E->getType()); | |||
6982 | if (Err) | |||
6983 | return std::move(Err); | |||
6984 | ||||
6985 | ExprValueKind VK = E->getValueKind(); | |||
6986 | ExprObjectKind OK = E->getObjectKind(); | |||
6987 | ||||
6988 | // The value of CondIsTrue only matters if the value is not | |||
6989 | // condition-dependent. | |||
6990 | bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue(); | |||
6991 | ||||
6992 | return new (Importer.getToContext()) | |||
6993 | ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK, | |||
6994 | ToRParenLoc, CondIsTrue); | |||
6995 | } | |||
6996 | ||||
6997 | ExpectedStmt ASTNodeImporter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { | |||
6998 | Error Err = Error::success(); | |||
6999 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
7000 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | |||
7001 | auto ToType = importChecked(Err, E->getType()); | |||
7002 | const unsigned NumSubExprs = E->getNumSubExprs(); | |||
7003 | ||||
7004 | llvm::SmallVector<Expr *, 8> ToSubExprs; | |||
7005 | llvm::ArrayRef<Expr *> FromSubExprs(E->getSubExprs(), NumSubExprs); | |||
7006 | ToSubExprs.resize(NumSubExprs); | |||
7007 | ||||
7008 | if ((Err = ImportContainerChecked(FromSubExprs, ToSubExprs))) | |||
7009 | return std::move(Err); | |||
7010 | ||||
7011 | return new (Importer.getToContext()) ShuffleVectorExpr( | |||
7012 | Importer.getToContext(), ToSubExprs, ToType, ToBeginLoc, ToRParenLoc); | |||
7013 | } | |||
7014 | ||||
7015 | ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) { | |||
7016 | ExpectedType TypeOrErr = import(E->getType()); | |||
7017 | if (!TypeOrErr) | |||
7018 | return TypeOrErr.takeError(); | |||
7019 | ||||
7020 | ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc()); | |||
7021 | if (!BeginLocOrErr) | |||
7022 | return BeginLocOrErr.takeError(); | |||
7023 | ||||
7024 | return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr); | |||
7025 | } | |||
7026 | ||||
7027 | ExpectedStmt | |||
7028 | ASTNodeImporter::VisitGenericSelectionExpr(GenericSelectionExpr *E) { | |||
7029 | Error Err = Error::success(); | |||
7030 | auto ToGenericLoc = importChecked(Err, E->getGenericLoc()); | |||
7031 | auto *ToControllingExpr = importChecked(Err, E->getControllingExpr()); | |||
7032 | auto ToDefaultLoc = importChecked(Err, E->getDefaultLoc()); | |||
7033 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
7034 | if (Err) | |||
7035 | return std::move(Err); | |||
7036 | ||||
7037 | ArrayRef<const TypeSourceInfo *> FromAssocTypes(E->getAssocTypeSourceInfos()); | |||
7038 | SmallVector<TypeSourceInfo *, 1> ToAssocTypes(FromAssocTypes.size()); | |||
7039 | if (Error Err = ImportContainerChecked(FromAssocTypes, ToAssocTypes)) | |||
7040 | return std::move(Err); | |||
7041 | ||||
7042 | ArrayRef<const Expr *> FromAssocExprs(E->getAssocExprs()); | |||
7043 | SmallVector<Expr *, 1> ToAssocExprs(FromAssocExprs.size()); | |||
7044 | if (Error Err = ImportContainerChecked(FromAssocExprs, ToAssocExprs)) | |||
7045 | return std::move(Err); | |||
7046 | ||||
7047 | const ASTContext &ToCtx = Importer.getToContext(); | |||
7048 | if (E->isResultDependent()) { | |||
7049 | return GenericSelectionExpr::Create( | |||
7050 | ToCtx, ToGenericLoc, ToControllingExpr, llvm::ArrayRef(ToAssocTypes), | |||
7051 | llvm::ArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, | |||
7052 | E->containsUnexpandedParameterPack()); | |||
7053 | } | |||
7054 | ||||
7055 | return GenericSelectionExpr::Create( | |||
7056 | ToCtx, ToGenericLoc, ToControllingExpr, llvm::ArrayRef(ToAssocTypes), | |||
7057 | llvm::ArrayRef(ToAssocExprs), ToDefaultLoc, ToRParenLoc, | |||
7058 | E->containsUnexpandedParameterPack(), E->getResultIndex()); | |||
7059 | } | |||
7060 | ||||
7061 | ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) { | |||
7062 | ||||
7063 | Error Err = Error::success(); | |||
7064 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | |||
7065 | auto ToType = importChecked(Err, E->getType()); | |||
7066 | auto ToFunctionName = importChecked(Err, E->getFunctionName()); | |||
7067 | if (Err) | |||
7068 | return std::move(Err); | |||
7069 | ||||
7070 | return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType, | |||
7071 | E->getIdentKind(), ToFunctionName); | |||
7072 | } | |||
7073 | ||||
7074 | ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { | |||
7075 | ||||
7076 | Error Err = Error::success(); | |||
7077 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | |||
7078 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); | |||
7079 | auto ToDecl = importChecked(Err, E->getDecl()); | |||
7080 | auto ToLocation = importChecked(Err, E->getLocation()); | |||
7081 | auto ToType = importChecked(Err, E->getType()); | |||
7082 | if (Err) | |||
7083 | return std::move(Err); | |||
7084 | ||||
7085 | NamedDecl *ToFoundD = nullptr; | |||
7086 | if (E->getDecl() != E->getFoundDecl()) { | |||
7087 | auto FoundDOrErr = import(E->getFoundDecl()); | |||
7088 | if (!FoundDOrErr) | |||
7089 | return FoundDOrErr.takeError(); | |||
7090 | ToFoundD = *FoundDOrErr; | |||
7091 | } | |||
7092 | ||||
7093 | TemplateArgumentListInfo ToTAInfo; | |||
7094 | TemplateArgumentListInfo *ToResInfo = nullptr; | |||
7095 | if (E->hasExplicitTemplateArgs()) { | |||
7096 | if (Error Err = | |||
7097 | ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(), | |||
7098 | E->template_arguments(), ToTAInfo)) | |||
7099 | return std::move(Err); | |||
7100 | ToResInfo = &ToTAInfo; | |||
7101 | } | |||
7102 | ||||
7103 | auto *ToE = DeclRefExpr::Create( | |||
7104 | Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, | |||
7105 | E->refersToEnclosingVariableOrCapture(), ToLocation, ToType, | |||
7106 | E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse()); | |||
7107 | if (E->hadMultipleCandidates()) | |||
7108 | ToE->setHadMultipleCandidates(true); | |||
7109 | return ToE; | |||
7110 | } | |||
7111 | ||||
7112 | ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { | |||
7113 | ExpectedType TypeOrErr = import(E->getType()); | |||
7114 | if (!TypeOrErr) | |||
7115 | return TypeOrErr.takeError(); | |||
7116 | ||||
7117 | return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr); | |||
7118 | } | |||
7119 | ||||
7120 | ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) { | |||
7121 | ExpectedExpr ToInitOrErr = import(E->getInit()); | |||
7122 | if (!ToInitOrErr) | |||
7123 | return ToInitOrErr.takeError(); | |||
7124 | ||||
7125 | ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc()); | |||
7126 | if (!ToEqualOrColonLocOrErr) | |||
7127 | return ToEqualOrColonLocOrErr.takeError(); | |||
7128 | ||||
7129 | SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1); | |||
7130 | // List elements from the second, the first is Init itself | |||
7131 | for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) { | |||
7132 | if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I))) | |||
7133 | ToIndexExprs[I - 1] = *ToArgOrErr; | |||
7134 | else | |||
7135 | return ToArgOrErr.takeError(); | |||
7136 | } | |||
7137 | ||||
7138 | SmallVector<Designator, 4> ToDesignators(E->size()); | |||
7139 | if (Error Err = ImportContainerChecked(E->designators(), ToDesignators)) | |||
7140 | return std::move(Err); | |||
7141 | ||||
7142 | return DesignatedInitExpr::Create( | |||
7143 | Importer.getToContext(), ToDesignators, | |||
7144 | ToIndexExprs, *ToEqualOrColonLocOrErr, | |||
7145 | E->usesGNUSyntax(), *ToInitOrErr); | |||
7146 | } | |||
7147 | ||||
7148 | ExpectedStmt | |||
7149 | ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { | |||
7150 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
7151 | if (!ToTypeOrErr) | |||
7152 | return ToTypeOrErr.takeError(); | |||
7153 | ||||
7154 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | |||
7155 | if (!ToLocationOrErr) | |||
7156 | return ToLocationOrErr.takeError(); | |||
7157 | ||||
7158 | return new (Importer.getToContext()) CXXNullPtrLiteralExpr( | |||
7159 | *ToTypeOrErr, *ToLocationOrErr); | |||
7160 | } | |||
7161 | ||||
7162 | ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { | |||
7163 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
7164 | if (!ToTypeOrErr) | |||
7165 | return ToTypeOrErr.takeError(); | |||
7166 | ||||
7167 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | |||
7168 | if (!ToLocationOrErr) | |||
7169 | return ToLocationOrErr.takeError(); | |||
7170 | ||||
7171 | return IntegerLiteral::Create( | |||
7172 | Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr); | |||
7173 | } | |||
7174 | ||||
7175 | ||||
7176 | ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) { | |||
7177 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
7178 | if (!ToTypeOrErr) | |||
7179 | return ToTypeOrErr.takeError(); | |||
7180 | ||||
7181 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | |||
7182 | if (!ToLocationOrErr) | |||
7183 | return ToLocationOrErr.takeError(); | |||
7184 | ||||
7185 | return FloatingLiteral::Create( | |||
7186 | Importer.getToContext(), E->getValue(), E->isExact(), | |||
7187 | *ToTypeOrErr, *ToLocationOrErr); | |||
7188 | } | |||
7189 | ||||
7190 | ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) { | |||
7191 | auto ToTypeOrErr = import(E->getType()); | |||
7192 | if (!ToTypeOrErr) | |||
7193 | return ToTypeOrErr.takeError(); | |||
7194 | ||||
7195 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); | |||
7196 | if (!ToSubExprOrErr) | |||
7197 | return ToSubExprOrErr.takeError(); | |||
7198 | ||||
7199 | return new (Importer.getToContext()) ImaginaryLiteral( | |||
7200 | *ToSubExprOrErr, *ToTypeOrErr); | |||
7201 | } | |||
7202 | ||||
7203 | ExpectedStmt ASTNodeImporter::VisitFixedPointLiteral(FixedPointLiteral *E) { | |||
7204 | auto ToTypeOrErr = import(E->getType()); | |||
7205 | if (!ToTypeOrErr) | |||
7206 | return ToTypeOrErr.takeError(); | |||
7207 | ||||
7208 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | |||
7209 | if (!ToLocationOrErr) | |||
7210 | return ToLocationOrErr.takeError(); | |||
7211 | ||||
7212 | return new (Importer.getToContext()) FixedPointLiteral( | |||
7213 | Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr, | |||
7214 | Importer.getToContext().getFixedPointScale(*ToTypeOrErr)); | |||
7215 | } | |||
7216 | ||||
7217 | ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { | |||
7218 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
7219 | if (!ToTypeOrErr) | |||
7220 | return ToTypeOrErr.takeError(); | |||
7221 | ||||
7222 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | |||
7223 | if (!ToLocationOrErr) | |||
7224 | return ToLocationOrErr.takeError(); | |||
7225 | ||||
7226 | return new (Importer.getToContext()) CharacterLiteral( | |||
7227 | E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr); | |||
7228 | } | |||
7229 | ||||
7230 | ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) { | |||
7231 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
7232 | if (!ToTypeOrErr) | |||
7233 | return ToTypeOrErr.takeError(); | |||
7234 | ||||
7235 | SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated()); | |||
7236 | if (Error Err = ImportArrayChecked( | |||
7237 | E->tokloc_begin(), E->tokloc_end(), ToLocations.begin())) | |||
7238 | return std::move(Err); | |||
7239 | ||||
7240 | return StringLiteral::Create( | |||
7241 | Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(), | |||
7242 | *ToTypeOrErr, ToLocations.data(), ToLocations.size()); | |||
7243 | } | |||
7244 | ||||
7245 | ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { | |||
7246 | ||||
7247 | Error Err = Error::success(); | |||
7248 | auto ToLParenLoc = importChecked(Err, E->getLParenLoc()); | |||
7249 | auto ToTypeSourceInfo = importChecked(Err, E->getTypeSourceInfo()); | |||
7250 | auto ToType = importChecked(Err, E->getType()); | |||
7251 | auto ToInitializer = importChecked(Err, E->getInitializer()); | |||
7252 | if (Err) | |||
7253 | return std::move(Err); | |||
7254 | ||||
7255 | return new (Importer.getToContext()) CompoundLiteralExpr( | |||
7256 | ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(), | |||
7257 | ToInitializer, E->isFileScope()); | |||
7258 | } | |||
7259 | ||||
7260 | ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) { | |||
7261 | ||||
7262 | Error Err = Error::success(); | |||
7263 | auto ToBuiltinLoc = importChecked(Err, E->getBuiltinLoc()); | |||
7264 | auto ToType = importChecked(Err, E->getType()); | |||
7265 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
7266 | if (Err) | |||
7267 | return std::move(Err); | |||
7268 | ||||
7269 | SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs()); | |||
7270 | if (Error Err = ImportArrayChecked( | |||
7271 | E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(), | |||
7272 | ToExprs.begin())) | |||
7273 | return std::move(Err); | |||
7274 | ||||
7275 | return new (Importer.getToContext()) AtomicExpr( | |||
7276 | ||||
7277 | ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc); | |||
7278 | } | |||
7279 | ||||
7280 | ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) { | |||
7281 | Error Err = Error::success(); | |||
7282 | auto ToAmpAmpLoc = importChecked(Err, E->getAmpAmpLoc()); | |||
7283 | auto ToLabelLoc = importChecked(Err, E->getLabelLoc()); | |||
7284 | auto ToLabel = importChecked(Err, E->getLabel()); | |||
7285 | auto ToType = importChecked(Err, E->getType()); | |||
7286 | if (Err) | |||
7287 | return std::move(Err); | |||
7288 | ||||
7289 | return new (Importer.getToContext()) AddrLabelExpr( | |||
7290 | ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType); | |||
7291 | } | |||
7292 | ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) { | |||
7293 | Error Err = Error::success(); | |||
7294 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | |||
7295 | auto ToResult = importChecked(Err, E->getAPValueResult()); | |||
7296 | if (Err) | |||
7297 | return std::move(Err); | |||
7298 | ||||
7299 | return ConstantExpr::Create(Importer.getToContext(), ToSubExpr, ToResult); | |||
7300 | } | |||
7301 | ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) { | |||
7302 | Error Err = Error::success(); | |||
7303 | auto ToLParen = importChecked(Err, E->getLParen()); | |||
7304 | auto ToRParen = importChecked(Err, E->getRParen()); | |||
7305 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | |||
7306 | if (Err) | |||
7307 | return std::move(Err); | |||
7308 | ||||
7309 | return new (Importer.getToContext()) | |||
7310 | ParenExpr(ToLParen, ToRParen, ToSubExpr); | |||
7311 | } | |||
7312 | ||||
7313 | ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) { | |||
7314 | SmallVector<Expr *, 4> ToExprs(E->getNumExprs()); | |||
7315 | if (Error Err = ImportContainerChecked(E->exprs(), ToExprs)) | |||
7316 | return std::move(Err); | |||
7317 | ||||
7318 | ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc()); | |||
7319 | if (!ToLParenLocOrErr) | |||
7320 | return ToLParenLocOrErr.takeError(); | |||
7321 | ||||
7322 | ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc()); | |||
7323 | if (!ToRParenLocOrErr) | |||
7324 | return ToRParenLocOrErr.takeError(); | |||
7325 | ||||
7326 | return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr, | |||
7327 | ToExprs, *ToRParenLocOrErr); | |||
7328 | } | |||
7329 | ||||
7330 | ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) { | |||
7331 | Error Err = Error::success(); | |||
7332 | auto ToSubStmt = importChecked(Err, E->getSubStmt()); | |||
7333 | auto ToType = importChecked(Err, E->getType()); | |||
7334 | auto ToLParenLoc = importChecked(Err, E->getLParenLoc()); | |||
7335 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
7336 | if (Err) | |||
7337 | return std::move(Err); | |||
7338 | ||||
7339 | return new (Importer.getToContext()) | |||
7340 | StmtExpr(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc, | |||
7341 | E->getTemplateDepth()); | |||
7342 | } | |||
7343 | ||||
7344 | ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { | |||
7345 | Error Err = Error::success(); | |||
7346 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | |||
7347 | auto ToType = importChecked(Err, E->getType()); | |||
7348 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
7349 | if (Err) | |||
7350 | return std::move(Err); | |||
7351 | ||||
7352 | return UnaryOperator::Create( | |||
7353 | Importer.getToContext(), ToSubExpr, E->getOpcode(), ToType, | |||
7354 | E->getValueKind(), E->getObjectKind(), ToOperatorLoc, E->canOverflow(), | |||
7355 | E->getFPOptionsOverride()); | |||
7356 | } | |||
7357 | ||||
7358 | ExpectedStmt | |||
7359 | ||||
7360 | ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { | |||
7361 | Error Err = Error::success(); | |||
7362 | auto ToType = importChecked(Err, E->getType()); | |||
7363 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
7364 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
7365 | if (Err) | |||
7366 | return std::move(Err); | |||
7367 | ||||
7368 | if (E->isArgumentType()) { | |||
7369 | Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr = | |||
7370 | import(E->getArgumentTypeInfo()); | |||
7371 | if (!ToArgumentTypeInfoOrErr) | |||
7372 | return ToArgumentTypeInfoOrErr.takeError(); | |||
7373 | ||||
7374 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( | |||
7375 | E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc, | |||
7376 | ToRParenLoc); | |||
7377 | } | |||
7378 | ||||
7379 | ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr()); | |||
7380 | if (!ToArgumentExprOrErr) | |||
7381 | return ToArgumentExprOrErr.takeError(); | |||
7382 | ||||
7383 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr( | |||
7384 | E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc); | |||
7385 | } | |||
7386 | ||||
7387 | ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { | |||
7388 | Error Err = Error::success(); | |||
7389 | auto ToLHS = importChecked(Err, E->getLHS()); | |||
7390 | auto ToRHS = importChecked(Err, E->getRHS()); | |||
7391 | auto ToType = importChecked(Err, E->getType()); | |||
7392 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
7393 | if (Err) | |||
7394 | return std::move(Err); | |||
7395 | ||||
7396 | return BinaryOperator::Create( | |||
7397 | Importer.getToContext(), ToLHS, ToRHS, E->getOpcode(), ToType, | |||
7398 | E->getValueKind(), E->getObjectKind(), ToOperatorLoc, | |||
7399 | E->getFPFeatures()); | |||
7400 | } | |||
7401 | ||||
7402 | ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) { | |||
7403 | Error Err = Error::success(); | |||
7404 | auto ToCond = importChecked(Err, E->getCond()); | |||
7405 | auto ToQuestionLoc = importChecked(Err, E->getQuestionLoc()); | |||
7406 | auto ToLHS = importChecked(Err, E->getLHS()); | |||
7407 | auto ToColonLoc = importChecked(Err, E->getColonLoc()); | |||
7408 | auto ToRHS = importChecked(Err, E->getRHS()); | |||
7409 | auto ToType = importChecked(Err, E->getType()); | |||
7410 | if (Err) | |||
7411 | return std::move(Err); | |||
7412 | ||||
7413 | return new (Importer.getToContext()) ConditionalOperator( | |||
7414 | ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType, | |||
7415 | E->getValueKind(), E->getObjectKind()); | |||
7416 | } | |||
7417 | ||||
7418 | ExpectedStmt | |||
7419 | ASTNodeImporter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { | |||
7420 | Error Err = Error::success(); | |||
7421 | auto ToCommon = importChecked(Err, E->getCommon()); | |||
7422 | auto ToOpaqueValue = importChecked(Err, E->getOpaqueValue()); | |||
7423 | auto ToCond = importChecked(Err, E->getCond()); | |||
7424 | auto ToTrueExpr = importChecked(Err, E->getTrueExpr()); | |||
7425 | auto ToFalseExpr = importChecked(Err, E->getFalseExpr()); | |||
7426 | auto ToQuestionLoc = importChecked(Err, E->getQuestionLoc()); | |||
7427 | auto ToColonLoc = importChecked(Err, E->getColonLoc()); | |||
7428 | auto ToType = importChecked(Err, E->getType()); | |||
7429 | if (Err) | |||
| ||||
7430 | return std::move(Err); | |||
7431 | ||||
7432 | return new (Importer.getToContext()) BinaryConditionalOperator( | |||
7433 | ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, | |||
7434 | ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(), | |||
7435 | E->getObjectKind()); | |||
7436 | } | |||
7437 | ||||
7438 | ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { | |||
7439 | Error Err = Error::success(); | |||
7440 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | |||
7441 | auto ToQueriedTypeSourceInfo = | |||
7442 | importChecked(Err, E->getQueriedTypeSourceInfo()); | |||
7443 | auto ToDimensionExpression = importChecked(Err, E->getDimensionExpression()); | |||
7444 | auto ToEndLoc = importChecked(Err, E->getEndLoc()); | |||
7445 | auto ToType = importChecked(Err, E->getType()); | |||
7446 | if (Err) | |||
7447 | return std::move(Err); | |||
7448 | ||||
7449 | return new (Importer.getToContext()) ArrayTypeTraitExpr( | |||
7450 | ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(), | |||
7451 | ToDimensionExpression, ToEndLoc, ToType); | |||
7452 | } | |||
7453 | ||||
7454 | ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { | |||
7455 | Error Err = Error::success(); | |||
7456 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | |||
7457 | auto ToQueriedExpression = importChecked(Err, E->getQueriedExpression()); | |||
7458 | auto ToEndLoc = importChecked(Err, E->getEndLoc()); | |||
7459 | auto ToType = importChecked(Err, E->getType()); | |||
7460 | if (Err) | |||
7461 | return std::move(Err); | |||
7462 | ||||
7463 | return new (Importer.getToContext()) ExpressionTraitExpr( | |||
7464 | ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(), | |||
7465 | ToEndLoc, ToType); | |||
7466 | } | |||
7467 | ||||
7468 | ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) { | |||
7469 | Error Err = Error::success(); | |||
7470 | auto ToLocation = importChecked(Err, E->getLocation()); | |||
7471 | auto ToType = importChecked(Err, E->getType()); | |||
7472 | auto ToSourceExpr = importChecked(Err, E->getSourceExpr()); | |||
7473 | if (Err) | |||
7474 | return std::move(Err); | |||
7475 | ||||
7476 | return new (Importer.getToContext()) OpaqueValueExpr( | |||
7477 | ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr); | |||
7478 | } | |||
7479 | ||||
7480 | ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { | |||
7481 | Error Err = Error::success(); | |||
7482 | auto ToLHS = importChecked(Err, E->getLHS()); | |||
7483 | auto ToRHS = importChecked(Err, E->getRHS()); | |||
7484 | auto ToType = importChecked(Err, E->getType()); | |||
7485 | auto ToRBracketLoc = importChecked(Err, E->getRBracketLoc()); | |||
7486 | if (Err) | |||
7487 | return std::move(Err); | |||
7488 | ||||
7489 | return new (Importer.getToContext()) ArraySubscriptExpr( | |||
7490 | ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(), | |||
7491 | ToRBracketLoc); | |||
7492 | } | |||
7493 | ||||
7494 | ExpectedStmt | |||
7495 | ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { | |||
7496 | Error Err = Error::success(); | |||
7497 | auto ToLHS = importChecked(Err, E->getLHS()); | |||
7498 | auto ToRHS = importChecked(Err, E->getRHS()); | |||
7499 | auto ToType = importChecked(Err, E->getType()); | |||
7500 | auto ToComputationLHSType = importChecked(Err, E->getComputationLHSType()); | |||
7501 | auto ToComputationResultType = | |||
7502 | importChecked(Err, E->getComputationResultType()); | |||
7503 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
7504 | if (Err) | |||
7505 | return std::move(Err); | |||
7506 | ||||
7507 | return CompoundAssignOperator::Create( | |||
7508 | Importer.getToContext(), ToLHS, ToRHS, E->getOpcode(), ToType, | |||
7509 | E->getValueKind(), E->getObjectKind(), ToOperatorLoc, | |||
7510 | E->getFPFeatures(), | |||
7511 | ToComputationLHSType, ToComputationResultType); | |||
7512 | } | |||
7513 | ||||
7514 | Expected<CXXCastPath> | |||
7515 | ASTNodeImporter::ImportCastPath(CastExpr *CE) { | |||
7516 | CXXCastPath Path; | |||
7517 | for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) { | |||
7518 | if (auto SpecOrErr = import(*I)) | |||
7519 | Path.push_back(*SpecOrErr); | |||
7520 | else | |||
7521 | return SpecOrErr.takeError(); | |||
7522 | } | |||
7523 | return Path; | |||
7524 | } | |||
7525 | ||||
7526 | ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { | |||
7527 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
7528 | if (!ToTypeOrErr) | |||
7529 | return ToTypeOrErr.takeError(); | |||
7530 | ||||
7531 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); | |||
7532 | if (!ToSubExprOrErr) | |||
7533 | return ToSubExprOrErr.takeError(); | |||
7534 | ||||
7535 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E); | |||
7536 | if (!ToBasePathOrErr) | |||
7537 | return ToBasePathOrErr.takeError(); | |||
7538 | ||||
7539 | return ImplicitCastExpr::Create( | |||
7540 | Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr, | |||
7541 | &(*ToBasePathOrErr), E->getValueKind(), E->getFPFeatures()); | |||
7542 | } | |||
7543 | ||||
7544 | ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) { | |||
7545 | Error Err = Error::success(); | |||
7546 | auto ToType = importChecked(Err, E->getType()); | |||
7547 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | |||
7548 | auto ToTypeInfoAsWritten = importChecked(Err, E->getTypeInfoAsWritten()); | |||
7549 | if (Err) | |||
7550 | return std::move(Err); | |||
7551 | ||||
7552 | Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E); | |||
7553 | if (!ToBasePathOrErr) | |||
7554 | return ToBasePathOrErr.takeError(); | |||
7555 | CXXCastPath *ToBasePath = &(*ToBasePathOrErr); | |||
7556 | ||||
7557 | switch (E->getStmtClass()) { | |||
7558 | case Stmt::CStyleCastExprClass: { | |||
7559 | auto *CCE = cast<CStyleCastExpr>(E); | |||
7560 | ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc()); | |||
7561 | if (!ToLParenLocOrErr) | |||
7562 | return ToLParenLocOrErr.takeError(); | |||
7563 | ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc()); | |||
7564 | if (!ToRParenLocOrErr) | |||
7565 | return ToRParenLocOrErr.takeError(); | |||
7566 | return CStyleCastExpr::Create( | |||
7567 | Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(), | |||
7568 | ToSubExpr, ToBasePath, CCE->getFPFeatures(), ToTypeInfoAsWritten, | |||
7569 | *ToLParenLocOrErr, *ToRParenLocOrErr); | |||
7570 | } | |||
7571 | ||||
7572 | case Stmt::CXXFunctionalCastExprClass: { | |||
7573 | auto *FCE = cast<CXXFunctionalCastExpr>(E); | |||
7574 | ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc()); | |||
7575 | if (!ToLParenLocOrErr) | |||
7576 | return ToLParenLocOrErr.takeError(); | |||
7577 | ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc()); | |||
7578 | if (!ToRParenLocOrErr) | |||
7579 | return ToRParenLocOrErr.takeError(); | |||
7580 | return CXXFunctionalCastExpr::Create( | |||
7581 | Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten, | |||
7582 | E->getCastKind(), ToSubExpr, ToBasePath, FCE->getFPFeatures(), | |||
7583 | *ToLParenLocOrErr, *ToRParenLocOrErr); | |||
7584 | } | |||
7585 | ||||
7586 | case Stmt::ObjCBridgedCastExprClass: { | |||
7587 | auto *OCE = cast<ObjCBridgedCastExpr>(E); | |||
7588 | ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc()); | |||
7589 | if (!ToLParenLocOrErr) | |||
7590 | return ToLParenLocOrErr.takeError(); | |||
7591 | ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc()); | |||
7592 | if (!ToBridgeKeywordLocOrErr) | |||
7593 | return ToBridgeKeywordLocOrErr.takeError(); | |||
7594 | return new (Importer.getToContext()) ObjCBridgedCastExpr( | |||
7595 | *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(), | |||
7596 | *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr); | |||
7597 | } | |||
7598 | default: | |||
7599 | llvm_unreachable("Cast expression of unsupported type!")::llvm::llvm_unreachable_internal("Cast expression of unsupported type!" , "clang/lib/AST/ASTImporter.cpp", 7599); | |||
7600 | return make_error<ASTImportError>(ASTImportError::UnsupportedConstruct); | |||
7601 | } | |||
7602 | } | |||
7603 | ||||
7604 | ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) { | |||
7605 | SmallVector<OffsetOfNode, 4> ToNodes; | |||
7606 | for (int I = 0, N = E->getNumComponents(); I < N; ++I) { | |||
7607 | const OffsetOfNode &FromNode = E->getComponent(I); | |||
7608 | ||||
7609 | SourceLocation ToBeginLoc, ToEndLoc; | |||
7610 | ||||
7611 | if (FromNode.getKind() != OffsetOfNode::Base) { | |||
7612 | Error Err = Error::success(); | |||
7613 | ToBeginLoc = importChecked(Err, FromNode.getBeginLoc()); | |||
7614 | ToEndLoc = importChecked(Err, FromNode.getEndLoc()); | |||
7615 | if (Err) | |||
7616 | return std::move(Err); | |||
7617 | } | |||
7618 | ||||
7619 | switch (FromNode.getKind()) { | |||
7620 | case OffsetOfNode::Array: | |||
7621 | ToNodes.push_back( | |||
7622 | OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc)); | |||
7623 | break; | |||
7624 | case OffsetOfNode::Base: { | |||
7625 | auto ToBSOrErr = import(FromNode.getBase()); | |||
7626 | if (!ToBSOrErr) | |||
7627 | return ToBSOrErr.takeError(); | |||
7628 | ToNodes.push_back(OffsetOfNode(*ToBSOrErr)); | |||
7629 | break; | |||
7630 | } | |||
7631 | case OffsetOfNode::Field: { | |||
7632 | auto ToFieldOrErr = import(FromNode.getField()); | |||
7633 | if (!ToFieldOrErr) | |||
7634 | return ToFieldOrErr.takeError(); | |||
7635 | ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc)); | |||
7636 | break; | |||
7637 | } | |||
7638 | case OffsetOfNode::Identifier: { | |||
7639 | IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName()); | |||
7640 | ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc)); | |||
7641 | break; | |||
7642 | } | |||
7643 | } | |||
7644 | } | |||
7645 | ||||
7646 | SmallVector<Expr *, 4> ToExprs(E->getNumExpressions()); | |||
7647 | for (int I = 0, N = E->getNumExpressions(); I < N; ++I) { | |||
7648 | ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I)); | |||
7649 | if (!ToIndexExprOrErr) | |||
7650 | return ToIndexExprOrErr.takeError(); | |||
7651 | ToExprs[I] = *ToIndexExprOrErr; | |||
7652 | } | |||
7653 | ||||
7654 | Error Err = Error::success(); | |||
7655 | auto ToType = importChecked(Err, E->getType()); | |||
7656 | auto ToTypeSourceInfo = importChecked(Err, E->getTypeSourceInfo()); | |||
7657 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
7658 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
7659 | if (Err) | |||
7660 | return std::move(Err); | |||
7661 | ||||
7662 | return OffsetOfExpr::Create( | |||
7663 | Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes, | |||
7664 | ToExprs, ToRParenLoc); | |||
7665 | } | |||
7666 | ||||
7667 | ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { | |||
7668 | Error Err = Error::success(); | |||
7669 | auto ToType = importChecked(Err, E->getType()); | |||
7670 | auto ToOperand = importChecked(Err, E->getOperand()); | |||
7671 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | |||
7672 | auto ToEndLoc = importChecked(Err, E->getEndLoc()); | |||
7673 | if (Err) | |||
7674 | return std::move(Err); | |||
7675 | ||||
7676 | CanThrowResult ToCanThrow; | |||
7677 | if (E->isValueDependent()) | |||
7678 | ToCanThrow = CT_Dependent; | |||
7679 | else | |||
7680 | ToCanThrow = E->getValue() ? CT_Can : CT_Cannot; | |||
7681 | ||||
7682 | return new (Importer.getToContext()) CXXNoexceptExpr( | |||
7683 | ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc); | |||
7684 | } | |||
7685 | ||||
7686 | ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) { | |||
7687 | Error Err = Error::success(); | |||
7688 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | |||
7689 | auto ToType = importChecked(Err, E->getType()); | |||
7690 | auto ToThrowLoc = importChecked(Err, E->getThrowLoc()); | |||
7691 | if (Err) | |||
7692 | return std::move(Err); | |||
7693 | ||||
7694 | return new (Importer.getToContext()) CXXThrowExpr( | |||
7695 | ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope()); | |||
7696 | } | |||
7697 | ||||
7698 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { | |||
7699 | ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation()); | |||
7700 | if (!ToUsedLocOrErr) | |||
7701 | return ToUsedLocOrErr.takeError(); | |||
7702 | ||||
7703 | auto ToParamOrErr = import(E->getParam()); | |||
7704 | if (!ToParamOrErr) | |||
7705 | return ToParamOrErr.takeError(); | |||
7706 | ||||
7707 | auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext()); | |||
7708 | if (!UsedContextOrErr) | |||
7709 | return UsedContextOrErr.takeError(); | |||
7710 | ||||
7711 | // Import the default arg if it was not imported yet. | |||
7712 | // This is needed because it can happen that during the import of the | |||
7713 | // default expression (from VisitParmVarDecl) the same ParmVarDecl is | |||
7714 | // encountered here. The default argument for a ParmVarDecl is set in the | |||
7715 | // ParmVarDecl only after it is imported (set in VisitParmVarDecl if not here, | |||
7716 | // see VisitParmVarDecl). | |||
7717 | ParmVarDecl *ToParam = *ToParamOrErr; | |||
7718 | if (!ToParam->getDefaultArg()) { | |||
7719 | std::optional<ParmVarDecl *> FromParam = | |||
7720 | Importer.getImportedFromDecl(ToParam); | |||
7721 | assert(FromParam && "ParmVarDecl was not imported?")(static_cast <bool> (FromParam && "ParmVarDecl was not imported?" ) ? void (0) : __assert_fail ("FromParam && \"ParmVarDecl was not imported?\"" , "clang/lib/AST/ASTImporter.cpp", 7721, __extension__ __PRETTY_FUNCTION__ )); | |||
7722 | ||||
7723 | if (Error Err = ImportDefaultArgOfParmVarDecl(*FromParam, ToParam)) | |||
7724 | return std::move(Err); | |||
7725 | } | |||
7726 | Expr *RewrittenInit = nullptr; | |||
7727 | if (E->hasRewrittenInit()) { | |||
7728 | ExpectedExpr ExprOrErr = import(E->getRewrittenExpr()); | |||
7729 | if (!ExprOrErr) | |||
7730 | return ExprOrErr.takeError(); | |||
7731 | RewrittenInit = ExprOrErr.get(); | |||
7732 | } | |||
7733 | return CXXDefaultArgExpr::Create(Importer.getToContext(), *ToUsedLocOrErr, | |||
7734 | *ToParamOrErr, RewrittenInit, | |||
7735 | *UsedContextOrErr); | |||
7736 | } | |||
7737 | ||||
7738 | ExpectedStmt | |||
7739 | ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { | |||
7740 | Error Err = Error::success(); | |||
7741 | auto ToType = importChecked(Err, E->getType()); | |||
7742 | auto ToTypeSourceInfo = importChecked(Err, E->getTypeSourceInfo()); | |||
7743 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
7744 | if (Err) | |||
7745 | return std::move(Err); | |||
7746 | ||||
7747 | return new (Importer.getToContext()) CXXScalarValueInitExpr( | |||
7748 | ToType, ToTypeSourceInfo, ToRParenLoc); | |||
7749 | } | |||
7750 | ||||
7751 | ExpectedStmt | |||
7752 | ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { | |||
7753 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); | |||
7754 | if (!ToSubExprOrErr) | |||
7755 | return ToSubExprOrErr.takeError(); | |||
7756 | ||||
7757 | auto ToDtorOrErr = import(E->getTemporary()->getDestructor()); | |||
7758 | if (!ToDtorOrErr) | |||
7759 | return ToDtorOrErr.takeError(); | |||
7760 | ||||
7761 | ASTContext &ToCtx = Importer.getToContext(); | |||
7762 | CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr); | |||
7763 | return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr); | |||
7764 | } | |||
7765 | ||||
7766 | ExpectedStmt | |||
7767 | ||||
7768 | ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { | |||
7769 | Error Err = Error::success(); | |||
7770 | auto ToConstructor = importChecked(Err, E->getConstructor()); | |||
7771 | auto ToType = importChecked(Err, E->getType()); | |||
7772 | auto ToTypeSourceInfo = importChecked(Err, E->getTypeSourceInfo()); | |||
7773 | auto ToParenOrBraceRange = importChecked(Err, E->getParenOrBraceRange()); | |||
7774 | if (Err) | |||
7775 | return std::move(Err); | |||
7776 | ||||
7777 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); | |||
7778 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) | |||
7779 | return std::move(Err); | |||
7780 | ||||
7781 | return CXXTemporaryObjectExpr::Create( | |||
7782 | Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs, | |||
7783 | ToParenOrBraceRange, E->hadMultipleCandidates(), | |||
7784 | E->isListInitialization(), E->isStdInitListInitialization(), | |||
7785 | E->requiresZeroInitialization()); | |||
7786 | } | |||
7787 | ||||
7788 | ExpectedDecl ASTNodeImporter::VisitLifetimeExtendedTemporaryDecl( | |||
7789 | LifetimeExtendedTemporaryDecl *D) { | |||
7790 | DeclContext *DC, *LexicalDC; | |||
7791 | if (Error Err = ImportDeclContext(D, DC, LexicalDC)) | |||
7792 | return std::move(Err); | |||
7793 | ||||
7794 | Error Err = Error::success(); | |||
7795 | auto Temporary = importChecked(Err, D->getTemporaryExpr()); | |||
7796 | auto ExtendingDecl = importChecked(Err, D->getExtendingDecl()); | |||
7797 | if (Err) | |||
7798 | return std::move(Err); | |||
7799 | // FIXME: Should ManglingNumber get numbers associated with 'to' context? | |||
7800 | ||||
7801 | LifetimeExtendedTemporaryDecl *To; | |||
7802 | if (GetImportedOrCreateDecl(To, D, Temporary, ExtendingDecl, | |||
7803 | D->getManglingNumber())) | |||
7804 | return To; | |||
7805 | ||||
7806 | To->setLexicalDeclContext(LexicalDC); | |||
7807 | LexicalDC->addDeclInternal(To); | |||
7808 | return To; | |||
7809 | } | |||
7810 | ||||
7811 | ExpectedStmt | |||
7812 | ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { | |||
7813 | Error Err = Error::success(); | |||
7814 | auto ToType = importChecked(Err, E->getType()); | |||
7815 | Expr *ToTemporaryExpr = importChecked( | |||
7816 | Err, E->getLifetimeExtendedTemporaryDecl() ? nullptr : E->getSubExpr()); | |||
7817 | auto ToMaterializedDecl = | |||
7818 | importChecked(Err, E->getLifetimeExtendedTemporaryDecl()); | |||
7819 | if (Err) | |||
7820 | return std::move(Err); | |||
7821 | ||||
7822 | if (!ToTemporaryExpr) | |||
7823 | ToTemporaryExpr = cast<Expr>(ToMaterializedDecl->getTemporaryExpr()); | |||
7824 | ||||
7825 | auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr( | |||
7826 | ToType, ToTemporaryExpr, E->isBoundToLvalueReference(), | |||
7827 | ToMaterializedDecl); | |||
7828 | ||||
7829 | return ToMTE; | |||
7830 | } | |||
7831 | ||||
7832 | ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) { | |||
7833 | Error Err = Error::success(); | |||
7834 | auto ToType = importChecked(Err, E->getType()); | |||
7835 | auto ToPattern = importChecked(Err, E->getPattern()); | |||
7836 | auto ToEllipsisLoc = importChecked(Err, E->getEllipsisLoc()); | |||
7837 | if (Err) | |||
7838 | return std::move(Err); | |||
7839 | ||||
7840 | return new (Importer.getToContext()) PackExpansionExpr( | |||
7841 | ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions()); | |||
7842 | } | |||
7843 | ||||
7844 | ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) { | |||
7845 | Error Err = Error::success(); | |||
7846 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
7847 | auto ToPack = importChecked(Err, E->getPack()); | |||
7848 | auto ToPackLoc = importChecked(Err, E->getPackLoc()); | |||
7849 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
7850 | if (Err) | |||
7851 | return std::move(Err); | |||
7852 | ||||
7853 | std::optional<unsigned> Length; | |||
7854 | if (!E->isValueDependent()) | |||
7855 | Length = E->getPackLength(); | |||
7856 | ||||
7857 | SmallVector<TemplateArgument, 8> ToPartialArguments; | |||
7858 | if (E->isPartiallySubstituted()) { | |||
7859 | if (Error Err = ImportTemplateArguments(E->getPartialArguments(), | |||
7860 | ToPartialArguments)) | |||
7861 | return std::move(Err); | |||
7862 | } | |||
7863 | ||||
7864 | return SizeOfPackExpr::Create( | |||
7865 | Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc, | |||
7866 | Length, ToPartialArguments); | |||
7867 | } | |||
7868 | ||||
7869 | ||||
7870 | ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) { | |||
7871 | Error Err = Error::success(); | |||
7872 | auto ToOperatorNew = importChecked(Err, E->getOperatorNew()); | |||
7873 | auto ToOperatorDelete = importChecked(Err, E->getOperatorDelete()); | |||
7874 | auto ToTypeIdParens = importChecked(Err, E->getTypeIdParens()); | |||
7875 | auto ToArraySize = importChecked(Err, E->getArraySize()); | |||
7876 | auto ToInitializer = importChecked(Err, E->getInitializer()); | |||
7877 | auto ToType = importChecked(Err, E->getType()); | |||
7878 | auto ToAllocatedTypeSourceInfo = | |||
7879 | importChecked(Err, E->getAllocatedTypeSourceInfo()); | |||
7880 | auto ToSourceRange = importChecked(Err, E->getSourceRange()); | |||
7881 | auto ToDirectInitRange = importChecked(Err, E->getDirectInitRange()); | |||
7882 | if (Err) | |||
7883 | return std::move(Err); | |||
7884 | ||||
7885 | SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs()); | |||
7886 | if (Error Err = | |||
7887 | ImportContainerChecked(E->placement_arguments(), ToPlacementArgs)) | |||
7888 | return std::move(Err); | |||
7889 | ||||
7890 | return CXXNewExpr::Create( | |||
7891 | Importer.getToContext(), E->isGlobalNew(), ToOperatorNew, | |||
7892 | ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(), | |||
7893 | ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(), | |||
7894 | ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange, | |||
7895 | ToDirectInitRange); | |||
7896 | } | |||
7897 | ||||
7898 | ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { | |||
7899 | Error Err = Error::success(); | |||
7900 | auto ToType = importChecked(Err, E->getType()); | |||
7901 | auto ToOperatorDelete = importChecked(Err, E->getOperatorDelete()); | |||
7902 | auto ToArgument = importChecked(Err, E->getArgument()); | |||
7903 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | |||
7904 | if (Err) | |||
7905 | return std::move(Err); | |||
7906 | ||||
7907 | return new (Importer.getToContext()) CXXDeleteExpr( | |||
7908 | ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(), | |||
7909 | E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument, | |||
7910 | ToBeginLoc); | |||
7911 | } | |||
7912 | ||||
7913 | ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) { | |||
7914 | Error Err = Error::success(); | |||
7915 | auto ToType = importChecked(Err, E->getType()); | |||
7916 | auto ToLocation = importChecked(Err, E->getLocation()); | |||
7917 | auto ToConstructor = importChecked(Err, E->getConstructor()); | |||
7918 | auto ToParenOrBraceRange = importChecked(Err, E->getParenOrBraceRange()); | |||
7919 | if (Err) | |||
7920 | return std::move(Err); | |||
7921 | ||||
7922 | SmallVector<Expr *, 6> ToArgs(E->getNumArgs()); | |||
7923 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) | |||
7924 | return std::move(Err); | |||
7925 | ||||
7926 | return CXXConstructExpr::Create( | |||
7927 | Importer.getToContext(), ToType, ToLocation, ToConstructor, | |||
7928 | E->isElidable(), ToArgs, E->hadMultipleCandidates(), | |||
7929 | E->isListInitialization(), E->isStdInitListInitialization(), | |||
7930 | E->requiresZeroInitialization(), E->getConstructionKind(), | |||
7931 | ToParenOrBraceRange); | |||
7932 | } | |||
7933 | ||||
7934 | ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) { | |||
7935 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); | |||
7936 | if (!ToSubExprOrErr) | |||
7937 | return ToSubExprOrErr.takeError(); | |||
7938 | ||||
7939 | SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects()); | |||
7940 | if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects)) | |||
7941 | return std::move(Err); | |||
7942 | ||||
7943 | return ExprWithCleanups::Create( | |||
7944 | Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(), | |||
7945 | ToObjects); | |||
7946 | } | |||
7947 | ||||
7948 | ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { | |||
7949 | Error Err = Error::success(); | |||
7950 | auto ToCallee = importChecked(Err, E->getCallee()); | |||
7951 | auto ToType = importChecked(Err, E->getType()); | |||
7952 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
7953 | if (Err) | |||
7954 | return std::move(Err); | |||
7955 | ||||
7956 | SmallVector<Expr *, 4> ToArgs(E->getNumArgs()); | |||
7957 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) | |||
7958 | return std::move(Err); | |||
7959 | ||||
7960 | return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, | |||
7961 | ToType, E->getValueKind(), ToRParenLoc, | |||
7962 | E->getFPFeatures()); | |||
7963 | } | |||
7964 | ||||
7965 | ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) { | |||
7966 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
7967 | if (!ToTypeOrErr) | |||
7968 | return ToTypeOrErr.takeError(); | |||
7969 | ||||
7970 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | |||
7971 | if (!ToLocationOrErr) | |||
7972 | return ToLocationOrErr.takeError(); | |||
7973 | ||||
7974 | return new (Importer.getToContext()) CXXThisExpr( | |||
7975 | *ToLocationOrErr, *ToTypeOrErr, E->isImplicit()); | |||
7976 | } | |||
7977 | ||||
7978 | ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { | |||
7979 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
7980 | if (!ToTypeOrErr) | |||
7981 | return ToTypeOrErr.takeError(); | |||
7982 | ||||
7983 | ExpectedSLoc ToLocationOrErr = import(E->getLocation()); | |||
7984 | if (!ToLocationOrErr) | |||
7985 | return ToLocationOrErr.takeError(); | |||
7986 | ||||
7987 | return CXXBoolLiteralExpr::Create(Importer.getToContext(), E->getValue(), | |||
7988 | *ToTypeOrErr, *ToLocationOrErr); | |||
7989 | } | |||
7990 | ||||
7991 | ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) { | |||
7992 | Error Err = Error::success(); | |||
7993 | auto ToBase = importChecked(Err, E->getBase()); | |||
7994 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
7995 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | |||
7996 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); | |||
7997 | auto ToMemberDecl = importChecked(Err, E->getMemberDecl()); | |||
7998 | auto ToType = importChecked(Err, E->getType()); | |||
7999 | auto ToDecl = importChecked(Err, E->getFoundDecl().getDecl()); | |||
8000 | auto ToName = importChecked(Err, E->getMemberNameInfo().getName()); | |||
8001 | auto ToLoc = importChecked(Err, E->getMemberNameInfo().getLoc()); | |||
8002 | if (Err) | |||
8003 | return std::move(Err); | |||
8004 | ||||
8005 | DeclAccessPair ToFoundDecl = | |||
8006 | DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess()); | |||
8007 | ||||
8008 | DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc); | |||
8009 | ||||
8010 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; | |||
8011 | if (E->hasExplicitTemplateArgs()) { | |||
8012 | if (Error Err = | |||
8013 | ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(), | |||
8014 | E->template_arguments(), ToTAInfo)) | |||
8015 | return std::move(Err); | |||
8016 | ResInfo = &ToTAInfo; | |||
8017 | } | |||
8018 | ||||
8019 | return MemberExpr::Create(Importer.getToContext(), ToBase, E->isArrow(), | |||
8020 | ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, | |||
8021 | ToMemberDecl, ToFoundDecl, ToMemberNameInfo, | |||
8022 | ResInfo, ToType, E->getValueKind(), | |||
8023 | E->getObjectKind(), E->isNonOdrUse()); | |||
8024 | } | |||
8025 | ||||
8026 | ExpectedStmt | |||
8027 | ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { | |||
8028 | Error Err = Error::success(); | |||
8029 | auto ToBase = importChecked(Err, E->getBase()); | |||
8030 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
8031 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | |||
8032 | auto ToScopeTypeInfo = importChecked(Err, E->getScopeTypeInfo()); | |||
8033 | auto ToColonColonLoc = importChecked(Err, E->getColonColonLoc()); | |||
8034 | auto ToTildeLoc = importChecked(Err, E->getTildeLoc()); | |||
8035 | if (Err) | |||
8036 | return std::move(Err); | |||
8037 | ||||
8038 | PseudoDestructorTypeStorage Storage; | |||
8039 | if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) { | |||
8040 | IdentifierInfo *ToII = Importer.Import(FromII); | |||
8041 | ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc()); | |||
8042 | if (!ToDestroyedTypeLocOrErr) | |||
8043 | return ToDestroyedTypeLocOrErr.takeError(); | |||
8044 | Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr); | |||
8045 | } else { | |||
8046 | if (auto ToTIOrErr = import(E->getDestroyedTypeInfo())) | |||
8047 | Storage = PseudoDestructorTypeStorage(*ToTIOrErr); | |||
8048 | else | |||
8049 | return ToTIOrErr.takeError(); | |||
8050 | } | |||
8051 | ||||
8052 | return new (Importer.getToContext()) CXXPseudoDestructorExpr( | |||
8053 | Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc, | |||
8054 | ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage); | |||
8055 | } | |||
8056 | ||||
8057 | ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr( | |||
8058 | CXXDependentScopeMemberExpr *E) { | |||
8059 | Error Err = Error::success(); | |||
8060 | auto ToType = importChecked(Err, E->getType()); | |||
8061 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
8062 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | |||
8063 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); | |||
8064 | auto ToFirstQualifierFoundInScope = | |||
8065 | importChecked(Err, E->getFirstQualifierFoundInScope()); | |||
8066 | if (Err) | |||
8067 | return std::move(Err); | |||
8068 | ||||
8069 | Expr *ToBase = nullptr; | |||
8070 | if (!E->isImplicitAccess()) { | |||
8071 | if (ExpectedExpr ToBaseOrErr = import(E->getBase())) | |||
8072 | ToBase = *ToBaseOrErr; | |||
8073 | else | |||
8074 | return ToBaseOrErr.takeError(); | |||
8075 | } | |||
8076 | ||||
8077 | TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr; | |||
8078 | ||||
8079 | if (E->hasExplicitTemplateArgs()) { | |||
8080 | if (Error Err = | |||
8081 | ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(), | |||
8082 | E->template_arguments(), ToTAInfo)) | |||
8083 | return std::move(Err); | |||
8084 | ResInfo = &ToTAInfo; | |||
8085 | } | |||
8086 | auto ToMember = importChecked(Err, E->getMember()); | |||
8087 | auto ToMemberLoc = importChecked(Err, E->getMemberLoc()); | |||
8088 | if (Err) | |||
8089 | return std::move(Err); | |||
8090 | DeclarationNameInfo ToMemberNameInfo(ToMember, ToMemberLoc); | |||
8091 | ||||
8092 | // Import additional name location/type info. | |||
8093 | if (Error Err = | |||
8094 | ImportDeclarationNameLoc(E->getMemberNameInfo(), ToMemberNameInfo)) | |||
8095 | return std::move(Err); | |||
8096 | ||||
8097 | return CXXDependentScopeMemberExpr::Create( | |||
8098 | Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc, | |||
8099 | ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope, | |||
8100 | ToMemberNameInfo, ResInfo); | |||
8101 | } | |||
8102 | ||||
8103 | ExpectedStmt | |||
8104 | ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { | |||
8105 | Error Err = Error::success(); | |||
8106 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | |||
8107 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); | |||
8108 | auto ToDeclName = importChecked(Err, E->getDeclName()); | |||
8109 | auto ToNameLoc = importChecked(Err, E->getNameInfo().getLoc()); | |||
8110 | auto ToLAngleLoc = importChecked(Err, E->getLAngleLoc()); | |||
8111 | auto ToRAngleLoc = importChecked(Err, E->getRAngleLoc()); | |||
8112 | if (Err) | |||
8113 | return std::move(Err); | |||
8114 | ||||
8115 | DeclarationNameInfo ToNameInfo(ToDeclName, ToNameLoc); | |||
8116 | if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo)) | |||
8117 | return std::move(Err); | |||
8118 | ||||
8119 | TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc); | |||
8120 | TemplateArgumentListInfo *ResInfo = nullptr; | |||
8121 | if (E->hasExplicitTemplateArgs()) { | |||
8122 | if (Error Err = | |||
8123 | ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo)) | |||
8124 | return std::move(Err); | |||
8125 | ResInfo = &ToTAInfo; | |||
8126 | } | |||
8127 | ||||
8128 | return DependentScopeDeclRefExpr::Create( | |||
8129 | Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, | |||
8130 | ToNameInfo, ResInfo); | |||
8131 | } | |||
8132 | ||||
8133 | ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr( | |||
8134 | CXXUnresolvedConstructExpr *E) { | |||
8135 | Error Err = Error::success(); | |||
8136 | auto ToLParenLoc = importChecked(Err, E->getLParenLoc()); | |||
8137 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
8138 | auto ToType = importChecked(Err, E->getType()); | |||
8139 | auto ToTypeSourceInfo = importChecked(Err, E->getTypeSourceInfo()); | |||
8140 | if (Err) | |||
8141 | return std::move(Err); | |||
8142 | ||||
8143 | SmallVector<Expr *, 8> ToArgs(E->getNumArgs()); | |||
8144 | if (Error Err = | |||
8145 | ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin())) | |||
8146 | return std::move(Err); | |||
8147 | ||||
8148 | return CXXUnresolvedConstructExpr::Create( | |||
8149 | Importer.getToContext(), ToType, ToTypeSourceInfo, ToLParenLoc, | |||
8150 | llvm::ArrayRef(ToArgs), ToRParenLoc, E->isListInitialization()); | |||
8151 | } | |||
8152 | ||||
8153 | ExpectedStmt | |||
8154 | ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) { | |||
8155 | Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass()); | |||
8156 | if (!ToNamingClassOrErr) | |||
8157 | return ToNamingClassOrErr.takeError(); | |||
8158 | ||||
8159 | auto ToQualifierLocOrErr = import(E->getQualifierLoc()); | |||
8160 | if (!ToQualifierLocOrErr) | |||
8161 | return ToQualifierLocOrErr.takeError(); | |||
8162 | ||||
8163 | Error Err = Error::success(); | |||
8164 | auto ToName = importChecked(Err, E->getName()); | |||
8165 | auto ToNameLoc = importChecked(Err, E->getNameLoc()); | |||
8166 | if (Err) | |||
8167 | return std::move(Err); | |||
8168 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); | |||
8169 | ||||
8170 | // Import additional name location/type info. | |||
8171 | if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo)) | |||
8172 | return std::move(Err); | |||
8173 | ||||
8174 | UnresolvedSet<8> ToDecls; | |||
8175 | for (auto *D : E->decls()) | |||
8176 | if (auto ToDOrErr = import(D)) | |||
8177 | ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr)); | |||
8178 | else | |||
8179 | return ToDOrErr.takeError(); | |||
8180 | ||||
8181 | if (E->hasExplicitTemplateArgs()) { | |||
8182 | TemplateArgumentListInfo ToTAInfo; | |||
8183 | if (Error Err = ImportTemplateArgumentListInfo( | |||
8184 | E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(), | |||
8185 | ToTAInfo)) | |||
8186 | return std::move(Err); | |||
8187 | ||||
8188 | ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc()); | |||
8189 | if (!ToTemplateKeywordLocOrErr) | |||
8190 | return ToTemplateKeywordLocOrErr.takeError(); | |||
8191 | ||||
8192 | return UnresolvedLookupExpr::Create( | |||
8193 | Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr, | |||
8194 | *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo, | |||
8195 | ToDecls.begin(), ToDecls.end()); | |||
8196 | } | |||
8197 | ||||
8198 | return UnresolvedLookupExpr::Create( | |||
8199 | Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr, | |||
8200 | ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(), | |||
8201 | ToDecls.end()); | |||
8202 | } | |||
8203 | ||||
8204 | ExpectedStmt | |||
8205 | ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { | |||
8206 | Error Err = Error::success(); | |||
8207 | auto ToType = importChecked(Err, E->getType()); | |||
8208 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
8209 | auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); | |||
8210 | auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); | |||
8211 | auto ToName = importChecked(Err, E->getName()); | |||
8212 | auto ToNameLoc = importChecked(Err, E->getNameLoc()); | |||
8213 | if (Err) | |||
8214 | return std::move(Err); | |||
8215 | ||||
8216 | DeclarationNameInfo ToNameInfo(ToName, ToNameLoc); | |||
8217 | // Import additional name location/type info. | |||
8218 | if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo)) | |||
8219 | return std::move(Err); | |||
8220 | ||||
8221 | UnresolvedSet<8> ToDecls; | |||
8222 | for (Decl *D : E->decls()) | |||
8223 | if (auto ToDOrErr = import(D)) | |||
8224 | ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr)); | |||
8225 | else | |||
8226 | return ToDOrErr.takeError(); | |||
8227 | ||||
8228 | TemplateArgumentListInfo ToTAInfo; | |||
8229 | TemplateArgumentListInfo *ResInfo = nullptr; | |||
8230 | if (E->hasExplicitTemplateArgs()) { | |||
8231 | TemplateArgumentListInfo FromTAInfo; | |||
8232 | E->copyTemplateArgumentsInto(FromTAInfo); | |||
8233 | if (Error Err = ImportTemplateArgumentListInfo(FromTAInfo, ToTAInfo)) | |||
8234 | return std::move(Err); | |||
8235 | ResInfo = &ToTAInfo; | |||
8236 | } | |||
8237 | ||||
8238 | Expr *ToBase = nullptr; | |||
8239 | if (!E->isImplicitAccess()) { | |||
8240 | if (ExpectedExpr ToBaseOrErr = import(E->getBase())) | |||
8241 | ToBase = *ToBaseOrErr; | |||
8242 | else | |||
8243 | return ToBaseOrErr.takeError(); | |||
8244 | } | |||
8245 | ||||
8246 | return UnresolvedMemberExpr::Create( | |||
8247 | Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType, | |||
8248 | E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, | |||
8249 | ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end()); | |||
8250 | } | |||
8251 | ||||
8252 | ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) { | |||
8253 | Error Err = Error::success(); | |||
8254 | auto ToCallee = importChecked(Err, E->getCallee()); | |||
8255 | auto ToType = importChecked(Err, E->getType()); | |||
8256 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
8257 | if (Err) | |||
8258 | return std::move(Err); | |||
8259 | ||||
8260 | unsigned NumArgs = E->getNumArgs(); | |||
8261 | llvm::SmallVector<Expr *, 2> ToArgs(NumArgs); | |||
8262 | if (Error Err = ImportContainerChecked(E->arguments(), ToArgs)) | |||
8263 | return std::move(Err); | |||
8264 | ||||
8265 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) { | |||
8266 | return CXXOperatorCallExpr::Create( | |||
8267 | Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType, | |||
8268 | OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(), | |||
8269 | OCE->getADLCallKind()); | |||
8270 | } | |||
8271 | ||||
8272 | return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType, | |||
8273 | E->getValueKind(), ToRParenLoc, E->getFPFeatures(), | |||
8274 | /*MinNumArgs=*/0, E->getADLCallKind()); | |||
8275 | } | |||
8276 | ||||
8277 | ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) { | |||
8278 | CXXRecordDecl *FromClass = E->getLambdaClass(); | |||
8279 | auto ToClassOrErr = import(FromClass); | |||
8280 | if (!ToClassOrErr) | |||
8281 | return ToClassOrErr.takeError(); | |||
8282 | CXXRecordDecl *ToClass = *ToClassOrErr; | |||
8283 | ||||
8284 | auto ToCallOpOrErr = import(E->getCallOperator()); | |||
8285 | if (!ToCallOpOrErr) | |||
8286 | return ToCallOpOrErr.takeError(); | |||
8287 | ||||
8288 | SmallVector<Expr *, 8> ToCaptureInits(E->capture_size()); | |||
8289 | if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits)) | |||
8290 | return std::move(Err); | |||
8291 | ||||
8292 | Error Err = Error::success(); | |||
8293 | auto ToIntroducerRange = importChecked(Err, E->getIntroducerRange()); | |||
8294 | auto ToCaptureDefaultLoc = importChecked(Err, E->getCaptureDefaultLoc()); | |||
8295 | auto ToEndLoc = importChecked(Err, E->getEndLoc()); | |||
8296 | if (Err) | |||
8297 | return std::move(Err); | |||
8298 | ||||
8299 | return LambdaExpr::Create(Importer.getToContext(), ToClass, ToIntroducerRange, | |||
8300 | E->getCaptureDefault(), ToCaptureDefaultLoc, | |||
8301 | E->hasExplicitParameters(), | |||
8302 | E->hasExplicitResultType(), ToCaptureInits, | |||
8303 | ToEndLoc, E->containsUnexpandedParameterPack()); | |||
8304 | } | |||
8305 | ||||
8306 | ||||
8307 | ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) { | |||
8308 | Error Err = Error::success(); | |||
8309 | auto ToLBraceLoc = importChecked(Err, E->getLBraceLoc()); | |||
8310 | auto ToRBraceLoc = importChecked(Err, E->getRBraceLoc()); | |||
8311 | auto ToType = importChecked(Err, E->getType()); | |||
8312 | if (Err) | |||
8313 | return std::move(Err); | |||
8314 | ||||
8315 | SmallVector<Expr *, 4> ToExprs(E->getNumInits()); | |||
8316 | if (Error Err = ImportContainerChecked(E->inits(), ToExprs)) | |||
8317 | return std::move(Err); | |||
8318 | ||||
8319 | ASTContext &ToCtx = Importer.getToContext(); | |||
8320 | InitListExpr *To = new (ToCtx) InitListExpr( | |||
8321 | ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc); | |||
8322 | To->setType(ToType); | |||
8323 | ||||
8324 | if (E->hasArrayFiller()) { | |||
8325 | if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller())) | |||
8326 | To->setArrayFiller(*ToFillerOrErr); | |||
8327 | else | |||
8328 | return ToFillerOrErr.takeError(); | |||
8329 | } | |||
8330 | ||||
8331 | if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) { | |||
8332 | if (auto ToFDOrErr = import(FromFD)) | |||
8333 | To->setInitializedFieldInUnion(*ToFDOrErr); | |||
8334 | else | |||
8335 | return ToFDOrErr.takeError(); | |||
8336 | } | |||
8337 | ||||
8338 | if (InitListExpr *SyntForm = E->getSyntacticForm()) { | |||
8339 | if (auto ToSyntFormOrErr = import(SyntForm)) | |||
8340 | To->setSyntacticForm(*ToSyntFormOrErr); | |||
8341 | else | |||
8342 | return ToSyntFormOrErr.takeError(); | |||
8343 | } | |||
8344 | ||||
8345 | // Copy InitListExprBitfields, which are not handled in the ctor of | |||
8346 | // InitListExpr. | |||
8347 | To->sawArrayRangeDesignator(E->hadArrayRangeDesignator()); | |||
8348 | ||||
8349 | return To; | |||
8350 | } | |||
8351 | ||||
8352 | ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr( | |||
8353 | CXXStdInitializerListExpr *E) { | |||
8354 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
8355 | if (!ToTypeOrErr) | |||
8356 | return ToTypeOrErr.takeError(); | |||
8357 | ||||
8358 | ExpectedExpr ToSubExprOrErr = import(E->getSubExpr()); | |||
8359 | if (!ToSubExprOrErr) | |||
8360 | return ToSubExprOrErr.takeError(); | |||
8361 | ||||
8362 | return new (Importer.getToContext()) CXXStdInitializerListExpr( | |||
8363 | *ToTypeOrErr, *ToSubExprOrErr); | |||
8364 | } | |||
8365 | ||||
8366 | ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr( | |||
8367 | CXXInheritedCtorInitExpr *E) { | |||
8368 | Error Err = Error::success(); | |||
8369 | auto ToLocation = importChecked(Err, E->getLocation()); | |||
8370 | auto ToType = importChecked(Err, E->getType()); | |||
8371 | auto ToConstructor = importChecked(Err, E->getConstructor()); | |||
8372 | if (Err) | |||
8373 | return std::move(Err); | |||
8374 | ||||
8375 | return new (Importer.getToContext()) CXXInheritedCtorInitExpr( | |||
8376 | ToLocation, ToType, ToConstructor, E->constructsVBase(), | |||
8377 | E->inheritedFromVBase()); | |||
8378 | } | |||
8379 | ||||
8380 | ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) { | |||
8381 | Error Err = Error::success(); | |||
8382 | auto ToType = importChecked(Err, E->getType()); | |||
8383 | auto ToCommonExpr = importChecked(Err, E->getCommonExpr()); | |||
8384 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | |||
8385 | if (Err) | |||
8386 | return std::move(Err); | |||
8387 | ||||
8388 | return new (Importer.getToContext()) ArrayInitLoopExpr( | |||
8389 | ToType, ToCommonExpr, ToSubExpr); | |||
8390 | } | |||
8391 | ||||
8392 | ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) { | |||
8393 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
8394 | if (!ToTypeOrErr) | |||
8395 | return ToTypeOrErr.takeError(); | |||
8396 | return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr); | |||
8397 | } | |||
8398 | ||||
8399 | ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) { | |||
8400 | ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc()); | |||
8401 | if (!ToBeginLocOrErr) | |||
8402 | return ToBeginLocOrErr.takeError(); | |||
8403 | ||||
8404 | auto ToFieldOrErr = import(E->getField()); | |||
8405 | if (!ToFieldOrErr) | |||
8406 | return ToFieldOrErr.takeError(); | |||
8407 | ||||
8408 | auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext()); | |||
8409 | if (!UsedContextOrErr) | |||
8410 | return UsedContextOrErr.takeError(); | |||
8411 | ||||
8412 | FieldDecl *ToField = *ToFieldOrErr; | |||
8413 | assert(ToField->hasInClassInitializer() &&(static_cast <bool> (ToField->hasInClassInitializer( ) && "Field should have in-class initializer if there is a default init " "expression that uses it.") ? void (0) : __assert_fail ("ToField->hasInClassInitializer() && \"Field should have in-class initializer if there is a default init \" \"expression that uses it.\"" , "clang/lib/AST/ASTImporter.cpp", 8415, __extension__ __PRETTY_FUNCTION__ )) | |||
8414 | "Field should have in-class initializer if there is a default init "(static_cast <bool> (ToField->hasInClassInitializer( ) && "Field should have in-class initializer if there is a default init " "expression that uses it.") ? void (0) : __assert_fail ("ToField->hasInClassInitializer() && \"Field should have in-class initializer if there is a default init \" \"expression that uses it.\"" , "clang/lib/AST/ASTImporter.cpp", 8415, __extension__ __PRETTY_FUNCTION__ )) | |||
8415 | "expression that uses it.")(static_cast <bool> (ToField->hasInClassInitializer( ) && "Field should have in-class initializer if there is a default init " "expression that uses it.") ? void (0) : __assert_fail ("ToField->hasInClassInitializer() && \"Field should have in-class initializer if there is a default init \" \"expression that uses it.\"" , "clang/lib/AST/ASTImporter.cpp", 8415, __extension__ __PRETTY_FUNCTION__ )); | |||
8416 | if (!ToField->getInClassInitializer()) { | |||
8417 | // The in-class initializer may be not yet set in "To" AST even if the | |||
8418 | // field is already there. This must be set here to make construction of | |||
8419 | // CXXDefaultInitExpr work. | |||
8420 | auto ToInClassInitializerOrErr = | |||
8421 | import(E->getField()->getInClassInitializer()); | |||
8422 | if (!ToInClassInitializerOrErr) | |||
8423 | return ToInClassInitializerOrErr.takeError(); | |||
8424 | ToField->setInClassInitializer(*ToInClassInitializerOrErr); | |||
8425 | } | |||
8426 | ||||
8427 | Expr *RewrittenInit = nullptr; | |||
8428 | if (E->hasRewrittenInit()) { | |||
8429 | ExpectedExpr ExprOrErr = import(E->getRewrittenExpr()); | |||
8430 | if (!ExprOrErr) | |||
8431 | return ExprOrErr.takeError(); | |||
8432 | RewrittenInit = ExprOrErr.get(); | |||
8433 | } | |||
8434 | ||||
8435 | return CXXDefaultInitExpr::Create(Importer.getToContext(), *ToBeginLocOrErr, | |||
8436 | ToField, *UsedContextOrErr, RewrittenInit); | |||
8437 | } | |||
8438 | ||||
8439 | ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { | |||
8440 | Error Err = Error::success(); | |||
8441 | auto ToType = importChecked(Err, E->getType()); | |||
8442 | auto ToSubExpr = importChecked(Err, E->getSubExpr()); | |||
8443 | auto ToTypeInfoAsWritten = importChecked(Err, E->getTypeInfoAsWritten()); | |||
8444 | auto ToOperatorLoc = importChecked(Err, E->getOperatorLoc()); | |||
8445 | auto ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
8446 | auto ToAngleBrackets = importChecked(Err, E->getAngleBrackets()); | |||
8447 | if (Err) | |||
8448 | return std::move(Err); | |||
8449 | ||||
8450 | ExprValueKind VK = E->getValueKind(); | |||
8451 | CastKind CK = E->getCastKind(); | |||
8452 | auto ToBasePathOrErr = ImportCastPath(E); | |||
8453 | if (!ToBasePathOrErr) | |||
8454 | return ToBasePathOrErr.takeError(); | |||
8455 | ||||
8456 | if (auto CCE = dyn_cast<CXXStaticCastExpr>(E)) { | |||
8457 | return CXXStaticCastExpr::Create( | |||
8458 | Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr), | |||
8459 | ToTypeInfoAsWritten, CCE->getFPFeatures(), ToOperatorLoc, ToRParenLoc, | |||
8460 | ToAngleBrackets); | |||
8461 | } else if (isa<CXXDynamicCastExpr>(E)) { | |||
8462 | return CXXDynamicCastExpr::Create( | |||
8463 | Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr), | |||
8464 | ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); | |||
8465 | } else if (isa<CXXReinterpretCastExpr>(E)) { | |||
8466 | return CXXReinterpretCastExpr::Create( | |||
8467 | Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr), | |||
8468 | ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); | |||
8469 | } else if (isa<CXXConstCastExpr>(E)) { | |||
8470 | return CXXConstCastExpr::Create( | |||
8471 | Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten, | |||
8472 | ToOperatorLoc, ToRParenLoc, ToAngleBrackets); | |||
8473 | } else { | |||
8474 | llvm_unreachable("Unknown cast type")::llvm::llvm_unreachable_internal("Unknown cast type", "clang/lib/AST/ASTImporter.cpp" , 8474); | |||
8475 | return make_error<ASTImportError>(); | |||
8476 | } | |||
8477 | } | |||
8478 | ||||
8479 | ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr( | |||
8480 | SubstNonTypeTemplateParmExpr *E) { | |||
8481 | Error Err = Error::success(); | |||
8482 | auto ToType = importChecked(Err, E->getType()); | |||
8483 | auto ToExprLoc = importChecked(Err, E->getExprLoc()); | |||
8484 | auto ToAssociatedDecl = importChecked(Err, E->getAssociatedDecl()); | |||
8485 | auto ToReplacement = importChecked(Err, E->getReplacement()); | |||
8486 | if (Err) | |||
8487 | return std::move(Err); | |||
8488 | ||||
8489 | return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr( | |||
8490 | ToType, E->getValueKind(), ToExprLoc, ToReplacement, ToAssociatedDecl, | |||
8491 | E->getIndex(), E->getPackIndex(), E->isReferenceParameter()); | |||
8492 | } | |||
8493 | ||||
8494 | ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) { | |||
8495 | Error Err = Error::success(); | |||
8496 | auto ToType = importChecked(Err, E->getType()); | |||
8497 | auto ToBeginLoc = importChecked(Err, E->getBeginLoc()); | |||
8498 | auto ToEndLoc = importChecked(Err, E->getEndLoc()); | |||
8499 | if (Err) | |||
8500 | return std::move(Err); | |||
8501 | ||||
8502 | SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs()); | |||
8503 | if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs)) | |||
8504 | return std::move(Err); | |||
8505 | ||||
8506 | // According to Sema::BuildTypeTrait(), if E is value-dependent, | |||
8507 | // Value is always false. | |||
8508 | bool ToValue = (E->isValueDependent() ? false : E->getValue()); | |||
8509 | ||||
8510 | return TypeTraitExpr::Create( | |||
8511 | Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs, | |||
8512 | ToEndLoc, ToValue); | |||
8513 | } | |||
8514 | ||||
8515 | ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) { | |||
8516 | ExpectedType ToTypeOrErr = import(E->getType()); | |||
8517 | if (!ToTypeOrErr) | |||
8518 | return ToTypeOrErr.takeError(); | |||
8519 | ||||
8520 | auto ToSourceRangeOrErr = import(E->getSourceRange()); | |||
8521 | if (!ToSourceRangeOrErr) | |||
8522 | return ToSourceRangeOrErr.takeError(); | |||
8523 | ||||
8524 | if (E->isTypeOperand()) { | |||
8525 | if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo())) | |||
8526 | return new (Importer.getToContext()) CXXTypeidExpr( | |||
8527 | *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr); | |||
8528 | else | |||
8529 | return ToTSIOrErr.takeError(); | |||
8530 | } | |||
8531 | ||||
8532 | ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand()); | |||
8533 | if (!ToExprOperandOrErr) | |||
8534 | return ToExprOperandOrErr.takeError(); | |||
8535 | ||||
8536 | return new (Importer.getToContext()) CXXTypeidExpr( | |||
8537 | *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr); | |||
8538 | } | |||
8539 | ||||
8540 | ExpectedStmt ASTNodeImporter::VisitCXXFoldExpr(CXXFoldExpr *E) { | |||
8541 | Error Err = Error::success(); | |||
8542 | ||||
8543 | QualType ToType = importChecked(Err, E->getType()); | |||
8544 | UnresolvedLookupExpr *ToCallee = importChecked(Err, E->getCallee()); | |||
8545 | SourceLocation ToLParenLoc = importChecked(Err, E->getLParenLoc()); | |||
8546 | Expr *ToLHS = importChecked(Err, E->getLHS()); | |||
8547 | SourceLocation ToEllipsisLoc = importChecked(Err, E->getEllipsisLoc()); | |||
8548 | Expr *ToRHS = importChecked(Err, E->getRHS()); | |||
8549 | SourceLocation ToRParenLoc = importChecked(Err, E->getRParenLoc()); | |||
8550 | ||||
8551 | if (Err) | |||
8552 | return std::move(Err); | |||
8553 | ||||
8554 | return new (Importer.getToContext()) | |||
8555 | CXXFoldExpr(ToType, ToCallee, ToLParenLoc, ToLHS, E->getOperator(), | |||
8556 | ToEllipsisLoc, ToRHS, ToRParenLoc, E->getNumExpansions()); | |||
8557 | } | |||
8558 | ||||
8559 | Error ASTNodeImporter::ImportOverriddenMethods(CXXMethodDecl *ToMethod, | |||
8560 | CXXMethodDecl *FromMethod) { | |||
8561 | Error ImportErrors = Error::success(); | |||
8562 | for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) { | |||
8563 | if (auto ImportedOrErr = import(FromOverriddenMethod)) | |||
8564 | ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>( | |||
8565 | (*ImportedOrErr)->getCanonicalDecl())); | |||
8566 | else | |||
8567 | ImportErrors = | |||
8568 | joinErrors(std::move(ImportErrors), ImportedOrErr.takeError()); | |||
8569 | } | |||
8570 | return ImportErrors; | |||
8571 | } | |||
8572 | ||||
8573 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, | |||
8574 | ASTContext &FromContext, FileManager &FromFileManager, | |||
8575 | bool MinimalImport, | |||
8576 | std::shared_ptr<ASTImporterSharedState> SharedState) | |||
8577 | : SharedState(SharedState), ToContext(ToContext), FromContext(FromContext), | |||
8578 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), | |||
8579 | Minimal(MinimalImport), ODRHandling(ODRHandlingType::Conservative) { | |||
8580 | ||||
8581 | // Create a default state without the lookup table: LLDB case. | |||
8582 | if (!SharedState) { | |||
8583 | this->SharedState = std::make_shared<ASTImporterSharedState>(); | |||
8584 | } | |||
8585 | ||||
8586 | ImportedDecls[FromContext.getTranslationUnitDecl()] = | |||
8587 | ToContext.getTranslationUnitDecl(); | |||
8588 | } | |||
8589 | ||||
8590 | ASTImporter::~ASTImporter() = default; | |||
8591 | ||||
8592 | std::optional<unsigned> ASTImporter::getFieldIndex(Decl *F) { | |||
8593 | assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&(static_cast <bool> (F && (isa<FieldDecl> (*F) || isa<IndirectFieldDecl>(*F)) && "Try to get field index for non-field." ) ? void (0) : __assert_fail ("F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) && \"Try to get field index for non-field.\"" , "clang/lib/AST/ASTImporter.cpp", 8594, __extension__ __PRETTY_FUNCTION__ )) | |||
8594 | "Try to get field index for non-field.")(static_cast <bool> (F && (isa<FieldDecl> (*F) || isa<IndirectFieldDecl>(*F)) && "Try to get field index for non-field." ) ? void (0) : __assert_fail ("F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) && \"Try to get field index for non-field.\"" , "clang/lib/AST/ASTImporter.cpp", 8594, __extension__ __PRETTY_FUNCTION__ )); | |||
8595 | ||||
8596 | auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); | |||
8597 | if (!Owner) | |||
8598 | return std::nullopt; | |||
8599 | ||||
8600 | unsigned Index = 0; | |||
8601 | for (const auto *D : Owner->decls()) { | |||
8602 | if (D == F) | |||
8603 | return Index; | |||
8604 | ||||
8605 | if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) | |||
8606 | ++Index; | |||
8607 | } | |||
8608 | ||||
8609 | llvm_unreachable("Field was not found in its parent context.")::llvm::llvm_unreachable_internal("Field was not found in its parent context." , "clang/lib/AST/ASTImporter.cpp", 8609); | |||
8610 | ||||
8611 | return std::nullopt; | |||
8612 | } | |||
8613 | ||||
8614 | ASTImporter::FoundDeclsTy | |||
8615 | ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) { | |||
8616 | // We search in the redecl context because of transparent contexts. | |||
8617 | // E.g. a simple C language enum is a transparent context: | |||
8618 | // enum E { A, B }; | |||
8619 | // Now if we had a global variable in the TU | |||
8620 | // int A; | |||
8621 | // then the enum constant 'A' and the variable 'A' violates ODR. | |||
8622 | // We can diagnose this only if we search in the redecl context. | |||
8623 | DeclContext *ReDC = DC->getRedeclContext(); | |||
8624 | if (SharedState->getLookupTable()) { | |||
8625 | ASTImporterLookupTable::LookupResult LookupResult = | |||
8626 | SharedState->getLookupTable()->lookup(ReDC, Name); | |||
8627 | return FoundDeclsTy(LookupResult.begin(), LookupResult.end()); | |||
8628 | } else { | |||
8629 | DeclContext::lookup_result NoloadLookupResult = ReDC->noload_lookup(Name); | |||
8630 | FoundDeclsTy Result(NoloadLookupResult.begin(), NoloadLookupResult.end()); | |||
8631 | // We must search by the slow case of localUncachedLookup because that is | |||
8632 | // working even if there is no LookupPtr for the DC. We could use | |||
8633 | // DC::buildLookup() to create the LookupPtr, but that would load external | |||
8634 | // decls again, we must avoid that case. | |||
8635 | // Also, even if we had the LookupPtr, we must find Decls which are not | |||
8636 | // in the LookupPtr, so we need the slow case. | |||
8637 | // These cases are handled in ASTImporterLookupTable, but we cannot use | |||
8638 | // that with LLDB since that traverses through the AST which initiates the | |||
8639 | // load of external decls again via DC::decls(). And again, we must avoid | |||
8640 | // loading external decls during the import. | |||
8641 | if (Result.empty()) | |||
8642 | ReDC->localUncachedLookup(Name, Result); | |||
8643 | return Result; | |||
8644 | } | |||
8645 | } | |||
8646 | ||||
8647 | void ASTImporter::AddToLookupTable(Decl *ToD) { | |||
8648 | SharedState->addDeclToLookup(ToD); | |||
8649 | } | |||
8650 | ||||
8651 | Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) { | |||
8652 | // Import the decl using ASTNodeImporter. | |||
8653 | ASTNodeImporter Importer(*this); | |||
8654 | return Importer.Visit(FromD); | |||
8655 | } | |||
8656 | ||||
8657 | void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) { | |||
8658 | MapImported(FromD, ToD); | |||
8659 | } | |||
8660 | ||||
8661 | llvm::Expected<ExprWithCleanups::CleanupObject> | |||
8662 | ASTImporter::Import(ExprWithCleanups::CleanupObject From) { | |||
8663 | if (auto *CLE = From.dyn_cast<CompoundLiteralExpr *>()) { | |||
8664 | if (Expected<Expr *> R = Import(CLE)) | |||
8665 | return ExprWithCleanups::CleanupObject(cast<CompoundLiteralExpr>(*R)); | |||
8666 | } | |||
8667 | ||||
8668 | // FIXME: Handle BlockDecl when we implement importing BlockExpr in | |||
8669 | // ASTNodeImporter. | |||
8670 | return make_error<ASTImportError>(ASTImportError::UnsupportedConstruct); | |||
8671 | } | |||
8672 | ||||
8673 | ExpectedTypePtr ASTImporter::Import(const Type *FromT) { | |||
8674 | if (!FromT) | |||
8675 | return FromT; | |||
8676 | ||||
8677 | // Check whether we've already imported this type. | |||
8678 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = | |||
8679 | ImportedTypes.find(FromT); | |||
8680 | if (Pos != ImportedTypes.end()) | |||
8681 | return Pos->second; | |||
8682 | ||||
8683 | // Import the type. | |||
8684 | ASTNodeImporter Importer(*this); | |||
8685 | ExpectedType ToTOrErr = Importer.Visit(FromT); | |||
8686 | if (!ToTOrErr) | |||
8687 | return ToTOrErr.takeError(); | |||
8688 | ||||
8689 | // Record the imported type. | |||
8690 | ImportedTypes[FromT] = ToTOrErr->getTypePtr(); | |||
8691 | ||||
8692 | return ToTOrErr->getTypePtr(); | |||
8693 | } | |||
8694 | ||||
8695 | Expected<QualType> ASTImporter::Import(QualType FromT) { | |||
8696 | if (FromT.isNull()) | |||
8697 | return QualType{}; | |||
8698 | ||||
8699 | ExpectedTypePtr ToTyOrErr = Import(FromT.getTypePtr()); | |||
8700 | if (!ToTyOrErr) | |||
8701 | return ToTyOrErr.takeError(); | |||
8702 | ||||
8703 | return ToContext.getQualifiedType(*ToTyOrErr, FromT.getLocalQualifiers()); | |||
8704 | } | |||
8705 | ||||
8706 | Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) { | |||
8707 | if (!FromTSI) | |||
8708 | return FromTSI; | |||
8709 | ||||
8710 | // FIXME: For now we just create a "trivial" type source info based | |||
8711 | // on the type and a single location. Implement a real version of this. | |||
8712 | ExpectedType TOrErr = Import(FromTSI->getType()); | |||
8713 | if (!TOrErr) | |||
8714 | return TOrErr.takeError(); | |||
8715 | ExpectedSLoc BeginLocOrErr = Import(FromTSI->getTypeLoc().getBeginLoc()); | |||
8716 | if (!BeginLocOrErr) | |||
8717 | return BeginLocOrErr.takeError(); | |||
8718 | ||||
8719 | return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr); | |||
8720 | } | |||
8721 | ||||
8722 | namespace { | |||
8723 | // To use this object, it should be created before the new attribute is created, | |||
8724 | // and destructed after it is created. The construction already performs the | |||
8725 | // import of the data. | |||
8726 | template <typename T> struct AttrArgImporter { | |||
8727 | AttrArgImporter(const AttrArgImporter<T> &) = delete; | |||
8728 | AttrArgImporter(AttrArgImporter<T> &&) = default; | |||
8729 | AttrArgImporter<T> &operator=(const AttrArgImporter<T> &) = delete; | |||
8730 | AttrArgImporter<T> &operator=(AttrArgImporter<T> &&) = default; | |||
8731 | ||||
8732 | AttrArgImporter(ASTNodeImporter &I, Error &Err, const T &From) | |||
8733 | : To(I.importChecked(Err, From)) {} | |||
8734 | ||||
8735 | const T &value() { return To; } | |||
8736 | ||||
8737 | private: | |||
8738 | T To; | |||
8739 | }; | |||
8740 | ||||
8741 | // To use this object, it should be created before the new attribute is created, | |||
8742 | // and destructed after it is created. The construction already performs the | |||
8743 | // import of the data. The array data is accessible in a pointer form, this form | |||
8744 | // is used by the attribute classes. This object should be created once for the | |||
8745 | // array data to be imported (the array size is not imported, just copied). | |||
8746 | template <typename T> struct AttrArgArrayImporter { | |||
8747 | AttrArgArrayImporter(const AttrArgArrayImporter<T> &) = delete; | |||
8748 | AttrArgArrayImporter(AttrArgArrayImporter<T> &&) = default; | |||
8749 | AttrArgArrayImporter<T> &operator=(const AttrArgArrayImporter<T> &) = delete; | |||
8750 | AttrArgArrayImporter<T> &operator=(AttrArgArrayImporter<T> &&) = default; | |||
8751 | ||||
8752 | AttrArgArrayImporter(ASTNodeImporter &I, Error &Err, | |||
8753 | const llvm::iterator_range<T *> &From, | |||
8754 | unsigned ArraySize) { | |||
8755 | if (Err) | |||
8756 | return; | |||
8757 | To.reserve(ArraySize); | |||
8758 | Err = I.ImportContainerChecked(From, To); | |||
8759 | } | |||
8760 | ||||
8761 | T *value() { return To.data(); } | |||
8762 | ||||
8763 | private: | |||
8764 | llvm::SmallVector<T, 2> To; | |||
8765 | }; | |||
8766 | ||||
8767 | class AttrImporter { | |||
8768 | Error Err{Error::success()}; | |||
8769 | Attr *ToAttr = nullptr; | |||
8770 | ASTImporter &Importer; | |||
8771 | ASTNodeImporter NImporter; | |||
8772 | ||||
8773 | public: | |||
8774 | AttrImporter(ASTImporter &I) : Importer(I), NImporter(I) {} | |||
8775 | ||||
8776 | // Create an "importer" for an attribute parameter. | |||
8777 | // Result of the 'value()' of that object is to be passed to the function | |||
8778 | // 'importAttr', in the order that is expected by the attribute class. | |||
8779 | template <class T> AttrArgImporter<T> importArg(const T &From) { | |||
8780 | return AttrArgImporter<T>(NImporter, Err, From); | |||
8781 | } | |||
8782 | ||||
8783 | // Create an "importer" for an attribute parameter that has array type. | |||
8784 | // Result of the 'value()' of that object is to be passed to the function | |||
8785 | // 'importAttr', then the size of the array as next argument. | |||
8786 | template <typename T> | |||
8787 | AttrArgArrayImporter<T> importArrayArg(const llvm::iterator_range<T *> &From, | |||
8788 | unsigned ArraySize) { | |||
8789 | return AttrArgArrayImporter<T>(NImporter, Err, From, ArraySize); | |||
8790 | } | |||
8791 | ||||
8792 | // Create an attribute object with the specified arguments. | |||
8793 | // The 'FromAttr' is the original (not imported) attribute, the 'ImportedArg' | |||
8794 | // should be values that are passed to the 'Create' function of the attribute. | |||
8795 | // (The 'Create' with 'ASTContext' first and 'AttributeCommonInfo' last is | |||
8796 | // used here.) As much data is copied or imported from the old attribute | |||
8797 | // as possible. The passed arguments should be already imported. | |||
8798 | // If an import error happens, the internal error is set to it, and any | |||
8799 | // further import attempt is ignored. | |||
8800 | template <typename T, typename... Arg> | |||
8801 | void importAttr(const T *FromAttr, Arg &&...ImportedArg) { | |||
8802 | static_assert(std::is_base_of<Attr, T>::value, | |||
8803 | "T should be subclass of Attr."); | |||
8804 | assert(!ToAttr && "Use one AttrImporter to import one Attribute object.")(static_cast <bool> (!ToAttr && "Use one AttrImporter to import one Attribute object." ) ? void (0) : __assert_fail ("!ToAttr && \"Use one AttrImporter to import one Attribute object.\"" , "clang/lib/AST/ASTImporter.cpp", 8804, __extension__ __PRETTY_FUNCTION__ )); | |||
8805 | ||||
8806 | const IdentifierInfo *ToAttrName = Importer.Import(FromAttr->getAttrName()); | |||
8807 | const IdentifierInfo *ToScopeName = | |||
8808 | Importer.Import(FromAttr->getScopeName()); | |||
8809 | SourceRange ToAttrRange = | |||
8810 | NImporter.importChecked(Err, FromAttr->getRange()); | |||
8811 | SourceLocation ToScopeLoc = | |||
8812 | NImporter.importChecked(Err, FromAttr->getScopeLoc()); | |||
8813 | ||||
8814 | if (Err) | |||
8815 | return; | |||
8816 | ||||
8817 | AttributeCommonInfo ToI(ToAttrName, ToScopeName, ToAttrRange, ToScopeLoc, | |||
8818 | FromAttr->getParsedKind(), FromAttr->getForm()); | |||
8819 | // The "SemanticSpelling" is not needed to be passed to the constructor. | |||
8820 | // That value is recalculated from the SpellingListIndex if needed. | |||
8821 | ToAttr = T::Create(Importer.getToContext(), | |||
8822 | std::forward<Arg>(ImportedArg)..., ToI); | |||
8823 | ||||
8824 | ToAttr->setImplicit(FromAttr->isImplicit()); | |||
8825 | ToAttr->setPackExpansion(FromAttr->isPackExpansion()); | |||
8826 | if (auto *ToInheritableAttr = dyn_cast<InheritableAttr>(ToAttr)) | |||
8827 | ToInheritableAttr->setInherited(FromAttr->isInherited()); | |||
8828 | } | |||
8829 | ||||
8830 | // Create a clone of the 'FromAttr' and import its source range only. | |||
8831 | // This causes objects with invalid references to be created if the 'FromAttr' | |||
8832 | // contains other data that should be imported. | |||
8833 | void cloneAttr(const Attr *FromAttr) { | |||
8834 | assert(!ToAttr && "Use one AttrImporter to import one Attribute object.")(static_cast <bool> (!ToAttr && "Use one AttrImporter to import one Attribute object." ) ? void (0) : __assert_fail ("!ToAttr && \"Use one AttrImporter to import one Attribute object.\"" , "clang/lib/AST/ASTImporter.cpp", 8834, __extension__ __PRETTY_FUNCTION__ )); | |||
8835 | ||||
8836 | SourceRange ToRange = NImporter.importChecked(Err, FromAttr->getRange()); | |||
8837 | if (Err) | |||
8838 | return; | |||
8839 | ||||
8840 | ToAttr = FromAttr->clone(Importer.getToContext()); | |||
8841 | ToAttr->setRange(ToRange); | |||
8842 | } | |||
8843 | ||||
8844 | // Get the result of the previous import attempt (can be used only once). | |||
8845 | llvm::Expected<Attr *> getResult() && { | |||
8846 | if (Err) | |||
8847 | return std::move(Err); | |||
8848 | assert(ToAttr && "Attribute should be created.")(static_cast <bool> (ToAttr && "Attribute should be created." ) ? void (0) : __assert_fail ("ToAttr && \"Attribute should be created.\"" , "clang/lib/AST/ASTImporter.cpp", 8848, __extension__ __PRETTY_FUNCTION__ )); | |||
8849 | return ToAttr; | |||
8850 | } | |||
8851 | }; | |||
8852 | } // namespace | |||
8853 | ||||
8854 | Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) { | |||
8855 | AttrImporter AI(*this); | |||
8856 | ||||
8857 | // FIXME: Is there some kind of AttrVisitor to use here? | |||
8858 | switch (FromAttr->getKind()) { | |||
8859 | case attr::Aligned: { | |||
8860 | auto *From = cast<AlignedAttr>(FromAttr); | |||
8861 | if (From->isAlignmentExpr()) | |||
8862 | AI.importAttr(From, true, AI.importArg(From->getAlignmentExpr()).value()); | |||
8863 | else | |||
8864 | AI.importAttr(From, false, | |||
8865 | AI.importArg(From->getAlignmentType()).value()); | |||
8866 | break; | |||
8867 | } | |||
8868 | ||||
8869 | case attr::Format: { | |||
8870 | const auto *From = cast<FormatAttr>(FromAttr); | |||
8871 | AI.importAttr(From, Import(From->getType()), From->getFormatIdx(), | |||
8872 | From->getFirstArg()); | |||
8873 | break; | |||
8874 | } | |||
8875 | ||||
8876 | case attr::EnableIf: { | |||
8877 | const auto *From = cast<EnableIfAttr>(FromAttr); | |||
8878 | AI.importAttr(From, AI.importArg(From->getCond()).value(), | |||
8879 | From->getMessage()); | |||
8880 | break; | |||
8881 | } | |||
8882 | ||||
8883 | case attr::AssertCapability: { | |||
8884 | const auto *From = cast<AssertCapabilityAttr>(FromAttr); | |||
8885 | AI.importAttr(From, | |||
8886 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8887 | From->args_size()); | |||
8888 | break; | |||
8889 | } | |||
8890 | case attr::AcquireCapability: { | |||
8891 | const auto *From = cast<AcquireCapabilityAttr>(FromAttr); | |||
8892 | AI.importAttr(From, | |||
8893 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8894 | From->args_size()); | |||
8895 | break; | |||
8896 | } | |||
8897 | case attr::TryAcquireCapability: { | |||
8898 | const auto *From = cast<TryAcquireCapabilityAttr>(FromAttr); | |||
8899 | AI.importAttr(From, AI.importArg(From->getSuccessValue()).value(), | |||
8900 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8901 | From->args_size()); | |||
8902 | break; | |||
8903 | } | |||
8904 | case attr::ReleaseCapability: { | |||
8905 | const auto *From = cast<ReleaseCapabilityAttr>(FromAttr); | |||
8906 | AI.importAttr(From, | |||
8907 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8908 | From->args_size()); | |||
8909 | break; | |||
8910 | } | |||
8911 | case attr::RequiresCapability: { | |||
8912 | const auto *From = cast<RequiresCapabilityAttr>(FromAttr); | |||
8913 | AI.importAttr(From, | |||
8914 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8915 | From->args_size()); | |||
8916 | break; | |||
8917 | } | |||
8918 | case attr::GuardedBy: { | |||
8919 | const auto *From = cast<GuardedByAttr>(FromAttr); | |||
8920 | AI.importAttr(From, AI.importArg(From->getArg()).value()); | |||
8921 | break; | |||
8922 | } | |||
8923 | case attr::PtGuardedBy: { | |||
8924 | const auto *From = cast<PtGuardedByAttr>(FromAttr); | |||
8925 | AI.importAttr(From, AI.importArg(From->getArg()).value()); | |||
8926 | break; | |||
8927 | } | |||
8928 | case attr::AcquiredAfter: { | |||
8929 | const auto *From = cast<AcquiredAfterAttr>(FromAttr); | |||
8930 | AI.importAttr(From, | |||
8931 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8932 | From->args_size()); | |||
8933 | break; | |||
8934 | } | |||
8935 | case attr::AcquiredBefore: { | |||
8936 | const auto *From = cast<AcquiredBeforeAttr>(FromAttr); | |||
8937 | AI.importAttr(From, | |||
8938 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8939 | From->args_size()); | |||
8940 | break; | |||
8941 | } | |||
8942 | case attr::AssertExclusiveLock: { | |||
8943 | const auto *From = cast<AssertExclusiveLockAttr>(FromAttr); | |||
8944 | AI.importAttr(From, | |||
8945 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8946 | From->args_size()); | |||
8947 | break; | |||
8948 | } | |||
8949 | case attr::AssertSharedLock: { | |||
8950 | const auto *From = cast<AssertSharedLockAttr>(FromAttr); | |||
8951 | AI.importAttr(From, | |||
8952 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8953 | From->args_size()); | |||
8954 | break; | |||
8955 | } | |||
8956 | case attr::ExclusiveTrylockFunction: { | |||
8957 | const auto *From = cast<ExclusiveTrylockFunctionAttr>(FromAttr); | |||
8958 | AI.importAttr(From, AI.importArg(From->getSuccessValue()).value(), | |||
8959 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8960 | From->args_size()); | |||
8961 | break; | |||
8962 | } | |||
8963 | case attr::SharedTrylockFunction: { | |||
8964 | const auto *From = cast<SharedTrylockFunctionAttr>(FromAttr); | |||
8965 | AI.importAttr(From, AI.importArg(From->getSuccessValue()).value(), | |||
8966 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8967 | From->args_size()); | |||
8968 | break; | |||
8969 | } | |||
8970 | case attr::LockReturned: { | |||
8971 | const auto *From = cast<LockReturnedAttr>(FromAttr); | |||
8972 | AI.importAttr(From, AI.importArg(From->getArg()).value()); | |||
8973 | break; | |||
8974 | } | |||
8975 | case attr::LocksExcluded: { | |||
8976 | const auto *From = cast<LocksExcludedAttr>(FromAttr); | |||
8977 | AI.importAttr(From, | |||
8978 | AI.importArrayArg(From->args(), From->args_size()).value(), | |||
8979 | From->args_size()); | |||
8980 | break; | |||
8981 | } | |||
8982 | ||||
8983 | default: { | |||
8984 | // The default branch works for attributes that have no arguments to import. | |||
8985 | // FIXME: Handle every attribute type that has arguments of type to import | |||
8986 | // (most often Expr* or Decl* or type) in the switch above. | |||
8987 | AI.cloneAttr(FromAttr); | |||
8988 | break; | |||
8989 | } | |||
8990 | } | |||
8991 | ||||
8992 | return std::move(AI).getResult(); | |||
8993 | } | |||
8994 | ||||
8995 | Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const { | |||
8996 | auto Pos = ImportedDecls.find(FromD); | |||
8997 | if (Pos != ImportedDecls.end()) | |||
8998 | return Pos->second; | |||
8999 | else | |||
9000 | return nullptr; | |||
9001 | } | |||
9002 | ||||
9003 | TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) { | |||
9004 | auto FromDPos = ImportedFromDecls.find(ToD); | |||
9005 | if (FromDPos == ImportedFromDecls.end()) | |||
9006 | return nullptr; | |||
9007 | return FromDPos->second->getTranslationUnitDecl(); | |||
9008 | } | |||
9009 | ||||
9010 | Error ASTImporter::ImportAttrs(Decl *ToD, Decl *FromD) { | |||
9011 | if (!FromD->hasAttrs() || ToD->hasAttrs()) | |||
9012 | return Error::success(); | |||
9013 | for (const Attr *FromAttr : FromD->getAttrs()) { | |||
9014 | auto ToAttrOrErr = Import(FromAttr); | |||
9015 | if (ToAttrOrErr) | |||
9016 | ToD->addAttr(*ToAttrOrErr); | |||
9017 | else | |||
9018 | return ToAttrOrErr.takeError(); | |||
9019 | } | |||
9020 | return Error::success(); | |||
9021 | } | |||
9022 | ||||
9023 | Expected<Decl *> ASTImporter::Import(Decl *FromD) { | |||
9024 | if (!FromD) | |||
9025 | return nullptr; | |||
9026 | ||||
9027 | // Push FromD to the stack, and remove that when we return. | |||
9028 | ImportPath.push(FromD); | |||
9029 | auto ImportPathBuilder = | |||
9030 | llvm::make_scope_exit([this]() { ImportPath.pop(); }); | |||
9031 | ||||
9032 | // Check whether there was a previous failed import. | |||
9033 | // If yes return the existing error. | |||
9034 | if (auto Error = getImportDeclErrorIfAny(FromD)) | |||
9035 | return make_error<ASTImportError>(*Error); | |||
9036 | ||||
9037 | // Check whether we've already imported this declaration. | |||
9038 | Decl *ToD = GetAlreadyImportedOrNull(FromD); | |||
9039 | if (ToD) { | |||
9040 | // Already imported (possibly from another TU) and with an error. | |||
9041 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { | |||
9042 | setImportDeclError(FromD, *Error); | |||
9043 | return make_error<ASTImportError>(*Error); | |||
9044 | } | |||
9045 | ||||
9046 | // If FromD has some updated flags after last import, apply it. | |||
9047 | updateFlags(FromD, ToD); | |||
9048 | // If we encounter a cycle during an import then we save the relevant part | |||
9049 | // of the import path associated to the Decl. | |||
9050 | if (ImportPath.hasCycleAtBack()) | |||
9051 | SavedImportPaths[FromD].push_back(ImportPath.copyCycleAtBack()); | |||
9052 | return ToD; | |||
9053 | } | |||
9054 | ||||
9055 | // Import the declaration. | |||
9056 | ExpectedDecl ToDOrErr = ImportImpl(FromD); | |||
9057 | if (!ToDOrErr) { | |||
9058 | // Failed to import. | |||
9059 | ||||
9060 | auto Pos = ImportedDecls.find(FromD); | |||
9061 | if (Pos != ImportedDecls.end()) { | |||
9062 | // Import failed after the object was created. | |||
9063 | // Remove all references to it. | |||
9064 | auto *ToD = Pos->second; | |||
9065 | ImportedDecls.erase(Pos); | |||
9066 | ||||
9067 | // ImportedDecls and ImportedFromDecls are not symmetric. It may happen | |||
9068 | // (e.g. with namespaces) that several decls from the 'from' context are | |||
9069 | // mapped to the same decl in the 'to' context. If we removed entries | |||
9070 | // from the LookupTable here then we may end up removing them multiple | |||
9071 | // times. | |||
9072 | ||||
9073 | // The Lookuptable contains decls only which are in the 'to' context. | |||
9074 | // Remove from the Lookuptable only if it is *imported* into the 'to' | |||
9075 | // context (and do not remove it if it was added during the initial | |||
9076 | // traverse of the 'to' context). | |||
9077 | auto PosF = ImportedFromDecls.find(ToD); | |||
9078 | if (PosF != ImportedFromDecls.end()) { | |||
9079 | // In the case of TypedefNameDecl we create the Decl first and only | |||
9080 | // then we import and set its DeclContext. So, the DC might not be set | |||
9081 | // when we reach here. | |||
9082 | if (ToD->getDeclContext()) | |||
9083 | SharedState->removeDeclFromLookup(ToD); | |||
9084 | ImportedFromDecls.erase(PosF); | |||
9085 | } | |||
9086 | ||||
9087 | // FIXME: AST may contain remaining references to the failed object. | |||
9088 | // However, the ImportDeclErrors in the shared state contains all the | |||
9089 | // failed objects together with their error. | |||
9090 | } | |||
9091 | ||||
9092 | // Error encountered for the first time. | |||
9093 | // After takeError the error is not usable any more in ToDOrErr. | |||
9094 | // Get a copy of the error object (any more simple solution for this?). | |||
9095 | ASTImportError ErrOut; | |||
9096 | handleAllErrors(ToDOrErr.takeError(), | |||
9097 | [&ErrOut](const ASTImportError &E) { ErrOut = E; }); | |||
9098 | setImportDeclError(FromD, ErrOut); | |||
9099 | // Set the error for the mapped to Decl, which is in the "to" context. | |||
9100 | if (Pos != ImportedDecls.end()) | |||
9101 | SharedState->setImportDeclError(Pos->second, ErrOut); | |||
9102 | ||||
9103 | // Set the error for all nodes which have been created before we | |||
9104 | // recognized the error. | |||
9105 | for (const auto &Path : SavedImportPaths[FromD]) { | |||
9106 | // The import path contains import-dependency nodes first. | |||
9107 | // Save the node that was imported as dependency of the current node. | |||
9108 | Decl *PrevFromDi = FromD; | |||
9109 | for (Decl *FromDi : Path) { | |||
9110 | // Begin and end of the path equals 'FromD', skip it. | |||
9111 | if (FromDi == FromD) | |||
9112 | continue; | |||
9113 | // We should not set import error on a node and all following nodes in | |||
9114 | // the path if child import errors are ignored. | |||
9115 | if (ChildErrorHandlingStrategy(FromDi).ignoreChildErrorOnParent( | |||
9116 | PrevFromDi)) | |||
9117 | break; | |||
9118 | PrevFromDi = FromDi; | |||
9119 | setImportDeclError(FromDi, ErrOut); | |||
9120 | //FIXME Should we remove these Decls from ImportedDecls? | |||
9121 | // Set the error for the mapped to Decl, which is in the "to" context. | |||
9122 | auto Ii = ImportedDecls.find(FromDi); | |||
9123 | if (Ii != ImportedDecls.end()) | |||
9124 | SharedState->setImportDeclError(Ii->second, ErrOut); | |||
9125 | // FIXME Should we remove these Decls from the LookupTable, | |||
9126 | // and from ImportedFromDecls? | |||
9127 | } | |||
9128 | } | |||
9129 | SavedImportPaths.erase(FromD); | |||
9130 | ||||
9131 | // Do not return ToDOrErr, error was taken out of it. | |||
9132 | return make_error<ASTImportError>(ErrOut); | |||
9133 | } | |||
9134 | ||||
9135 | ToD = *ToDOrErr; | |||
9136 | ||||
9137 | // FIXME: Handle the "already imported with error" case. We can get here | |||
9138 | // nullptr only if GetImportedOrCreateDecl returned nullptr (after a | |||
9139 | // previously failed create was requested). | |||
9140 | // Later GetImportedOrCreateDecl can be updated to return the error. | |||
9141 | if (!ToD) { | |||
9142 | auto Err = getImportDeclErrorIfAny(FromD); | |||
9143 | assert(Err)(static_cast <bool> (Err) ? void (0) : __assert_fail ("Err" , "clang/lib/AST/ASTImporter.cpp", 9143, __extension__ __PRETTY_FUNCTION__ )); | |||
9144 | return make_error<ASTImportError>(*Err); | |||
9145 | } | |||
9146 | ||||
9147 | // We could import from the current TU without error. But previously we | |||
9148 | // already had imported a Decl as `ToD` from another TU (with another | |||
9149 | // ASTImporter object) and with an error. | |||
9150 | if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) { | |||
9151 | setImportDeclError(FromD, *Error); | |||
9152 | return make_error<ASTImportError>(*Error); | |||
9153 | } | |||
9154 | ||||
9155 | // Make sure that ImportImpl registered the imported decl. | |||
9156 | assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?")(static_cast <bool> (ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?") ? void (0) : __assert_fail ( "ImportedDecls.count(FromD) != 0 && \"Missing call to MapImported?\"" , "clang/lib/AST/ASTImporter.cpp", 9156, __extension__ __PRETTY_FUNCTION__ )); | |||
9157 | if (auto Error = ImportAttrs(ToD, FromD)) | |||
9158 | return std::move(Error); | |||
9159 | ||||
9160 | // Notify subclasses. | |||
9161 | Imported(FromD, ToD); | |||
9162 | ||||
9163 | updateFlags(FromD, ToD); | |||
9164 | SavedImportPaths.erase(FromD); | |||
9165 | return ToDOrErr; | |||
9166 | } | |||
9167 | ||||
9168 | llvm::Expected<InheritedConstructor> | |||
9169 | ASTImporter::Import(const InheritedConstructor &From) { | |||
9170 | return ASTNodeImporter(*this).ImportInheritedConstructor(From); | |||
9171 | } | |||
9172 | ||||
9173 | Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) { | |||
9174 | if (!FromDC) | |||
9175 | return FromDC; | |||
9176 | ||||
9177 | ExpectedDecl ToDCOrErr = Import(cast<Decl>(FromDC)); | |||
9178 | if (!ToDCOrErr) | |||
9179 | return ToDCOrErr.takeError(); | |||
9180 | auto *ToDC = cast<DeclContext>(*ToDCOrErr); | |||
9181 | ||||
9182 | // When we're using a record/enum/Objective-C class/protocol as a context, we | |||
9183 | // need it to have a definition. | |||
9184 | if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) { | |||
9185 | auto *FromRecord = cast<RecordDecl>(FromDC); | |||
9186 | if (ToRecord->isCompleteDefinition()) | |||
9187 | return ToDC; | |||
9188 | ||||
9189 | // If FromRecord is not defined we need to force it to be. | |||
9190 | // Simply calling CompleteDecl(...) for a RecordDecl will break some cases | |||
9191 | // it will start the definition but we never finish it. | |||
9192 | // If there are base classes they won't be imported and we will | |||
9193 | // be missing anything that we inherit from those bases. | |||
9194 | if (FromRecord->getASTContext().getExternalSource() && | |||
9195 | !FromRecord->isCompleteDefinition()) | |||
9196 | FromRecord->getASTContext().getExternalSource()->CompleteType(FromRecord); | |||
9197 | ||||
9198 | if (FromRecord->isCompleteDefinition()) | |||
9199 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( | |||
9200 | FromRecord, ToRecord, ASTNodeImporter::IDK_Basic)) | |||
9201 | return std::move(Err); | |||
9202 | } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) { | |||
9203 | auto *FromEnum = cast<EnumDecl>(FromDC); | |||
9204 | if (ToEnum->isCompleteDefinition()) { | |||
9205 | // Do nothing. | |||
9206 | } else if (FromEnum->isCompleteDefinition()) { | |||
9207 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( | |||
9208 | FromEnum, ToEnum, ASTNodeImporter::IDK_Basic)) | |||
9209 | return std::move(Err); | |||
9210 | } else { | |||
9211 | CompleteDecl(ToEnum); | |||
9212 | } | |||
9213 | } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { | |||
9214 | auto *FromClass = cast<ObjCInterfaceDecl>(FromDC); | |||
9215 | if (ToClass->getDefinition()) { | |||
9216 | // Do nothing. | |||
9217 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { | |||
9218 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( | |||
9219 | FromDef, ToClass, ASTNodeImporter::IDK_Basic)) | |||
9220 | return std::move(Err); | |||
9221 | } else { | |||
9222 | CompleteDecl(ToClass); | |||
9223 | } | |||
9224 | } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { | |||
9225 | auto *FromProto = cast<ObjCProtocolDecl>(FromDC); | |||
9226 | if (ToProto->getDefinition()) { | |||
9227 | // Do nothing. | |||
9228 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { | |||
9229 | if (Error Err = ASTNodeImporter(*this).ImportDefinition( | |||
9230 | FromDef, ToProto, ASTNodeImporter::IDK_Basic)) | |||
9231 | return std::move(Err); | |||
9232 | } else { | |||
9233 | CompleteDecl(ToProto); | |||
9234 | } | |||
9235 | } | |||
9236 | ||||
9237 | return ToDC; | |||
9238 | } | |||
9239 | ||||
9240 | Expected<Expr *> ASTImporter::Import(Expr *FromE) { | |||
9241 | if (ExpectedStmt ToSOrErr = Import(cast_or_null<Stmt>(FromE))) | |||
9242 | return cast_or_null<Expr>(*ToSOrErr); | |||
9243 | else | |||
9244 | return ToSOrErr.takeError(); | |||
9245 | } | |||
9246 | ||||
9247 | Expected<Stmt *> ASTImporter::Import(Stmt *FromS) { | |||
9248 | if (!FromS) | |||
9249 | return nullptr; | |||
9250 | ||||
9251 | // Check whether we've already imported this statement. | |||
9252 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); | |||
9253 | if (Pos != ImportedStmts.end()) | |||
9254 | return Pos->second; | |||
9255 | ||||
9256 | // Import the statement. | |||
9257 | ASTNodeImporter Importer(*this); | |||
9258 | ExpectedStmt ToSOrErr = Importer.Visit(FromS); | |||
9259 | if (!ToSOrErr) | |||
9260 | return ToSOrErr; | |||
9261 | ||||
9262 | if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) { | |||
9263 | auto *FromE = cast<Expr>(FromS); | |||
9264 | // Copy ExprBitfields, which may not be handled in Expr subclasses | |||
9265 | // constructors. | |||
9266 | ToE->setValueKind(FromE->getValueKind()); | |||
9267 | ToE->setObjectKind(FromE->getObjectKind()); | |||
9268 | ToE->setDependence(FromE->getDependence()); | |||
9269 | } | |||
9270 | ||||
9271 | // Record the imported statement object. | |||
9272 | ImportedStmts[FromS] = *ToSOrErr; | |||
9273 | return ToSOrErr; | |||
9274 | } | |||
9275 | ||||
9276 | Expected<NestedNameSpecifier *> | |||
9277 | ASTImporter::Import(NestedNameSpecifier *FromNNS) { | |||
9278 | if (!FromNNS) | |||
9279 | return nullptr; | |||
9280 | ||||
9281 | NestedNameSpecifier *Prefix = nullptr; | |||
9282 | if (Error Err = importInto(Prefix, FromNNS->getPrefix())) | |||
9283 | return std::move(Err); | |||
9284 | ||||
9285 | switch (FromNNS->getKind()) { | |||
9286 | case NestedNameSpecifier::Identifier: | |||
9287 | assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.")(static_cast <bool> (FromNNS->getAsIdentifier() && "NNS should contain identifier.") ? void (0) : __assert_fail ("FromNNS->getAsIdentifier() && \"NNS should contain identifier.\"" , "clang/lib/AST/ASTImporter.cpp", 9287, __extension__ __PRETTY_FUNCTION__ )); | |||
9288 | return NestedNameSpecifier::Create(ToContext, Prefix, | |||
9289 | Import(FromNNS->getAsIdentifier())); | |||
9290 | ||||
9291 | case NestedNameSpecifier::Namespace: | |||
9292 | if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) { | |||
9293 | return NestedNameSpecifier::Create(ToContext, Prefix, | |||
9294 | cast<NamespaceDecl>(*NSOrErr)); | |||
9295 | } else | |||
9296 | return NSOrErr.takeError(); | |||
9297 | ||||
9298 | case NestedNameSpecifier::NamespaceAlias: | |||
9299 | if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias())) | |||
9300 | return NestedNameSpecifier::Create(ToContext, Prefix, | |||
9301 | cast<NamespaceAliasDecl>(*NSADOrErr)); | |||
9302 | else | |||
9303 | return NSADOrErr.takeError(); | |||
9304 | ||||
9305 | case NestedNameSpecifier::Global: | |||
9306 | return NestedNameSpecifier::GlobalSpecifier(ToContext); | |||
9307 | ||||
9308 | case NestedNameSpecifier::Super: | |||
9309 | if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl())) | |||
9310 | return NestedNameSpecifier::SuperSpecifier(ToContext, | |||
9311 | cast<CXXRecordDecl>(*RDOrErr)); | |||
9312 | else | |||
9313 | return RDOrErr.takeError(); | |||
9314 | ||||
9315 | case NestedNameSpecifier::TypeSpec: | |||
9316 | case NestedNameSpecifier::TypeSpecWithTemplate: | |||
9317 | if (ExpectedTypePtr TyOrErr = Import(FromNNS->getAsType())) { | |||
9318 | bool TSTemplate = | |||
9319 | FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate; | |||
9320 | return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate, | |||
9321 | *TyOrErr); | |||
9322 | } else { | |||
9323 | return TyOrErr.takeError(); | |||
9324 | } | |||
9325 | } | |||
9326 | ||||
9327 | llvm_unreachable("Invalid nested name specifier kind")::llvm::llvm_unreachable_internal("Invalid nested name specifier kind" , "clang/lib/AST/ASTImporter.cpp", 9327); | |||
9328 | } | |||
9329 | ||||
9330 | Expected<NestedNameSpecifierLoc> | |||
9331 | ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { | |||
9332 | // Copied from NestedNameSpecifier mostly. | |||
9333 | SmallVector<NestedNameSpecifierLoc , 8> NestedNames; | |||
9334 | NestedNameSpecifierLoc NNS = FromNNS; | |||
9335 | ||||
9336 | // Push each of the nested-name-specifiers's onto a stack for | |||
9337 | // serialization in reverse order. | |||
9338 | while (NNS) { | |||
9339 | NestedNames.push_back(NNS); | |||
9340 | NNS = NNS.getPrefix(); | |||
9341 | } | |||
9342 | ||||
9343 | NestedNameSpecifierLocBuilder Builder; | |||
9344 | ||||
9345 | while (!NestedNames.empty()) { | |||
9346 | NNS = NestedNames.pop_back_val(); | |||
9347 | NestedNameSpecifier *Spec = nullptr; | |||
9348 | if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier())) | |||
9349 | return std::move(Err); | |||
9350 | ||||
9351 | NestedNameSpecifier::SpecifierKind Kind = Spec->getKind(); | |||
9352 | ||||
9353 | SourceLocation ToLocalBeginLoc, ToLocalEndLoc; | |||
9354 | if (Kind != NestedNameSpecifier::Super) { | |||
9355 | if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc())) | |||
9356 | return std::move(Err); | |||
9357 | ||||
9358 | if (Kind != NestedNameSpecifier::Global) | |||
9359 | if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc())) | |||
9360 | return std::move(Err); | |||
9361 | } | |||
9362 | ||||
9363 | switch (Kind) { | |||
9364 | case NestedNameSpecifier::Identifier: | |||
9365 | Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc, | |||
9366 | ToLocalEndLoc); | |||
9367 | break; | |||
9368 | ||||
9369 | case NestedNameSpecifier::Namespace: | |||
9370 | Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc, | |||
9371 | ToLocalEndLoc); | |||
9372 | break; | |||
9373 | ||||
9374 | case NestedNameSpecifier::NamespaceAlias: | |||
9375 | Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(), | |||
9376 | ToLocalBeginLoc, ToLocalEndLoc); | |||
9377 | break; | |||
9378 | ||||
9379 | case NestedNameSpecifier::TypeSpec: | |||
9380 | case NestedNameSpecifier::TypeSpecWithTemplate: { | |||
9381 | SourceLocation ToTLoc; | |||
9382 | if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc())) | |||
9383 | return std::move(Err); | |||
9384 | TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( | |||
9385 | QualType(Spec->getAsType(), 0), ToTLoc); | |||
9386 | if (Kind == NestedNameSpecifier::TypeSpecWithTemplate) | |||
9387 | // ToLocalBeginLoc is here the location of the 'template' keyword. | |||
9388 | Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(), | |||
9389 | ToLocalEndLoc); | |||
9390 | else | |||
9391 | // No location for 'template' keyword here. | |||
9392 | Builder.Extend(getToContext(), SourceLocation{}, TSI->getTypeLoc(), | |||
9393 | ToLocalEndLoc); | |||
9394 | break; | |||
9395 | } | |||
9396 | ||||
9397 | case NestedNameSpecifier::Global: | |||
9398 | Builder.MakeGlobal(getToContext(), ToLocalBeginLoc); | |||
9399 | break; | |||
9400 | ||||
9401 | case NestedNameSpecifier::Super: { | |||
9402 | auto ToSourceRangeOrErr = Import(NNS.getSourceRange()); | |||
9403 | if (!ToSourceRangeOrErr) | |||
9404 | return ToSourceRangeOrErr.takeError(); | |||
9405 | ||||
9406 | Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(), | |||
9407 | ToSourceRangeOrErr->getBegin(), | |||
9408 | ToSourceRangeOrErr->getEnd()); | |||
9409 | } | |||
9410 | } | |||
9411 | } | |||
9412 | ||||
9413 | return Builder.getWithLocInContext(getToContext()); | |||
9414 | } | |||
9415 | ||||
9416 | Expected<TemplateName> ASTImporter::Import(TemplateName From) { | |||
9417 | switch (From.getKind()) { | |||
9418 | case TemplateName::Template: | |||
9419 | if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl())) | |||
9420 | return TemplateName(cast<TemplateDecl>((*ToTemplateOrErr)->getCanonicalDecl())); | |||
9421 | else | |||
9422 | return ToTemplateOrErr.takeError(); | |||
9423 | ||||
9424 | case TemplateName::OverloadedTemplate: { | |||
9425 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); | |||
9426 | UnresolvedSet<2> ToTemplates; | |||
9427 | for (auto *I : *FromStorage) { | |||
9428 | if (auto ToOrErr = Import(I)) | |||
9429 | ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr)); | |||
9430 | else | |||
9431 | return ToOrErr.takeError(); | |||
9432 | } | |||
9433 | return ToContext.getOverloadedTemplateName(ToTemplates.begin(), | |||
9434 | ToTemplates.end()); | |||
9435 | } | |||
9436 | ||||
9437 | case TemplateName::AssumedTemplate: { | |||
9438 | AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName(); | |||
9439 | auto DeclNameOrErr = Import(FromStorage->getDeclName()); | |||
9440 | if (!DeclNameOrErr) | |||
9441 | return DeclNameOrErr.takeError(); | |||
9442 | return ToContext.getAssumedTemplateName(*DeclNameOrErr); | |||
9443 | } | |||
9444 | ||||
9445 | case TemplateName::QualifiedTemplate: { | |||
9446 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); | |||
9447 | auto QualifierOrErr = Import(QTN->getQualifier()); | |||
9448 | if (!QualifierOrErr) | |||
9449 | return QualifierOrErr.takeError(); | |||
9450 | auto TNOrErr = Import(QTN->getUnderlyingTemplate()); | |||
9451 | if (!TNOrErr) | |||
9452 | return TNOrErr.takeError(); | |||
9453 | return ToContext.getQualifiedTemplateName( | |||
9454 | *QualifierOrErr, QTN->hasTemplateKeyword(), *TNOrErr); | |||
9455 | } | |||
9456 | ||||
9457 | case TemplateName::DependentTemplate: { | |||
9458 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); | |||
9459 | auto QualifierOrErr = Import(DTN->getQualifier()); | |||
9460 | if (!QualifierOrErr) | |||
9461 | return QualifierOrErr.takeError(); | |||
9462 | ||||
9463 | if (DTN->isIdentifier()) { | |||
9464 | return ToContext.getDependentTemplateName(*QualifierOrErr, | |||
9465 | Import(DTN->getIdentifier())); | |||
9466 | } | |||
9467 | ||||
9468 | return ToContext.getDependentTemplateName(*QualifierOrErr, | |||
9469 | DTN->getOperator()); | |||
9470 | } | |||
9471 | ||||
9472 | case TemplateName::SubstTemplateTemplateParm: { | |||
9473 | SubstTemplateTemplateParmStorage *Subst = | |||
9474 | From.getAsSubstTemplateTemplateParm(); | |||
9475 | auto ReplacementOrErr = Import(Subst->getReplacement()); | |||
9476 | if (!ReplacementOrErr) | |||
9477 | return ReplacementOrErr.takeError(); | |||
9478 | ||||
9479 | auto AssociatedDeclOrErr = Import(Subst->getAssociatedDecl()); | |||
9480 | if (!AssociatedDeclOrErr) | |||
9481 | return AssociatedDeclOrErr.takeError(); | |||
9482 | ||||
9483 | return ToContext.getSubstTemplateTemplateParm( | |||
9484 | *ReplacementOrErr, *AssociatedDeclOrErr, Subst->getIndex(), | |||
9485 | Subst->getPackIndex()); | |||
9486 | } | |||
9487 | ||||
9488 | case TemplateName::SubstTemplateTemplateParmPack: { | |||
9489 | SubstTemplateTemplateParmPackStorage *SubstPack = | |||
9490 | From.getAsSubstTemplateTemplateParmPack(); | |||
9491 | ASTNodeImporter Importer(*this); | |||
9492 | auto ArgPackOrErr = | |||
9493 | Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); | |||
9494 | if (!ArgPackOrErr) | |||
9495 | return ArgPackOrErr.takeError(); | |||
9496 | ||||
9497 | auto AssociatedDeclOrErr = Import(SubstPack->getAssociatedDecl()); | |||
9498 | if (!AssociatedDeclOrErr) | |||
9499 | return AssociatedDeclOrErr.takeError(); | |||
9500 | ||||
9501 | return ToContext.getSubstTemplateTemplateParmPack( | |||
9502 | *ArgPackOrErr, *AssociatedDeclOrErr, SubstPack->getIndex(), | |||
9503 | SubstPack->getFinal()); | |||
9504 | } | |||
9505 | case TemplateName::UsingTemplate: { | |||
9506 | auto UsingOrError = Import(From.getAsUsingShadowDecl()); | |||
9507 | if (!UsingOrError) | |||
9508 | return UsingOrError.takeError(); | |||
9509 | return TemplateName(cast<UsingShadowDecl>(*UsingOrError)); | |||
9510 | } | |||
9511 | } | |||
9512 | ||||
9513 | llvm_unreachable("Invalid template name kind")::llvm::llvm_unreachable_internal("Invalid template name kind" , "clang/lib/AST/ASTImporter.cpp", 9513); | |||
9514 | } | |||
9515 | ||||
9516 | Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) { | |||
9517 | if (FromLoc.isInvalid()) | |||
9518 | return SourceLocation{}; | |||
9519 | ||||
9520 | SourceManager &FromSM = FromContext.getSourceManager(); | |||
9521 | bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc); | |||
9522 | ||||
9523 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); | |||
9524 | Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin); | |||
9525 | if (!ToFileIDOrErr) | |||
9526 | return ToFileIDOrErr.takeError(); | |||
9527 | SourceManager &ToSM = ToContext.getSourceManager(); | |||
9528 | return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second); | |||
9529 | } | |||
9530 | ||||
9531 | Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) { | |||
9532 | SourceLocation ToBegin, ToEnd; | |||
9533 | if (Error Err = importInto(ToBegin, FromRange.getBegin())) | |||
9534 | return std::move(Err); | |||
9535 | if (Error Err = importInto(ToEnd, FromRange.getEnd())) | |||
9536 | return std::move(Err); | |||
9537 | ||||
9538 | return SourceRange(ToBegin, ToEnd); | |||
9539 | } | |||
9540 | ||||
9541 | Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) { | |||
9542 | llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID); | |||
9543 | if (Pos != ImportedFileIDs.end()) | |||
9544 | return Pos->second; | |||
9545 | ||||
9546 | SourceManager &FromSM = FromContext.getSourceManager(); | |||
9547 | SourceManager &ToSM = ToContext.getSourceManager(); | |||
9548 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); | |||
9549 | ||||
9550 | // Map the FromID to the "to" source manager. | |||
9551 | FileID ToID; | |||
9552 | if (FromSLoc.isExpansion()) { | |||
9553 | const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion(); | |||
9554 | ExpectedSLoc ToSpLoc = Import(FromEx.getSpellingLoc()); | |||
9555 | if (!ToSpLoc) | |||
9556 | return ToSpLoc.takeError(); | |||
9557 | ExpectedSLoc ToExLocS = Import(FromEx.getExpansionLocStart()); | |||
9558 | if (!ToExLocS) | |||
9559 | return ToExLocS.takeError(); | |||
9560 | unsigned ExLength = FromSM.getFileIDSize(FromID); | |||
9561 | SourceLocation MLoc; | |||
9562 | if (FromEx.isMacroArgExpansion()) { | |||
9563 | MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, ExLength); | |||
9564 | } else { | |||
9565 | if (ExpectedSLoc ToExLocE = Import(FromEx.getExpansionLocEnd())) | |||
9566 | MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, ExLength, | |||
9567 | FromEx.isExpansionTokenRange()); | |||
9568 | else | |||
9569 | return ToExLocE.takeError(); | |||
9570 | } | |||
9571 | ToID = ToSM.getFileID(MLoc); | |||
9572 | } else { | |||
9573 | const SrcMgr::ContentCache *Cache = &FromSLoc.getFile().getContentCache(); | |||
9574 | ||||
9575 | if (!IsBuiltin && !Cache->BufferOverridden) { | |||
9576 | // Include location of this file. | |||
9577 | ExpectedSLoc ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); | |||
9578 | if (!ToIncludeLoc) | |||
9579 | return ToIncludeLoc.takeError(); | |||
9580 | ||||
9581 | // Every FileID that is not the main FileID needs to have a valid include | |||
9582 | // location so that the include chain points to the main FileID. When | |||
9583 | // importing the main FileID (which has no include location), we need to | |||
9584 | // create a fake include location in the main file to keep this property | |||
9585 | // intact. | |||
9586 | SourceLocation ToIncludeLocOrFakeLoc = *ToIncludeLoc; | |||
9587 | if (FromID == FromSM.getMainFileID()) | |||
9588 | ToIncludeLocOrFakeLoc = ToSM.getLocForStartOfFile(ToSM.getMainFileID()); | |||
9589 | ||||
9590 | if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { | |||
9591 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the | |||
9592 | // disk again | |||
9593 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather | |||
9594 | // than mmap the files several times. | |||
9595 | auto Entry = | |||
9596 | ToFileManager.getOptionalFileRef(Cache->OrigEntry->getName()); | |||
9597 | // FIXME: The filename may be a virtual name that does probably not | |||
9598 | // point to a valid file and we get no Entry here. In this case try with | |||
9599 | // the memory buffer below. | |||
9600 | if (Entry) | |||
9601 | ToID = ToSM.createFileID(*Entry, ToIncludeLocOrFakeLoc, | |||
9602 | FromSLoc.getFile().getFileCharacteristic()); | |||
9603 | } | |||
9604 | } | |||
9605 | ||||
9606 | if (ToID.isInvalid() || IsBuiltin) { | |||
9607 | // FIXME: We want to re-use the existing MemoryBuffer! | |||
9608 | std::optional<llvm::MemoryBufferRef> FromBuf = | |||
9609 | Cache->getBufferOrNone(FromContext.getDiagnostics(), | |||
9610 | FromSM.getFileManager(), SourceLocation{}); | |||
9611 | if (!FromBuf) | |||
9612 | return llvm::make_error<ASTImportError>(ASTImportError::Unknown); | |||
9613 | ||||
9614 | std::unique_ptr<llvm::MemoryBuffer> ToBuf = | |||
9615 | llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), | |||
9616 | FromBuf->getBufferIdentifier()); | |||
9617 | ToID = ToSM.createFileID(std::move(ToBuf), | |||
9618 | FromSLoc.getFile().getFileCharacteristic()); | |||
9619 | } | |||
9620 | } | |||
9621 | ||||
9622 | assert(ToID.isValid() && "Unexpected invalid fileID was created.")(static_cast <bool> (ToID.isValid() && "Unexpected invalid fileID was created." ) ? void (0) : __assert_fail ("ToID.isValid() && \"Unexpected invalid fileID was created.\"" , "clang/lib/AST/ASTImporter.cpp", 9622, __extension__ __PRETTY_FUNCTION__ )); | |||
9623 | ||||
9624 | ImportedFileIDs[FromID] = ToID; | |||
9625 | return ToID; | |||
9626 | } | |||
9627 | ||||
9628 | Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) { | |||
9629 | ExpectedExpr ToExprOrErr = Import(From->getInit()); | |||
9630 | if (!ToExprOrErr) | |||
9631 | return ToExprOrErr.takeError(); | |||
9632 | ||||
9633 | auto LParenLocOrErr = Import(From->getLParenLoc()); | |||
9634 | if (!LParenLocOrErr) | |||
9635 | return LParenLocOrErr.takeError(); | |||
9636 | ||||
9637 | auto RParenLocOrErr = Import(From->getRParenLoc()); | |||
9638 | if (!RParenLocOrErr) | |||
9639 | return RParenLocOrErr.takeError(); | |||
9640 | ||||
9641 | if (From->isBaseInitializer()) { | |||
9642 | auto ToTInfoOrErr = Import(From->getTypeSourceInfo()); | |||
9643 | if (!ToTInfoOrErr) | |||
9644 | return ToTInfoOrErr.takeError(); | |||
9645 | ||||
9646 | SourceLocation EllipsisLoc; | |||
9647 | if (From->isPackExpansion()) | |||
9648 | if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc())) | |||
9649 | return std::move(Err); | |||
9650 | ||||
9651 | return new (ToContext) CXXCtorInitializer( | |||
9652 | ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr, | |||
9653 | *ToExprOrErr, *RParenLocOrErr, EllipsisLoc); | |||
9654 | } else if (From->isMemberInitializer()) { | |||
9655 | ExpectedDecl ToFieldOrErr = Import(From->getMember()); | |||
9656 | if (!ToFieldOrErr) | |||
9657 | return ToFieldOrErr.takeError(); | |||
9658 | ||||
9659 | auto MemberLocOrErr = Import(From->getMemberLocation()); | |||
9660 | if (!MemberLocOrErr) | |||
9661 | return MemberLocOrErr.takeError(); | |||
9662 | ||||
9663 | return new (ToContext) CXXCtorInitializer( | |||
9664 | ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr, | |||
9665 | *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); | |||
9666 | } else if (From->isIndirectMemberInitializer()) { | |||
9667 | ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember()); | |||
9668 | if (!ToIFieldOrErr) | |||
9669 | return ToIFieldOrErr.takeError(); | |||
9670 | ||||
9671 | auto MemberLocOrErr = Import(From->getMemberLocation()); | |||
9672 | if (!MemberLocOrErr) | |||
9673 | return MemberLocOrErr.takeError(); | |||
9674 | ||||
9675 | return new (ToContext) CXXCtorInitializer( | |||
9676 | ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr), | |||
9677 | *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); | |||
9678 | } else if (From->isDelegatingInitializer()) { | |||
9679 | auto ToTInfoOrErr = Import(From->getTypeSourceInfo()); | |||
9680 | if (!ToTInfoOrErr) | |||
9681 | return ToTInfoOrErr.takeError(); | |||
9682 | ||||
9683 | return new (ToContext) | |||
9684 | CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr, | |||
9685 | *ToExprOrErr, *RParenLocOrErr); | |||
9686 | } else { | |||
9687 | // FIXME: assert? | |||
9688 | return make_error<ASTImportError>(); | |||
9689 | } | |||
9690 | } | |||
9691 | ||||
9692 | Expected<CXXBaseSpecifier *> | |||
9693 | ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) { | |||
9694 | auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec); | |||
9695 | if (Pos != ImportedCXXBaseSpecifiers.end()) | |||
9696 | return Pos->second; | |||
9697 | ||||
9698 | Expected<SourceRange> ToSourceRange = Import(BaseSpec->getSourceRange()); | |||
9699 | if (!ToSourceRange) | |||
9700 | return ToSourceRange.takeError(); | |||
9701 | Expected<TypeSourceInfo *> ToTSI = Import(BaseSpec->getTypeSourceInfo()); | |||
9702 | if (!ToTSI) | |||
9703 | return ToTSI.takeError(); | |||
9704 | ExpectedSLoc ToEllipsisLoc = Import(BaseSpec->getEllipsisLoc()); | |||
9705 | if (!ToEllipsisLoc) | |||
9706 | return ToEllipsisLoc.takeError(); | |||
9707 | CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier( | |||
9708 | *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), | |||
9709 | BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc); | |||
9710 | ImportedCXXBaseSpecifiers[BaseSpec] = Imported; | |||
9711 | return Imported; | |||
9712 | } | |||
9713 | ||||
9714 | llvm::Expected<APValue> ASTImporter::Import(const APValue &FromValue) { | |||
9715 | ASTNodeImporter Importer(*this); | |||
9716 | return Importer.ImportAPValue(FromValue); | |||
9717 | } | |||
9718 | ||||
9719 | Error ASTImporter::ImportDefinition(Decl *From) { | |||
9720 | ExpectedDecl ToOrErr = Import(From); | |||
9721 | if (!ToOrErr) | |||
9722 | return ToOrErr.takeError(); | |||
9723 | Decl *To = *ToOrErr; | |||
9724 | ||||
9725 | auto *FromDC = cast<DeclContext>(From); | |||
9726 | ASTNodeImporter Importer(*this); | |||
9727 | ||||
9728 | if (auto *ToRecord = dyn_cast<RecordDecl>(To)) { | |||
9729 | if (!ToRecord->getDefinition()) { | |||
9730 | return Importer.ImportDefinition( | |||
9731 | cast<RecordDecl>(FromDC), ToRecord, | |||
9732 | ASTNodeImporter::IDK_Everything); | |||
9733 | } | |||
9734 | } | |||
9735 | ||||
9736 | if (auto *ToEnum = dyn_cast<EnumDecl>(To)) { | |||
9737 | if (!ToEnum->getDefinition()) { | |||
9738 | return Importer.ImportDefinition( | |||
9739 | cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything); | |||
9740 | } | |||
9741 | } | |||
9742 | ||||
9743 | if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { | |||
9744 | if (!ToIFace->getDefinition()) { | |||
9745 | return Importer.ImportDefinition( | |||
9746 | cast<ObjCInterfaceDecl>(FromDC), ToIFace, | |||
9747 | ASTNodeImporter::IDK_Everything); | |||
9748 | } | |||
9749 | } | |||
9750 | ||||
9751 | if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { | |||
9752 | if (!ToProto->getDefinition()) { | |||
9753 | return Importer.ImportDefinition( | |||
9754 | cast<ObjCProtocolDecl>(FromDC), ToProto, | |||
9755 | ASTNodeImporter::IDK_Everything); | |||
9756 | } | |||
9757 | } | |||
9758 | ||||
9759 | return Importer.ImportDeclContext(FromDC, true); | |||
9760 | } | |||
9761 | ||||
9762 | Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) { | |||
9763 | if (!FromName) | |||
9764 | return DeclarationName{}; | |||
9765 | ||||
9766 | switch (FromName.getNameKind()) { | |||
9767 | case DeclarationName::Identifier: | |||
9768 | return DeclarationName(Import(FromName.getAsIdentifierInfo())); | |||
9769 | ||||
9770 | case DeclarationName::ObjCZeroArgSelector: | |||
9771 | case DeclarationName::ObjCOneArgSelector: | |||
9772 | case DeclarationName::ObjCMultiArgSelector: | |||
9773 | if (auto ToSelOrErr = Import(FromName.getObjCSelector())) | |||
9774 | return DeclarationName(*ToSelOrErr); | |||
9775 | else | |||
9776 | return ToSelOrErr.takeError(); | |||
9777 | ||||
9778 | case DeclarationName::CXXConstructorName: { | |||
9779 | if (auto ToTyOrErr = Import(FromName.getCXXNameType())) | |||
9780 | return ToContext.DeclarationNames.getCXXConstructorName( | |||
9781 | ToContext.getCanonicalType(*ToTyOrErr)); | |||
9782 | else | |||
9783 | return ToTyOrErr.takeError(); | |||
9784 | } | |||
9785 | ||||
9786 | case DeclarationName::CXXDestructorName: { | |||
9787 | if (auto ToTyOrErr = Import(FromName.getCXXNameType())) | |||
9788 | return ToContext.DeclarationNames.getCXXDestructorName( | |||
9789 | ToContext.getCanonicalType(*ToTyOrErr)); | |||
9790 | else | |||
9791 | return ToTyOrErr.takeError(); | |||
9792 | } | |||
9793 | ||||
9794 | case DeclarationName::CXXDeductionGuideName: { | |||
9795 | if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate())) | |||
9796 | return ToContext.DeclarationNames.getCXXDeductionGuideName( | |||
9797 | cast<TemplateDecl>(*ToTemplateOrErr)); | |||
9798 | else | |||
9799 | return ToTemplateOrErr.takeError(); | |||
9800 | } | |||
9801 | ||||
9802 | case DeclarationName::CXXConversionFunctionName: { | |||
9803 | if (auto ToTyOrErr = Import(FromName.getCXXNameType())) | |||
9804 | return ToContext.DeclarationNames.getCXXConversionFunctionName( | |||
9805 | ToContext.getCanonicalType(*ToTyOrErr)); | |||
9806 | else | |||
9807 | return ToTyOrErr.takeError(); | |||
9808 | } | |||
9809 | ||||
9810 | case DeclarationName::CXXOperatorName: | |||
9811 | return ToContext.DeclarationNames.getCXXOperatorName( | |||
9812 | FromName.getCXXOverloadedOperator()); | |||
9813 | ||||
9814 | case DeclarationName::CXXLiteralOperatorName: | |||
9815 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( | |||
9816 | Import(FromName.getCXXLiteralIdentifier())); | |||
9817 | ||||
9818 | case DeclarationName::CXXUsingDirective: | |||
9819 | // FIXME: STATICS! | |||
9820 | return DeclarationName::getUsingDirectiveName(); | |||
9821 | } | |||
9822 | ||||
9823 | llvm_unreachable("Invalid DeclarationName Kind!")::llvm::llvm_unreachable_internal("Invalid DeclarationName Kind!" , "clang/lib/AST/ASTImporter.cpp", 9823); | |||
9824 | } | |||
9825 | ||||
9826 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { | |||
9827 | if (!FromId) | |||
9828 | return nullptr; | |||
9829 | ||||
9830 | IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName()); | |||
9831 | ||||
9832 | if (!ToId->getBuiltinID() && FromId->getBuiltinID()) | |||
9833 | ToId->setBuiltinID(FromId->getBuiltinID()); | |||
9834 | ||||
9835 | return ToId; | |||
9836 | } | |||
9837 | ||||
9838 | Expected<Selector> ASTImporter::Import(Selector FromSel) { | |||
9839 | if (FromSel.isNull()) | |||
9840 | return Selector{}; | |||
9841 | ||||
9842 | SmallVector<IdentifierInfo *, 4> Idents; | |||
9843 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); | |||
9844 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) | |||
9845 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); | |||
9846 | return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); | |||
9847 | } | |||
9848 | ||||
9849 | llvm::Expected<APValue> | |||
9850 | ASTNodeImporter::ImportAPValue(const APValue &FromValue) { | |||
9851 | APValue Result; | |||
9852 | llvm::Error Err = llvm::Error::success(); | |||
9853 | auto ImportLoop = [&](const APValue *From, APValue *To, unsigned Size) { | |||
9854 | for (unsigned Idx = 0; Idx < Size; Idx++) { | |||
9855 | APValue Tmp = importChecked(Err, From[Idx]); | |||
9856 | To[Idx] = Tmp; | |||
9857 | } | |||
9858 | }; | |||
9859 | switch (FromValue.getKind()) { | |||
9860 | case APValue::None: | |||
9861 | case APValue::Indeterminate: | |||
9862 | case APValue::Int: | |||
9863 | case APValue::Float: | |||
9864 | case APValue::FixedPoint: | |||
9865 | case APValue::ComplexInt: | |||
9866 | case APValue::ComplexFloat: | |||
9867 | Result = FromValue; | |||
9868 | break; | |||
9869 | case APValue::Vector: { | |||
9870 | Result.MakeVector(); | |||
9871 | MutableArrayRef<APValue> Elts = | |||
9872 | Result.setVectorUninit(FromValue.getVectorLength()); | |||
9873 | ImportLoop(((const APValue::Vec *)(const char *)&FromValue.Data)->Elts, | |||
9874 | Elts.data(), FromValue.getVectorLength()); | |||
9875 | break; | |||
9876 | } | |||
9877 | case APValue::Array: | |||
9878 | Result.MakeArray(FromValue.getArrayInitializedElts(), | |||
9879 | FromValue.getArraySize()); | |||
9880 | ImportLoop(((const APValue::Arr *)(const char *)&FromValue.Data)->Elts, | |||
9881 | ((const APValue::Arr *)(const char *)&Result.Data)->Elts, | |||
9882 | FromValue.getArrayInitializedElts()); | |||
9883 | break; | |||
9884 | case APValue::Struct: | |||
9885 | Result.MakeStruct(FromValue.getStructNumBases(), | |||
9886 | FromValue.getStructNumFields()); | |||
9887 | ImportLoop( | |||
9888 | ((const APValue::StructData *)(const char *)&FromValue.Data)->Elts, | |||
9889 | ((const APValue::StructData *)(const char *)&Result.Data)->Elts, | |||
9890 | FromValue.getStructNumBases() + FromValue.getStructNumFields()); | |||
9891 | break; | |||
9892 | case APValue::Union: { | |||
9893 | Result.MakeUnion(); | |||
9894 | const Decl *ImpFDecl = importChecked(Err, FromValue.getUnionField()); | |||
9895 | APValue ImpValue = importChecked(Err, FromValue.getUnionValue()); | |||
9896 | if (Err) | |||
9897 | return std::move(Err); | |||
9898 | Result.setUnion(cast<FieldDecl>(ImpFDecl), ImpValue); | |||
9899 | break; | |||
9900 | } | |||
9901 | case APValue::AddrLabelDiff: { | |||
9902 | Result.MakeAddrLabelDiff(); | |||
9903 | const Expr *ImpLHS = importChecked(Err, FromValue.getAddrLabelDiffLHS()); | |||
9904 | const Expr *ImpRHS = importChecked(Err, FromValue.getAddrLabelDiffRHS()); | |||
9905 | if (Err) | |||
9906 | return std::move(Err); | |||
9907 | Result.setAddrLabelDiff(cast<AddrLabelExpr>(ImpLHS), | |||
9908 | cast<AddrLabelExpr>(ImpRHS)); | |||
9909 | break; | |||
9910 | } | |||
9911 | case APValue::MemberPointer: { | |||
9912 | const Decl *ImpMemPtrDecl = | |||
9913 | importChecked(Err, FromValue.getMemberPointerDecl()); | |||
9914 | if (Err) | |||
9915 | return std::move(Err); | |||
9916 | MutableArrayRef<const CXXRecordDecl *> ToPath = | |||
9917 | Result.setMemberPointerUninit( | |||
9918 | cast<const ValueDecl>(ImpMemPtrDecl), | |||
9919 | FromValue.isMemberPointerToDerivedMember(), | |||
9920 | FromValue.getMemberPointerPath().size()); | |||
9921 | llvm::ArrayRef<const CXXRecordDecl *> FromPath = | |||
9922 | Result.getMemberPointerPath(); | |||
9923 | for (unsigned Idx = 0; Idx < FromValue.getMemberPointerPath().size(); | |||
9924 | Idx++) { | |||
9925 | const Decl *ImpDecl = importChecked(Err, FromPath[Idx]); | |||
9926 | if (Err) | |||
9927 | return std::move(Err); | |||
9928 | ToPath[Idx] = cast<const CXXRecordDecl>(ImpDecl->getCanonicalDecl()); | |||
9929 | } | |||
9930 | break; | |||
9931 | } | |||
9932 | case APValue::LValue: | |||
9933 | APValue::LValueBase Base; | |||
9934 | QualType FromElemTy; | |||
9935 | if (FromValue.getLValueBase()) { | |||
9936 | assert(!FromValue.getLValueBase().is<DynamicAllocLValue>() &&(static_cast <bool> (!FromValue.getLValueBase().is<DynamicAllocLValue >() && "in C++20 dynamic allocation are transient so they shouldn't " "appear in the AST") ? void (0) : __assert_fail ("!FromValue.getLValueBase().is<DynamicAllocLValue>() && \"in C++20 dynamic allocation are transient so they shouldn't \" \"appear in the AST\"" , "clang/lib/AST/ASTImporter.cpp", 9938, __extension__ __PRETTY_FUNCTION__ )) | |||
9937 | "in C++20 dynamic allocation are transient so they shouldn't "(static_cast <bool> (!FromValue.getLValueBase().is<DynamicAllocLValue >() && "in C++20 dynamic allocation are transient so they shouldn't " "appear in the AST") ? void (0) : __assert_fail ("!FromValue.getLValueBase().is<DynamicAllocLValue>() && \"in C++20 dynamic allocation are transient so they shouldn't \" \"appear in the AST\"" , "clang/lib/AST/ASTImporter.cpp", 9938, __extension__ __PRETTY_FUNCTION__ )) | |||
9938 | "appear in the AST")(static_cast <bool> (!FromValue.getLValueBase().is<DynamicAllocLValue >() && "in C++20 dynamic allocation are transient so they shouldn't " "appear in the AST") ? void (0) : __assert_fail ("!FromValue.getLValueBase().is<DynamicAllocLValue>() && \"in C++20 dynamic allocation are transient so they shouldn't \" \"appear in the AST\"" , "clang/lib/AST/ASTImporter.cpp", 9938, __extension__ __PRETTY_FUNCTION__ )); | |||
9939 | if (!FromValue.getLValueBase().is<TypeInfoLValue>()) { | |||
9940 | if (const auto *E = | |||
9941 | FromValue.getLValueBase().dyn_cast<const Expr *>()) { | |||
9942 | FromElemTy = E->getType(); | |||
9943 | const Expr *ImpExpr = importChecked(Err, E); | |||
9944 | if (Err) | |||
9945 | return std::move(Err); | |||
9946 | Base = APValue::LValueBase(ImpExpr, | |||
9947 | FromValue.getLValueBase().getCallIndex(), | |||
9948 | FromValue.getLValueBase().getVersion()); | |||
9949 | } else { | |||
9950 | FromElemTy = | |||
9951 | FromValue.getLValueBase().get<const ValueDecl *>()->getType(); | |||
9952 | const Decl *ImpDecl = importChecked( | |||
9953 | Err, FromValue.getLValueBase().get<const ValueDecl *>()); | |||
9954 | if (Err) | |||
9955 | return std::move(Err); | |||
9956 | Base = APValue::LValueBase(cast<ValueDecl>(ImpDecl), | |||
9957 | FromValue.getLValueBase().getCallIndex(), | |||
9958 | FromValue.getLValueBase().getVersion()); | |||
9959 | } | |||
9960 | } else { | |||
9961 | FromElemTy = FromValue.getLValueBase().getTypeInfoType(); | |||
9962 | const Type *ImpTypeInfo = importChecked( | |||
9963 | Err, FromValue.getLValueBase().get<TypeInfoLValue>().getType()); | |||
9964 | QualType ImpType = | |||
9965 | importChecked(Err, FromValue.getLValueBase().getTypeInfoType()); | |||
9966 | if (Err) | |||
9967 | return std::move(Err); | |||
9968 | Base = APValue::LValueBase::getTypeInfo(TypeInfoLValue(ImpTypeInfo), | |||
9969 | ImpType); | |||
9970 | } | |||
9971 | } | |||
9972 | CharUnits Offset = FromValue.getLValueOffset(); | |||
9973 | unsigned PathLength = FromValue.getLValuePath().size(); | |||
9974 | Result.MakeLValue(); | |||
9975 | if (FromValue.hasLValuePath()) { | |||
9976 | MutableArrayRef<APValue::LValuePathEntry> ToPath = Result.setLValueUninit( | |||
9977 | Base, Offset, PathLength, FromValue.isLValueOnePastTheEnd(), | |||
9978 | FromValue.isNullPointer()); | |||
9979 | llvm::ArrayRef<APValue::LValuePathEntry> FromPath = | |||
9980 | FromValue.getLValuePath(); | |||
9981 | for (unsigned LoopIdx = 0; LoopIdx < PathLength; LoopIdx++) { | |||
9982 | if (FromElemTy->isRecordType()) { | |||
9983 | const Decl *FromDecl = | |||
9984 | FromPath[LoopIdx].getAsBaseOrMember().getPointer(); | |||
9985 | const Decl *ImpDecl = importChecked(Err, FromDecl); | |||
9986 | if (Err) | |||
9987 | return std::move(Err); | |||
9988 | if (auto *RD = dyn_cast<CXXRecordDecl>(FromDecl)) | |||
9989 | FromElemTy = Importer.FromContext.getRecordType(RD); | |||
9990 | else | |||
9991 | FromElemTy = cast<ValueDecl>(FromDecl)->getType(); | |||
9992 | ToPath[LoopIdx] = APValue::LValuePathEntry(APValue::BaseOrMemberType( | |||
9993 | ImpDecl, FromPath[LoopIdx].getAsBaseOrMember().getInt())); | |||
9994 | } else { | |||
9995 | FromElemTy = | |||
9996 | Importer.FromContext.getAsArrayType(FromElemTy)->getElementType(); | |||
9997 | ToPath[LoopIdx] = APValue::LValuePathEntry::ArrayIndex( | |||
9998 | FromPath[LoopIdx].getAsArrayIndex()); | |||
9999 | } | |||
10000 | } | |||
10001 | } else | |||
10002 | Result.setLValue(Base, Offset, APValue::NoLValuePath{}, | |||
10003 | FromValue.isNullPointer()); | |||
10004 | } | |||
10005 | if (Err) | |||
10006 | return std::move(Err); | |||
10007 | return Result; | |||
10008 | } | |||
10009 | ||||
10010 | Expected<DeclarationName> ASTImporter::HandleNameConflict(DeclarationName Name, | |||
10011 | DeclContext *DC, | |||
10012 | unsigned IDNS, | |||
10013 | NamedDecl **Decls, | |||
10014 | unsigned NumDecls) { | |||
10015 | if (ODRHandling == ODRHandlingType::Conservative) | |||
10016 | // Report error at any name conflict. | |||
10017 | return make_error<ASTImportError>(ASTImportError::NameConflict); | |||
10018 | else | |||
10019 | // Allow to create the new Decl with the same name. | |||
10020 | return Name; | |||
10021 | } | |||
10022 | ||||
10023 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { | |||
10024 | if (LastDiagFromFrom) | |||
10025 | ToContext.getDiagnostics().notePriorDiagnosticFrom( | |||
10026 | FromContext.getDiagnostics()); | |||
10027 | LastDiagFromFrom = false; | |||
10028 | return ToContext.getDiagnostics().Report(Loc, DiagID); | |||
10029 | } | |||
10030 | ||||
10031 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { | |||
10032 | if (!LastDiagFromFrom) | |||
10033 | FromContext.getDiagnostics().notePriorDiagnosticFrom( | |||
10034 | ToContext.getDiagnostics()); | |||
10035 | LastDiagFromFrom = true; | |||
10036 | return FromContext.getDiagnostics().Report(Loc, DiagID); | |||
10037 | } | |||
10038 | ||||
10039 | void ASTImporter::CompleteDecl (Decl *D) { | |||
10040 | if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) { | |||
10041 | if (!ID->getDefinition()) | |||
10042 | ID->startDefinition(); | |||
10043 | } | |||
10044 | else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) { | |||
10045 | if (!PD->getDefinition()) | |||
10046 | PD->startDefinition(); | |||
10047 | } | |||
10048 | else if (auto *TD = dyn_cast<TagDecl>(D)) { | |||
10049 | if (!TD->getDefinition() && !TD->isBeingDefined()) { | |||
10050 | TD->startDefinition(); | |||
10051 | TD->setCompleteDefinition(true); | |||
10052 | } | |||
10053 | } | |||
10054 | else { | |||
10055 | assert(0 && "CompleteDecl called on a Decl that can't be completed")(static_cast <bool> (0 && "CompleteDecl called on a Decl that can't be completed" ) ? void (0) : __assert_fail ("0 && \"CompleteDecl called on a Decl that can't be completed\"" , "clang/lib/AST/ASTImporter.cpp", 10055, __extension__ __PRETTY_FUNCTION__ )); | |||
10056 | } | |||
10057 | } | |||
10058 | ||||
10059 | Decl *ASTImporter::MapImported(Decl *From, Decl *To) { | |||
10060 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From); | |||
10061 | assert((Pos == ImportedDecls.end() || Pos->second == To) &&(static_cast <bool> ((Pos == ImportedDecls.end() || Pos ->second == To) && "Try to import an already imported Decl" ) ? void (0) : __assert_fail ("(Pos == ImportedDecls.end() || Pos->second == To) && \"Try to import an already imported Decl\"" , "clang/lib/AST/ASTImporter.cpp", 10062, __extension__ __PRETTY_FUNCTION__ )) | |||
10062 | "Try to import an already imported Decl")(static_cast <bool> ((Pos == ImportedDecls.end() || Pos ->second == To) && "Try to import an already imported Decl" ) ? void (0) : __assert_fail ("(Pos == ImportedDecls.end() || Pos->second == To) && \"Try to import an already imported Decl\"" , "clang/lib/AST/ASTImporter.cpp", 10062, __extension__ __PRETTY_FUNCTION__ )); | |||
10063 | if (Pos != ImportedDecls.end()) | |||
10064 | return Pos->second; | |||
10065 | ImportedDecls[From] = To; | |||
10066 | // This mapping should be maintained only in this function. Therefore do not | |||
10067 | // check for additional consistency. | |||
10068 | ImportedFromDecls[To] = From; | |||
10069 | // In the case of TypedefNameDecl we create the Decl first and only then we | |||
10070 | // import and set its DeclContext. So, the DC is still not set when we reach | |||
10071 | // here from GetImportedOrCreateDecl. | |||
10072 | if (To->getDeclContext()) | |||
10073 | AddToLookupTable(To); | |||
10074 | return To; | |||
10075 | } | |||
10076 | ||||
10077 | std::optional<ASTImportError> | |||
10078 | ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const { | |||
10079 | auto Pos = ImportDeclErrors.find(FromD); | |||
10080 | if (Pos != ImportDeclErrors.end()) | |||
10081 | return Pos->second; | |||
10082 | else | |||
10083 | return std::nullopt; | |||
10084 | } | |||
10085 | ||||
10086 | void ASTImporter::setImportDeclError(Decl *From, ASTImportError Error) { | |||
10087 | auto InsertRes = ImportDeclErrors.insert({From, Error}); | |||
10088 | (void)InsertRes; | |||
10089 | // Either we set the error for the first time, or we already had set one and | |||
10090 | // now we want to set the same error. | |||
10091 | assert(InsertRes.second || InsertRes.first->second.Error == Error.Error)(static_cast <bool> (InsertRes.second || InsertRes.first ->second.Error == Error.Error) ? void (0) : __assert_fail ( "InsertRes.second || InsertRes.first->second.Error == Error.Error" , "clang/lib/AST/ASTImporter.cpp", 10091, __extension__ __PRETTY_FUNCTION__ )); | |||
10092 | } | |||
10093 | ||||
10094 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, | |||
10095 | bool Complain) { | |||
10096 | llvm::DenseMap<const Type *, const Type *>::iterator Pos = | |||
10097 | ImportedTypes.find(From.getTypePtr()); | |||
10098 | if (Pos != ImportedTypes.end()) { | |||
10099 | if (ExpectedType ToFromOrErr = Import(From)) { | |||
10100 | if (ToContext.hasSameType(*ToFromOrErr, To)) | |||
10101 | return true; | |||
10102 | } else { | |||
10103 | llvm::consumeError(ToFromOrErr.takeError()); | |||
10104 | } | |||
10105 | } | |||
10106 | ||||
10107 | StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, | |||
10108 | getStructuralEquivalenceKind(*this), false, | |||
10109 | Complain); | |||
10110 | return Ctx.IsEquivalent(From, To); | |||
10111 | } |
1 | //===--- Expr.h - Classes for representing expressions ----------*- C++ -*-===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file defines the Expr interface and subclasses. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #ifndef LLVM_CLANG_AST_EXPR_H | |||
14 | #define LLVM_CLANG_AST_EXPR_H | |||
15 | ||||
16 | #include "clang/AST/APValue.h" | |||
17 | #include "clang/AST/ASTVector.h" | |||
18 | #include "clang/AST/ComputeDependence.h" | |||
19 | #include "clang/AST/Decl.h" | |||
20 | #include "clang/AST/DeclAccessPair.h" | |||
21 | #include "clang/AST/DependenceFlags.h" | |||
22 | #include "clang/AST/OperationKinds.h" | |||
23 | #include "clang/AST/Stmt.h" | |||
24 | #include "clang/AST/TemplateBase.h" | |||
25 | #include "clang/AST/Type.h" | |||
26 | #include "clang/Basic/CharInfo.h" | |||
27 | #include "clang/Basic/LangOptions.h" | |||
28 | #include "clang/Basic/SyncScope.h" | |||
29 | #include "clang/Basic/TypeTraits.h" | |||
30 | #include "llvm/ADT/APFloat.h" | |||
31 | #include "llvm/ADT/APSInt.h" | |||
32 | #include "llvm/ADT/SmallVector.h" | |||
33 | #include "llvm/ADT/StringRef.h" | |||
34 | #include "llvm/ADT/iterator.h" | |||
35 | #include "llvm/ADT/iterator_range.h" | |||
36 | #include "llvm/Support/AtomicOrdering.h" | |||
37 | #include "llvm/Support/Compiler.h" | |||
38 | #include "llvm/Support/TrailingObjects.h" | |||
39 | #include <optional> | |||
40 | ||||
41 | namespace clang { | |||
42 | class APValue; | |||
43 | class ASTContext; | |||
44 | class BlockDecl; | |||
45 | class CXXBaseSpecifier; | |||
46 | class CXXMemberCallExpr; | |||
47 | class CXXOperatorCallExpr; | |||
48 | class CastExpr; | |||
49 | class Decl; | |||
50 | class IdentifierInfo; | |||
51 | class MaterializeTemporaryExpr; | |||
52 | class NamedDecl; | |||
53 | class ObjCPropertyRefExpr; | |||
54 | class OpaqueValueExpr; | |||
55 | class ParmVarDecl; | |||
56 | class StringLiteral; | |||
57 | class TargetInfo; | |||
58 | class ValueDecl; | |||
59 | ||||
60 | /// A simple array of base specifiers. | |||
61 | typedef SmallVector<CXXBaseSpecifier*, 4> CXXCastPath; | |||
62 | ||||
63 | /// An adjustment to be made to the temporary created when emitting a | |||
64 | /// reference binding, which accesses a particular subobject of that temporary. | |||
65 | struct SubobjectAdjustment { | |||
66 | enum { | |||
67 | DerivedToBaseAdjustment, | |||
68 | FieldAdjustment, | |||
69 | MemberPointerAdjustment | |||
70 | } Kind; | |||
71 | ||||
72 | struct DTB { | |||
73 | const CastExpr *BasePath; | |||
74 | const CXXRecordDecl *DerivedClass; | |||
75 | }; | |||
76 | ||||
77 | struct P { | |||
78 | const MemberPointerType *MPT; | |||
79 | Expr *RHS; | |||
80 | }; | |||
81 | ||||
82 | union { | |||
83 | struct DTB DerivedToBase; | |||
84 | FieldDecl *Field; | |||
85 | struct P Ptr; | |||
86 | }; | |||
87 | ||||
88 | SubobjectAdjustment(const CastExpr *BasePath, | |||
89 | const CXXRecordDecl *DerivedClass) | |||
90 | : Kind(DerivedToBaseAdjustment) { | |||
91 | DerivedToBase.BasePath = BasePath; | |||
92 | DerivedToBase.DerivedClass = DerivedClass; | |||
93 | } | |||
94 | ||||
95 | SubobjectAdjustment(FieldDecl *Field) | |||
96 | : Kind(FieldAdjustment) { | |||
97 | this->Field = Field; | |||
98 | } | |||
99 | ||||
100 | SubobjectAdjustment(const MemberPointerType *MPT, Expr *RHS) | |||
101 | : Kind(MemberPointerAdjustment) { | |||
102 | this->Ptr.MPT = MPT; | |||
103 | this->Ptr.RHS = RHS; | |||
104 | } | |||
105 | }; | |||
106 | ||||
107 | /// This represents one expression. Note that Expr's are subclasses of Stmt. | |||
108 | /// This allows an expression to be transparently used any place a Stmt is | |||
109 | /// required. | |||
110 | class Expr : public ValueStmt { | |||
111 | QualType TR; | |||
112 | ||||
113 | public: | |||
114 | Expr() = delete; | |||
115 | Expr(const Expr&) = delete; | |||
116 | Expr(Expr &&) = delete; | |||
117 | Expr &operator=(const Expr&) = delete; | |||
118 | Expr &operator=(Expr&&) = delete; | |||
119 | ||||
120 | protected: | |||
121 | Expr(StmtClass SC, QualType T, ExprValueKind VK, ExprObjectKind OK) | |||
122 | : ValueStmt(SC) { | |||
123 | ExprBits.Dependent = 0; | |||
124 | ExprBits.ValueKind = VK; | |||
125 | ExprBits.ObjectKind = OK; | |||
126 | assert(ExprBits.ObjectKind == OK && "truncated kind")(static_cast <bool> (ExprBits.ObjectKind == OK && "truncated kind") ? void (0) : __assert_fail ("ExprBits.ObjectKind == OK && \"truncated kind\"" , "clang/include/clang/AST/Expr.h", 126, __extension__ __PRETTY_FUNCTION__ )); | |||
127 | setType(T); | |||
128 | } | |||
129 | ||||
130 | /// Construct an empty expression. | |||
131 | explicit Expr(StmtClass SC, EmptyShell) : ValueStmt(SC) { } | |||
132 | ||||
133 | /// Each concrete expr subclass is expected to compute its dependence and call | |||
134 | /// this in the constructor. | |||
135 | void setDependence(ExprDependence Deps) { | |||
136 | ExprBits.Dependent = static_cast<unsigned>(Deps); | |||
137 | } | |||
138 | friend class ASTImporter; // Sets dependence directly. | |||
139 | friend class ASTStmtReader; // Sets dependence directly. | |||
140 | ||||
141 | public: | |||
142 | QualType getType() const { return TR; } | |||
143 | void setType(QualType t) { | |||
144 | // In C++, the type of an expression is always adjusted so that it | |||
145 | // will not have reference type (C++ [expr]p6). Use | |||
146 | // QualType::getNonReferenceType() to retrieve the non-reference | |||
147 | // type. Additionally, inspect Expr::isLvalue to determine whether | |||
148 | // an expression that is adjusted in this manner should be | |||
149 | // considered an lvalue. | |||
150 | assert((t.isNull() || !t->isReferenceType()) &&(static_cast <bool> ((t.isNull() || !t->isReferenceType ()) && "Expressions can't have reference type") ? void (0) : __assert_fail ("(t.isNull() || !t->isReferenceType()) && \"Expressions can't have reference type\"" , "clang/include/clang/AST/Expr.h", 151, __extension__ __PRETTY_FUNCTION__ )) | |||
151 | "Expressions can't have reference type")(static_cast <bool> ((t.isNull() || !t->isReferenceType ()) && "Expressions can't have reference type") ? void (0) : __assert_fail ("(t.isNull() || !t->isReferenceType()) && \"Expressions can't have reference type\"" , "clang/include/clang/AST/Expr.h", 151, __extension__ __PRETTY_FUNCTION__ )); | |||
152 | ||||
153 | TR = t; | |||
154 | } | |||
155 | ||||
156 | ExprDependence getDependence() const { | |||
157 | return static_cast<ExprDependence>(ExprBits.Dependent); | |||
158 | } | |||
159 | ||||
160 | /// Determines whether the value of this expression depends on | |||
161 | /// - a template parameter (C++ [temp.dep.constexpr]) | |||
162 | /// - or an error, whose resolution is unknown | |||
163 | /// | |||
164 | /// For example, the array bound of "Chars" in the following example is | |||
165 | /// value-dependent. | |||
166 | /// @code | |||
167 | /// template<int Size, char (&Chars)[Size]> struct meta_string; | |||
168 | /// @endcode | |||
169 | bool isValueDependent() const { | |||
170 | return static_cast<bool>(getDependence() & ExprDependence::Value); | |||
171 | } | |||
172 | ||||
173 | /// Determines whether the type of this expression depends on | |||
174 | /// - a template parameter (C++ [temp.dep.expr], which means that its type | |||
175 | /// could change from one template instantiation to the next) | |||
176 | /// - or an error | |||
177 | /// | |||
178 | /// For example, the expressions "x" and "x + y" are type-dependent in | |||
179 | /// the following code, but "y" is not type-dependent: | |||
180 | /// @code | |||
181 | /// template<typename T> | |||
182 | /// void add(T x, int y) { | |||
183 | /// x + y; | |||
184 | /// } | |||
185 | /// @endcode | |||
186 | bool isTypeDependent() const { | |||
187 | return static_cast<bool>(getDependence() & ExprDependence::Type); | |||
188 | } | |||
189 | ||||
190 | /// Whether this expression is instantiation-dependent, meaning that | |||
191 | /// it depends in some way on | |||
192 | /// - a template parameter (even if neither its type nor (constant) value | |||
193 | /// can change due to the template instantiation) | |||
194 | /// - or an error | |||
195 | /// | |||
196 | /// In the following example, the expression \c sizeof(sizeof(T() + T())) is | |||
197 | /// instantiation-dependent (since it involves a template parameter \c T), but | |||
198 | /// is neither type- nor value-dependent, since the type of the inner | |||
199 | /// \c sizeof is known (\c std::size_t) and therefore the size of the outer | |||
200 | /// \c sizeof is known. | |||
201 | /// | |||
202 | /// \code | |||
203 | /// template<typename T> | |||
204 | /// void f(T x, T y) { | |||
205 | /// sizeof(sizeof(T() + T()); | |||
206 | /// } | |||
207 | /// \endcode | |||
208 | /// | |||
209 | /// \code | |||
210 | /// void func(int) { | |||
211 | /// func(); // the expression is instantiation-dependent, because it depends | |||
212 | /// // on an error. | |||
213 | /// } | |||
214 | /// \endcode | |||
215 | bool isInstantiationDependent() const { | |||
216 | return static_cast<bool>(getDependence() & ExprDependence::Instantiation); | |||
217 | } | |||
218 | ||||
219 | /// Whether this expression contains an unexpanded parameter | |||
220 | /// pack (for C++11 variadic templates). | |||
221 | /// | |||
222 | /// Given the following function template: | |||
223 | /// | |||
224 | /// \code | |||
225 | /// template<typename F, typename ...Types> | |||
226 | /// void forward(const F &f, Types &&...args) { | |||
227 | /// f(static_cast<Types&&>(args)...); | |||
228 | /// } | |||
229 | /// \endcode | |||
230 | /// | |||
231 | /// The expressions \c args and \c static_cast<Types&&>(args) both | |||
232 | /// contain parameter packs. | |||
233 | bool containsUnexpandedParameterPack() const { | |||
234 | return static_cast<bool>(getDependence() & ExprDependence::UnexpandedPack); | |||
235 | } | |||
236 | ||||
237 | /// Whether this expression contains subexpressions which had errors, e.g. a | |||
238 | /// TypoExpr. | |||
239 | bool containsErrors() const { | |||
240 | return static_cast<bool>(getDependence() & ExprDependence::Error); | |||
241 | } | |||
242 | ||||
243 | /// getExprLoc - Return the preferred location for the arrow when diagnosing | |||
244 | /// a problem with a generic expression. | |||
245 | SourceLocation getExprLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
246 | ||||
247 | /// Determine whether an lvalue-to-rvalue conversion should implicitly be | |||
248 | /// applied to this expression if it appears as a discarded-value expression | |||
249 | /// in C++11 onwards. This applies to certain forms of volatile glvalues. | |||
250 | bool isReadIfDiscardedInCPlusPlus11() const; | |||
251 | ||||
252 | /// isUnusedResultAWarning - Return true if this immediate expression should | |||
253 | /// be warned about if the result is unused. If so, fill in expr, location, | |||
254 | /// and ranges with expr to warn on and source locations/ranges appropriate | |||
255 | /// for a warning. | |||
256 | bool isUnusedResultAWarning(const Expr *&WarnExpr, SourceLocation &Loc, | |||
257 | SourceRange &R1, SourceRange &R2, | |||
258 | ASTContext &Ctx) const; | |||
259 | ||||
260 | /// isLValue - True if this expression is an "l-value" according to | |||
261 | /// the rules of the current language. C and C++ give somewhat | |||
262 | /// different rules for this concept, but in general, the result of | |||
263 | /// an l-value expression identifies a specific object whereas the | |||
264 | /// result of an r-value expression is a value detached from any | |||
265 | /// specific storage. | |||
266 | /// | |||
267 | /// C++11 divides the concept of "r-value" into pure r-values | |||
268 | /// ("pr-values") and so-called expiring values ("x-values"), which | |||
269 | /// identify specific objects that can be safely cannibalized for | |||
270 | /// their resources. | |||
271 | bool isLValue() const { return getValueKind() == VK_LValue; } | |||
272 | bool isPRValue() const { return getValueKind() == VK_PRValue; } | |||
273 | bool isXValue() const { return getValueKind() == VK_XValue; } | |||
274 | bool isGLValue() const { return getValueKind() != VK_PRValue; } | |||
275 | ||||
276 | enum LValueClassification { | |||
277 | LV_Valid, | |||
278 | LV_NotObjectType, | |||
279 | LV_IncompleteVoidType, | |||
280 | LV_DuplicateVectorComponents, | |||
281 | LV_InvalidExpression, | |||
282 | LV_InvalidMessageExpression, | |||
283 | LV_MemberFunction, | |||
284 | LV_SubObjCPropertySetting, | |||
285 | LV_ClassTemporary, | |||
286 | LV_ArrayTemporary | |||
287 | }; | |||
288 | /// Reasons why an expression might not be an l-value. | |||
289 | LValueClassification ClassifyLValue(ASTContext &Ctx) const; | |||
290 | ||||
291 | enum isModifiableLvalueResult { | |||
292 | MLV_Valid, | |||
293 | MLV_NotObjectType, | |||
294 | MLV_IncompleteVoidType, | |||
295 | MLV_DuplicateVectorComponents, | |||
296 | MLV_InvalidExpression, | |||
297 | MLV_LValueCast, // Specialized form of MLV_InvalidExpression. | |||
298 | MLV_IncompleteType, | |||
299 | MLV_ConstQualified, | |||
300 | MLV_ConstQualifiedField, | |||
301 | MLV_ConstAddrSpace, | |||
302 | MLV_ArrayType, | |||
303 | MLV_NoSetterProperty, | |||
304 | MLV_MemberFunction, | |||
305 | MLV_SubObjCPropertySetting, | |||
306 | MLV_InvalidMessageExpression, | |||
307 | MLV_ClassTemporary, | |||
308 | MLV_ArrayTemporary | |||
309 | }; | |||
310 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, | |||
311 | /// does not have an incomplete type, does not have a const-qualified type, | |||
312 | /// and if it is a structure or union, does not have any member (including, | |||
313 | /// recursively, any member or element of all contained aggregates or unions) | |||
314 | /// with a const-qualified type. | |||
315 | /// | |||
316 | /// \param Loc [in,out] - A source location which *may* be filled | |||
317 | /// in with the location of the expression making this a | |||
318 | /// non-modifiable lvalue, if specified. | |||
319 | isModifiableLvalueResult | |||
320 | isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc = nullptr) const; | |||
321 | ||||
322 | /// The return type of classify(). Represents the C++11 expression | |||
323 | /// taxonomy. | |||
324 | class Classification { | |||
325 | public: | |||
326 | /// The various classification results. Most of these mean prvalue. | |||
327 | enum Kinds { | |||
328 | CL_LValue, | |||
329 | CL_XValue, | |||
330 | CL_Function, // Functions cannot be lvalues in C. | |||
331 | CL_Void, // Void cannot be an lvalue in C. | |||
332 | CL_AddressableVoid, // Void expression whose address can be taken in C. | |||
333 | CL_DuplicateVectorComponents, // A vector shuffle with dupes. | |||
334 | CL_MemberFunction, // An expression referring to a member function | |||
335 | CL_SubObjCPropertySetting, | |||
336 | CL_ClassTemporary, // A temporary of class type, or subobject thereof. | |||
337 | CL_ArrayTemporary, // A temporary of array type. | |||
338 | CL_ObjCMessageRValue, // ObjC message is an rvalue | |||
339 | CL_PRValue // A prvalue for any other reason, of any other type | |||
340 | }; | |||
341 | /// The results of modification testing. | |||
342 | enum ModifiableType { | |||
343 | CM_Untested, // testModifiable was false. | |||
344 | CM_Modifiable, | |||
345 | CM_RValue, // Not modifiable because it's an rvalue | |||
346 | CM_Function, // Not modifiable because it's a function; C++ only | |||
347 | CM_LValueCast, // Same as CM_RValue, but indicates GCC cast-as-lvalue ext | |||
348 | CM_NoSetterProperty,// Implicit assignment to ObjC property without setter | |||
349 | CM_ConstQualified, | |||
350 | CM_ConstQualifiedField, | |||
351 | CM_ConstAddrSpace, | |||
352 | CM_ArrayType, | |||
353 | CM_IncompleteType | |||
354 | }; | |||
355 | ||||
356 | private: | |||
357 | friend class Expr; | |||
358 | ||||
359 | unsigned short Kind; | |||
360 | unsigned short Modifiable; | |||
361 | ||||
362 | explicit Classification(Kinds k, ModifiableType m) | |||
363 | : Kind(k), Modifiable(m) | |||
364 | {} | |||
365 | ||||
366 | public: | |||
367 | Classification() {} | |||
368 | ||||
369 | Kinds getKind() const { return static_cast<Kinds>(Kind); } | |||
370 | ModifiableType getModifiable() const { | |||
371 | assert(Modifiable != CM_Untested && "Did not test for modifiability.")(static_cast <bool> (Modifiable != CM_Untested && "Did not test for modifiability.") ? void (0) : __assert_fail ("Modifiable != CM_Untested && \"Did not test for modifiability.\"" , "clang/include/clang/AST/Expr.h", 371, __extension__ __PRETTY_FUNCTION__ )); | |||
372 | return static_cast<ModifiableType>(Modifiable); | |||
373 | } | |||
374 | bool isLValue() const { return Kind == CL_LValue; } | |||
375 | bool isXValue() const { return Kind == CL_XValue; } | |||
376 | bool isGLValue() const { return Kind <= CL_XValue; } | |||
377 | bool isPRValue() const { return Kind >= CL_Function; } | |||
378 | bool isRValue() const { return Kind >= CL_XValue; } | |||
379 | bool isModifiable() const { return getModifiable() == CM_Modifiable; } | |||
380 | ||||
381 | /// Create a simple, modifiably lvalue | |||
382 | static Classification makeSimpleLValue() { | |||
383 | return Classification(CL_LValue, CM_Modifiable); | |||
384 | } | |||
385 | ||||
386 | }; | |||
387 | /// Classify - Classify this expression according to the C++11 | |||
388 | /// expression taxonomy. | |||
389 | /// | |||
390 | /// C++11 defines ([basic.lval]) a new taxonomy of expressions to replace the | |||
391 | /// old lvalue vs rvalue. This function determines the type of expression this | |||
392 | /// is. There are three expression types: | |||
393 | /// - lvalues are classical lvalues as in C++03. | |||
394 | /// - prvalues are equivalent to rvalues in C++03. | |||
395 | /// - xvalues are expressions yielding unnamed rvalue references, e.g. a | |||
396 | /// function returning an rvalue reference. | |||
397 | /// lvalues and xvalues are collectively referred to as glvalues, while | |||
398 | /// prvalues and xvalues together form rvalues. | |||
399 | Classification Classify(ASTContext &Ctx) const { | |||
400 | return ClassifyImpl(Ctx, nullptr); | |||
401 | } | |||
402 | ||||
403 | /// ClassifyModifiable - Classify this expression according to the | |||
404 | /// C++11 expression taxonomy, and see if it is valid on the left side | |||
405 | /// of an assignment. | |||
406 | /// | |||
407 | /// This function extends classify in that it also tests whether the | |||
408 | /// expression is modifiable (C99 6.3.2.1p1). | |||
409 | /// \param Loc A source location that might be filled with a relevant location | |||
410 | /// if the expression is not modifiable. | |||
411 | Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const{ | |||
412 | return ClassifyImpl(Ctx, &Loc); | |||
413 | } | |||
414 | ||||
415 | /// Returns the set of floating point options that apply to this expression. | |||
416 | /// Only meaningful for operations on floating point values. | |||
417 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const; | |||
418 | ||||
419 | /// getValueKindForType - Given a formal return or parameter type, | |||
420 | /// give its value kind. | |||
421 | static ExprValueKind getValueKindForType(QualType T) { | |||
422 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) | |||
423 | return (isa<LValueReferenceType>(RT) | |||
424 | ? VK_LValue | |||
425 | : (RT->getPointeeType()->isFunctionType() | |||
426 | ? VK_LValue : VK_XValue)); | |||
427 | return VK_PRValue; | |||
428 | } | |||
429 | ||||
430 | /// getValueKind - The value kind that this expression produces. | |||
431 | ExprValueKind getValueKind() const { | |||
432 | return static_cast<ExprValueKind>(ExprBits.ValueKind); | |||
433 | } | |||
434 | ||||
435 | /// getObjectKind - The object kind that this expression produces. | |||
436 | /// Object kinds are meaningful only for expressions that yield an | |||
437 | /// l-value or x-value. | |||
438 | ExprObjectKind getObjectKind() const { | |||
439 | return static_cast<ExprObjectKind>(ExprBits.ObjectKind); | |||
440 | } | |||
441 | ||||
442 | bool isOrdinaryOrBitFieldObject() const { | |||
443 | ExprObjectKind OK = getObjectKind(); | |||
444 | return (OK == OK_Ordinary || OK == OK_BitField); | |||
445 | } | |||
446 | ||||
447 | /// setValueKind - Set the value kind produced by this expression. | |||
448 | void setValueKind(ExprValueKind Cat) { ExprBits.ValueKind = Cat; } | |||
449 | ||||
450 | /// setObjectKind - Set the object kind produced by this expression. | |||
451 | void setObjectKind(ExprObjectKind Cat) { ExprBits.ObjectKind = Cat; } | |||
452 | ||||
453 | private: | |||
454 | Classification ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const; | |||
455 | ||||
456 | public: | |||
457 | ||||
458 | /// Returns true if this expression is a gl-value that | |||
459 | /// potentially refers to a bit-field. | |||
460 | /// | |||
461 | /// In C++, whether a gl-value refers to a bitfield is essentially | |||
462 | /// an aspect of the value-kind type system. | |||
463 | bool refersToBitField() const { return getObjectKind() == OK_BitField; } | |||
464 | ||||
465 | /// If this expression refers to a bit-field, retrieve the | |||
466 | /// declaration of that bit-field. | |||
467 | /// | |||
468 | /// Note that this returns a non-null pointer in subtly different | |||
469 | /// places than refersToBitField returns true. In particular, this can | |||
470 | /// return a non-null pointer even for r-values loaded from | |||
471 | /// bit-fields, but it will return null for a conditional bit-field. | |||
472 | FieldDecl *getSourceBitField(); | |||
473 | ||||
474 | const FieldDecl *getSourceBitField() const { | |||
475 | return const_cast<Expr*>(this)->getSourceBitField(); | |||
476 | } | |||
477 | ||||
478 | Decl *getReferencedDeclOfCallee(); | |||
479 | const Decl *getReferencedDeclOfCallee() const { | |||
480 | return const_cast<Expr*>(this)->getReferencedDeclOfCallee(); | |||
481 | } | |||
482 | ||||
483 | /// If this expression is an l-value for an Objective C | |||
484 | /// property, find the underlying property reference expression. | |||
485 | const ObjCPropertyRefExpr *getObjCProperty() const; | |||
486 | ||||
487 | /// Check if this expression is the ObjC 'self' implicit parameter. | |||
488 | bool isObjCSelfExpr() const; | |||
489 | ||||
490 | /// Returns whether this expression refers to a vector element. | |||
491 | bool refersToVectorElement() const; | |||
492 | ||||
493 | /// Returns whether this expression refers to a matrix element. | |||
494 | bool refersToMatrixElement() const { | |||
495 | return getObjectKind() == OK_MatrixComponent; | |||
496 | } | |||
497 | ||||
498 | /// Returns whether this expression refers to a global register | |||
499 | /// variable. | |||
500 | bool refersToGlobalRegisterVar() const; | |||
501 | ||||
502 | /// Returns whether this expression has a placeholder type. | |||
503 | bool hasPlaceholderType() const { | |||
504 | return getType()->isPlaceholderType(); | |||
505 | } | |||
506 | ||||
507 | /// Returns whether this expression has a specific placeholder type. | |||
508 | bool hasPlaceholderType(BuiltinType::Kind K) const { | |||
509 | assert(BuiltinType::isPlaceholderTypeKind(K))(static_cast <bool> (BuiltinType::isPlaceholderTypeKind (K)) ? void (0) : __assert_fail ("BuiltinType::isPlaceholderTypeKind(K)" , "clang/include/clang/AST/Expr.h", 509, __extension__ __PRETTY_FUNCTION__ )); | |||
510 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(getType())) | |||
511 | return BT->getKind() == K; | |||
512 | return false; | |||
513 | } | |||
514 | ||||
515 | /// isKnownToHaveBooleanValue - Return true if this is an integer expression | |||
516 | /// that is known to return 0 or 1. This happens for _Bool/bool expressions | |||
517 | /// but also int expressions which are produced by things like comparisons in | |||
518 | /// C. | |||
519 | /// | |||
520 | /// \param Semantic If true, only return true for expressions that are known | |||
521 | /// to be semantically boolean, which might not be true even for expressions | |||
522 | /// that are known to evaluate to 0/1. For instance, reading an unsigned | |||
523 | /// bit-field with width '1' will evaluate to 0/1, but doesn't necessarily | |||
524 | /// semantically correspond to a bool. | |||
525 | bool isKnownToHaveBooleanValue(bool Semantic = true) const; | |||
526 | ||||
527 | /// Check whether this array fits the idiom of a flexible array member, | |||
528 | /// depending on the value of -fstrict-flex-array. | |||
529 | /// When IgnoreTemplateOrMacroSubstitution is set, it doesn't consider sizes | |||
530 | /// resulting from the substitution of a macro or a template as special sizes. | |||
531 | bool isFlexibleArrayMemberLike( | |||
532 | ASTContext &Context, | |||
533 | LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel, | |||
534 | bool IgnoreTemplateOrMacroSubstitution = false) const; | |||
535 | ||||
536 | /// isIntegerConstantExpr - Return the value if this expression is a valid | |||
537 | /// integer constant expression. If not a valid i-c-e, return std::nullopt | |||
538 | /// and fill in Loc (if specified) with the location of the invalid | |||
539 | /// expression. | |||
540 | /// | |||
541 | /// Note: This does not perform the implicit conversions required by C++11 | |||
542 | /// [expr.const]p5. | |||
543 | std::optional<llvm::APSInt> | |||
544 | getIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc = nullptr, | |||
545 | bool isEvaluated = true) const; | |||
546 | bool isIntegerConstantExpr(const ASTContext &Ctx, | |||
547 | SourceLocation *Loc = nullptr) const; | |||
548 | ||||
549 | /// isCXX98IntegralConstantExpr - Return true if this expression is an | |||
550 | /// integral constant expression in C++98. Can only be used in C++. | |||
551 | bool isCXX98IntegralConstantExpr(const ASTContext &Ctx) const; | |||
552 | ||||
553 | /// isCXX11ConstantExpr - Return true if this expression is a constant | |||
554 | /// expression in C++11. Can only be used in C++. | |||
555 | /// | |||
556 | /// Note: This does not perform the implicit conversions required by C++11 | |||
557 | /// [expr.const]p5. | |||
558 | bool isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result = nullptr, | |||
559 | SourceLocation *Loc = nullptr) const; | |||
560 | ||||
561 | /// isPotentialConstantExpr - Return true if this function's definition | |||
562 | /// might be usable in a constant expression in C++11, if it were marked | |||
563 | /// constexpr. Return false if the function can never produce a constant | |||
564 | /// expression, along with diagnostics describing why not. | |||
565 | static bool isPotentialConstantExpr(const FunctionDecl *FD, | |||
566 | SmallVectorImpl< | |||
567 | PartialDiagnosticAt> &Diags); | |||
568 | ||||
569 | /// isPotentialConstantExprUnevaluted - Return true if this expression might | |||
570 | /// be usable in a constant expression in C++11 in an unevaluated context, if | |||
571 | /// it were in function FD marked constexpr. Return false if the function can | |||
572 | /// never produce a constant expression, along with diagnostics describing | |||
573 | /// why not. | |||
574 | static bool isPotentialConstantExprUnevaluated(Expr *E, | |||
575 | const FunctionDecl *FD, | |||
576 | SmallVectorImpl< | |||
577 | PartialDiagnosticAt> &Diags); | |||
578 | ||||
579 | /// isConstantInitializer - Returns true if this expression can be emitted to | |||
580 | /// IR as a constant, and thus can be used as a constant initializer in C. | |||
581 | /// If this expression is not constant and Culprit is non-null, | |||
582 | /// it is used to store the address of first non constant expr. | |||
583 | bool isConstantInitializer(ASTContext &Ctx, bool ForRef, | |||
584 | const Expr **Culprit = nullptr) const; | |||
585 | ||||
586 | /// If this expression is an unambiguous reference to a single declaration, | |||
587 | /// in the style of __builtin_function_start, return that declaration. Note | |||
588 | /// that this may return a non-static member function or field in C++ if this | |||
589 | /// expression is a member pointer constant. | |||
590 | const ValueDecl *getAsBuiltinConstantDeclRef(const ASTContext &Context) const; | |||
591 | ||||
592 | /// EvalStatus is a struct with detailed info about an evaluation in progress. | |||
593 | struct EvalStatus { | |||
594 | /// Whether the evaluated expression has side effects. | |||
595 | /// For example, (f() && 0) can be folded, but it still has side effects. | |||
596 | bool HasSideEffects = false; | |||
597 | ||||
598 | /// Whether the evaluation hit undefined behavior. | |||
599 | /// For example, 1.0 / 0.0 can be folded to Inf, but has undefined behavior. | |||
600 | /// Likewise, INT_MAX + 1 can be folded to INT_MIN, but has UB. | |||
601 | bool HasUndefinedBehavior = false; | |||
602 | ||||
603 | /// Diag - If this is non-null, it will be filled in with a stack of notes | |||
604 | /// indicating why evaluation failed (or why it failed to produce a constant | |||
605 | /// expression). | |||
606 | /// If the expression is unfoldable, the notes will indicate why it's not | |||
607 | /// foldable. If the expression is foldable, but not a constant expression, | |||
608 | /// the notes will describes why it isn't a constant expression. If the | |||
609 | /// expression *is* a constant expression, no notes will be produced. | |||
610 | SmallVectorImpl<PartialDiagnosticAt> *Diag = nullptr; | |||
611 | ||||
612 | EvalStatus() = default; | |||
613 | ||||
614 | // hasSideEffects - Return true if the evaluated expression has | |||
615 | // side effects. | |||
616 | bool hasSideEffects() const { | |||
617 | return HasSideEffects; | |||
618 | } | |||
619 | }; | |||
620 | ||||
621 | /// EvalResult is a struct with detailed info about an evaluated expression. | |||
622 | struct EvalResult : EvalStatus { | |||
623 | /// Val - This is the value the expression can be folded to. | |||
624 | APValue Val; | |||
625 | ||||
626 | // isGlobalLValue - Return true if the evaluated lvalue expression | |||
627 | // is global. | |||
628 | bool isGlobalLValue() const; | |||
629 | }; | |||
630 | ||||
631 | /// EvaluateAsRValue - Return true if this is a constant which we can fold to | |||
632 | /// an rvalue using any crazy technique (that has nothing to do with language | |||
633 | /// standards) that we want to, even if the expression has side-effects. If | |||
634 | /// this function returns true, it returns the folded constant in Result. If | |||
635 | /// the expression is a glvalue, an lvalue-to-rvalue conversion will be | |||
636 | /// applied. | |||
637 | bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, | |||
638 | bool InConstantContext = false) const; | |||
639 | ||||
640 | /// EvaluateAsBooleanCondition - Return true if this is a constant | |||
641 | /// which we can fold and convert to a boolean condition using | |||
642 | /// any crazy technique that we want to, even if the expression has | |||
643 | /// side-effects. | |||
644 | bool EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, | |||
645 | bool InConstantContext = false) const; | |||
646 | ||||
647 | enum SideEffectsKind { | |||
648 | SE_NoSideEffects, ///< Strictly evaluate the expression. | |||
649 | SE_AllowUndefinedBehavior, ///< Allow UB that we can give a value, but not | |||
650 | ///< arbitrary unmodeled side effects. | |||
651 | SE_AllowSideEffects ///< Allow any unmodeled side effect. | |||
652 | }; | |||
653 | ||||
654 | /// EvaluateAsInt - Return true if this is a constant which we can fold and | |||
655 | /// convert to an integer, using any crazy technique that we want to. | |||
656 | bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, | |||
657 | SideEffectsKind AllowSideEffects = SE_NoSideEffects, | |||
658 | bool InConstantContext = false) const; | |||
659 | ||||
660 | /// EvaluateAsFloat - Return true if this is a constant which we can fold and | |||
661 | /// convert to a floating point value, using any crazy technique that we | |||
662 | /// want to. | |||
663 | bool EvaluateAsFloat(llvm::APFloat &Result, const ASTContext &Ctx, | |||
664 | SideEffectsKind AllowSideEffects = SE_NoSideEffects, | |||
665 | bool InConstantContext = false) const; | |||
666 | ||||
667 | /// EvaluateAsFixedPoint - Return true if this is a constant which we can fold | |||
668 | /// and convert to a fixed point value. | |||
669 | bool EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx, | |||
670 | SideEffectsKind AllowSideEffects = SE_NoSideEffects, | |||
671 | bool InConstantContext = false) const; | |||
672 | ||||
673 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be | |||
674 | /// constant folded without side-effects, but discard the result. | |||
675 | bool isEvaluatable(const ASTContext &Ctx, | |||
676 | SideEffectsKind AllowSideEffects = SE_NoSideEffects) const; | |||
677 | ||||
678 | /// HasSideEffects - This routine returns true for all those expressions | |||
679 | /// which have any effect other than producing a value. Example is a function | |||
680 | /// call, volatile variable read, or throwing an exception. If | |||
681 | /// IncludePossibleEffects is false, this call treats certain expressions with | |||
682 | /// potential side effects (such as function call-like expressions, | |||
683 | /// instantiation-dependent expressions, or invocations from a macro) as not | |||
684 | /// having side effects. | |||
685 | bool HasSideEffects(const ASTContext &Ctx, | |||
686 | bool IncludePossibleEffects = true) const; | |||
687 | ||||
688 | /// Determine whether this expression involves a call to any function | |||
689 | /// that is not trivial. | |||
690 | bool hasNonTrivialCall(const ASTContext &Ctx) const; | |||
691 | ||||
692 | /// EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded | |||
693 | /// integer. This must be called on an expression that constant folds to an | |||
694 | /// integer. | |||
695 | llvm::APSInt EvaluateKnownConstInt( | |||
696 | const ASTContext &Ctx, | |||
697 | SmallVectorImpl<PartialDiagnosticAt> *Diag = nullptr) const; | |||
698 | ||||
699 | llvm::APSInt EvaluateKnownConstIntCheckOverflow( | |||
700 | const ASTContext &Ctx, | |||
701 | SmallVectorImpl<PartialDiagnosticAt> *Diag = nullptr) const; | |||
702 | ||||
703 | void EvaluateForOverflow(const ASTContext &Ctx) const; | |||
704 | ||||
705 | /// EvaluateAsLValue - Evaluate an expression to see if we can fold it to an | |||
706 | /// lvalue with link time known address, with no side-effects. | |||
707 | bool EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, | |||
708 | bool InConstantContext = false) const; | |||
709 | ||||
710 | /// EvaluateAsInitializer - Evaluate an expression as if it were the | |||
711 | /// initializer of the given declaration. Returns true if the initializer | |||
712 | /// can be folded to a constant, and produces any relevant notes. In C++11, | |||
713 | /// notes will be produced if the expression is not a constant expression. | |||
714 | bool EvaluateAsInitializer(APValue &Result, const ASTContext &Ctx, | |||
715 | const VarDecl *VD, | |||
716 | SmallVectorImpl<PartialDiagnosticAt> &Notes, | |||
717 | bool IsConstantInitializer) const; | |||
718 | ||||
719 | /// EvaluateWithSubstitution - Evaluate an expression as if from the context | |||
720 | /// of a call to the given function with the given arguments, inside an | |||
721 | /// unevaluated context. Returns true if the expression could be folded to a | |||
722 | /// constant. | |||
723 | bool EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, | |||
724 | const FunctionDecl *Callee, | |||
725 | ArrayRef<const Expr*> Args, | |||
726 | const Expr *This = nullptr) const; | |||
727 | ||||
728 | enum class ConstantExprKind { | |||
729 | /// An integer constant expression (an array bound, enumerator, case value, | |||
730 | /// bit-field width, or similar) or similar. | |||
731 | Normal, | |||
732 | /// A non-class template argument. Such a value is only used for mangling, | |||
733 | /// not for code generation, so can refer to dllimported functions. | |||
734 | NonClassTemplateArgument, | |||
735 | /// A class template argument. Such a value is used for code generation. | |||
736 | ClassTemplateArgument, | |||
737 | /// An immediate invocation. The destruction of the end result of this | |||
738 | /// evaluation is not part of the evaluation, but all other temporaries | |||
739 | /// are destroyed. | |||
740 | ImmediateInvocation, | |||
741 | }; | |||
742 | ||||
743 | /// Evaluate an expression that is required to be a constant expression. Does | |||
744 | /// not check the syntactic constraints for C and C++98 constant expressions. | |||
745 | bool EvaluateAsConstantExpr( | |||
746 | EvalResult &Result, const ASTContext &Ctx, | |||
747 | ConstantExprKind Kind = ConstantExprKind::Normal) const; | |||
748 | ||||
749 | /// If the current Expr is a pointer, this will try to statically | |||
750 | /// determine the number of bytes available where the pointer is pointing. | |||
751 | /// Returns true if all of the above holds and we were able to figure out the | |||
752 | /// size, false otherwise. | |||
753 | /// | |||
754 | /// \param Type - How to evaluate the size of the Expr, as defined by the | |||
755 | /// "type" parameter of __builtin_object_size | |||
756 | bool tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, | |||
757 | unsigned Type) const; | |||
758 | ||||
759 | /// If the current Expr is a pointer, this will try to statically | |||
760 | /// determine the strlen of the string pointed to. | |||
761 | /// Returns true if all of the above holds and we were able to figure out the | |||
762 | /// strlen, false otherwise. | |||
763 | bool tryEvaluateStrLen(uint64_t &Result, ASTContext &Ctx) const; | |||
764 | ||||
765 | /// Enumeration used to describe the kind of Null pointer constant | |||
766 | /// returned from \c isNullPointerConstant(). | |||
767 | enum NullPointerConstantKind { | |||
768 | /// Expression is not a Null pointer constant. | |||
769 | NPCK_NotNull = 0, | |||
770 | ||||
771 | /// Expression is a Null pointer constant built from a zero integer | |||
772 | /// expression that is not a simple, possibly parenthesized, zero literal. | |||
773 | /// C++ Core Issue 903 will classify these expressions as "not pointers" | |||
774 | /// once it is adopted. | |||
775 | /// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 | |||
776 | NPCK_ZeroExpression, | |||
777 | ||||
778 | /// Expression is a Null pointer constant built from a literal zero. | |||
779 | NPCK_ZeroLiteral, | |||
780 | ||||
781 | /// Expression is a C++11 nullptr. | |||
782 | NPCK_CXX11_nullptr, | |||
783 | ||||
784 | /// Expression is a GNU-style __null constant. | |||
785 | NPCK_GNUNull | |||
786 | }; | |||
787 | ||||
788 | /// Enumeration used to describe how \c isNullPointerConstant() | |||
789 | /// should cope with value-dependent expressions. | |||
790 | enum NullPointerConstantValueDependence { | |||
791 | /// Specifies that the expression should never be value-dependent. | |||
792 | NPC_NeverValueDependent = 0, | |||
793 | ||||
794 | /// Specifies that a value-dependent expression of integral or | |||
795 | /// dependent type should be considered a null pointer constant. | |||
796 | NPC_ValueDependentIsNull, | |||
797 | ||||
798 | /// Specifies that a value-dependent expression should be considered | |||
799 | /// to never be a null pointer constant. | |||
800 | NPC_ValueDependentIsNotNull | |||
801 | }; | |||
802 | ||||
803 | /// isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to | |||
804 | /// a Null pointer constant. The return value can further distinguish the | |||
805 | /// kind of NULL pointer constant that was detected. | |||
806 | NullPointerConstantKind isNullPointerConstant( | |||
807 | ASTContext &Ctx, | |||
808 | NullPointerConstantValueDependence NPC) const; | |||
809 | ||||
810 | /// isOBJCGCCandidate - Return true if this expression may be used in a read/ | |||
811 | /// write barrier. | |||
812 | bool isOBJCGCCandidate(ASTContext &Ctx) const; | |||
813 | ||||
814 | /// Returns true if this expression is a bound member function. | |||
815 | bool isBoundMemberFunction(ASTContext &Ctx) const; | |||
816 | ||||
817 | /// Given an expression of bound-member type, find the type | |||
818 | /// of the member. Returns null if this is an *overloaded* bound | |||
819 | /// member expression. | |||
820 | static QualType findBoundMemberType(const Expr *expr); | |||
821 | ||||
822 | /// Skip past any invisible AST nodes which might surround this | |||
823 | /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes, | |||
824 | /// but also injected CXXMemberExpr and CXXConstructExpr which represent | |||
825 | /// implicit conversions. | |||
826 | Expr *IgnoreUnlessSpelledInSource(); | |||
827 | const Expr *IgnoreUnlessSpelledInSource() const { | |||
828 | return const_cast<Expr *>(this)->IgnoreUnlessSpelledInSource(); | |||
829 | } | |||
830 | ||||
831 | /// Skip past any implicit casts which might surround this expression until | |||
832 | /// reaching a fixed point. Skips: | |||
833 | /// * ImplicitCastExpr | |||
834 | /// * FullExpr | |||
835 | Expr *IgnoreImpCasts() LLVM_READONLY__attribute__((__pure__)); | |||
836 | const Expr *IgnoreImpCasts() const { | |||
837 | return const_cast<Expr *>(this)->IgnoreImpCasts(); | |||
838 | } | |||
839 | ||||
840 | /// Skip past any casts which might surround this expression until reaching | |||
841 | /// a fixed point. Skips: | |||
842 | /// * CastExpr | |||
843 | /// * FullExpr | |||
844 | /// * MaterializeTemporaryExpr | |||
845 | /// * SubstNonTypeTemplateParmExpr | |||
846 | Expr *IgnoreCasts() LLVM_READONLY__attribute__((__pure__)); | |||
847 | const Expr *IgnoreCasts() const { | |||
848 | return const_cast<Expr *>(this)->IgnoreCasts(); | |||
849 | } | |||
850 | ||||
851 | /// Skip past any implicit AST nodes which might surround this expression | |||
852 | /// until reaching a fixed point. Skips: | |||
853 | /// * What IgnoreImpCasts() skips | |||
854 | /// * MaterializeTemporaryExpr | |||
855 | /// * CXXBindTemporaryExpr | |||
856 | Expr *IgnoreImplicit() LLVM_READONLY__attribute__((__pure__)); | |||
857 | const Expr *IgnoreImplicit() const { | |||
858 | return const_cast<Expr *>(this)->IgnoreImplicit(); | |||
859 | } | |||
860 | ||||
861 | /// Skip past any implicit AST nodes which might surround this expression | |||
862 | /// until reaching a fixed point. Same as IgnoreImplicit, except that it | |||
863 | /// also skips over implicit calls to constructors and conversion functions. | |||
864 | /// | |||
865 | /// FIXME: Should IgnoreImplicit do this? | |||
866 | Expr *IgnoreImplicitAsWritten() LLVM_READONLY__attribute__((__pure__)); | |||
867 | const Expr *IgnoreImplicitAsWritten() const { | |||
868 | return const_cast<Expr *>(this)->IgnoreImplicitAsWritten(); | |||
869 | } | |||
870 | ||||
871 | /// Skip past any parentheses which might surround this expression until | |||
872 | /// reaching a fixed point. Skips: | |||
873 | /// * ParenExpr | |||
874 | /// * UnaryOperator if `UO_Extension` | |||
875 | /// * GenericSelectionExpr if `!isResultDependent()` | |||
876 | /// * ChooseExpr if `!isConditionDependent()` | |||
877 | /// * ConstantExpr | |||
878 | Expr *IgnoreParens() LLVM_READONLY__attribute__((__pure__)); | |||
879 | const Expr *IgnoreParens() const { | |||
880 | return const_cast<Expr *>(this)->IgnoreParens(); | |||
881 | } | |||
882 | ||||
883 | /// Skip past any parentheses and implicit casts which might surround this | |||
884 | /// expression until reaching a fixed point. | |||
885 | /// FIXME: IgnoreParenImpCasts really ought to be equivalent to | |||
886 | /// IgnoreParens() + IgnoreImpCasts() until reaching a fixed point. However | |||
887 | /// this is currently not the case. Instead IgnoreParenImpCasts() skips: | |||
888 | /// * What IgnoreParens() skips | |||
889 | /// * What IgnoreImpCasts() skips | |||
890 | /// * MaterializeTemporaryExpr | |||
891 | /// * SubstNonTypeTemplateParmExpr | |||
892 | Expr *IgnoreParenImpCasts() LLVM_READONLY__attribute__((__pure__)); | |||
893 | const Expr *IgnoreParenImpCasts() const { | |||
894 | return const_cast<Expr *>(this)->IgnoreParenImpCasts(); | |||
895 | } | |||
896 | ||||
897 | /// Skip past any parentheses and casts which might surround this expression | |||
898 | /// until reaching a fixed point. Skips: | |||
899 | /// * What IgnoreParens() skips | |||
900 | /// * What IgnoreCasts() skips | |||
901 | Expr *IgnoreParenCasts() LLVM_READONLY__attribute__((__pure__)); | |||
902 | const Expr *IgnoreParenCasts() const { | |||
903 | return const_cast<Expr *>(this)->IgnoreParenCasts(); | |||
904 | } | |||
905 | ||||
906 | /// Skip conversion operators. If this Expr is a call to a conversion | |||
907 | /// operator, return the argument. | |||
908 | Expr *IgnoreConversionOperatorSingleStep() LLVM_READONLY__attribute__((__pure__)); | |||
909 | const Expr *IgnoreConversionOperatorSingleStep() const { | |||
910 | return const_cast<Expr *>(this)->IgnoreConversionOperatorSingleStep(); | |||
911 | } | |||
912 | ||||
913 | /// Skip past any parentheses and lvalue casts which might surround this | |||
914 | /// expression until reaching a fixed point. Skips: | |||
915 | /// * What IgnoreParens() skips | |||
916 | /// * What IgnoreCasts() skips, except that only lvalue-to-rvalue | |||
917 | /// casts are skipped | |||
918 | /// FIXME: This is intended purely as a temporary workaround for code | |||
919 | /// that hasn't yet been rewritten to do the right thing about those | |||
920 | /// casts, and may disappear along with the last internal use. | |||
921 | Expr *IgnoreParenLValueCasts() LLVM_READONLY__attribute__((__pure__)); | |||
922 | const Expr *IgnoreParenLValueCasts() const { | |||
923 | return const_cast<Expr *>(this)->IgnoreParenLValueCasts(); | |||
924 | } | |||
925 | ||||
926 | /// Skip past any parentheses and casts which do not change the value | |||
927 | /// (including ptr->int casts of the same size) until reaching a fixed point. | |||
928 | /// Skips: | |||
929 | /// * What IgnoreParens() skips | |||
930 | /// * CastExpr which do not change the value | |||
931 | /// * SubstNonTypeTemplateParmExpr | |||
932 | Expr *IgnoreParenNoopCasts(const ASTContext &Ctx) LLVM_READONLY__attribute__((__pure__)); | |||
933 | const Expr *IgnoreParenNoopCasts(const ASTContext &Ctx) const { | |||
934 | return const_cast<Expr *>(this)->IgnoreParenNoopCasts(Ctx); | |||
935 | } | |||
936 | ||||
937 | /// Skip past any parentheses and derived-to-base casts until reaching a | |||
938 | /// fixed point. Skips: | |||
939 | /// * What IgnoreParens() skips | |||
940 | /// * CastExpr which represent a derived-to-base cast (CK_DerivedToBase, | |||
941 | /// CK_UncheckedDerivedToBase and CK_NoOp) | |||
942 | Expr *IgnoreParenBaseCasts() LLVM_READONLY__attribute__((__pure__)); | |||
943 | const Expr *IgnoreParenBaseCasts() const { | |||
944 | return const_cast<Expr *>(this)->IgnoreParenBaseCasts(); | |||
945 | } | |||
946 | ||||
947 | /// Determine whether this expression is a default function argument. | |||
948 | /// | |||
949 | /// Default arguments are implicitly generated in the abstract syntax tree | |||
950 | /// by semantic analysis for function calls, object constructions, etc. in | |||
951 | /// C++. Default arguments are represented by \c CXXDefaultArgExpr nodes; | |||
952 | /// this routine also looks through any implicit casts to determine whether | |||
953 | /// the expression is a default argument. | |||
954 | bool isDefaultArgument() const; | |||
955 | ||||
956 | /// Determine whether the result of this expression is a | |||
957 | /// temporary object of the given class type. | |||
958 | bool isTemporaryObject(ASTContext &Ctx, const CXXRecordDecl *TempTy) const; | |||
959 | ||||
960 | /// Whether this expression is an implicit reference to 'this' in C++. | |||
961 | bool isImplicitCXXThis() const; | |||
962 | ||||
963 | static bool hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs); | |||
964 | ||||
965 | /// For an expression of class type or pointer to class type, | |||
966 | /// return the most derived class decl the expression is known to refer to. | |||
967 | /// | |||
968 | /// If this expression is a cast, this method looks through it to find the | |||
969 | /// most derived decl that can be inferred from the expression. | |||
970 | /// This is valid because derived-to-base conversions have undefined | |||
971 | /// behavior if the object isn't dynamically of the derived type. | |||
972 | const CXXRecordDecl *getBestDynamicClassType() const; | |||
973 | ||||
974 | /// Get the inner expression that determines the best dynamic class. | |||
975 | /// If this is a prvalue, we guarantee that it is of the most-derived type | |||
976 | /// for the object itself. | |||
977 | const Expr *getBestDynamicClassTypeExpr() const; | |||
978 | ||||
979 | /// Walk outwards from an expression we want to bind a reference to and | |||
980 | /// find the expression whose lifetime needs to be extended. Record | |||
981 | /// the LHSs of comma expressions and adjustments needed along the path. | |||
982 | const Expr *skipRValueSubobjectAdjustments( | |||
983 | SmallVectorImpl<const Expr *> &CommaLHS, | |||
984 | SmallVectorImpl<SubobjectAdjustment> &Adjustments) const; | |||
985 | const Expr *skipRValueSubobjectAdjustments() const { | |||
986 | SmallVector<const Expr *, 8> CommaLHSs; | |||
987 | SmallVector<SubobjectAdjustment, 8> Adjustments; | |||
988 | return skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); | |||
989 | } | |||
990 | ||||
991 | /// Checks that the two Expr's will refer to the same value as a comparison | |||
992 | /// operand. The caller must ensure that the values referenced by the Expr's | |||
993 | /// are not modified between E1 and E2 or the result my be invalid. | |||
994 | static bool isSameComparisonOperand(const Expr* E1, const Expr* E2); | |||
995 | ||||
996 | static bool classof(const Stmt *T) { | |||
997 | return T->getStmtClass() >= firstExprConstant && | |||
998 | T->getStmtClass() <= lastExprConstant; | |||
999 | } | |||
1000 | }; | |||
1001 | // PointerLikeTypeTraits is specialized so it can be used with a forward-decl of | |||
1002 | // Expr. Verify that we got it right. | |||
1003 | static_assert(llvm::PointerLikeTypeTraits<Expr *>::NumLowBitsAvailable <= | |||
1004 | llvm::detail::ConstantLog2<alignof(Expr)>::value, | |||
1005 | "PointerLikeTypeTraits<Expr*> assumes too much alignment."); | |||
1006 | ||||
1007 | using ConstantExprKind = Expr::ConstantExprKind; | |||
1008 | ||||
1009 | //===----------------------------------------------------------------------===// | |||
1010 | // Wrapper Expressions. | |||
1011 | //===----------------------------------------------------------------------===// | |||
1012 | ||||
1013 | /// FullExpr - Represents a "full-expression" node. | |||
1014 | class FullExpr : public Expr { | |||
1015 | protected: | |||
1016 | Stmt *SubExpr; | |||
1017 | ||||
1018 | FullExpr(StmtClass SC, Expr *subexpr) | |||
1019 | : Expr(SC, subexpr->getType(), subexpr->getValueKind(), | |||
1020 | subexpr->getObjectKind()), | |||
1021 | SubExpr(subexpr) { | |||
1022 | setDependence(computeDependence(this)); | |||
1023 | } | |||
1024 | FullExpr(StmtClass SC, EmptyShell Empty) | |||
1025 | : Expr(SC, Empty) {} | |||
1026 | public: | |||
1027 | const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } | |||
1028 | Expr *getSubExpr() { return cast<Expr>(SubExpr); } | |||
1029 | ||||
1030 | /// As with any mutator of the AST, be very careful when modifying an | |||
1031 | /// existing AST to preserve its invariants. | |||
1032 | void setSubExpr(Expr *E) { SubExpr = E; } | |||
1033 | ||||
1034 | static bool classof(const Stmt *T) { | |||
1035 | return T->getStmtClass() >= firstFullExprConstant && | |||
1036 | T->getStmtClass() <= lastFullExprConstant; | |||
1037 | } | |||
1038 | }; | |||
1039 | ||||
1040 | /// ConstantExpr - An expression that occurs in a constant context and | |||
1041 | /// optionally the result of evaluating the expression. | |||
1042 | class ConstantExpr final | |||
1043 | : public FullExpr, | |||
1044 | private llvm::TrailingObjects<ConstantExpr, APValue, uint64_t> { | |||
1045 | static_assert(std::is_same<uint64_t, llvm::APInt::WordType>::value, | |||
1046 | "ConstantExpr assumes that llvm::APInt::WordType is uint64_t " | |||
1047 | "for tail-allocated storage"); | |||
1048 | friend TrailingObjects; | |||
1049 | friend class ASTStmtReader; | |||
1050 | friend class ASTStmtWriter; | |||
1051 | ||||
1052 | public: | |||
1053 | /// Describes the kind of result that can be tail-allocated. | |||
1054 | enum ResultStorageKind { RSK_None, RSK_Int64, RSK_APValue }; | |||
1055 | ||||
1056 | private: | |||
1057 | size_t numTrailingObjects(OverloadToken<APValue>) const { | |||
1058 | return ConstantExprBits.ResultKind == ConstantExpr::RSK_APValue; | |||
1059 | } | |||
1060 | size_t numTrailingObjects(OverloadToken<uint64_t>) const { | |||
1061 | return ConstantExprBits.ResultKind == ConstantExpr::RSK_Int64; | |||
1062 | } | |||
1063 | ||||
1064 | uint64_t &Int64Result() { | |||
1065 | assert(ConstantExprBits.ResultKind == ConstantExpr::RSK_Int64 &&(static_cast <bool> (ConstantExprBits.ResultKind == ConstantExpr ::RSK_Int64 && "invalid accessor") ? void (0) : __assert_fail ("ConstantExprBits.ResultKind == ConstantExpr::RSK_Int64 && \"invalid accessor\"" , "clang/include/clang/AST/Expr.h", 1066, __extension__ __PRETTY_FUNCTION__ )) | |||
1066 | "invalid accessor")(static_cast <bool> (ConstantExprBits.ResultKind == ConstantExpr ::RSK_Int64 && "invalid accessor") ? void (0) : __assert_fail ("ConstantExprBits.ResultKind == ConstantExpr::RSK_Int64 && \"invalid accessor\"" , "clang/include/clang/AST/Expr.h", 1066, __extension__ __PRETTY_FUNCTION__ )); | |||
1067 | return *getTrailingObjects<uint64_t>(); | |||
1068 | } | |||
1069 | const uint64_t &Int64Result() const { | |||
1070 | return const_cast<ConstantExpr *>(this)->Int64Result(); | |||
1071 | } | |||
1072 | APValue &APValueResult() { | |||
1073 | assert(ConstantExprBits.ResultKind == ConstantExpr::RSK_APValue &&(static_cast <bool> (ConstantExprBits.ResultKind == ConstantExpr ::RSK_APValue && "invalid accessor") ? void (0) : __assert_fail ("ConstantExprBits.ResultKind == ConstantExpr::RSK_APValue && \"invalid accessor\"" , "clang/include/clang/AST/Expr.h", 1074, __extension__ __PRETTY_FUNCTION__ )) | |||
1074 | "invalid accessor")(static_cast <bool> (ConstantExprBits.ResultKind == ConstantExpr ::RSK_APValue && "invalid accessor") ? void (0) : __assert_fail ("ConstantExprBits.ResultKind == ConstantExpr::RSK_APValue && \"invalid accessor\"" , "clang/include/clang/AST/Expr.h", 1074, __extension__ __PRETTY_FUNCTION__ )); | |||
1075 | return *getTrailingObjects<APValue>(); | |||
1076 | } | |||
1077 | APValue &APValueResult() const { | |||
1078 | return const_cast<ConstantExpr *>(this)->APValueResult(); | |||
1079 | } | |||
1080 | ||||
1081 | ConstantExpr(Expr *SubExpr, ResultStorageKind StorageKind, | |||
1082 | bool IsImmediateInvocation); | |||
1083 | ConstantExpr(EmptyShell Empty, ResultStorageKind StorageKind); | |||
1084 | ||||
1085 | public: | |||
1086 | static ConstantExpr *Create(const ASTContext &Context, Expr *E, | |||
1087 | const APValue &Result); | |||
1088 | static ConstantExpr *Create(const ASTContext &Context, Expr *E, | |||
1089 | ResultStorageKind Storage = RSK_None, | |||
1090 | bool IsImmediateInvocation = false); | |||
1091 | static ConstantExpr *CreateEmpty(const ASTContext &Context, | |||
1092 | ResultStorageKind StorageKind); | |||
1093 | ||||
1094 | static ResultStorageKind getStorageKind(const APValue &Value); | |||
1095 | static ResultStorageKind getStorageKind(const Type *T, | |||
1096 | const ASTContext &Context); | |||
1097 | ||||
1098 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
1099 | return SubExpr->getBeginLoc(); | |||
1100 | } | |||
1101 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
1102 | return SubExpr->getEndLoc(); | |||
1103 | } | |||
1104 | ||||
1105 | static bool classof(const Stmt *T) { | |||
1106 | return T->getStmtClass() == ConstantExprClass; | |||
1107 | } | |||
1108 | ||||
1109 | void SetResult(APValue Value, const ASTContext &Context) { | |||
1110 | MoveIntoResult(Value, Context); | |||
1111 | } | |||
1112 | void MoveIntoResult(APValue &Value, const ASTContext &Context); | |||
1113 | ||||
1114 | APValue::ValueKind getResultAPValueKind() const { | |||
1115 | return static_cast<APValue::ValueKind>(ConstantExprBits.APValueKind); | |||
1116 | } | |||
1117 | ResultStorageKind getResultStorageKind() const { | |||
1118 | return static_cast<ResultStorageKind>(ConstantExprBits.ResultKind); | |||
1119 | } | |||
1120 | bool isImmediateInvocation() const { | |||
1121 | return ConstantExprBits.IsImmediateInvocation; | |||
1122 | } | |||
1123 | bool hasAPValueResult() const { | |||
1124 | return ConstantExprBits.APValueKind != APValue::None; | |||
1125 | } | |||
1126 | APValue getAPValueResult() const; | |||
1127 | APValue &getResultAsAPValue() const { return APValueResult(); } | |||
1128 | llvm::APSInt getResultAsAPSInt() const; | |||
1129 | // Iterators | |||
1130 | child_range children() { return child_range(&SubExpr, &SubExpr+1); } | |||
1131 | const_child_range children() const { | |||
1132 | return const_child_range(&SubExpr, &SubExpr + 1); | |||
1133 | } | |||
1134 | }; | |||
1135 | ||||
1136 | //===----------------------------------------------------------------------===// | |||
1137 | // Primary Expressions. | |||
1138 | //===----------------------------------------------------------------------===// | |||
1139 | ||||
1140 | /// OpaqueValueExpr - An expression referring to an opaque object of a | |||
1141 | /// fixed type and value class. These don't correspond to concrete | |||
1142 | /// syntax; instead they're used to express operations (usually copy | |||
1143 | /// operations) on values whose source is generally obvious from | |||
1144 | /// context. | |||
1145 | class OpaqueValueExpr : public Expr { | |||
1146 | friend class ASTStmtReader; | |||
1147 | Expr *SourceExpr; | |||
1148 | ||||
1149 | public: | |||
1150 | OpaqueValueExpr(SourceLocation Loc, QualType T, ExprValueKind VK, | |||
1151 | ExprObjectKind OK = OK_Ordinary, Expr *SourceExpr = nullptr) | |||
1152 | : Expr(OpaqueValueExprClass, T, VK, OK), SourceExpr(SourceExpr) { | |||
1153 | setIsUnique(false); | |||
1154 | OpaqueValueExprBits.Loc = Loc; | |||
1155 | setDependence(computeDependence(this)); | |||
1156 | } | |||
1157 | ||||
1158 | /// Given an expression which invokes a copy constructor --- i.e. a | |||
1159 | /// CXXConstructExpr, possibly wrapped in an ExprWithCleanups --- | |||
1160 | /// find the OpaqueValueExpr that's the source of the construction. | |||
1161 | static const OpaqueValueExpr *findInCopyConstruct(const Expr *expr); | |||
1162 | ||||
1163 | explicit OpaqueValueExpr(EmptyShell Empty) | |||
1164 | : Expr(OpaqueValueExprClass, Empty) {} | |||
1165 | ||||
1166 | /// Retrieve the location of this expression. | |||
1167 | SourceLocation getLocation() const { return OpaqueValueExprBits.Loc; } | |||
1168 | ||||
1169 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
1170 | return SourceExpr ? SourceExpr->getBeginLoc() : getLocation(); | |||
1171 | } | |||
1172 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
1173 | return SourceExpr ? SourceExpr->getEndLoc() : getLocation(); | |||
1174 | } | |||
1175 | SourceLocation getExprLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
1176 | return SourceExpr ? SourceExpr->getExprLoc() : getLocation(); | |||
1177 | } | |||
1178 | ||||
1179 | child_range children() { | |||
1180 | return child_range(child_iterator(), child_iterator()); | |||
1181 | } | |||
1182 | ||||
1183 | const_child_range children() const { | |||
1184 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
1185 | } | |||
1186 | ||||
1187 | /// The source expression of an opaque value expression is the | |||
1188 | /// expression which originally generated the value. This is | |||
1189 | /// provided as a convenience for analyses that don't wish to | |||
1190 | /// precisely model the execution behavior of the program. | |||
1191 | /// | |||
1192 | /// The source expression is typically set when building the | |||
1193 | /// expression which binds the opaque value expression in the first | |||
1194 | /// place. | |||
1195 | Expr *getSourceExpr() const { return SourceExpr; } | |||
1196 | ||||
1197 | void setIsUnique(bool V) { | |||
1198 | assert((!V || SourceExpr) &&(static_cast <bool> ((!V || SourceExpr) && "unique OVEs are expected to have source expressions" ) ? void (0) : __assert_fail ("(!V || SourceExpr) && \"unique OVEs are expected to have source expressions\"" , "clang/include/clang/AST/Expr.h", 1199, __extension__ __PRETTY_FUNCTION__ )) | |||
1199 | "unique OVEs are expected to have source expressions")(static_cast <bool> ((!V || SourceExpr) && "unique OVEs are expected to have source expressions" ) ? void (0) : __assert_fail ("(!V || SourceExpr) && \"unique OVEs are expected to have source expressions\"" , "clang/include/clang/AST/Expr.h", 1199, __extension__ __PRETTY_FUNCTION__ )); | |||
1200 | OpaqueValueExprBits.IsUnique = V; | |||
1201 | } | |||
1202 | ||||
1203 | bool isUnique() const { return OpaqueValueExprBits.IsUnique; } | |||
1204 | ||||
1205 | static bool classof(const Stmt *T) { | |||
1206 | return T->getStmtClass() == OpaqueValueExprClass; | |||
1207 | } | |||
1208 | }; | |||
1209 | ||||
1210 | /// A reference to a declared variable, function, enum, etc. | |||
1211 | /// [C99 6.5.1p2] | |||
1212 | /// | |||
1213 | /// This encodes all the information about how a declaration is referenced | |||
1214 | /// within an expression. | |||
1215 | /// | |||
1216 | /// There are several optional constructs attached to DeclRefExprs only when | |||
1217 | /// they apply in order to conserve memory. These are laid out past the end of | |||
1218 | /// the object, and flags in the DeclRefExprBitfield track whether they exist: | |||
1219 | /// | |||
1220 | /// DeclRefExprBits.HasQualifier: | |||
1221 | /// Specifies when this declaration reference expression has a C++ | |||
1222 | /// nested-name-specifier. | |||
1223 | /// DeclRefExprBits.HasFoundDecl: | |||
1224 | /// Specifies when this declaration reference expression has a record of | |||
1225 | /// a NamedDecl (different from the referenced ValueDecl) which was found | |||
1226 | /// during name lookup and/or overload resolution. | |||
1227 | /// DeclRefExprBits.HasTemplateKWAndArgsInfo: | |||
1228 | /// Specifies when this declaration reference expression has an explicit | |||
1229 | /// C++ template keyword and/or template argument list. | |||
1230 | /// DeclRefExprBits.RefersToEnclosingVariableOrCapture | |||
1231 | /// Specifies when this declaration reference expression (validly) | |||
1232 | /// refers to an enclosed local or a captured variable. | |||
1233 | class DeclRefExpr final | |||
1234 | : public Expr, | |||
1235 | private llvm::TrailingObjects<DeclRefExpr, NestedNameSpecifierLoc, | |||
1236 | NamedDecl *, ASTTemplateKWAndArgsInfo, | |||
1237 | TemplateArgumentLoc> { | |||
1238 | friend class ASTStmtReader; | |||
1239 | friend class ASTStmtWriter; | |||
1240 | friend TrailingObjects; | |||
1241 | ||||
1242 | /// The declaration that we are referencing. | |||
1243 | ValueDecl *D; | |||
1244 | ||||
1245 | /// Provides source/type location info for the declaration name | |||
1246 | /// embedded in D. | |||
1247 | DeclarationNameLoc DNLoc; | |||
1248 | ||||
1249 | size_t numTrailingObjects(OverloadToken<NestedNameSpecifierLoc>) const { | |||
1250 | return hasQualifier(); | |||
1251 | } | |||
1252 | ||||
1253 | size_t numTrailingObjects(OverloadToken<NamedDecl *>) const { | |||
1254 | return hasFoundDecl(); | |||
1255 | } | |||
1256 | ||||
1257 | size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const { | |||
1258 | return hasTemplateKWAndArgsInfo(); | |||
1259 | } | |||
1260 | ||||
1261 | /// Test whether there is a distinct FoundDecl attached to the end of | |||
1262 | /// this DRE. | |||
1263 | bool hasFoundDecl() const { return DeclRefExprBits.HasFoundDecl; } | |||
1264 | ||||
1265 | DeclRefExpr(const ASTContext &Ctx, NestedNameSpecifierLoc QualifierLoc, | |||
1266 | SourceLocation TemplateKWLoc, ValueDecl *D, | |||
1267 | bool RefersToEnlosingVariableOrCapture, | |||
1268 | const DeclarationNameInfo &NameInfo, NamedDecl *FoundD, | |||
1269 | const TemplateArgumentListInfo *TemplateArgs, QualType T, | |||
1270 | ExprValueKind VK, NonOdrUseReason NOUR); | |||
1271 | ||||
1272 | /// Construct an empty declaration reference expression. | |||
1273 | explicit DeclRefExpr(EmptyShell Empty) : Expr(DeclRefExprClass, Empty) {} | |||
1274 | ||||
1275 | public: | |||
1276 | DeclRefExpr(const ASTContext &Ctx, ValueDecl *D, | |||
1277 | bool RefersToEnclosingVariableOrCapture, QualType T, | |||
1278 | ExprValueKind VK, SourceLocation L, | |||
1279 | const DeclarationNameLoc &LocInfo = DeclarationNameLoc(), | |||
1280 | NonOdrUseReason NOUR = NOUR_None); | |||
1281 | ||||
1282 | static DeclRefExpr * | |||
1283 | Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, | |||
1284 | SourceLocation TemplateKWLoc, ValueDecl *D, | |||
1285 | bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc, | |||
1286 | QualType T, ExprValueKind VK, NamedDecl *FoundD = nullptr, | |||
1287 | const TemplateArgumentListInfo *TemplateArgs = nullptr, | |||
1288 | NonOdrUseReason NOUR = NOUR_None); | |||
1289 | ||||
1290 | static DeclRefExpr * | |||
1291 | Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, | |||
1292 | SourceLocation TemplateKWLoc, ValueDecl *D, | |||
1293 | bool RefersToEnclosingVariableOrCapture, | |||
1294 | const DeclarationNameInfo &NameInfo, QualType T, ExprValueKind VK, | |||
1295 | NamedDecl *FoundD = nullptr, | |||
1296 | const TemplateArgumentListInfo *TemplateArgs = nullptr, | |||
1297 | NonOdrUseReason NOUR = NOUR_None); | |||
1298 | ||||
1299 | /// Construct an empty declaration reference expression. | |||
1300 | static DeclRefExpr *CreateEmpty(const ASTContext &Context, bool HasQualifier, | |||
1301 | bool HasFoundDecl, | |||
1302 | bool HasTemplateKWAndArgsInfo, | |||
1303 | unsigned NumTemplateArgs); | |||
1304 | ||||
1305 | ValueDecl *getDecl() { return D; } | |||
1306 | const ValueDecl *getDecl() const { return D; } | |||
1307 | void setDecl(ValueDecl *NewD); | |||
1308 | ||||
1309 | DeclarationNameInfo getNameInfo() const { | |||
1310 | return DeclarationNameInfo(getDecl()->getDeclName(), getLocation(), DNLoc); | |||
1311 | } | |||
1312 | ||||
1313 | SourceLocation getLocation() const { return DeclRefExprBits.Loc; } | |||
1314 | void setLocation(SourceLocation L) { DeclRefExprBits.Loc = L; } | |||
1315 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
1316 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
1317 | ||||
1318 | /// Determine whether this declaration reference was preceded by a | |||
1319 | /// C++ nested-name-specifier, e.g., \c N::foo. | |||
1320 | bool hasQualifier() const { return DeclRefExprBits.HasQualifier; } | |||
1321 | ||||
1322 | /// If the name was qualified, retrieves the nested-name-specifier | |||
1323 | /// that precedes the name, with source-location information. | |||
1324 | NestedNameSpecifierLoc getQualifierLoc() const { | |||
1325 | if (!hasQualifier()) | |||
1326 | return NestedNameSpecifierLoc(); | |||
1327 | return *getTrailingObjects<NestedNameSpecifierLoc>(); | |||
1328 | } | |||
1329 | ||||
1330 | /// If the name was qualified, retrieves the nested-name-specifier | |||
1331 | /// that precedes the name. Otherwise, returns NULL. | |||
1332 | NestedNameSpecifier *getQualifier() const { | |||
1333 | return getQualifierLoc().getNestedNameSpecifier(); | |||
1334 | } | |||
1335 | ||||
1336 | /// Get the NamedDecl through which this reference occurred. | |||
1337 | /// | |||
1338 | /// This Decl may be different from the ValueDecl actually referred to in the | |||
1339 | /// presence of using declarations, etc. It always returns non-NULL, and may | |||
1340 | /// simple return the ValueDecl when appropriate. | |||
1341 | ||||
1342 | NamedDecl *getFoundDecl() { | |||
1343 | return hasFoundDecl() ? *getTrailingObjects<NamedDecl *>() : D; | |||
1344 | } | |||
1345 | ||||
1346 | /// Get the NamedDecl through which this reference occurred. | |||
1347 | /// See non-const variant. | |||
1348 | const NamedDecl *getFoundDecl() const { | |||
1349 | return hasFoundDecl() ? *getTrailingObjects<NamedDecl *>() : D; | |||
1350 | } | |||
1351 | ||||
1352 | bool hasTemplateKWAndArgsInfo() const { | |||
1353 | return DeclRefExprBits.HasTemplateKWAndArgsInfo; | |||
1354 | } | |||
1355 | ||||
1356 | /// Retrieve the location of the template keyword preceding | |||
1357 | /// this name, if any. | |||
1358 | SourceLocation getTemplateKeywordLoc() const { | |||
1359 | if (!hasTemplateKWAndArgsInfo()) | |||
1360 | return SourceLocation(); | |||
1361 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc; | |||
1362 | } | |||
1363 | ||||
1364 | /// Retrieve the location of the left angle bracket starting the | |||
1365 | /// explicit template argument list following the name, if any. | |||
1366 | SourceLocation getLAngleLoc() const { | |||
1367 | if (!hasTemplateKWAndArgsInfo()) | |||
1368 | return SourceLocation(); | |||
1369 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc; | |||
1370 | } | |||
1371 | ||||
1372 | /// Retrieve the location of the right angle bracket ending the | |||
1373 | /// explicit template argument list following the name, if any. | |||
1374 | SourceLocation getRAngleLoc() const { | |||
1375 | if (!hasTemplateKWAndArgsInfo()) | |||
1376 | return SourceLocation(); | |||
1377 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc; | |||
1378 | } | |||
1379 | ||||
1380 | /// Determines whether the name in this declaration reference | |||
1381 | /// was preceded by the template keyword. | |||
1382 | bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } | |||
1383 | ||||
1384 | /// Determines whether this declaration reference was followed by an | |||
1385 | /// explicit template argument list. | |||
1386 | bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } | |||
1387 | ||||
1388 | /// Copies the template arguments (if present) into the given | |||
1389 | /// structure. | |||
1390 | void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { | |||
1391 | if (hasExplicitTemplateArgs()) | |||
1392 | getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto( | |||
1393 | getTrailingObjects<TemplateArgumentLoc>(), List); | |||
1394 | } | |||
1395 | ||||
1396 | /// Retrieve the template arguments provided as part of this | |||
1397 | /// template-id. | |||
1398 | const TemplateArgumentLoc *getTemplateArgs() const { | |||
1399 | if (!hasExplicitTemplateArgs()) | |||
1400 | return nullptr; | |||
1401 | return getTrailingObjects<TemplateArgumentLoc>(); | |||
1402 | } | |||
1403 | ||||
1404 | /// Retrieve the number of template arguments provided as part of this | |||
1405 | /// template-id. | |||
1406 | unsigned getNumTemplateArgs() const { | |||
1407 | if (!hasExplicitTemplateArgs()) | |||
1408 | return 0; | |||
1409 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs; | |||
1410 | } | |||
1411 | ||||
1412 | ArrayRef<TemplateArgumentLoc> template_arguments() const { | |||
1413 | return {getTemplateArgs(), getNumTemplateArgs()}; | |||
1414 | } | |||
1415 | ||||
1416 | /// Returns true if this expression refers to a function that | |||
1417 | /// was resolved from an overloaded set having size greater than 1. | |||
1418 | bool hadMultipleCandidates() const { | |||
1419 | return DeclRefExprBits.HadMultipleCandidates; | |||
1420 | } | |||
1421 | /// Sets the flag telling whether this expression refers to | |||
1422 | /// a function that was resolved from an overloaded set having size | |||
1423 | /// greater than 1. | |||
1424 | void setHadMultipleCandidates(bool V = true) { | |||
1425 | DeclRefExprBits.HadMultipleCandidates = V; | |||
1426 | } | |||
1427 | ||||
1428 | /// Is this expression a non-odr-use reference, and if so, why? | |||
1429 | NonOdrUseReason isNonOdrUse() const { | |||
1430 | return static_cast<NonOdrUseReason>(DeclRefExprBits.NonOdrUseReason); | |||
1431 | } | |||
1432 | ||||
1433 | /// Does this DeclRefExpr refer to an enclosing local or a captured | |||
1434 | /// variable? | |||
1435 | bool refersToEnclosingVariableOrCapture() const { | |||
1436 | return DeclRefExprBits.RefersToEnclosingVariableOrCapture; | |||
1437 | } | |||
1438 | ||||
1439 | static bool classof(const Stmt *T) { | |||
1440 | return T->getStmtClass() == DeclRefExprClass; | |||
1441 | } | |||
1442 | ||||
1443 | // Iterators | |||
1444 | child_range children() { | |||
1445 | return child_range(child_iterator(), child_iterator()); | |||
1446 | } | |||
1447 | ||||
1448 | const_child_range children() const { | |||
1449 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
1450 | } | |||
1451 | }; | |||
1452 | ||||
1453 | /// Used by IntegerLiteral/FloatingLiteral to store the numeric without | |||
1454 | /// leaking memory. | |||
1455 | /// | |||
1456 | /// For large floats/integers, APFloat/APInt will allocate memory from the heap | |||
1457 | /// to represent these numbers. Unfortunately, when we use a BumpPtrAllocator | |||
1458 | /// to allocate IntegerLiteral/FloatingLiteral nodes the memory associated with | |||
1459 | /// the APFloat/APInt values will never get freed. APNumericStorage uses | |||
1460 | /// ASTContext's allocator for memory allocation. | |||
1461 | class APNumericStorage { | |||
1462 | union { | |||
1463 | uint64_t VAL; ///< Used to store the <= 64 bits integer value. | |||
1464 | uint64_t *pVal; ///< Used to store the >64 bits integer value. | |||
1465 | }; | |||
1466 | unsigned BitWidth; | |||
1467 | ||||
1468 | bool hasAllocation() const { return llvm::APInt::getNumWords(BitWidth) > 1; } | |||
1469 | ||||
1470 | APNumericStorage(const APNumericStorage &) = delete; | |||
1471 | void operator=(const APNumericStorage &) = delete; | |||
1472 | ||||
1473 | protected: | |||
1474 | APNumericStorage() : VAL(0), BitWidth(0) { } | |||
1475 | ||||
1476 | llvm::APInt getIntValue() const { | |||
1477 | unsigned NumWords = llvm::APInt::getNumWords(BitWidth); | |||
1478 | if (NumWords > 1) | |||
1479 | return llvm::APInt(BitWidth, NumWords, pVal); | |||
1480 | else | |||
1481 | return llvm::APInt(BitWidth, VAL); | |||
1482 | } | |||
1483 | void setIntValue(const ASTContext &C, const llvm::APInt &Val); | |||
1484 | }; | |||
1485 | ||||
1486 | class APIntStorage : private APNumericStorage { | |||
1487 | public: | |||
1488 | llvm::APInt getValue() const { return getIntValue(); } | |||
1489 | void setValue(const ASTContext &C, const llvm::APInt &Val) { | |||
1490 | setIntValue(C, Val); | |||
1491 | } | |||
1492 | }; | |||
1493 | ||||
1494 | class APFloatStorage : private APNumericStorage { | |||
1495 | public: | |||
1496 | llvm::APFloat getValue(const llvm::fltSemantics &Semantics) const { | |||
1497 | return llvm::APFloat(Semantics, getIntValue()); | |||
1498 | } | |||
1499 | void setValue(const ASTContext &C, const llvm::APFloat &Val) { | |||
1500 | setIntValue(C, Val.bitcastToAPInt()); | |||
1501 | } | |||
1502 | }; | |||
1503 | ||||
1504 | class IntegerLiteral : public Expr, public APIntStorage { | |||
1505 | SourceLocation Loc; | |||
1506 | ||||
1507 | /// Construct an empty integer literal. | |||
1508 | explicit IntegerLiteral(EmptyShell Empty) | |||
1509 | : Expr(IntegerLiteralClass, Empty) { } | |||
1510 | ||||
1511 | public: | |||
1512 | // type should be IntTy, LongTy, LongLongTy, UnsignedIntTy, UnsignedLongTy, | |||
1513 | // or UnsignedLongLongTy | |||
1514 | IntegerLiteral(const ASTContext &C, const llvm::APInt &V, QualType type, | |||
1515 | SourceLocation l); | |||
1516 | ||||
1517 | /// Returns a new integer literal with value 'V' and type 'type'. | |||
1518 | /// \param type - either IntTy, LongTy, LongLongTy, UnsignedIntTy, | |||
1519 | /// UnsignedLongTy, or UnsignedLongLongTy which should match the size of V | |||
1520 | /// \param V - the value that the returned integer literal contains. | |||
1521 | static IntegerLiteral *Create(const ASTContext &C, const llvm::APInt &V, | |||
1522 | QualType type, SourceLocation l); | |||
1523 | /// Returns a new empty integer literal. | |||
1524 | static IntegerLiteral *Create(const ASTContext &C, EmptyShell Empty); | |||
1525 | ||||
1526 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return Loc; } | |||
1527 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return Loc; } | |||
1528 | ||||
1529 | /// Retrieve the location of the literal. | |||
1530 | SourceLocation getLocation() const { return Loc; } | |||
1531 | ||||
1532 | void setLocation(SourceLocation Location) { Loc = Location; } | |||
1533 | ||||
1534 | static bool classof(const Stmt *T) { | |||
1535 | return T->getStmtClass() == IntegerLiteralClass; | |||
1536 | } | |||
1537 | ||||
1538 | // Iterators | |||
1539 | child_range children() { | |||
1540 | return child_range(child_iterator(), child_iterator()); | |||
1541 | } | |||
1542 | const_child_range children() const { | |||
1543 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
1544 | } | |||
1545 | }; | |||
1546 | ||||
1547 | class FixedPointLiteral : public Expr, public APIntStorage { | |||
1548 | SourceLocation Loc; | |||
1549 | unsigned Scale; | |||
1550 | ||||
1551 | /// \brief Construct an empty fixed-point literal. | |||
1552 | explicit FixedPointLiteral(EmptyShell Empty) | |||
1553 | : Expr(FixedPointLiteralClass, Empty) {} | |||
1554 | ||||
1555 | public: | |||
1556 | FixedPointLiteral(const ASTContext &C, const llvm::APInt &V, QualType type, | |||
1557 | SourceLocation l, unsigned Scale); | |||
1558 | ||||
1559 | // Store the int as is without any bit shifting. | |||
1560 | static FixedPointLiteral *CreateFromRawInt(const ASTContext &C, | |||
1561 | const llvm::APInt &V, | |||
1562 | QualType type, SourceLocation l, | |||
1563 | unsigned Scale); | |||
1564 | ||||
1565 | /// Returns an empty fixed-point literal. | |||
1566 | static FixedPointLiteral *Create(const ASTContext &C, EmptyShell Empty); | |||
1567 | ||||
1568 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return Loc; } | |||
1569 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return Loc; } | |||
1570 | ||||
1571 | /// \brief Retrieve the location of the literal. | |||
1572 | SourceLocation getLocation() const { return Loc; } | |||
1573 | ||||
1574 | void setLocation(SourceLocation Location) { Loc = Location; } | |||
1575 | ||||
1576 | unsigned getScale() const { return Scale; } | |||
1577 | void setScale(unsigned S) { Scale = S; } | |||
1578 | ||||
1579 | static bool classof(const Stmt *T) { | |||
1580 | return T->getStmtClass() == FixedPointLiteralClass; | |||
1581 | } | |||
1582 | ||||
1583 | std::string getValueAsString(unsigned Radix) const; | |||
1584 | ||||
1585 | // Iterators | |||
1586 | child_range children() { | |||
1587 | return child_range(child_iterator(), child_iterator()); | |||
1588 | } | |||
1589 | const_child_range children() const { | |||
1590 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
1591 | } | |||
1592 | }; | |||
1593 | ||||
1594 | class CharacterLiteral : public Expr { | |||
1595 | public: | |||
1596 | enum CharacterKind { | |||
1597 | Ascii, | |||
1598 | Wide, | |||
1599 | UTF8, | |||
1600 | UTF16, | |||
1601 | UTF32 | |||
1602 | }; | |||
1603 | ||||
1604 | private: | |||
1605 | unsigned Value; | |||
1606 | SourceLocation Loc; | |||
1607 | public: | |||
1608 | // type should be IntTy | |||
1609 | CharacterLiteral(unsigned value, CharacterKind kind, QualType type, | |||
1610 | SourceLocation l) | |||
1611 | : Expr(CharacterLiteralClass, type, VK_PRValue, OK_Ordinary), | |||
1612 | Value(value), Loc(l) { | |||
1613 | CharacterLiteralBits.Kind = kind; | |||
1614 | setDependence(ExprDependence::None); | |||
1615 | } | |||
1616 | ||||
1617 | /// Construct an empty character literal. | |||
1618 | CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { } | |||
1619 | ||||
1620 | SourceLocation getLocation() const { return Loc; } | |||
1621 | CharacterKind getKind() const { | |||
1622 | return static_cast<CharacterKind>(CharacterLiteralBits.Kind); | |||
1623 | } | |||
1624 | ||||
1625 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return Loc; } | |||
1626 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return Loc; } | |||
1627 | ||||
1628 | unsigned getValue() const { return Value; } | |||
1629 | ||||
1630 | void setLocation(SourceLocation Location) { Loc = Location; } | |||
1631 | void setKind(CharacterKind kind) { CharacterLiteralBits.Kind = kind; } | |||
1632 | void setValue(unsigned Val) { Value = Val; } | |||
1633 | ||||
1634 | static bool classof(const Stmt *T) { | |||
1635 | return T->getStmtClass() == CharacterLiteralClass; | |||
1636 | } | |||
1637 | ||||
1638 | static void print(unsigned val, CharacterKind Kind, raw_ostream &OS); | |||
1639 | ||||
1640 | // Iterators | |||
1641 | child_range children() { | |||
1642 | return child_range(child_iterator(), child_iterator()); | |||
1643 | } | |||
1644 | const_child_range children() const { | |||
1645 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
1646 | } | |||
1647 | }; | |||
1648 | ||||
1649 | class FloatingLiteral : public Expr, private APFloatStorage { | |||
1650 | SourceLocation Loc; | |||
1651 | ||||
1652 | FloatingLiteral(const ASTContext &C, const llvm::APFloat &V, bool isexact, | |||
1653 | QualType Type, SourceLocation L); | |||
1654 | ||||
1655 | /// Construct an empty floating-point literal. | |||
1656 | explicit FloatingLiteral(const ASTContext &C, EmptyShell Empty); | |||
1657 | ||||
1658 | public: | |||
1659 | static FloatingLiteral *Create(const ASTContext &C, const llvm::APFloat &V, | |||
1660 | bool isexact, QualType Type, SourceLocation L); | |||
1661 | static FloatingLiteral *Create(const ASTContext &C, EmptyShell Empty); | |||
1662 | ||||
1663 | llvm::APFloat getValue() const { | |||
1664 | return APFloatStorage::getValue(getSemantics()); | |||
1665 | } | |||
1666 | void setValue(const ASTContext &C, const llvm::APFloat &Val) { | |||
1667 | assert(&getSemantics() == &Val.getSemantics() && "Inconsistent semantics")(static_cast <bool> (&getSemantics() == &Val.getSemantics () && "Inconsistent semantics") ? void (0) : __assert_fail ("&getSemantics() == &Val.getSemantics() && \"Inconsistent semantics\"" , "clang/include/clang/AST/Expr.h", 1667, __extension__ __PRETTY_FUNCTION__ )); | |||
1668 | APFloatStorage::setValue(C, Val); | |||
1669 | } | |||
1670 | ||||
1671 | /// Get a raw enumeration value representing the floating-point semantics of | |||
1672 | /// this literal (32-bit IEEE, x87, ...), suitable for serialisation. | |||
1673 | llvm::APFloatBase::Semantics getRawSemantics() const { | |||
1674 | return static_cast<llvm::APFloatBase::Semantics>( | |||
1675 | FloatingLiteralBits.Semantics); | |||
1676 | } | |||
1677 | ||||
1678 | /// Set the raw enumeration value representing the floating-point semantics of | |||
1679 | /// this literal (32-bit IEEE, x87, ...), suitable for serialisation. | |||
1680 | void setRawSemantics(llvm::APFloatBase::Semantics Sem) { | |||
1681 | FloatingLiteralBits.Semantics = Sem; | |||
1682 | } | |||
1683 | ||||
1684 | /// Return the APFloat semantics this literal uses. | |||
1685 | const llvm::fltSemantics &getSemantics() const { | |||
1686 | return llvm::APFloatBase::EnumToSemantics( | |||
1687 | static_cast<llvm::APFloatBase::Semantics>( | |||
1688 | FloatingLiteralBits.Semantics)); | |||
1689 | } | |||
1690 | ||||
1691 | /// Set the APFloat semantics this literal uses. | |||
1692 | void setSemantics(const llvm::fltSemantics &Sem) { | |||
1693 | FloatingLiteralBits.Semantics = llvm::APFloatBase::SemanticsToEnum(Sem); | |||
1694 | } | |||
1695 | ||||
1696 | bool isExact() const { return FloatingLiteralBits.IsExact; } | |||
1697 | void setExact(bool E) { FloatingLiteralBits.IsExact = E; } | |||
1698 | ||||
1699 | /// getValueAsApproximateDouble - This returns the value as an inaccurate | |||
1700 | /// double. Note that this may cause loss of precision, but is useful for | |||
1701 | /// debugging dumps, etc. | |||
1702 | double getValueAsApproximateDouble() const; | |||
1703 | ||||
1704 | SourceLocation getLocation() const { return Loc; } | |||
1705 | void setLocation(SourceLocation L) { Loc = L; } | |||
1706 | ||||
1707 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return Loc; } | |||
1708 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return Loc; } | |||
1709 | ||||
1710 | static bool classof(const Stmt *T) { | |||
1711 | return T->getStmtClass() == FloatingLiteralClass; | |||
1712 | } | |||
1713 | ||||
1714 | // Iterators | |||
1715 | child_range children() { | |||
1716 | return child_range(child_iterator(), child_iterator()); | |||
1717 | } | |||
1718 | const_child_range children() const { | |||
1719 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
1720 | } | |||
1721 | }; | |||
1722 | ||||
1723 | /// ImaginaryLiteral - We support imaginary integer and floating point literals, | |||
1724 | /// like "1.0i". We represent these as a wrapper around FloatingLiteral and | |||
1725 | /// IntegerLiteral classes. Instances of this class always have a Complex type | |||
1726 | /// whose element type matches the subexpression. | |||
1727 | /// | |||
1728 | class ImaginaryLiteral : public Expr { | |||
1729 | Stmt *Val; | |||
1730 | public: | |||
1731 | ImaginaryLiteral(Expr *val, QualType Ty) | |||
1732 | : Expr(ImaginaryLiteralClass, Ty, VK_PRValue, OK_Ordinary), Val(val) { | |||
1733 | setDependence(ExprDependence::None); | |||
1734 | } | |||
1735 | ||||
1736 | /// Build an empty imaginary literal. | |||
1737 | explicit ImaginaryLiteral(EmptyShell Empty) | |||
1738 | : Expr(ImaginaryLiteralClass, Empty) { } | |||
1739 | ||||
1740 | const Expr *getSubExpr() const { return cast<Expr>(Val); } | |||
1741 | Expr *getSubExpr() { return cast<Expr>(Val); } | |||
1742 | void setSubExpr(Expr *E) { Val = E; } | |||
1743 | ||||
1744 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
1745 | return Val->getBeginLoc(); | |||
1746 | } | |||
1747 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return Val->getEndLoc(); } | |||
1748 | ||||
1749 | static bool classof(const Stmt *T) { | |||
1750 | return T->getStmtClass() == ImaginaryLiteralClass; | |||
1751 | } | |||
1752 | ||||
1753 | // Iterators | |||
1754 | child_range children() { return child_range(&Val, &Val+1); } | |||
1755 | const_child_range children() const { | |||
1756 | return const_child_range(&Val, &Val + 1); | |||
1757 | } | |||
1758 | }; | |||
1759 | ||||
1760 | /// StringLiteral - This represents a string literal expression, e.g. "foo" | |||
1761 | /// or L"bar" (wide strings). The actual string data can be obtained with | |||
1762 | /// getBytes() and is NOT null-terminated. The length of the string data is | |||
1763 | /// determined by calling getByteLength(). | |||
1764 | /// | |||
1765 | /// The C type for a string is always a ConstantArrayType. In C++, the char | |||
1766 | /// type is const qualified, in C it is not. | |||
1767 | /// | |||
1768 | /// Note that strings in C can be formed by concatenation of multiple string | |||
1769 | /// literal pptokens in translation phase #6. This keeps track of the locations | |||
1770 | /// of each of these pieces. | |||
1771 | /// | |||
1772 | /// Strings in C can also be truncated and extended by assigning into arrays, | |||
1773 | /// e.g. with constructs like: | |||
1774 | /// char X[2] = "foobar"; | |||
1775 | /// In this case, getByteLength() will return 6, but the string literal will | |||
1776 | /// have type "char[2]". | |||
1777 | class StringLiteral final | |||
1778 | : public Expr, | |||
1779 | private llvm::TrailingObjects<StringLiteral, unsigned, SourceLocation, | |||
1780 | char> { | |||
1781 | friend class ASTStmtReader; | |||
1782 | friend TrailingObjects; | |||
1783 | ||||
1784 | /// StringLiteral is followed by several trailing objects. They are in order: | |||
1785 | /// | |||
1786 | /// * A single unsigned storing the length in characters of this string. The | |||
1787 | /// length in bytes is this length times the width of a single character. | |||
1788 | /// Always present and stored as a trailing objects because storing it in | |||
1789 | /// StringLiteral would increase the size of StringLiteral by sizeof(void *) | |||
1790 | /// due to alignment requirements. If you add some data to StringLiteral, | |||
1791 | /// consider moving it inside StringLiteral. | |||
1792 | /// | |||
1793 | /// * An array of getNumConcatenated() SourceLocation, one for each of the | |||
1794 | /// token this string is made of. | |||
1795 | /// | |||
1796 | /// * An array of getByteLength() char used to store the string data. | |||
1797 | ||||
1798 | public: | |||
1799 | enum StringKind { Ordinary, Wide, UTF8, UTF16, UTF32 }; | |||
1800 | ||||
1801 | private: | |||
1802 | unsigned numTrailingObjects(OverloadToken<unsigned>) const { return 1; } | |||
1803 | unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { | |||
1804 | return getNumConcatenated(); | |||
1805 | } | |||
1806 | ||||
1807 | unsigned numTrailingObjects(OverloadToken<char>) const { | |||
1808 | return getByteLength(); | |||
1809 | } | |||
1810 | ||||
1811 | char *getStrDataAsChar() { return getTrailingObjects<char>(); } | |||
1812 | const char *getStrDataAsChar() const { return getTrailingObjects<char>(); } | |||
1813 | ||||
1814 | const uint16_t *getStrDataAsUInt16() const { | |||
1815 | return reinterpret_cast<const uint16_t *>(getTrailingObjects<char>()); | |||
1816 | } | |||
1817 | ||||
1818 | const uint32_t *getStrDataAsUInt32() const { | |||
1819 | return reinterpret_cast<const uint32_t *>(getTrailingObjects<char>()); | |||
1820 | } | |||
1821 | ||||
1822 | /// Build a string literal. | |||
1823 | StringLiteral(const ASTContext &Ctx, StringRef Str, StringKind Kind, | |||
1824 | bool Pascal, QualType Ty, const SourceLocation *Loc, | |||
1825 | unsigned NumConcatenated); | |||
1826 | ||||
1827 | /// Build an empty string literal. | |||
1828 | StringLiteral(EmptyShell Empty, unsigned NumConcatenated, unsigned Length, | |||
1829 | unsigned CharByteWidth); | |||
1830 | ||||
1831 | /// Map a target and string kind to the appropriate character width. | |||
1832 | static unsigned mapCharByteWidth(TargetInfo const &Target, StringKind SK); | |||
1833 | ||||
1834 | /// Set one of the string literal token. | |||
1835 | void setStrTokenLoc(unsigned TokNum, SourceLocation L) { | |||
1836 | assert(TokNum < getNumConcatenated() && "Invalid tok number")(static_cast <bool> (TokNum < getNumConcatenated() && "Invalid tok number") ? void (0) : __assert_fail ("TokNum < getNumConcatenated() && \"Invalid tok number\"" , "clang/include/clang/AST/Expr.h", 1836, __extension__ __PRETTY_FUNCTION__ )); | |||
1837 | getTrailingObjects<SourceLocation>()[TokNum] = L; | |||
1838 | } | |||
1839 | ||||
1840 | public: | |||
1841 | /// This is the "fully general" constructor that allows representation of | |||
1842 | /// strings formed from multiple concatenated tokens. | |||
1843 | static StringLiteral *Create(const ASTContext &Ctx, StringRef Str, | |||
1844 | StringKind Kind, bool Pascal, QualType Ty, | |||
1845 | const SourceLocation *Loc, | |||
1846 | unsigned NumConcatenated); | |||
1847 | ||||
1848 | /// Simple constructor for string literals made from one token. | |||
1849 | static StringLiteral *Create(const ASTContext &Ctx, StringRef Str, | |||
1850 | StringKind Kind, bool Pascal, QualType Ty, | |||
1851 | SourceLocation Loc) { | |||
1852 | return Create(Ctx, Str, Kind, Pascal, Ty, &Loc, 1); | |||
1853 | } | |||
1854 | ||||
1855 | /// Construct an empty string literal. | |||
1856 | static StringLiteral *CreateEmpty(const ASTContext &Ctx, | |||
1857 | unsigned NumConcatenated, unsigned Length, | |||
1858 | unsigned CharByteWidth); | |||
1859 | ||||
1860 | StringRef getString() const { | |||
1861 | assert(getCharByteWidth() == 1 &&(static_cast <bool> (getCharByteWidth() == 1 && "This function is used in places that assume strings use char" ) ? void (0) : __assert_fail ("getCharByteWidth() == 1 && \"This function is used in places that assume strings use char\"" , "clang/include/clang/AST/Expr.h", 1862, __extension__ __PRETTY_FUNCTION__ )) | |||
1862 | "This function is used in places that assume strings use char")(static_cast <bool> (getCharByteWidth() == 1 && "This function is used in places that assume strings use char" ) ? void (0) : __assert_fail ("getCharByteWidth() == 1 && \"This function is used in places that assume strings use char\"" , "clang/include/clang/AST/Expr.h", 1862, __extension__ __PRETTY_FUNCTION__ )); | |||
1863 | return StringRef(getStrDataAsChar(), getByteLength()); | |||
1864 | } | |||
1865 | ||||
1866 | /// Allow access to clients that need the byte representation, such as | |||
1867 | /// ASTWriterStmt::VisitStringLiteral(). | |||
1868 | StringRef getBytes() const { | |||
1869 | // FIXME: StringRef may not be the right type to use as a result for this. | |||
1870 | return StringRef(getStrDataAsChar(), getByteLength()); | |||
1871 | } | |||
1872 | ||||
1873 | void outputString(raw_ostream &OS) const; | |||
1874 | ||||
1875 | uint32_t getCodeUnit(size_t i) const { | |||
1876 | assert(i < getLength() && "out of bounds access")(static_cast <bool> (i < getLength() && "out of bounds access" ) ? void (0) : __assert_fail ("i < getLength() && \"out of bounds access\"" , "clang/include/clang/AST/Expr.h", 1876, __extension__ __PRETTY_FUNCTION__ )); | |||
1877 | switch (getCharByteWidth()) { | |||
1878 | case 1: | |||
1879 | return static_cast<unsigned char>(getStrDataAsChar()[i]); | |||
1880 | case 2: | |||
1881 | return getStrDataAsUInt16()[i]; | |||
1882 | case 4: | |||
1883 | return getStrDataAsUInt32()[i]; | |||
1884 | } | |||
1885 | llvm_unreachable("Unsupported character width!")::llvm::llvm_unreachable_internal("Unsupported character width!" , "clang/include/clang/AST/Expr.h", 1885); | |||
1886 | } | |||
1887 | ||||
1888 | unsigned getByteLength() const { return getCharByteWidth() * getLength(); } | |||
1889 | unsigned getLength() const { return *getTrailingObjects<unsigned>(); } | |||
1890 | unsigned getCharByteWidth() const { return StringLiteralBits.CharByteWidth; } | |||
1891 | ||||
1892 | StringKind getKind() const { | |||
1893 | return static_cast<StringKind>(StringLiteralBits.Kind); | |||
1894 | } | |||
1895 | ||||
1896 | bool isOrdinary() const { return getKind() == Ordinary; } | |||
1897 | bool isWide() const { return getKind() == Wide; } | |||
1898 | bool isUTF8() const { return getKind() == UTF8; } | |||
1899 | bool isUTF16() const { return getKind() == UTF16; } | |||
1900 | bool isUTF32() const { return getKind() == UTF32; } | |||
1901 | bool isPascal() const { return StringLiteralBits.IsPascal; } | |||
1902 | ||||
1903 | bool containsNonAscii() const { | |||
1904 | for (auto c : getString()) | |||
1905 | if (!isASCII(c)) | |||
1906 | return true; | |||
1907 | return false; | |||
1908 | } | |||
1909 | ||||
1910 | bool containsNonAsciiOrNull() const { | |||
1911 | for (auto c : getString()) | |||
1912 | if (!isASCII(c) || !c) | |||
1913 | return true; | |||
1914 | return false; | |||
1915 | } | |||
1916 | ||||
1917 | /// getNumConcatenated - Get the number of string literal tokens that were | |||
1918 | /// concatenated in translation phase #6 to form this string literal. | |||
1919 | unsigned getNumConcatenated() const { | |||
1920 | return StringLiteralBits.NumConcatenated; | |||
1921 | } | |||
1922 | ||||
1923 | /// Get one of the string literal token. | |||
1924 | SourceLocation getStrTokenLoc(unsigned TokNum) const { | |||
1925 | assert(TokNum < getNumConcatenated() && "Invalid tok number")(static_cast <bool> (TokNum < getNumConcatenated() && "Invalid tok number") ? void (0) : __assert_fail ("TokNum < getNumConcatenated() && \"Invalid tok number\"" , "clang/include/clang/AST/Expr.h", 1925, __extension__ __PRETTY_FUNCTION__ )); | |||
1926 | return getTrailingObjects<SourceLocation>()[TokNum]; | |||
1927 | } | |||
1928 | ||||
1929 | /// getLocationOfByte - Return a source location that points to the specified | |||
1930 | /// byte of this string literal. | |||
1931 | /// | |||
1932 | /// Strings are amazingly complex. They can be formed from multiple tokens | |||
1933 | /// and can have escape sequences in them in addition to the usual trigraph | |||
1934 | /// and escaped newline business. This routine handles this complexity. | |||
1935 | /// | |||
1936 | SourceLocation | |||
1937 | getLocationOfByte(unsigned ByteNo, const SourceManager &SM, | |||
1938 | const LangOptions &Features, const TargetInfo &Target, | |||
1939 | unsigned *StartToken = nullptr, | |||
1940 | unsigned *StartTokenByteOffset = nullptr) const; | |||
1941 | ||||
1942 | typedef const SourceLocation *tokloc_iterator; | |||
1943 | ||||
1944 | tokloc_iterator tokloc_begin() const { | |||
1945 | return getTrailingObjects<SourceLocation>(); | |||
1946 | } | |||
1947 | ||||
1948 | tokloc_iterator tokloc_end() const { | |||
1949 | return getTrailingObjects<SourceLocation>() + getNumConcatenated(); | |||
1950 | } | |||
1951 | ||||
1952 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return *tokloc_begin(); } | |||
1953 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return *(tokloc_end() - 1); } | |||
1954 | ||||
1955 | static bool classof(const Stmt *T) { | |||
1956 | return T->getStmtClass() == StringLiteralClass; | |||
1957 | } | |||
1958 | ||||
1959 | // Iterators | |||
1960 | child_range children() { | |||
1961 | return child_range(child_iterator(), child_iterator()); | |||
1962 | } | |||
1963 | const_child_range children() const { | |||
1964 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
1965 | } | |||
1966 | }; | |||
1967 | ||||
1968 | /// [C99 6.4.2.2] - A predefined identifier such as __func__. | |||
1969 | class PredefinedExpr final | |||
1970 | : public Expr, | |||
1971 | private llvm::TrailingObjects<PredefinedExpr, Stmt *> { | |||
1972 | friend class ASTStmtReader; | |||
1973 | friend TrailingObjects; | |||
1974 | ||||
1975 | // PredefinedExpr is optionally followed by a single trailing | |||
1976 | // "Stmt *" for the predefined identifier. It is present if and only if | |||
1977 | // hasFunctionName() is true and is always a "StringLiteral *". | |||
1978 | ||||
1979 | public: | |||
1980 | enum IdentKind { | |||
1981 | Func, | |||
1982 | Function, | |||
1983 | LFunction, // Same as Function, but as wide string. | |||
1984 | FuncDName, | |||
1985 | FuncSig, | |||
1986 | LFuncSig, // Same as FuncSig, but as wide string | |||
1987 | PrettyFunction, | |||
1988 | /// The same as PrettyFunction, except that the | |||
1989 | /// 'virtual' keyword is omitted for virtual member functions. | |||
1990 | PrettyFunctionNoVirtual | |||
1991 | }; | |||
1992 | ||||
1993 | private: | |||
1994 | PredefinedExpr(SourceLocation L, QualType FNTy, IdentKind IK, | |||
1995 | StringLiteral *SL); | |||
1996 | ||||
1997 | explicit PredefinedExpr(EmptyShell Empty, bool HasFunctionName); | |||
1998 | ||||
1999 | /// True if this PredefinedExpr has storage for a function name. | |||
2000 | bool hasFunctionName() const { return PredefinedExprBits.HasFunctionName; } | |||
2001 | ||||
2002 | void setFunctionName(StringLiteral *SL) { | |||
2003 | assert(hasFunctionName() &&(static_cast <bool> (hasFunctionName() && "This PredefinedExpr has no storage for a function name!" ) ? void (0) : __assert_fail ("hasFunctionName() && \"This PredefinedExpr has no storage for a function name!\"" , "clang/include/clang/AST/Expr.h", 2004, __extension__ __PRETTY_FUNCTION__ )) | |||
2004 | "This PredefinedExpr has no storage for a function name!")(static_cast <bool> (hasFunctionName() && "This PredefinedExpr has no storage for a function name!" ) ? void (0) : __assert_fail ("hasFunctionName() && \"This PredefinedExpr has no storage for a function name!\"" , "clang/include/clang/AST/Expr.h", 2004, __extension__ __PRETTY_FUNCTION__ )); | |||
2005 | *getTrailingObjects<Stmt *>() = SL; | |||
2006 | } | |||
2007 | ||||
2008 | public: | |||
2009 | /// Create a PredefinedExpr. | |||
2010 | static PredefinedExpr *Create(const ASTContext &Ctx, SourceLocation L, | |||
2011 | QualType FNTy, IdentKind IK, StringLiteral *SL); | |||
2012 | ||||
2013 | /// Create an empty PredefinedExpr. | |||
2014 | static PredefinedExpr *CreateEmpty(const ASTContext &Ctx, | |||
2015 | bool HasFunctionName); | |||
2016 | ||||
2017 | IdentKind getIdentKind() const { | |||
2018 | return static_cast<IdentKind>(PredefinedExprBits.Kind); | |||
2019 | } | |||
2020 | ||||
2021 | SourceLocation getLocation() const { return PredefinedExprBits.Loc; } | |||
2022 | void setLocation(SourceLocation L) { PredefinedExprBits.Loc = L; } | |||
2023 | ||||
2024 | StringLiteral *getFunctionName() { | |||
2025 | return hasFunctionName() | |||
2026 | ? static_cast<StringLiteral *>(*getTrailingObjects<Stmt *>()) | |||
2027 | : nullptr; | |||
2028 | } | |||
2029 | ||||
2030 | const StringLiteral *getFunctionName() const { | |||
2031 | return hasFunctionName() | |||
2032 | ? static_cast<StringLiteral *>(*getTrailingObjects<Stmt *>()) | |||
2033 | : nullptr; | |||
2034 | } | |||
2035 | ||||
2036 | static StringRef getIdentKindName(IdentKind IK); | |||
2037 | StringRef getIdentKindName() const { | |||
2038 | return getIdentKindName(getIdentKind()); | |||
2039 | } | |||
2040 | ||||
2041 | static std::string ComputeName(IdentKind IK, const Decl *CurrentDecl); | |||
2042 | ||||
2043 | SourceLocation getBeginLoc() const { return getLocation(); } | |||
2044 | SourceLocation getEndLoc() const { return getLocation(); } | |||
2045 | ||||
2046 | static bool classof(const Stmt *T) { | |||
2047 | return T->getStmtClass() == PredefinedExprClass; | |||
2048 | } | |||
2049 | ||||
2050 | // Iterators | |||
2051 | child_range children() { | |||
2052 | return child_range(getTrailingObjects<Stmt *>(), | |||
2053 | getTrailingObjects<Stmt *>() + hasFunctionName()); | |||
2054 | } | |||
2055 | ||||
2056 | const_child_range children() const { | |||
2057 | return const_child_range(getTrailingObjects<Stmt *>(), | |||
2058 | getTrailingObjects<Stmt *>() + hasFunctionName()); | |||
2059 | } | |||
2060 | }; | |||
2061 | ||||
2062 | // This represents a use of the __builtin_sycl_unique_stable_name, which takes a | |||
2063 | // type-id, and at CodeGen time emits a unique string representation of the | |||
2064 | // type in a way that permits us to properly encode information about the SYCL | |||
2065 | // kernels. | |||
2066 | class SYCLUniqueStableNameExpr final : public Expr { | |||
2067 | friend class ASTStmtReader; | |||
2068 | SourceLocation OpLoc, LParen, RParen; | |||
2069 | TypeSourceInfo *TypeInfo; | |||
2070 | ||||
2071 | SYCLUniqueStableNameExpr(EmptyShell Empty, QualType ResultTy); | |||
2072 | SYCLUniqueStableNameExpr(SourceLocation OpLoc, SourceLocation LParen, | |||
2073 | SourceLocation RParen, QualType ResultTy, | |||
2074 | TypeSourceInfo *TSI); | |||
2075 | ||||
2076 | void setTypeSourceInfo(TypeSourceInfo *Ty) { TypeInfo = Ty; } | |||
2077 | ||||
2078 | void setLocation(SourceLocation L) { OpLoc = L; } | |||
2079 | void setLParenLocation(SourceLocation L) { LParen = L; } | |||
2080 | void setRParenLocation(SourceLocation L) { RParen = L; } | |||
2081 | ||||
2082 | public: | |||
2083 | TypeSourceInfo *getTypeSourceInfo() { return TypeInfo; } | |||
2084 | ||||
2085 | const TypeSourceInfo *getTypeSourceInfo() const { return TypeInfo; } | |||
2086 | ||||
2087 | static SYCLUniqueStableNameExpr * | |||
2088 | Create(const ASTContext &Ctx, SourceLocation OpLoc, SourceLocation LParen, | |||
2089 | SourceLocation RParen, TypeSourceInfo *TSI); | |||
2090 | ||||
2091 | static SYCLUniqueStableNameExpr *CreateEmpty(const ASTContext &Ctx); | |||
2092 | ||||
2093 | SourceLocation getBeginLoc() const { return getLocation(); } | |||
2094 | SourceLocation getEndLoc() const { return RParen; } | |||
2095 | SourceLocation getLocation() const { return OpLoc; } | |||
2096 | SourceLocation getLParenLocation() const { return LParen; } | |||
2097 | SourceLocation getRParenLocation() const { return RParen; } | |||
2098 | ||||
2099 | static bool classof(const Stmt *T) { | |||
2100 | return T->getStmtClass() == SYCLUniqueStableNameExprClass; | |||
2101 | } | |||
2102 | ||||
2103 | // Iterators | |||
2104 | child_range children() { | |||
2105 | return child_range(child_iterator(), child_iterator()); | |||
2106 | } | |||
2107 | ||||
2108 | const_child_range children() const { | |||
2109 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
2110 | } | |||
2111 | ||||
2112 | // Convenience function to generate the name of the currently stored type. | |||
2113 | std::string ComputeName(ASTContext &Context) const; | |||
2114 | ||||
2115 | // Get the generated name of the type. Note that this only works after all | |||
2116 | // kernels have been instantiated. | |||
2117 | static std::string ComputeName(ASTContext &Context, QualType Ty); | |||
2118 | }; | |||
2119 | ||||
2120 | /// ParenExpr - This represents a parethesized expression, e.g. "(1)". This | |||
2121 | /// AST node is only formed if full location information is requested. | |||
2122 | class ParenExpr : public Expr { | |||
2123 | SourceLocation L, R; | |||
2124 | Stmt *Val; | |||
2125 | public: | |||
2126 | ParenExpr(SourceLocation l, SourceLocation r, Expr *val) | |||
2127 | : Expr(ParenExprClass, val->getType(), val->getValueKind(), | |||
2128 | val->getObjectKind()), | |||
2129 | L(l), R(r), Val(val) { | |||
2130 | setDependence(computeDependence(this)); | |||
2131 | } | |||
2132 | ||||
2133 | /// Construct an empty parenthesized expression. | |||
2134 | explicit ParenExpr(EmptyShell Empty) | |||
2135 | : Expr(ParenExprClass, Empty) { } | |||
2136 | ||||
2137 | const Expr *getSubExpr() const { return cast<Expr>(Val); } | |||
2138 | Expr *getSubExpr() { return cast<Expr>(Val); } | |||
2139 | void setSubExpr(Expr *E) { Val = E; } | |||
2140 | ||||
2141 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return L; } | |||
2142 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return R; } | |||
2143 | ||||
2144 | /// Get the location of the left parentheses '('. | |||
2145 | SourceLocation getLParen() const { return L; } | |||
2146 | void setLParen(SourceLocation Loc) { L = Loc; } | |||
2147 | ||||
2148 | /// Get the location of the right parentheses ')'. | |||
2149 | SourceLocation getRParen() const { return R; } | |||
2150 | void setRParen(SourceLocation Loc) { R = Loc; } | |||
2151 | ||||
2152 | static bool classof(const Stmt *T) { | |||
2153 | return T->getStmtClass() == ParenExprClass; | |||
2154 | } | |||
2155 | ||||
2156 | // Iterators | |||
2157 | child_range children() { return child_range(&Val, &Val+1); } | |||
2158 | const_child_range children() const { | |||
2159 | return const_child_range(&Val, &Val + 1); | |||
2160 | } | |||
2161 | }; | |||
2162 | ||||
2163 | /// UnaryOperator - This represents the unary-expression's (except sizeof and | |||
2164 | /// alignof), the postinc/postdec operators from postfix-expression, and various | |||
2165 | /// extensions. | |||
2166 | /// | |||
2167 | /// Notes on various nodes: | |||
2168 | /// | |||
2169 | /// Real/Imag - These return the real/imag part of a complex operand. If | |||
2170 | /// applied to a non-complex value, the former returns its operand and the | |||
2171 | /// later returns zero in the type of the operand. | |||
2172 | /// | |||
2173 | class UnaryOperator final | |||
2174 | : public Expr, | |||
2175 | private llvm::TrailingObjects<UnaryOperator, FPOptionsOverride> { | |||
2176 | Stmt *Val; | |||
2177 | ||||
2178 | size_t numTrailingObjects(OverloadToken<FPOptionsOverride>) const { | |||
2179 | return UnaryOperatorBits.HasFPFeatures ? 1 : 0; | |||
2180 | } | |||
2181 | ||||
2182 | FPOptionsOverride &getTrailingFPFeatures() { | |||
2183 | assert(UnaryOperatorBits.HasFPFeatures)(static_cast <bool> (UnaryOperatorBits.HasFPFeatures) ? void (0) : __assert_fail ("UnaryOperatorBits.HasFPFeatures", "clang/include/clang/AST/Expr.h", 2183, __extension__ __PRETTY_FUNCTION__ )); | |||
2184 | return *getTrailingObjects<FPOptionsOverride>(); | |||
2185 | } | |||
2186 | ||||
2187 | const FPOptionsOverride &getTrailingFPFeatures() const { | |||
2188 | assert(UnaryOperatorBits.HasFPFeatures)(static_cast <bool> (UnaryOperatorBits.HasFPFeatures) ? void (0) : __assert_fail ("UnaryOperatorBits.HasFPFeatures", "clang/include/clang/AST/Expr.h", 2188, __extension__ __PRETTY_FUNCTION__ )); | |||
2189 | return *getTrailingObjects<FPOptionsOverride>(); | |||
2190 | } | |||
2191 | ||||
2192 | public: | |||
2193 | typedef UnaryOperatorKind Opcode; | |||
2194 | ||||
2195 | protected: | |||
2196 | UnaryOperator(const ASTContext &Ctx, Expr *input, Opcode opc, QualType type, | |||
2197 | ExprValueKind VK, ExprObjectKind OK, SourceLocation l, | |||
2198 | bool CanOverflow, FPOptionsOverride FPFeatures); | |||
2199 | ||||
2200 | /// Build an empty unary operator. | |||
2201 | explicit UnaryOperator(bool HasFPFeatures, EmptyShell Empty) | |||
2202 | : Expr(UnaryOperatorClass, Empty) { | |||
2203 | UnaryOperatorBits.Opc = UO_AddrOf; | |||
2204 | UnaryOperatorBits.HasFPFeatures = HasFPFeatures; | |||
2205 | } | |||
2206 | ||||
2207 | public: | |||
2208 | static UnaryOperator *CreateEmpty(const ASTContext &C, bool hasFPFeatures); | |||
2209 | ||||
2210 | static UnaryOperator *Create(const ASTContext &C, Expr *input, Opcode opc, | |||
2211 | QualType type, ExprValueKind VK, | |||
2212 | ExprObjectKind OK, SourceLocation l, | |||
2213 | bool CanOverflow, FPOptionsOverride FPFeatures); | |||
2214 | ||||
2215 | Opcode getOpcode() const { | |||
2216 | return static_cast<Opcode>(UnaryOperatorBits.Opc); | |||
2217 | } | |||
2218 | void setOpcode(Opcode Opc) { UnaryOperatorBits.Opc = Opc; } | |||
2219 | ||||
2220 | Expr *getSubExpr() const { return cast<Expr>(Val); } | |||
2221 | void setSubExpr(Expr *E) { Val = E; } | |||
2222 | ||||
2223 | /// getOperatorLoc - Return the location of the operator. | |||
2224 | SourceLocation getOperatorLoc() const { return UnaryOperatorBits.Loc; } | |||
2225 | void setOperatorLoc(SourceLocation L) { UnaryOperatorBits.Loc = L; } | |||
2226 | ||||
2227 | /// Returns true if the unary operator can cause an overflow. For instance, | |||
2228 | /// signed int i = INT_MAX; i++; | |||
2229 | /// signed char c = CHAR_MAX; c++; | |||
2230 | /// Due to integer promotions, c++ is promoted to an int before the postfix | |||
2231 | /// increment, and the result is an int that cannot overflow. However, i++ | |||
2232 | /// can overflow. | |||
2233 | bool canOverflow() const { return UnaryOperatorBits.CanOverflow; } | |||
2234 | void setCanOverflow(bool C) { UnaryOperatorBits.CanOverflow = C; } | |||
2235 | ||||
2236 | /// Get the FP contractability status of this operator. Only meaningful for | |||
2237 | /// operations on floating point types. | |||
2238 | bool isFPContractableWithinStatement(const LangOptions &LO) const { | |||
2239 | return getFPFeaturesInEffect(LO).allowFPContractWithinStatement(); | |||
2240 | } | |||
2241 | ||||
2242 | /// Get the FENV_ACCESS status of this operator. Only meaningful for | |||
2243 | /// operations on floating point types. | |||
2244 | bool isFEnvAccessOn(const LangOptions &LO) const { | |||
2245 | return getFPFeaturesInEffect(LO).getAllowFEnvAccess(); | |||
2246 | } | |||
2247 | ||||
2248 | /// isPostfix - Return true if this is a postfix operation, like x++. | |||
2249 | static bool isPostfix(Opcode Op) { | |||
2250 | return Op == UO_PostInc || Op == UO_PostDec; | |||
2251 | } | |||
2252 | ||||
2253 | /// isPrefix - Return true if this is a prefix operation, like --x. | |||
2254 | static bool isPrefix(Opcode Op) { | |||
2255 | return Op == UO_PreInc || Op == UO_PreDec; | |||
2256 | } | |||
2257 | ||||
2258 | bool isPrefix() const { return isPrefix(getOpcode()); } | |||
2259 | bool isPostfix() const { return isPostfix(getOpcode()); } | |||
2260 | ||||
2261 | static bool isIncrementOp(Opcode Op) { | |||
2262 | return Op == UO_PreInc || Op == UO_PostInc; | |||
2263 | } | |||
2264 | bool isIncrementOp() const { | |||
2265 | return isIncrementOp(getOpcode()); | |||
2266 | } | |||
2267 | ||||
2268 | static bool isDecrementOp(Opcode Op) { | |||
2269 | return Op == UO_PreDec || Op == UO_PostDec; | |||
2270 | } | |||
2271 | bool isDecrementOp() const { | |||
2272 | return isDecrementOp(getOpcode()); | |||
2273 | } | |||
2274 | ||||
2275 | static bool isIncrementDecrementOp(Opcode Op) { return Op <= UO_PreDec; } | |||
2276 | bool isIncrementDecrementOp() const { | |||
2277 | return isIncrementDecrementOp(getOpcode()); | |||
2278 | } | |||
2279 | ||||
2280 | static bool isArithmeticOp(Opcode Op) { | |||
2281 | return Op >= UO_Plus && Op <= UO_LNot; | |||
2282 | } | |||
2283 | bool isArithmeticOp() const { return isArithmeticOp(getOpcode()); } | |||
2284 | ||||
2285 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it | |||
2286 | /// corresponds to, e.g. "sizeof" or "[pre]++" | |||
2287 | static StringRef getOpcodeStr(Opcode Op); | |||
2288 | ||||
2289 | /// Retrieve the unary opcode that corresponds to the given | |||
2290 | /// overloaded operator. | |||
2291 | static Opcode getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix); | |||
2292 | ||||
2293 | /// Retrieve the overloaded operator kind that corresponds to | |||
2294 | /// the given unary opcode. | |||
2295 | static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); | |||
2296 | ||||
2297 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
2298 | return isPostfix() ? Val->getBeginLoc() : getOperatorLoc(); | |||
2299 | } | |||
2300 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
2301 | return isPostfix() ? getOperatorLoc() : Val->getEndLoc(); | |||
2302 | } | |||
2303 | SourceLocation getExprLoc() const { return getOperatorLoc(); } | |||
2304 | ||||
2305 | static bool classof(const Stmt *T) { | |||
2306 | return T->getStmtClass() == UnaryOperatorClass; | |||
2307 | } | |||
2308 | ||||
2309 | // Iterators | |||
2310 | child_range children() { return child_range(&Val, &Val+1); } | |||
2311 | const_child_range children() const { | |||
2312 | return const_child_range(&Val, &Val + 1); | |||
2313 | } | |||
2314 | ||||
2315 | /// Is FPFeatures in Trailing Storage? | |||
2316 | bool hasStoredFPFeatures() const { return UnaryOperatorBits.HasFPFeatures; } | |||
2317 | ||||
2318 | /// Get FPFeatures from trailing storage. | |||
2319 | FPOptionsOverride getStoredFPFeatures() const { | |||
2320 | return getTrailingFPFeatures(); | |||
2321 | } | |||
2322 | ||||
2323 | protected: | |||
2324 | /// Set FPFeatures in trailing storage, used only by Serialization | |||
2325 | void setStoredFPFeatures(FPOptionsOverride F) { getTrailingFPFeatures() = F; } | |||
2326 | ||||
2327 | public: | |||
2328 | /// Get the FP features status of this operator. Only meaningful for | |||
2329 | /// operations on floating point types. | |||
2330 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const { | |||
2331 | if (UnaryOperatorBits.HasFPFeatures) | |||
2332 | return getStoredFPFeatures().applyOverrides(LO); | |||
2333 | return FPOptions::defaultWithoutTrailingStorage(LO); | |||
2334 | } | |||
2335 | FPOptionsOverride getFPOptionsOverride() const { | |||
2336 | if (UnaryOperatorBits.HasFPFeatures) | |||
2337 | return getStoredFPFeatures(); | |||
2338 | return FPOptionsOverride(); | |||
2339 | } | |||
2340 | ||||
2341 | friend TrailingObjects; | |||
2342 | friend class ASTReader; | |||
2343 | friend class ASTStmtReader; | |||
2344 | friend class ASTStmtWriter; | |||
2345 | }; | |||
2346 | ||||
2347 | /// Helper class for OffsetOfExpr. | |||
2348 | ||||
2349 | // __builtin_offsetof(type, identifier(.identifier|[expr])*) | |||
2350 | class OffsetOfNode { | |||
2351 | public: | |||
2352 | /// The kind of offsetof node we have. | |||
2353 | enum Kind { | |||
2354 | /// An index into an array. | |||
2355 | Array = 0x00, | |||
2356 | /// A field. | |||
2357 | Field = 0x01, | |||
2358 | /// A field in a dependent type, known only by its name. | |||
2359 | Identifier = 0x02, | |||
2360 | /// An implicit indirection through a C++ base class, when the | |||
2361 | /// field found is in a base class. | |||
2362 | Base = 0x03 | |||
2363 | }; | |||
2364 | ||||
2365 | private: | |||
2366 | enum { MaskBits = 2, Mask = 0x03 }; | |||
2367 | ||||
2368 | /// The source range that covers this part of the designator. | |||
2369 | SourceRange Range; | |||
2370 | ||||
2371 | /// The data describing the designator, which comes in three | |||
2372 | /// different forms, depending on the lower two bits. | |||
2373 | /// - An unsigned index into the array of Expr*'s stored after this node | |||
2374 | /// in memory, for [constant-expression] designators. | |||
2375 | /// - A FieldDecl*, for references to a known field. | |||
2376 | /// - An IdentifierInfo*, for references to a field with a given name | |||
2377 | /// when the class type is dependent. | |||
2378 | /// - A CXXBaseSpecifier*, for references that look at a field in a | |||
2379 | /// base class. | |||
2380 | uintptr_t Data; | |||
2381 | ||||
2382 | public: | |||
2383 | /// Create an offsetof node that refers to an array element. | |||
2384 | OffsetOfNode(SourceLocation LBracketLoc, unsigned Index, | |||
2385 | SourceLocation RBracketLoc) | |||
2386 | : Range(LBracketLoc, RBracketLoc), Data((Index << 2) | Array) {} | |||
2387 | ||||
2388 | /// Create an offsetof node that refers to a field. | |||
2389 | OffsetOfNode(SourceLocation DotLoc, FieldDecl *Field, SourceLocation NameLoc) | |||
2390 | : Range(DotLoc.isValid() ? DotLoc : NameLoc, NameLoc), | |||
2391 | Data(reinterpret_cast<uintptr_t>(Field) | OffsetOfNode::Field) {} | |||
2392 | ||||
2393 | /// Create an offsetof node that refers to an identifier. | |||
2394 | OffsetOfNode(SourceLocation DotLoc, IdentifierInfo *Name, | |||
2395 | SourceLocation NameLoc) | |||
2396 | : Range(DotLoc.isValid() ? DotLoc : NameLoc, NameLoc), | |||
2397 | Data(reinterpret_cast<uintptr_t>(Name) | Identifier) {} | |||
2398 | ||||
2399 | /// Create an offsetof node that refers into a C++ base class. | |||
2400 | explicit OffsetOfNode(const CXXBaseSpecifier *Base) | |||
2401 | : Data(reinterpret_cast<uintptr_t>(Base) | OffsetOfNode::Base) {} | |||
2402 | ||||
2403 | /// Determine what kind of offsetof node this is. | |||
2404 | Kind getKind() const { return static_cast<Kind>(Data & Mask); } | |||
2405 | ||||
2406 | /// For an array element node, returns the index into the array | |||
2407 | /// of expressions. | |||
2408 | unsigned getArrayExprIndex() const { | |||
2409 | assert(getKind() == Array)(static_cast <bool> (getKind() == Array) ? void (0) : __assert_fail ("getKind() == Array", "clang/include/clang/AST/Expr.h", 2409 , __extension__ __PRETTY_FUNCTION__)); | |||
2410 | return Data >> 2; | |||
2411 | } | |||
2412 | ||||
2413 | /// For a field offsetof node, returns the field. | |||
2414 | FieldDecl *getField() const { | |||
2415 | assert(getKind() == Field)(static_cast <bool> (getKind() == Field) ? void (0) : __assert_fail ("getKind() == Field", "clang/include/clang/AST/Expr.h", 2415 , __extension__ __PRETTY_FUNCTION__)); | |||
2416 | return reinterpret_cast<FieldDecl *>(Data & ~(uintptr_t)Mask); | |||
2417 | } | |||
2418 | ||||
2419 | /// For a field or identifier offsetof node, returns the name of | |||
2420 | /// the field. | |||
2421 | IdentifierInfo *getFieldName() const; | |||
2422 | ||||
2423 | /// For a base class node, returns the base specifier. | |||
2424 | CXXBaseSpecifier *getBase() const { | |||
2425 | assert(getKind() == Base)(static_cast <bool> (getKind() == Base) ? void (0) : __assert_fail ("getKind() == Base", "clang/include/clang/AST/Expr.h", 2425 , __extension__ __PRETTY_FUNCTION__)); | |||
2426 | return reinterpret_cast<CXXBaseSpecifier *>(Data & ~(uintptr_t)Mask); | |||
2427 | } | |||
2428 | ||||
2429 | /// Retrieve the source range that covers this offsetof node. | |||
2430 | /// | |||
2431 | /// For an array element node, the source range contains the locations of | |||
2432 | /// the square brackets. For a field or identifier node, the source range | |||
2433 | /// contains the location of the period (if there is one) and the | |||
2434 | /// identifier. | |||
2435 | SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__)) { return Range; } | |||
2436 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return Range.getBegin(); } | |||
2437 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return Range.getEnd(); } | |||
2438 | }; | |||
2439 | ||||
2440 | /// OffsetOfExpr - [C99 7.17] - This represents an expression of the form | |||
2441 | /// offsetof(record-type, member-designator). For example, given: | |||
2442 | /// @code | |||
2443 | /// struct S { | |||
2444 | /// float f; | |||
2445 | /// double d; | |||
2446 | /// }; | |||
2447 | /// struct T { | |||
2448 | /// int i; | |||
2449 | /// struct S s[10]; | |||
2450 | /// }; | |||
2451 | /// @endcode | |||
2452 | /// we can represent and evaluate the expression @c offsetof(struct T, s[2].d). | |||
2453 | ||||
2454 | class OffsetOfExpr final | |||
2455 | : public Expr, | |||
2456 | private llvm::TrailingObjects<OffsetOfExpr, OffsetOfNode, Expr *> { | |||
2457 | SourceLocation OperatorLoc, RParenLoc; | |||
2458 | // Base type; | |||
2459 | TypeSourceInfo *TSInfo; | |||
2460 | // Number of sub-components (i.e. instances of OffsetOfNode). | |||
2461 | unsigned NumComps; | |||
2462 | // Number of sub-expressions (i.e. array subscript expressions). | |||
2463 | unsigned NumExprs; | |||
2464 | ||||
2465 | size_t numTrailingObjects(OverloadToken<OffsetOfNode>) const { | |||
2466 | return NumComps; | |||
2467 | } | |||
2468 | ||||
2469 | OffsetOfExpr(const ASTContext &C, QualType type, | |||
2470 | SourceLocation OperatorLoc, TypeSourceInfo *tsi, | |||
2471 | ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs, | |||
2472 | SourceLocation RParenLoc); | |||
2473 | ||||
2474 | explicit OffsetOfExpr(unsigned numComps, unsigned numExprs) | |||
2475 | : Expr(OffsetOfExprClass, EmptyShell()), | |||
2476 | TSInfo(nullptr), NumComps(numComps), NumExprs(numExprs) {} | |||
2477 | ||||
2478 | public: | |||
2479 | ||||
2480 | static OffsetOfExpr *Create(const ASTContext &C, QualType type, | |||
2481 | SourceLocation OperatorLoc, TypeSourceInfo *tsi, | |||
2482 | ArrayRef<OffsetOfNode> comps, | |||
2483 | ArrayRef<Expr*> exprs, SourceLocation RParenLoc); | |||
2484 | ||||
2485 | static OffsetOfExpr *CreateEmpty(const ASTContext &C, | |||
2486 | unsigned NumComps, unsigned NumExprs); | |||
2487 | ||||
2488 | /// getOperatorLoc - Return the location of the operator. | |||
2489 | SourceLocation getOperatorLoc() const { return OperatorLoc; } | |||
2490 | void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } | |||
2491 | ||||
2492 | /// Return the location of the right parentheses. | |||
2493 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
2494 | void setRParenLoc(SourceLocation R) { RParenLoc = R; } | |||
2495 | ||||
2496 | TypeSourceInfo *getTypeSourceInfo() const { | |||
2497 | return TSInfo; | |||
2498 | } | |||
2499 | void setTypeSourceInfo(TypeSourceInfo *tsi) { | |||
2500 | TSInfo = tsi; | |||
2501 | } | |||
2502 | ||||
2503 | const OffsetOfNode &getComponent(unsigned Idx) const { | |||
2504 | assert(Idx < NumComps && "Subscript out of range")(static_cast <bool> (Idx < NumComps && "Subscript out of range" ) ? void (0) : __assert_fail ("Idx < NumComps && \"Subscript out of range\"" , "clang/include/clang/AST/Expr.h", 2504, __extension__ __PRETTY_FUNCTION__ )); | |||
2505 | return getTrailingObjects<OffsetOfNode>()[Idx]; | |||
2506 | } | |||
2507 | ||||
2508 | void setComponent(unsigned Idx, OffsetOfNode ON) { | |||
2509 | assert(Idx < NumComps && "Subscript out of range")(static_cast <bool> (Idx < NumComps && "Subscript out of range" ) ? void (0) : __assert_fail ("Idx < NumComps && \"Subscript out of range\"" , "clang/include/clang/AST/Expr.h", 2509, __extension__ __PRETTY_FUNCTION__ )); | |||
2510 | getTrailingObjects<OffsetOfNode>()[Idx] = ON; | |||
2511 | } | |||
2512 | ||||
2513 | unsigned getNumComponents() const { | |||
2514 | return NumComps; | |||
2515 | } | |||
2516 | ||||
2517 | Expr* getIndexExpr(unsigned Idx) { | |||
2518 | assert(Idx < NumExprs && "Subscript out of range")(static_cast <bool> (Idx < NumExprs && "Subscript out of range" ) ? void (0) : __assert_fail ("Idx < NumExprs && \"Subscript out of range\"" , "clang/include/clang/AST/Expr.h", 2518, __extension__ __PRETTY_FUNCTION__ )); | |||
2519 | return getTrailingObjects<Expr *>()[Idx]; | |||
2520 | } | |||
2521 | ||||
2522 | const Expr *getIndexExpr(unsigned Idx) const { | |||
2523 | assert(Idx < NumExprs && "Subscript out of range")(static_cast <bool> (Idx < NumExprs && "Subscript out of range" ) ? void (0) : __assert_fail ("Idx < NumExprs && \"Subscript out of range\"" , "clang/include/clang/AST/Expr.h", 2523, __extension__ __PRETTY_FUNCTION__ )); | |||
2524 | return getTrailingObjects<Expr *>()[Idx]; | |||
2525 | } | |||
2526 | ||||
2527 | void setIndexExpr(unsigned Idx, Expr* E) { | |||
2528 | assert(Idx < NumComps && "Subscript out of range")(static_cast <bool> (Idx < NumComps && "Subscript out of range" ) ? void (0) : __assert_fail ("Idx < NumComps && \"Subscript out of range\"" , "clang/include/clang/AST/Expr.h", 2528, __extension__ __PRETTY_FUNCTION__ )); | |||
2529 | getTrailingObjects<Expr *>()[Idx] = E; | |||
2530 | } | |||
2531 | ||||
2532 | unsigned getNumExpressions() const { | |||
2533 | return NumExprs; | |||
2534 | } | |||
2535 | ||||
2536 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return OperatorLoc; } | |||
2537 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return RParenLoc; } | |||
2538 | ||||
2539 | static bool classof(const Stmt *T) { | |||
2540 | return T->getStmtClass() == OffsetOfExprClass; | |||
2541 | } | |||
2542 | ||||
2543 | // Iterators | |||
2544 | child_range children() { | |||
2545 | Stmt **begin = reinterpret_cast<Stmt **>(getTrailingObjects<Expr *>()); | |||
2546 | return child_range(begin, begin + NumExprs); | |||
2547 | } | |||
2548 | const_child_range children() const { | |||
2549 | Stmt *const *begin = | |||
2550 | reinterpret_cast<Stmt *const *>(getTrailingObjects<Expr *>()); | |||
2551 | return const_child_range(begin, begin + NumExprs); | |||
2552 | } | |||
2553 | friend TrailingObjects; | |||
2554 | }; | |||
2555 | ||||
2556 | /// UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) | |||
2557 | /// expression operand. Used for sizeof/alignof (C99 6.5.3.4) and | |||
2558 | /// vec_step (OpenCL 1.1 6.11.12). | |||
2559 | class UnaryExprOrTypeTraitExpr : public Expr { | |||
2560 | union { | |||
2561 | TypeSourceInfo *Ty; | |||
2562 | Stmt *Ex; | |||
2563 | } Argument; | |||
2564 | SourceLocation OpLoc, RParenLoc; | |||
2565 | ||||
2566 | public: | |||
2567 | UnaryExprOrTypeTraitExpr(UnaryExprOrTypeTrait ExprKind, TypeSourceInfo *TInfo, | |||
2568 | QualType resultType, SourceLocation op, | |||
2569 | SourceLocation rp) | |||
2570 | : Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_PRValue, | |||
2571 | OK_Ordinary), | |||
2572 | OpLoc(op), RParenLoc(rp) { | |||
2573 | assert(ExprKind <= UETT_Last && "invalid enum value!")(static_cast <bool> (ExprKind <= UETT_Last && "invalid enum value!") ? void (0) : __assert_fail ("ExprKind <= UETT_Last && \"invalid enum value!\"" , "clang/include/clang/AST/Expr.h", 2573, __extension__ __PRETTY_FUNCTION__ )); | |||
2574 | UnaryExprOrTypeTraitExprBits.Kind = ExprKind; | |||
2575 | assert(static_cast<unsigned>(ExprKind) ==(static_cast <bool> (static_cast<unsigned>(ExprKind ) == UnaryExprOrTypeTraitExprBits.Kind && "UnaryExprOrTypeTraitExprBits.Kind overflow!" ) ? void (0) : __assert_fail ("static_cast<unsigned>(ExprKind) == UnaryExprOrTypeTraitExprBits.Kind && \"UnaryExprOrTypeTraitExprBits.Kind overflow!\"" , "clang/include/clang/AST/Expr.h", 2577, __extension__ __PRETTY_FUNCTION__ )) | |||
2576 | UnaryExprOrTypeTraitExprBits.Kind &&(static_cast <bool> (static_cast<unsigned>(ExprKind ) == UnaryExprOrTypeTraitExprBits.Kind && "UnaryExprOrTypeTraitExprBits.Kind overflow!" ) ? void (0) : __assert_fail ("static_cast<unsigned>(ExprKind) == UnaryExprOrTypeTraitExprBits.Kind && \"UnaryExprOrTypeTraitExprBits.Kind overflow!\"" , "clang/include/clang/AST/Expr.h", 2577, __extension__ __PRETTY_FUNCTION__ )) | |||
2577 | "UnaryExprOrTypeTraitExprBits.Kind overflow!")(static_cast <bool> (static_cast<unsigned>(ExprKind ) == UnaryExprOrTypeTraitExprBits.Kind && "UnaryExprOrTypeTraitExprBits.Kind overflow!" ) ? void (0) : __assert_fail ("static_cast<unsigned>(ExprKind) == UnaryExprOrTypeTraitExprBits.Kind && \"UnaryExprOrTypeTraitExprBits.Kind overflow!\"" , "clang/include/clang/AST/Expr.h", 2577, __extension__ __PRETTY_FUNCTION__ )); | |||
2578 | UnaryExprOrTypeTraitExprBits.IsType = true; | |||
2579 | Argument.Ty = TInfo; | |||
2580 | setDependence(computeDependence(this)); | |||
2581 | } | |||
2582 | ||||
2583 | UnaryExprOrTypeTraitExpr(UnaryExprOrTypeTrait ExprKind, Expr *E, | |||
2584 | QualType resultType, SourceLocation op, | |||
2585 | SourceLocation rp); | |||
2586 | ||||
2587 | /// Construct an empty sizeof/alignof expression. | |||
2588 | explicit UnaryExprOrTypeTraitExpr(EmptyShell Empty) | |||
2589 | : Expr(UnaryExprOrTypeTraitExprClass, Empty) { } | |||
2590 | ||||
2591 | UnaryExprOrTypeTrait getKind() const { | |||
2592 | return static_cast<UnaryExprOrTypeTrait>(UnaryExprOrTypeTraitExprBits.Kind); | |||
2593 | } | |||
2594 | void setKind(UnaryExprOrTypeTrait K) { | |||
2595 | assert(K <= UETT_Last && "invalid enum value!")(static_cast <bool> (K <= UETT_Last && "invalid enum value!" ) ? void (0) : __assert_fail ("K <= UETT_Last && \"invalid enum value!\"" , "clang/include/clang/AST/Expr.h", 2595, __extension__ __PRETTY_FUNCTION__ )); | |||
2596 | UnaryExprOrTypeTraitExprBits.Kind = K; | |||
2597 | assert(static_cast<unsigned>(K) == UnaryExprOrTypeTraitExprBits.Kind &&(static_cast <bool> (static_cast<unsigned>(K) == UnaryExprOrTypeTraitExprBits .Kind && "UnaryExprOrTypeTraitExprBits.Kind overflow!" ) ? void (0) : __assert_fail ("static_cast<unsigned>(K) == UnaryExprOrTypeTraitExprBits.Kind && \"UnaryExprOrTypeTraitExprBits.Kind overflow!\"" , "clang/include/clang/AST/Expr.h", 2598, __extension__ __PRETTY_FUNCTION__ )) | |||
2598 | "UnaryExprOrTypeTraitExprBits.Kind overflow!")(static_cast <bool> (static_cast<unsigned>(K) == UnaryExprOrTypeTraitExprBits .Kind && "UnaryExprOrTypeTraitExprBits.Kind overflow!" ) ? void (0) : __assert_fail ("static_cast<unsigned>(K) == UnaryExprOrTypeTraitExprBits.Kind && \"UnaryExprOrTypeTraitExprBits.Kind overflow!\"" , "clang/include/clang/AST/Expr.h", 2598, __extension__ __PRETTY_FUNCTION__ )); | |||
2599 | } | |||
2600 | ||||
2601 | bool isArgumentType() const { return UnaryExprOrTypeTraitExprBits.IsType; } | |||
2602 | QualType getArgumentType() const { | |||
2603 | return getArgumentTypeInfo()->getType(); | |||
2604 | } | |||
2605 | TypeSourceInfo *getArgumentTypeInfo() const { | |||
2606 | assert(isArgumentType() && "calling getArgumentType() when arg is expr")(static_cast <bool> (isArgumentType() && "calling getArgumentType() when arg is expr" ) ? void (0) : __assert_fail ("isArgumentType() && \"calling getArgumentType() when arg is expr\"" , "clang/include/clang/AST/Expr.h", 2606, __extension__ __PRETTY_FUNCTION__ )); | |||
2607 | return Argument.Ty; | |||
2608 | } | |||
2609 | Expr *getArgumentExpr() { | |||
2610 | assert(!isArgumentType() && "calling getArgumentExpr() when arg is type")(static_cast <bool> (!isArgumentType() && "calling getArgumentExpr() when arg is type" ) ? void (0) : __assert_fail ("!isArgumentType() && \"calling getArgumentExpr() when arg is type\"" , "clang/include/clang/AST/Expr.h", 2610, __extension__ __PRETTY_FUNCTION__ )); | |||
2611 | return static_cast<Expr*>(Argument.Ex); | |||
2612 | } | |||
2613 | const Expr *getArgumentExpr() const { | |||
2614 | return const_cast<UnaryExprOrTypeTraitExpr*>(this)->getArgumentExpr(); | |||
2615 | } | |||
2616 | ||||
2617 | void setArgument(Expr *E) { | |||
2618 | Argument.Ex = E; | |||
2619 | UnaryExprOrTypeTraitExprBits.IsType = false; | |||
2620 | } | |||
2621 | void setArgument(TypeSourceInfo *TInfo) { | |||
2622 | Argument.Ty = TInfo; | |||
2623 | UnaryExprOrTypeTraitExprBits.IsType = true; | |||
2624 | } | |||
2625 | ||||
2626 | /// Gets the argument type, or the type of the argument expression, whichever | |||
2627 | /// is appropriate. | |||
2628 | QualType getTypeOfArgument() const { | |||
2629 | return isArgumentType() ? getArgumentType() : getArgumentExpr()->getType(); | |||
2630 | } | |||
2631 | ||||
2632 | SourceLocation getOperatorLoc() const { return OpLoc; } | |||
2633 | void setOperatorLoc(SourceLocation L) { OpLoc = L; } | |||
2634 | ||||
2635 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
2636 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } | |||
2637 | ||||
2638 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return OpLoc; } | |||
2639 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return RParenLoc; } | |||
2640 | ||||
2641 | static bool classof(const Stmt *T) { | |||
2642 | return T->getStmtClass() == UnaryExprOrTypeTraitExprClass; | |||
2643 | } | |||
2644 | ||||
2645 | // Iterators | |||
2646 | child_range children(); | |||
2647 | const_child_range children() const; | |||
2648 | }; | |||
2649 | ||||
2650 | //===----------------------------------------------------------------------===// | |||
2651 | // Postfix Operators. | |||
2652 | //===----------------------------------------------------------------------===// | |||
2653 | ||||
2654 | /// ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting. | |||
2655 | class ArraySubscriptExpr : public Expr { | |||
2656 | enum { LHS, RHS, END_EXPR }; | |||
2657 | Stmt *SubExprs[END_EXPR]; | |||
2658 | ||||
2659 | bool lhsIsBase() const { return getRHS()->getType()->isIntegerType(); } | |||
2660 | ||||
2661 | public: | |||
2662 | ArraySubscriptExpr(Expr *lhs, Expr *rhs, QualType t, ExprValueKind VK, | |||
2663 | ExprObjectKind OK, SourceLocation rbracketloc) | |||
2664 | : Expr(ArraySubscriptExprClass, t, VK, OK) { | |||
2665 | SubExprs[LHS] = lhs; | |||
2666 | SubExprs[RHS] = rhs; | |||
2667 | ArrayOrMatrixSubscriptExprBits.RBracketLoc = rbracketloc; | |||
2668 | setDependence(computeDependence(this)); | |||
2669 | } | |||
2670 | ||||
2671 | /// Create an empty array subscript expression. | |||
2672 | explicit ArraySubscriptExpr(EmptyShell Shell) | |||
2673 | : Expr(ArraySubscriptExprClass, Shell) { } | |||
2674 | ||||
2675 | /// An array access can be written A[4] or 4[A] (both are equivalent). | |||
2676 | /// - getBase() and getIdx() always present the normalized view: A[4]. | |||
2677 | /// In this case getBase() returns "A" and getIdx() returns "4". | |||
2678 | /// - getLHS() and getRHS() present the syntactic view. e.g. for | |||
2679 | /// 4[A] getLHS() returns "4". | |||
2680 | /// Note: Because vector element access is also written A[4] we must | |||
2681 | /// predicate the format conversion in getBase and getIdx only on the | |||
2682 | /// the type of the RHS, as it is possible for the LHS to be a vector of | |||
2683 | /// integer type | |||
2684 | Expr *getLHS() { return cast<Expr>(SubExprs[LHS]); } | |||
2685 | const Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } | |||
2686 | void setLHS(Expr *E) { SubExprs[LHS] = E; } | |||
2687 | ||||
2688 | Expr *getRHS() { return cast<Expr>(SubExprs[RHS]); } | |||
2689 | const Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } | |||
2690 | void setRHS(Expr *E) { SubExprs[RHS] = E; } | |||
2691 | ||||
2692 | Expr *getBase() { return lhsIsBase() ? getLHS() : getRHS(); } | |||
2693 | const Expr *getBase() const { return lhsIsBase() ? getLHS() : getRHS(); } | |||
2694 | ||||
2695 | Expr *getIdx() { return lhsIsBase() ? getRHS() : getLHS(); } | |||
2696 | const Expr *getIdx() const { return lhsIsBase() ? getRHS() : getLHS(); } | |||
2697 | ||||
2698 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
2699 | return getLHS()->getBeginLoc(); | |||
2700 | } | |||
2701 | SourceLocation getEndLoc() const { return getRBracketLoc(); } | |||
2702 | ||||
2703 | SourceLocation getRBracketLoc() const { | |||
2704 | return ArrayOrMatrixSubscriptExprBits.RBracketLoc; | |||
2705 | } | |||
2706 | void setRBracketLoc(SourceLocation L) { | |||
2707 | ArrayOrMatrixSubscriptExprBits.RBracketLoc = L; | |||
2708 | } | |||
2709 | ||||
2710 | SourceLocation getExprLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
2711 | return getBase()->getExprLoc(); | |||
2712 | } | |||
2713 | ||||
2714 | static bool classof(const Stmt *T) { | |||
2715 | return T->getStmtClass() == ArraySubscriptExprClass; | |||
2716 | } | |||
2717 | ||||
2718 | // Iterators | |||
2719 | child_range children() { | |||
2720 | return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); | |||
2721 | } | |||
2722 | const_child_range children() const { | |||
2723 | return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); | |||
2724 | } | |||
2725 | }; | |||
2726 | ||||
2727 | /// MatrixSubscriptExpr - Matrix subscript expression for the MatrixType | |||
2728 | /// extension. | |||
2729 | /// MatrixSubscriptExpr can be either incomplete (only Base and RowIdx are set | |||
2730 | /// so far, the type is IncompleteMatrixIdx) or complete (Base, RowIdx and | |||
2731 | /// ColumnIdx refer to valid expressions). Incomplete matrix expressions only | |||
2732 | /// exist during the initial construction of the AST. | |||
2733 | class MatrixSubscriptExpr : public Expr { | |||
2734 | enum { BASE, ROW_IDX, COLUMN_IDX, END_EXPR }; | |||
2735 | Stmt *SubExprs[END_EXPR]; | |||
2736 | ||||
2737 | public: | |||
2738 | MatrixSubscriptExpr(Expr *Base, Expr *RowIdx, Expr *ColumnIdx, QualType T, | |||
2739 | SourceLocation RBracketLoc) | |||
2740 | : Expr(MatrixSubscriptExprClass, T, Base->getValueKind(), | |||
2741 | OK_MatrixComponent) { | |||
2742 | SubExprs[BASE] = Base; | |||
2743 | SubExprs[ROW_IDX] = RowIdx; | |||
2744 | SubExprs[COLUMN_IDX] = ColumnIdx; | |||
2745 | ArrayOrMatrixSubscriptExprBits.RBracketLoc = RBracketLoc; | |||
2746 | setDependence(computeDependence(this)); | |||
2747 | } | |||
2748 | ||||
2749 | /// Create an empty matrix subscript expression. | |||
2750 | explicit MatrixSubscriptExpr(EmptyShell Shell) | |||
2751 | : Expr(MatrixSubscriptExprClass, Shell) {} | |||
2752 | ||||
2753 | bool isIncomplete() const { | |||
2754 | bool IsIncomplete = hasPlaceholderType(BuiltinType::IncompleteMatrixIdx); | |||
2755 | assert((SubExprs[COLUMN_IDX] || IsIncomplete) &&(static_cast <bool> ((SubExprs[COLUMN_IDX] || IsIncomplete ) && "expressions without column index must be marked as incomplete" ) ? void (0) : __assert_fail ("(SubExprs[COLUMN_IDX] || IsIncomplete) && \"expressions without column index must be marked as incomplete\"" , "clang/include/clang/AST/Expr.h", 2756, __extension__ __PRETTY_FUNCTION__ )) | |||
2756 | "expressions without column index must be marked as incomplete")(static_cast <bool> ((SubExprs[COLUMN_IDX] || IsIncomplete ) && "expressions without column index must be marked as incomplete" ) ? void (0) : __assert_fail ("(SubExprs[COLUMN_IDX] || IsIncomplete) && \"expressions without column index must be marked as incomplete\"" , "clang/include/clang/AST/Expr.h", 2756, __extension__ __PRETTY_FUNCTION__ )); | |||
2757 | return IsIncomplete; | |||
2758 | } | |||
2759 | Expr *getBase() { return cast<Expr>(SubExprs[BASE]); } | |||
2760 | const Expr *getBase() const { return cast<Expr>(SubExprs[BASE]); } | |||
2761 | void setBase(Expr *E) { SubExprs[BASE] = E; } | |||
2762 | ||||
2763 | Expr *getRowIdx() { return cast<Expr>(SubExprs[ROW_IDX]); } | |||
2764 | const Expr *getRowIdx() const { return cast<Expr>(SubExprs[ROW_IDX]); } | |||
2765 | void setRowIdx(Expr *E) { SubExprs[ROW_IDX] = E; } | |||
2766 | ||||
2767 | Expr *getColumnIdx() { return cast_or_null<Expr>(SubExprs[COLUMN_IDX]); } | |||
2768 | const Expr *getColumnIdx() const { | |||
2769 | assert(!isIncomplete() &&(static_cast <bool> (!isIncomplete() && "cannot get the column index of an incomplete expression" ) ? void (0) : __assert_fail ("!isIncomplete() && \"cannot get the column index of an incomplete expression\"" , "clang/include/clang/AST/Expr.h", 2770, __extension__ __PRETTY_FUNCTION__ )) | |||
2770 | "cannot get the column index of an incomplete expression")(static_cast <bool> (!isIncomplete() && "cannot get the column index of an incomplete expression" ) ? void (0) : __assert_fail ("!isIncomplete() && \"cannot get the column index of an incomplete expression\"" , "clang/include/clang/AST/Expr.h", 2770, __extension__ __PRETTY_FUNCTION__ )); | |||
2771 | return cast<Expr>(SubExprs[COLUMN_IDX]); | |||
2772 | } | |||
2773 | void setColumnIdx(Expr *E) { SubExprs[COLUMN_IDX] = E; } | |||
2774 | ||||
2775 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
2776 | return getBase()->getBeginLoc(); | |||
2777 | } | |||
2778 | ||||
2779 | SourceLocation getEndLoc() const { return getRBracketLoc(); } | |||
2780 | ||||
2781 | SourceLocation getExprLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
2782 | return getBase()->getExprLoc(); | |||
2783 | } | |||
2784 | ||||
2785 | SourceLocation getRBracketLoc() const { | |||
2786 | return ArrayOrMatrixSubscriptExprBits.RBracketLoc; | |||
2787 | } | |||
2788 | void setRBracketLoc(SourceLocation L) { | |||
2789 | ArrayOrMatrixSubscriptExprBits.RBracketLoc = L; | |||
2790 | } | |||
2791 | ||||
2792 | static bool classof(const Stmt *T) { | |||
2793 | return T->getStmtClass() == MatrixSubscriptExprClass; | |||
2794 | } | |||
2795 | ||||
2796 | // Iterators | |||
2797 | child_range children() { | |||
2798 | return child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); | |||
2799 | } | |||
2800 | const_child_range children() const { | |||
2801 | return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); | |||
2802 | } | |||
2803 | }; | |||
2804 | ||||
2805 | /// CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]). | |||
2806 | /// CallExpr itself represents a normal function call, e.g., "f(x, 2)", | |||
2807 | /// while its subclasses may represent alternative syntax that (semantically) | |||
2808 | /// results in a function call. For example, CXXOperatorCallExpr is | |||
2809 | /// a subclass for overloaded operator calls that use operator syntax, e.g., | |||
2810 | /// "str1 + str2" to resolve to a function call. | |||
2811 | class CallExpr : public Expr { | |||
2812 | enum { FN = 0, PREARGS_START = 1 }; | |||
2813 | ||||
2814 | /// The number of arguments in the call expression. | |||
2815 | unsigned NumArgs; | |||
2816 | ||||
2817 | /// The location of the right parentheses. This has a different meaning for | |||
2818 | /// the derived classes of CallExpr. | |||
2819 | SourceLocation RParenLoc; | |||
2820 | ||||
2821 | // CallExpr store some data in trailing objects. However since CallExpr | |||
2822 | // is used a base of other expression classes we cannot use | |||
2823 | // llvm::TrailingObjects. Instead we manually perform the pointer arithmetic | |||
2824 | // and casts. | |||
2825 | // | |||
2826 | // The trailing objects are in order: | |||
2827 | // | |||
2828 | // * A single "Stmt *" for the callee expression. | |||
2829 | // | |||
2830 | // * An array of getNumPreArgs() "Stmt *" for the pre-argument expressions. | |||
2831 | // | |||
2832 | // * An array of getNumArgs() "Stmt *" for the argument expressions. | |||
2833 | // | |||
2834 | // * An optional of type FPOptionsOverride. | |||
2835 | // | |||
2836 | // Note that we store the offset in bytes from the this pointer to the start | |||
2837 | // of the trailing objects. It would be perfectly possible to compute it | |||
2838 | // based on the dynamic kind of the CallExpr. However 1.) we have plenty of | |||
2839 | // space in the bit-fields of Stmt. 2.) It was benchmarked to be faster to | |||
2840 | // compute this once and then load the offset from the bit-fields of Stmt, | |||
2841 | // instead of re-computing the offset each time the trailing objects are | |||
2842 | // accessed. | |||
2843 | ||||
2844 | /// Return a pointer to the start of the trailing array of "Stmt *". | |||
2845 | Stmt **getTrailingStmts() { | |||
2846 | return reinterpret_cast<Stmt **>(reinterpret_cast<char *>(this) + | |||
2847 | CallExprBits.OffsetToTrailingObjects); | |||
2848 | } | |||
2849 | Stmt *const *getTrailingStmts() const { | |||
2850 | return const_cast<CallExpr *>(this)->getTrailingStmts(); | |||
2851 | } | |||
2852 | ||||
2853 | /// Map a statement class to the appropriate offset in bytes from the | |||
2854 | /// this pointer to the trailing objects. | |||
2855 | static unsigned offsetToTrailingObjects(StmtClass SC); | |||
2856 | ||||
2857 | unsigned getSizeOfTrailingStmts() const { | |||
2858 | return (1 + getNumPreArgs() + getNumArgs()) * sizeof(Stmt *); | |||
2859 | } | |||
2860 | ||||
2861 | size_t getOffsetOfTrailingFPFeatures() const { | |||
2862 | assert(hasStoredFPFeatures())(static_cast <bool> (hasStoredFPFeatures()) ? void (0) : __assert_fail ("hasStoredFPFeatures()", "clang/include/clang/AST/Expr.h" , 2862, __extension__ __PRETTY_FUNCTION__)); | |||
2863 | return CallExprBits.OffsetToTrailingObjects + getSizeOfTrailingStmts(); | |||
2864 | } | |||
2865 | ||||
2866 | public: | |||
2867 | enum class ADLCallKind : bool { NotADL, UsesADL }; | |||
2868 | static constexpr ADLCallKind NotADL = ADLCallKind::NotADL; | |||
2869 | static constexpr ADLCallKind UsesADL = ADLCallKind::UsesADL; | |||
2870 | ||||
2871 | protected: | |||
2872 | /// Build a call expression, assuming that appropriate storage has been | |||
2873 | /// allocated for the trailing objects. | |||
2874 | CallExpr(StmtClass SC, Expr *Fn, ArrayRef<Expr *> PreArgs, | |||
2875 | ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK, | |||
2876 | SourceLocation RParenLoc, FPOptionsOverride FPFeatures, | |||
2877 | unsigned MinNumArgs, ADLCallKind UsesADL); | |||
2878 | ||||
2879 | /// Build an empty call expression, for deserialization. | |||
2880 | CallExpr(StmtClass SC, unsigned NumPreArgs, unsigned NumArgs, | |||
2881 | bool hasFPFeatures, EmptyShell Empty); | |||
2882 | ||||
2883 | /// Return the size in bytes needed for the trailing objects. | |||
2884 | /// Used by the derived classes to allocate the right amount of storage. | |||
2885 | static unsigned sizeOfTrailingObjects(unsigned NumPreArgs, unsigned NumArgs, | |||
2886 | bool HasFPFeatures) { | |||
2887 | return (1 + NumPreArgs + NumArgs) * sizeof(Stmt *) + | |||
2888 | HasFPFeatures * sizeof(FPOptionsOverride); | |||
2889 | } | |||
2890 | ||||
2891 | Stmt *getPreArg(unsigned I) { | |||
2892 | assert(I < getNumPreArgs() && "Prearg access out of range!")(static_cast <bool> (I < getNumPreArgs() && "Prearg access out of range!" ) ? void (0) : __assert_fail ("I < getNumPreArgs() && \"Prearg access out of range!\"" , "clang/include/clang/AST/Expr.h", 2892, __extension__ __PRETTY_FUNCTION__ )); | |||
2893 | return getTrailingStmts()[PREARGS_START + I]; | |||
2894 | } | |||
2895 | const Stmt *getPreArg(unsigned I) const { | |||
2896 | assert(I < getNumPreArgs() && "Prearg access out of range!")(static_cast <bool> (I < getNumPreArgs() && "Prearg access out of range!" ) ? void (0) : __assert_fail ("I < getNumPreArgs() && \"Prearg access out of range!\"" , "clang/include/clang/AST/Expr.h", 2896, __extension__ __PRETTY_FUNCTION__ )); | |||
2897 | return getTrailingStmts()[PREARGS_START + I]; | |||
2898 | } | |||
2899 | void setPreArg(unsigned I, Stmt *PreArg) { | |||
2900 | assert(I < getNumPreArgs() && "Prearg access out of range!")(static_cast <bool> (I < getNumPreArgs() && "Prearg access out of range!" ) ? void (0) : __assert_fail ("I < getNumPreArgs() && \"Prearg access out of range!\"" , "clang/include/clang/AST/Expr.h", 2900, __extension__ __PRETTY_FUNCTION__ )); | |||
2901 | getTrailingStmts()[PREARGS_START + I] = PreArg; | |||
2902 | } | |||
2903 | ||||
2904 | unsigned getNumPreArgs() const { return CallExprBits.NumPreArgs; } | |||
2905 | ||||
2906 | /// Return a pointer to the trailing FPOptions | |||
2907 | FPOptionsOverride *getTrailingFPFeatures() { | |||
2908 | assert(hasStoredFPFeatures())(static_cast <bool> (hasStoredFPFeatures()) ? void (0) : __assert_fail ("hasStoredFPFeatures()", "clang/include/clang/AST/Expr.h" , 2908, __extension__ __PRETTY_FUNCTION__)); | |||
2909 | return reinterpret_cast<FPOptionsOverride *>( | |||
2910 | reinterpret_cast<char *>(this) + CallExprBits.OffsetToTrailingObjects + | |||
2911 | getSizeOfTrailingStmts()); | |||
2912 | } | |||
2913 | const FPOptionsOverride *getTrailingFPFeatures() const { | |||
2914 | assert(hasStoredFPFeatures())(static_cast <bool> (hasStoredFPFeatures()) ? void (0) : __assert_fail ("hasStoredFPFeatures()", "clang/include/clang/AST/Expr.h" , 2914, __extension__ __PRETTY_FUNCTION__)); | |||
2915 | return reinterpret_cast<const FPOptionsOverride *>( | |||
2916 | reinterpret_cast<const char *>(this) + | |||
2917 | CallExprBits.OffsetToTrailingObjects + getSizeOfTrailingStmts()); | |||
2918 | } | |||
2919 | ||||
2920 | public: | |||
2921 | /// Create a call expression. | |||
2922 | /// \param Fn The callee expression, | |||
2923 | /// \param Args The argument array, | |||
2924 | /// \param Ty The type of the call expression (which is *not* the return | |||
2925 | /// type in general), | |||
2926 | /// \param VK The value kind of the call expression (lvalue, rvalue, ...), | |||
2927 | /// \param RParenLoc The location of the right parenthesis in the call | |||
2928 | /// expression. | |||
2929 | /// \param FPFeatures Floating-point features associated with the call, | |||
2930 | /// \param MinNumArgs Specifies the minimum number of arguments. The actual | |||
2931 | /// number of arguments will be the greater of Args.size() | |||
2932 | /// and MinNumArgs. This is used in a few places to allocate | |||
2933 | /// enough storage for the default arguments. | |||
2934 | /// \param UsesADL Specifies whether the callee was found through | |||
2935 | /// argument-dependent lookup. | |||
2936 | /// | |||
2937 | /// Note that you can use CreateTemporary if you need a temporary call | |||
2938 | /// expression on the stack. | |||
2939 | static CallExpr *Create(const ASTContext &Ctx, Expr *Fn, | |||
2940 | ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK, | |||
2941 | SourceLocation RParenLoc, | |||
2942 | FPOptionsOverride FPFeatures, unsigned MinNumArgs = 0, | |||
2943 | ADLCallKind UsesADL = NotADL); | |||
2944 | ||||
2945 | /// Create a temporary call expression with no arguments in the memory | |||
2946 | /// pointed to by Mem. Mem must points to at least sizeof(CallExpr) | |||
2947 | /// + sizeof(Stmt *) bytes of storage, aligned to alignof(CallExpr): | |||
2948 | /// | |||
2949 | /// \code{.cpp} | |||
2950 | /// alignas(CallExpr) char Buffer[sizeof(CallExpr) + sizeof(Stmt *)]; | |||
2951 | /// CallExpr *TheCall = CallExpr::CreateTemporary(Buffer, etc); | |||
2952 | /// \endcode | |||
2953 | static CallExpr *CreateTemporary(void *Mem, Expr *Fn, QualType Ty, | |||
2954 | ExprValueKind VK, SourceLocation RParenLoc, | |||
2955 | ADLCallKind UsesADL = NotADL); | |||
2956 | ||||
2957 | /// Create an empty call expression, for deserialization. | |||
2958 | static CallExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs, | |||
2959 | bool HasFPFeatures, EmptyShell Empty); | |||
2960 | ||||
2961 | Expr *getCallee() { return cast<Expr>(getTrailingStmts()[FN]); } | |||
2962 | const Expr *getCallee() const { return cast<Expr>(getTrailingStmts()[FN]); } | |||
2963 | void setCallee(Expr *F) { getTrailingStmts()[FN] = F; } | |||
2964 | ||||
2965 | ADLCallKind getADLCallKind() const { | |||
2966 | return static_cast<ADLCallKind>(CallExprBits.UsesADL); | |||
2967 | } | |||
2968 | void setADLCallKind(ADLCallKind V = UsesADL) { | |||
2969 | CallExprBits.UsesADL = static_cast<bool>(V); | |||
2970 | } | |||
2971 | bool usesADL() const { return getADLCallKind() == UsesADL; } | |||
2972 | ||||
2973 | bool hasStoredFPFeatures() const { return CallExprBits.HasFPFeatures; } | |||
2974 | ||||
2975 | Decl *getCalleeDecl() { return getCallee()->getReferencedDeclOfCallee(); } | |||
2976 | const Decl *getCalleeDecl() const { | |||
2977 | return getCallee()->getReferencedDeclOfCallee(); | |||
2978 | } | |||
2979 | ||||
2980 | /// If the callee is a FunctionDecl, return it. Otherwise return null. | |||
2981 | FunctionDecl *getDirectCallee() { | |||
2982 | return dyn_cast_or_null<FunctionDecl>(getCalleeDecl()); | |||
2983 | } | |||
2984 | const FunctionDecl *getDirectCallee() const { | |||
2985 | return dyn_cast_or_null<FunctionDecl>(getCalleeDecl()); | |||
2986 | } | |||
2987 | ||||
2988 | /// getNumArgs - Return the number of actual arguments to this call. | |||
2989 | unsigned getNumArgs() const { return NumArgs; } | |||
2990 | ||||
2991 | /// Retrieve the call arguments. | |||
2992 | Expr **getArgs() { | |||
2993 | return reinterpret_cast<Expr **>(getTrailingStmts() + PREARGS_START + | |||
2994 | getNumPreArgs()); | |||
2995 | } | |||
2996 | const Expr *const *getArgs() const { | |||
2997 | return reinterpret_cast<const Expr *const *>( | |||
2998 | getTrailingStmts() + PREARGS_START + getNumPreArgs()); | |||
2999 | } | |||
3000 | ||||
3001 | /// getArg - Return the specified argument. | |||
3002 | Expr *getArg(unsigned Arg) { | |||
3003 | assert(Arg < getNumArgs() && "Arg access out of range!")(static_cast <bool> (Arg < getNumArgs() && "Arg access out of range!" ) ? void (0) : __assert_fail ("Arg < getNumArgs() && \"Arg access out of range!\"" , "clang/include/clang/AST/Expr.h", 3003, __extension__ __PRETTY_FUNCTION__ )); | |||
3004 | return getArgs()[Arg]; | |||
3005 | } | |||
3006 | const Expr *getArg(unsigned Arg) const { | |||
3007 | assert(Arg < getNumArgs() && "Arg access out of range!")(static_cast <bool> (Arg < getNumArgs() && "Arg access out of range!" ) ? void (0) : __assert_fail ("Arg < getNumArgs() && \"Arg access out of range!\"" , "clang/include/clang/AST/Expr.h", 3007, __extension__ __PRETTY_FUNCTION__ )); | |||
3008 | return getArgs()[Arg]; | |||
3009 | } | |||
3010 | ||||
3011 | /// setArg - Set the specified argument. | |||
3012 | /// ! the dependence bits might be stale after calling this setter, it is | |||
3013 | /// *caller*'s responsibility to recompute them by calling | |||
3014 | /// computeDependence(). | |||
3015 | void setArg(unsigned Arg, Expr *ArgExpr) { | |||
3016 | assert(Arg < getNumArgs() && "Arg access out of range!")(static_cast <bool> (Arg < getNumArgs() && "Arg access out of range!" ) ? void (0) : __assert_fail ("Arg < getNumArgs() && \"Arg access out of range!\"" , "clang/include/clang/AST/Expr.h", 3016, __extension__ __PRETTY_FUNCTION__ )); | |||
3017 | getArgs()[Arg] = ArgExpr; | |||
3018 | } | |||
3019 | ||||
3020 | /// Compute and set dependence bits. | |||
3021 | void computeDependence() { | |||
3022 | setDependence(clang::computeDependence( | |||
3023 | this, llvm::ArrayRef( | |||
3024 | reinterpret_cast<Expr **>(getTrailingStmts() + PREARGS_START), | |||
3025 | getNumPreArgs()))); | |||
3026 | } | |||
3027 | ||||
3028 | /// Reduce the number of arguments in this call expression. This is used for | |||
3029 | /// example during error recovery to drop extra arguments. There is no way | |||
3030 | /// to perform the opposite because: 1.) We don't track how much storage | |||
3031 | /// we have for the argument array 2.) This would potentially require growing | |||
3032 | /// the argument array, something we cannot support since the arguments are | |||
3033 | /// stored in a trailing array. | |||
3034 | void shrinkNumArgs(unsigned NewNumArgs) { | |||
3035 | assert((NewNumArgs <= getNumArgs()) &&(static_cast <bool> ((NewNumArgs <= getNumArgs()) && "shrinkNumArgs cannot increase the number of arguments!") ? void (0) : __assert_fail ("(NewNumArgs <= getNumArgs()) && \"shrinkNumArgs cannot increase the number of arguments!\"" , "clang/include/clang/AST/Expr.h", 3036, __extension__ __PRETTY_FUNCTION__ )) | |||
3036 | "shrinkNumArgs cannot increase the number of arguments!")(static_cast <bool> ((NewNumArgs <= getNumArgs()) && "shrinkNumArgs cannot increase the number of arguments!") ? void (0) : __assert_fail ("(NewNumArgs <= getNumArgs()) && \"shrinkNumArgs cannot increase the number of arguments!\"" , "clang/include/clang/AST/Expr.h", 3036, __extension__ __PRETTY_FUNCTION__ )); | |||
3037 | NumArgs = NewNumArgs; | |||
3038 | } | |||
3039 | ||||
3040 | /// Bluntly set a new number of arguments without doing any checks whatsoever. | |||
3041 | /// Only used during construction of a CallExpr in a few places in Sema. | |||
3042 | /// FIXME: Find a way to remove it. | |||
3043 | void setNumArgsUnsafe(unsigned NewNumArgs) { NumArgs = NewNumArgs; } | |||
3044 | ||||
3045 | typedef ExprIterator arg_iterator; | |||
3046 | typedef ConstExprIterator const_arg_iterator; | |||
3047 | typedef llvm::iterator_range<arg_iterator> arg_range; | |||
3048 | typedef llvm::iterator_range<const_arg_iterator> const_arg_range; | |||
3049 | ||||
3050 | arg_range arguments() { return arg_range(arg_begin(), arg_end()); } | |||
3051 | const_arg_range arguments() const { | |||
3052 | return const_arg_range(arg_begin(), arg_end()); | |||
3053 | } | |||
3054 | ||||
3055 | arg_iterator arg_begin() { | |||
3056 | return getTrailingStmts() + PREARGS_START + getNumPreArgs(); | |||
3057 | } | |||
3058 | arg_iterator arg_end() { return arg_begin() + getNumArgs(); } | |||
3059 | ||||
3060 | const_arg_iterator arg_begin() const { | |||
3061 | return getTrailingStmts() + PREARGS_START + getNumPreArgs(); | |||
3062 | } | |||
3063 | const_arg_iterator arg_end() const { return arg_begin() + getNumArgs(); } | |||
3064 | ||||
3065 | /// This method provides fast access to all the subexpressions of | |||
3066 | /// a CallExpr without going through the slower virtual child_iterator | |||
3067 | /// interface. This provides efficient reverse iteration of the | |||
3068 | /// subexpressions. This is currently used for CFG construction. | |||
3069 | ArrayRef<Stmt *> getRawSubExprs() { | |||
3070 | return llvm::ArrayRef(getTrailingStmts(), | |||
3071 | PREARGS_START + getNumPreArgs() + getNumArgs()); | |||
3072 | } | |||
3073 | ||||
3074 | /// Get FPOptionsOverride from trailing storage. | |||
3075 | FPOptionsOverride getStoredFPFeatures() const { | |||
3076 | assert(hasStoredFPFeatures())(static_cast <bool> (hasStoredFPFeatures()) ? void (0) : __assert_fail ("hasStoredFPFeatures()", "clang/include/clang/AST/Expr.h" , 3076, __extension__ __PRETTY_FUNCTION__)); | |||
3077 | return *getTrailingFPFeatures(); | |||
3078 | } | |||
3079 | /// Set FPOptionsOverride in trailing storage. Used only by Serialization. | |||
3080 | void setStoredFPFeatures(FPOptionsOverride F) { | |||
3081 | assert(hasStoredFPFeatures())(static_cast <bool> (hasStoredFPFeatures()) ? void (0) : __assert_fail ("hasStoredFPFeatures()", "clang/include/clang/AST/Expr.h" , 3081, __extension__ __PRETTY_FUNCTION__)); | |||
3082 | *getTrailingFPFeatures() = F; | |||
3083 | } | |||
3084 | ||||
3085 | /// Get the FP features status of this operator. Only meaningful for | |||
3086 | /// operations on floating point types. | |||
3087 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const { | |||
3088 | if (hasStoredFPFeatures()) | |||
3089 | return getStoredFPFeatures().applyOverrides(LO); | |||
3090 | return FPOptions::defaultWithoutTrailingStorage(LO); | |||
3091 | } | |||
3092 | ||||
3093 | FPOptionsOverride getFPFeatures() const { | |||
3094 | if (hasStoredFPFeatures()) | |||
3095 | return getStoredFPFeatures(); | |||
3096 | return FPOptionsOverride(); | |||
3097 | } | |||
3098 | ||||
3099 | /// getBuiltinCallee - If this is a call to a builtin, return the builtin ID | |||
3100 | /// of the callee. If not, return 0. | |||
3101 | unsigned getBuiltinCallee() const; | |||
3102 | ||||
3103 | /// Returns \c true if this is a call to a builtin which does not | |||
3104 | /// evaluate side-effects within its arguments. | |||
3105 | bool isUnevaluatedBuiltinCall(const ASTContext &Ctx) const; | |||
3106 | ||||
3107 | /// getCallReturnType - Get the return type of the call expr. This is not | |||
3108 | /// always the type of the expr itself, if the return type is a reference | |||
3109 | /// type. | |||
3110 | QualType getCallReturnType(const ASTContext &Ctx) const; | |||
3111 | ||||
3112 | /// Returns the WarnUnusedResultAttr that is either declared on the called | |||
3113 | /// function, or its return type declaration. | |||
3114 | const Attr *getUnusedResultAttr(const ASTContext &Ctx) const; | |||
3115 | ||||
3116 | /// Returns true if this call expression should warn on unused results. | |||
3117 | bool hasUnusedResultAttr(const ASTContext &Ctx) const { | |||
3118 | return getUnusedResultAttr(Ctx) != nullptr; | |||
3119 | } | |||
3120 | ||||
3121 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
3122 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } | |||
3123 | ||||
3124 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
3125 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
3126 | ||||
3127 | /// Return true if this is a call to __assume() or __builtin_assume() with | |||
3128 | /// a non-value-dependent constant parameter evaluating as false. | |||
3129 | bool isBuiltinAssumeFalse(const ASTContext &Ctx) const; | |||
3130 | ||||
3131 | /// Used by Sema to implement MSVC-compatible delayed name lookup. | |||
3132 | /// (Usually Exprs themselves should set dependence). | |||
3133 | void markDependentForPostponedNameLookup() { | |||
3134 | setDependence(getDependence() | ExprDependence::TypeValueInstantiation); | |||
3135 | } | |||
3136 | ||||
3137 | bool isCallToStdMove() const; | |||
3138 | ||||
3139 | static bool classof(const Stmt *T) { | |||
3140 | return T->getStmtClass() >= firstCallExprConstant && | |||
3141 | T->getStmtClass() <= lastCallExprConstant; | |||
3142 | } | |||
3143 | ||||
3144 | // Iterators | |||
3145 | child_range children() { | |||
3146 | return child_range(getTrailingStmts(), getTrailingStmts() + PREARGS_START + | |||
3147 | getNumPreArgs() + getNumArgs()); | |||
3148 | } | |||
3149 | ||||
3150 | const_child_range children() const { | |||
3151 | return const_child_range(getTrailingStmts(), | |||
3152 | getTrailingStmts() + PREARGS_START + | |||
3153 | getNumPreArgs() + getNumArgs()); | |||
3154 | } | |||
3155 | }; | |||
3156 | ||||
3157 | /// Extra data stored in some MemberExpr objects. | |||
3158 | struct MemberExprNameQualifier { | |||
3159 | /// The nested-name-specifier that qualifies the name, including | |||
3160 | /// source-location information. | |||
3161 | NestedNameSpecifierLoc QualifierLoc; | |||
3162 | ||||
3163 | /// The DeclAccessPair through which the MemberDecl was found due to | |||
3164 | /// name qualifiers. | |||
3165 | DeclAccessPair FoundDecl; | |||
3166 | }; | |||
3167 | ||||
3168 | /// MemberExpr - [C99 6.5.2.3] Structure and Union Members. X->F and X.F. | |||
3169 | /// | |||
3170 | class MemberExpr final | |||
3171 | : public Expr, | |||
3172 | private llvm::TrailingObjects<MemberExpr, MemberExprNameQualifier, | |||
3173 | ASTTemplateKWAndArgsInfo, | |||
3174 | TemplateArgumentLoc> { | |||
3175 | friend class ASTReader; | |||
3176 | friend class ASTStmtReader; | |||
3177 | friend class ASTStmtWriter; | |||
3178 | friend TrailingObjects; | |||
3179 | ||||
3180 | /// Base - the expression for the base pointer or structure references. In | |||
3181 | /// X.F, this is "X". | |||
3182 | Stmt *Base; | |||
3183 | ||||
3184 | /// MemberDecl - This is the decl being referenced by the field/member name. | |||
3185 | /// In X.F, this is the decl referenced by F. | |||
3186 | ValueDecl *MemberDecl; | |||
3187 | ||||
3188 | /// MemberDNLoc - Provides source/type location info for the | |||
3189 | /// declaration name embedded in MemberDecl. | |||
3190 | DeclarationNameLoc MemberDNLoc; | |||
3191 | ||||
3192 | /// MemberLoc - This is the location of the member name. | |||
3193 | SourceLocation MemberLoc; | |||
3194 | ||||
3195 | size_t numTrailingObjects(OverloadToken<MemberExprNameQualifier>) const { | |||
3196 | return hasQualifierOrFoundDecl(); | |||
3197 | } | |||
3198 | ||||
3199 | size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const { | |||
3200 | return hasTemplateKWAndArgsInfo(); | |||
3201 | } | |||
3202 | ||||
3203 | bool hasQualifierOrFoundDecl() const { | |||
3204 | return MemberExprBits.HasQualifierOrFoundDecl; | |||
3205 | } | |||
3206 | ||||
3207 | bool hasTemplateKWAndArgsInfo() const { | |||
3208 | return MemberExprBits.HasTemplateKWAndArgsInfo; | |||
3209 | } | |||
3210 | ||||
3211 | MemberExpr(Expr *Base, bool IsArrow, SourceLocation OperatorLoc, | |||
3212 | ValueDecl *MemberDecl, const DeclarationNameInfo &NameInfo, | |||
3213 | QualType T, ExprValueKind VK, ExprObjectKind OK, | |||
3214 | NonOdrUseReason NOUR); | |||
3215 | MemberExpr(EmptyShell Empty) | |||
3216 | : Expr(MemberExprClass, Empty), Base(), MemberDecl() {} | |||
3217 | ||||
3218 | public: | |||
3219 | static MemberExpr *Create(const ASTContext &C, Expr *Base, bool IsArrow, | |||
3220 | SourceLocation OperatorLoc, | |||
3221 | NestedNameSpecifierLoc QualifierLoc, | |||
3222 | SourceLocation TemplateKWLoc, ValueDecl *MemberDecl, | |||
3223 | DeclAccessPair FoundDecl, | |||
3224 | DeclarationNameInfo MemberNameInfo, | |||
3225 | const TemplateArgumentListInfo *TemplateArgs, | |||
3226 | QualType T, ExprValueKind VK, ExprObjectKind OK, | |||
3227 | NonOdrUseReason NOUR); | |||
3228 | ||||
3229 | /// Create an implicit MemberExpr, with no location, qualifier, template | |||
3230 | /// arguments, and so on. Suitable only for non-static member access. | |||
3231 | static MemberExpr *CreateImplicit(const ASTContext &C, Expr *Base, | |||
3232 | bool IsArrow, ValueDecl *MemberDecl, | |||
3233 | QualType T, ExprValueKind VK, | |||
3234 | ExprObjectKind OK) { | |||
3235 | return Create(C, Base, IsArrow, SourceLocation(), NestedNameSpecifierLoc(), | |||
3236 | SourceLocation(), MemberDecl, | |||
3237 | DeclAccessPair::make(MemberDecl, MemberDecl->getAccess()), | |||
3238 | DeclarationNameInfo(), nullptr, T, VK, OK, NOUR_None); | |||
3239 | } | |||
3240 | ||||
3241 | static MemberExpr *CreateEmpty(const ASTContext &Context, bool HasQualifier, | |||
3242 | bool HasFoundDecl, | |||
3243 | bool HasTemplateKWAndArgsInfo, | |||
3244 | unsigned NumTemplateArgs); | |||
3245 | ||||
3246 | void setBase(Expr *E) { Base = E; } | |||
3247 | Expr *getBase() const { return cast<Expr>(Base); } | |||
3248 | ||||
3249 | /// Retrieve the member declaration to which this expression refers. | |||
3250 | /// | |||
3251 | /// The returned declaration will be a FieldDecl or (in C++) a VarDecl (for | |||
3252 | /// static data members), a CXXMethodDecl, or an EnumConstantDecl. | |||
3253 | ValueDecl *getMemberDecl() const { return MemberDecl; } | |||
3254 | void setMemberDecl(ValueDecl *D); | |||
3255 | ||||
3256 | /// Retrieves the declaration found by lookup. | |||
3257 | DeclAccessPair getFoundDecl() const { | |||
3258 | if (!hasQualifierOrFoundDecl()) | |||
3259 | return DeclAccessPair::make(getMemberDecl(), | |||
3260 | getMemberDecl()->getAccess()); | |||
3261 | return getTrailingObjects<MemberExprNameQualifier>()->FoundDecl; | |||
3262 | } | |||
3263 | ||||
3264 | /// Determines whether this member expression actually had | |||
3265 | /// a C++ nested-name-specifier prior to the name of the member, e.g., | |||
3266 | /// x->Base::foo. | |||
3267 | bool hasQualifier() const { return getQualifier() != nullptr; } | |||
3268 | ||||
3269 | /// If the member name was qualified, retrieves the | |||
3270 | /// nested-name-specifier that precedes the member name, with source-location | |||
3271 | /// information. | |||
3272 | NestedNameSpecifierLoc getQualifierLoc() const { | |||
3273 | if (!hasQualifierOrFoundDecl()) | |||
3274 | return NestedNameSpecifierLoc(); | |||
3275 | return getTrailingObjects<MemberExprNameQualifier>()->QualifierLoc; | |||
3276 | } | |||
3277 | ||||
3278 | /// If the member name was qualified, retrieves the | |||
3279 | /// nested-name-specifier that precedes the member name. Otherwise, returns | |||
3280 | /// NULL. | |||
3281 | NestedNameSpecifier *getQualifier() const { | |||
3282 | return getQualifierLoc().getNestedNameSpecifier(); | |||
3283 | } | |||
3284 | ||||
3285 | /// Retrieve the location of the template keyword preceding | |||
3286 | /// the member name, if any. | |||
3287 | SourceLocation getTemplateKeywordLoc() const { | |||
3288 | if (!hasTemplateKWAndArgsInfo()) | |||
3289 | return SourceLocation(); | |||
3290 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc; | |||
3291 | } | |||
3292 | ||||
3293 | /// Retrieve the location of the left angle bracket starting the | |||
3294 | /// explicit template argument list following the member name, if any. | |||
3295 | SourceLocation getLAngleLoc() const { | |||
3296 | if (!hasTemplateKWAndArgsInfo()) | |||
3297 | return SourceLocation(); | |||
3298 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc; | |||
3299 | } | |||
3300 | ||||
3301 | /// Retrieve the location of the right angle bracket ending the | |||
3302 | /// explicit template argument list following the member name, if any. | |||
3303 | SourceLocation getRAngleLoc() const { | |||
3304 | if (!hasTemplateKWAndArgsInfo()) | |||
3305 | return SourceLocation(); | |||
3306 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc; | |||
3307 | } | |||
3308 | ||||
3309 | /// Determines whether the member name was preceded by the template keyword. | |||
3310 | bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } | |||
3311 | ||||
3312 | /// Determines whether the member name was followed by an | |||
3313 | /// explicit template argument list. | |||
3314 | bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } | |||
3315 | ||||
3316 | /// Copies the template arguments (if present) into the given | |||
3317 | /// structure. | |||
3318 | void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { | |||
3319 | if (hasExplicitTemplateArgs()) | |||
3320 | getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto( | |||
3321 | getTrailingObjects<TemplateArgumentLoc>(), List); | |||
3322 | } | |||
3323 | ||||
3324 | /// Retrieve the template arguments provided as part of this | |||
3325 | /// template-id. | |||
3326 | const TemplateArgumentLoc *getTemplateArgs() const { | |||
3327 | if (!hasExplicitTemplateArgs()) | |||
3328 | return nullptr; | |||
3329 | ||||
3330 | return getTrailingObjects<TemplateArgumentLoc>(); | |||
3331 | } | |||
3332 | ||||
3333 | /// Retrieve the number of template arguments provided as part of this | |||
3334 | /// template-id. | |||
3335 | unsigned getNumTemplateArgs() const { | |||
3336 | if (!hasExplicitTemplateArgs()) | |||
3337 | return 0; | |||
3338 | ||||
3339 | return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs; | |||
3340 | } | |||
3341 | ||||
3342 | ArrayRef<TemplateArgumentLoc> template_arguments() const { | |||
3343 | return {getTemplateArgs(), getNumTemplateArgs()}; | |||
3344 | } | |||
3345 | ||||
3346 | /// Retrieve the member declaration name info. | |||
3347 | DeclarationNameInfo getMemberNameInfo() const { | |||
3348 | return DeclarationNameInfo(MemberDecl->getDeclName(), | |||
3349 | MemberLoc, MemberDNLoc); | |||
3350 | } | |||
3351 | ||||
3352 | SourceLocation getOperatorLoc() const { return MemberExprBits.OperatorLoc; } | |||
3353 | ||||
3354 | bool isArrow() const { return MemberExprBits.IsArrow; } | |||
3355 | void setArrow(bool A) { MemberExprBits.IsArrow = A; } | |||
3356 | ||||
3357 | /// getMemberLoc - Return the location of the "member", in X->F, it is the | |||
3358 | /// location of 'F'. | |||
3359 | SourceLocation getMemberLoc() const { return MemberLoc; } | |||
3360 | void setMemberLoc(SourceLocation L) { MemberLoc = L; } | |||
3361 | ||||
3362 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
3363 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
3364 | ||||
3365 | SourceLocation getExprLoc() const LLVM_READONLY__attribute__((__pure__)) { return MemberLoc; } | |||
3366 | ||||
3367 | /// Determine whether the base of this explicit is implicit. | |||
3368 | bool isImplicitAccess() const { | |||
3369 | return getBase() && getBase()->isImplicitCXXThis(); | |||
3370 | } | |||
3371 | ||||
3372 | /// Returns true if this member expression refers to a method that | |||
3373 | /// was resolved from an overloaded set having size greater than 1. | |||
3374 | bool hadMultipleCandidates() const { | |||
3375 | return MemberExprBits.HadMultipleCandidates; | |||
3376 | } | |||
3377 | /// Sets the flag telling whether this expression refers to | |||
3378 | /// a method that was resolved from an overloaded set having size | |||
3379 | /// greater than 1. | |||
3380 | void setHadMultipleCandidates(bool V = true) { | |||
3381 | MemberExprBits.HadMultipleCandidates = V; | |||
3382 | } | |||
3383 | ||||
3384 | /// Returns true if virtual dispatch is performed. | |||
3385 | /// If the member access is fully qualified, (i.e. X::f()), virtual | |||
3386 | /// dispatching is not performed. In -fapple-kext mode qualified | |||
3387 | /// calls to virtual method will still go through the vtable. | |||
3388 | bool performsVirtualDispatch(const LangOptions &LO) const { | |||
3389 | return LO.AppleKext || !hasQualifier(); | |||
3390 | } | |||
3391 | ||||
3392 | /// Is this expression a non-odr-use reference, and if so, why? | |||
3393 | /// This is only meaningful if the named member is a static member. | |||
3394 | NonOdrUseReason isNonOdrUse() const { | |||
3395 | return static_cast<NonOdrUseReason>(MemberExprBits.NonOdrUseReason); | |||
3396 | } | |||
3397 | ||||
3398 | static bool classof(const Stmt *T) { | |||
3399 | return T->getStmtClass() == MemberExprClass; | |||
3400 | } | |||
3401 | ||||
3402 | // Iterators | |||
3403 | child_range children() { return child_range(&Base, &Base+1); } | |||
3404 | const_child_range children() const { | |||
3405 | return const_child_range(&Base, &Base + 1); | |||
3406 | } | |||
3407 | }; | |||
3408 | ||||
3409 | /// CompoundLiteralExpr - [C99 6.5.2.5] | |||
3410 | /// | |||
3411 | class CompoundLiteralExpr : public Expr { | |||
3412 | /// LParenLoc - If non-null, this is the location of the left paren in a | |||
3413 | /// compound literal like "(int){4}". This can be null if this is a | |||
3414 | /// synthesized compound expression. | |||
3415 | SourceLocation LParenLoc; | |||
3416 | ||||
3417 | /// The type as written. This can be an incomplete array type, in | |||
3418 | /// which case the actual expression type will be different. | |||
3419 | /// The int part of the pair stores whether this expr is file scope. | |||
3420 | llvm::PointerIntPair<TypeSourceInfo *, 1, bool> TInfoAndScope; | |||
3421 | Stmt *Init; | |||
3422 | public: | |||
3423 | CompoundLiteralExpr(SourceLocation lparenloc, TypeSourceInfo *tinfo, | |||
3424 | QualType T, ExprValueKind VK, Expr *init, bool fileScope) | |||
3425 | : Expr(CompoundLiteralExprClass, T, VK, OK_Ordinary), | |||
3426 | LParenLoc(lparenloc), TInfoAndScope(tinfo, fileScope), Init(init) { | |||
3427 | setDependence(computeDependence(this)); | |||
3428 | } | |||
3429 | ||||
3430 | /// Construct an empty compound literal. | |||
3431 | explicit CompoundLiteralExpr(EmptyShell Empty) | |||
3432 | : Expr(CompoundLiteralExprClass, Empty) { } | |||
3433 | ||||
3434 | const Expr *getInitializer() const { return cast<Expr>(Init); } | |||
3435 | Expr *getInitializer() { return cast<Expr>(Init); } | |||
3436 | void setInitializer(Expr *E) { Init = E; } | |||
3437 | ||||
3438 | bool isFileScope() const { return TInfoAndScope.getInt(); } | |||
3439 | void setFileScope(bool FS) { TInfoAndScope.setInt(FS); } | |||
3440 | ||||
3441 | SourceLocation getLParenLoc() const { return LParenLoc; } | |||
3442 | void setLParenLoc(SourceLocation L) { LParenLoc = L; } | |||
3443 | ||||
3444 | TypeSourceInfo *getTypeSourceInfo() const { | |||
3445 | return TInfoAndScope.getPointer(); | |||
3446 | } | |||
3447 | void setTypeSourceInfo(TypeSourceInfo *tinfo) { | |||
3448 | TInfoAndScope.setPointer(tinfo); | |||
3449 | } | |||
3450 | ||||
3451 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
3452 | // FIXME: Init should never be null. | |||
3453 | if (!Init) | |||
3454 | return SourceLocation(); | |||
3455 | if (LParenLoc.isInvalid()) | |||
3456 | return Init->getBeginLoc(); | |||
3457 | return LParenLoc; | |||
3458 | } | |||
3459 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
3460 | // FIXME: Init should never be null. | |||
3461 | if (!Init) | |||
3462 | return SourceLocation(); | |||
3463 | return Init->getEndLoc(); | |||
3464 | } | |||
3465 | ||||
3466 | static bool classof(const Stmt *T) { | |||
3467 | return T->getStmtClass() == CompoundLiteralExprClass; | |||
3468 | } | |||
3469 | ||||
3470 | // Iterators | |||
3471 | child_range children() { return child_range(&Init, &Init+1); } | |||
3472 | const_child_range children() const { | |||
3473 | return const_child_range(&Init, &Init + 1); | |||
3474 | } | |||
3475 | }; | |||
3476 | ||||
3477 | /// CastExpr - Base class for type casts, including both implicit | |||
3478 | /// casts (ImplicitCastExpr) and explicit casts that have some | |||
3479 | /// representation in the source code (ExplicitCastExpr's derived | |||
3480 | /// classes). | |||
3481 | class CastExpr : public Expr { | |||
3482 | Stmt *Op; | |||
3483 | ||||
3484 | bool CastConsistency() const; | |||
3485 | ||||
3486 | const CXXBaseSpecifier * const *path_buffer() const { | |||
3487 | return const_cast<CastExpr*>(this)->path_buffer(); | |||
3488 | } | |||
3489 | CXXBaseSpecifier **path_buffer(); | |||
3490 | ||||
3491 | friend class ASTStmtReader; | |||
3492 | ||||
3493 | protected: | |||
3494 | CastExpr(StmtClass SC, QualType ty, ExprValueKind VK, const CastKind kind, | |||
3495 | Expr *op, unsigned BasePathSize, bool HasFPFeatures) | |||
3496 | : Expr(SC, ty, VK, OK_Ordinary), Op(op) { | |||
3497 | CastExprBits.Kind = kind; | |||
3498 | CastExprBits.PartOfExplicitCast = false; | |||
3499 | CastExprBits.BasePathSize = BasePathSize; | |||
3500 | assert((CastExprBits.BasePathSize == BasePathSize) &&(static_cast <bool> ((CastExprBits.BasePathSize == BasePathSize ) && "BasePathSize overflow!") ? void (0) : __assert_fail ("(CastExprBits.BasePathSize == BasePathSize) && \"BasePathSize overflow!\"" , "clang/include/clang/AST/Expr.h", 3501, __extension__ __PRETTY_FUNCTION__ )) | |||
3501 | "BasePathSize overflow!")(static_cast <bool> ((CastExprBits.BasePathSize == BasePathSize ) && "BasePathSize overflow!") ? void (0) : __assert_fail ("(CastExprBits.BasePathSize == BasePathSize) && \"BasePathSize overflow!\"" , "clang/include/clang/AST/Expr.h", 3501, __extension__ __PRETTY_FUNCTION__ )); | |||
3502 | assert(CastConsistency())(static_cast <bool> (CastConsistency()) ? void (0) : __assert_fail ("CastConsistency()", "clang/include/clang/AST/Expr.h", 3502 , __extension__ __PRETTY_FUNCTION__)); | |||
3503 | CastExprBits.HasFPFeatures = HasFPFeatures; | |||
3504 | } | |||
3505 | ||||
3506 | /// Construct an empty cast. | |||
3507 | CastExpr(StmtClass SC, EmptyShell Empty, unsigned BasePathSize, | |||
3508 | bool HasFPFeatures) | |||
3509 | : Expr(SC, Empty) { | |||
3510 | CastExprBits.PartOfExplicitCast = false; | |||
3511 | CastExprBits.BasePathSize = BasePathSize; | |||
3512 | CastExprBits.HasFPFeatures = HasFPFeatures; | |||
3513 | assert((CastExprBits.BasePathSize == BasePathSize) &&(static_cast <bool> ((CastExprBits.BasePathSize == BasePathSize ) && "BasePathSize overflow!") ? void (0) : __assert_fail ("(CastExprBits.BasePathSize == BasePathSize) && \"BasePathSize overflow!\"" , "clang/include/clang/AST/Expr.h", 3514, __extension__ __PRETTY_FUNCTION__ )) | |||
3514 | "BasePathSize overflow!")(static_cast <bool> ((CastExprBits.BasePathSize == BasePathSize ) && "BasePathSize overflow!") ? void (0) : __assert_fail ("(CastExprBits.BasePathSize == BasePathSize) && \"BasePathSize overflow!\"" , "clang/include/clang/AST/Expr.h", 3514, __extension__ __PRETTY_FUNCTION__ )); | |||
3515 | } | |||
3516 | ||||
3517 | /// Return a pointer to the trailing FPOptions. | |||
3518 | /// \pre hasStoredFPFeatures() == true | |||
3519 | FPOptionsOverride *getTrailingFPFeatures(); | |||
3520 | const FPOptionsOverride *getTrailingFPFeatures() const { | |||
3521 | return const_cast<CastExpr *>(this)->getTrailingFPFeatures(); | |||
3522 | } | |||
3523 | ||||
3524 | public: | |||
3525 | CastKind getCastKind() const { return (CastKind) CastExprBits.Kind; } | |||
3526 | void setCastKind(CastKind K) { CastExprBits.Kind = K; } | |||
3527 | ||||
3528 | static const char *getCastKindName(CastKind CK); | |||
3529 | const char *getCastKindName() const { return getCastKindName(getCastKind()); } | |||
3530 | ||||
3531 | Expr *getSubExpr() { return cast<Expr>(Op); } | |||
3532 | const Expr *getSubExpr() const { return cast<Expr>(Op); } | |||
3533 | void setSubExpr(Expr *E) { Op = E; } | |||
3534 | ||||
3535 | /// Retrieve the cast subexpression as it was written in the source | |||
3536 | /// code, looking through any implicit casts or other intermediate nodes | |||
3537 | /// introduced by semantic analysis. | |||
3538 | Expr *getSubExprAsWritten(); | |||
3539 | const Expr *getSubExprAsWritten() const { | |||
3540 | return const_cast<CastExpr *>(this)->getSubExprAsWritten(); | |||
3541 | } | |||
3542 | ||||
3543 | /// If this cast applies a user-defined conversion, retrieve the conversion | |||
3544 | /// function that it invokes. | |||
3545 | NamedDecl *getConversionFunction() const; | |||
3546 | ||||
3547 | typedef CXXBaseSpecifier **path_iterator; | |||
3548 | typedef const CXXBaseSpecifier *const *path_const_iterator; | |||
3549 | bool path_empty() const { return path_size() == 0; } | |||
3550 | unsigned path_size() const { return CastExprBits.BasePathSize; } | |||
3551 | path_iterator path_begin() { return path_buffer(); } | |||
3552 | path_iterator path_end() { return path_buffer() + path_size(); } | |||
3553 | path_const_iterator path_begin() const { return path_buffer(); } | |||
3554 | path_const_iterator path_end() const { return path_buffer() + path_size(); } | |||
3555 | ||||
3556 | llvm::iterator_range<path_iterator> path() { | |||
3557 | return llvm::make_range(path_begin(), path_end()); | |||
3558 | } | |||
3559 | llvm::iterator_range<path_const_iterator> path() const { | |||
3560 | return llvm::make_range(path_begin(), path_end()); | |||
3561 | } | |||
3562 | ||||
3563 | const FieldDecl *getTargetUnionField() const { | |||
3564 | assert(getCastKind() == CK_ToUnion)(static_cast <bool> (getCastKind() == CK_ToUnion) ? void (0) : __assert_fail ("getCastKind() == CK_ToUnion", "clang/include/clang/AST/Expr.h" , 3564, __extension__ __PRETTY_FUNCTION__)); | |||
3565 | return getTargetFieldForToUnionCast(getType(), getSubExpr()->getType()); | |||
3566 | } | |||
3567 | ||||
3568 | bool hasStoredFPFeatures() const { return CastExprBits.HasFPFeatures; } | |||
3569 | ||||
3570 | /// Get FPOptionsOverride from trailing storage. | |||
3571 | FPOptionsOverride getStoredFPFeatures() const { | |||
3572 | assert(hasStoredFPFeatures())(static_cast <bool> (hasStoredFPFeatures()) ? void (0) : __assert_fail ("hasStoredFPFeatures()", "clang/include/clang/AST/Expr.h" , 3572, __extension__ __PRETTY_FUNCTION__)); | |||
3573 | return *getTrailingFPFeatures(); | |||
3574 | } | |||
3575 | ||||
3576 | /// Get the FP features status of this operation. Only meaningful for | |||
3577 | /// operations on floating point types. | |||
3578 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const { | |||
3579 | if (hasStoredFPFeatures()) | |||
3580 | return getStoredFPFeatures().applyOverrides(LO); | |||
3581 | return FPOptions::defaultWithoutTrailingStorage(LO); | |||
3582 | } | |||
3583 | ||||
3584 | FPOptionsOverride getFPFeatures() const { | |||
3585 | if (hasStoredFPFeatures()) | |||
3586 | return getStoredFPFeatures(); | |||
3587 | return FPOptionsOverride(); | |||
3588 | } | |||
3589 | ||||
3590 | static const FieldDecl *getTargetFieldForToUnionCast(QualType unionType, | |||
3591 | QualType opType); | |||
3592 | static const FieldDecl *getTargetFieldForToUnionCast(const RecordDecl *RD, | |||
3593 | QualType opType); | |||
3594 | ||||
3595 | static bool classof(const Stmt *T) { | |||
3596 | return T->getStmtClass() >= firstCastExprConstant && | |||
3597 | T->getStmtClass() <= lastCastExprConstant; | |||
3598 | } | |||
3599 | ||||
3600 | // Iterators | |||
3601 | child_range children() { return child_range(&Op, &Op+1); } | |||
3602 | const_child_range children() const { return const_child_range(&Op, &Op + 1); } | |||
3603 | }; | |||
3604 | ||||
3605 | /// ImplicitCastExpr - Allows us to explicitly represent implicit type | |||
3606 | /// conversions, which have no direct representation in the original | |||
3607 | /// source code. For example: converting T[]->T*, void f()->void | |||
3608 | /// (*f)(), float->double, short->int, etc. | |||
3609 | /// | |||
3610 | /// In C, implicit casts always produce rvalues. However, in C++, an | |||
3611 | /// implicit cast whose result is being bound to a reference will be | |||
3612 | /// an lvalue or xvalue. For example: | |||
3613 | /// | |||
3614 | /// @code | |||
3615 | /// class Base { }; | |||
3616 | /// class Derived : public Base { }; | |||
3617 | /// Derived &&ref(); | |||
3618 | /// void f(Derived d) { | |||
3619 | /// Base& b = d; // initializer is an ImplicitCastExpr | |||
3620 | /// // to an lvalue of type Base | |||
3621 | /// Base&& r = ref(); // initializer is an ImplicitCastExpr | |||
3622 | /// // to an xvalue of type Base | |||
3623 | /// } | |||
3624 | /// @endcode | |||
3625 | class ImplicitCastExpr final | |||
3626 | : public CastExpr, | |||
3627 | private llvm::TrailingObjects<ImplicitCastExpr, CXXBaseSpecifier *, | |||
3628 | FPOptionsOverride> { | |||
3629 | ||||
3630 | ImplicitCastExpr(QualType ty, CastKind kind, Expr *op, | |||
3631 | unsigned BasePathLength, FPOptionsOverride FPO, | |||
3632 | ExprValueKind VK) | |||
3633 | : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, BasePathLength, | |||
3634 | FPO.requiresTrailingStorage()) { | |||
3635 | setDependence(computeDependence(this)); | |||
3636 | if (hasStoredFPFeatures()) | |||
3637 | *getTrailingFPFeatures() = FPO; | |||
3638 | } | |||
3639 | ||||
3640 | /// Construct an empty implicit cast. | |||
3641 | explicit ImplicitCastExpr(EmptyShell Shell, unsigned PathSize, | |||
3642 | bool HasFPFeatures) | |||
3643 | : CastExpr(ImplicitCastExprClass, Shell, PathSize, HasFPFeatures) {} | |||
3644 | ||||
3645 | unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const { | |||
3646 | return path_size(); | |||
3647 | } | |||
3648 | ||||
3649 | public: | |||
3650 | enum OnStack_t { OnStack }; | |||
3651 | ImplicitCastExpr(OnStack_t _, QualType ty, CastKind kind, Expr *op, | |||
3652 | ExprValueKind VK, FPOptionsOverride FPO) | |||
3653 | : CastExpr(ImplicitCastExprClass, ty, VK, kind, op, 0, | |||
3654 | FPO.requiresTrailingStorage()) { | |||
3655 | if (hasStoredFPFeatures()) | |||
3656 | *getTrailingFPFeatures() = FPO; | |||
3657 | } | |||
3658 | ||||
3659 | bool isPartOfExplicitCast() const { return CastExprBits.PartOfExplicitCast; } | |||
3660 | void setIsPartOfExplicitCast(bool PartOfExplicitCast) { | |||
3661 | CastExprBits.PartOfExplicitCast = PartOfExplicitCast; | |||
3662 | } | |||
3663 | ||||
3664 | static ImplicitCastExpr *Create(const ASTContext &Context, QualType T, | |||
3665 | CastKind Kind, Expr *Operand, | |||
3666 | const CXXCastPath *BasePath, | |||
3667 | ExprValueKind Cat, FPOptionsOverride FPO); | |||
3668 | ||||
3669 | static ImplicitCastExpr *CreateEmpty(const ASTContext &Context, | |||
3670 | unsigned PathSize, bool HasFPFeatures); | |||
3671 | ||||
3672 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
3673 | return getSubExpr()->getBeginLoc(); | |||
3674 | } | |||
3675 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
3676 | return getSubExpr()->getEndLoc(); | |||
3677 | } | |||
3678 | ||||
3679 | static bool classof(const Stmt *T) { | |||
3680 | return T->getStmtClass() == ImplicitCastExprClass; | |||
3681 | } | |||
3682 | ||||
3683 | friend TrailingObjects; | |||
3684 | friend class CastExpr; | |||
3685 | }; | |||
3686 | ||||
3687 | /// ExplicitCastExpr - An explicit cast written in the source | |||
3688 | /// code. | |||
3689 | /// | |||
3690 | /// This class is effectively an abstract class, because it provides | |||
3691 | /// the basic representation of an explicitly-written cast without | |||
3692 | /// specifying which kind of cast (C cast, functional cast, static | |||
3693 | /// cast, etc.) was written; specific derived classes represent the | |||
3694 | /// particular style of cast and its location information. | |||
3695 | /// | |||
3696 | /// Unlike implicit casts, explicit cast nodes have two different | |||
3697 | /// types: the type that was written into the source code, and the | |||
3698 | /// actual type of the expression as determined by semantic | |||
3699 | /// analysis. These types may differ slightly. For example, in C++ one | |||
3700 | /// can cast to a reference type, which indicates that the resulting | |||
3701 | /// expression will be an lvalue or xvalue. The reference type, however, | |||
3702 | /// will not be used as the type of the expression. | |||
3703 | class ExplicitCastExpr : public CastExpr { | |||
3704 | /// TInfo - Source type info for the (written) type | |||
3705 | /// this expression is casting to. | |||
3706 | TypeSourceInfo *TInfo; | |||
3707 | ||||
3708 | protected: | |||
3709 | ExplicitCastExpr(StmtClass SC, QualType exprTy, ExprValueKind VK, | |||
3710 | CastKind kind, Expr *op, unsigned PathSize, | |||
3711 | bool HasFPFeatures, TypeSourceInfo *writtenTy) | |||
3712 | : CastExpr(SC, exprTy, VK, kind, op, PathSize, HasFPFeatures), | |||
3713 | TInfo(writtenTy) { | |||
3714 | setDependence(computeDependence(this)); | |||
3715 | } | |||
3716 | ||||
3717 | /// Construct an empty explicit cast. | |||
3718 | ExplicitCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize, | |||
3719 | bool HasFPFeatures) | |||
3720 | : CastExpr(SC, Shell, PathSize, HasFPFeatures) {} | |||
3721 | ||||
3722 | public: | |||
3723 | /// getTypeInfoAsWritten - Returns the type source info for the type | |||
3724 | /// that this expression is casting to. | |||
3725 | TypeSourceInfo *getTypeInfoAsWritten() const { return TInfo; } | |||
3726 | void setTypeInfoAsWritten(TypeSourceInfo *writtenTy) { TInfo = writtenTy; } | |||
3727 | ||||
3728 | /// getTypeAsWritten - Returns the type that this expression is | |||
3729 | /// casting to, as written in the source code. | |||
3730 | QualType getTypeAsWritten() const { return TInfo->getType(); } | |||
3731 | ||||
3732 | static bool classof(const Stmt *T) { | |||
3733 | return T->getStmtClass() >= firstExplicitCastExprConstant && | |||
3734 | T->getStmtClass() <= lastExplicitCastExprConstant; | |||
3735 | } | |||
3736 | }; | |||
3737 | ||||
3738 | /// CStyleCastExpr - An explicit cast in C (C99 6.5.4) or a C-style | |||
3739 | /// cast in C++ (C++ [expr.cast]), which uses the syntax | |||
3740 | /// (Type)expr. For example: @c (int)f. | |||
3741 | class CStyleCastExpr final | |||
3742 | : public ExplicitCastExpr, | |||
3743 | private llvm::TrailingObjects<CStyleCastExpr, CXXBaseSpecifier *, | |||
3744 | FPOptionsOverride> { | |||
3745 | SourceLocation LPLoc; // the location of the left paren | |||
3746 | SourceLocation RPLoc; // the location of the right paren | |||
3747 | ||||
3748 | CStyleCastExpr(QualType exprTy, ExprValueKind vk, CastKind kind, Expr *op, | |||
3749 | unsigned PathSize, FPOptionsOverride FPO, | |||
3750 | TypeSourceInfo *writtenTy, SourceLocation l, SourceLocation r) | |||
3751 | : ExplicitCastExpr(CStyleCastExprClass, exprTy, vk, kind, op, PathSize, | |||
3752 | FPO.requiresTrailingStorage(), writtenTy), | |||
3753 | LPLoc(l), RPLoc(r) { | |||
3754 | if (hasStoredFPFeatures()) | |||
3755 | *getTrailingFPFeatures() = FPO; | |||
3756 | } | |||
3757 | ||||
3758 | /// Construct an empty C-style explicit cast. | |||
3759 | explicit CStyleCastExpr(EmptyShell Shell, unsigned PathSize, | |||
3760 | bool HasFPFeatures) | |||
3761 | : ExplicitCastExpr(CStyleCastExprClass, Shell, PathSize, HasFPFeatures) {} | |||
3762 | ||||
3763 | unsigned numTrailingObjects(OverloadToken<CXXBaseSpecifier *>) const { | |||
3764 | return path_size(); | |||
3765 | } | |||
3766 | ||||
3767 | public: | |||
3768 | static CStyleCastExpr * | |||
3769 | Create(const ASTContext &Context, QualType T, ExprValueKind VK, CastKind K, | |||
3770 | Expr *Op, const CXXCastPath *BasePath, FPOptionsOverride FPO, | |||
3771 | TypeSourceInfo *WrittenTy, SourceLocation L, SourceLocation R); | |||
3772 | ||||
3773 | static CStyleCastExpr *CreateEmpty(const ASTContext &Context, | |||
3774 | unsigned PathSize, bool HasFPFeatures); | |||
3775 | ||||
3776 | SourceLocation getLParenLoc() const { return LPLoc; } | |||
3777 | void setLParenLoc(SourceLocation L) { LPLoc = L; } | |||
3778 | ||||
3779 | SourceLocation getRParenLoc() const { return RPLoc; } | |||
3780 | void setRParenLoc(SourceLocation L) { RPLoc = L; } | |||
3781 | ||||
3782 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return LPLoc; } | |||
3783 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
3784 | return getSubExpr()->getEndLoc(); | |||
3785 | } | |||
3786 | ||||
3787 | static bool classof(const Stmt *T) { | |||
3788 | return T->getStmtClass() == CStyleCastExprClass; | |||
3789 | } | |||
3790 | ||||
3791 | friend TrailingObjects; | |||
3792 | friend class CastExpr; | |||
3793 | }; | |||
3794 | ||||
3795 | /// A builtin binary operation expression such as "x + y" or "x <= y". | |||
3796 | /// | |||
3797 | /// This expression node kind describes a builtin binary operation, | |||
3798 | /// such as "x + y" for integer values "x" and "y". The operands will | |||
3799 | /// already have been converted to appropriate types (e.g., by | |||
3800 | /// performing promotions or conversions). | |||
3801 | /// | |||
3802 | /// In C++, where operators may be overloaded, a different kind of | |||
3803 | /// expression node (CXXOperatorCallExpr) is used to express the | |||
3804 | /// invocation of an overloaded operator with operator syntax. Within | |||
3805 | /// a C++ template, whether BinaryOperator or CXXOperatorCallExpr is | |||
3806 | /// used to store an expression "x + y" depends on the subexpressions | |||
3807 | /// for x and y. If neither x or y is type-dependent, and the "+" | |||
3808 | /// operator resolves to a built-in operation, BinaryOperator will be | |||
3809 | /// used to express the computation (x and y may still be | |||
3810 | /// value-dependent). If either x or y is type-dependent, or if the | |||
3811 | /// "+" resolves to an overloaded operator, CXXOperatorCallExpr will | |||
3812 | /// be used to express the computation. | |||
3813 | class BinaryOperator : public Expr { | |||
3814 | enum { LHS, RHS, END_EXPR }; | |||
3815 | Stmt *SubExprs[END_EXPR]; | |||
3816 | ||||
3817 | public: | |||
3818 | typedef BinaryOperatorKind Opcode; | |||
3819 | ||||
3820 | protected: | |||
3821 | size_t offsetOfTrailingStorage() const; | |||
3822 | ||||
3823 | /// Return a pointer to the trailing FPOptions | |||
3824 | FPOptionsOverride *getTrailingFPFeatures() { | |||
3825 | assert(BinaryOperatorBits.HasFPFeatures)(static_cast <bool> (BinaryOperatorBits.HasFPFeatures) ? void (0) : __assert_fail ("BinaryOperatorBits.HasFPFeatures" , "clang/include/clang/AST/Expr.h", 3825, __extension__ __PRETTY_FUNCTION__ )); | |||
3826 | return reinterpret_cast<FPOptionsOverride *>( | |||
3827 | reinterpret_cast<char *>(this) + offsetOfTrailingStorage()); | |||
3828 | } | |||
3829 | const FPOptionsOverride *getTrailingFPFeatures() const { | |||
3830 | assert(BinaryOperatorBits.HasFPFeatures)(static_cast <bool> (BinaryOperatorBits.HasFPFeatures) ? void (0) : __assert_fail ("BinaryOperatorBits.HasFPFeatures" , "clang/include/clang/AST/Expr.h", 3830, __extension__ __PRETTY_FUNCTION__ )); | |||
3831 | return reinterpret_cast<const FPOptionsOverride *>( | |||
3832 | reinterpret_cast<const char *>(this) + offsetOfTrailingStorage()); | |||
3833 | } | |||
3834 | ||||
3835 | /// Build a binary operator, assuming that appropriate storage has been | |||
3836 | /// allocated for the trailing objects when needed. | |||
3837 | BinaryOperator(const ASTContext &Ctx, Expr *lhs, Expr *rhs, Opcode opc, | |||
3838 | QualType ResTy, ExprValueKind VK, ExprObjectKind OK, | |||
3839 | SourceLocation opLoc, FPOptionsOverride FPFeatures); | |||
3840 | ||||
3841 | /// Construct an empty binary operator. | |||
3842 | explicit BinaryOperator(EmptyShell Empty) : Expr(BinaryOperatorClass, Empty) { | |||
3843 | BinaryOperatorBits.Opc = BO_Comma; | |||
3844 | } | |||
3845 | ||||
3846 | public: | |||
3847 | static BinaryOperator *CreateEmpty(const ASTContext &C, bool hasFPFeatures); | |||
3848 | ||||
3849 | static BinaryOperator *Create(const ASTContext &C, Expr *lhs, Expr *rhs, | |||
3850 | Opcode opc, QualType ResTy, ExprValueKind VK, | |||
3851 | ExprObjectKind OK, SourceLocation opLoc, | |||
3852 | FPOptionsOverride FPFeatures); | |||
3853 | SourceLocation getExprLoc() const { return getOperatorLoc(); } | |||
3854 | SourceLocation getOperatorLoc() const { return BinaryOperatorBits.OpLoc; } | |||
3855 | void setOperatorLoc(SourceLocation L) { BinaryOperatorBits.OpLoc = L; } | |||
3856 | ||||
3857 | Opcode getOpcode() const { | |||
3858 | return static_cast<Opcode>(BinaryOperatorBits.Opc); | |||
3859 | } | |||
3860 | void setOpcode(Opcode Opc) { BinaryOperatorBits.Opc = Opc; } | |||
3861 | ||||
3862 | Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } | |||
3863 | void setLHS(Expr *E) { SubExprs[LHS] = E; } | |||
3864 | Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } | |||
3865 | void setRHS(Expr *E) { SubExprs[RHS] = E; } | |||
3866 | ||||
3867 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
3868 | return getLHS()->getBeginLoc(); | |||
3869 | } | |||
3870 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
3871 | return getRHS()->getEndLoc(); | |||
3872 | } | |||
3873 | ||||
3874 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it | |||
3875 | /// corresponds to, e.g. "<<=". | |||
3876 | static StringRef getOpcodeStr(Opcode Op); | |||
3877 | ||||
3878 | StringRef getOpcodeStr() const { return getOpcodeStr(getOpcode()); } | |||
3879 | ||||
3880 | /// Retrieve the binary opcode that corresponds to the given | |||
3881 | /// overloaded operator. | |||
3882 | static Opcode getOverloadedOpcode(OverloadedOperatorKind OO); | |||
3883 | ||||
3884 | /// Retrieve the overloaded operator kind that corresponds to | |||
3885 | /// the given binary opcode. | |||
3886 | static OverloadedOperatorKind getOverloadedOperator(Opcode Opc); | |||
3887 | ||||
3888 | /// predicates to categorize the respective opcodes. | |||
3889 | static bool isPtrMemOp(Opcode Opc) { | |||
3890 | return Opc == BO_PtrMemD || Opc == BO_PtrMemI; | |||
3891 | } | |||
3892 | bool isPtrMemOp() const { return isPtrMemOp(getOpcode()); } | |||
3893 | ||||
3894 | static bool isMultiplicativeOp(Opcode Opc) { | |||
3895 | return Opc >= BO_Mul && Opc <= BO_Rem; | |||
3896 | } | |||
3897 | bool isMultiplicativeOp() const { return isMultiplicativeOp(getOpcode()); } | |||
3898 | static bool isAdditiveOp(Opcode Opc) { return Opc == BO_Add || Opc==BO_Sub; } | |||
3899 | bool isAdditiveOp() const { return isAdditiveOp(getOpcode()); } | |||
3900 | static bool isShiftOp(Opcode Opc) { return Opc == BO_Shl || Opc == BO_Shr; } | |||
3901 | bool isShiftOp() const { return isShiftOp(getOpcode()); } | |||
3902 | ||||
3903 | static bool isBitwiseOp(Opcode Opc) { return Opc >= BO_And && Opc <= BO_Or; } | |||
3904 | bool isBitwiseOp() const { return isBitwiseOp(getOpcode()); } | |||
3905 | ||||
3906 | static bool isRelationalOp(Opcode Opc) { return Opc >= BO_LT && Opc<=BO_GE; } | |||
3907 | bool isRelationalOp() const { return isRelationalOp(getOpcode()); } | |||
3908 | ||||
3909 | static bool isEqualityOp(Opcode Opc) { return Opc == BO_EQ || Opc == BO_NE; } | |||
3910 | bool isEqualityOp() const { return isEqualityOp(getOpcode()); } | |||
3911 | ||||
3912 | static bool isComparisonOp(Opcode Opc) { return Opc >= BO_Cmp && Opc<=BO_NE; } | |||
3913 | bool isComparisonOp() const { return isComparisonOp(getOpcode()); } | |||
3914 | ||||
3915 | static bool isCommaOp(Opcode Opc) { return Opc == BO_Comma; } | |||
3916 | bool isCommaOp() const { return isCommaOp(getOpcode()); } | |||
3917 | ||||
3918 | static Opcode negateComparisonOp(Opcode Opc) { | |||
3919 | switch (Opc) { | |||
3920 | default: | |||
3921 | llvm_unreachable("Not a comparison operator.")::llvm::llvm_unreachable_internal("Not a comparison operator." , "clang/include/clang/AST/Expr.h", 3921); | |||
3922 | case BO_LT: return BO_GE; | |||
3923 | case BO_GT: return BO_LE; | |||
3924 | case BO_LE: return BO_GT; | |||
3925 | case BO_GE: return BO_LT; | |||
3926 | case BO_EQ: return BO_NE; | |||
3927 | case BO_NE: return BO_EQ; | |||
3928 | } | |||
3929 | } | |||
3930 | ||||
3931 | static Opcode reverseComparisonOp(Opcode Opc) { | |||
3932 | switch (Opc) { | |||
3933 | default: | |||
3934 | llvm_unreachable("Not a comparison operator.")::llvm::llvm_unreachable_internal("Not a comparison operator." , "clang/include/clang/AST/Expr.h", 3934); | |||
3935 | case BO_LT: return BO_GT; | |||
3936 | case BO_GT: return BO_LT; | |||
3937 | case BO_LE: return BO_GE; | |||
3938 | case BO_GE: return BO_LE; | |||
3939 | case BO_EQ: | |||
3940 | case BO_NE: | |||
3941 | return Opc; | |||
3942 | } | |||
3943 | } | |||
3944 | ||||
3945 | static bool isLogicalOp(Opcode Opc) { return Opc == BO_LAnd || Opc==BO_LOr; } | |||
3946 | bool isLogicalOp() const { return isLogicalOp(getOpcode()); } | |||
3947 | ||||
3948 | static bool isAssignmentOp(Opcode Opc) { | |||
3949 | return Opc >= BO_Assign && Opc <= BO_OrAssign; | |||
3950 | } | |||
3951 | bool isAssignmentOp() const { return isAssignmentOp(getOpcode()); } | |||
3952 | ||||
3953 | static bool isCompoundAssignmentOp(Opcode Opc) { | |||
3954 | return Opc > BO_Assign && Opc <= BO_OrAssign; | |||
3955 | } | |||
3956 | bool isCompoundAssignmentOp() const { | |||
3957 | return isCompoundAssignmentOp(getOpcode()); | |||
3958 | } | |||
3959 | static Opcode getOpForCompoundAssignment(Opcode Opc) { | |||
3960 | assert(isCompoundAssignmentOp(Opc))(static_cast <bool> (isCompoundAssignmentOp(Opc)) ? void (0) : __assert_fail ("isCompoundAssignmentOp(Opc)", "clang/include/clang/AST/Expr.h" , 3960, __extension__ __PRETTY_FUNCTION__)); | |||
3961 | if (Opc >= BO_AndAssign) | |||
3962 | return Opcode(unsigned(Opc) - BO_AndAssign + BO_And); | |||
3963 | else | |||
3964 | return Opcode(unsigned(Opc) - BO_MulAssign + BO_Mul); | |||
3965 | } | |||
3966 | ||||
3967 | static bool isShiftAssignOp(Opcode Opc) { | |||
3968 | return Opc == BO_ShlAssign || Opc == BO_ShrAssign; | |||
3969 | } | |||
3970 | bool isShiftAssignOp() const { | |||
3971 | return isShiftAssignOp(getOpcode()); | |||
3972 | } | |||
3973 | ||||
3974 | /// Return true if a binary operator using the specified opcode and operands | |||
3975 | /// would match the 'p = (i8*)nullptr + n' idiom for casting a pointer-sized | |||
3976 | /// integer to a pointer. | |||
3977 | static bool isNullPointerArithmeticExtension(ASTContext &Ctx, Opcode Opc, | |||
3978 | const Expr *LHS, | |||
3979 | const Expr *RHS); | |||
3980 | ||||
3981 | static bool classof(const Stmt *S) { | |||
3982 | return S->getStmtClass() >= firstBinaryOperatorConstant && | |||
3983 | S->getStmtClass() <= lastBinaryOperatorConstant; | |||
3984 | } | |||
3985 | ||||
3986 | // Iterators | |||
3987 | child_range children() { | |||
3988 | return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); | |||
3989 | } | |||
3990 | const_child_range children() const { | |||
3991 | return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); | |||
3992 | } | |||
3993 | ||||
3994 | /// Set and fetch the bit that shows whether FPFeatures needs to be | |||
3995 | /// allocated in Trailing Storage | |||
3996 | void setHasStoredFPFeatures(bool B) { BinaryOperatorBits.HasFPFeatures = B; } | |||
3997 | bool hasStoredFPFeatures() const { return BinaryOperatorBits.HasFPFeatures; } | |||
3998 | ||||
3999 | /// Get FPFeatures from trailing storage | |||
4000 | FPOptionsOverride getStoredFPFeatures() const { | |||
4001 | assert(hasStoredFPFeatures())(static_cast <bool> (hasStoredFPFeatures()) ? void (0) : __assert_fail ("hasStoredFPFeatures()", "clang/include/clang/AST/Expr.h" , 4001, __extension__ __PRETTY_FUNCTION__)); | |||
4002 | return *getTrailingFPFeatures(); | |||
4003 | } | |||
4004 | /// Set FPFeatures in trailing storage, used only by Serialization | |||
4005 | void setStoredFPFeatures(FPOptionsOverride F) { | |||
4006 | assert(BinaryOperatorBits.HasFPFeatures)(static_cast <bool> (BinaryOperatorBits.HasFPFeatures) ? void (0) : __assert_fail ("BinaryOperatorBits.HasFPFeatures" , "clang/include/clang/AST/Expr.h", 4006, __extension__ __PRETTY_FUNCTION__ )); | |||
4007 | *getTrailingFPFeatures() = F; | |||
4008 | } | |||
4009 | ||||
4010 | /// Get the FP features status of this operator. Only meaningful for | |||
4011 | /// operations on floating point types. | |||
4012 | FPOptions getFPFeaturesInEffect(const LangOptions &LO) const { | |||
4013 | if (BinaryOperatorBits.HasFPFeatures) | |||
4014 | return getStoredFPFeatures().applyOverrides(LO); | |||
4015 | return FPOptions::defaultWithoutTrailingStorage(LO); | |||
4016 | } | |||
4017 | ||||
4018 | // This is used in ASTImporter | |||
4019 | FPOptionsOverride getFPFeatures() const { | |||
4020 | if (BinaryOperatorBits.HasFPFeatures) | |||
4021 | return getStoredFPFeatures(); | |||
4022 | return FPOptionsOverride(); | |||
4023 | } | |||
4024 | ||||
4025 | /// Get the FP contractability status of this operator. Only meaningful for | |||
4026 | /// operations on floating point types. | |||
4027 | bool isFPContractableWithinStatement(const LangOptions &LO) const { | |||
4028 | return getFPFeaturesInEffect(LO).allowFPContractWithinStatement(); | |||
4029 | } | |||
4030 | ||||
4031 | /// Get the FENV_ACCESS status of this operator. Only meaningful for | |||
4032 | /// operations on floating point types. | |||
4033 | bool isFEnvAccessOn(const LangOptions &LO) const { | |||
4034 | return getFPFeaturesInEffect(LO).getAllowFEnvAccess(); | |||
4035 | } | |||
4036 | ||||
4037 | protected: | |||
4038 | BinaryOperator(const ASTContext &Ctx, Expr *lhs, Expr *rhs, Opcode opc, | |||
4039 | QualType ResTy, ExprValueKind VK, ExprObjectKind OK, | |||
4040 | SourceLocation opLoc, FPOptionsOverride FPFeatures, | |||
4041 | bool dead2); | |||
4042 | ||||
4043 | /// Construct an empty BinaryOperator, SC is CompoundAssignOperator. | |||
4044 | BinaryOperator(StmtClass SC, EmptyShell Empty) : Expr(SC, Empty) { | |||
4045 | BinaryOperatorBits.Opc = BO_MulAssign; | |||
4046 | } | |||
4047 | ||||
4048 | /// Return the size in bytes needed for the trailing objects. | |||
4049 | /// Used to allocate the right amount of storage. | |||
4050 | static unsigned sizeOfTrailingObjects(bool HasFPFeatures) { | |||
4051 | return HasFPFeatures * sizeof(FPOptionsOverride); | |||
4052 | } | |||
4053 | }; | |||
4054 | ||||
4055 | /// CompoundAssignOperator - For compound assignments (e.g. +=), we keep | |||
4056 | /// track of the type the operation is performed in. Due to the semantics of | |||
4057 | /// these operators, the operands are promoted, the arithmetic performed, an | |||
4058 | /// implicit conversion back to the result type done, then the assignment takes | |||
4059 | /// place. This captures the intermediate type which the computation is done | |||
4060 | /// in. | |||
4061 | class CompoundAssignOperator : public BinaryOperator { | |||
4062 | QualType ComputationLHSType; | |||
4063 | QualType ComputationResultType; | |||
4064 | ||||
4065 | /// Construct an empty CompoundAssignOperator. | |||
4066 | explicit CompoundAssignOperator(const ASTContext &C, EmptyShell Empty, | |||
4067 | bool hasFPFeatures) | |||
4068 | : BinaryOperator(CompoundAssignOperatorClass, Empty) {} | |||
4069 | ||||
4070 | protected: | |||
4071 | CompoundAssignOperator(const ASTContext &C, Expr *lhs, Expr *rhs, Opcode opc, | |||
4072 | QualType ResType, ExprValueKind VK, ExprObjectKind OK, | |||
4073 | SourceLocation OpLoc, FPOptionsOverride FPFeatures, | |||
4074 | QualType CompLHSType, QualType CompResultType) | |||
4075 | : BinaryOperator(C, lhs, rhs, opc, ResType, VK, OK, OpLoc, FPFeatures, | |||
4076 | true), | |||
4077 | ComputationLHSType(CompLHSType), ComputationResultType(CompResultType) { | |||
4078 | assert(isCompoundAssignmentOp() &&(static_cast <bool> (isCompoundAssignmentOp() && "Only should be used for compound assignments") ? void (0) : __assert_fail ("isCompoundAssignmentOp() && \"Only should be used for compound assignments\"" , "clang/include/clang/AST/Expr.h", 4079, __extension__ __PRETTY_FUNCTION__ )) | |||
4079 | "Only should be used for compound assignments")(static_cast <bool> (isCompoundAssignmentOp() && "Only should be used for compound assignments") ? void (0) : __assert_fail ("isCompoundAssignmentOp() && \"Only should be used for compound assignments\"" , "clang/include/clang/AST/Expr.h", 4079, __extension__ __PRETTY_FUNCTION__ )); | |||
4080 | } | |||
4081 | ||||
4082 | public: | |||
4083 | static CompoundAssignOperator *CreateEmpty(const ASTContext &C, | |||
4084 | bool hasFPFeatures); | |||
4085 | ||||
4086 | static CompoundAssignOperator * | |||
4087 | Create(const ASTContext &C, Expr *lhs, Expr *rhs, Opcode opc, QualType ResTy, | |||
4088 | ExprValueKind VK, ExprObjectKind OK, SourceLocation opLoc, | |||
4089 | FPOptionsOverride FPFeatures, QualType CompLHSType = QualType(), | |||
4090 | QualType CompResultType = QualType()); | |||
4091 | ||||
4092 | // The two computation types are the type the LHS is converted | |||
4093 | // to for the computation and the type of the result; the two are | |||
4094 | // distinct in a few cases (specifically, int+=ptr and ptr-=ptr). | |||
4095 | QualType getComputationLHSType() const { return ComputationLHSType; } | |||
4096 | void setComputationLHSType(QualType T) { ComputationLHSType = T; } | |||
4097 | ||||
4098 | QualType getComputationResultType() const { return ComputationResultType; } | |||
4099 | void setComputationResultType(QualType T) { ComputationResultType = T; } | |||
4100 | ||||
4101 | static bool classof(const Stmt *S) { | |||
4102 | return S->getStmtClass() == CompoundAssignOperatorClass; | |||
4103 | } | |||
4104 | }; | |||
4105 | ||||
4106 | inline size_t BinaryOperator::offsetOfTrailingStorage() const { | |||
4107 | assert(BinaryOperatorBits.HasFPFeatures)(static_cast <bool> (BinaryOperatorBits.HasFPFeatures) ? void (0) : __assert_fail ("BinaryOperatorBits.HasFPFeatures" , "clang/include/clang/AST/Expr.h", 4107, __extension__ __PRETTY_FUNCTION__ )); | |||
4108 | return isa<CompoundAssignOperator>(this) ? sizeof(CompoundAssignOperator) | |||
4109 | : sizeof(BinaryOperator); | |||
4110 | } | |||
4111 | ||||
4112 | /// AbstractConditionalOperator - An abstract base class for | |||
4113 | /// ConditionalOperator and BinaryConditionalOperator. | |||
4114 | class AbstractConditionalOperator : public Expr { | |||
4115 | SourceLocation QuestionLoc, ColonLoc; | |||
4116 | friend class ASTStmtReader; | |||
4117 | ||||
4118 | protected: | |||
4119 | AbstractConditionalOperator(StmtClass SC, QualType T, ExprValueKind VK, | |||
4120 | ExprObjectKind OK, SourceLocation qloc, | |||
4121 | SourceLocation cloc) | |||
4122 | : Expr(SC, T, VK, OK), QuestionLoc(qloc), ColonLoc(cloc) {} | |||
4123 | ||||
4124 | AbstractConditionalOperator(StmtClass SC, EmptyShell Empty) | |||
4125 | : Expr(SC, Empty) { } | |||
4126 | ||||
4127 | public: | |||
4128 | /// getCond - Return the expression representing the condition for | |||
4129 | /// the ?: operator. | |||
4130 | Expr *getCond() const; | |||
4131 | ||||
4132 | /// getTrueExpr - Return the subexpression representing the value of | |||
4133 | /// the expression if the condition evaluates to true. | |||
4134 | Expr *getTrueExpr() const; | |||
4135 | ||||
4136 | /// getFalseExpr - Return the subexpression representing the value of | |||
4137 | /// the expression if the condition evaluates to false. This is | |||
4138 | /// the same as getRHS. | |||
4139 | Expr *getFalseExpr() const; | |||
4140 | ||||
4141 | SourceLocation getQuestionLoc() const { return QuestionLoc; } | |||
4142 | SourceLocation getColonLoc() const { return ColonLoc; } | |||
4143 | ||||
4144 | static bool classof(const Stmt *T) { | |||
4145 | return T->getStmtClass() == ConditionalOperatorClass || | |||
4146 | T->getStmtClass() == BinaryConditionalOperatorClass; | |||
4147 | } | |||
4148 | }; | |||
4149 | ||||
4150 | /// ConditionalOperator - The ?: ternary operator. The GNU "missing | |||
4151 | /// middle" extension is a BinaryConditionalOperator. | |||
4152 | class ConditionalOperator : public AbstractConditionalOperator { | |||
4153 | enum { COND, LHS, RHS, END_EXPR }; | |||
4154 | Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. | |||
4155 | ||||
4156 | friend class ASTStmtReader; | |||
4157 | public: | |||
4158 | ConditionalOperator(Expr *cond, SourceLocation QLoc, Expr *lhs, | |||
4159 | SourceLocation CLoc, Expr *rhs, QualType t, | |||
4160 | ExprValueKind VK, ExprObjectKind OK) | |||
4161 | : AbstractConditionalOperator(ConditionalOperatorClass, t, VK, OK, QLoc, | |||
4162 | CLoc) { | |||
4163 | SubExprs[COND] = cond; | |||
4164 | SubExprs[LHS] = lhs; | |||
4165 | SubExprs[RHS] = rhs; | |||
4166 | setDependence(computeDependence(this)); | |||
4167 | } | |||
4168 | ||||
4169 | /// Build an empty conditional operator. | |||
4170 | explicit ConditionalOperator(EmptyShell Empty) | |||
4171 | : AbstractConditionalOperator(ConditionalOperatorClass, Empty) { } | |||
4172 | ||||
4173 | /// getCond - Return the expression representing the condition for | |||
4174 | /// the ?: operator. | |||
4175 | Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } | |||
4176 | ||||
4177 | /// getTrueExpr - Return the subexpression representing the value of | |||
4178 | /// the expression if the condition evaluates to true. | |||
4179 | Expr *getTrueExpr() const { return cast<Expr>(SubExprs[LHS]); } | |||
4180 | ||||
4181 | /// getFalseExpr - Return the subexpression representing the value of | |||
4182 | /// the expression if the condition evaluates to false. This is | |||
4183 | /// the same as getRHS. | |||
4184 | Expr *getFalseExpr() const { return cast<Expr>(SubExprs[RHS]); } | |||
4185 | ||||
4186 | Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } | |||
4187 | Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } | |||
4188 | ||||
4189 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
4190 | return getCond()->getBeginLoc(); | |||
4191 | } | |||
4192 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
4193 | return getRHS()->getEndLoc(); | |||
4194 | } | |||
4195 | ||||
4196 | static bool classof(const Stmt *T) { | |||
4197 | return T->getStmtClass() == ConditionalOperatorClass; | |||
4198 | } | |||
4199 | ||||
4200 | // Iterators | |||
4201 | child_range children() { | |||
4202 | return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); | |||
4203 | } | |||
4204 | const_child_range children() const { | |||
4205 | return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); | |||
4206 | } | |||
4207 | }; | |||
4208 | ||||
4209 | /// BinaryConditionalOperator - The GNU extension to the conditional | |||
4210 | /// operator which allows the middle operand to be omitted. | |||
4211 | /// | |||
4212 | /// This is a different expression kind on the assumption that almost | |||
4213 | /// every client ends up needing to know that these are different. | |||
4214 | class BinaryConditionalOperator : public AbstractConditionalOperator { | |||
4215 | enum { COMMON, COND, LHS, RHS, NUM_SUBEXPRS }; | |||
4216 | ||||
4217 | /// - the common condition/left-hand-side expression, which will be | |||
4218 | /// evaluated as the opaque value | |||
4219 | /// - the condition, expressed in terms of the opaque value | |||
4220 | /// - the left-hand-side, expressed in terms of the opaque value | |||
4221 | /// - the right-hand-side | |||
4222 | Stmt *SubExprs[NUM_SUBEXPRS]; | |||
4223 | OpaqueValueExpr *OpaqueValue; | |||
4224 | ||||
4225 | friend class ASTStmtReader; | |||
4226 | public: | |||
4227 | BinaryConditionalOperator(Expr *common, OpaqueValueExpr *opaqueValue, | |||
4228 | Expr *cond, Expr *lhs, Expr *rhs, | |||
4229 | SourceLocation qloc, SourceLocation cloc, | |||
4230 | QualType t, ExprValueKind VK, ExprObjectKind OK) | |||
4231 | : AbstractConditionalOperator(BinaryConditionalOperatorClass, t, VK, OK, | |||
4232 | qloc, cloc), | |||
4233 | OpaqueValue(opaqueValue) { | |||
4234 | SubExprs[COMMON] = common; | |||
4235 | SubExprs[COND] = cond; | |||
4236 | SubExprs[LHS] = lhs; | |||
4237 | SubExprs[RHS] = rhs; | |||
4238 | assert(OpaqueValue->getSourceExpr() == common && "Wrong opaque value")(static_cast <bool> (OpaqueValue->getSourceExpr() == common && "Wrong opaque value") ? void (0) : __assert_fail ("OpaqueValue->getSourceExpr() == common && \"Wrong opaque value\"" , "clang/include/clang/AST/Expr.h", 4238, __extension__ __PRETTY_FUNCTION__ )); | |||
| ||||
4239 | setDependence(computeDependence(this)); | |||
4240 | } | |||
4241 | ||||
4242 | /// Build an empty conditional operator. | |||
4243 | explicit BinaryConditionalOperator(EmptyShell Empty) | |||
4244 | : AbstractConditionalOperator(BinaryConditionalOperatorClass, Empty) { } | |||
4245 | ||||
4246 | /// getCommon - Return the common expression, written to the | |||
4247 | /// left of the condition. The opaque value will be bound to the | |||
4248 | /// result of this expression. | |||
4249 | Expr *getCommon() const { return cast<Expr>(SubExprs[COMMON]); } | |||
4250 | ||||
4251 | /// getOpaqueValue - Return the opaque value placeholder. | |||
4252 | OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; } | |||
4253 | ||||
4254 | /// getCond - Return the condition expression; this is defined | |||
4255 | /// in terms of the opaque value. | |||
4256 | Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } | |||
4257 | ||||
4258 | /// getTrueExpr - Return the subexpression which will be | |||
4259 | /// evaluated if the condition evaluates to true; this is defined | |||
4260 | /// in terms of the opaque value. | |||
4261 | Expr *getTrueExpr() const { | |||
4262 | return cast<Expr>(SubExprs[LHS]); | |||
4263 | } | |||
4264 | ||||
4265 | /// getFalseExpr - Return the subexpression which will be | |||
4266 | /// evaluated if the condnition evaluates to false; this is | |||
4267 | /// defined in terms of the opaque value. | |||
4268 | Expr *getFalseExpr() const { | |||
4269 | return cast<Expr>(SubExprs[RHS]); | |||
4270 | } | |||
4271 | ||||
4272 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
4273 | return getCommon()->getBeginLoc(); | |||
4274 | } | |||
4275 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
4276 | return getFalseExpr()->getEndLoc(); | |||
4277 | } | |||
4278 | ||||
4279 | static bool classof(const Stmt *T) { | |||
4280 | return T->getStmtClass() == BinaryConditionalOperatorClass; | |||
4281 | } | |||
4282 | ||||
4283 | // Iterators | |||
4284 | child_range children() { | |||
4285 | return child_range(SubExprs, SubExprs + NUM_SUBEXPRS); | |||
4286 | } | |||
4287 | const_child_range children() const { | |||
4288 | return const_child_range(SubExprs, SubExprs + NUM_SUBEXPRS); | |||
4289 | } | |||
4290 | }; | |||
4291 | ||||
4292 | inline Expr *AbstractConditionalOperator::getCond() const { | |||
4293 | if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) | |||
4294 | return co->getCond(); | |||
4295 | return cast<BinaryConditionalOperator>(this)->getCond(); | |||
4296 | } | |||
4297 | ||||
4298 | inline Expr *AbstractConditionalOperator::getTrueExpr() const { | |||
4299 | if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) | |||
4300 | return co->getTrueExpr(); | |||
4301 | return cast<BinaryConditionalOperator>(this)->getTrueExpr(); | |||
4302 | } | |||
4303 | ||||
4304 | inline Expr *AbstractConditionalOperator::getFalseExpr() const { | |||
4305 | if (const ConditionalOperator *co = dyn_cast<ConditionalOperator>(this)) | |||
4306 | return co->getFalseExpr(); | |||
4307 | return cast<BinaryConditionalOperator>(this)->getFalseExpr(); | |||
4308 | } | |||
4309 | ||||
4310 | /// AddrLabelExpr - The GNU address of label extension, representing &&label. | |||
4311 | class AddrLabelExpr : public Expr { | |||
4312 | SourceLocation AmpAmpLoc, LabelLoc; | |||
4313 | LabelDecl *Label; | |||
4314 | public: | |||
4315 | AddrLabelExpr(SourceLocation AALoc, SourceLocation LLoc, LabelDecl *L, | |||
4316 | QualType t) | |||
4317 | : Expr(AddrLabelExprClass, t, VK_PRValue, OK_Ordinary), AmpAmpLoc(AALoc), | |||
4318 | LabelLoc(LLoc), Label(L) { | |||
4319 | setDependence(ExprDependence::None); | |||
4320 | } | |||
4321 | ||||
4322 | /// Build an empty address of a label expression. | |||
4323 | explicit AddrLabelExpr(EmptyShell Empty) | |||
4324 | : Expr(AddrLabelExprClass, Empty) { } | |||
4325 | ||||
4326 | SourceLocation getAmpAmpLoc() const { return AmpAmpLoc; } | |||
4327 | void setAmpAmpLoc(SourceLocation L) { AmpAmpLoc = L; } | |||
4328 | SourceLocation getLabelLoc() const { return LabelLoc; } | |||
4329 | void setLabelLoc(SourceLocation L) { LabelLoc = L; } | |||
4330 | ||||
4331 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return AmpAmpLoc; } | |||
4332 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return LabelLoc; } | |||
4333 | ||||
4334 | LabelDecl *getLabel() const { return Label; } | |||
4335 | void setLabel(LabelDecl *L) { Label = L; } | |||
4336 | ||||
4337 | static bool classof(const Stmt *T) { | |||
4338 | return T->getStmtClass() == AddrLabelExprClass; | |||
4339 | } | |||
4340 | ||||
4341 | // Iterators | |||
4342 | child_range children() { | |||
4343 | return child_range(child_iterator(), child_iterator()); | |||
4344 | } | |||
4345 | const_child_range children() const { | |||
4346 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
4347 | } | |||
4348 | }; | |||
4349 | ||||
4350 | /// StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}). | |||
4351 | /// The StmtExpr contains a single CompoundStmt node, which it evaluates and | |||
4352 | /// takes the value of the last subexpression. | |||
4353 | /// | |||
4354 | /// A StmtExpr is always an r-value; values "returned" out of a | |||
4355 | /// StmtExpr will be copied. | |||
4356 | class StmtExpr : public Expr { | |||
4357 | Stmt *SubStmt; | |||
4358 | SourceLocation LParenLoc, RParenLoc; | |||
4359 | public: | |||
4360 | StmtExpr(CompoundStmt *SubStmt, QualType T, SourceLocation LParenLoc, | |||
4361 | SourceLocation RParenLoc, unsigned TemplateDepth) | |||
4362 | : Expr(StmtExprClass, T, VK_PRValue, OK_Ordinary), SubStmt(SubStmt), | |||
4363 | LParenLoc(LParenLoc), RParenLoc(RParenLoc) { | |||
4364 | setDependence(computeDependence(this, TemplateDepth)); | |||
4365 | // FIXME: A templated statement expression should have an associated | |||
4366 | // DeclContext so that nested declarations always have a dependent context. | |||
4367 | StmtExprBits.TemplateDepth = TemplateDepth; | |||
4368 | } | |||
4369 | ||||
4370 | /// Build an empty statement expression. | |||
4371 | explicit StmtExpr(EmptyShell Empty) : Expr(StmtExprClass, Empty) { } | |||
4372 | ||||
4373 | CompoundStmt *getSubStmt() { return cast<CompoundStmt>(SubStmt); } | |||
4374 | const CompoundStmt *getSubStmt() const { return cast<CompoundStmt>(SubStmt); } | |||
4375 | void setSubStmt(CompoundStmt *S) { SubStmt = S; } | |||
4376 | ||||
4377 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return LParenLoc; } | |||
4378 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return RParenLoc; } | |||
4379 | ||||
4380 | SourceLocation getLParenLoc() const { return LParenLoc; } | |||
4381 | void setLParenLoc(SourceLocation L) { LParenLoc = L; } | |||
4382 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
4383 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } | |||
4384 | ||||
4385 | unsigned getTemplateDepth() const { return StmtExprBits.TemplateDepth; } | |||
4386 | ||||
4387 | static bool classof(const Stmt *T) { | |||
4388 | return T->getStmtClass() == StmtExprClass; | |||
4389 | } | |||
4390 | ||||
4391 | // Iterators | |||
4392 | child_range children() { return child_range(&SubStmt, &SubStmt+1); } | |||
4393 | const_child_range children() const { | |||
4394 | return const_child_range(&SubStmt, &SubStmt + 1); | |||
4395 | } | |||
4396 | }; | |||
4397 | ||||
4398 | /// ShuffleVectorExpr - clang-specific builtin-in function | |||
4399 | /// __builtin_shufflevector. | |||
4400 | /// This AST node represents a operator that does a constant | |||
4401 | /// shuffle, similar to LLVM's shufflevector instruction. It takes | |||
4402 | /// two vectors and a variable number of constant indices, | |||
4403 | /// and returns the appropriately shuffled vector. | |||
4404 | class ShuffleVectorExpr : public Expr { | |||
4405 | SourceLocation BuiltinLoc, RParenLoc; | |||
4406 | ||||
4407 | // SubExprs - the list of values passed to the __builtin_shufflevector | |||
4408 | // function. The first two are vectors, and the rest are constant | |||
4409 | // indices. The number of values in this list is always | |||
4410 | // 2+the number of indices in the vector type. | |||
4411 | Stmt **SubExprs; | |||
4412 | unsigned NumExprs; | |||
4413 | ||||
4414 | public: | |||
4415 | ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args, QualType Type, | |||
4416 | SourceLocation BLoc, SourceLocation RP); | |||
4417 | ||||
4418 | /// Build an empty vector-shuffle expression. | |||
4419 | explicit ShuffleVectorExpr(EmptyShell Empty) | |||
4420 | : Expr(ShuffleVectorExprClass, Empty), SubExprs(nullptr) { } | |||
4421 | ||||
4422 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } | |||
4423 | void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } | |||
4424 | ||||
4425 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
4426 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } | |||
4427 | ||||
4428 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return BuiltinLoc; } | |||
4429 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return RParenLoc; } | |||
4430 | ||||
4431 | static bool classof(const Stmt *T) { | |||
4432 | return T->getStmtClass() == ShuffleVectorExprClass; | |||
4433 | } | |||
4434 | ||||
4435 | /// getNumSubExprs - Return the size of the SubExprs array. This includes the | |||
4436 | /// constant expression, the actual arguments passed in, and the function | |||
4437 | /// pointers. | |||
4438 | unsigned getNumSubExprs() const { return NumExprs; } | |||
4439 | ||||
4440 | /// Retrieve the array of expressions. | |||
4441 | Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } | |||
4442 | ||||
4443 | /// getExpr - Return the Expr at the specified index. | |||
4444 | Expr *getExpr(unsigned Index) { | |||
4445 | assert((Index < NumExprs) && "Arg access out of range!")(static_cast <bool> ((Index < NumExprs) && "Arg access out of range!" ) ? void (0) : __assert_fail ("(Index < NumExprs) && \"Arg access out of range!\"" , "clang/include/clang/AST/Expr.h", 4445, __extension__ __PRETTY_FUNCTION__ )); | |||
4446 | return cast<Expr>(SubExprs[Index]); | |||
4447 | } | |||
4448 | const Expr *getExpr(unsigned Index) const { | |||
4449 | assert((Index < NumExprs) && "Arg access out of range!")(static_cast <bool> ((Index < NumExprs) && "Arg access out of range!" ) ? void (0) : __assert_fail ("(Index < NumExprs) && \"Arg access out of range!\"" , "clang/include/clang/AST/Expr.h", 4449, __extension__ __PRETTY_FUNCTION__ )); | |||
4450 | return cast<Expr>(SubExprs[Index]); | |||
4451 | } | |||
4452 | ||||
4453 | void setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs); | |||
4454 | ||||
4455 | llvm::APSInt getShuffleMaskIdx(const ASTContext &Ctx, unsigned N) const { | |||
4456 | assert((N < NumExprs - 2) && "Shuffle idx out of range!")(static_cast <bool> ((N < NumExprs - 2) && "Shuffle idx out of range!" ) ? void (0) : __assert_fail ("(N < NumExprs - 2) && \"Shuffle idx out of range!\"" , "clang/include/clang/AST/Expr.h", 4456, __extension__ __PRETTY_FUNCTION__ )); | |||
4457 | return getExpr(N+2)->EvaluateKnownConstInt(Ctx); | |||
4458 | } | |||
4459 | ||||
4460 | // Iterators | |||
4461 | child_range children() { | |||
4462 | return child_range(&SubExprs[0], &SubExprs[0]+NumExprs); | |||
4463 | } | |||
4464 | const_child_range children() const { | |||
4465 | return const_child_range(&SubExprs[0], &SubExprs[0] + NumExprs); | |||
4466 | } | |||
4467 | }; | |||
4468 | ||||
4469 | /// ConvertVectorExpr - Clang builtin function __builtin_convertvector | |||
4470 | /// This AST node provides support for converting a vector type to another | |||
4471 | /// vector type of the same arity. | |||
4472 | class ConvertVectorExpr : public Expr { | |||
4473 | private: | |||
4474 | Stmt *SrcExpr; | |||
4475 | TypeSourceInfo *TInfo; | |||
4476 | SourceLocation BuiltinLoc, RParenLoc; | |||
4477 | ||||
4478 | friend class ASTReader; | |||
4479 | friend class ASTStmtReader; | |||
4480 | explicit ConvertVectorExpr(EmptyShell Empty) : Expr(ConvertVectorExprClass, Empty) {} | |||
4481 | ||||
4482 | public: | |||
4483 | ConvertVectorExpr(Expr *SrcExpr, TypeSourceInfo *TI, QualType DstType, | |||
4484 | ExprValueKind VK, ExprObjectKind OK, | |||
4485 | SourceLocation BuiltinLoc, SourceLocation RParenLoc) | |||
4486 | : Expr(ConvertVectorExprClass, DstType, VK, OK), SrcExpr(SrcExpr), | |||
4487 | TInfo(TI), BuiltinLoc(BuiltinLoc), RParenLoc(RParenLoc) { | |||
4488 | setDependence(computeDependence(this)); | |||
4489 | } | |||
4490 | ||||
4491 | /// getSrcExpr - Return the Expr to be converted. | |||
4492 | Expr *getSrcExpr() const { return cast<Expr>(SrcExpr); } | |||
4493 | ||||
4494 | /// getTypeSourceInfo - Return the destination type. | |||
4495 | TypeSourceInfo *getTypeSourceInfo() const { | |||
4496 | return TInfo; | |||
4497 | } | |||
4498 | void setTypeSourceInfo(TypeSourceInfo *ti) { | |||
4499 | TInfo = ti; | |||
4500 | } | |||
4501 | ||||
4502 | /// getBuiltinLoc - Return the location of the __builtin_convertvector token. | |||
4503 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } | |||
4504 | ||||
4505 | /// getRParenLoc - Return the location of final right parenthesis. | |||
4506 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
4507 | ||||
4508 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return BuiltinLoc; } | |||
4509 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return RParenLoc; } | |||
4510 | ||||
4511 | static bool classof(const Stmt *T) { | |||
4512 | return T->getStmtClass() == ConvertVectorExprClass; | |||
4513 | } | |||
4514 | ||||
4515 | // Iterators | |||
4516 | child_range children() { return child_range(&SrcExpr, &SrcExpr+1); } | |||
4517 | const_child_range children() const { | |||
4518 | return const_child_range(&SrcExpr, &SrcExpr + 1); | |||
4519 | } | |||
4520 | }; | |||
4521 | ||||
4522 | /// ChooseExpr - GNU builtin-in function __builtin_choose_expr. | |||
4523 | /// This AST node is similar to the conditional operator (?:) in C, with | |||
4524 | /// the following exceptions: | |||
4525 | /// - the test expression must be a integer constant expression. | |||
4526 | /// - the expression returned acts like the chosen subexpression in every | |||
4527 | /// visible way: the type is the same as that of the chosen subexpression, | |||
4528 | /// and all predicates (whether it's an l-value, whether it's an integer | |||
4529 | /// constant expression, etc.) return the same result as for the chosen | |||
4530 | /// sub-expression. | |||
4531 | class ChooseExpr : public Expr { | |||
4532 | enum { COND, LHS, RHS, END_EXPR }; | |||
4533 | Stmt* SubExprs[END_EXPR]; // Left/Middle/Right hand sides. | |||
4534 | SourceLocation BuiltinLoc, RParenLoc; | |||
4535 | bool CondIsTrue; | |||
4536 | public: | |||
4537 | ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t, | |||
4538 | ExprValueKind VK, ExprObjectKind OK, SourceLocation RP, | |||
4539 | bool condIsTrue) | |||
4540 | : Expr(ChooseExprClass, t, VK, OK), BuiltinLoc(BLoc), RParenLoc(RP), | |||
4541 | CondIsTrue(condIsTrue) { | |||
4542 | SubExprs[COND] = cond; | |||
4543 | SubExprs[LHS] = lhs; | |||
4544 | SubExprs[RHS] = rhs; | |||
4545 | ||||
4546 | setDependence(computeDependence(this)); | |||
4547 | } | |||
4548 | ||||
4549 | /// Build an empty __builtin_choose_expr. | |||
4550 | explicit ChooseExpr(EmptyShell Empty) : Expr(ChooseExprClass, Empty) { } | |||
4551 | ||||
4552 | /// isConditionTrue - Return whether the condition is true (i.e. not | |||
4553 | /// equal to zero). | |||
4554 | bool isConditionTrue() const { | |||
4555 | assert(!isConditionDependent() &&(static_cast <bool> (!isConditionDependent() && "Dependent condition isn't true or false") ? void (0) : __assert_fail ("!isConditionDependent() && \"Dependent condition isn't true or false\"" , "clang/include/clang/AST/Expr.h", 4556, __extension__ __PRETTY_FUNCTION__ )) | |||
4556 | "Dependent condition isn't true or false")(static_cast <bool> (!isConditionDependent() && "Dependent condition isn't true or false") ? void (0) : __assert_fail ("!isConditionDependent() && \"Dependent condition isn't true or false\"" , "clang/include/clang/AST/Expr.h", 4556, __extension__ __PRETTY_FUNCTION__ )); | |||
4557 | return CondIsTrue; | |||
4558 | } | |||
4559 | void setIsConditionTrue(bool isTrue) { CondIsTrue = isTrue; } | |||
4560 | ||||
4561 | bool isConditionDependent() const { | |||
4562 | return getCond()->isTypeDependent() || getCond()->isValueDependent(); | |||
4563 | } | |||
4564 | ||||
4565 | /// getChosenSubExpr - Return the subexpression chosen according to the | |||
4566 | /// condition. | |||
4567 | Expr *getChosenSubExpr() const { | |||
4568 | return isConditionTrue() ? getLHS() : getRHS(); | |||
4569 | } | |||
4570 | ||||
4571 | Expr *getCond() const { return cast<Expr>(SubExprs[COND]); } | |||
4572 | void setCond(Expr *E) { SubExprs[COND] = E; } | |||
4573 | Expr *getLHS() const { return cast<Expr>(SubExprs[LHS]); } | |||
4574 | void setLHS(Expr *E) { SubExprs[LHS] = E; } | |||
4575 | Expr *getRHS() const { return cast<Expr>(SubExprs[RHS]); } | |||
4576 | void setRHS(Expr *E) { SubExprs[RHS] = E; } | |||
4577 | ||||
4578 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } | |||
4579 | void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } | |||
4580 | ||||
4581 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
4582 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } | |||
4583 | ||||
4584 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return BuiltinLoc; } | |||
4585 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return RParenLoc; } | |||
4586 | ||||
4587 | static bool classof(const Stmt *T) { | |||
4588 | return T->getStmtClass() == ChooseExprClass; | |||
4589 | } | |||
4590 | ||||
4591 | // Iterators | |||
4592 | child_range children() { | |||
4593 | return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); | |||
4594 | } | |||
4595 | const_child_range children() const { | |||
4596 | return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); | |||
4597 | } | |||
4598 | }; | |||
4599 | ||||
4600 | /// GNUNullExpr - Implements the GNU __null extension, which is a name | |||
4601 | /// for a null pointer constant that has integral type (e.g., int or | |||
4602 | /// long) and is the same size and alignment as a pointer. The __null | |||
4603 | /// extension is typically only used by system headers, which define | |||
4604 | /// NULL as __null in C++ rather than using 0 (which is an integer | |||
4605 | /// that may not match the size of a pointer). | |||
4606 | class GNUNullExpr : public Expr { | |||
4607 | /// TokenLoc - The location of the __null keyword. | |||
4608 | SourceLocation TokenLoc; | |||
4609 | ||||
4610 | public: | |||
4611 | GNUNullExpr(QualType Ty, SourceLocation Loc) | |||
4612 | : Expr(GNUNullExprClass, Ty, VK_PRValue, OK_Ordinary), TokenLoc(Loc) { | |||
4613 | setDependence(ExprDependence::None); | |||
4614 | } | |||
4615 | ||||
4616 | /// Build an empty GNU __null expression. | |||
4617 | explicit GNUNullExpr(EmptyShell Empty) : Expr(GNUNullExprClass, Empty) { } | |||
4618 | ||||
4619 | /// getTokenLocation - The location of the __null token. | |||
4620 | SourceLocation getTokenLocation() const { return TokenLoc; } | |||
4621 | void setTokenLocation(SourceLocation L) { TokenLoc = L; } | |||
4622 | ||||
4623 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return TokenLoc; } | |||
4624 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return TokenLoc; } | |||
4625 | ||||
4626 | static bool classof(const Stmt *T) { | |||
4627 | return T->getStmtClass() == GNUNullExprClass; | |||
4628 | } | |||
4629 | ||||
4630 | // Iterators | |||
4631 | child_range children() { | |||
4632 | return child_range(child_iterator(), child_iterator()); | |||
4633 | } | |||
4634 | const_child_range children() const { | |||
4635 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
4636 | } | |||
4637 | }; | |||
4638 | ||||
4639 | /// Represents a call to the builtin function \c __builtin_va_arg. | |||
4640 | class VAArgExpr : public Expr { | |||
4641 | Stmt *Val; | |||
4642 | llvm::PointerIntPair<TypeSourceInfo *, 1, bool> TInfo; | |||
4643 | SourceLocation BuiltinLoc, RParenLoc; | |||
4644 | public: | |||
4645 | VAArgExpr(SourceLocation BLoc, Expr *e, TypeSourceInfo *TInfo, | |||
4646 | SourceLocation RPLoc, QualType t, bool IsMS) | |||
4647 | : Expr(VAArgExprClass, t, VK_PRValue, OK_Ordinary), Val(e), | |||
4648 | TInfo(TInfo, IsMS), BuiltinLoc(BLoc), RParenLoc(RPLoc) { | |||
4649 | setDependence(computeDependence(this)); | |||
4650 | } | |||
4651 | ||||
4652 | /// Create an empty __builtin_va_arg expression. | |||
4653 | explicit VAArgExpr(EmptyShell Empty) | |||
4654 | : Expr(VAArgExprClass, Empty), Val(nullptr), TInfo(nullptr, false) {} | |||
4655 | ||||
4656 | const Expr *getSubExpr() const { return cast<Expr>(Val); } | |||
4657 | Expr *getSubExpr() { return cast<Expr>(Val); } | |||
4658 | void setSubExpr(Expr *E) { Val = E; } | |||
4659 | ||||
4660 | /// Returns whether this is really a Win64 ABI va_arg expression. | |||
4661 | bool isMicrosoftABI() const { return TInfo.getInt(); } | |||
4662 | void setIsMicrosoftABI(bool IsMS) { TInfo.setInt(IsMS); } | |||
4663 | ||||
4664 | TypeSourceInfo *getWrittenTypeInfo() const { return TInfo.getPointer(); } | |||
4665 | void setWrittenTypeInfo(TypeSourceInfo *TI) { TInfo.setPointer(TI); } | |||
4666 | ||||
4667 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } | |||
4668 | void setBuiltinLoc(SourceLocation L) { BuiltinLoc = L; } | |||
4669 | ||||
4670 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
4671 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } | |||
4672 | ||||
4673 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return BuiltinLoc; } | |||
4674 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return RParenLoc; } | |||
4675 | ||||
4676 | static bool classof(const Stmt *T) { | |||
4677 | return T->getStmtClass() == VAArgExprClass; | |||
4678 | } | |||
4679 | ||||
4680 | // Iterators | |||
4681 | child_range children() { return child_range(&Val, &Val+1); } | |||
4682 | const_child_range children() const { | |||
4683 | return const_child_range(&Val, &Val + 1); | |||
4684 | } | |||
4685 | }; | |||
4686 | ||||
4687 | /// Represents a function call to one of __builtin_LINE(), __builtin_COLUMN(), | |||
4688 | /// __builtin_FUNCTION(), __builtin_FILE(), __builtin_FILE_NAME(), | |||
4689 | /// or __builtin_source_location(). | |||
4690 | class SourceLocExpr final : public Expr { | |||
4691 | SourceLocation BuiltinLoc, RParenLoc; | |||
4692 | DeclContext *ParentContext; | |||
4693 | ||||
4694 | public: | |||
4695 | enum IdentKind { Function, File, FileName, Line, Column, SourceLocStruct }; | |||
4696 | ||||
4697 | SourceLocExpr(const ASTContext &Ctx, IdentKind Type, QualType ResultTy, | |||
4698 | SourceLocation BLoc, SourceLocation RParenLoc, | |||
4699 | DeclContext *Context); | |||
4700 | ||||
4701 | /// Build an empty call expression. | |||
4702 | explicit SourceLocExpr(EmptyShell Empty) : Expr(SourceLocExprClass, Empty) {} | |||
4703 | ||||
4704 | /// Return the result of evaluating this SourceLocExpr in the specified | |||
4705 | /// (and possibly null) default argument or initialization context. | |||
4706 | APValue EvaluateInContext(const ASTContext &Ctx, | |||
4707 | const Expr *DefaultExpr) const; | |||
4708 | ||||
4709 | /// Return a string representing the name of the specific builtin function. | |||
4710 | StringRef getBuiltinStr() const; | |||
4711 | ||||
4712 | IdentKind getIdentKind() const { | |||
4713 | return static_cast<IdentKind>(SourceLocExprBits.Kind); | |||
4714 | } | |||
4715 | ||||
4716 | bool isIntType() const { | |||
4717 | switch (getIdentKind()) { | |||
4718 | case File: | |||
4719 | case FileName: | |||
4720 | case Function: | |||
4721 | case SourceLocStruct: | |||
4722 | return false; | |||
4723 | case Line: | |||
4724 | case Column: | |||
4725 | return true; | |||
4726 | } | |||
4727 | llvm_unreachable("unknown source location expression kind")::llvm::llvm_unreachable_internal("unknown source location expression kind" , "clang/include/clang/AST/Expr.h", 4727); | |||
4728 | } | |||
4729 | ||||
4730 | /// If the SourceLocExpr has been resolved return the subexpression | |||
4731 | /// representing the resolved value. Otherwise return null. | |||
4732 | const DeclContext *getParentContext() const { return ParentContext; } | |||
4733 | DeclContext *getParentContext() { return ParentContext; } | |||
4734 | ||||
4735 | SourceLocation getLocation() const { return BuiltinLoc; } | |||
4736 | SourceLocation getBeginLoc() const { return BuiltinLoc; } | |||
4737 | SourceLocation getEndLoc() const { return RParenLoc; } | |||
4738 | ||||
4739 | child_range children() { | |||
4740 | return child_range(child_iterator(), child_iterator()); | |||
4741 | } | |||
4742 | ||||
4743 | const_child_range children() const { | |||
4744 | return const_child_range(child_iterator(), child_iterator()); | |||
4745 | } | |||
4746 | ||||
4747 | static bool classof(const Stmt *T) { | |||
4748 | return T->getStmtClass() == SourceLocExprClass; | |||
4749 | } | |||
4750 | ||||
4751 | private: | |||
4752 | friend class ASTStmtReader; | |||
4753 | }; | |||
4754 | ||||
4755 | /// Describes an C or C++ initializer list. | |||
4756 | /// | |||
4757 | /// InitListExpr describes an initializer list, which can be used to | |||
4758 | /// initialize objects of different types, including | |||
4759 | /// struct/class/union types, arrays, and vectors. For example: | |||
4760 | /// | |||
4761 | /// @code | |||
4762 | /// struct foo x = { 1, { 2, 3 } }; | |||
4763 | /// @endcode | |||
4764 | /// | |||
4765 | /// Prior to semantic analysis, an initializer list will represent the | |||
4766 | /// initializer list as written by the user, but will have the | |||
4767 | /// placeholder type "void". This initializer list is called the | |||
4768 | /// syntactic form of the initializer, and may contain C99 designated | |||
4769 | /// initializers (represented as DesignatedInitExprs), initializations | |||
4770 | /// of subobject members without explicit braces, and so on. Clients | |||
4771 | /// interested in the original syntax of the initializer list should | |||
4772 | /// use the syntactic form of the initializer list. | |||
4773 | /// | |||
4774 | /// After semantic analysis, the initializer list will represent the | |||
4775 | /// semantic form of the initializer, where the initializations of all | |||
4776 | /// subobjects are made explicit with nested InitListExpr nodes and | |||
4777 | /// C99 designators have been eliminated by placing the designated | |||
4778 | /// initializations into the subobject they initialize. Additionally, | |||
4779 | /// any "holes" in the initialization, where no initializer has been | |||
4780 | /// specified for a particular subobject, will be replaced with | |||
4781 | /// implicitly-generated ImplicitValueInitExpr expressions that | |||
4782 | /// value-initialize the subobjects. Note, however, that the | |||
4783 | /// initializer lists may still have fewer initializers than there are | |||
4784 | /// elements to initialize within the object. | |||
4785 | /// | |||
4786 | /// After semantic analysis has completed, given an initializer list, | |||
4787 | /// method isSemanticForm() returns true if and only if this is the | |||
4788 | /// semantic form of the initializer list (note: the same AST node | |||
4789 | /// may at the same time be the syntactic form). | |||
4790 | /// Given the semantic form of the initializer list, one can retrieve | |||
4791 | /// the syntactic form of that initializer list (when different) | |||
4792 | /// using method getSyntacticForm(); the method returns null if applied | |||
4793 | /// to a initializer list which is already in syntactic form. | |||
4794 | /// Similarly, given the syntactic form (i.e., an initializer list such | |||
4795 | /// that isSemanticForm() returns false), one can retrieve the semantic | |||
4796 | /// form using method getSemanticForm(). | |||
4797 | /// Since many initializer lists have the same syntactic and semantic forms, | |||
4798 | /// getSyntacticForm() may return NULL, indicating that the current | |||
4799 | /// semantic initializer list also serves as its syntactic form. | |||
4800 | class InitListExpr : public Expr { | |||
4801 | // FIXME: Eliminate this vector in favor of ASTContext allocation | |||
4802 | typedef ASTVector<Stmt *> InitExprsTy; | |||
4803 | InitExprsTy InitExprs; | |||
4804 | SourceLocation LBraceLoc, RBraceLoc; | |||
4805 | ||||
4806 | /// The alternative form of the initializer list (if it exists). | |||
4807 | /// The int part of the pair stores whether this initializer list is | |||
4808 | /// in semantic form. If not null, the pointer points to: | |||
4809 | /// - the syntactic form, if this is in semantic form; | |||
4810 | /// - the semantic form, if this is in syntactic form. | |||
4811 | llvm::PointerIntPair<InitListExpr *, 1, bool> AltForm; | |||
4812 | ||||
4813 | /// Either: | |||
4814 | /// If this initializer list initializes an array with more elements than | |||
4815 | /// there are initializers in the list, specifies an expression to be used | |||
4816 | /// for value initialization of the rest of the elements. | |||
4817 | /// Or | |||
4818 | /// If this initializer list initializes a union, specifies which | |||
4819 | /// field within the union will be initialized. | |||
4820 | llvm::PointerUnion<Expr *, FieldDecl *> ArrayFillerOrUnionFieldInit; | |||
4821 | ||||
4822 | public: | |||
4823 | InitListExpr(const ASTContext &C, SourceLocation lbraceloc, | |||
4824 | ArrayRef<Expr*> initExprs, SourceLocation rbraceloc); | |||
4825 | ||||
4826 | /// Build an empty initializer list. | |||
4827 | explicit InitListExpr(EmptyShell Empty) | |||
4828 | : Expr(InitListExprClass, Empty), AltForm(nullptr, true) { } | |||
4829 | ||||
4830 | unsigned getNumInits() const { return InitExprs.size(); } | |||
4831 | ||||
4832 | /// Retrieve the set of initializers. | |||
4833 | Expr **getInits() { return reinterpret_cast<Expr **>(InitExprs.data()); } | |||
4834 | ||||
4835 | /// Retrieve the set of initializers. | |||
4836 | Expr * const *getInits() const { | |||
4837 | return reinterpret_cast<Expr * const *>(InitExprs.data()); | |||
4838 | } | |||
4839 | ||||
4840 | ArrayRef<Expr *> inits() { return llvm::ArrayRef(getInits(), getNumInits()); } | |||
4841 | ||||
4842 | ArrayRef<Expr *> inits() const { | |||
4843 | return llvm::ArrayRef(getInits(), getNumInits()); | |||
4844 | } | |||
4845 | ||||
4846 | const Expr *getInit(unsigned Init) const { | |||
4847 | assert(Init < getNumInits() && "Initializer access out of range!")(static_cast <bool> (Init < getNumInits() && "Initializer access out of range!") ? void (0) : __assert_fail ("Init < getNumInits() && \"Initializer access out of range!\"" , "clang/include/clang/AST/Expr.h", 4847, __extension__ __PRETTY_FUNCTION__ )); | |||
4848 | return cast_or_null<Expr>(InitExprs[Init]); | |||
4849 | } | |||
4850 | ||||
4851 | Expr *getInit(unsigned Init) { | |||
4852 | assert(Init < getNumInits() && "Initializer access out of range!")(static_cast <bool> (Init < getNumInits() && "Initializer access out of range!") ? void (0) : __assert_fail ("Init < getNumInits() && \"Initializer access out of range!\"" , "clang/include/clang/AST/Expr.h", 4852, __extension__ __PRETTY_FUNCTION__ )); | |||
4853 | return cast_or_null<Expr>(InitExprs[Init]); | |||
4854 | } | |||
4855 | ||||
4856 | void setInit(unsigned Init, Expr *expr) { | |||
4857 | assert(Init < getNumInits() && "Initializer access out of range!")(static_cast <bool> (Init < getNumInits() && "Initializer access out of range!") ? void (0) : __assert_fail ("Init < getNumInits() && \"Initializer access out of range!\"" , "clang/include/clang/AST/Expr.h", 4857, __extension__ __PRETTY_FUNCTION__ )); | |||
4858 | InitExprs[Init] = expr; | |||
4859 | ||||
4860 | if (expr) | |||
4861 | setDependence(getDependence() | expr->getDependence()); | |||
4862 | } | |||
4863 | ||||
4864 | /// Mark the semantic form of the InitListExpr as error when the semantic | |||
4865 | /// analysis fails. | |||
4866 | void markError() { | |||
4867 | assert(isSemanticForm())(static_cast <bool> (isSemanticForm()) ? void (0) : __assert_fail ("isSemanticForm()", "clang/include/clang/AST/Expr.h", 4867, __extension__ __PRETTY_FUNCTION__)); | |||
4868 | setDependence(getDependence() | ExprDependence::ErrorDependent); | |||
4869 | } | |||
4870 | ||||
4871 | /// Reserve space for some number of initializers. | |||
4872 | void reserveInits(const ASTContext &C, unsigned NumInits); | |||
4873 | ||||
4874 | /// Specify the number of initializers | |||
4875 | /// | |||
4876 | /// If there are more than @p NumInits initializers, the remaining | |||
4877 | /// initializers will be destroyed. If there are fewer than @p | |||
4878 | /// NumInits initializers, NULL expressions will be added for the | |||
4879 | /// unknown initializers. | |||
4880 | void resizeInits(const ASTContext &Context, unsigned NumInits); | |||
4881 | ||||
4882 | /// Updates the initializer at index @p Init with the new | |||
4883 | /// expression @p expr, and returns the old expression at that | |||
4884 | /// location. | |||
4885 | /// | |||
4886 | /// When @p Init is out of range for this initializer list, the | |||
4887 | /// initializer list will be extended with NULL expressions to | |||
4888 | /// accommodate the new entry. | |||
4889 | Expr *updateInit(const ASTContext &C, unsigned Init, Expr *expr); | |||
4890 | ||||
4891 | /// If this initializer list initializes an array with more elements | |||
4892 | /// than there are initializers in the list, specifies an expression to be | |||
4893 | /// used for value initialization of the rest of the elements. | |||
4894 | Expr *getArrayFiller() { | |||
4895 | return ArrayFillerOrUnionFieldInit.dyn_cast<Expr *>(); | |||
4896 | } | |||
4897 | const Expr *getArrayFiller() const { | |||
4898 | return const_cast<InitListExpr *>(this)->getArrayFiller(); | |||
4899 | } | |||
4900 | void setArrayFiller(Expr *filler); | |||
4901 | ||||
4902 | /// Return true if this is an array initializer and its array "filler" | |||
4903 | /// has been set. | |||
4904 | bool hasArrayFiller() const { return getArrayFiller(); } | |||
4905 | ||||
4906 | /// Determine whether this initializer list contains a designated initializer. | |||
4907 | bool hasDesignatedInit() const { | |||
4908 | return std::any_of(begin(), end(), [](const Stmt *S) { | |||
4909 | return isa<DesignatedInitExpr>(S); | |||
4910 | }); | |||
4911 | } | |||
4912 | ||||
4913 | /// If this initializes a union, specifies which field in the | |||
4914 | /// union to initialize. | |||
4915 | /// | |||
4916 | /// Typically, this field is the first named field within the | |||
4917 | /// union. However, a designated initializer can specify the | |||
4918 | /// initialization of a different field within the union. | |||
4919 | FieldDecl *getInitializedFieldInUnion() { | |||
4920 | return ArrayFillerOrUnionFieldInit.dyn_cast<FieldDecl *>(); | |||
4921 | } | |||
4922 | const FieldDecl *getInitializedFieldInUnion() const { | |||
4923 | return const_cast<InitListExpr *>(this)->getInitializedFieldInUnion(); | |||
4924 | } | |||
4925 | void setInitializedFieldInUnion(FieldDecl *FD) { | |||
4926 | assert((FD == nullptr(static_cast <bool> ((FD == nullptr || getInitializedFieldInUnion () == nullptr || getInitializedFieldInUnion() == FD) && "Only one field of a union may be initialized at a time!") ? void (0) : __assert_fail ("(FD == nullptr || getInitializedFieldInUnion() == nullptr || getInitializedFieldInUnion() == FD) && \"Only one field of a union may be initialized at a time!\"" , "clang/include/clang/AST/Expr.h", 4929, __extension__ __PRETTY_FUNCTION__ )) | |||
4927 | || getInitializedFieldInUnion() == nullptr(static_cast <bool> ((FD == nullptr || getInitializedFieldInUnion () == nullptr || getInitializedFieldInUnion() == FD) && "Only one field of a union may be initialized at a time!") ? void (0) : __assert_fail ("(FD == nullptr || getInitializedFieldInUnion() == nullptr || getInitializedFieldInUnion() == FD) && \"Only one field of a union may be initialized at a time!\"" , "clang/include/clang/AST/Expr.h", 4929, __extension__ __PRETTY_FUNCTION__ )) | |||
4928 | || getInitializedFieldInUnion() == FD)(static_cast <bool> ((FD == nullptr || getInitializedFieldInUnion () == nullptr || getInitializedFieldInUnion() == FD) && "Only one field of a union may be initialized at a time!") ? void (0) : __assert_fail ("(FD == nullptr || getInitializedFieldInUnion() == nullptr || getInitializedFieldInUnion() == FD) && \"Only one field of a union may be initialized at a time!\"" , "clang/include/clang/AST/Expr.h", 4929, __extension__ __PRETTY_FUNCTION__ )) | |||
4929 | && "Only one field of a union may be initialized at a time!")(static_cast <bool> ((FD == nullptr || getInitializedFieldInUnion () == nullptr || getInitializedFieldInUnion() == FD) && "Only one field of a union may be initialized at a time!") ? void (0) : __assert_fail ("(FD == nullptr || getInitializedFieldInUnion() == nullptr || getInitializedFieldInUnion() == FD) && \"Only one field of a union may be initialized at a time!\"" , "clang/include/clang/AST/Expr.h", 4929, __extension__ __PRETTY_FUNCTION__ )); | |||
4930 | ArrayFillerOrUnionFieldInit = FD; | |||
4931 | } | |||
4932 | ||||
4933 | // Explicit InitListExpr's originate from source code (and have valid source | |||
4934 | // locations). Implicit InitListExpr's are created by the semantic analyzer. | |||
4935 | // FIXME: This is wrong; InitListExprs created by semantic analysis have | |||
4936 | // valid source locations too! | |||
4937 | bool isExplicit() const { | |||
4938 | return LBraceLoc.isValid() && RBraceLoc.isValid(); | |||
4939 | } | |||
4940 | ||||
4941 | /// Is this an initializer for an array of characters, initialized by a string | |||
4942 | /// literal or an @encode? | |||
4943 | bool isStringLiteralInit() const; | |||
4944 | ||||
4945 | /// Is this a transparent initializer list (that is, an InitListExpr that is | |||
4946 | /// purely syntactic, and whose semantics are that of the sole contained | |||
4947 | /// initializer)? | |||
4948 | bool isTransparent() const; | |||
4949 | ||||
4950 | /// Is this the zero initializer {0} in a language which considers it | |||
4951 | /// idiomatic? | |||
4952 | bool isIdiomaticZeroInitializer(const LangOptions &LangOpts) const; | |||
4953 | ||||
4954 | SourceLocation getLBraceLoc() const { return LBraceLoc; } | |||
4955 | void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } | |||
4956 | SourceLocation getRBraceLoc() const { return RBraceLoc; } | |||
4957 | void setRBraceLoc(SourceLocation Loc) { RBraceLoc = Loc; } | |||
4958 | ||||
4959 | bool isSemanticForm() const { return AltForm.getInt(); } | |||
4960 | InitListExpr *getSemanticForm() const { | |||
4961 | return isSemanticForm() ? nullptr : AltForm.getPointer(); | |||
4962 | } | |||
4963 | bool isSyntacticForm() const { | |||
4964 | return !AltForm.getInt() || !AltForm.getPointer(); | |||
4965 | } | |||
4966 | InitListExpr *getSyntacticForm() const { | |||
4967 | return isSemanticForm() ? AltForm.getPointer() : nullptr; | |||
4968 | } | |||
4969 | ||||
4970 | void setSyntacticForm(InitListExpr *Init) { | |||
4971 | AltForm.setPointer(Init); | |||
4972 | AltForm.setInt(true); | |||
4973 | Init->AltForm.setPointer(this); | |||
4974 | Init->AltForm.setInt(false); | |||
4975 | } | |||
4976 | ||||
4977 | bool hadArrayRangeDesignator() const { | |||
4978 | return InitListExprBits.HadArrayRangeDesignator != 0; | |||
4979 | } | |||
4980 | void sawArrayRangeDesignator(bool ARD = true) { | |||
4981 | InitListExprBits.HadArrayRangeDesignator = ARD; | |||
4982 | } | |||
4983 | ||||
4984 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
4985 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
4986 | ||||
4987 | static bool classof(const Stmt *T) { | |||
4988 | return T->getStmtClass() == InitListExprClass; | |||
4989 | } | |||
4990 | ||||
4991 | // Iterators | |||
4992 | child_range children() { | |||
4993 | const_child_range CCR = const_cast<const InitListExpr *>(this)->children(); | |||
4994 | return child_range(cast_away_const(CCR.begin()), | |||
4995 | cast_away_const(CCR.end())); | |||
4996 | } | |||
4997 | ||||
4998 | const_child_range children() const { | |||
4999 | // FIXME: This does not include the array filler expression. | |||
5000 | if (InitExprs.empty()) | |||
5001 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
5002 | return const_child_range(&InitExprs[0], &InitExprs[0] + InitExprs.size()); | |||
5003 | } | |||
5004 | ||||
5005 | typedef InitExprsTy::iterator iterator; | |||
5006 | typedef InitExprsTy::const_iterator const_iterator; | |||
5007 | typedef InitExprsTy::reverse_iterator reverse_iterator; | |||
5008 | typedef InitExprsTy::const_reverse_iterator const_reverse_iterator; | |||
5009 | ||||
5010 | iterator begin() { return InitExprs.begin(); } | |||
5011 | const_iterator begin() const { return InitExprs.begin(); } | |||
5012 | iterator end() { return InitExprs.end(); } | |||
5013 | const_iterator end() const { return InitExprs.end(); } | |||
5014 | reverse_iterator rbegin() { return InitExprs.rbegin(); } | |||
5015 | const_reverse_iterator rbegin() const { return InitExprs.rbegin(); } | |||
5016 | reverse_iterator rend() { return InitExprs.rend(); } | |||
5017 | const_reverse_iterator rend() const { return InitExprs.rend(); } | |||
5018 | ||||
5019 | friend class ASTStmtReader; | |||
5020 | friend class ASTStmtWriter; | |||
5021 | }; | |||
5022 | ||||
5023 | /// Represents a C99 designated initializer expression. | |||
5024 | /// | |||
5025 | /// A designated initializer expression (C99 6.7.8) contains one or | |||
5026 | /// more designators (which can be field designators, array | |||
5027 | /// designators, or GNU array-range designators) followed by an | |||
5028 | /// expression that initializes the field or element(s) that the | |||
5029 | /// designators refer to. For example, given: | |||
5030 | /// | |||
5031 | /// @code | |||
5032 | /// struct point { | |||
5033 | /// double x; | |||
5034 | /// double y; | |||
5035 | /// }; | |||
5036 | /// struct point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; | |||
5037 | /// @endcode | |||
5038 | /// | |||
5039 | /// The InitListExpr contains three DesignatedInitExprs, the first of | |||
5040 | /// which covers @c [2].y=1.0. This DesignatedInitExpr will have two | |||
5041 | /// designators, one array designator for @c [2] followed by one field | |||
5042 | /// designator for @c .y. The initialization expression will be 1.0. | |||
5043 | class DesignatedInitExpr final | |||
5044 | : public Expr, | |||
5045 | private llvm::TrailingObjects<DesignatedInitExpr, Stmt *> { | |||
5046 | public: | |||
5047 | /// Forward declaration of the Designator class. | |||
5048 | class Designator; | |||
5049 | ||||
5050 | private: | |||
5051 | /// The location of the '=' or ':' prior to the actual initializer | |||
5052 | /// expression. | |||
5053 | SourceLocation EqualOrColonLoc; | |||
5054 | ||||
5055 | /// Whether this designated initializer used the GNU deprecated | |||
5056 | /// syntax rather than the C99 '=' syntax. | |||
5057 | unsigned GNUSyntax : 1; | |||
5058 | ||||
5059 | /// The number of designators in this initializer expression. | |||
5060 | unsigned NumDesignators : 15; | |||
5061 | ||||
5062 | /// The number of subexpressions of this initializer expression, | |||
5063 | /// which contains both the initializer and any additional | |||
5064 | /// expressions used by array and array-range designators. | |||
5065 | unsigned NumSubExprs : 16; | |||
5066 | ||||
5067 | /// The designators in this designated initialization | |||
5068 | /// expression. | |||
5069 | Designator *Designators; | |||
5070 | ||||
5071 | DesignatedInitExpr(const ASTContext &C, QualType Ty, | |||
5072 | llvm::ArrayRef<Designator> Designators, | |||
5073 | SourceLocation EqualOrColonLoc, bool GNUSyntax, | |||
5074 | ArrayRef<Expr *> IndexExprs, Expr *Init); | |||
5075 | ||||
5076 | explicit DesignatedInitExpr(unsigned NumSubExprs) | |||
5077 | : Expr(DesignatedInitExprClass, EmptyShell()), | |||
5078 | NumDesignators(0), NumSubExprs(NumSubExprs), Designators(nullptr) { } | |||
5079 | ||||
5080 | public: | |||
5081 | /// Represents a single C99 designator. | |||
5082 | /// | |||
5083 | /// @todo This class is infuriatingly similar to clang::Designator, | |||
5084 | /// but minor differences (storing indices vs. storing pointers) | |||
5085 | /// keep us from reusing it. Try harder, later, to rectify these | |||
5086 | /// differences. | |||
5087 | class Designator { | |||
5088 | /// A field designator, e.g., ".x". | |||
5089 | struct FieldDesignatorInfo { | |||
5090 | /// Refers to the field that is being initialized. The low bit | |||
5091 | /// of this field determines whether this is actually a pointer | |||
5092 | /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When | |||
5093 | /// initially constructed, a field designator will store an | |||
5094 | /// IdentifierInfo*. After semantic analysis has resolved that | |||
5095 | /// name, the field designator will instead store a FieldDecl*. | |||
5096 | uintptr_t NameOrField; | |||
5097 | ||||
5098 | /// The location of the '.' in the designated initializer. | |||
5099 | SourceLocation DotLoc; | |||
5100 | ||||
5101 | /// The location of the field name in the designated initializer. | |||
5102 | SourceLocation FieldLoc; | |||
5103 | ||||
5104 | FieldDesignatorInfo(const IdentifierInfo *II, SourceLocation DotLoc, | |||
5105 | SourceLocation FieldLoc) | |||
5106 | : NameOrField(reinterpret_cast<uintptr_t>(II) | 0x1), DotLoc(DotLoc), | |||
5107 | FieldLoc(FieldLoc) {} | |||
5108 | }; | |||
5109 | ||||
5110 | /// An array or GNU array-range designator, e.g., "[9]" or "[10...15]". | |||
5111 | struct ArrayOrRangeDesignatorInfo { | |||
5112 | /// Location of the first index expression within the designated | |||
5113 | /// initializer expression's list of subexpressions. | |||
5114 | unsigned Index; | |||
5115 | ||||
5116 | /// The location of the '[' starting the array range designator. | |||
5117 | SourceLocation LBracketLoc; | |||
5118 | ||||
5119 | /// The location of the ellipsis separating the start and end | |||
5120 | /// indices. Only valid for GNU array-range designators. | |||
5121 | SourceLocation EllipsisLoc; | |||
5122 | ||||
5123 | /// The location of the ']' terminating the array range designator. | |||
5124 | SourceLocation RBracketLoc; | |||
5125 | ||||
5126 | ArrayOrRangeDesignatorInfo(unsigned Index, SourceLocation LBracketLoc, | |||
5127 | SourceLocation RBracketLoc) | |||
5128 | : Index(Index), LBracketLoc(LBracketLoc), RBracketLoc(RBracketLoc) {} | |||
5129 | ||||
5130 | ArrayOrRangeDesignatorInfo(unsigned Index, | |||
5131 | SourceLocation LBracketLoc, | |||
5132 | SourceLocation EllipsisLoc, | |||
5133 | SourceLocation RBracketLoc) | |||
5134 | : Index(Index), LBracketLoc(LBracketLoc), EllipsisLoc(EllipsisLoc), | |||
5135 | RBracketLoc(RBracketLoc) {} | |||
5136 | }; | |||
5137 | ||||
5138 | /// The kind of designator this describes. | |||
5139 | enum DesignatorKind { | |||
5140 | FieldDesignator, | |||
5141 | ArrayDesignator, | |||
5142 | ArrayRangeDesignator | |||
5143 | }; | |||
5144 | ||||
5145 | DesignatorKind Kind; | |||
5146 | ||||
5147 | union { | |||
5148 | /// A field designator, e.g., ".x". | |||
5149 | struct FieldDesignatorInfo FieldInfo; | |||
5150 | ||||
5151 | /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". | |||
5152 | struct ArrayOrRangeDesignatorInfo ArrayOrRangeInfo; | |||
5153 | }; | |||
5154 | ||||
5155 | Designator(DesignatorKind Kind) : Kind(Kind) {} | |||
5156 | ||||
5157 | public: | |||
5158 | Designator() {} | |||
5159 | ||||
5160 | bool isFieldDesignator() const { return Kind == FieldDesignator; } | |||
5161 | bool isArrayDesignator() const { return Kind == ArrayDesignator; } | |||
5162 | bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } | |||
5163 | ||||
5164 | //===------------------------------------------------------------------===// | |||
5165 | // FieldDesignatorInfo | |||
5166 | ||||
5167 | /// Creates a field designator. | |||
5168 | static Designator CreateFieldDesignator(const IdentifierInfo *FieldName, | |||
5169 | SourceLocation DotLoc, | |||
5170 | SourceLocation FieldLoc) { | |||
5171 | Designator D(FieldDesignator); | |||
5172 | new (&D.FieldInfo) FieldDesignatorInfo(FieldName, DotLoc, FieldLoc); | |||
5173 | return D; | |||
5174 | } | |||
5175 | ||||
5176 | const IdentifierInfo *getFieldName() const; | |||
5177 | ||||
5178 | FieldDecl *getFieldDecl() const { | |||
5179 | assert(isFieldDesignator() && "Only valid on a field designator")(static_cast <bool> (isFieldDesignator() && "Only valid on a field designator" ) ? void (0) : __assert_fail ("isFieldDesignator() && \"Only valid on a field designator\"" , "clang/include/clang/AST/Expr.h", 5179, __extension__ __PRETTY_FUNCTION__ )); | |||
5180 | if (FieldInfo.NameOrField & 0x01) | |||
5181 | return nullptr; | |||
5182 | return reinterpret_cast<FieldDecl *>(FieldInfo.NameOrField); | |||
5183 | } | |||
5184 | ||||
5185 | void setFieldDecl(FieldDecl *FD) { | |||
5186 | assert(isFieldDesignator() && "Only valid on a field designator")(static_cast <bool> (isFieldDesignator() && "Only valid on a field designator" ) ? void (0) : __assert_fail ("isFieldDesignator() && \"Only valid on a field designator\"" , "clang/include/clang/AST/Expr.h", 5186, __extension__ __PRETTY_FUNCTION__ )); | |||
5187 | FieldInfo.NameOrField = reinterpret_cast<uintptr_t>(FD); | |||
5188 | } | |||
5189 | ||||
5190 | SourceLocation getDotLoc() const { | |||
5191 | assert(isFieldDesignator() && "Only valid on a field designator")(static_cast <bool> (isFieldDesignator() && "Only valid on a field designator" ) ? void (0) : __assert_fail ("isFieldDesignator() && \"Only valid on a field designator\"" , "clang/include/clang/AST/Expr.h", 5191, __extension__ __PRETTY_FUNCTION__ )); | |||
5192 | return FieldInfo.DotLoc; | |||
5193 | } | |||
5194 | ||||
5195 | SourceLocation getFieldLoc() const { | |||
5196 | assert(isFieldDesignator() && "Only valid on a field designator")(static_cast <bool> (isFieldDesignator() && "Only valid on a field designator" ) ? void (0) : __assert_fail ("isFieldDesignator() && \"Only valid on a field designator\"" , "clang/include/clang/AST/Expr.h", 5196, __extension__ __PRETTY_FUNCTION__ )); | |||
5197 | return FieldInfo.FieldLoc; | |||
5198 | } | |||
5199 | ||||
5200 | //===------------------------------------------------------------------===// | |||
5201 | // ArrayOrRangeDesignator | |||
5202 | ||||
5203 | /// Creates an array designator. | |||
5204 | static Designator CreateArrayDesignator(unsigned Index, | |||
5205 | SourceLocation LBracketLoc, | |||
5206 | SourceLocation RBracketLoc) { | |||
5207 | Designator D(ArrayDesignator); | |||
5208 | new (&D.ArrayOrRangeInfo) ArrayOrRangeDesignatorInfo(Index, LBracketLoc, | |||
5209 | RBracketLoc); | |||
5210 | return D; | |||
5211 | } | |||
5212 | ||||
5213 | /// Creates a GNU array-range designator. | |||
5214 | static Designator CreateArrayRangeDesignator(unsigned Index, | |||
5215 | SourceLocation LBracketLoc, | |||
5216 | SourceLocation EllipsisLoc, | |||
5217 | SourceLocation RBracketLoc) { | |||
5218 | Designator D(ArrayRangeDesignator); | |||
5219 | new (&D.ArrayOrRangeInfo) ArrayOrRangeDesignatorInfo(Index, LBracketLoc, | |||
5220 | EllipsisLoc, | |||
5221 | RBracketLoc); | |||
5222 | return D; | |||
5223 | } | |||
5224 | ||||
5225 | unsigned getArrayIndex() const { | |||
5226 | assert((isArrayDesignator() || isArrayRangeDesignator()) &&(static_cast <bool> ((isArrayDesignator() || isArrayRangeDesignator ()) && "Only valid on an array or array-range designator" ) ? void (0) : __assert_fail ("(isArrayDesignator() || isArrayRangeDesignator()) && \"Only valid on an array or array-range designator\"" , "clang/include/clang/AST/Expr.h", 5227, __extension__ __PRETTY_FUNCTION__ )) | |||
5227 | "Only valid on an array or array-range designator")(static_cast <bool> ((isArrayDesignator() || isArrayRangeDesignator ()) && "Only valid on an array or array-range designator" ) ? void (0) : __assert_fail ("(isArrayDesignator() || isArrayRangeDesignator()) && \"Only valid on an array or array-range designator\"" , "clang/include/clang/AST/Expr.h", 5227, __extension__ __PRETTY_FUNCTION__ )); | |||
5228 | return ArrayOrRangeInfo.Index; | |||
5229 | } | |||
5230 | ||||
5231 | SourceLocation getLBracketLoc() const { | |||
5232 | assert((isArrayDesignator() || isArrayRangeDesignator()) &&(static_cast <bool> ((isArrayDesignator() || isArrayRangeDesignator ()) && "Only valid on an array or array-range designator" ) ? void (0) : __assert_fail ("(isArrayDesignator() || isArrayRangeDesignator()) && \"Only valid on an array or array-range designator\"" , "clang/include/clang/AST/Expr.h", 5233, __extension__ __PRETTY_FUNCTION__ )) | |||
5233 | "Only valid on an array or array-range designator")(static_cast <bool> ((isArrayDesignator() || isArrayRangeDesignator ()) && "Only valid on an array or array-range designator" ) ? void (0) : __assert_fail ("(isArrayDesignator() || isArrayRangeDesignator()) && \"Only valid on an array or array-range designator\"" , "clang/include/clang/AST/Expr.h", 5233, __extension__ __PRETTY_FUNCTION__ )); | |||
5234 | return ArrayOrRangeInfo.LBracketLoc; | |||
5235 | } | |||
5236 | ||||
5237 | SourceLocation getEllipsisLoc() const { | |||
5238 | assert(isArrayRangeDesignator() &&(static_cast <bool> (isArrayRangeDesignator() && "Only valid on an array-range designator") ? void (0) : __assert_fail ("isArrayRangeDesignator() && \"Only valid on an array-range designator\"" , "clang/include/clang/AST/Expr.h", 5239, __extension__ __PRETTY_FUNCTION__ )) | |||
5239 | "Only valid on an array-range designator")(static_cast <bool> (isArrayRangeDesignator() && "Only valid on an array-range designator") ? void (0) : __assert_fail ("isArrayRangeDesignator() && \"Only valid on an array-range designator\"" , "clang/include/clang/AST/Expr.h", 5239, __extension__ __PRETTY_FUNCTION__ )); | |||
5240 | return ArrayOrRangeInfo.EllipsisLoc; | |||
5241 | } | |||
5242 | ||||
5243 | SourceLocation getRBracketLoc() const { | |||
5244 | assert((isArrayDesignator() || isArrayRangeDesignator()) &&(static_cast <bool> ((isArrayDesignator() || isArrayRangeDesignator ()) && "Only valid on an array or array-range designator" ) ? void (0) : __assert_fail ("(isArrayDesignator() || isArrayRangeDesignator()) && \"Only valid on an array or array-range designator\"" , "clang/include/clang/AST/Expr.h", 5245, __extension__ __PRETTY_FUNCTION__ )) | |||
5245 | "Only valid on an array or array-range designator")(static_cast <bool> ((isArrayDesignator() || isArrayRangeDesignator ()) && "Only valid on an array or array-range designator" ) ? void (0) : __assert_fail ("(isArrayDesignator() || isArrayRangeDesignator()) && \"Only valid on an array or array-range designator\"" , "clang/include/clang/AST/Expr.h", 5245, __extension__ __PRETTY_FUNCTION__ )); | |||
5246 | return ArrayOrRangeInfo.RBracketLoc; | |||
5247 | } | |||
5248 | ||||
5249 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
5250 | if (isFieldDesignator()) | |||
5251 | return getDotLoc().isInvalid() ? getFieldLoc() : getDotLoc(); | |||
5252 | return getLBracketLoc(); | |||
5253 | } | |||
5254 | ||||
5255 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
5256 | return isFieldDesignator() ? getFieldLoc() : getRBracketLoc(); | |||
5257 | } | |||
5258 | ||||
5259 | SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__)) { | |||
5260 | return SourceRange(getBeginLoc(), getEndLoc()); | |||
5261 | } | |||
5262 | }; | |||
5263 | ||||
5264 | static DesignatedInitExpr *Create(const ASTContext &C, | |||
5265 | llvm::ArrayRef<Designator> Designators, | |||
5266 | ArrayRef<Expr*> IndexExprs, | |||
5267 | SourceLocation EqualOrColonLoc, | |||
5268 | bool GNUSyntax, Expr *Init); | |||
5269 | ||||
5270 | static DesignatedInitExpr *CreateEmpty(const ASTContext &C, | |||
5271 | unsigned NumIndexExprs); | |||
5272 | ||||
5273 | /// Returns the number of designators in this initializer. | |||
5274 | unsigned size() const { return NumDesignators; } | |||
5275 | ||||
5276 | // Iterator access to the designators. | |||
5277 | llvm::MutableArrayRef<Designator> designators() { | |||
5278 | return {Designators, NumDesignators}; | |||
5279 | } | |||
5280 | ||||
5281 | llvm::ArrayRef<Designator> designators() const { | |||
5282 | return {Designators, NumDesignators}; | |||
5283 | } | |||
5284 | ||||
5285 | Designator *getDesignator(unsigned Idx) { return &designators()[Idx]; } | |||
5286 | const Designator *getDesignator(unsigned Idx) const { | |||
5287 | return &designators()[Idx]; | |||
5288 | } | |||
5289 | ||||
5290 | void setDesignators(const ASTContext &C, const Designator *Desigs, | |||
5291 | unsigned NumDesigs); | |||
5292 | ||||
5293 | Expr *getArrayIndex(const Designator &D) const; | |||
5294 | Expr *getArrayRangeStart(const Designator &D) const; | |||
5295 | Expr *getArrayRangeEnd(const Designator &D) const; | |||
5296 | ||||
5297 | /// Retrieve the location of the '=' that precedes the | |||
5298 | /// initializer value itself, if present. | |||
5299 | SourceLocation getEqualOrColonLoc() const { return EqualOrColonLoc; } | |||
5300 | void setEqualOrColonLoc(SourceLocation L) { EqualOrColonLoc = L; } | |||
5301 | ||||
5302 | /// Whether this designated initializer should result in direct-initialization | |||
5303 | /// of the designated subobject (eg, '{.foo{1, 2, 3}}'). | |||
5304 | bool isDirectInit() const { return EqualOrColonLoc.isInvalid(); } | |||
5305 | ||||
5306 | /// Determines whether this designated initializer used the | |||
5307 | /// deprecated GNU syntax for designated initializers. | |||
5308 | bool usesGNUSyntax() const { return GNUSyntax; } | |||
5309 | void setGNUSyntax(bool GNU) { GNUSyntax = GNU; } | |||
5310 | ||||
5311 | /// Retrieve the initializer value. | |||
5312 | Expr *getInit() const { | |||
5313 | return cast<Expr>(*const_cast<DesignatedInitExpr*>(this)->child_begin()); | |||
5314 | } | |||
5315 | ||||
5316 | void setInit(Expr *init) { | |||
5317 | *child_begin() = init; | |||
5318 | } | |||
5319 | ||||
5320 | /// Retrieve the total number of subexpressions in this | |||
5321 | /// designated initializer expression, including the actual | |||
5322 | /// initialized value and any expressions that occur within array | |||
5323 | /// and array-range designators. | |||
5324 | unsigned getNumSubExprs() const { return NumSubExprs; } | |||
5325 | ||||
5326 | Expr *getSubExpr(unsigned Idx) const { | |||
5327 | assert(Idx < NumSubExprs && "Subscript out of range")(static_cast <bool> (Idx < NumSubExprs && "Subscript out of range" ) ? void (0) : __assert_fail ("Idx < NumSubExprs && \"Subscript out of range\"" , "clang/include/clang/AST/Expr.h", 5327, __extension__ __PRETTY_FUNCTION__ )); | |||
5328 | return cast<Expr>(getTrailingObjects<Stmt *>()[Idx]); | |||
5329 | } | |||
5330 | ||||
5331 | void setSubExpr(unsigned Idx, Expr *E) { | |||
5332 | assert(Idx < NumSubExprs && "Subscript out of range")(static_cast <bool> (Idx < NumSubExprs && "Subscript out of range" ) ? void (0) : __assert_fail ("Idx < NumSubExprs && \"Subscript out of range\"" , "clang/include/clang/AST/Expr.h", 5332, __extension__ __PRETTY_FUNCTION__ )); | |||
5333 | getTrailingObjects<Stmt *>()[Idx] = E; | |||
5334 | } | |||
5335 | ||||
5336 | /// Replaces the designator at index @p Idx with the series | |||
5337 | /// of designators in [First, Last). | |||
5338 | void ExpandDesignator(const ASTContext &C, unsigned Idx, | |||
5339 | const Designator *First, const Designator *Last); | |||
5340 | ||||
5341 | SourceRange getDesignatorsSourceRange() const; | |||
5342 | ||||
5343 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
5344 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
5345 | ||||
5346 | static bool classof(const Stmt *T) { | |||
5347 | return T->getStmtClass() == DesignatedInitExprClass; | |||
5348 | } | |||
5349 | ||||
5350 | // Iterators | |||
5351 | child_range children() { | |||
5352 | Stmt **begin = getTrailingObjects<Stmt *>(); | |||
5353 | return child_range(begin, begin + NumSubExprs); | |||
5354 | } | |||
5355 | const_child_range children() const { | |||
5356 | Stmt * const *begin = getTrailingObjects<Stmt *>(); | |||
5357 | return const_child_range(begin, begin + NumSubExprs); | |||
5358 | } | |||
5359 | ||||
5360 | friend TrailingObjects; | |||
5361 | }; | |||
5362 | ||||
5363 | /// Represents a place-holder for an object not to be initialized by | |||
5364 | /// anything. | |||
5365 | /// | |||
5366 | /// This only makes sense when it appears as part of an updater of a | |||
5367 | /// DesignatedInitUpdateExpr (see below). The base expression of a DIUE | |||
5368 | /// initializes a big object, and the NoInitExpr's mark the spots within the | |||
5369 | /// big object not to be overwritten by the updater. | |||
5370 | /// | |||
5371 | /// \see DesignatedInitUpdateExpr | |||
5372 | class NoInitExpr : public Expr { | |||
5373 | public: | |||
5374 | explicit NoInitExpr(QualType ty) | |||
5375 | : Expr(NoInitExprClass, ty, VK_PRValue, OK_Ordinary) { | |||
5376 | setDependence(computeDependence(this)); | |||
5377 | } | |||
5378 | ||||
5379 | explicit NoInitExpr(EmptyShell Empty) | |||
5380 | : Expr(NoInitExprClass, Empty) { } | |||
5381 | ||||
5382 | static bool classof(const Stmt *T) { | |||
5383 | return T->getStmtClass() == NoInitExprClass; | |||
5384 | } | |||
5385 | ||||
5386 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return SourceLocation(); } | |||
5387 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return SourceLocation(); } | |||
5388 | ||||
5389 | // Iterators | |||
5390 | child_range children() { | |||
5391 | return child_range(child_iterator(), child_iterator()); | |||
5392 | } | |||
5393 | const_child_range children() const { | |||
5394 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
5395 | } | |||
5396 | }; | |||
5397 | ||||
5398 | // In cases like: | |||
5399 | // struct Q { int a, b, c; }; | |||
5400 | // Q *getQ(); | |||
5401 | // void foo() { | |||
5402 | // struct A { Q q; } a = { *getQ(), .q.b = 3 }; | |||
5403 | // } | |||
5404 | // | |||
5405 | // We will have an InitListExpr for a, with type A, and then a | |||
5406 | // DesignatedInitUpdateExpr for "a.q" with type Q. The "base" for this DIUE | |||
5407 | // is the call expression *getQ(); the "updater" for the DIUE is ".q.b = 3" | |||
5408 | // | |||
5409 | class DesignatedInitUpdateExpr : public Expr { | |||
5410 | // BaseAndUpdaterExprs[0] is the base expression; | |||
5411 | // BaseAndUpdaterExprs[1] is an InitListExpr overwriting part of the base. | |||
5412 | Stmt *BaseAndUpdaterExprs[2]; | |||
5413 | ||||
5414 | public: | |||
5415 | DesignatedInitUpdateExpr(const ASTContext &C, SourceLocation lBraceLoc, | |||
5416 | Expr *baseExprs, SourceLocation rBraceLoc); | |||
5417 | ||||
5418 | explicit DesignatedInitUpdateExpr(EmptyShell Empty) | |||
5419 | : Expr(DesignatedInitUpdateExprClass, Empty) { } | |||
5420 | ||||
5421 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
5422 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)); | |||
5423 | ||||
5424 | static bool classof(const Stmt *T) { | |||
5425 | return T->getStmtClass() == DesignatedInitUpdateExprClass; | |||
5426 | } | |||
5427 | ||||
5428 | Expr *getBase() const { return cast<Expr>(BaseAndUpdaterExprs[0]); } | |||
5429 | void setBase(Expr *Base) { BaseAndUpdaterExprs[0] = Base; } | |||
5430 | ||||
5431 | InitListExpr *getUpdater() const { | |||
5432 | return cast<InitListExpr>(BaseAndUpdaterExprs[1]); | |||
5433 | } | |||
5434 | void setUpdater(Expr *Updater) { BaseAndUpdaterExprs[1] = Updater; } | |||
5435 | ||||
5436 | // Iterators | |||
5437 | // children = the base and the updater | |||
5438 | child_range children() { | |||
5439 | return child_range(&BaseAndUpdaterExprs[0], &BaseAndUpdaterExprs[0] + 2); | |||
5440 | } | |||
5441 | const_child_range children() const { | |||
5442 | return const_child_range(&BaseAndUpdaterExprs[0], | |||
5443 | &BaseAndUpdaterExprs[0] + 2); | |||
5444 | } | |||
5445 | }; | |||
5446 | ||||
5447 | /// Represents a loop initializing the elements of an array. | |||
5448 | /// | |||
5449 | /// The need to initialize the elements of an array occurs in a number of | |||
5450 | /// contexts: | |||
5451 | /// | |||
5452 | /// * in the implicit copy/move constructor for a class with an array member | |||
5453 | /// * when a lambda-expression captures an array by value | |||
5454 | /// * when a decomposition declaration decomposes an array | |||
5455 | /// | |||
5456 | /// There are two subexpressions: a common expression (the source array) | |||
5457 | /// that is evaluated once up-front, and a per-element initializer that | |||
5458 | /// runs once for each array element. | |||
5459 | /// | |||
5460 | /// Within the per-element initializer, the common expression may be referenced | |||
5461 | /// via an OpaqueValueExpr, and the current index may be obtained via an | |||
5462 | /// ArrayInitIndexExpr. | |||
5463 | class ArrayInitLoopExpr : public Expr { | |||
5464 | Stmt *SubExprs[2]; | |||
5465 | ||||
5466 | explicit ArrayInitLoopExpr(EmptyShell Empty) | |||
5467 | : Expr(ArrayInitLoopExprClass, Empty), SubExprs{} {} | |||
5468 | ||||
5469 | public: | |||
5470 | explicit ArrayInitLoopExpr(QualType T, Expr *CommonInit, Expr *ElementInit) | |||
5471 | : Expr(ArrayInitLoopExprClass, T, VK_PRValue, OK_Ordinary), | |||
5472 | SubExprs{CommonInit, ElementInit} { | |||
5473 | setDependence(computeDependence(this)); | |||
5474 | } | |||
5475 | ||||
5476 | /// Get the common subexpression shared by all initializations (the source | |||
5477 | /// array). | |||
5478 | OpaqueValueExpr *getCommonExpr() const { | |||
5479 | return cast<OpaqueValueExpr>(SubExprs[0]); | |||
5480 | } | |||
5481 | ||||
5482 | /// Get the initializer to use for each array element. | |||
5483 | Expr *getSubExpr() const { return cast<Expr>(SubExprs[1]); } | |||
5484 | ||||
5485 | llvm::APInt getArraySize() const { | |||
5486 | return cast<ConstantArrayType>(getType()->castAsArrayTypeUnsafe()) | |||
5487 | ->getSize(); | |||
5488 | } | |||
5489 | ||||
5490 | static bool classof(const Stmt *S) { | |||
5491 | return S->getStmtClass() == ArrayInitLoopExprClass; | |||
5492 | } | |||
5493 | ||||
5494 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
5495 | return getCommonExpr()->getBeginLoc(); | |||
5496 | } | |||
5497 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
5498 | return getCommonExpr()->getEndLoc(); | |||
5499 | } | |||
5500 | ||||
5501 | child_range children() { | |||
5502 | return child_range(SubExprs, SubExprs + 2); | |||
5503 | } | |||
5504 | const_child_range children() const { | |||
5505 | return const_child_range(SubExprs, SubExprs + 2); | |||
5506 | } | |||
5507 | ||||
5508 | friend class ASTReader; | |||
5509 | friend class ASTStmtReader; | |||
5510 | friend class ASTStmtWriter; | |||
5511 | }; | |||
5512 | ||||
5513 | /// Represents the index of the current element of an array being | |||
5514 | /// initialized by an ArrayInitLoopExpr. This can only appear within the | |||
5515 | /// subexpression of an ArrayInitLoopExpr. | |||
5516 | class ArrayInitIndexExpr : public Expr { | |||
5517 | explicit ArrayInitIndexExpr(EmptyShell Empty) | |||
5518 | : Expr(ArrayInitIndexExprClass, Empty) {} | |||
5519 | ||||
5520 | public: | |||
5521 | explicit ArrayInitIndexExpr(QualType T) | |||
5522 | : Expr(ArrayInitIndexExprClass, T, VK_PRValue, OK_Ordinary) { | |||
5523 | setDependence(ExprDependence::None); | |||
5524 | } | |||
5525 | ||||
5526 | static bool classof(const Stmt *S) { | |||
5527 | return S->getStmtClass() == ArrayInitIndexExprClass; | |||
5528 | } | |||
5529 | ||||
5530 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return SourceLocation(); } | |||
5531 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return SourceLocation(); } | |||
5532 | ||||
5533 | child_range children() { | |||
5534 | return child_range(child_iterator(), child_iterator()); | |||
5535 | } | |||
5536 | const_child_range children() const { | |||
5537 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
5538 | } | |||
5539 | ||||
5540 | friend class ASTReader; | |||
5541 | friend class ASTStmtReader; | |||
5542 | }; | |||
5543 | ||||
5544 | /// Represents an implicitly-generated value initialization of | |||
5545 | /// an object of a given type. | |||
5546 | /// | |||
5547 | /// Implicit value initializations occur within semantic initializer | |||
5548 | /// list expressions (InitListExpr) as placeholders for subobject | |||
5549 | /// initializations not explicitly specified by the user. | |||
5550 | /// | |||
5551 | /// \see InitListExpr | |||
5552 | class ImplicitValueInitExpr : public Expr { | |||
5553 | public: | |||
5554 | explicit ImplicitValueInitExpr(QualType ty) | |||
5555 | : Expr(ImplicitValueInitExprClass, ty, VK_PRValue, OK_Ordinary) { | |||
5556 | setDependence(computeDependence(this)); | |||
5557 | } | |||
5558 | ||||
5559 | /// Construct an empty implicit value initialization. | |||
5560 | explicit ImplicitValueInitExpr(EmptyShell Empty) | |||
5561 | : Expr(ImplicitValueInitExprClass, Empty) { } | |||
5562 | ||||
5563 | static bool classof(const Stmt *T) { | |||
5564 | return T->getStmtClass() == ImplicitValueInitExprClass; | |||
5565 | } | |||
5566 | ||||
5567 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return SourceLocation(); } | |||
5568 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return SourceLocation(); } | |||
5569 | ||||
5570 | // Iterators | |||
5571 | child_range children() { | |||
5572 | return child_range(child_iterator(), child_iterator()); | |||
5573 | } | |||
5574 | const_child_range children() const { | |||
5575 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
5576 | } | |||
5577 | }; | |||
5578 | ||||
5579 | class ParenListExpr final | |||
5580 | : public Expr, | |||
5581 | private llvm::TrailingObjects<ParenListExpr, Stmt *> { | |||
5582 | friend class ASTStmtReader; | |||
5583 | friend TrailingObjects; | |||
5584 | ||||
5585 | /// The location of the left and right parentheses. | |||
5586 | SourceLocation LParenLoc, RParenLoc; | |||
5587 | ||||
5588 | /// Build a paren list. | |||
5589 | ParenListExpr(SourceLocation LParenLoc, ArrayRef<Expr *> Exprs, | |||
5590 | SourceLocation RParenLoc); | |||
5591 | ||||
5592 | /// Build an empty paren list. | |||
5593 | ParenListExpr(EmptyShell Empty, unsigned NumExprs); | |||
5594 | ||||
5595 | public: | |||
5596 | /// Create a paren list. | |||
5597 | static ParenListExpr *Create(const ASTContext &Ctx, SourceLocation LParenLoc, | |||
5598 | ArrayRef<Expr *> Exprs, | |||
5599 | SourceLocation RParenLoc); | |||
5600 | ||||
5601 | /// Create an empty paren list. | |||
5602 | static ParenListExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumExprs); | |||
5603 | ||||
5604 | /// Return the number of expressions in this paren list. | |||
5605 | unsigned getNumExprs() const { return ParenListExprBits.NumExprs; } | |||
5606 | ||||
5607 | Expr *getExpr(unsigned Init) { | |||
5608 | assert(Init < getNumExprs() && "Initializer access out of range!")(static_cast <bool> (Init < getNumExprs() && "Initializer access out of range!") ? void (0) : __assert_fail ("Init < getNumExprs() && \"Initializer access out of range!\"" , "clang/include/clang/AST/Expr.h", 5608, __extension__ __PRETTY_FUNCTION__ )); | |||
5609 | return getExprs()[Init]; | |||
5610 | } | |||
5611 | ||||
5612 | const Expr *getExpr(unsigned Init) const { | |||
5613 | return const_cast<ParenListExpr *>(this)->getExpr(Init); | |||
5614 | } | |||
5615 | ||||
5616 | Expr **getExprs() { | |||
5617 | return reinterpret_cast<Expr **>(getTrailingObjects<Stmt *>()); | |||
5618 | } | |||
5619 | ||||
5620 | ArrayRef<Expr *> exprs() { return llvm::ArrayRef(getExprs(), getNumExprs()); } | |||
5621 | ||||
5622 | SourceLocation getLParenLoc() const { return LParenLoc; } | |||
5623 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
5624 | SourceLocation getBeginLoc() const { return getLParenLoc(); } | |||
5625 | SourceLocation getEndLoc() const { return getRParenLoc(); } | |||
5626 | ||||
5627 | static bool classof(const Stmt *T) { | |||
5628 | return T->getStmtClass() == ParenListExprClass; | |||
5629 | } | |||
5630 | ||||
5631 | // Iterators | |||
5632 | child_range children() { | |||
5633 | return child_range(getTrailingObjects<Stmt *>(), | |||
5634 | getTrailingObjects<Stmt *>() + getNumExprs()); | |||
5635 | } | |||
5636 | const_child_range children() const { | |||
5637 | return const_child_range(getTrailingObjects<Stmt *>(), | |||
5638 | getTrailingObjects<Stmt *>() + getNumExprs()); | |||
5639 | } | |||
5640 | }; | |||
5641 | ||||
5642 | /// Represents a C11 generic selection. | |||
5643 | /// | |||
5644 | /// A generic selection (C11 6.5.1.1) contains an unevaluated controlling | |||
5645 | /// expression, followed by one or more generic associations. Each generic | |||
5646 | /// association specifies a type name and an expression, or "default" and an | |||
5647 | /// expression (in which case it is known as a default generic association). | |||
5648 | /// The type and value of the generic selection are identical to those of its | |||
5649 | /// result expression, which is defined as the expression in the generic | |||
5650 | /// association with a type name that is compatible with the type of the | |||
5651 | /// controlling expression, or the expression in the default generic association | |||
5652 | /// if no types are compatible. For example: | |||
5653 | /// | |||
5654 | /// @code | |||
5655 | /// _Generic(X, double: 1, float: 2, default: 3) | |||
5656 | /// @endcode | |||
5657 | /// | |||
5658 | /// The above expression evaluates to 1 if 1.0 is substituted for X, 2 if 1.0f | |||
5659 | /// or 3 if "hello". | |||
5660 | /// | |||
5661 | /// As an extension, generic selections are allowed in C++, where the following | |||
5662 | /// additional semantics apply: | |||
5663 | /// | |||
5664 | /// Any generic selection whose controlling expression is type-dependent or | |||
5665 | /// which names a dependent type in its association list is result-dependent, | |||
5666 | /// which means that the choice of result expression is dependent. | |||
5667 | /// Result-dependent generic associations are both type- and value-dependent. | |||
5668 | class GenericSelectionExpr final | |||
5669 | : public Expr, | |||
5670 | private llvm::TrailingObjects<GenericSelectionExpr, Stmt *, | |||
5671 | TypeSourceInfo *> { | |||
5672 | friend class ASTStmtReader; | |||
5673 | friend class ASTStmtWriter; | |||
5674 | friend TrailingObjects; | |||
5675 | ||||
5676 | /// The number of association expressions and the index of the result | |||
5677 | /// expression in the case where the generic selection expression is not | |||
5678 | /// result-dependent. The result index is equal to ResultDependentIndex | |||
5679 | /// if and only if the generic selection expression is result-dependent. | |||
5680 | unsigned NumAssocs, ResultIndex; | |||
5681 | enum : unsigned { | |||
5682 | ResultDependentIndex = std::numeric_limits<unsigned>::max(), | |||
5683 | ControllingIndex = 0, | |||
5684 | AssocExprStartIndex = 1 | |||
5685 | }; | |||
5686 | ||||
5687 | /// The location of the "default" and of the right parenthesis. | |||
5688 | SourceLocation DefaultLoc, RParenLoc; | |||
5689 | ||||
5690 | // GenericSelectionExpr is followed by several trailing objects. | |||
5691 | // They are (in order): | |||
5692 | // | |||
5693 | // * A single Stmt * for the controlling expression. | |||
5694 | // * An array of getNumAssocs() Stmt * for the association expressions. | |||
5695 | // * An array of getNumAssocs() TypeSourceInfo *, one for each of the | |||
5696 | // association expressions. | |||
5697 | unsigned numTrailingObjects(OverloadToken<Stmt *>) const { | |||
5698 | // Add one to account for the controlling expression; the remainder | |||
5699 | // are the associated expressions. | |||
5700 | return 1 + getNumAssocs(); | |||
5701 | } | |||
5702 | ||||
5703 | unsigned numTrailingObjects(OverloadToken<TypeSourceInfo *>) const { | |||
5704 | return getNumAssocs(); | |||
5705 | } | |||
5706 | ||||
5707 | template <bool Const> class AssociationIteratorTy; | |||
5708 | /// Bundle together an association expression and its TypeSourceInfo. | |||
5709 | /// The Const template parameter is for the const and non-const versions | |||
5710 | /// of AssociationTy. | |||
5711 | template <bool Const> class AssociationTy { | |||
5712 | friend class GenericSelectionExpr; | |||
5713 | template <bool OtherConst> friend class AssociationIteratorTy; | |||
5714 | using ExprPtrTy = std::conditional_t<Const, const Expr *, Expr *>; | |||
5715 | using TSIPtrTy = | |||
5716 | std::conditional_t<Const, const TypeSourceInfo *, TypeSourceInfo *>; | |||
5717 | ExprPtrTy E; | |||
5718 | TSIPtrTy TSI; | |||
5719 | bool Selected; | |||
5720 | AssociationTy(ExprPtrTy E, TSIPtrTy TSI, bool Selected) | |||
5721 | : E(E), TSI(TSI), Selected(Selected) {} | |||
5722 | ||||
5723 | public: | |||
5724 | ExprPtrTy getAssociationExpr() const { return E; } | |||
5725 | TSIPtrTy getTypeSourceInfo() const { return TSI; } | |||
5726 | QualType getType() const { return TSI ? TSI->getType() : QualType(); } | |||
5727 | bool isSelected() const { return Selected; } | |||
5728 | AssociationTy *operator->() { return this; } | |||
5729 | const AssociationTy *operator->() const { return this; } | |||
5730 | }; // class AssociationTy | |||
5731 | ||||
5732 | /// Iterator over const and non-const Association objects. The Association | |||
5733 | /// objects are created on the fly when the iterator is dereferenced. | |||
5734 | /// This abstract over how exactly the association expressions and the | |||
5735 | /// corresponding TypeSourceInfo * are stored. | |||
5736 | template <bool Const> | |||
5737 | class AssociationIteratorTy | |||
5738 | : public llvm::iterator_facade_base< | |||
5739 | AssociationIteratorTy<Const>, std::input_iterator_tag, | |||
5740 | AssociationTy<Const>, std::ptrdiff_t, AssociationTy<Const>, | |||
5741 | AssociationTy<Const>> { | |||
5742 | friend class GenericSelectionExpr; | |||
5743 | // FIXME: This iterator could conceptually be a random access iterator, and | |||
5744 | // it would be nice if we could strengthen the iterator category someday. | |||
5745 | // However this iterator does not satisfy two requirements of forward | |||
5746 | // iterators: | |||
5747 | // a) reference = T& or reference = const T& | |||
5748 | // b) If It1 and It2 are both dereferenceable, then It1 == It2 if and only | |||
5749 | // if *It1 and *It2 are bound to the same objects. | |||
5750 | // An alternative design approach was discussed during review; | |||
5751 | // store an Association object inside the iterator, and return a reference | |||
5752 | // to it when dereferenced. This idea was discarded beacuse of nasty | |||
5753 | // lifetime issues: | |||
5754 | // AssociationIterator It = ...; | |||
5755 | // const Association &Assoc = *It++; // Oops, Assoc is dangling. | |||
5756 | using BaseTy = typename AssociationIteratorTy::iterator_facade_base; | |||
5757 | using StmtPtrPtrTy = | |||
5758 | std::conditional_t<Const, const Stmt *const *, Stmt **>; | |||
5759 | using TSIPtrPtrTy = std::conditional_t<Const, const TypeSourceInfo *const *, | |||
5760 | TypeSourceInfo **>; | |||
5761 | StmtPtrPtrTy E; // = nullptr; FIXME: Once support for gcc 4.8 is dropped. | |||
5762 | TSIPtrPtrTy TSI; // Kept in sync with E. | |||
5763 | unsigned Offset = 0, SelectedOffset = 0; | |||
5764 | AssociationIteratorTy(StmtPtrPtrTy E, TSIPtrPtrTy TSI, unsigned Offset, | |||
5765 | unsigned SelectedOffset) | |||
5766 | : E(E), TSI(TSI), Offset(Offset), SelectedOffset(SelectedOffset) {} | |||
5767 | ||||
5768 | public: | |||
5769 | AssociationIteratorTy() : E(nullptr), TSI(nullptr) {} | |||
5770 | typename BaseTy::reference operator*() const { | |||
5771 | return AssociationTy<Const>(cast<Expr>(*E), *TSI, | |||
5772 | Offset == SelectedOffset); | |||
5773 | } | |||
5774 | typename BaseTy::pointer operator->() const { return **this; } | |||
5775 | using BaseTy::operator++; | |||
5776 | AssociationIteratorTy &operator++() { | |||
5777 | ++E; | |||
5778 | ++TSI; | |||
5779 | ++Offset; | |||
5780 | return *this; | |||
5781 | } | |||
5782 | bool operator==(AssociationIteratorTy Other) const { return E == Other.E; } | |||
5783 | }; // class AssociationIterator | |||
5784 | ||||
5785 | /// Build a non-result-dependent generic selection expression. | |||
5786 | GenericSelectionExpr(const ASTContext &Context, SourceLocation GenericLoc, | |||
5787 | Expr *ControllingExpr, | |||
5788 | ArrayRef<TypeSourceInfo *> AssocTypes, | |||
5789 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, | |||
5790 | SourceLocation RParenLoc, | |||
5791 | bool ContainsUnexpandedParameterPack, | |||
5792 | unsigned ResultIndex); | |||
5793 | ||||
5794 | /// Build a result-dependent generic selection expression. | |||
5795 | GenericSelectionExpr(const ASTContext &Context, SourceLocation GenericLoc, | |||
5796 | Expr *ControllingExpr, | |||
5797 | ArrayRef<TypeSourceInfo *> AssocTypes, | |||
5798 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, | |||
5799 | SourceLocation RParenLoc, | |||
5800 | bool ContainsUnexpandedParameterPack); | |||
5801 | ||||
5802 | /// Build an empty generic selection expression for deserialization. | |||
5803 | explicit GenericSelectionExpr(EmptyShell Empty, unsigned NumAssocs); | |||
5804 | ||||
5805 | public: | |||
5806 | /// Create a non-result-dependent generic selection expression. | |||
5807 | static GenericSelectionExpr * | |||
5808 | Create(const ASTContext &Context, SourceLocation GenericLoc, | |||
5809 | Expr *ControllingExpr, ArrayRef<TypeSourceInfo *> AssocTypes, | |||
5810 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, | |||
5811 | SourceLocation RParenLoc, bool ContainsUnexpandedParameterPack, | |||
5812 | unsigned ResultIndex); | |||
5813 | ||||
5814 | /// Create a result-dependent generic selection expression. | |||
5815 | static GenericSelectionExpr * | |||
5816 | Create(const ASTContext &Context, SourceLocation GenericLoc, | |||
5817 | Expr *ControllingExpr, ArrayRef<TypeSourceInfo *> AssocTypes, | |||
5818 | ArrayRef<Expr *> AssocExprs, SourceLocation DefaultLoc, | |||
5819 | SourceLocation RParenLoc, bool ContainsUnexpandedParameterPack); | |||
5820 | ||||
5821 | /// Create an empty generic selection expression for deserialization. | |||
5822 | static GenericSelectionExpr *CreateEmpty(const ASTContext &Context, | |||
5823 | unsigned NumAssocs); | |||
5824 | ||||
5825 | using Association = AssociationTy<false>; | |||
5826 | using ConstAssociation = AssociationTy<true>; | |||
5827 | using AssociationIterator = AssociationIteratorTy<false>; | |||
5828 | using ConstAssociationIterator = AssociationIteratorTy<true>; | |||
5829 | using association_range = llvm::iterator_range<AssociationIterator>; | |||
5830 | using const_association_range = | |||
5831 | llvm::iterator_range<ConstAssociationIterator>; | |||
5832 | ||||
5833 | /// The number of association expressions. | |||
5834 | unsigned getNumAssocs() const { return NumAssocs; } | |||
5835 | ||||
5836 | /// The zero-based index of the result expression's generic association in | |||
5837 | /// the generic selection's association list. Defined only if the | |||
5838 | /// generic selection is not result-dependent. | |||
5839 | unsigned getResultIndex() const { | |||
5840 | assert(!isResultDependent() &&(static_cast <bool> (!isResultDependent() && "Generic selection is result-dependent but getResultIndex called!" ) ? void (0) : __assert_fail ("!isResultDependent() && \"Generic selection is result-dependent but getResultIndex called!\"" , "clang/include/clang/AST/Expr.h", 5841, __extension__ __PRETTY_FUNCTION__ )) | |||
5841 | "Generic selection is result-dependent but getResultIndex called!")(static_cast <bool> (!isResultDependent() && "Generic selection is result-dependent but getResultIndex called!" ) ? void (0) : __assert_fail ("!isResultDependent() && \"Generic selection is result-dependent but getResultIndex called!\"" , "clang/include/clang/AST/Expr.h", 5841, __extension__ __PRETTY_FUNCTION__ )); | |||
5842 | return ResultIndex; | |||
5843 | } | |||
5844 | ||||
5845 | /// Whether this generic selection is result-dependent. | |||
5846 | bool isResultDependent() const { return ResultIndex == ResultDependentIndex; } | |||
5847 | ||||
5848 | /// Return the controlling expression of this generic selection expression. | |||
5849 | Expr *getControllingExpr() { | |||
5850 | return cast<Expr>(getTrailingObjects<Stmt *>()[ControllingIndex]); | |||
5851 | } | |||
5852 | const Expr *getControllingExpr() const { | |||
5853 | return cast<Expr>(getTrailingObjects<Stmt *>()[ControllingIndex]); | |||
5854 | } | |||
5855 | ||||
5856 | /// Return the result expression of this controlling expression. Defined if | |||
5857 | /// and only if the generic selection expression is not result-dependent. | |||
5858 | Expr *getResultExpr() { | |||
5859 | return cast<Expr>( | |||
5860 | getTrailingObjects<Stmt *>()[AssocExprStartIndex + getResultIndex()]); | |||
5861 | } | |||
5862 | const Expr *getResultExpr() const { | |||
5863 | return cast<Expr>( | |||
5864 | getTrailingObjects<Stmt *>()[AssocExprStartIndex + getResultIndex()]); | |||
5865 | } | |||
5866 | ||||
5867 | ArrayRef<Expr *> getAssocExprs() const { | |||
5868 | return {reinterpret_cast<Expr *const *>(getTrailingObjects<Stmt *>() + | |||
5869 | AssocExprStartIndex), | |||
5870 | NumAssocs}; | |||
5871 | } | |||
5872 | ArrayRef<TypeSourceInfo *> getAssocTypeSourceInfos() const { | |||
5873 | return {getTrailingObjects<TypeSourceInfo *>(), NumAssocs}; | |||
5874 | } | |||
5875 | ||||
5876 | /// Return the Ith association expression with its TypeSourceInfo, | |||
5877 | /// bundled together in GenericSelectionExpr::(Const)Association. | |||
5878 | Association getAssociation(unsigned I) { | |||
5879 | assert(I < getNumAssocs() &&(static_cast <bool> (I < getNumAssocs() && "Out-of-range index in GenericSelectionExpr::getAssociation!" ) ? void (0) : __assert_fail ("I < getNumAssocs() && \"Out-of-range index in GenericSelectionExpr::getAssociation!\"" , "clang/include/clang/AST/Expr.h", 5880, __extension__ __PRETTY_FUNCTION__ )) | |||
5880 | "Out-of-range index in GenericSelectionExpr::getAssociation!")(static_cast <bool> (I < getNumAssocs() && "Out-of-range index in GenericSelectionExpr::getAssociation!" ) ? void (0) : __assert_fail ("I < getNumAssocs() && \"Out-of-range index in GenericSelectionExpr::getAssociation!\"" , "clang/include/clang/AST/Expr.h", 5880, __extension__ __PRETTY_FUNCTION__ )); | |||
5881 | return Association( | |||
5882 | cast<Expr>(getTrailingObjects<Stmt *>()[AssocExprStartIndex + I]), | |||
5883 | getTrailingObjects<TypeSourceInfo *>()[I], | |||
5884 | !isResultDependent() && (getResultIndex() == I)); | |||
5885 | } | |||
5886 | ConstAssociation getAssociation(unsigned I) const { | |||
5887 | assert(I < getNumAssocs() &&(static_cast <bool> (I < getNumAssocs() && "Out-of-range index in GenericSelectionExpr::getAssociation!" ) ? void (0) : __assert_fail ("I < getNumAssocs() && \"Out-of-range index in GenericSelectionExpr::getAssociation!\"" , "clang/include/clang/AST/Expr.h", 5888, __extension__ __PRETTY_FUNCTION__ )) | |||
5888 | "Out-of-range index in GenericSelectionExpr::getAssociation!")(static_cast <bool> (I < getNumAssocs() && "Out-of-range index in GenericSelectionExpr::getAssociation!" ) ? void (0) : __assert_fail ("I < getNumAssocs() && \"Out-of-range index in GenericSelectionExpr::getAssociation!\"" , "clang/include/clang/AST/Expr.h", 5888, __extension__ __PRETTY_FUNCTION__ )); | |||
5889 | return ConstAssociation( | |||
5890 | cast<Expr>(getTrailingObjects<Stmt *>()[AssocExprStartIndex + I]), | |||
5891 | getTrailingObjects<TypeSourceInfo *>()[I], | |||
5892 | !isResultDependent() && (getResultIndex() == I)); | |||
5893 | } | |||
5894 | ||||
5895 | association_range associations() { | |||
5896 | AssociationIterator Begin(getTrailingObjects<Stmt *>() + | |||
5897 | AssocExprStartIndex, | |||
5898 | getTrailingObjects<TypeSourceInfo *>(), | |||
5899 | /*Offset=*/0, ResultIndex); | |||
5900 | AssociationIterator End(Begin.E + NumAssocs, Begin.TSI + NumAssocs, | |||
5901 | /*Offset=*/NumAssocs, ResultIndex); | |||
5902 | return llvm::make_range(Begin, End); | |||
5903 | } | |||
5904 | ||||
5905 | const_association_range associations() const { | |||
5906 | ConstAssociationIterator Begin(getTrailingObjects<Stmt *>() + | |||
5907 | AssocExprStartIndex, | |||
5908 | getTrailingObjects<TypeSourceInfo *>(), | |||
5909 | /*Offset=*/0, ResultIndex); | |||
5910 | ConstAssociationIterator End(Begin.E + NumAssocs, Begin.TSI + NumAssocs, | |||
5911 | /*Offset=*/NumAssocs, ResultIndex); | |||
5912 | return llvm::make_range(Begin, End); | |||
5913 | } | |||
5914 | ||||
5915 | SourceLocation getGenericLoc() const { | |||
5916 | return GenericSelectionExprBits.GenericLoc; | |||
5917 | } | |||
5918 | SourceLocation getDefaultLoc() const { return DefaultLoc; } | |||
5919 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
5920 | SourceLocation getBeginLoc() const { return getGenericLoc(); } | |||
5921 | SourceLocation getEndLoc() const { return getRParenLoc(); } | |||
5922 | ||||
5923 | static bool classof(const Stmt *T) { | |||
5924 | return T->getStmtClass() == GenericSelectionExprClass; | |||
5925 | } | |||
5926 | ||||
5927 | child_range children() { | |||
5928 | return child_range(getTrailingObjects<Stmt *>(), | |||
5929 | getTrailingObjects<Stmt *>() + | |||
5930 | numTrailingObjects(OverloadToken<Stmt *>())); | |||
5931 | } | |||
5932 | const_child_range children() const { | |||
5933 | return const_child_range(getTrailingObjects<Stmt *>(), | |||
5934 | getTrailingObjects<Stmt *>() + | |||
5935 | numTrailingObjects(OverloadToken<Stmt *>())); | |||
5936 | } | |||
5937 | }; | |||
5938 | ||||
5939 | //===----------------------------------------------------------------------===// | |||
5940 | // Clang Extensions | |||
5941 | //===----------------------------------------------------------------------===// | |||
5942 | ||||
5943 | /// ExtVectorElementExpr - This represents access to specific elements of a | |||
5944 | /// vector, and may occur on the left hand side or right hand side. For example | |||
5945 | /// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector. | |||
5946 | /// | |||
5947 | /// Note that the base may have either vector or pointer to vector type, just | |||
5948 | /// like a struct field reference. | |||
5949 | /// | |||
5950 | class ExtVectorElementExpr : public Expr { | |||
5951 | Stmt *Base; | |||
5952 | IdentifierInfo *Accessor; | |||
5953 | SourceLocation AccessorLoc; | |||
5954 | public: | |||
5955 | ExtVectorElementExpr(QualType ty, ExprValueKind VK, Expr *base, | |||
5956 | IdentifierInfo &accessor, SourceLocation loc) | |||
5957 | : Expr(ExtVectorElementExprClass, ty, VK, | |||
5958 | (VK == VK_PRValue ? OK_Ordinary : OK_VectorComponent)), | |||
5959 | Base(base), Accessor(&accessor), AccessorLoc(loc) { | |||
5960 | setDependence(computeDependence(this)); | |||
5961 | } | |||
5962 | ||||
5963 | /// Build an empty vector element expression. | |||
5964 | explicit ExtVectorElementExpr(EmptyShell Empty) | |||
5965 | : Expr(ExtVectorElementExprClass, Empty) { } | |||
5966 | ||||
5967 | const Expr *getBase() const { return cast<Expr>(Base); } | |||
5968 | Expr *getBase() { return cast<Expr>(Base); } | |||
5969 | void setBase(Expr *E) { Base = E; } | |||
5970 | ||||
5971 | IdentifierInfo &getAccessor() const { return *Accessor; } | |||
5972 | void setAccessor(IdentifierInfo *II) { Accessor = II; } | |||
5973 | ||||
5974 | SourceLocation getAccessorLoc() const { return AccessorLoc; } | |||
5975 | void setAccessorLoc(SourceLocation L) { AccessorLoc = L; } | |||
5976 | ||||
5977 | /// getNumElements - Get the number of components being selected. | |||
5978 | unsigned getNumElements() const; | |||
5979 | ||||
5980 | /// containsDuplicateElements - Return true if any element access is | |||
5981 | /// repeated. | |||
5982 | bool containsDuplicateElements() const; | |||
5983 | ||||
5984 | /// getEncodedElementAccess - Encode the elements accessed into an llvm | |||
5985 | /// aggregate Constant of ConstantInt(s). | |||
5986 | void getEncodedElementAccess(SmallVectorImpl<uint32_t> &Elts) const; | |||
5987 | ||||
5988 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
5989 | return getBase()->getBeginLoc(); | |||
5990 | } | |||
5991 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return AccessorLoc; } | |||
5992 | ||||
5993 | /// isArrow - Return true if the base expression is a pointer to vector, | |||
5994 | /// return false if the base expression is a vector. | |||
5995 | bool isArrow() const; | |||
5996 | ||||
5997 | static bool classof(const Stmt *T) { | |||
5998 | return T->getStmtClass() == ExtVectorElementExprClass; | |||
5999 | } | |||
6000 | ||||
6001 | // Iterators | |||
6002 | child_range children() { return child_range(&Base, &Base+1); } | |||
6003 | const_child_range children() const { | |||
6004 | return const_child_range(&Base, &Base + 1); | |||
6005 | } | |||
6006 | }; | |||
6007 | ||||
6008 | /// BlockExpr - Adaptor class for mixing a BlockDecl with expressions. | |||
6009 | /// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } | |||
6010 | class BlockExpr : public Expr { | |||
6011 | protected: | |||
6012 | BlockDecl *TheBlock; | |||
6013 | public: | |||
6014 | BlockExpr(BlockDecl *BD, QualType ty) | |||
6015 | : Expr(BlockExprClass, ty, VK_PRValue, OK_Ordinary), TheBlock(BD) { | |||
6016 | setDependence(computeDependence(this)); | |||
6017 | } | |||
6018 | ||||
6019 | /// Build an empty block expression. | |||
6020 | explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { } | |||
6021 | ||||
6022 | const BlockDecl *getBlockDecl() const { return TheBlock; } | |||
6023 | BlockDecl *getBlockDecl() { return TheBlock; } | |||
6024 | void setBlockDecl(BlockDecl *BD) { TheBlock = BD; } | |||
6025 | ||||
6026 | // Convenience functions for probing the underlying BlockDecl. | |||
6027 | SourceLocation getCaretLocation() const; | |||
6028 | const Stmt *getBody() const; | |||
6029 | Stmt *getBody(); | |||
6030 | ||||
6031 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
6032 | return getCaretLocation(); | |||
6033 | } | |||
6034 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
6035 | return getBody()->getEndLoc(); | |||
6036 | } | |||
6037 | ||||
6038 | /// getFunctionType - Return the underlying function type for this block. | |||
6039 | const FunctionProtoType *getFunctionType() const; | |||
6040 | ||||
6041 | static bool classof(const Stmt *T) { | |||
6042 | return T->getStmtClass() == BlockExprClass; | |||
6043 | } | |||
6044 | ||||
6045 | // Iterators | |||
6046 | child_range children() { | |||
6047 | return child_range(child_iterator(), child_iterator()); | |||
6048 | } | |||
6049 | const_child_range children() const { | |||
6050 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
6051 | } | |||
6052 | }; | |||
6053 | ||||
6054 | /// Copy initialization expr of a __block variable and a boolean flag that | |||
6055 | /// indicates whether the expression can throw. | |||
6056 | struct BlockVarCopyInit { | |||
6057 | BlockVarCopyInit() = default; | |||
6058 | BlockVarCopyInit(Expr *CopyExpr, bool CanThrow) | |||
6059 | : ExprAndFlag(CopyExpr, CanThrow) {} | |||
6060 | void setExprAndFlag(Expr *CopyExpr, bool CanThrow) { | |||
6061 | ExprAndFlag.setPointerAndInt(CopyExpr, CanThrow); | |||
6062 | } | |||
6063 | Expr *getCopyExpr() const { return ExprAndFlag.getPointer(); } | |||
6064 | bool canThrow() const { return ExprAndFlag.getInt(); } | |||
6065 | llvm::PointerIntPair<Expr *, 1, bool> ExprAndFlag; | |||
6066 | }; | |||
6067 | ||||
6068 | /// AsTypeExpr - Clang builtin function __builtin_astype [OpenCL 6.2.4.2] | |||
6069 | /// This AST node provides support for reinterpreting a type to another | |||
6070 | /// type of the same size. | |||
6071 | class AsTypeExpr : public Expr { | |||
6072 | private: | |||
6073 | Stmt *SrcExpr; | |||
6074 | SourceLocation BuiltinLoc, RParenLoc; | |||
6075 | ||||
6076 | friend class ASTReader; | |||
6077 | friend class ASTStmtReader; | |||
6078 | explicit AsTypeExpr(EmptyShell Empty) : Expr(AsTypeExprClass, Empty) {} | |||
6079 | ||||
6080 | public: | |||
6081 | AsTypeExpr(Expr *SrcExpr, QualType DstType, ExprValueKind VK, | |||
6082 | ExprObjectKind OK, SourceLocation BuiltinLoc, | |||
6083 | SourceLocation RParenLoc) | |||
6084 | : Expr(AsTypeExprClass, DstType, VK, OK), SrcExpr(SrcExpr), | |||
6085 | BuiltinLoc(BuiltinLoc), RParenLoc(RParenLoc) { | |||
6086 | setDependence(computeDependence(this)); | |||
6087 | } | |||
6088 | ||||
6089 | /// getSrcExpr - Return the Expr to be converted. | |||
6090 | Expr *getSrcExpr() const { return cast<Expr>(SrcExpr); } | |||
6091 | ||||
6092 | /// getBuiltinLoc - Return the location of the __builtin_astype token. | |||
6093 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } | |||
6094 | ||||
6095 | /// getRParenLoc - Return the location of final right parenthesis. | |||
6096 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
6097 | ||||
6098 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return BuiltinLoc; } | |||
6099 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return RParenLoc; } | |||
6100 | ||||
6101 | static bool classof(const Stmt *T) { | |||
6102 | return T->getStmtClass() == AsTypeExprClass; | |||
6103 | } | |||
6104 | ||||
6105 | // Iterators | |||
6106 | child_range children() { return child_range(&SrcExpr, &SrcExpr+1); } | |||
6107 | const_child_range children() const { | |||
6108 | return const_child_range(&SrcExpr, &SrcExpr + 1); | |||
6109 | } | |||
6110 | }; | |||
6111 | ||||
6112 | /// PseudoObjectExpr - An expression which accesses a pseudo-object | |||
6113 | /// l-value. A pseudo-object is an abstract object, accesses to which | |||
6114 | /// are translated to calls. The pseudo-object expression has a | |||
6115 | /// syntactic form, which shows how the expression was actually | |||
6116 | /// written in the source code, and a semantic form, which is a series | |||
6117 | /// of expressions to be executed in order which detail how the | |||
6118 | /// operation is actually evaluated. Optionally, one of the semantic | |||
6119 | /// forms may also provide a result value for the expression. | |||
6120 | /// | |||
6121 | /// If any of the semantic-form expressions is an OpaqueValueExpr, | |||
6122 | /// that OVE is required to have a source expression, and it is bound | |||
6123 | /// to the result of that source expression. Such OVEs may appear | |||
6124 | /// only in subsequent semantic-form expressions and as | |||
6125 | /// sub-expressions of the syntactic form. | |||
6126 | /// | |||
6127 | /// PseudoObjectExpr should be used only when an operation can be | |||
6128 | /// usefully described in terms of fairly simple rewrite rules on | |||
6129 | /// objects and functions that are meant to be used by end-developers. | |||
6130 | /// For example, under the Itanium ABI, dynamic casts are implemented | |||
6131 | /// as a call to a runtime function called __dynamic_cast; using this | |||
6132 | /// class to describe that would be inappropriate because that call is | |||
6133 | /// not really part of the user-visible semantics, and instead the | |||
6134 | /// cast is properly reflected in the AST and IR-generation has been | |||
6135 | /// taught to generate the call as necessary. In contrast, an | |||
6136 | /// Objective-C property access is semantically defined to be | |||
6137 | /// equivalent to a particular message send, and this is very much | |||
6138 | /// part of the user model. The name of this class encourages this | |||
6139 | /// modelling design. | |||
6140 | class PseudoObjectExpr final | |||
6141 | : public Expr, | |||
6142 | private llvm::TrailingObjects<PseudoObjectExpr, Expr *> { | |||
6143 | // PseudoObjectExprBits.NumSubExprs - The number of sub-expressions. | |||
6144 | // Always at least two, because the first sub-expression is the | |||
6145 | // syntactic form. | |||
6146 | ||||
6147 | // PseudoObjectExprBits.ResultIndex - The index of the | |||
6148 | // sub-expression holding the result. 0 means the result is void, | |||
6149 | // which is unambiguous because it's the index of the syntactic | |||
6150 | // form. Note that this is therefore 1 higher than the value passed | |||
6151 | // in to Create, which is an index within the semantic forms. | |||
6152 | // Note also that ASTStmtWriter assumes this encoding. | |||
6153 | ||||
6154 | Expr **getSubExprsBuffer() { return getTrailingObjects<Expr *>(); } | |||
6155 | const Expr * const *getSubExprsBuffer() const { | |||
6156 | return getTrailingObjects<Expr *>(); | |||
6157 | } | |||
6158 | ||||
6159 | PseudoObjectExpr(QualType type, ExprValueKind VK, | |||
6160 | Expr *syntactic, ArrayRef<Expr*> semantic, | |||
6161 | unsigned resultIndex); | |||
6162 | ||||
6163 | PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs); | |||
6164 | ||||
6165 | unsigned getNumSubExprs() const { | |||
6166 | return PseudoObjectExprBits.NumSubExprs; | |||
6167 | } | |||
6168 | ||||
6169 | public: | |||
6170 | /// NoResult - A value for the result index indicating that there is | |||
6171 | /// no semantic result. | |||
6172 | enum : unsigned { NoResult = ~0U }; | |||
6173 | ||||
6174 | static PseudoObjectExpr *Create(const ASTContext &Context, Expr *syntactic, | |||
6175 | ArrayRef<Expr*> semantic, | |||
6176 | unsigned resultIndex); | |||
6177 | ||||
6178 | static PseudoObjectExpr *Create(const ASTContext &Context, EmptyShell shell, | |||
6179 | unsigned numSemanticExprs); | |||
6180 | ||||
6181 | /// Return the syntactic form of this expression, i.e. the | |||
6182 | /// expression it actually looks like. Likely to be expressed in | |||
6183 | /// terms of OpaqueValueExprs bound in the semantic form. | |||
6184 | Expr *getSyntacticForm() { return getSubExprsBuffer()[0]; } | |||
6185 | const Expr *getSyntacticForm() const { return getSubExprsBuffer()[0]; } | |||
6186 | ||||
6187 | /// Return the index of the result-bearing expression into the semantics | |||
6188 | /// expressions, or PseudoObjectExpr::NoResult if there is none. | |||
6189 | unsigned getResultExprIndex() const { | |||
6190 | if (PseudoObjectExprBits.ResultIndex == 0) return NoResult; | |||
6191 | return PseudoObjectExprBits.ResultIndex - 1; | |||
6192 | } | |||
6193 | ||||
6194 | /// Return the result-bearing expression, or null if there is none. | |||
6195 | Expr *getResultExpr() { | |||
6196 | if (PseudoObjectExprBits.ResultIndex == 0) | |||
6197 | return nullptr; | |||
6198 | return getSubExprsBuffer()[PseudoObjectExprBits.ResultIndex]; | |||
6199 | } | |||
6200 | const Expr *getResultExpr() const { | |||
6201 | return const_cast<PseudoObjectExpr*>(this)->getResultExpr(); | |||
6202 | } | |||
6203 | ||||
6204 | unsigned getNumSemanticExprs() const { return getNumSubExprs() - 1; } | |||
6205 | ||||
6206 | typedef Expr * const *semantics_iterator; | |||
6207 | typedef const Expr * const *const_semantics_iterator; | |||
6208 | semantics_iterator semantics_begin() { | |||
6209 | return getSubExprsBuffer() + 1; | |||
6210 | } | |||
6211 | const_semantics_iterator semantics_begin() const { | |||
6212 | return getSubExprsBuffer() + 1; | |||
6213 | } | |||
6214 | semantics_iterator semantics_end() { | |||
6215 | return getSubExprsBuffer() + getNumSubExprs(); | |||
6216 | } | |||
6217 | const_semantics_iterator semantics_end() const { | |||
6218 | return getSubExprsBuffer() + getNumSubExprs(); | |||
6219 | } | |||
6220 | ||||
6221 | llvm::iterator_range<semantics_iterator> semantics() { | |||
6222 | return llvm::make_range(semantics_begin(), semantics_end()); | |||
6223 | } | |||
6224 | llvm::iterator_range<const_semantics_iterator> semantics() const { | |||
6225 | return llvm::make_range(semantics_begin(), semantics_end()); | |||
6226 | } | |||
6227 | ||||
6228 | Expr *getSemanticExpr(unsigned index) { | |||
6229 | assert(index + 1 < getNumSubExprs())(static_cast <bool> (index + 1 < getNumSubExprs()) ? void (0) : __assert_fail ("index + 1 < getNumSubExprs()", "clang/include/clang/AST/Expr.h", 6229, __extension__ __PRETTY_FUNCTION__ )); | |||
6230 | return getSubExprsBuffer()[index + 1]; | |||
6231 | } | |||
6232 | const Expr *getSemanticExpr(unsigned index) const { | |||
6233 | return const_cast<PseudoObjectExpr*>(this)->getSemanticExpr(index); | |||
6234 | } | |||
6235 | ||||
6236 | SourceLocation getExprLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
6237 | return getSyntacticForm()->getExprLoc(); | |||
6238 | } | |||
6239 | ||||
6240 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
6241 | return getSyntacticForm()->getBeginLoc(); | |||
6242 | } | |||
6243 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { | |||
6244 | return getSyntacticForm()->getEndLoc(); | |||
6245 | } | |||
6246 | ||||
6247 | child_range children() { | |||
6248 | const_child_range CCR = | |||
6249 | const_cast<const PseudoObjectExpr *>(this)->children(); | |||
6250 | return child_range(cast_away_const(CCR.begin()), | |||
6251 | cast_away_const(CCR.end())); | |||
6252 | } | |||
6253 | const_child_range children() const { | |||
6254 | Stmt *const *cs = const_cast<Stmt *const *>( | |||
6255 | reinterpret_cast<const Stmt *const *>(getSubExprsBuffer())); | |||
6256 | return const_child_range(cs, cs + getNumSubExprs()); | |||
6257 | } | |||
6258 | ||||
6259 | static bool classof(const Stmt *T) { | |||
6260 | return T->getStmtClass() == PseudoObjectExprClass; | |||
6261 | } | |||
6262 | ||||
6263 | friend TrailingObjects; | |||
6264 | friend class ASTStmtReader; | |||
6265 | }; | |||
6266 | ||||
6267 | /// AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, | |||
6268 | /// __atomic_load, __atomic_store, and __atomic_compare_exchange_*, for the | |||
6269 | /// similarly-named C++11 instructions, and __c11 variants for <stdatomic.h>, | |||
6270 | /// and corresponding __opencl_atomic_* for OpenCL 2.0. | |||
6271 | /// All of these instructions take one primary pointer, at least one memory | |||
6272 | /// order. The instructions for which getScopeModel returns non-null value | |||
6273 | /// take one synch scope. | |||
6274 | class AtomicExpr : public Expr { | |||
6275 | public: | |||
6276 | enum AtomicOp { | |||
6277 | #define BUILTIN(ID, TYPE, ATTRS) | |||
6278 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) AO ## ID, | |||
6279 | #include "clang/Basic/Builtins.def" | |||
6280 | // Avoid trailing comma | |||
6281 | BI_First = 0 | |||
6282 | }; | |||
6283 | ||||
6284 | private: | |||
6285 | /// Location of sub-expressions. | |||
6286 | /// The location of Scope sub-expression is NumSubExprs - 1, which is | |||
6287 | /// not fixed, therefore is not defined in enum. | |||
6288 | enum { PTR, ORDER, VAL1, ORDER_FAIL, VAL2, WEAK, END_EXPR }; | |||
6289 | Stmt *SubExprs[END_EXPR + 1]; | |||
6290 | unsigned NumSubExprs; | |||
6291 | SourceLocation BuiltinLoc, RParenLoc; | |||
6292 | AtomicOp Op; | |||
6293 | ||||
6294 | friend class ASTStmtReader; | |||
6295 | public: | |||
6296 | AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args, QualType t, | |||
6297 | AtomicOp op, SourceLocation RP); | |||
6298 | ||||
6299 | /// Determine the number of arguments the specified atomic builtin | |||
6300 | /// should have. | |||
6301 | static unsigned getNumSubExprs(AtomicOp Op); | |||
6302 | ||||
6303 | /// Build an empty AtomicExpr. | |||
6304 | explicit AtomicExpr(EmptyShell Empty) : Expr(AtomicExprClass, Empty) { } | |||
6305 | ||||
6306 | Expr *getPtr() const { | |||
6307 | return cast<Expr>(SubExprs[PTR]); | |||
6308 | } | |||
6309 | Expr *getOrder() const { | |||
6310 | return cast<Expr>(SubExprs[ORDER]); | |||
6311 | } | |||
6312 | Expr *getScope() const { | |||
6313 | assert(getScopeModel() && "No scope")(static_cast <bool> (getScopeModel() && "No scope" ) ? void (0) : __assert_fail ("getScopeModel() && \"No scope\"" , "clang/include/clang/AST/Expr.h", 6313, __extension__ __PRETTY_FUNCTION__ )); | |||
6314 | return cast<Expr>(SubExprs[NumSubExprs - 1]); | |||
6315 | } | |||
6316 | Expr *getVal1() const { | |||
6317 | if (Op == AO__c11_atomic_init || Op == AO__opencl_atomic_init) | |||
6318 | return cast<Expr>(SubExprs[ORDER]); | |||
6319 | assert(NumSubExprs > VAL1)(static_cast <bool> (NumSubExprs > VAL1) ? void (0) : __assert_fail ("NumSubExprs > VAL1", "clang/include/clang/AST/Expr.h" , 6319, __extension__ __PRETTY_FUNCTION__)); | |||
6320 | return cast<Expr>(SubExprs[VAL1]); | |||
6321 | } | |||
6322 | Expr *getOrderFail() const { | |||
6323 | assert(NumSubExprs > ORDER_FAIL)(static_cast <bool> (NumSubExprs > ORDER_FAIL) ? void (0) : __assert_fail ("NumSubExprs > ORDER_FAIL", "clang/include/clang/AST/Expr.h" , 6323, __extension__ __PRETTY_FUNCTION__)); | |||
6324 | return cast<Expr>(SubExprs[ORDER_FAIL]); | |||
6325 | } | |||
6326 | Expr *getVal2() const { | |||
6327 | if (Op == AO__atomic_exchange) | |||
6328 | return cast<Expr>(SubExprs[ORDER_FAIL]); | |||
6329 | assert(NumSubExprs > VAL2)(static_cast <bool> (NumSubExprs > VAL2) ? void (0) : __assert_fail ("NumSubExprs > VAL2", "clang/include/clang/AST/Expr.h" , 6329, __extension__ __PRETTY_FUNCTION__)); | |||
6330 | return cast<Expr>(SubExprs[VAL2]); | |||
6331 | } | |||
6332 | Expr *getWeak() const { | |||
6333 | assert(NumSubExprs > WEAK)(static_cast <bool> (NumSubExprs > WEAK) ? void (0) : __assert_fail ("NumSubExprs > WEAK", "clang/include/clang/AST/Expr.h" , 6333, __extension__ __PRETTY_FUNCTION__)); | |||
6334 | return cast<Expr>(SubExprs[WEAK]); | |||
6335 | } | |||
6336 | QualType getValueType() const; | |||
6337 | ||||
6338 | AtomicOp getOp() const { return Op; } | |||
6339 | unsigned getNumSubExprs() const { return NumSubExprs; } | |||
6340 | ||||
6341 | Expr **getSubExprs() { return reinterpret_cast<Expr **>(SubExprs); } | |||
6342 | const Expr * const *getSubExprs() const { | |||
6343 | return reinterpret_cast<Expr * const *>(SubExprs); | |||
6344 | } | |||
6345 | ||||
6346 | bool isVolatile() const { | |||
6347 | return getPtr()->getType()->getPointeeType().isVolatileQualified(); | |||
6348 | } | |||
6349 | ||||
6350 | bool isCmpXChg() const { | |||
6351 | return getOp() == AO__c11_atomic_compare_exchange_strong || | |||
6352 | getOp() == AO__c11_atomic_compare_exchange_weak || | |||
6353 | getOp() == AO__hip_atomic_compare_exchange_strong || | |||
6354 | getOp() == AO__opencl_atomic_compare_exchange_strong || | |||
6355 | getOp() == AO__opencl_atomic_compare_exchange_weak || | |||
6356 | getOp() == AO__hip_atomic_compare_exchange_weak || | |||
6357 | getOp() == AO__atomic_compare_exchange || | |||
6358 | getOp() == AO__atomic_compare_exchange_n; | |||
6359 | } | |||
6360 | ||||
6361 | bool isOpenCL() const { | |||
6362 | return getOp() >= AO__opencl_atomic_init && | |||
6363 | getOp() <= AO__opencl_atomic_fetch_max; | |||
6364 | } | |||
6365 | ||||
6366 | SourceLocation getBuiltinLoc() const { return BuiltinLoc; } | |||
6367 | SourceLocation getRParenLoc() const { return RParenLoc; } | |||
6368 | ||||
6369 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return BuiltinLoc; } | |||
6370 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return RParenLoc; } | |||
6371 | ||||
6372 | static bool classof(const Stmt *T) { | |||
6373 | return T->getStmtClass() == AtomicExprClass; | |||
6374 | } | |||
6375 | ||||
6376 | // Iterators | |||
6377 | child_range children() { | |||
6378 | return child_range(SubExprs, SubExprs+NumSubExprs); | |||
6379 | } | |||
6380 | const_child_range children() const { | |||
6381 | return const_child_range(SubExprs, SubExprs + NumSubExprs); | |||
6382 | } | |||
6383 | ||||
6384 | /// Get atomic scope model for the atomic op code. | |||
6385 | /// \return empty atomic scope model if the atomic op code does not have | |||
6386 | /// scope operand. | |||
6387 | static std::unique_ptr<AtomicScopeModel> getScopeModel(AtomicOp Op) { | |||
6388 | auto Kind = | |||
6389 | (Op >= AO__opencl_atomic_load && Op <= AO__opencl_atomic_fetch_max) | |||
6390 | ? AtomicScopeModelKind::OpenCL | |||
6391 | : (Op >= AO__hip_atomic_load && Op <= AO__hip_atomic_fetch_max) | |||
6392 | ? AtomicScopeModelKind::HIP | |||
6393 | : AtomicScopeModelKind::None; | |||
6394 | return AtomicScopeModel::create(Kind); | |||
6395 | } | |||
6396 | ||||
6397 | /// Get atomic scope model. | |||
6398 | /// \return empty atomic scope model if this atomic expression does not have | |||
6399 | /// scope operand. | |||
6400 | std::unique_ptr<AtomicScopeModel> getScopeModel() const { | |||
6401 | return getScopeModel(getOp()); | |||
6402 | } | |||
6403 | }; | |||
6404 | ||||
6405 | /// TypoExpr - Internal placeholder for expressions where typo correction | |||
6406 | /// still needs to be performed and/or an error diagnostic emitted. | |||
6407 | class TypoExpr : public Expr { | |||
6408 | // The location for the typo name. | |||
6409 | SourceLocation TypoLoc; | |||
6410 | ||||
6411 | public: | |||
6412 | TypoExpr(QualType T, SourceLocation TypoLoc) | |||
6413 | : Expr(TypoExprClass, T, VK_LValue, OK_Ordinary), TypoLoc(TypoLoc) { | |||
6414 | assert(T->isDependentType() && "TypoExpr given a non-dependent type")(static_cast <bool> (T->isDependentType() && "TypoExpr given a non-dependent type") ? void (0) : __assert_fail ("T->isDependentType() && \"TypoExpr given a non-dependent type\"" , "clang/include/clang/AST/Expr.h", 6414, __extension__ __PRETTY_FUNCTION__ )); | |||
6415 | setDependence(ExprDependence::TypeValueInstantiation | | |||
6416 | ExprDependence::Error); | |||
6417 | } | |||
6418 | ||||
6419 | child_range children() { | |||
6420 | return child_range(child_iterator(), child_iterator()); | |||
6421 | } | |||
6422 | const_child_range children() const { | |||
6423 | return const_child_range(const_child_iterator(), const_child_iterator()); | |||
6424 | } | |||
6425 | ||||
6426 | SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return TypoLoc; } | |||
6427 | SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) { return TypoLoc; } | |||
6428 | ||||
6429 | static bool classof(const Stmt *T) { | |||
6430 | return T->getStmtClass() == TypoExprClass; | |||
6431 | } | |||
6432 | ||||
6433 | }; | |||
6434 | ||||
6435 | /// Frontend produces RecoveryExprs on semantic errors that prevent creating | |||
6436 | /// other well-formed expressions. E.g. when type-checking of a binary operator | |||
6437 | /// fails, we cannot produce a BinaryOperator expression. Instead, we can choose | |||
6438 | /// to produce a recovery expression storing left and right operands. | |||
6439 | /// | |||
6440 | /// RecoveryExpr does not have any semantic meaning in C++, it is only useful to | |||
6441 | /// preserve expressions in AST that would otherwise be dropped. It captures | |||
6442 | /// subexpressions of some expression that we could not construct and source | |||
6443 | /// range covered by the expression. | |||
6444 | /// | |||
6445 | /// By default, RecoveryExpr uses dependence-bits to take advantage of existing | |||
6446 | /// machinery to deal with dependent code in C++, e.g. RecoveryExpr is preserved | |||
6447 | /// in `decltype(<broken-expr>)` as part of the `DependentDecltypeType`. In | |||
6448 | /// addition to that, clang does not report most errors on dependent | |||
6449 | /// expressions, so we get rid of bogus errors for free. However, note that | |||
6450 | /// unlike other dependent expressions, RecoveryExpr can be produced in | |||
6451 | /// non-template contexts. | |||
6452 | /// | |||
6453 | /// We will preserve the type in RecoveryExpr when the type is known, e.g. | |||
6454 | /// preserving the return type for a broken non-overloaded function call, a | |||
6455 | /// overloaded call where all candidates have the same return type. In this | |||
6456 | /// case, the expression is not type-dependent (unless the known type is itself | |||
6457 | /// dependent) | |||
6458 | /// | |||
6459 | /// One can also reliably suppress all bogus errors on expressions containing | |||
6460 | /// recovery expressions by examining results of Expr::containsErrors(). | |||
6461 | class RecoveryExpr final : public Expr, | |||
6462 | private llvm::TrailingObjects<RecoveryExpr, Expr *> { | |||
6463 | public: | |||
6464 | static RecoveryExpr *Create(ASTContext &Ctx, QualType T, | |||
6465 | SourceLocation BeginLoc, SourceLocation EndLoc, | |||
6466 | ArrayRef<Expr *> SubExprs); | |||
6467 | static RecoveryExpr *CreateEmpty(ASTContext &Ctx, unsigned NumSubExprs); | |||
6468 | ||||
6469 | ArrayRef<Expr *> subExpressions() { | |||
6470 | auto *B = getTrailingObjects<Expr *>(); | |||
6471 | return llvm::ArrayRef(B, B + NumExprs); | |||
6472 | } | |||
6473 | ||||
6474 | ArrayRef<const Expr *> subExpressions() const { | |||
6475 | return const_cast<RecoveryExpr *>(this)->subExpressions(); | |||
6476 | } | |||
6477 | ||||
6478 | child_range children() { | |||
6479 | Stmt **B = reinterpret_cast<Stmt **>(getTrailingObjects<Expr *>()); | |||
6480 | return child_range(B, B + NumExprs); | |||
6481 | } | |||
6482 | ||||
6483 | SourceLocation getBeginLoc() const { return BeginLoc; } | |||
6484 | SourceLocation getEndLoc() const { return EndLoc; } | |||
6485 | ||||
6486 | static bool classof(const Stmt *T) { | |||
6487 | return T->getStmtClass() == RecoveryExprClass; | |||
6488 | } | |||
6489 | ||||
6490 | private: | |||
6491 | RecoveryExpr(ASTContext &Ctx, QualType T, SourceLocation BeginLoc, | |||
6492 | SourceLocation EndLoc, ArrayRef<Expr *> SubExprs); | |||
6493 | RecoveryExpr(EmptyShell Empty, unsigned NumSubExprs) | |||
6494 | : Expr(RecoveryExprClass, Empty), NumExprs(NumSubExprs) {} | |||
6495 | ||||
6496 | size_t numTrailingObjects(OverloadToken<Stmt *>) const { return NumExprs; } | |||
6497 | ||||
6498 | SourceLocation BeginLoc, EndLoc; | |||
6499 | unsigned NumExprs; | |||
6500 | friend TrailingObjects; | |||
6501 | friend class ASTStmtReader; | |||
6502 | friend class ASTStmtWriter; | |||
6503 | }; | |||
6504 | ||||
6505 | } // end namespace clang | |||
6506 | ||||
6507 | #endif // LLVM_CLANG_AST_EXPR_H |