clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CGBlocks.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/lib/CodeGen/CGBlocks.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "CGBlocks.h" |
14 | #include "CGCXXABI.h" |
15 | #include "CGDebugInfo.h" |
16 | #include "CGObjCRuntime.h" |
17 | #include "CGOpenCLRuntime.h" |
18 | #include "CodeGenFunction.h" |
19 | #include "CodeGenModule.h" |
20 | #include "ConstantEmitter.h" |
21 | #include "TargetInfo.h" |
22 | #include "clang/AST/Attr.h" |
23 | #include "clang/AST/DeclObjC.h" |
24 | #include "clang/CodeGen/ConstantInitBuilder.h" |
25 | #include "llvm/ADT/SmallSet.h" |
26 | #include "llvm/IR/DataLayout.h" |
27 | #include "llvm/IR/Module.h" |
28 | #include "llvm/Support/ScopedPrinter.h" |
29 | #include <algorithm> |
30 | #include <cstdio> |
31 | |
32 | using namespace clang; |
33 | using namespace CodeGen; |
34 | |
35 | CGBlockInfo::CGBlockInfo(const BlockDecl *block, StringRef name) |
36 | : Name(name), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false), |
37 | HasCXXObject(false), UsesStret(false), HasCapturedVariableLayout(false), |
38 | CapturesNonExternalType(false), LocalAddress(Address::invalid()), |
39 | StructureType(nullptr), Block(block) { |
40 | |
41 | |
42 | |
43 | if (!name.empty() && name[0] == '\01') |
44 | name = name.substr(1); |
45 | } |
46 | |
47 | |
48 | BlockByrefHelpers::~BlockByrefHelpers() {} |
49 | |
50 | |
51 | static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, |
52 | const CGBlockInfo &blockInfo, |
53 | llvm::Constant *blockFn); |
54 | |
55 | |
56 | static llvm::Constant *buildCopyHelper(CodeGenModule &CGM, |
57 | const CGBlockInfo &blockInfo) { |
58 | return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo); |
59 | } |
60 | |
61 | |
62 | static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM, |
63 | const CGBlockInfo &blockInfo) { |
64 | return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo); |
65 | } |
66 | |
67 | namespace { |
68 | |
69 | |
70 | |
71 | enum class BlockCaptureEntityKind { |
72 | CXXRecord, |
73 | ARCWeak, |
74 | ARCStrong, |
75 | NonTrivialCStruct, |
76 | BlockObject, |
77 | None |
78 | }; |
79 | |
80 | |
81 | |
82 | struct BlockCaptureManagedEntity { |
83 | BlockCaptureEntityKind CopyKind, DisposeKind; |
84 | BlockFieldFlags CopyFlags, DisposeFlags; |
85 | const BlockDecl::Capture *CI; |
86 | const CGBlockInfo::Capture *Capture; |
87 | |
88 | BlockCaptureManagedEntity(BlockCaptureEntityKind CopyType, |
89 | BlockCaptureEntityKind DisposeType, |
90 | BlockFieldFlags CopyFlags, |
91 | BlockFieldFlags DisposeFlags, |
92 | const BlockDecl::Capture &CI, |
93 | const CGBlockInfo::Capture &Capture) |
94 | : CopyKind(CopyType), DisposeKind(DisposeType), CopyFlags(CopyFlags), |
95 | DisposeFlags(DisposeFlags), CI(&CI), Capture(&Capture) {} |
96 | |
97 | bool operator<(const BlockCaptureManagedEntity &Other) const { |
98 | return Capture->getOffset() < Other.Capture->getOffset(); |
99 | } |
100 | }; |
101 | |
102 | enum class CaptureStrKind { |
103 | |
104 | CopyHelper, |
105 | |
106 | DisposeHelper, |
107 | |
108 | Merged |
109 | }; |
110 | |
111 | } |
112 | |
113 | static void findBlockCapturedManagedEntities( |
114 | const CGBlockInfo &BlockInfo, const LangOptions &LangOpts, |
115 | SmallVectorImpl<BlockCaptureManagedEntity> &ManagedCaptures); |
116 | |
117 | static std::string getBlockCaptureStr(const BlockCaptureManagedEntity &E, |
118 | CaptureStrKind StrKind, |
119 | CharUnits BlockAlignment, |
120 | CodeGenModule &CGM); |
121 | |
122 | static std::string getBlockDescriptorName(const CGBlockInfo &BlockInfo, |
123 | CodeGenModule &CGM) { |
124 | std::string Name = "__block_descriptor_"; |
125 | Name += llvm::to_string(BlockInfo.BlockSize.getQuantity()) + "_"; |
126 | |
127 | if (BlockInfo.needsCopyDisposeHelpers()) { |
128 | if (CGM.getLangOpts().Exceptions) |
129 | Name += "e"; |
130 | if (CGM.getCodeGenOpts().ObjCAutoRefCountExceptions) |
131 | Name += "a"; |
132 | Name += llvm::to_string(BlockInfo.BlockAlign.getQuantity()) + "_"; |
133 | |
134 | SmallVector<BlockCaptureManagedEntity, 4> ManagedCaptures; |
135 | findBlockCapturedManagedEntities(BlockInfo, CGM.getContext().getLangOpts(), |
136 | ManagedCaptures); |
137 | |
138 | for (const BlockCaptureManagedEntity &E : ManagedCaptures) { |
139 | Name += llvm::to_string(E.Capture->getOffset().getQuantity()); |
140 | |
141 | if (E.CopyKind == E.DisposeKind) { |
142 | |
143 | |
144 | assert(E.CopyKind != BlockCaptureEntityKind::None && |
145 | "shouldn't see BlockCaptureManagedEntity that is None"); |
146 | Name += getBlockCaptureStr(E, CaptureStrKind::Merged, |
147 | BlockInfo.BlockAlign, CGM); |
148 | } else { |
149 | |
150 | |
151 | |
152 | Name += getBlockCaptureStr(E, CaptureStrKind::CopyHelper, |
153 | BlockInfo.BlockAlign, CGM); |
154 | Name += getBlockCaptureStr(E, CaptureStrKind::DisposeHelper, |
155 | BlockInfo.BlockAlign, CGM); |
156 | } |
157 | } |
158 | Name += "_"; |
159 | } |
160 | |
161 | std::string TypeAtEncoding = |
162 | CGM.getContext().getObjCEncodingForBlock(BlockInfo.getBlockExpr()); |
163 | |
164 | |
165 | std::replace(TypeAtEncoding.begin(), TypeAtEncoding.end(), '@', '\1'); |
166 | Name += "e" + llvm::to_string(TypeAtEncoding.size()) + "_" + TypeAtEncoding; |
167 | Name += "l" + CGM.getObjCRuntime().getRCBlockLayoutStr(CGM, BlockInfo); |
168 | return Name; |
169 | } |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM, |
186 | const CGBlockInfo &blockInfo) { |
187 | ASTContext &C = CGM.getContext(); |
188 | |
189 | llvm::IntegerType *ulong = |
190 | cast<llvm::IntegerType>(CGM.getTypes().ConvertType(C.UnsignedLongTy)); |
191 | llvm::PointerType *i8p = nullptr; |
192 | if (CGM.getLangOpts().OpenCL) |
193 | i8p = |
194 | llvm::Type::getInt8PtrTy( |
195 | CGM.getLLVMContext(), C.getTargetAddressSpace(LangAS::opencl_constant)); |
196 | else |
197 | i8p = CGM.VoidPtrTy; |
198 | |
199 | std::string descName; |
200 | |
201 | |
202 | if (C.getLangOpts().ObjC && |
203 | CGM.getLangOpts().getGC() == LangOptions::NonGC) { |
204 | descName = getBlockDescriptorName(blockInfo, CGM); |
205 | if (llvm::GlobalValue *desc = CGM.getModule().getNamedValue(descName)) |
206 | return llvm::ConstantExpr::getBitCast(desc, |
207 | CGM.getBlockDescriptorType()); |
208 | } |
209 | |
210 | |
211 | |
212 | ConstantInitBuilder builder(CGM); |
213 | auto elements = builder.beginStruct(); |
214 | |
215 | |
216 | elements.addInt(ulong, 0); |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | elements.addInt(ulong, blockInfo.BlockSize.getQuantity()); |
223 | |
224 | |
225 | bool hasInternalHelper = false; |
226 | if (blockInfo.needsCopyDisposeHelpers()) { |
227 | |
228 | llvm::Constant *copyHelper = buildCopyHelper(CGM, blockInfo); |
229 | elements.add(copyHelper); |
230 | |
231 | |
232 | llvm::Constant *disposeHelper = buildDisposeHelper(CGM, blockInfo); |
233 | elements.add(disposeHelper); |
234 | |
235 | if (cast<llvm::Function>(copyHelper->getOperand(0))->hasInternalLinkage() || |
236 | cast<llvm::Function>(disposeHelper->getOperand(0)) |
237 | ->hasInternalLinkage()) |
238 | hasInternalHelper = true; |
239 | } |
240 | |
241 | |
242 | std::string typeAtEncoding = |
243 | CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr()); |
244 | elements.add(llvm::ConstantExpr::getBitCast( |
245 | CGM.GetAddrOfConstantCString(typeAtEncoding).getPointer(), i8p)); |
246 | |
247 | |
248 | if (C.getLangOpts().ObjC) { |
249 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) |
250 | elements.add(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo)); |
251 | else |
252 | elements.add(CGM.getObjCRuntime().BuildRCBlockLayout(CGM, blockInfo)); |
253 | } |
254 | else |
255 | elements.addNullPointer(i8p); |
256 | |
257 | unsigned AddrSpace = 0; |
258 | if (C.getLangOpts().OpenCL) |
259 | AddrSpace = C.getTargetAddressSpace(LangAS::opencl_constant); |
260 | |
261 | llvm::GlobalValue::LinkageTypes linkage; |
262 | if (descName.empty()) { |
263 | linkage = llvm::GlobalValue::InternalLinkage; |
264 | descName = "__block_descriptor_tmp"; |
265 | } else if (hasInternalHelper) { |
266 | |
267 | |
268 | linkage = llvm::GlobalValue::InternalLinkage; |
269 | } else { |
270 | linkage = llvm::GlobalValue::LinkOnceODRLinkage; |
271 | } |
272 | |
273 | llvm::GlobalVariable *global = |
274 | elements.finishAndCreateGlobal(descName, CGM.getPointerAlign(), |
275 | true, linkage, AddrSpace); |
276 | |
277 | if (linkage == llvm::GlobalValue::LinkOnceODRLinkage) { |
278 | if (CGM.supportsCOMDAT()) |
279 | global->setComdat(CGM.getModule().getOrInsertComdat(descName)); |
280 | global->setVisibility(llvm::GlobalValue::HiddenVisibility); |
281 | global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
282 | } |
283 | |
284 | return llvm::ConstantExpr::getBitCast(global, CGM.getBlockDescriptorType()); |
285 | } |
286 | |
287 | |
288 | |
289 | |
290 | |
291 | |
292 | |
293 | |
294 | |
295 | |
296 | |
297 | |
298 | |
299 | |
300 | |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | |
314 | |
315 | |
316 | |
317 | |
318 | |
319 | |
320 | |
321 | |
322 | |
323 | |
324 | |
325 | |
326 | |
327 | |
328 | |
329 | |
330 | |
331 | |
332 | |
333 | |
334 | |
335 | |
336 | |
337 | |
338 | namespace { |
339 | |
340 | struct BlockLayoutChunk { |
341 | CharUnits Alignment; |
342 | CharUnits Size; |
343 | Qualifiers::ObjCLifetime Lifetime; |
344 | const BlockDecl::Capture *Capture; |
345 | llvm::Type *Type; |
346 | QualType FieldType; |
347 | |
348 | BlockLayoutChunk(CharUnits align, CharUnits size, |
349 | Qualifiers::ObjCLifetime lifetime, |
350 | const BlockDecl::Capture *capture, |
351 | llvm::Type *type, QualType fieldType) |
352 | : Alignment(align), Size(size), Lifetime(lifetime), |
353 | Capture(capture), Type(type), FieldType(fieldType) {} |
354 | |
355 | |
356 | void setIndex(CGBlockInfo &info, unsigned index, CharUnits offset) { |
357 | if (!Capture) { |
358 | info.CXXThisIndex = index; |
359 | info.CXXThisOffset = offset; |
360 | } else { |
361 | auto C = CGBlockInfo::Capture::makeIndex(index, offset, FieldType); |
362 | info.Captures.insert({Capture->getVariable(), C}); |
363 | } |
364 | } |
365 | }; |
366 | |
367 | |
368 | |
369 | bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) { |
370 | if (left.Alignment != right.Alignment) |
371 | return left.Alignment > right.Alignment; |
372 | |
373 | auto getPrefOrder = [](const BlockLayoutChunk &chunk) { |
374 | if (chunk.Capture && chunk.Capture->isByRef()) |
375 | return 1; |
376 | if (chunk.Lifetime == Qualifiers::OCL_Strong) |
377 | return 0; |
378 | if (chunk.Lifetime == Qualifiers::OCL_Weak) |
379 | return 2; |
380 | return 3; |
381 | }; |
382 | |
383 | return getPrefOrder(left) < getPrefOrder(right); |
384 | } |
385 | } |
386 | |
387 | |
388 | static bool isSafeForCXXConstantCapture(QualType type) { |
389 | const RecordType *recordType = |
390 | type->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
391 | |
392 | |
393 | if (!recordType) return true; |
394 | |
395 | const auto *record = cast<CXXRecordDecl>(recordType->getDecl()); |
396 | |
397 | |
398 | if (!record->hasTrivialDestructor()) return false; |
399 | if (record->hasNonTrivialCopyConstructor()) return false; |
400 | |
401 | |
402 | |
403 | return !record->hasMutableFields(); |
404 | } |
405 | |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | |
412 | static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM, |
413 | CodeGenFunction *CGF, |
414 | const VarDecl *var) { |
415 | |
416 | |
417 | if (isa<ParmVarDecl>(var)) |
418 | return nullptr; |
419 | |
420 | QualType type = var->getType(); |
421 | |
422 | |
423 | if (!type.isConstQualified()) return nullptr; |
424 | |
425 | |
426 | |
427 | |
428 | |
429 | |
430 | if (CGM.getLangOpts().CPlusPlus && !isSafeForCXXConstantCapture(type)) |
431 | return nullptr; |
432 | |
433 | |
434 | |
435 | |
436 | const Expr *init = var->getInit(); |
437 | if (!init) return nullptr; |
438 | |
439 | return ConstantEmitter(CGM, CGF).tryEmitAbstractForInitializer(*var); |
440 | } |
441 | |
442 | |
443 | |
444 | static CharUnits getLowBit(CharUnits v) { |
445 | return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1)); |
446 | } |
447 | |
448 | static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info, |
449 | SmallVectorImpl<llvm::Type*> &elementTypes) { |
450 | |
451 | assert(elementTypes.empty()); |
452 | if (CGM.getLangOpts().OpenCL) { |
453 | |
454 | |
455 | auto GenericAS = |
456 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic); |
457 | auto GenPtrAlign = |
458 | CharUnits::fromQuantity(CGM.getTarget().getPointerAlign(GenericAS) / 8); |
459 | auto GenPtrSize = |
460 | CharUnits::fromQuantity(CGM.getTarget().getPointerWidth(GenericAS) / 8); |
461 | assert(CGM.getIntSize() <= GenPtrSize); |
462 | assert(CGM.getIntAlign() <= GenPtrAlign); |
463 | assert((2 * CGM.getIntSize()).isMultipleOf(GenPtrAlign)); |
464 | elementTypes.push_back(CGM.IntTy); |
465 | elementTypes.push_back(CGM.IntTy); |
466 | elementTypes.push_back( |
467 | CGM.getOpenCLRuntime() |
468 | .getGenericVoidPointerType()); |
469 | unsigned Offset = |
470 | 2 * CGM.getIntSize().getQuantity() + GenPtrSize.getQuantity(); |
471 | unsigned BlockAlign = GenPtrAlign.getQuantity(); |
472 | if (auto *Helper = |
473 | CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) { |
474 | for (auto I : Helper->getCustomFieldTypes()) { |
475 | |
476 | |
477 | unsigned Align = CGM.getDataLayout().getABITypeAlignment(I); |
478 | if (BlockAlign < Align) |
479 | BlockAlign = Align; |
480 | assert(Offset % Align == 0); |
481 | Offset += CGM.getDataLayout().getTypeAllocSize(I); |
482 | elementTypes.push_back(I); |
483 | } |
484 | } |
485 | info.BlockAlign = CharUnits::fromQuantity(BlockAlign); |
486 | info.BlockSize = CharUnits::fromQuantity(Offset); |
487 | } else { |
488 | |
489 | |
490 | assert(CGM.getIntSize() <= CGM.getPointerSize()); |
491 | assert(CGM.getIntAlign() <= CGM.getPointerAlign()); |
492 | assert((2 * CGM.getIntSize()).isMultipleOf(CGM.getPointerAlign())); |
493 | info.BlockAlign = CGM.getPointerAlign(); |
494 | info.BlockSize = 3 * CGM.getPointerSize() + 2 * CGM.getIntSize(); |
495 | elementTypes.push_back(CGM.VoidPtrTy); |
496 | elementTypes.push_back(CGM.IntTy); |
497 | elementTypes.push_back(CGM.IntTy); |
498 | elementTypes.push_back(CGM.VoidPtrTy); |
499 | elementTypes.push_back(CGM.getBlockDescriptorType()); |
500 | } |
501 | } |
502 | |
503 | static QualType getCaptureFieldType(const CodeGenFunction &CGF, |
504 | const BlockDecl::Capture &CI) { |
505 | const VarDecl *VD = CI.getVariable(); |
506 | |
507 | |
508 | |
509 | if (CGF.BlockInfo && CI.isNested()) |
510 | return CGF.BlockInfo->getCapture(VD).fieldType(); |
511 | if (auto *FD = CGF.LambdaCaptureFields.lookup(VD)) |
512 | return FD->getType(); |
513 | |
514 | |
515 | |
516 | return VD->isNonEscapingByref() ? |
517 | CGF.getContext().getLValueReferenceType(VD->getType()) : VD->getType(); |
518 | } |
519 | |
520 | |
521 | |
522 | static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF, |
523 | CGBlockInfo &info) { |
524 | ASTContext &C = CGM.getContext(); |
525 | const BlockDecl *block = info.getBlockDecl(); |
526 | |
527 | SmallVector<llvm::Type*, 8> elementTypes; |
528 | initializeForBlockHeader(CGM, info, elementTypes); |
529 | bool hasNonConstantCustomFields = false; |
530 | if (auto *OpenCLHelper = |
| 5 | | Assuming 'OpenCLHelper' is null | |
|
| |
531 | CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) |
532 | hasNonConstantCustomFields = |
533 | !OpenCLHelper->areAllCustomFieldValuesConstant(info); |
534 | if (!block->hasCaptures() && !hasNonConstantCustomFields) { |
| 7 | | Calling 'BlockDecl::hasCaptures' | |
|
| 10 | | Returning from 'BlockDecl::hasCaptures' | |
|
535 | info.StructureType = |
536 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
537 | info.CanBeGlobal = true; |
538 | return; |
539 | } |
540 | else if (C.getLangOpts().ObjC && |
| 11 | | Assuming field 'ObjC' is 0 | |
|
541 | CGM.getLangOpts().getGC() == LangOptions::NonGC) |
542 | info.HasCapturedVariableLayout = true; |
543 | |
544 | |
545 | SmallVector<BlockLayoutChunk, 16> layout; |
546 | layout.reserve(block->capturesCXXThis() + |
547 | (block->capture_end() - block->capture_begin())); |
548 | |
549 | CharUnits maxFieldAlign; |
550 | |
551 | |
552 | if (block->capturesCXXThis()) { |
| 12 | | Assuming the condition is true | |
|
| |
553 | assert(CGF && CGF->CurFuncDecl && isa<CXXMethodDecl>(CGF->CurFuncDecl) && |
554 | "Can't capture 'this' outside a method"); |
555 | QualType thisType = cast<CXXMethodDecl>(CGF->CurFuncDecl)->getThisType(); |
| 14 | | Access to field 'CurFuncDecl' results in a dereference of a null pointer (loaded from variable 'CGF') |
|
556 | |
557 | |
558 | |
559 | llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType); |
560 | auto TInfo = CGM.getContext().getTypeInfoInChars(thisType); |
561 | maxFieldAlign = std::max(maxFieldAlign, TInfo.Align); |
562 | |
563 | layout.push_back(BlockLayoutChunk(TInfo.Align, TInfo.Width, |
564 | Qualifiers::OCL_None, |
565 | nullptr, llvmType, thisType)); |
566 | } |
567 | |
568 | |
569 | for (const auto &CI : block->captures()) { |
570 | const VarDecl *variable = CI.getVariable(); |
571 | |
572 | if (CI.isEscapingByref()) { |
573 | |
574 | info.NeedsCopyDispose = true; |
575 | |
576 | |
577 | CharUnits align = CGM.getPointerAlign(); |
578 | maxFieldAlign = std::max(maxFieldAlign, align); |
579 | |
580 | |
581 | |
582 | assert(CGF && getCaptureFieldType(*CGF, CI) == variable->getType() && |
583 | "capture type differs from the variable type"); |
584 | layout.push_back(BlockLayoutChunk(align, CGM.getPointerSize(), |
585 | Qualifiers::OCL_None, &CI, |
586 | CGM.VoidPtrTy, variable->getType())); |
587 | continue; |
588 | } |
589 | |
590 | |
591 | |
592 | if (llvm::Constant *constant = tryCaptureAsConstant(CGM, CGF, variable)) { |
593 | info.Captures[variable] = CGBlockInfo::Capture::makeConstant(constant); |
594 | continue; |
595 | } |
596 | |
597 | QualType VT = getCaptureFieldType(*CGF, CI); |
598 | |
599 | |
600 | |
601 | Qualifiers::ObjCLifetime lifetime = VT.getObjCLifetime(); |
602 | if (lifetime) { |
603 | switch (lifetime) { |
604 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
605 | case Qualifiers::OCL_ExplicitNone: |
606 | case Qualifiers::OCL_Autoreleasing: |
607 | break; |
608 | |
609 | case Qualifiers::OCL_Strong: |
610 | case Qualifiers::OCL_Weak: |
611 | info.NeedsCopyDispose = true; |
612 | } |
613 | |
614 | |
615 | } else if (VT->isObjCRetainableType()) { |
616 | |
617 | |
618 | if (VT->isObjCInertUnsafeUnretainedType()) { |
619 | lifetime = Qualifiers::OCL_ExplicitNone; |
620 | } else { |
621 | info.NeedsCopyDispose = true; |
622 | |
623 | lifetime = Qualifiers::OCL_Strong; |
624 | } |
625 | |
626 | |
627 | } else if (CI.hasCopyExpr()) { |
628 | info.NeedsCopyDispose = true; |
629 | info.HasCXXObject = true; |
630 | if (!VT->getAsCXXRecordDecl()->isExternallyVisible()) |
631 | info.CapturesNonExternalType = true; |
632 | |
633 | |
634 | |
635 | } else if (VT.isNonTrivialToPrimitiveCopy() == QualType::PCK_Struct || |
636 | VT.isDestructedType() == QualType::DK_nontrivial_c_struct) { |
637 | info.NeedsCopyDispose = true; |
638 | |
639 | |
640 | } else if (CGM.getLangOpts().CPlusPlus) { |
641 | if (const CXXRecordDecl *record = VT->getAsCXXRecordDecl()) { |
642 | if (!record->hasTrivialDestructor()) { |
643 | info.HasCXXObject = true; |
644 | info.NeedsCopyDispose = true; |
645 | if (!record->isExternallyVisible()) |
646 | info.CapturesNonExternalType = true; |
647 | } |
648 | } |
649 | } |
650 | |
651 | CharUnits size = C.getTypeSizeInChars(VT); |
652 | CharUnits align = C.getDeclAlign(variable); |
653 | |
654 | maxFieldAlign = std::max(maxFieldAlign, align); |
655 | |
656 | llvm::Type *llvmType = |
657 | CGM.getTypes().ConvertTypeForMem(VT); |
658 | |
659 | layout.push_back( |
660 | BlockLayoutChunk(align, size, lifetime, &CI, llvmType, VT)); |
661 | } |
662 | |
663 | |
664 | if (layout.empty()) { |
665 | info.StructureType = |
666 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
667 | info.CanBeGlobal = true; |
668 | return; |
669 | } |
670 | |
671 | |
672 | |
673 | |
674 | llvm::stable_sort(layout); |
675 | |
676 | |
677 | info.BlockHeaderForcedGapOffset = info.BlockSize; |
678 | info.BlockHeaderForcedGapSize = CharUnits::Zero(); |
679 | |
680 | CharUnits &blockSize = info.BlockSize; |
681 | info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign); |
682 | |
683 | |
684 | |
685 | CharUnits endAlign = getLowBit(blockSize); |
686 | |
687 | |
688 | |
689 | |
690 | |
691 | |
692 | |
693 | |
694 | |
695 | |
696 | |
697 | |
698 | if (endAlign < maxFieldAlign) { |
699 | SmallVectorImpl<BlockLayoutChunk>::iterator |
700 | li = layout.begin() + 1, le = layout.end(); |
701 | |
702 | |
703 | |
704 | for (; li != le && endAlign < li->Alignment; ++li) |
705 | ; |
706 | |
707 | |
708 | |
709 | if (li != le) { |
710 | SmallVectorImpl<BlockLayoutChunk>::iterator first = li; |
711 | for (; li != le; ++li) { |
712 | assert(endAlign >= li->Alignment); |
713 | |
714 | li->setIndex(info, elementTypes.size(), blockSize); |
715 | elementTypes.push_back(li->Type); |
716 | blockSize += li->Size; |
717 | endAlign = getLowBit(blockSize); |
718 | |
719 | |
720 | if (endAlign >= maxFieldAlign) { |
721 | break; |
722 | } |
723 | } |
724 | |
725 | layout.erase(first, li); |
726 | } |
727 | } |
728 | |
729 | assert(endAlign == getLowBit(blockSize)); |
730 | |
731 | |
732 | |
733 | if (endAlign < maxFieldAlign) { |
734 | CharUnits newBlockSize = blockSize.alignTo(maxFieldAlign); |
735 | CharUnits padding = newBlockSize - blockSize; |
736 | |
737 | |
738 | |
739 | if (blockSize == info.BlockHeaderForcedGapOffset) { |
740 | info.BlockHeaderForcedGapSize = padding; |
741 | } |
742 | |
743 | elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty, |
744 | padding.getQuantity())); |
745 | blockSize = newBlockSize; |
746 | endAlign = getLowBit(blockSize); |
747 | } |
748 | |
749 | assert(endAlign >= maxFieldAlign); |
750 | assert(endAlign == getLowBit(blockSize)); |
751 | |
752 | |
753 | |
754 | for (SmallVectorImpl<BlockLayoutChunk>::iterator |
755 | li = layout.begin(), le = layout.end(); li != le; ++li) { |
756 | if (endAlign < li->Alignment) { |
757 | |
758 | |
759 | |
760 | CharUnits padding = li->Alignment - endAlign; |
761 | elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty, |
762 | padding.getQuantity())); |
763 | blockSize += padding; |
764 | endAlign = getLowBit(blockSize); |
765 | } |
766 | assert(endAlign >= li->Alignment); |
767 | li->setIndex(info, elementTypes.size(), blockSize); |
768 | elementTypes.push_back(li->Type); |
769 | blockSize += li->Size; |
770 | endAlign = getLowBit(blockSize); |
771 | } |
772 | |
773 | info.StructureType = |
774 | llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); |
775 | } |
776 | |
777 | |
778 | llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) { |
779 | |
780 | |
781 | if (!blockExpr->getBlockDecl()->hasCaptures()) |
782 | |
783 | |
784 | if (llvm::Constant *Block = CGM.getAddrOfGlobalBlockIfEmitted(blockExpr)) |
785 | return Block; |
786 | |
787 | CGBlockInfo blockInfo(blockExpr->getBlockDecl(), CurFn->getName()); |
788 | computeBlockInfo(CGM, this, blockInfo); |
789 | blockInfo.BlockExpression = blockExpr; |
790 | if (!blockInfo.CanBeGlobal) |
791 | blockInfo.LocalAddress = CreateTempAlloca(blockInfo.StructureType, |
792 | blockInfo.BlockAlign, "block"); |
793 | return EmitBlockLiteral(blockInfo); |
794 | } |
795 | |
796 | llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) { |
797 | bool IsOpenCL = CGM.getContext().getLangOpts().OpenCL; |
798 | auto GenVoidPtrTy = |
799 | IsOpenCL ? CGM.getOpenCLRuntime().getGenericVoidPointerType() : VoidPtrTy; |
800 | LangAS GenVoidPtrAddr = IsOpenCL ? LangAS::opencl_generic : LangAS::Default; |
801 | auto GenVoidPtrSize = CharUnits::fromQuantity( |
802 | CGM.getTarget().getPointerWidth( |
803 | CGM.getContext().getTargetAddressSpace(GenVoidPtrAddr)) / |
804 | 8); |
805 | |
806 | bool isLambdaConv = blockInfo.getBlockDecl()->isConversionFromLambda(); |
807 | CodeGenFunction BlockCGF{CGM, true}; |
808 | BlockCGF.SanOpts = SanOpts; |
809 | auto *InvokeFn = BlockCGF.GenerateBlockFunction( |
810 | CurGD, blockInfo, LocalDeclMap, isLambdaConv, blockInfo.CanBeGlobal); |
811 | auto *blockFn = llvm::ConstantExpr::getPointerCast(InvokeFn, GenVoidPtrTy); |
812 | |
813 | |
814 | if (blockInfo.CanBeGlobal) |
815 | return CGM.getAddrOfGlobalBlockIfEmitted(blockInfo.BlockExpression); |
816 | |
817 | |
818 | |
819 | Address blockAddr = blockInfo.LocalAddress; |
820 | assert(blockAddr.isValid() && "block has no address!"); |
821 | |
822 | llvm::Constant *isa; |
823 | llvm::Constant *descriptor; |
824 | BlockFlags flags; |
825 | if (!IsOpenCL) { |
826 | |
827 | |
828 | |
829 | llvm::Constant *blockISA = blockInfo.getBlockDecl()->doesNotEscape() |
830 | ? CGM.getNSConcreteGlobalBlock() |
831 | : CGM.getNSConcreteStackBlock(); |
832 | isa = llvm::ConstantExpr::getBitCast(blockISA, VoidPtrTy); |
833 | |
834 | |
835 | descriptor = buildBlockDescriptor(CGM, blockInfo); |
836 | |
837 | |
838 | flags = BLOCK_HAS_SIGNATURE; |
839 | if (blockInfo.HasCapturedVariableLayout) |
840 | flags |= BLOCK_HAS_EXTENDED_LAYOUT; |
841 | if (blockInfo.needsCopyDisposeHelpers()) |
842 | flags |= BLOCK_HAS_COPY_DISPOSE; |
843 | if (blockInfo.HasCXXObject) |
844 | flags |= BLOCK_HAS_CXX_OBJ; |
845 | if (blockInfo.UsesStret) |
846 | flags |= BLOCK_USE_STRET; |
847 | if (blockInfo.getBlockDecl()->doesNotEscape()) |
848 | flags |= BLOCK_IS_NOESCAPE | BLOCK_IS_GLOBAL; |
849 | } |
850 | |
851 | auto projectField = [&](unsigned index, const Twine &name) -> Address { |
852 | return Builder.CreateStructGEP(blockAddr, index, name); |
853 | }; |
854 | auto storeField = [&](llvm::Value *value, unsigned index, const Twine &name) { |
855 | Builder.CreateStore(value, projectField(index, name)); |
856 | }; |
857 | |
858 | |
859 | { |
860 | |
861 | unsigned index = 0; |
862 | CharUnits offset; |
863 | auto addHeaderField = [&](llvm::Value *value, CharUnits size, |
864 | const Twine &name) { |
865 | storeField(value, index, name); |
866 | offset += size; |
867 | index++; |
868 | }; |
869 | |
870 | if (!IsOpenCL) { |
871 | addHeaderField(isa, getPointerSize(), "block.isa"); |
872 | addHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()), |
873 | getIntSize(), "block.flags"); |
874 | addHeaderField(llvm::ConstantInt::get(IntTy, 0), getIntSize(), |
875 | "block.reserved"); |
876 | } else { |
877 | addHeaderField( |
878 | llvm::ConstantInt::get(IntTy, blockInfo.BlockSize.getQuantity()), |
879 | getIntSize(), "block.size"); |
880 | addHeaderField( |
881 | llvm::ConstantInt::get(IntTy, blockInfo.BlockAlign.getQuantity()), |
882 | getIntSize(), "block.align"); |
883 | } |
884 | addHeaderField(blockFn, GenVoidPtrSize, "block.invoke"); |
885 | if (!IsOpenCL) |
886 | addHeaderField(descriptor, getPointerSize(), "block.descriptor"); |
887 | else if (auto *Helper = |
888 | CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) { |
889 | for (auto I : Helper->getCustomFieldValues(*this, blockInfo)) { |
890 | addHeaderField( |
891 | I.first, |
892 | CharUnits::fromQuantity( |
893 | CGM.getDataLayout().getTypeAllocSize(I.first->getType())), |
894 | I.second); |
895 | } |
896 | } |
897 | } |
898 | |
899 | |
900 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
901 | |
902 | |
903 | if (blockDecl->capturesCXXThis()) { |
904 | Address addr = |
905 | projectField(blockInfo.CXXThisIndex, "block.captured-this.addr"); |
906 | Builder.CreateStore(LoadCXXThis(), addr); |
907 | } |
908 | |
909 | |
910 | for (const auto &CI : blockDecl->captures()) { |
911 | const VarDecl *variable = CI.getVariable(); |
912 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
913 | |
914 | |
915 | if (capture.isConstant()) continue; |
916 | |
917 | QualType type = capture.fieldType(); |
918 | |
919 | |
920 | |
921 | Address blockField = projectField(capture.getIndex(), "block.captured"); |
922 | |
923 | |
924 | |
925 | Address src = Address::invalid(); |
926 | |
927 | if (blockDecl->isConversionFromLambda()) { |
928 | |
929 | |
930 | src = Address::invalid(); |
931 | } else if (CI.isEscapingByref()) { |
932 | if (BlockInfo && CI.isNested()) { |
933 | |
934 | const CGBlockInfo::Capture &enclosingCapture = |
935 | BlockInfo->getCapture(variable); |
936 | |
937 | |
938 | src = Builder.CreateStructGEP(LoadBlockStruct(), |
939 | enclosingCapture.getIndex(), |
940 | "block.capture.addr"); |
941 | } else { |
942 | auto I = LocalDeclMap.find(variable); |
943 | assert(I != LocalDeclMap.end()); |
944 | src = I->second; |
945 | } |
946 | } else { |
947 | DeclRefExpr declRef(getContext(), const_cast<VarDecl *>(variable), |
948 | CI.isNested(), |
949 | type.getNonReferenceType(), VK_LValue, |
950 | SourceLocation()); |
951 | src = EmitDeclRefLValue(&declRef).getAddress(*this); |
952 | }; |
953 | |
954 | |
955 | |
956 | |
957 | |
958 | if (CI.isEscapingByref()) { |
959 | |
960 | llvm::Value *byrefPointer; |
961 | if (CI.isNested()) |
962 | byrefPointer = Builder.CreateLoad(src, "byref.capture"); |
963 | else |
964 | byrefPointer = Builder.CreateBitCast(src.getPointer(), VoidPtrTy); |
965 | |
966 | |
967 | Builder.CreateStore(byrefPointer, blockField); |
968 | |
969 | |
970 | } else if (const Expr *copyExpr = CI.getCopyExpr()) { |
971 | if (blockDecl->isConversionFromLambda()) { |
972 | |
973 | |
974 | AggValueSlot Slot = |
975 | AggValueSlot::forAddr(blockField, Qualifiers(), |
976 | AggValueSlot::IsDestructed, |
977 | AggValueSlot::DoesNotNeedGCBarriers, |
978 | AggValueSlot::IsNotAliased, |
979 | AggValueSlot::DoesNotOverlap); |
980 | EmitAggExpr(copyExpr, Slot); |
981 | } else { |
982 | EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr); |
983 | } |
984 | |
985 | |
986 | } else if (type->isReferenceType()) { |
987 | Builder.CreateStore(src.getPointer(), blockField); |
988 | |
989 | |
990 | } else if (type.isConstQualified() && |
991 | type.getObjCLifetime() == Qualifiers::OCL_Strong && |
992 | CGM.getCodeGenOpts().OptimizationLevel != 0) { |
993 | llvm::Value *value = Builder.CreateLoad(src, "captured"); |
994 | Builder.CreateStore(value, blockField); |
995 | |
996 | |
997 | |
998 | |
999 | |
1000 | |
1001 | |
1002 | |
1003 | } else if (type.getObjCLifetime() == Qualifiers::OCL_Strong && |
1004 | type->isBlockPointerType()) { |
1005 | |
1006 | llvm::Value *value = Builder.CreateLoad(src, "block.captured_block"); |
1007 | value = EmitARCRetainNonBlock(value); |
1008 | |
1009 | |
1010 | Builder.CreateStore(value, blockField); |
1011 | |
1012 | |
1013 | } else { |
1014 | |
1015 | |
1016 | ImplicitParamDecl BlockFieldPseudoVar(getContext(), type, |
1017 | ImplicitParamDecl::Other); |
1018 | |
1019 | |
1020 | |
1021 | DeclRefExpr declRef(getContext(), const_cast<VarDecl *>(variable), |
1022 | CI.isNested(), |
1023 | type, VK_LValue, SourceLocation()); |
1024 | |
1025 | ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue, |
1026 | &declRef, VK_PRValue, FPOptionsOverride()); |
1027 | |
1028 | |
1029 | |
1030 | EmitExprAsInit(&l2r, &BlockFieldPseudoVar, |
1031 | MakeAddrLValue(blockField, type, AlignmentSource::Decl), |
1032 | false); |
1033 | } |
1034 | |
1035 | |
1036 | if (!blockInfo.NeedsCopyDispose) |
1037 | continue; |
1038 | |
1039 | |
1040 | |
1041 | if (CI.isByRef()) |
1042 | continue; |
1043 | |
1044 | |
1045 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
1046 | if (dtorKind == QualType::DK_none) |
1047 | continue; |
1048 | |
1049 | CodeGenFunction::Destroyer *destroyer; |
1050 | |
1051 | |
1052 | |
1053 | |
1054 | |
1055 | |
1056 | |
1057 | if (type.isConstQualified() && |
1058 | type.getObjCLifetime() == Qualifiers::OCL_Strong && |
1059 | CGM.getCodeGenOpts().OptimizationLevel != 0) { |
1060 | assert(CGM.getLangOpts().ObjCAutoRefCount && |
1061 | "expected ObjC ARC to be enabled"); |
1062 | destroyer = emitARCIntrinsicUse; |
1063 | } else if (dtorKind == QualType::DK_objc_strong_lifetime) { |
1064 | destroyer = destroyARCStrongImprecise; |
1065 | } else { |
1066 | destroyer = getDestroyer(dtorKind); |
1067 | } |
1068 | |
1069 | CleanupKind cleanupKind = NormalCleanup; |
1070 | bool useArrayEHCleanup = needsEHCleanup(dtorKind); |
1071 | if (useArrayEHCleanup) |
1072 | cleanupKind = NormalAndEHCleanup; |
1073 | |
1074 | |
1075 | |
1076 | |
1077 | |
1078 | auto IsBlockDeclInRetExpr = [&]() { |
1079 | auto *EWC = llvm::dyn_cast_or_null<ExprWithCleanups>(RetExpr); |
1080 | if (EWC) |
1081 | for (auto &C : EWC->getObjects()) |
1082 | if (auto *BD = C.dyn_cast<BlockDecl *>()) |
1083 | if (BD == blockDecl) |
1084 | return true; |
1085 | return false; |
1086 | }; |
1087 | |
1088 | if (IsBlockDeclInRetExpr()) |
1089 | pushDestroy(cleanupKind, blockField, type, destroyer, useArrayEHCleanup); |
1090 | else |
1091 | pushLifetimeExtendedDestroy(cleanupKind, blockField, type, destroyer, |
1092 | useArrayEHCleanup); |
1093 | } |
1094 | |
1095 | |
1096 | |
1097 | llvm::Value *result = Builder.CreatePointerCast( |
1098 | blockAddr.getPointer(), ConvertType(blockInfo.getBlockExpr()->getType())); |
1099 | |
1100 | if (IsOpenCL) { |
1101 | CGM.getOpenCLRuntime().recordBlockInfo(blockInfo.BlockExpression, InvokeFn, |
1102 | result); |
1103 | } |
1104 | |
1105 | return result; |
1106 | } |
1107 | |
1108 | |
1109 | llvm::Type *CodeGenModule::getBlockDescriptorType() { |
1110 | if (BlockDescriptorType) |
1111 | return BlockDescriptorType; |
1112 | |
1113 | llvm::Type *UnsignedLongTy = |
1114 | getTypes().ConvertType(getContext().UnsignedLongTy); |
1115 | |
1116 | |
1117 | |
1118 | |
1119 | |
1120 | |
1121 | |
1122 | |
1123 | |
1124 | |
1125 | |
1126 | |
1127 | |
1128 | |
1129 | |
1130 | BlockDescriptorType = llvm::StructType::create( |
1131 | "struct.__block_descriptor", UnsignedLongTy, UnsignedLongTy); |
1132 | |
1133 | |
1134 | unsigned AddrSpace = 0; |
1135 | if (getLangOpts().OpenCL) |
1136 | AddrSpace = getContext().getTargetAddressSpace(LangAS::opencl_constant); |
1137 | BlockDescriptorType = llvm::PointerType::get(BlockDescriptorType, AddrSpace); |
1138 | return BlockDescriptorType; |
1139 | } |
1140 | |
1141 | llvm::Type *CodeGenModule::getGenericBlockLiteralType() { |
1142 | if (GenericBlockLiteralType) |
1143 | return GenericBlockLiteralType; |
1144 | |
1145 | llvm::Type *BlockDescPtrTy = getBlockDescriptorType(); |
1146 | |
1147 | if (getLangOpts().OpenCL) { |
1148 | |
1149 | |
1150 | |
1151 | |
1152 | |
1153 | |
1154 | SmallVector<llvm::Type *, 8> StructFields( |
1155 | {IntTy, IntTy, getOpenCLRuntime().getGenericVoidPointerType()}); |
1156 | if (auto *Helper = getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) { |
1157 | for (auto I : Helper->getCustomFieldTypes()) |
1158 | StructFields.push_back(I); |
1159 | } |
1160 | GenericBlockLiteralType = llvm::StructType::create( |
1161 | StructFields, "struct.__opencl_block_literal_generic"); |
1162 | } else { |
1163 | |
1164 | |
1165 | |
1166 | |
1167 | |
1168 | |
1169 | |
1170 | GenericBlockLiteralType = |
1171 | llvm::StructType::create("struct.__block_literal_generic", VoidPtrTy, |
1172 | IntTy, IntTy, VoidPtrTy, BlockDescPtrTy); |
1173 | } |
1174 | |
1175 | return GenericBlockLiteralType; |
1176 | } |
1177 | |
1178 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E, |
1179 | ReturnValueSlot ReturnValue) { |
1180 | const auto *BPT = E->getCallee()->getType()->castAs<BlockPointerType>(); |
1181 | llvm::Value *BlockPtr = EmitScalarExpr(E->getCallee()); |
1182 | llvm::Type *GenBlockTy = CGM.getGenericBlockLiteralType(); |
1183 | llvm::Value *Func = nullptr; |
1184 | QualType FnType = BPT->getPointeeType(); |
1185 | ASTContext &Ctx = getContext(); |
1186 | CallArgList Args; |
1187 | |
1188 | if (getLangOpts().OpenCL) { |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | llvm::Type *GenericVoidPtrTy = |
1194 | CGM.getOpenCLRuntime().getGenericVoidPointerType(); |
1195 | llvm::Value *BlockDescriptor = Builder.CreatePointerCast( |
1196 | BlockPtr, GenericVoidPtrTy); |
1197 | QualType VoidPtrQualTy = Ctx.getPointerType( |
1198 | Ctx.getAddrSpaceQualType(Ctx.VoidTy, LangAS::opencl_generic)); |
1199 | Args.add(RValue::get(BlockDescriptor), VoidPtrQualTy); |
1200 | |
1201 | EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), E->arguments()); |
1202 | |
1203 | |
1204 | if (!isa<ParmVarDecl>(E->getCalleeDecl())) |
1205 | Func = CGM.getOpenCLRuntime().getInvokeFunction(E->getCallee()); |
1206 | else { |
1207 | llvm::Value *FuncPtr = Builder.CreateStructGEP(GenBlockTy, BlockPtr, 2); |
1208 | Func = Builder.CreateAlignedLoad(GenericVoidPtrTy, FuncPtr, |
1209 | getPointerAlign()); |
1210 | } |
1211 | } else { |
1212 | |
1213 | BlockPtr = Builder.CreatePointerCast( |
1214 | BlockPtr, llvm::PointerType::get(GenBlockTy, 0), "block.literal"); |
1215 | |
1216 | llvm::Value *FuncPtr = Builder.CreateStructGEP(GenBlockTy, BlockPtr, 3); |
1217 | |
1218 | |
1219 | BlockPtr = Builder.CreatePointerCast(BlockPtr, VoidPtrTy); |
1220 | Args.add(RValue::get(BlockPtr), Ctx.VoidPtrTy); |
1221 | |
1222 | EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), E->arguments()); |
1223 | |
1224 | |
1225 | Func = Builder.CreateAlignedLoad(VoidPtrTy, FuncPtr, getPointerAlign()); |
1226 | } |
1227 | |
1228 | const FunctionType *FuncTy = FnType->castAs<FunctionType>(); |
1229 | const CGFunctionInfo &FnInfo = |
1230 | CGM.getTypes().arrangeBlockFunctionCall(Args, FuncTy); |
1231 | |
1232 | |
1233 | llvm::Type *BlockFTy = CGM.getTypes().GetFunctionType(FnInfo); |
1234 | |
1235 | llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
1236 | Func = Builder.CreatePointerCast(Func, BlockFTyPtr); |
1237 | |
1238 | |
1239 | CGCallee Callee(CGCalleeInfo(), Func); |
1240 | |
1241 | |
1242 | return EmitCall(FnInfo, Callee, ReturnValue, Args); |
1243 | } |
1244 | |
1245 | Address CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable) { |
1246 | assert(BlockInfo && "evaluating block ref without block information?"); |
1247 | const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable); |
1248 | |
1249 | |
1250 | if (capture.isConstant()) return LocalDeclMap.find(variable)->second; |
1251 | |
1252 | Address addr = Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(), |
1253 | "block.capture.addr"); |
1254 | |
1255 | if (variable->isEscapingByref()) { |
1256 | |
1257 | |
1258 | |
1259 | auto &byrefInfo = getBlockByrefInfo(variable); |
1260 | addr = Address(Builder.CreateLoad(addr), byrefInfo.ByrefAlignment); |
1261 | |
1262 | auto byrefPointerType = llvm::PointerType::get(byrefInfo.Type, 0); |
1263 | addr = Builder.CreateBitCast(addr, byrefPointerType, "byref.addr"); |
1264 | |
1265 | addr = emitBlockByrefAddress(addr, byrefInfo, true, |
1266 | variable->getName()); |
1267 | } |
1268 | |
1269 | assert((!variable->isNonEscapingByref() || |
1270 | capture.fieldType()->isReferenceType()) && |
1271 | "the capture field of a non-escaping variable should have a " |
1272 | "reference type"); |
1273 | if (capture.fieldType()->isReferenceType()) |
1274 | addr = EmitLoadOfReference(MakeAddrLValue(addr, capture.fieldType())); |
1275 | |
1276 | return addr; |
1277 | } |
1278 | |
1279 | void CodeGenModule::setAddrOfGlobalBlock(const BlockExpr *BE, |
1280 | llvm::Constant *Addr) { |
1281 | bool Ok = EmittedGlobalBlocks.insert(std::make_pair(BE, Addr)).second; |
1282 | (void)Ok; |
1283 | assert(Ok && "Trying to replace an already-existing global block!"); |
1284 | } |
1285 | |
1286 | llvm::Constant * |
1287 | CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, |
1288 | StringRef Name) { |
1289 | if (llvm::Constant *Block = getAddrOfGlobalBlockIfEmitted(BE)) |
| 1 | Assuming 'Block' is null | |
|
| |
1290 | return Block; |
1291 | |
1292 | CGBlockInfo blockInfo(BE->getBlockDecl(), Name); |
1293 | blockInfo.BlockExpression = BE; |
1294 | |
1295 | |
1296 | computeBlockInfo(*this, nullptr, blockInfo); |
| 3 | | Passing null pointer value via 2nd parameter 'CGF' | |
|
| 4 | | Calling 'computeBlockInfo' | |
|
1297 | |
1298 | |
1299 | { |
1300 | CodeGenFunction::DeclMapTy LocalDeclMap; |
1301 | CodeGenFunction(*this).GenerateBlockFunction( |
1302 | GlobalDecl(), blockInfo, LocalDeclMap, |
1303 | false, true); |
1304 | } |
1305 | |
1306 | return getAddrOfGlobalBlockIfEmitted(BE); |
1307 | } |
1308 | |
1309 | static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, |
1310 | const CGBlockInfo &blockInfo, |
1311 | llvm::Constant *blockFn) { |
1312 | assert(blockInfo.CanBeGlobal); |
1313 | |
1314 | |
1315 | |
1316 | assert(!CGM.getAddrOfGlobalBlockIfEmitted(blockInfo.BlockExpression) && |
1317 | "Refusing to re-emit a global block."); |
1318 | |
1319 | |
1320 | ConstantInitBuilder builder(CGM); |
1321 | auto fields = builder.beginStruct(); |
1322 | |
1323 | bool IsOpenCL = CGM.getLangOpts().OpenCL; |
1324 | bool IsWindows = CGM.getTarget().getTriple().isOSWindows(); |
1325 | if (!IsOpenCL) { |
1326 | |
1327 | if (IsWindows) |
1328 | fields.addNullPointer(CGM.Int8PtrPtrTy); |
1329 | else |
1330 | fields.add(CGM.getNSConcreteGlobalBlock()); |
1331 | |
1332 | |
1333 | BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE; |
1334 | if (blockInfo.UsesStret) |
1335 | flags |= BLOCK_USE_STRET; |
1336 | |
1337 | fields.addInt(CGM.IntTy, flags.getBitMask()); |
1338 | |
1339 | |
1340 | fields.addInt(CGM.IntTy, 0); |
1341 | } else { |
1342 | fields.addInt(CGM.IntTy, blockInfo.BlockSize.getQuantity()); |
1343 | fields.addInt(CGM.IntTy, blockInfo.BlockAlign.getQuantity()); |
1344 | } |
1345 | |
1346 | |
1347 | fields.add(blockFn); |
1348 | |
1349 | if (!IsOpenCL) { |
1350 | |
1351 | fields.add(buildBlockDescriptor(CGM, blockInfo)); |
1352 | } else if (auto *Helper = |
1353 | CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) { |
1354 | for (auto I : Helper->getCustomFieldValues(CGM, blockInfo)) { |
1355 | fields.add(I); |
1356 | } |
1357 | } |
1358 | |
1359 | unsigned AddrSpace = 0; |
1360 | if (CGM.getContext().getLangOpts().OpenCL) |
1361 | AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_global); |
1362 | |
1363 | llvm::GlobalVariable *literal = fields.finishAndCreateGlobal( |
1364 | "__block_literal_global", blockInfo.BlockAlign, |
1365 | !IsWindows, llvm::GlobalVariable::InternalLinkage, AddrSpace); |
1366 | |
1367 | literal->addAttribute("objc_arc_inert"); |
1368 | |
1369 | |
1370 | |
1371 | if (IsWindows) { |
1372 | auto *Init = llvm::Function::Create(llvm::FunctionType::get(CGM.VoidTy, |
1373 | {}), llvm::GlobalValue::InternalLinkage, ".block_isa_init", |
1374 | &CGM.getModule()); |
1375 | llvm::IRBuilder<> b(llvm::BasicBlock::Create(CGM.getLLVMContext(), "entry", |
1376 | Init)); |
1377 | b.CreateAlignedStore(CGM.getNSConcreteGlobalBlock(), |
1378 | b.CreateStructGEP(literal->getValueType(), literal, 0), |
1379 | CGM.getPointerAlign().getAsAlign()); |
1380 | b.CreateRetVoid(); |
1381 | |
1382 | |
1383 | auto *InitVar = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), |
1384 | true, llvm::GlobalValue::InternalLinkage, |
1385 | Init, ".block_isa_init_ptr"); |
1386 | InitVar->setSection(".CRT$XCLa"); |
1387 | CGM.addUsedGlobal(InitVar); |
1388 | } |
1389 | |
1390 | |
1391 | llvm::Type *RequiredType = |
1392 | CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType()); |
1393 | llvm::Constant *Result = |
1394 | llvm::ConstantExpr::getPointerCast(literal, RequiredType); |
1395 | CGM.setAddrOfGlobalBlock(blockInfo.BlockExpression, Result); |
1396 | if (CGM.getContext().getLangOpts().OpenCL) |
1397 | CGM.getOpenCLRuntime().recordBlockInfo( |
1398 | blockInfo.BlockExpression, |
1399 | cast<llvm::Function>(blockFn->stripPointerCasts()), Result); |
1400 | return Result; |
1401 | } |
1402 | |
1403 | void CodeGenFunction::setBlockContextParameter(const ImplicitParamDecl *D, |
1404 | unsigned argNum, |
1405 | llvm::Value *arg) { |
1406 | assert(BlockInfo && "not emitting prologue of block invocation function?!"); |
1407 | |
1408 | |
1409 | |
1410 | Address alloc = CreateMemTemp(D->getType(), D->getName() + ".addr"); |
1411 | Builder.CreateStore(arg, alloc); |
1412 | if (CGDebugInfo *DI = getDebugInfo()) { |
1413 | if (CGM.getCodeGenOpts().hasReducedDebugInfo()) { |
1414 | DI->setLocation(D->getLocation()); |
1415 | DI->EmitDeclareOfBlockLiteralArgVariable( |
1416 | *BlockInfo, D->getName(), argNum, |
1417 | cast<llvm::AllocaInst>(alloc.getPointer()), Builder); |
1418 | } |
1419 | } |
1420 | |
1421 | SourceLocation StartLoc = BlockInfo->getBlockExpr()->getBody()->getBeginLoc(); |
1422 | ApplyDebugLocation Scope(*this, StartLoc); |
1423 | |
1424 | |
1425 | |
1426 | BlockPointer = Builder.CreatePointerCast( |
1427 | arg, |
1428 | BlockInfo->StructureType->getPointerTo( |
1429 | getContext().getLangOpts().OpenCL |
1430 | ? getContext().getTargetAddressSpace(LangAS::opencl_generic) |
1431 | : 0), |
1432 | "block"); |
1433 | } |
1434 | |
1435 | Address CodeGenFunction::LoadBlockStruct() { |
1436 | assert(BlockInfo && "not in a block invocation function!"); |
1437 | assert(BlockPointer && "no block pointer set!"); |
1438 | return Address(BlockPointer, BlockInfo->BlockAlign); |
1439 | } |
1440 | |
1441 | llvm::Function * |
1442 | CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, |
1443 | const CGBlockInfo &blockInfo, |
1444 | const DeclMapTy &ldm, |
1445 | bool IsLambdaConversionToBlock, |
1446 | bool BuildGlobalBlock) { |
1447 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
1448 | |
1449 | CurGD = GD; |
1450 | |
1451 | CurEHLocation = blockInfo.getBlockExpr()->getEndLoc(); |
1452 | |
1453 | BlockInfo = &blockInfo; |
1454 | |
1455 | |
1456 | |
1457 | |
1458 | for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) { |
1459 | const auto *var = dyn_cast<VarDecl>(i->first); |
1460 | if (var && !var->hasLocalStorage()) |
1461 | setAddrOfLocalVar(var, i->second); |
1462 | } |
1463 | |
1464 | |
1465 | |
1466 | |
1467 | FunctionArgList args; |
1468 | |
1469 | |
1470 | |
1471 | QualType selfTy = getContext().VoidPtrTy; |
1472 | |
1473 | |
1474 | |
1475 | |
1476 | |
1477 | if (getLangOpts().OpenCL) |
1478 | selfTy = getContext().getPointerType(getContext().getAddrSpaceQualType( |
1479 | getContext().VoidTy, LangAS::opencl_generic)); |
1480 | |
1481 | IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); |
1482 | |
1483 | ImplicitParamDecl SelfDecl(getContext(), const_cast<BlockDecl *>(blockDecl), |
1484 | SourceLocation(), II, selfTy, |
1485 | ImplicitParamDecl::ObjCSelf); |
1486 | args.push_back(&SelfDecl); |
1487 | |
1488 | |
1489 | args.append(blockDecl->param_begin(), blockDecl->param_end()); |
1490 | |
1491 | |
1492 | const FunctionProtoType *fnType = blockInfo.getBlockExpr()->getFunctionType(); |
1493 | const CGFunctionInfo &fnInfo = |
1494 | CGM.getTypes().arrangeBlockFunctionDeclaration(fnType, args); |
1495 | if (CGM.ReturnSlotInterferesWithArgs(fnInfo)) |
1496 | blockInfo.UsesStret = true; |
1497 | |
1498 | llvm::FunctionType *fnLLVMType = CGM.getTypes().GetFunctionType(fnInfo); |
1499 | |
1500 | StringRef name = CGM.getBlockMangledName(GD, blockDecl); |
1501 | llvm::Function *fn = llvm::Function::Create( |
1502 | fnLLVMType, llvm::GlobalValue::InternalLinkage, name, &CGM.getModule()); |
1503 | CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo); |
1504 | |
1505 | if (BuildGlobalBlock) { |
1506 | auto GenVoidPtrTy = getContext().getLangOpts().OpenCL |
1507 | ? CGM.getOpenCLRuntime().getGenericVoidPointerType() |
1508 | : VoidPtrTy; |
1509 | buildGlobalBlock(CGM, blockInfo, |
1510 | llvm::ConstantExpr::getPointerCast(fn, GenVoidPtrTy)); |
1511 | } |
1512 | |
1513 | |
1514 | StartFunction(blockDecl, fnType->getReturnType(), fn, fnInfo, args, |
1515 | blockDecl->getLocation(), |
1516 | blockInfo.getBlockExpr()->getBody()->getBeginLoc()); |
1517 | |
1518 | |
1519 | |
1520 | |
1521 | |
1522 | llvm::Value *BlockPointerDbgLoc = BlockPointer; |
1523 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) { |
1524 | |
1525 | Address Alloca = CreateTempAlloca(BlockPointer->getType(), |
1526 | getPointerAlign(), |
1527 | "block.addr"); |
1528 | |
1529 | |
1530 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
1531 | Builder.CreateStore(BlockPointer, Alloca); |
1532 | BlockPointerDbgLoc = Alloca.getPointer(); |
1533 | } |
1534 | |
1535 | |
1536 | |
1537 | if (blockDecl->capturesCXXThis()) { |
1538 | Address addr = Builder.CreateStructGEP( |
1539 | LoadBlockStruct(), blockInfo.CXXThisIndex, "block.captured-this"); |
1540 | CXXThisValue = Builder.CreateLoad(addr, "this"); |
1541 | } |
1542 | |
1543 | |
1544 | for (const auto &CI : blockDecl->captures()) { |
1545 | const VarDecl *variable = CI.getVariable(); |
1546 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
1547 | if (!capture.isConstant()) continue; |
1548 | |
1549 | CharUnits align = getContext().getDeclAlign(variable); |
1550 | Address alloca = |
1551 | CreateMemTemp(variable->getType(), align, "block.captured-const"); |
1552 | |
1553 | Builder.CreateStore(capture.getConstant(), alloca); |
1554 | |
1555 | setAddrOfLocalVar(variable, alloca); |
1556 | } |
1557 | |
1558 | |
1559 | llvm::BasicBlock *entry = Builder.GetInsertBlock(); |
1560 | llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); |
1561 | --entry_ptr; |
1562 | |
1563 | if (IsLambdaConversionToBlock) |
1564 | EmitLambdaBlockInvokeBody(); |
1565 | else { |
1566 | PGO.assignRegionCounters(GlobalDecl(blockDecl), fn); |
1567 | incrementProfileCounter(blockDecl->getBody()); |
1568 | EmitStmt(blockDecl->getBody()); |
1569 | } |
1570 | |
1571 | |
1572 | llvm::BasicBlock *resume = Builder.GetInsertBlock(); |
1573 | |
1574 | |
1575 | ++entry_ptr; |
1576 | Builder.SetInsertPoint(entry, entry_ptr); |
1577 | |
1578 | |
1579 | |
1580 | if (CGDebugInfo *DI = getDebugInfo()) { |
1581 | for (const auto &CI : blockDecl->captures()) { |
1582 | const VarDecl *variable = CI.getVariable(); |
1583 | DI->EmitLocation(Builder, variable->getLocation()); |
1584 | |
1585 | if (CGM.getCodeGenOpts().hasReducedDebugInfo()) { |
1586 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
1587 | if (capture.isConstant()) { |
1588 | auto addr = LocalDeclMap.find(variable)->second; |
1589 | (void)DI->EmitDeclareOfAutoVariable(variable, addr.getPointer(), |
1590 | Builder); |
1591 | continue; |
1592 | } |
1593 | |
1594 | DI->EmitDeclareOfBlockDeclRefVariable( |
1595 | variable, BlockPointerDbgLoc, Builder, blockInfo, |
1596 | entry_ptr == entry->end() ? nullptr : &*entry_ptr); |
1597 | } |
1598 | } |
1599 | |
1600 | DI->EmitLocation(Builder, |
1601 | cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc()); |
1602 | } |
1603 | |
1604 | |
1605 | if (resume == nullptr) |
1606 | Builder.ClearInsertionPoint(); |
1607 | else |
1608 | Builder.SetInsertPoint(resume); |
1609 | |
1610 | FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc()); |
1611 | |
1612 | return fn; |
1613 | } |
1614 | |
1615 | static std::pair<BlockCaptureEntityKind, BlockFieldFlags> |
1616 | computeCopyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T, |
1617 | const LangOptions &LangOpts) { |
1618 | if (CI.getCopyExpr()) { |
1619 | assert(!CI.isByRef()); |
1620 | |
1621 | return std::make_pair(BlockCaptureEntityKind::CXXRecord, BlockFieldFlags()); |
1622 | } |
1623 | BlockFieldFlags Flags; |
1624 | if (CI.isEscapingByref()) { |
1625 | Flags = BLOCK_FIELD_IS_BYREF; |
1626 | if (T.isObjCGCWeak()) |
1627 | Flags |= BLOCK_FIELD_IS_WEAK; |
1628 | return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags); |
1629 | } |
1630 | |
1631 | Flags = BLOCK_FIELD_IS_OBJECT; |
1632 | bool isBlockPointer = T->isBlockPointerType(); |
1633 | if (isBlockPointer) |
1634 | Flags = BLOCK_FIELD_IS_BLOCK; |
1635 | |
1636 | switch (T.isNonTrivialToPrimitiveCopy()) { |
1637 | case QualType::PCK_Struct: |
1638 | return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct, |
1639 | BlockFieldFlags()); |
1640 | case QualType::PCK_ARCWeak: |
1641 | |
1642 | return std::make_pair(BlockCaptureEntityKind::ARCWeak, Flags); |
1643 | case QualType::PCK_ARCStrong: |
1644 | |
1645 | |
1646 | |
1647 | |
1648 | return std::make_pair(!isBlockPointer ? BlockCaptureEntityKind::ARCStrong |
1649 | : BlockCaptureEntityKind::BlockObject, |
1650 | Flags); |
1651 | case QualType::PCK_Trivial: |
1652 | case QualType::PCK_VolatileTrivial: { |
1653 | if (!T->isObjCRetainableType()) |
1654 | |
1655 | return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags()); |
1656 | |
1657 | |
1658 | Qualifiers QS = T.getQualifiers(); |
1659 | |
1660 | |
1661 | |
1662 | if (!QS.getObjCLifetime() && !LangOpts.ObjCAutoRefCount) |
1663 | return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags); |
1664 | |
1665 | |
1666 | return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags()); |
1667 | } |
1668 | } |
1669 | llvm_unreachable("after exhaustive PrimitiveCopyKind switch"); |
1670 | } |
1671 | |
1672 | static std::pair<BlockCaptureEntityKind, BlockFieldFlags> |
1673 | computeDestroyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T, |
1674 | const LangOptions &LangOpts); |
1675 | |
1676 | |
1677 | static void findBlockCapturedManagedEntities( |
1678 | const CGBlockInfo &BlockInfo, const LangOptions &LangOpts, |
1679 | SmallVectorImpl<BlockCaptureManagedEntity> &ManagedCaptures) { |
1680 | for (const auto &CI : BlockInfo.getBlockDecl()->captures()) { |
1681 | const VarDecl *Variable = CI.getVariable(); |
1682 | const CGBlockInfo::Capture &Capture = BlockInfo.getCapture(Variable); |
1683 | if (Capture.isConstant()) |
1684 | continue; |
1685 | |
1686 | QualType VT = Capture.fieldType(); |
1687 | auto CopyInfo = computeCopyInfoForBlockCapture(CI, VT, LangOpts); |
1688 | auto DisposeInfo = computeDestroyInfoForBlockCapture(CI, VT, LangOpts); |
1689 | if (CopyInfo.first != BlockCaptureEntityKind::None || |
1690 | DisposeInfo.first != BlockCaptureEntityKind::None) |
1691 | ManagedCaptures.emplace_back(CopyInfo.first, DisposeInfo.first, |
1692 | CopyInfo.second, DisposeInfo.second, CI, |
1693 | Capture); |
1694 | } |
1695 | |
1696 | |
1697 | llvm::sort(ManagedCaptures); |
1698 | } |
1699 | |
1700 | namespace { |
1701 | |
1702 | struct CallBlockRelease final : EHScopeStack::Cleanup { |
1703 | Address Addr; |
1704 | BlockFieldFlags FieldFlags; |
1705 | bool LoadBlockVarAddr, CanThrow; |
1706 | |
1707 | CallBlockRelease(Address Addr, BlockFieldFlags Flags, bool LoadValue, |
1708 | bool CT) |
1709 | : Addr(Addr), FieldFlags(Flags), LoadBlockVarAddr(LoadValue), |
1710 | CanThrow(CT) {} |
1711 | |
1712 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
1713 | llvm::Value *BlockVarAddr; |
1714 | if (LoadBlockVarAddr) { |
1715 | BlockVarAddr = CGF.Builder.CreateLoad(Addr); |
1716 | BlockVarAddr = CGF.Builder.CreateBitCast(BlockVarAddr, CGF.VoidPtrTy); |
1717 | } else { |
1718 | BlockVarAddr = Addr.getPointer(); |
1719 | } |
1720 | |
1721 | CGF.BuildBlockRelease(BlockVarAddr, FieldFlags, CanThrow); |
1722 | } |
1723 | }; |
1724 | } |
1725 | |
1726 | |
1727 | bool CodeGenFunction::cxxDestructorCanThrow(QualType T) { |
1728 | if (const auto *RD = T->getAsCXXRecordDecl()) |
1729 | if (const CXXDestructorDecl *DD = RD->getDestructor()) |
1730 | return DD->getType()->castAs<FunctionProtoType>()->canThrow(); |
1731 | return false; |
1732 | } |
1733 | |
1734 | |
1735 | static std::string getBlockCaptureStr(const BlockCaptureManagedEntity &E, |
1736 | CaptureStrKind StrKind, |
1737 | CharUnits BlockAlignment, |
1738 | CodeGenModule &CGM) { |
1739 | std::string Str; |
1740 | ASTContext &Ctx = CGM.getContext(); |
1741 | const BlockDecl::Capture &CI = *E.CI; |
1742 | QualType CaptureTy = CI.getVariable()->getType(); |
1743 | |
1744 | BlockCaptureEntityKind Kind; |
1745 | BlockFieldFlags Flags; |
1746 | |
1747 | |
1748 | |
1749 | assert((StrKind != CaptureStrKind::Merged || |
1750 | (E.CopyKind == E.DisposeKind && E.CopyFlags == E.DisposeFlags)) && |
1751 | "different operations and flags"); |
1752 | |
1753 | if (StrKind == CaptureStrKind::DisposeHelper) { |
1754 | Kind = E.DisposeKind; |
1755 | Flags = E.DisposeFlags; |
1756 | } else { |
1757 | Kind = E.CopyKind; |
1758 | Flags = E.CopyFlags; |
1759 | } |
1760 | |
1761 | switch (Kind) { |
1762 | case BlockCaptureEntityKind::CXXRecord: { |
1763 | Str += "c"; |
1764 | SmallString<256> TyStr; |
1765 | llvm::raw_svector_ostream Out(TyStr); |
1766 | CGM.getCXXABI().getMangleContext().mangleTypeName(CaptureTy, Out); |
1767 | Str += llvm::to_string(TyStr.size()) + TyStr.c_str(); |
1768 | break; |
1769 | } |
1770 | case BlockCaptureEntityKind::ARCWeak: |
1771 | Str += "w"; |
1772 | break; |
1773 | case BlockCaptureEntityKind::ARCStrong: |
1774 | Str += "s"; |
1775 | break; |
1776 | case BlockCaptureEntityKind::BlockObject: { |
1777 | const VarDecl *Var = CI.getVariable(); |
1778 | unsigned F = Flags.getBitMask(); |
1779 | if (F & BLOCK_FIELD_IS_BYREF) { |
1780 | Str += "r"; |
1781 | if (F & BLOCK_FIELD_IS_WEAK) |
1782 | Str += "w"; |
1783 | else { |
1784 | |
1785 | |
1786 | if (StrKind != CaptureStrKind::DisposeHelper) { |
1787 | if (Ctx.getBlockVarCopyInit(Var).canThrow()) |
1788 | Str += "c"; |
1789 | } |
1790 | if (StrKind != CaptureStrKind::CopyHelper) { |
1791 | if (CodeGenFunction::cxxDestructorCanThrow(CaptureTy)) |
1792 | Str += "d"; |
1793 | } |
1794 | } |
1795 | } else { |
1796 | assert((F & BLOCK_FIELD_IS_OBJECT) && "unexpected flag value"); |
1797 | if (F == BLOCK_FIELD_IS_BLOCK) |
1798 | Str += "b"; |
1799 | else |
1800 | Str += "o"; |
1801 | } |
1802 | break; |
1803 | } |
1804 | case BlockCaptureEntityKind::NonTrivialCStruct: { |
1805 | bool IsVolatile = CaptureTy.isVolatileQualified(); |
1806 | CharUnits Alignment = |
1807 | BlockAlignment.alignmentAtOffset(E.Capture->getOffset()); |
1808 | |
1809 | Str += "n"; |
1810 | std::string FuncStr; |
1811 | if (StrKind == CaptureStrKind::DisposeHelper) |
1812 | FuncStr = CodeGenFunction::getNonTrivialDestructorStr( |
1813 | CaptureTy, Alignment, IsVolatile, Ctx); |
1814 | else |
1815 | |
1816 | |
1817 | FuncStr = CodeGenFunction::getNonTrivialCopyConstructorStr( |
1818 | CaptureTy, Alignment, IsVolatile, Ctx); |
1819 | |
1820 | |
1821 | Str += llvm::to_string(FuncStr.size()) + "_" + FuncStr; |
1822 | break; |
1823 | } |
1824 | case BlockCaptureEntityKind::None: |
1825 | break; |
1826 | } |
1827 | |
1828 | return Str; |
1829 | } |
1830 | |
1831 | static std::string getCopyDestroyHelperFuncName( |
1832 | const SmallVectorImpl<BlockCaptureManagedEntity> &Captures, |
1833 | CharUnits BlockAlignment, CaptureStrKind StrKind, CodeGenModule &CGM) { |
1834 | assert((StrKind == CaptureStrKind::CopyHelper || |
1835 | StrKind == CaptureStrKind::DisposeHelper) && |
1836 | "unexpected CaptureStrKind"); |
1837 | std::string Name = StrKind == CaptureStrKind::CopyHelper |
1838 | ? "__copy_helper_block_" |
1839 | : "__destroy_helper_block_"; |
1840 | if (CGM.getLangOpts().Exceptions) |
1841 | Name += "e"; |
1842 | if (CGM.getCodeGenOpts().ObjCAutoRefCountExceptions) |
1843 | Name += "a"; |
1844 | Name += llvm::to_string(BlockAlignment.getQuantity()) + "_"; |
1845 | |
1846 | for (const BlockCaptureManagedEntity &E : Captures) { |
1847 | Name += llvm::to_string(E.Capture->getOffset().getQuantity()); |
1848 | Name += getBlockCaptureStr(E, StrKind, BlockAlignment, CGM); |
1849 | } |
1850 | |
1851 | return Name; |
1852 | } |
1853 | |
1854 | static void pushCaptureCleanup(BlockCaptureEntityKind CaptureKind, |
1855 | Address Field, QualType CaptureType, |
1856 | BlockFieldFlags Flags, bool ForCopyHelper, |
1857 | VarDecl *Var, CodeGenFunction &CGF) { |
1858 | bool EHOnly = ForCopyHelper; |
1859 | |
1860 | switch (CaptureKind) { |
1861 | case BlockCaptureEntityKind::CXXRecord: |
1862 | case BlockCaptureEntityKind::ARCWeak: |
1863 | case BlockCaptureEntityKind::NonTrivialCStruct: |
1864 | case BlockCaptureEntityKind::ARCStrong: { |
1865 | if (CaptureType.isDestructedType() && |
1866 | (!EHOnly || CGF.needsEHCleanup(CaptureType.isDestructedType()))) { |
1867 | CodeGenFunction::Destroyer *Destroyer = |
1868 | CaptureKind == BlockCaptureEntityKind::ARCStrong |
1869 | ? CodeGenFunction::destroyARCStrongImprecise |
1870 | : CGF.getDestroyer(CaptureType.isDestructedType()); |
1871 | CleanupKind Kind = |
1872 | EHOnly ? EHCleanup |
1873 | : CGF.getCleanupKind(CaptureType.isDestructedType()); |
1874 | CGF.pushDestroy(Kind, Field, CaptureType, Destroyer, Kind & EHCleanup); |
1875 | } |
1876 | break; |
1877 | } |
1878 | case BlockCaptureEntityKind::BlockObject: { |
1879 | if (!EHOnly || CGF.getLangOpts().Exceptions) { |
1880 | CleanupKind Kind = EHOnly ? EHCleanup : NormalAndEHCleanup; |
1881 | |
1882 | |
1883 | |
1884 | bool CanThrow = |
1885 | !ForCopyHelper && CGF.cxxDestructorCanThrow(CaptureType); |
1886 | CGF.enterByrefCleanup(Kind, Field, Flags, true, |
1887 | CanThrow); |
1888 | } |
1889 | break; |
1890 | } |
1891 | case BlockCaptureEntityKind::None: |
1892 | break; |
1893 | } |
1894 | } |
1895 | |
1896 | static void setBlockHelperAttributesVisibility(bool CapturesNonExternalType, |
1897 | llvm::Function *Fn, |
1898 | const CGFunctionInfo &FI, |
1899 | CodeGenModule &CGM) { |
1900 | if (CapturesNonExternalType) { |
1901 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); |
1902 | } else { |
1903 | Fn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
1904 | Fn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
1905 | CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Fn, false); |
1906 | CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Fn); |
1907 | } |
1908 | } |
1909 | |
1910 | |
1911 | |
1912 | |
1913 | |
1914 | |
1915 | |
1916 | |
1917 | llvm::Constant * |
1918 | CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) { |
1919 | SmallVector<BlockCaptureManagedEntity, 4> CopiedCaptures; |
1920 | findBlockCapturedManagedEntities(blockInfo, getLangOpts(), CopiedCaptures); |
1921 | std::string FuncName = |
1922 | getCopyDestroyHelperFuncName(CopiedCaptures, blockInfo.BlockAlign, |
1923 | CaptureStrKind::CopyHelper, CGM); |
1924 | |
1925 | if (llvm::GlobalValue *Func = CGM.getModule().getNamedValue(FuncName)) |
1926 | return llvm::ConstantExpr::getBitCast(Func, VoidPtrTy); |
1927 | |
1928 | ASTContext &C = getContext(); |
1929 | |
1930 | QualType ReturnTy = C.VoidTy; |
1931 | |
1932 | FunctionArgList args; |
1933 | ImplicitParamDecl DstDecl(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
1934 | args.push_back(&DstDecl); |
1935 | ImplicitParamDecl SrcDecl(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
1936 | args.push_back(&SrcDecl); |
1937 | |
1938 | const CGFunctionInfo &FI = |
1939 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args); |
1940 | |
1941 | |
1942 | |
1943 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); |
1944 | |
1945 | llvm::Function *Fn = |
1946 | llvm::Function::Create(LTy, llvm::GlobalValue::LinkOnceODRLinkage, |
1947 | FuncName, &CGM.getModule()); |
1948 | if (CGM.supportsCOMDAT()) |
1949 | Fn->setComdat(CGM.getModule().getOrInsertComdat(FuncName)); |
1950 | |
1951 | SmallVector<QualType, 2> ArgTys; |
1952 | ArgTys.push_back(C.VoidPtrTy); |
1953 | ArgTys.push_back(C.VoidPtrTy); |
1954 | |
1955 | setBlockHelperAttributesVisibility(blockInfo.CapturesNonExternalType, Fn, FI, |
1956 | CGM); |
1957 | StartFunction(GlobalDecl(), ReturnTy, Fn, FI, args); |
1958 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
1959 | |
1960 | llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); |
1961 | |
1962 | Address src = GetAddrOfLocalVar(&SrcDecl); |
1963 | src = Address(Builder.CreateLoad(src), blockInfo.BlockAlign); |
1964 | src = Builder.CreateBitCast(src, structPtrTy, "block.source"); |
1965 | |
1966 | Address dst = GetAddrOfLocalVar(&DstDecl); |
1967 | dst = Address(Builder.CreateLoad(dst), blockInfo.BlockAlign); |
1968 | dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest"); |
1969 | |
1970 | for (const auto &CopiedCapture : CopiedCaptures) { |
1971 | const BlockDecl::Capture &CI = *CopiedCapture.CI; |
1972 | const CGBlockInfo::Capture &capture = *CopiedCapture.Capture; |
1973 | QualType captureType = CI.getVariable()->getType(); |
1974 | BlockFieldFlags flags = CopiedCapture.CopyFlags; |
1975 | |
1976 | unsigned index = capture.getIndex(); |
1977 | Address srcField = Builder.CreateStructGEP(src, index); |
1978 | Address dstField = Builder.CreateStructGEP(dst, index); |
1979 | |
1980 | switch (CopiedCapture.CopyKind) { |
1981 | case BlockCaptureEntityKind::CXXRecord: |
1982 | |
1983 | assert(CI.getCopyExpr() && "copy expression for variable is missing"); |
1984 | EmitSynthesizedCXXCopyCtor(dstField, srcField, CI.getCopyExpr()); |
1985 | break; |
1986 | case BlockCaptureEntityKind::ARCWeak: |
1987 | EmitARCCopyWeak(dstField, srcField); |
1988 | break; |
1989 | case BlockCaptureEntityKind::NonTrivialCStruct: { |
1990 | |
1991 | |
1992 | QualType varType = CI.getVariable()->getType(); |
1993 | callCStructCopyConstructor(MakeAddrLValue(dstField, varType), |
1994 | MakeAddrLValue(srcField, varType)); |
1995 | break; |
1996 | } |
1997 | case BlockCaptureEntityKind::ARCStrong: { |
1998 | llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src"); |
1999 | |
2000 | |
2001 | |
2002 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) { |
2003 | auto *ty = cast<llvm::PointerType>(srcValue->getType()); |
2004 | llvm::Value *null = llvm::ConstantPointerNull::get(ty); |
2005 | Builder.CreateStore(null, dstField); |
2006 | EmitARCStoreStrongCall(dstField, srcValue, true); |
2007 | |
2008 | |
2009 | |
2010 | |
2011 | } else { |
2012 | EmitARCRetainNonBlock(srcValue); |
2013 | |
2014 | |
2015 | |
2016 | |
2017 | if (!needsEHCleanup(captureType.isDestructedType())) |
2018 | cast<llvm::Instruction>(dstField.getPointer())->eraseFromParent(); |
2019 | } |
2020 | break; |
2021 | } |
2022 | case BlockCaptureEntityKind::BlockObject: { |
2023 | llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src"); |
2024 | srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy); |
2025 | llvm::Value *dstAddr = |
2026 | Builder.CreateBitCast(dstField.getPointer(), VoidPtrTy); |
2027 | llvm::Value *args[] = { |
2028 | dstAddr, srcValue, llvm::ConstantInt::get(Int32Ty, flags.getBitMask()) |
2029 | }; |
2030 | |
2031 | if (CI.isByRef() && C.getBlockVarCopyInit(CI.getVariable()).canThrow()) |
2032 | EmitRuntimeCallOrInvoke(CGM.getBlockObjectAssign(), args); |
2033 | else |
2034 | EmitNounwindRuntimeCall(CGM.getBlockObjectAssign(), args); |
2035 | break; |
2036 | } |
2037 | case BlockCaptureEntityKind::None: |
2038 | continue; |
2039 | } |
2040 | |
2041 | |
2042 | |
2043 | pushCaptureCleanup(CopiedCapture.CopyKind, dstField, captureType, flags, |
2044 | true, CI.getVariable(), *this); |
2045 | } |
2046 | |
2047 | FinishFunction(); |
2048 | |
2049 | return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
2050 | } |
2051 | |
2052 | static BlockFieldFlags |
2053 | getBlockFieldFlagsForObjCObjectPointer(const BlockDecl::Capture &CI, |
2054 | QualType T) { |
2055 | BlockFieldFlags Flags = BLOCK_FIELD_IS_OBJECT; |
2056 | if (T->isBlockPointerType()) |
2057 | Flags = BLOCK_FIELD_IS_BLOCK; |
2058 | return Flags; |
2059 | } |
2060 | |
2061 | static std::pair<BlockCaptureEntityKind, BlockFieldFlags> |
2062 | computeDestroyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T, |
2063 | const LangOptions &LangOpts) { |
2064 | if (CI.isEscapingByref()) { |
2065 | BlockFieldFlags Flags = BLOCK_FIELD_IS_BYREF; |
2066 | if (T.isObjCGCWeak()) |
2067 | Flags |= BLOCK_FIELD_IS_WEAK; |
2068 | return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags); |
2069 | } |
2070 | |
2071 | switch (T.isDestructedType()) { |
2072 | case QualType::DK_cxx_destructor: |
2073 | return std::make_pair(BlockCaptureEntityKind::CXXRecord, BlockFieldFlags()); |
2074 | case QualType::DK_objc_strong_lifetime: |
2075 | |
2076 | |
2077 | return std::make_pair(BlockCaptureEntityKind::ARCStrong, |
2078 | getBlockFieldFlagsForObjCObjectPointer(CI, T)); |
2079 | case QualType::DK_objc_weak_lifetime: |
2080 | |
2081 | return std::make_pair(BlockCaptureEntityKind::ARCWeak, |
2082 | getBlockFieldFlagsForObjCObjectPointer(CI, T)); |
2083 | case QualType::DK_nontrivial_c_struct: |
2084 | return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct, |
2085 | BlockFieldFlags()); |
2086 | case QualType::DK_none: { |
2087 | |
2088 | if (T->isObjCRetainableType() && !T.getQualifiers().hasObjCLifetime() && |
2089 | !LangOpts.ObjCAutoRefCount) |
2090 | return std::make_pair(BlockCaptureEntityKind::BlockObject, |
2091 | getBlockFieldFlagsForObjCObjectPointer(CI, T)); |
2092 | |
2093 | return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags()); |
2094 | } |
2095 | } |
2096 | llvm_unreachable("after exhaustive DestructionKind switch"); |
2097 | } |
2098 | |
2099 | |
2100 | |
2101 | |
2102 | |
2103 | |
2104 | |
2105 | |
2106 | llvm::Constant * |
2107 | CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) { |
2108 | SmallVector<BlockCaptureManagedEntity, 4> DestroyedCaptures; |
2109 | findBlockCapturedManagedEntities(blockInfo, getLangOpts(), DestroyedCaptures); |
2110 | std::string FuncName = |
2111 | getCopyDestroyHelperFuncName(DestroyedCaptures, blockInfo.BlockAlign, |
2112 | CaptureStrKind::DisposeHelper, CGM); |
2113 | |
2114 | if (llvm::GlobalValue *Func = CGM.getModule().getNamedValue(FuncName)) |
2115 | return llvm::ConstantExpr::getBitCast(Func, VoidPtrTy); |
2116 | |
2117 | ASTContext &C = getContext(); |
2118 | |
2119 | QualType ReturnTy = C.VoidTy; |
2120 | |
2121 | FunctionArgList args; |
2122 | ImplicitParamDecl SrcDecl(C, C.VoidPtrTy, ImplicitParamDecl::Other); |
2123 | args.push_back(&SrcDecl); |
2124 | |
2125 | const CGFunctionInfo &FI = |
2126 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args); |
2127 | |
2128 | |
2129 | |
2130 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); |
2131 | |
2132 | llvm::Function *Fn = |
2133 | llvm::Function::Create(LTy, llvm::GlobalValue::LinkOnceODRLinkage, |
2134 | FuncName, &CGM.getModule()); |
2135 | if (CGM.supportsCOMDAT()) |
2136 | Fn->setComdat(CGM.getModule().getOrInsertComdat(FuncName)); |
2137 | |
2138 | SmallVector<QualType, 1> ArgTys; |
2139 | ArgTys.push_back(C.VoidPtrTy); |
2140 | |
2141 | setBlockHelperAttributesVisibility(blockInfo.CapturesNonExternalType, Fn, FI, |
2142 | CGM); |
2143 | StartFunction(GlobalDecl(), ReturnTy, Fn, FI, args); |
2144 | markAsIgnoreThreadCheckingAtRuntime(Fn); |
2145 | |
2146 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
2147 | |
2148 | llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); |
2149 | |
2150 | Address src = GetAddrOfLocalVar(&SrcDecl); |
2151 | src = Address(Builder.CreateLoad(src), blockInfo.BlockAlign); |
2152 | src = Builder.CreateBitCast(src, structPtrTy, "block"); |
2153 | |
2154 | CodeGenFunction::RunCleanupsScope cleanups(*this); |
2155 | |
2156 | for (const auto &DestroyedCapture : DestroyedCaptures) { |
2157 | const BlockDecl::Capture &CI = *DestroyedCapture.CI; |
2158 | const CGBlockInfo::Capture &capture = *DestroyedCapture.Capture; |
2159 | BlockFieldFlags flags = DestroyedCapture.DisposeFlags; |
2160 | |
2161 | Address srcField = Builder.CreateStructGEP(src, capture.getIndex()); |
2162 | |
2163 | pushCaptureCleanup(DestroyedCapture.DisposeKind, srcField, |
2164 | CI.getVariable()->getType(), flags, |
2165 | false, CI.getVariable(), *this); |
2166 | } |
2167 | |
2168 | cleanups.ForceCleanup(); |
2169 | |
2170 | FinishFunction(); |
2171 | |
2172 | return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); |
2173 | } |
2174 | |
2175 | namespace { |
2176 | |
2177 | |
2178 | class ObjectByrefHelpers final : public BlockByrefHelpers { |
2179 | BlockFieldFlags Flags; |
2180 | |
2181 | public: |
2182 | ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags) |
2183 | : BlockByrefHelpers(alignment), Flags(flags) {} |
2184 | |
2185 | void emitCopy(CodeGenFunction &CGF, Address destField, |
2186 | Address srcField) override { |
2187 | destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy); |
2188 | |
2189 | srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy); |
2190 | llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField); |
2191 | |
2192 | unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask(); |
2193 | |
2194 | llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags); |
2195 | llvm::FunctionCallee fn = CGF.CGM.getBlockObjectAssign(); |
2196 | |
2197 | llvm::Value *args[] = { destField.getPointer(), srcValue, flagsVal }; |
2198 | CGF.EmitNounwindRuntimeCall(fn, args); |
2199 | } |
2200 | |
2201 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
2202 | field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0)); |
2203 | llvm::Value *value = CGF.Builder.CreateLoad(field); |
2204 | |
2205 | CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER, false); |
2206 | } |
2207 | |
2208 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
2209 | id.AddInteger(Flags.getBitMask()); |
2210 | } |
2211 | }; |
2212 | |
2213 | |
2214 | class ARCWeakByrefHelpers final : public BlockByrefHelpers { |
2215 | public: |
2216 | ARCWeakByrefHelpers(CharUnits alignment) : BlockByrefHelpers(alignment) {} |
2217 | |
2218 | void emitCopy(CodeGenFunction &CGF, Address destField, |
2219 | Address srcField) override { |
2220 | CGF.EmitARCMoveWeak(destField, srcField); |
2221 | } |
2222 | |
2223 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
2224 | CGF.EmitARCDestroyWeak(field); |
2225 | } |
2226 | |
2227 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
2228 | |
2229 | id.AddInteger(0); |
2230 | } |
2231 | }; |
2232 | |
2233 | |
2234 | |
2235 | class ARCStrongByrefHelpers final : public BlockByrefHelpers { |
2236 | public: |
2237 | ARCStrongByrefHelpers(CharUnits alignment) : BlockByrefHelpers(alignment) {} |
2238 | |
2239 | void emitCopy(CodeGenFunction &CGF, Address destField, |
2240 | Address srcField) override { |
2241 | |
2242 | |
2243 | |
2244 | llvm::Value *value = CGF.Builder.CreateLoad(srcField); |
2245 | |
2246 | llvm::Value *null = |
2247 | llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType())); |
2248 | |
2249 | if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) { |
2250 | CGF.Builder.CreateStore(null, destField); |
2251 | CGF.EmitARCStoreStrongCall(destField, value, true); |
2252 | CGF.EmitARCStoreStrongCall(srcField, null, true); |
2253 | return; |
2254 | } |
2255 | CGF.Builder.CreateStore(value, destField); |
2256 | CGF.Builder.CreateStore(null, srcField); |
2257 | } |
2258 | |
2259 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
2260 | CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime); |
2261 | } |
2262 | |
2263 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
2264 | |
2265 | id.AddInteger(1); |
2266 | } |
2267 | }; |
2268 | |
2269 | |
2270 | |
2271 | class ARCStrongBlockByrefHelpers final : public BlockByrefHelpers { |
2272 | public: |
2273 | ARCStrongBlockByrefHelpers(CharUnits alignment) |
2274 | : BlockByrefHelpers(alignment) {} |
2275 | |
2276 | void emitCopy(CodeGenFunction &CGF, Address destField, |
2277 | Address srcField) override { |
2278 | |
2279 | |
2280 | |
2281 | llvm::Value *oldValue = CGF.Builder.CreateLoad(srcField); |
2282 | llvm::Value *copy = CGF.EmitARCRetainBlock(oldValue, true); |
2283 | CGF.Builder.CreateStore(copy, destField); |
2284 | } |
2285 | |
2286 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
2287 | CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime); |
2288 | } |
2289 | |
2290 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
2291 | |
2292 | id.AddInteger(2); |
2293 | } |
2294 | }; |
2295 | |
2296 | |
2297 | |
2298 | class CXXByrefHelpers final : public BlockByrefHelpers { |
2299 | QualType VarType; |
2300 | const Expr *CopyExpr; |
2301 | |
2302 | public: |
2303 | CXXByrefHelpers(CharUnits alignment, QualType type, |
2304 | const Expr *copyExpr) |
2305 | : BlockByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {} |
2306 | |
2307 | bool needsCopy() const override { return CopyExpr != nullptr; } |
2308 | void emitCopy(CodeGenFunction &CGF, Address destField, |
2309 | Address srcField) override { |
2310 | if (!CopyExpr) return; |
2311 | CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr); |
2312 | } |
2313 | |
2314 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
2315 | EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin(); |
2316 | CGF.PushDestructorCleanup(VarType, field); |
2317 | CGF.PopCleanupBlocks(cleanupDepth); |
2318 | } |
2319 | |
2320 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
2321 | id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr()); |
2322 | } |
2323 | }; |
2324 | |
2325 | |
2326 | |
2327 | class NonTrivialCStructByrefHelpers final : public BlockByrefHelpers { |
2328 | QualType VarType; |
2329 | |
2330 | public: |
2331 | NonTrivialCStructByrefHelpers(CharUnits alignment, QualType type) |
2332 | : BlockByrefHelpers(alignment), VarType(type) {} |
2333 | |
2334 | void emitCopy(CodeGenFunction &CGF, Address destField, |
2335 | Address srcField) override { |
2336 | CGF.callCStructMoveConstructor(CGF.MakeAddrLValue(destField, VarType), |
2337 | CGF.MakeAddrLValue(srcField, VarType)); |
2338 | } |
2339 | |
2340 | bool needsDispose() const override { |
2341 | return VarType.isDestructedType(); |
2342 | } |
2343 | |
2344 | void emitDispose(CodeGenFunction &CGF, Address field) override { |
2345 | EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin(); |
2346 | CGF.pushDestroy(VarType.isDestructedType(), field, VarType); |
2347 | CGF.PopCleanupBlocks(cleanupDepth); |
2348 | } |
2349 | |
2350 | void profileImpl(llvm::FoldingSetNodeID &id) const override { |
2351 | id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr()); |
2352 | } |
2353 | }; |
2354 | } |
2355 | |
2356 | static llvm::Constant * |
2357 | generateByrefCopyHelper(CodeGenFunction &CGF, const BlockByrefInfo &byrefInfo, |
2358 | BlockByrefHelpers &generator) { |
2359 | ASTContext &Context = CGF.getContext(); |
2360 | |
2361 | QualType ReturnTy = Context.VoidTy; |
2362 | |
2363 | FunctionArgList args; |
2364 | ImplicitParamDecl Dst(Context, Context.VoidPtrTy, ImplicitParamDecl::Other); |
2365 | args.push_back(&Dst); |
2366 | |
2367 | ImplicitParamDecl Src(Context, Context.VoidPtrTy, ImplicitParamDecl::Other); |
2368 | args.push_back(&Src); |
2369 | |
2370 | const CGFunctionInfo &FI = |
2371 | CGF.CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args); |
2372 | |
2373 | llvm::FunctionType *LTy = CGF.CGM.getTypes().GetFunctionType(FI); |
2374 | |
2375 | |
2376 | |
2377 | llvm::Function *Fn = |
2378 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
2379 | "__Block_byref_object_copy_", &CGF.CGM.getModule()); |
2380 | |
2381 | SmallVector<QualType, 2> ArgTys; |
2382 | ArgTys.push_back(Context.VoidPtrTy); |
2383 | ArgTys.push_back(Context.VoidPtrTy); |
2384 | |
2385 | CGF.CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); |
2386 | |
2387 | CGF.StartFunction(GlobalDecl(), ReturnTy, Fn, FI, args); |
2388 | |
2389 | auto AL = ApplyDebugLocation::CreateArtificial(CGF); |
2390 | |
2391 | if (generator.needsCopy()) { |
2392 | llvm::Type *byrefPtrType = byrefInfo.Type->getPointerTo(0); |
2393 | |
2394 | |
2395 | Address destField = CGF.GetAddrOfLocalVar(&Dst); |
2396 | destField = Address(CGF.Builder.CreateLoad(destField), |
2397 | byrefInfo.ByrefAlignment); |
2398 | destField = CGF.Builder.CreateBitCast(destField, byrefPtrType); |
2399 | destField = CGF.emitBlockByrefAddress(destField, byrefInfo, false, |
2400 | "dest-object"); |
2401 | |
2402 | |
2403 | Address srcField = CGF.GetAddrOfLocalVar(&Src); |
2404 | srcField = Address(CGF.Builder.CreateLoad(srcField), |
2405 | byrefInfo.ByrefAlignment); |
2406 | srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType); |
2407 | srcField = CGF.emitBlockByrefAddress(srcField, byrefInfo, false, |
2408 | "src-object"); |
2409 | |
2410 | generator.emitCopy(CGF, destField, srcField); |
2411 | } |
2412 | |
2413 | CGF.FinishFunction(); |
2414 | |
2415 | return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy); |
2416 | } |
2417 | |
2418 | |
2419 | static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM, |
2420 | const BlockByrefInfo &byrefInfo, |
2421 | BlockByrefHelpers &generator) { |
2422 | CodeGenFunction CGF(CGM); |
2423 | return generateByrefCopyHelper(CGF, byrefInfo, generator); |
2424 | } |
2425 | |
2426 | |
2427 | static llvm::Constant * |
2428 | generateByrefDisposeHelper(CodeGenFunction &CGF, |
2429 | const BlockByrefInfo &byrefInfo, |
2430 | BlockByrefHelpers &generator) { |
2431 | ASTContext &Context = CGF.getContext(); |
2432 | QualType R = Context.VoidTy; |
2433 | |
2434 | FunctionArgList args; |
2435 | ImplicitParamDecl Src(CGF.getContext(), Context.VoidPtrTy, |
2436 | ImplicitParamDecl::Other); |
2437 | args.push_back(&Src); |
2438 | |
2439 | const CGFunctionInfo &FI = |
2440 | CGF.CGM.getTypes().arrangeBuiltinFunctionDeclaration(R, args); |
2441 | |
2442 | llvm::FunctionType *LTy = CGF.CGM.getTypes().GetFunctionType(FI); |
2443 | |
2444 | |
2445 | |
2446 | llvm::Function *Fn = |
2447 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
2448 | "__Block_byref_object_dispose_", |
2449 | &CGF.CGM.getModule()); |
2450 | |
2451 | SmallVector<QualType, 1> ArgTys; |
2452 | ArgTys.push_back(Context.VoidPtrTy); |
2453 | |
2454 | CGF.CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); |
2455 | |
2456 | CGF.StartFunction(GlobalDecl(), R, Fn, FI, args); |
2457 | |
2458 | auto AL = ApplyDebugLocation::CreateArtificial(CGF); |
2459 | |
2460 | if (generator.needsDispose()) { |
2461 | Address addr = CGF.GetAddrOfLocalVar(&Src); |
2462 | addr = Address(CGF.Builder.CreateLoad(addr), byrefInfo.ByrefAlignment); |
2463 | auto byrefPtrType = byrefInfo.Type->getPointerTo(0); |
2464 | addr = CGF.Builder.CreateBitCast(addr, byrefPtrType); |
2465 | addr = CGF.emitBlockByrefAddress(addr, byrefInfo, false, "object"); |
2466 | |
2467 | generator.emitDispose(CGF, addr); |
2468 | } |
2469 | |
2470 | CGF.FinishFunction(); |
2471 | |
2472 | return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy); |
2473 | } |
2474 | |
2475 | |
2476 | static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM, |
2477 | const BlockByrefInfo &byrefInfo, |
2478 | BlockByrefHelpers &generator) { |
2479 | CodeGenFunction CGF(CGM); |
2480 | return generateByrefDisposeHelper(CGF, byrefInfo, generator); |
2481 | } |
2482 | |
2483 | |
2484 | |
2485 | template <class T> |
2486 | static T *buildByrefHelpers(CodeGenModule &CGM, const BlockByrefInfo &byrefInfo, |
2487 | T &&generator) { |
2488 | llvm::FoldingSetNodeID id; |
2489 | generator.Profile(id); |
2490 | |
2491 | void *insertPos; |
2492 | BlockByrefHelpers *node |
2493 | = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos); |
2494 | if (node) return static_cast<T*>(node); |
2495 | |
2496 | generator.CopyHelper = buildByrefCopyHelper(CGM, byrefInfo, generator); |
2497 | generator.DisposeHelper = buildByrefDisposeHelper(CGM, byrefInfo, generator); |
2498 | |
2499 | T *copy = new (CGM.getContext()) T(std::forward<T>(generator)); |
2500 | CGM.ByrefHelpersCache.InsertNode(copy, insertPos); |
2501 | return copy; |
2502 | } |
2503 | |
2504 | |
2505 | |
2506 | |
2507 | BlockByrefHelpers * |
2508 | CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType, |
2509 | const AutoVarEmission &emission) { |
2510 | const VarDecl &var = *emission.Variable; |
2511 | assert(var.isEscapingByref() && |
2512 | "only escaping __block variables need byref helpers"); |
2513 | |
2514 | QualType type = var.getType(); |
2515 | |
2516 | auto &byrefInfo = getBlockByrefInfo(&var); |
2517 | |
2518 | |
2519 | |
2520 | CharUnits valueAlignment = |
2521 | byrefInfo.ByrefAlignment.alignmentAtOffset(byrefInfo.FieldOffset); |
2522 | |
2523 | if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) { |
2524 | const Expr *copyExpr = |
2525 | CGM.getContext().getBlockVarCopyInit(&var).getCopyExpr(); |
2526 | if (!copyExpr && record->hasTrivialDestructor()) return nullptr; |
2527 | |
2528 | return ::buildByrefHelpers( |
2529 | CGM, byrefInfo, CXXByrefHelpers(valueAlignment, type, copyExpr)); |
2530 | } |
2531 | |
2532 | |
2533 | |
2534 | if (type.isNonTrivialToPrimitiveDestructiveMove() == QualType::PCK_Struct || |
2535 | type.isDestructedType() == QualType::DK_nontrivial_c_struct) |
2536 | return ::buildByrefHelpers( |
2537 | CGM, byrefInfo, NonTrivialCStructByrefHelpers(valueAlignment, type)); |
2538 | |
2539 | |
2540 | |
2541 | if (!type->isObjCRetainableType()) return nullptr; |
2542 | |
2543 | Qualifiers qs = type.getQualifiers(); |
2544 | |
2545 | |
2546 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
2547 | switch (lifetime) { |
2548 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
2549 | |
2550 | |
2551 | case Qualifiers::OCL_ExplicitNone: |
2552 | case Qualifiers::OCL_Autoreleasing: |
2553 | return nullptr; |
2554 | |
2555 | |
2556 | |
2557 | case Qualifiers::OCL_Weak: |
2558 | return ::buildByrefHelpers(CGM, byrefInfo, |
2559 | ARCWeakByrefHelpers(valueAlignment)); |
2560 | |
2561 | |
2562 | case Qualifiers::OCL_Strong: |
2563 | |
2564 | |
2565 | if (type->isBlockPointerType()) { |
2566 | return ::buildByrefHelpers(CGM, byrefInfo, |
2567 | ARCStrongBlockByrefHelpers(valueAlignment)); |
2568 | |
2569 | |
2570 | |
2571 | } else { |
2572 | return ::buildByrefHelpers(CGM, byrefInfo, |
2573 | ARCStrongByrefHelpers(valueAlignment)); |
2574 | } |
2575 | } |
2576 | llvm_unreachable("fell out of lifetime switch!"); |
2577 | } |
2578 | |
2579 | BlockFieldFlags flags; |
2580 | if (type->isBlockPointerType()) { |
2581 | flags |= BLOCK_FIELD_IS_BLOCK; |
2582 | } else if (CGM.getContext().isObjCNSObjectType(type) || |
2583 | type->isObjCObjectPointerType()) { |
2584 | flags |= BLOCK_FIELD_IS_OBJECT; |
2585 | } else { |
2586 | return nullptr; |
2587 | } |
2588 | |
2589 | if (type.isObjCGCWeak()) |
2590 | flags |= BLOCK_FIELD_IS_WEAK; |
2591 | |
2592 | return ::buildByrefHelpers(CGM, byrefInfo, |
2593 | ObjectByrefHelpers(valueAlignment, flags)); |
2594 | } |
2595 | |
2596 | Address CodeGenFunction::emitBlockByrefAddress(Address baseAddr, |
2597 | const VarDecl *var, |
2598 | bool followForward) { |
2599 | auto &info = getBlockByrefInfo(var); |
2600 | return emitBlockByrefAddress(baseAddr, info, followForward, var->getName()); |
2601 | } |
2602 | |
2603 | Address CodeGenFunction::emitBlockByrefAddress(Address baseAddr, |
2604 | const BlockByrefInfo &info, |
2605 | bool followForward, |
2606 | const llvm::Twine &name) { |
2607 | |
2608 | if (followForward) { |
2609 | Address forwardingAddr = Builder.CreateStructGEP(baseAddr, 1, "forwarding"); |
2610 | baseAddr = Address(Builder.CreateLoad(forwardingAddr), info.ByrefAlignment); |
2611 | } |
2612 | |
2613 | return Builder.CreateStructGEP(baseAddr, info.FieldIndex, name); |
2614 | } |
2615 | |
2616 | |
2617 | |
2618 | |
2619 | |
2620 | |
2621 | |
2622 | |
2623 | |
2624 | |
2625 | |
2626 | |
2627 | |
2628 | |
2629 | |
2630 | |
2631 | const BlockByrefInfo &CodeGenFunction::getBlockByrefInfo(const VarDecl *D) { |
2632 | auto it = BlockByrefInfos.find(D); |
2633 | if (it != BlockByrefInfos.end()) |
2634 | return it->second; |
2635 | |
2636 | llvm::StructType *byrefType = |
2637 | llvm::StructType::create(getLLVMContext(), |
2638 | "struct.__block_byref_" + D->getNameAsString()); |
2639 | |
2640 | QualType Ty = D->getType(); |
2641 | |
2642 | CharUnits size; |
2643 | SmallVector<llvm::Type *, 8> types; |
2644 | |
2645 | |
2646 | types.push_back(Int8PtrTy); |
2647 | size += getPointerSize(); |
2648 | |
2649 | |
2650 | types.push_back(llvm::PointerType::getUnqual(byrefType)); |
2651 | size += getPointerSize(); |
2652 | |
2653 | |
2654 | types.push_back(Int32Ty); |
2655 | size += CharUnits::fromQuantity(4); |
2656 | |
2657 | |
2658 | types.push_back(Int32Ty); |
2659 | size += CharUnits::fromQuantity(4); |
2660 | |
2661 | |
2662 | bool hasCopyAndDispose = getContext().BlockRequiresCopying(Ty, D); |
2663 | if (hasCopyAndDispose) { |
2664 | |
2665 | types.push_back(Int8PtrTy); |
2666 | size += getPointerSize(); |
2667 | |
2668 | |
2669 | types.push_back(Int8PtrTy); |
2670 | size += getPointerSize(); |
2671 | } |
2672 | |
2673 | bool HasByrefExtendedLayout = false; |
2674 | Qualifiers::ObjCLifetime Lifetime = Qualifiers::OCL_None; |
2675 | if (getContext().getByrefLifetime(Ty, Lifetime, HasByrefExtendedLayout) && |
2676 | HasByrefExtendedLayout) { |
2677 | |
2678 | types.push_back(Int8PtrTy); |
2679 | size += CharUnits::fromQuantity(PointerSizeInBytes); |
2680 | } |
2681 | |
2682 | |
2683 | llvm::Type *varTy = ConvertTypeForMem(Ty); |
2684 | |
2685 | bool packed = false; |
2686 | CharUnits varAlign = getContext().getDeclAlign(D); |
2687 | CharUnits varOffset = size.alignTo(varAlign); |
2688 | |
2689 | |
2690 | if (varOffset != size) { |
2691 | llvm::Type *paddingTy = |
2692 | llvm::ArrayType::get(Int8Ty, (varOffset - size).getQuantity()); |
2693 | |
2694 | types.push_back(paddingTy); |
2695 | size = varOffset; |
2696 | |
2697 | |
2698 | } else if (CGM.getDataLayout().getABITypeAlignment(varTy) |
2699 | > varAlign.getQuantity()) { |
2700 | packed = true; |
2701 | } |
2702 | types.push_back(varTy); |
2703 | |
2704 | byrefType->setBody(types, packed); |
2705 | |
2706 | BlockByrefInfo info; |
2707 | info.Type = byrefType; |
2708 | info.FieldIndex = types.size() - 1; |
2709 | info.FieldOffset = varOffset; |
2710 | info.ByrefAlignment = std::max(varAlign, getPointerAlign()); |
2711 | |
2712 | auto pair = BlockByrefInfos.insert({D, info}); |
2713 | assert(pair.second && "info was inserted recursively?"); |
2714 | return pair.first->second; |
2715 | } |
2716 | |
2717 | |
2718 | |
2719 | void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) { |
2720 | |
2721 | Address addr = emission.Addr; |
2722 | |
2723 | |
2724 | llvm::StructType *byrefType = cast<llvm::StructType>( |
2725 | cast<llvm::PointerType>(addr.getPointer()->getType())->getElementType()); |
2726 | |
2727 | unsigned nextHeaderIndex = 0; |
2728 | CharUnits nextHeaderOffset; |
2729 | auto storeHeaderField = [&](llvm::Value *value, CharUnits fieldSize, |
2730 | const Twine &name) { |
2731 | auto fieldAddr = Builder.CreateStructGEP(addr, nextHeaderIndex, name); |
2732 | Builder.CreateStore(value, fieldAddr); |
2733 | |
2734 | nextHeaderIndex++; |
2735 | nextHeaderOffset += fieldSize; |
2736 | }; |
2737 | |
2738 | |
2739 | BlockByrefHelpers *helpers = buildByrefHelpers(*byrefType, emission); |
2740 | |
2741 | const VarDecl &D = *emission.Variable; |
2742 | QualType type = D.getType(); |
2743 | |
2744 | bool HasByrefExtendedLayout = false; |
2745 | Qualifiers::ObjCLifetime ByrefLifetime = Qualifiers::OCL_None; |
2746 | bool ByRefHasLifetime = |
2747 | getContext().getByrefLifetime(type, ByrefLifetime, HasByrefExtendedLayout); |
2748 | |
2749 | llvm::Value *V; |
2750 | |
2751 | |
2752 | int isa = 0; |
2753 | if (type.isObjCGCWeak()) |
2754 | isa = 1; |
2755 | V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa"); |
2756 | storeHeaderField(V, getPointerSize(), "byref.isa"); |
2757 | |
2758 | |
2759 | storeHeaderField(addr.getPointer(), getPointerSize(), "byref.forwarding"); |
2760 | |
2761 | |
2762 | |
2763 | |
2764 | BlockFlags flags; |
2765 | if (helpers) flags |= BLOCK_BYREF_HAS_COPY_DISPOSE; |
2766 | if (ByRefHasLifetime) { |
2767 | if (HasByrefExtendedLayout) flags |= BLOCK_BYREF_LAYOUT_EXTENDED; |
2768 | else switch (ByrefLifetime) { |
2769 | case Qualifiers::OCL_Strong: |
2770 | flags |= BLOCK_BYREF_LAYOUT_STRONG; |
2771 | break; |
2772 | case Qualifiers::OCL_Weak: |
2773 | flags |= BLOCK_BYREF_LAYOUT_WEAK; |
2774 | break; |
2775 | case Qualifiers::OCL_ExplicitNone: |
2776 | flags |= BLOCK_BYREF_LAYOUT_UNRETAINED; |
2777 | break; |
2778 | case Qualifiers::OCL_None: |
2779 | if (!type->isObjCObjectPointerType() && !type->isBlockPointerType()) |
2780 | flags |= BLOCK_BYREF_LAYOUT_NON_OBJECT; |
2781 | break; |
2782 | default: |
2783 | break; |
2784 | } |
2785 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
2786 | printf("\n Inline flag for BYREF variable layout (%d):", flags.getBitMask()); |
2787 | if (flags & BLOCK_BYREF_HAS_COPY_DISPOSE) |
2788 | printf(" BLOCK_BYREF_HAS_COPY_DISPOSE"); |
2789 | if (flags & BLOCK_BYREF_LAYOUT_MASK) { |
2790 | BlockFlags ThisFlag(flags.getBitMask() & BLOCK_BYREF_LAYOUT_MASK); |
2791 | if (ThisFlag == BLOCK_BYREF_LAYOUT_EXTENDED) |
2792 | printf(" BLOCK_BYREF_LAYOUT_EXTENDED"); |
2793 | if (ThisFlag == BLOCK_BYREF_LAYOUT_STRONG) |
2794 | printf(" BLOCK_BYREF_LAYOUT_STRONG"); |
2795 | if (ThisFlag == BLOCK_BYREF_LAYOUT_WEAK) |
2796 | printf(" BLOCK_BYREF_LAYOUT_WEAK"); |
2797 | if (ThisFlag == BLOCK_BYREF_LAYOUT_UNRETAINED) |
2798 | printf(" BLOCK_BYREF_LAYOUT_UNRETAINED"); |
2799 | if (ThisFlag == BLOCK_BYREF_LAYOUT_NON_OBJECT) |
2800 | printf(" BLOCK_BYREF_LAYOUT_NON_OBJECT"); |
2801 | } |
2802 | printf("\n"); |
2803 | } |
2804 | } |
2805 | storeHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()), |
2806 | getIntSize(), "byref.flags"); |
2807 | |
2808 | CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType); |
2809 | V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity()); |
2810 | storeHeaderField(V, getIntSize(), "byref.size"); |
2811 | |
2812 | if (helpers) { |
2813 | storeHeaderField(helpers->CopyHelper, getPointerSize(), |
2814 | "byref.copyHelper"); |
2815 | storeHeaderField(helpers->DisposeHelper, getPointerSize(), |
2816 | "byref.disposeHelper"); |
2817 | } |
2818 | |
2819 | if (ByRefHasLifetime && HasByrefExtendedLayout) { |
2820 | auto layoutInfo = CGM.getObjCRuntime().BuildByrefLayout(CGM, type); |
2821 | storeHeaderField(layoutInfo, getPointerSize(), "byref.layout"); |
2822 | } |
2823 | } |
2824 | |
2825 | void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags, |
2826 | bool CanThrow) { |
2827 | llvm::FunctionCallee F = CGM.getBlockObjectDispose(); |
2828 | llvm::Value *args[] = { |
2829 | Builder.CreateBitCast(V, Int8PtrTy), |
2830 | llvm::ConstantInt::get(Int32Ty, flags.getBitMask()) |
2831 | }; |
2832 | |
2833 | if (CanThrow) |
2834 | EmitRuntimeCallOrInvoke(F, args); |
2835 | else |
2836 | EmitNounwindRuntimeCall(F, args); |
2837 | } |
2838 | |
2839 | void CodeGenFunction::enterByrefCleanup(CleanupKind Kind, Address Addr, |
2840 | BlockFieldFlags Flags, |
2841 | bool LoadBlockVarAddr, bool CanThrow) { |
2842 | EHStack.pushCleanup<CallBlockRelease>(Kind, Addr, Flags, LoadBlockVarAddr, |
2843 | CanThrow); |
2844 | } |
2845 | |
2846 | |
2847 | static void configureBlocksRuntimeObject(CodeGenModule &CGM, |
2848 | llvm::Constant *C) { |
2849 | auto *GV = cast<llvm::GlobalValue>(C->stripPointerCasts()); |
2850 | |
2851 | if (CGM.getTarget().getTriple().isOSBinFormatCOFF()) { |
2852 | IdentifierInfo &II = CGM.getContext().Idents.get(C->getName()); |
2853 | TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl(); |
2854 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
2855 | |
2856 | assert((isa<llvm::Function>(C->stripPointerCasts()) || |
2857 | isa<llvm::GlobalVariable>(C->stripPointerCasts())) && |
2858 | "expected Function or GlobalVariable"); |
2859 | |
2860 | const NamedDecl *ND = nullptr; |
2861 | for (const auto *Result : DC->lookup(&II)) |
2862 | if ((ND = dyn_cast<FunctionDecl>(Result)) || |
2863 | (ND = dyn_cast<VarDecl>(Result))) |
2864 | break; |
2865 | |
2866 | |
2867 | if (GV->isDeclaration() && (!ND || !ND->hasAttr<DLLExportAttr>())) { |
2868 | GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
2869 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
2870 | } else { |
2871 | GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
2872 | GV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
2873 | } |
2874 | } |
2875 | |
2876 | if (CGM.getLangOpts().BlocksRuntimeOptional && GV->isDeclaration() && |
2877 | GV->hasExternalLinkage()) |
2878 | GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
2879 | |
2880 | CGM.setDSOLocal(GV); |
2881 | } |
2882 | |
2883 | llvm::FunctionCallee CodeGenModule::getBlockObjectDispose() { |
2884 | if (BlockObjectDispose) |
2885 | return BlockObjectDispose; |
2886 | |
2887 | llvm::Type *args[] = { Int8PtrTy, Int32Ty }; |
2888 | llvm::FunctionType *fty |
2889 | = llvm::FunctionType::get(VoidTy, args, false); |
2890 | BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose"); |
2891 | configureBlocksRuntimeObject( |
2892 | *this, cast<llvm::Constant>(BlockObjectDispose.getCallee())); |
2893 | return BlockObjectDispose; |
2894 | } |
2895 | |
2896 | llvm::FunctionCallee CodeGenModule::getBlockObjectAssign() { |
2897 | if (BlockObjectAssign) |
2898 | return BlockObjectAssign; |
2899 | |
2900 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty }; |
2901 | llvm::FunctionType *fty |
2902 | = llvm::FunctionType::get(VoidTy, args, false); |
2903 | BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign"); |
2904 | configureBlocksRuntimeObject( |
2905 | *this, cast<llvm::Constant>(BlockObjectAssign.getCallee())); |
2906 | return BlockObjectAssign; |
2907 | } |
2908 | |
2909 | llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() { |
2910 | if (NSConcreteGlobalBlock) |
2911 | return NSConcreteGlobalBlock; |
2912 | |
2913 | NSConcreteGlobalBlock = |
2914 | GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock", Int8PtrTy, 0, nullptr); |
2915 | configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock); |
2916 | return NSConcreteGlobalBlock; |
2917 | } |
2918 | |
2919 | llvm::Constant *CodeGenModule::getNSConcreteStackBlock() { |
2920 | if (NSConcreteStackBlock) |
2921 | return NSConcreteStackBlock; |
2922 | |
2923 | NSConcreteStackBlock = |
2924 | GetOrCreateLLVMGlobal("_NSConcreteStackBlock", Int8PtrTy, 0, nullptr); |
2925 | configureBlocksRuntimeObject(*this, NSConcreteStackBlock); |
2926 | return NSConcreteStackBlock; |
2927 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #ifndef LLVM_CLANG_AST_DECL_H |
14 | #define LLVM_CLANG_AST_DECL_H |
15 | |
16 | #include "clang/AST/APValue.h" |
17 | #include "clang/AST/ASTContextAllocate.h" |
18 | #include "clang/AST/DeclAccessPair.h" |
19 | #include "clang/AST/DeclBase.h" |
20 | #include "clang/AST/DeclarationName.h" |
21 | #include "clang/AST/ExternalASTSource.h" |
22 | #include "clang/AST/NestedNameSpecifier.h" |
23 | #include "clang/AST/Redeclarable.h" |
24 | #include "clang/AST/Type.h" |
25 | #include "clang/Basic/AddressSpaces.h" |
26 | #include "clang/Basic/Diagnostic.h" |
27 | #include "clang/Basic/IdentifierTable.h" |
28 | #include "clang/Basic/LLVM.h" |
29 | #include "clang/Basic/Linkage.h" |
30 | #include "clang/Basic/OperatorKinds.h" |
31 | #include "clang/Basic/PartialDiagnostic.h" |
32 | #include "clang/Basic/PragmaKinds.h" |
33 | #include "clang/Basic/SourceLocation.h" |
34 | #include "clang/Basic/Specifiers.h" |
35 | #include "clang/Basic/Visibility.h" |
36 | #include "llvm/ADT/APSInt.h" |
37 | #include "llvm/ADT/ArrayRef.h" |
38 | #include "llvm/ADT/Optional.h" |
39 | #include "llvm/ADT/PointerIntPair.h" |
40 | #include "llvm/ADT/PointerUnion.h" |
41 | #include "llvm/ADT/StringRef.h" |
42 | #include "llvm/ADT/iterator_range.h" |
43 | #include "llvm/Support/Casting.h" |
44 | #include "llvm/Support/Compiler.h" |
45 | #include "llvm/Support/TrailingObjects.h" |
46 | #include <cassert> |
47 | #include <cstddef> |
48 | #include <cstdint> |
49 | #include <string> |
50 | #include <utility> |
51 | |
52 | namespace clang { |
53 | |
54 | class ASTContext; |
55 | struct ASTTemplateArgumentListInfo; |
56 | class Attr; |
57 | class CompoundStmt; |
58 | class DependentFunctionTemplateSpecializationInfo; |
59 | class EnumDecl; |
60 | class Expr; |
61 | class FunctionTemplateDecl; |
62 | class FunctionTemplateSpecializationInfo; |
63 | class FunctionTypeLoc; |
64 | class LabelStmt; |
65 | class MemberSpecializationInfo; |
66 | class Module; |
67 | class NamespaceDecl; |
68 | class ParmVarDecl; |
69 | class RecordDecl; |
70 | class Stmt; |
71 | class StringLiteral; |
72 | class TagDecl; |
73 | class TemplateArgumentList; |
74 | class TemplateArgumentListInfo; |
75 | class TemplateParameterList; |
76 | class TypeAliasTemplateDecl; |
77 | class TypeLoc; |
78 | class UnresolvedSetImpl; |
79 | class VarTemplateDecl; |
80 | |
81 | |
82 | class TranslationUnitDecl : public Decl, |
83 | public DeclContext, |
84 | public Redeclarable<TranslationUnitDecl> { |
85 | using redeclarable_base = Redeclarable<TranslationUnitDecl>; |
86 | |
87 | TranslationUnitDecl *getNextRedeclarationImpl() override { |
88 | return getNextRedeclaration(); |
89 | } |
90 | |
91 | TranslationUnitDecl *getPreviousDeclImpl() override { |
92 | return getPreviousDecl(); |
93 | } |
94 | |
95 | TranslationUnitDecl *getMostRecentDeclImpl() override { |
96 | return getMostRecentDecl(); |
97 | } |
98 | |
99 | ASTContext &Ctx; |
100 | |
101 | |
102 | |
103 | NamespaceDecl *AnonymousNamespace = nullptr; |
104 | |
105 | explicit TranslationUnitDecl(ASTContext &ctx); |
106 | |
107 | virtual void anchor(); |
108 | |
109 | public: |
110 | using redecl_range = redeclarable_base::redecl_range; |
111 | using redecl_iterator = redeclarable_base::redecl_iterator; |
112 | |
113 | using redeclarable_base::getMostRecentDecl; |
114 | using redeclarable_base::getPreviousDecl; |
115 | using redeclarable_base::isFirstDecl; |
116 | using redeclarable_base::redecls; |
117 | using redeclarable_base::redecls_begin; |
118 | using redeclarable_base::redecls_end; |
119 | |
120 | ASTContext &getASTContext() const { return Ctx; } |
121 | |
122 | NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; } |
123 | void setAnonymousNamespace(NamespaceDecl *D) { AnonymousNamespace = D; } |
124 | |
125 | static TranslationUnitDecl *Create(ASTContext &C); |
126 | |
127 | |
128 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
129 | static bool classofKind(Kind K) { return K == TranslationUnit; } |
130 | static DeclContext *castToDeclContext(const TranslationUnitDecl *D) { |
131 | return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D)); |
132 | } |
133 | static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) { |
134 | return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC)); |
135 | } |
136 | }; |
137 | |
138 | |
139 | |
140 | class PragmaCommentDecl final |
141 | : public Decl, |
142 | private llvm::TrailingObjects<PragmaCommentDecl, char> { |
143 | friend class ASTDeclReader; |
144 | friend class ASTDeclWriter; |
145 | friend TrailingObjects; |
146 | |
147 | PragmaMSCommentKind CommentKind; |
148 | |
149 | PragmaCommentDecl(TranslationUnitDecl *TU, SourceLocation CommentLoc, |
150 | PragmaMSCommentKind CommentKind) |
151 | : Decl(PragmaComment, TU, CommentLoc), CommentKind(CommentKind) {} |
152 | |
153 | virtual void anchor(); |
154 | |
155 | public: |
156 | static PragmaCommentDecl *Create(const ASTContext &C, TranslationUnitDecl *DC, |
157 | SourceLocation CommentLoc, |
158 | PragmaMSCommentKind CommentKind, |
159 | StringRef Arg); |
160 | static PragmaCommentDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
161 | unsigned ArgSize); |
162 | |
163 | PragmaMSCommentKind getCommentKind() const { return CommentKind; } |
164 | |
165 | StringRef getArg() const { return getTrailingObjects<char>(); } |
166 | |
167 | |
168 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
169 | static bool classofKind(Kind K) { return K == PragmaComment; } |
170 | }; |
171 | |
172 | |
173 | |
174 | class PragmaDetectMismatchDecl final |
175 | : public Decl, |
176 | private llvm::TrailingObjects<PragmaDetectMismatchDecl, char> { |
177 | friend class ASTDeclReader; |
178 | friend class ASTDeclWriter; |
179 | friend TrailingObjects; |
180 | |
181 | size_t ValueStart; |
182 | |
183 | PragmaDetectMismatchDecl(TranslationUnitDecl *TU, SourceLocation Loc, |
184 | size_t ValueStart) |
185 | : Decl(PragmaDetectMismatch, TU, Loc), ValueStart(ValueStart) {} |
186 | |
187 | virtual void anchor(); |
188 | |
189 | public: |
190 | static PragmaDetectMismatchDecl *Create(const ASTContext &C, |
191 | TranslationUnitDecl *DC, |
192 | SourceLocation Loc, StringRef Name, |
193 | StringRef Value); |
194 | static PragmaDetectMismatchDecl * |
195 | CreateDeserialized(ASTContext &C, unsigned ID, unsigned NameValueSize); |
196 | |
197 | StringRef getName() const { return getTrailingObjects<char>(); } |
198 | StringRef getValue() const { return getTrailingObjects<char>() + ValueStart; } |
199 | |
200 | |
201 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
202 | static bool classofKind(Kind K) { return K == PragmaDetectMismatch; } |
203 | }; |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | class ExternCContextDecl : public Decl, public DeclContext { |
223 | explicit ExternCContextDecl(TranslationUnitDecl *TU) |
224 | : Decl(ExternCContext, TU, SourceLocation()), |
225 | DeclContext(ExternCContext) {} |
226 | |
227 | virtual void anchor(); |
228 | |
229 | public: |
230 | static ExternCContextDecl *Create(const ASTContext &C, |
231 | TranslationUnitDecl *TU); |
232 | |
233 | |
234 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
235 | static bool classofKind(Kind K) { return K == ExternCContext; } |
236 | static DeclContext *castToDeclContext(const ExternCContextDecl *D) { |
237 | return static_cast<DeclContext *>(const_cast<ExternCContextDecl*>(D)); |
238 | } |
239 | static ExternCContextDecl *castFromDeclContext(const DeclContext *DC) { |
240 | return static_cast<ExternCContextDecl *>(const_cast<DeclContext*>(DC)); |
241 | } |
242 | }; |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | class NamedDecl : public Decl { |
250 | |
251 | |
252 | |
253 | DeclarationName Name; |
254 | |
255 | virtual void anchor(); |
256 | |
257 | private: |
258 | NamedDecl *getUnderlyingDeclImpl() LLVM_READONLY; |
259 | |
260 | protected: |
261 | NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N) |
262 | : Decl(DK, DC, L), Name(N) {} |
263 | |
264 | public: |
265 | |
266 | |
267 | |
268 | |
269 | |
270 | IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); } |
271 | |
272 | |
273 | |
274 | |
275 | |
276 | StringRef getName() const { |
277 | assert(Name.isIdentifier() && "Name is not a simple identifier"); |
278 | return getIdentifier() ? getIdentifier()->getName() : ""; |
279 | } |
280 | |
281 | |
282 | |
283 | |
284 | |
285 | |
286 | |
287 | |
288 | |
289 | |
290 | |
291 | |
292 | std::string getNameAsString() const { return Name.getAsString(); } |
293 | |
294 | |
295 | |
296 | virtual void printName(raw_ostream &os) const; |
297 | |
298 | |
299 | |
300 | |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | DeclarationName getDeclName() const { return Name; } |
314 | |
315 | |
316 | void setDeclName(DeclarationName N) { Name = N; } |
317 | |
318 | |
319 | |
320 | |
321 | |
322 | |
323 | |
324 | |
325 | |
326 | void printQualifiedName(raw_ostream &OS) const; |
327 | void printQualifiedName(raw_ostream &OS, const PrintingPolicy &Policy) const; |
328 | |
329 | |
330 | |
331 | |
332 | |
333 | void printNestedNameSpecifier(raw_ostream &OS) const; |
334 | void printNestedNameSpecifier(raw_ostream &OS, |
335 | const PrintingPolicy &Policy) const; |
336 | |
337 | |
338 | std::string getQualifiedNameAsString() const; |
339 | |
340 | |
341 | |
342 | |
343 | |
344 | |
345 | |
346 | virtual void getNameForDiagnostic(raw_ostream &OS, |
347 | const PrintingPolicy &Policy, |
348 | bool Qualified) const; |
349 | |
350 | |
351 | |
352 | |
353 | |
354 | |
355 | |
356 | |
357 | |
358 | |
359 | |
360 | bool declarationReplaces(NamedDecl *OldD, bool IsKnownNewer = true) const; |
361 | |
362 | |
363 | bool hasLinkage() const; |
364 | |
365 | using Decl::isModulePrivate; |
366 | using Decl::setModulePrivate; |
367 | |
368 | |
369 | bool isCXXClassMember() const { |
370 | const DeclContext *DC = getDeclContext(); |
371 | |
372 | |
373 | |
374 | |
375 | if (isa<EnumDecl>(DC)) |
376 | DC = DC->getRedeclContext(); |
377 | |
378 | return DC->isRecord(); |
379 | } |
380 | |
381 | |
382 | |
383 | bool isCXXInstanceMember() const; |
384 | |
385 | |
386 | |
387 | ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const; |
388 | |
389 | |
390 | |
391 | |
392 | |
393 | |
394 | Linkage getLinkageInternal() const; |
395 | |
396 | |
397 | |
398 | Linkage getFormalLinkage() const { |
399 | return clang::getFormalLinkage(getLinkageInternal()); |
400 | } |
401 | |
402 | |
403 | bool hasExternalFormalLinkage() const { |
404 | return isExternalFormalLinkage(getLinkageInternal()); |
405 | } |
406 | |
407 | bool isExternallyVisible() const { |
408 | return clang::isExternallyVisible(getLinkageInternal()); |
409 | } |
410 | |
411 | |
412 | |
413 | bool isExternallyDeclarable() const { |
414 | return isExternallyVisible() && !getOwningModuleForLinkage(); |
415 | } |
416 | |
417 | |
418 | Visibility getVisibility() const { |
419 | return getLinkageAndVisibility().getVisibility(); |
420 | } |
421 | |
422 | |
423 | LinkageInfo getLinkageAndVisibility() const; |
424 | |
425 | |
426 | enum ExplicitVisibilityKind { |
427 | |
428 | |
429 | |
430 | VisibilityForType, |
431 | |
432 | |
433 | |
434 | |
435 | VisibilityForValue |
436 | }; |
437 | |
438 | |
439 | |
440 | Optional<Visibility> |
441 | getExplicitVisibility(ExplicitVisibilityKind kind) const; |
442 | |
443 | |
444 | |
445 | bool isLinkageValid() const; |
446 | |
447 | |
448 | |
449 | |
450 | |
451 | |
452 | |
453 | bool hasLinkageBeenComputed() const { |
454 | return hasCachedLinkage(); |
455 | } |
456 | |
457 | |
458 | |
459 | NamedDecl *getUnderlyingDecl() { |
460 | |
461 | if (this->getKind() != UsingShadow && |
462 | this->getKind() != ConstructorUsingShadow && |
463 | this->getKind() != ObjCCompatibleAlias && |
464 | this->getKind() != NamespaceAlias) |
465 | return this; |
466 | |
467 | return getUnderlyingDeclImpl(); |
468 | } |
469 | const NamedDecl *getUnderlyingDecl() const { |
470 | return const_cast<NamedDecl*>(this)->getUnderlyingDecl(); |
471 | } |
472 | |
473 | NamedDecl *getMostRecentDecl() { |
474 | return cast<NamedDecl>(static_cast<Decl *>(this)->getMostRecentDecl()); |
475 | } |
476 | const NamedDecl *getMostRecentDecl() const { |
477 | return const_cast<NamedDecl*>(this)->getMostRecentDecl(); |
478 | } |
479 | |
480 | ObjCStringFormatFamily getObjCFStringFormattingFamily() const; |
481 | |
482 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
483 | static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; } |
484 | }; |
485 | |
486 | inline raw_ostream &operator<<(raw_ostream &OS, const NamedDecl &ND) { |
487 | ND.printName(OS); |
488 | return OS; |
489 | } |
490 | |
491 | |
492 | |
493 | |
494 | |
495 | |
496 | class LabelDecl : public NamedDecl { |
497 | LabelStmt *TheStmt; |
498 | StringRef MSAsmName; |
499 | bool MSAsmNameResolved = false; |
500 | |
501 | |
502 | |
503 | |
504 | SourceLocation LocStart; |
505 | |
506 | LabelDecl(DeclContext *DC, SourceLocation IdentL, IdentifierInfo *II, |
507 | LabelStmt *S, SourceLocation StartL) |
508 | : NamedDecl(Label, DC, IdentL, II), TheStmt(S), LocStart(StartL) {} |
509 | |
510 | void anchor() override; |
511 | |
512 | public: |
513 | static LabelDecl *Create(ASTContext &C, DeclContext *DC, |
514 | SourceLocation IdentL, IdentifierInfo *II); |
515 | static LabelDecl *Create(ASTContext &C, DeclContext *DC, |
516 | SourceLocation IdentL, IdentifierInfo *II, |
517 | SourceLocation GnuLabelL); |
518 | static LabelDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
519 | |
520 | LabelStmt *getStmt() const { return TheStmt; } |
521 | void setStmt(LabelStmt *T) { TheStmt = T; } |
522 | |
523 | bool isGnuLocal() const { return LocStart != getLocation(); } |
524 | void setLocStart(SourceLocation L) { LocStart = L; } |
525 | |
526 | SourceRange getSourceRange() const override LLVM_READONLY { |
527 | return SourceRange(LocStart, getLocation()); |
528 | } |
529 | |
530 | bool isMSAsmLabel() const { return !MSAsmName.empty(); } |
531 | bool isResolvedMSAsmLabel() const { return isMSAsmLabel() && MSAsmNameResolved; } |
532 | void setMSAsmLabel(StringRef Name); |
533 | StringRef getMSAsmLabel() const { return MSAsmName; } |
534 | void setMSAsmLabelResolved() { MSAsmNameResolved = true; } |
535 | |
536 | |
537 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
538 | static bool classofKind(Kind K) { return K == Label; } |
539 | }; |
540 | |
541 | |
542 | class NamespaceDecl : public NamedDecl, public DeclContext, |
543 | public Redeclarable<NamespaceDecl> |
544 | { |
545 | |
546 | |
547 | SourceLocation LocStart; |
548 | |
549 | |
550 | SourceLocation RBraceLoc; |
551 | |
552 | |
553 | |
554 | |
555 | |
556 | llvm::PointerIntPair<NamespaceDecl *, 1, bool> AnonOrFirstNamespaceAndInline; |
557 | |
558 | NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline, |
559 | SourceLocation StartLoc, SourceLocation IdLoc, |
560 | IdentifierInfo *Id, NamespaceDecl *PrevDecl); |
561 | |
562 | using redeclarable_base = Redeclarable<NamespaceDecl>; |
563 | |
564 | NamespaceDecl *getNextRedeclarationImpl() override; |
565 | NamespaceDecl *getPreviousDeclImpl() override; |
566 | NamespaceDecl *getMostRecentDeclImpl() override; |
567 | |
568 | public: |
569 | friend class ASTDeclReader; |
570 | friend class ASTDeclWriter; |
571 | |
572 | static NamespaceDecl *Create(ASTContext &C, DeclContext *DC, |
573 | bool Inline, SourceLocation StartLoc, |
574 | SourceLocation IdLoc, IdentifierInfo *Id, |
575 | NamespaceDecl *PrevDecl); |
576 | |
577 | static NamespaceDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
578 | |
579 | using redecl_range = redeclarable_base::redecl_range; |
580 | using redecl_iterator = redeclarable_base::redecl_iterator; |
581 | |
582 | using redeclarable_base::redecls_begin; |
583 | using redeclarable_base::redecls_end; |
584 | using redeclarable_base::redecls; |
585 | using redeclarable_base::getPreviousDecl; |
586 | using redeclarable_base::getMostRecentDecl; |
587 | using redeclarable_base::isFirstDecl; |
588 | |
589 | |
590 | |
591 | |
592 | |
593 | |
594 | |
595 | |
596 | |
597 | |
598 | bool isAnonymousNamespace() const { |
599 | return !getIdentifier(); |
600 | } |
601 | |
602 | |
603 | bool isInline() const { |
604 | return AnonOrFirstNamespaceAndInline.getInt(); |
605 | } |
606 | |
607 | |
608 | void setInline(bool Inline) { |
609 | AnonOrFirstNamespaceAndInline.setInt(Inline); |
610 | } |
611 | |
612 | |
613 | bool isRedundantInlineQualifierFor(DeclarationName Name) const { |
614 | if (!isInline()) |
615 | return false; |
616 | auto X = lookup(Name); |
617 | auto Y = getParent()->lookup(Name); |
618 | return std::distance(X.begin(), X.end()) == |
619 | std::distance(Y.begin(), Y.end()); |
620 | } |
621 | |
622 | |
623 | NamespaceDecl *getOriginalNamespace(); |
624 | |
625 | |
626 | const NamespaceDecl *getOriginalNamespace() const; |
627 | |
628 | |
629 | |
630 | |
631 | bool isOriginalNamespace() const; |
632 | |
633 | |
634 | |
635 | NamespaceDecl *getAnonymousNamespace() const { |
636 | return getOriginalNamespace()->AnonOrFirstNamespaceAndInline.getPointer(); |
637 | } |
638 | |
639 | void setAnonymousNamespace(NamespaceDecl *D) { |
640 | getOriginalNamespace()->AnonOrFirstNamespaceAndInline.setPointer(D); |
641 | } |
642 | |
643 | |
644 | NamespaceDecl *getCanonicalDecl() override { |
645 | return getOriginalNamespace(); |
646 | } |
647 | const NamespaceDecl *getCanonicalDecl() const { |
648 | return getOriginalNamespace(); |
649 | } |
650 | |
651 | SourceRange getSourceRange() const override LLVM_READONLY { |
652 | return SourceRange(LocStart, RBraceLoc); |
653 | } |
654 | |
655 | SourceLocation getBeginLoc() const LLVM_READONLY { return LocStart; } |
656 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
657 | void setLocStart(SourceLocation L) { LocStart = L; } |
658 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
659 | |
660 | |
661 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
662 | static bool classofKind(Kind K) { return K == Namespace; } |
663 | static DeclContext *castToDeclContext(const NamespaceDecl *D) { |
664 | return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D)); |
665 | } |
666 | static NamespaceDecl *castFromDeclContext(const DeclContext *DC) { |
667 | return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC)); |
668 | } |
669 | }; |
670 | |
671 | |
672 | |
673 | |
674 | class ValueDecl : public NamedDecl { |
675 | QualType DeclType; |
676 | |
677 | void anchor() override; |
678 | |
679 | protected: |
680 | ValueDecl(Kind DK, DeclContext *DC, SourceLocation L, |
681 | DeclarationName N, QualType T) |
682 | : NamedDecl(DK, DC, L, N), DeclType(T) {} |
683 | |
684 | public: |
685 | QualType getType() const { return DeclType; } |
686 | void setType(QualType newType) { DeclType = newType; } |
687 | |
688 | |
689 | |
690 | bool isWeak() const; |
691 | |
692 | |
693 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
694 | static bool classofKind(Kind K) { return K >= firstValue && K <= lastValue; } |
695 | }; |
696 | |
697 | |
698 | |
699 | struct QualifierInfo { |
700 | NestedNameSpecifierLoc QualifierLoc; |
701 | |
702 | |
703 | |
704 | |
705 | |
706 | unsigned NumTemplParamLists = 0; |
707 | |
708 | |
709 | |
710 | |
711 | |
712 | |
713 | TemplateParameterList** TemplParamLists = nullptr; |
714 | |
715 | QualifierInfo() = default; |
716 | QualifierInfo(const QualifierInfo &) = delete; |
717 | QualifierInfo& operator=(const QualifierInfo &) = delete; |
718 | |
719 | |
720 | void setTemplateParameterListsInfo(ASTContext &Context, |
721 | ArrayRef<TemplateParameterList *> TPLists); |
722 | }; |
723 | |
724 | |
725 | |
726 | class DeclaratorDecl : public ValueDecl { |
727 | |
728 | |
729 | |
730 | struct ExtInfo : public QualifierInfo { |
731 | TypeSourceInfo *TInfo; |
732 | Expr *TrailingRequiresClause = nullptr; |
733 | }; |
734 | |
735 | llvm::PointerUnion<TypeSourceInfo *, ExtInfo *> DeclInfo; |
736 | |
737 | |
738 | |
739 | SourceLocation InnerLocStart; |
740 | |
741 | bool hasExtInfo() const { return DeclInfo.is<ExtInfo*>(); } |
742 | ExtInfo *getExtInfo() { return DeclInfo.get<ExtInfo*>(); } |
743 | const ExtInfo *getExtInfo() const { return DeclInfo.get<ExtInfo*>(); } |
744 | |
745 | protected: |
746 | DeclaratorDecl(Kind DK, DeclContext *DC, SourceLocation L, |
747 | DeclarationName N, QualType T, TypeSourceInfo *TInfo, |
748 | SourceLocation StartL) |
749 | : ValueDecl(DK, DC, L, N, T), DeclInfo(TInfo), InnerLocStart(StartL) {} |
750 | |
751 | public: |
752 | friend class ASTDeclReader; |
753 | friend class ASTDeclWriter; |
754 | |
755 | TypeSourceInfo *getTypeSourceInfo() const { |
756 | return hasExtInfo() |
757 | ? getExtInfo()->TInfo |
758 | : DeclInfo.get<TypeSourceInfo*>(); |
759 | } |
760 | |
761 | void setTypeSourceInfo(TypeSourceInfo *TI) { |
762 | if (hasExtInfo()) |
763 | getExtInfo()->TInfo = TI; |
764 | else |
765 | DeclInfo = TI; |
766 | } |
767 | |
768 | |
769 | SourceLocation getInnerLocStart() const { return InnerLocStart; } |
770 | void setInnerLocStart(SourceLocation L) { InnerLocStart = L; } |
771 | |
772 | |
773 | |
774 | SourceLocation getOuterLocStart() const; |
775 | |
776 | SourceRange getSourceRange() const override LLVM_READONLY; |
777 | |
778 | SourceLocation getBeginLoc() const LLVM_READONLY { |
779 | return getOuterLocStart(); |
780 | } |
781 | |
782 | |
783 | |
784 | NestedNameSpecifier *getQualifier() const { |
785 | return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() |
786 | : nullptr; |
787 | } |
788 | |
789 | |
790 | |
791 | |
792 | NestedNameSpecifierLoc getQualifierLoc() const { |
793 | return hasExtInfo() ? getExtInfo()->QualifierLoc |
794 | : NestedNameSpecifierLoc(); |
795 | } |
796 | |
797 | void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); |
798 | |
799 | |
800 | |
801 | |
802 | Expr *getTrailingRequiresClause() { |
803 | return hasExtInfo() ? getExtInfo()->TrailingRequiresClause |
804 | : nullptr; |
805 | } |
806 | |
807 | const Expr *getTrailingRequiresClause() const { |
808 | return hasExtInfo() ? getExtInfo()->TrailingRequiresClause |
809 | : nullptr; |
810 | } |
811 | |
812 | void setTrailingRequiresClause(Expr *TrailingRequiresClause); |
813 | |
814 | unsigned getNumTemplateParameterLists() const { |
815 | return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; |
816 | } |
817 | |
818 | TemplateParameterList *getTemplateParameterList(unsigned index) const { |
819 | assert(index < getNumTemplateParameterLists()); |
820 | return getExtInfo()->TemplParamLists[index]; |
821 | } |
822 | |
823 | void setTemplateParameterListsInfo(ASTContext &Context, |
824 | ArrayRef<TemplateParameterList *> TPLists); |
825 | |
826 | SourceLocation getTypeSpecStartLoc() const; |
827 | SourceLocation getTypeSpecEndLoc() const; |
828 | |
829 | |
830 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
831 | static bool classofKind(Kind K) { |
832 | return K >= firstDeclarator && K <= lastDeclarator; |
833 | } |
834 | }; |
835 | |
836 | |
837 | |
838 | |
839 | struct EvaluatedStmt { |
840 | |
841 | bool WasEvaluated : 1; |
842 | |
843 | |
844 | bool IsEvaluating : 1; |
845 | |
846 | |
847 | |
848 | |
849 | |
850 | bool HasConstantInitialization : 1; |
851 | |
852 | |
853 | |
854 | |
855 | |
856 | |
857 | bool HasConstantDestruction : 1; |
858 | |
859 | |
860 | |
861 | bool HasICEInit : 1; |
862 | bool CheckedForICEInit : 1; |
863 | |
864 | Stmt *Value; |
865 | APValue Evaluated; |
866 | |
867 | EvaluatedStmt() |
868 | : WasEvaluated(false), IsEvaluating(false), |
869 | HasConstantInitialization(false), HasConstantDestruction(false), |
870 | HasICEInit(false), CheckedForICEInit(false) {} |
871 | }; |
872 | |
873 | |
874 | class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> { |
875 | public: |
876 | |
877 | enum InitializationStyle { |
878 | |
879 | CInit, |
880 | |
881 | |
882 | CallInit, |
883 | |
884 | |
885 | ListInit |
886 | }; |
887 | |
888 | |
889 | enum TLSKind { |
890 | |
891 | TLS_None, |
892 | |
893 | |
894 | TLS_Static, |
895 | |
896 | |
897 | TLS_Dynamic |
898 | }; |
899 | |
900 | |
901 | |
902 | |
903 | static const char *getStorageClassSpecifierString(StorageClass SC); |
904 | |
905 | protected: |
906 | |
907 | |
908 | |
909 | |
910 | |
911 | |
912 | |
913 | using InitType = llvm::PointerUnion<Stmt *, EvaluatedStmt *>; |
914 | |
915 | |
916 | |
917 | mutable InitType Init; |
918 | |
919 | private: |
920 | friend class ASTDeclReader; |
921 | friend class ASTNodeImporter; |
922 | friend class StmtIteratorBase; |
923 | |
924 | class VarDeclBitfields { |
925 | friend class ASTDeclReader; |
926 | friend class VarDecl; |
927 | |
928 | unsigned SClass : 3; |
929 | unsigned TSCSpec : 2; |
930 | unsigned InitStyle : 2; |
931 | |
932 | |
933 | |
934 | unsigned ARCPseudoStrong : 1; |
935 | }; |
936 | enum { NumVarDeclBits = 8 }; |
937 | |
938 | protected: |
939 | enum { NumParameterIndexBits = 8 }; |
940 | |
941 | enum DefaultArgKind { |
942 | DAK_None, |
943 | DAK_Unparsed, |
944 | DAK_Uninstantiated, |
945 | DAK_Normal |
946 | }; |
947 | |
948 | enum { NumScopeDepthOrObjCQualsBits = 7 }; |
949 | |
950 | class ParmVarDeclBitfields { |
951 | friend class ASTDeclReader; |
952 | friend class ParmVarDecl; |
953 | |
954 | unsigned : NumVarDeclBits; |
955 | |
956 | |
957 | |
958 | unsigned HasInheritedDefaultArg : 1; |
959 | |
960 | |
961 | |
962 | |
963 | |
964 | unsigned DefaultArgKind : 2; |
965 | |
966 | |
967 | unsigned IsKNRPromoted : 1; |
968 | |
969 | |
970 | unsigned IsObjCMethodParam : 1; |
971 | |
972 | |
973 | |
974 | |
975 | |
976 | unsigned ScopeDepthOrObjCQuals : NumScopeDepthOrObjCQualsBits; |
977 | |
978 | |
979 | |
980 | unsigned ParameterIndex : NumParameterIndexBits; |
981 | }; |
982 | |
983 | class NonParmVarDeclBitfields { |
984 | friend class ASTDeclReader; |
985 | friend class ImplicitParamDecl; |
986 | friend class VarDecl; |
987 | |
988 | unsigned : NumVarDeclBits; |
989 | |
990 | |
991 | |
992 | |
993 | unsigned IsThisDeclarationADemotedDefinition : 1; |
994 | |
995 | |
996 | |
997 | unsigned ExceptionVar : 1; |
998 | |
999 | |
1000 | |
1001 | |
1002 | unsigned NRVOVariable : 1; |
1003 | |
1004 | |
1005 | |
1006 | unsigned CXXForRangeDecl : 1; |
1007 | |
1008 | |
1009 | unsigned ObjCForDecl : 1; |
1010 | |
1011 | |
1012 | unsigned IsInline : 1; |
1013 | |
1014 | |
1015 | unsigned IsInlineSpecified : 1; |
1016 | |
1017 | |
1018 | unsigned IsConstexpr : 1; |
1019 | |
1020 | |
1021 | |
1022 | unsigned IsInitCapture : 1; |
1023 | |
1024 | |
1025 | |
1026 | |
1027 | unsigned PreviousDeclInSameBlockScope : 1; |
1028 | |
1029 | |
1030 | |
1031 | unsigned ImplicitParamKind : 3; |
1032 | |
1033 | unsigned EscapingByref : 1; |
1034 | }; |
1035 | |
1036 | union { |
1037 | unsigned AllBits; |
1038 | VarDeclBitfields VarDeclBits; |
1039 | ParmVarDeclBitfields ParmVarDeclBits; |
1040 | NonParmVarDeclBitfields NonParmVarDeclBits; |
1041 | }; |
1042 | |
1043 | VarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1044 | SourceLocation IdLoc, IdentifierInfo *Id, QualType T, |
1045 | TypeSourceInfo *TInfo, StorageClass SC); |
1046 | |
1047 | using redeclarable_base = Redeclarable<VarDecl>; |
1048 | |
1049 | VarDecl *getNextRedeclarationImpl() override { |
1050 | return getNextRedeclaration(); |
1051 | } |
1052 | |
1053 | VarDecl *getPreviousDeclImpl() override { |
1054 | return getPreviousDecl(); |
1055 | } |
1056 | |
1057 | VarDecl *getMostRecentDeclImpl() override { |
1058 | return getMostRecentDecl(); |
1059 | } |
1060 | |
1061 | public: |
1062 | using redecl_range = redeclarable_base::redecl_range; |
1063 | using redecl_iterator = redeclarable_base::redecl_iterator; |
1064 | |
1065 | using redeclarable_base::redecls_begin; |
1066 | using redeclarable_base::redecls_end; |
1067 | using redeclarable_base::redecls; |
1068 | using redeclarable_base::getPreviousDecl; |
1069 | using redeclarable_base::getMostRecentDecl; |
1070 | using redeclarable_base::isFirstDecl; |
1071 | |
1072 | static VarDecl *Create(ASTContext &C, DeclContext *DC, |
1073 | SourceLocation StartLoc, SourceLocation IdLoc, |
1074 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
1075 | StorageClass S); |
1076 | |
1077 | static VarDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1078 | |
1079 | SourceRange getSourceRange() const override LLVM_READONLY; |
1080 | |
1081 | |
1082 | |
1083 | StorageClass getStorageClass() const { |
1084 | return (StorageClass) VarDeclBits.SClass; |
1085 | } |
1086 | void setStorageClass(StorageClass SC); |
1087 | |
1088 | void setTSCSpec(ThreadStorageClassSpecifier TSC) { |
1089 | VarDeclBits.TSCSpec = TSC; |
1090 | assert(VarDeclBits.TSCSpec == TSC && "truncation"); |
1091 | } |
1092 | ThreadStorageClassSpecifier getTSCSpec() const { |
1093 | return static_cast<ThreadStorageClassSpecifier>(VarDeclBits.TSCSpec); |
1094 | } |
1095 | TLSKind getTLSKind() const; |
1096 | |
1097 | |
1098 | |
1099 | bool hasLocalStorage() const { |
1100 | if (getStorageClass() == SC_None) { |
1101 | |
1102 | |
1103 | |
1104 | |
1105 | if (getType().getAddressSpace() == LangAS::opencl_constant) |
1106 | return false; |
1107 | |
1108 | return !isFileVarDecl() && getTSCSpec() == TSCS_unspecified; |
1109 | } |
1110 | |
1111 | |
1112 | if (getStorageClass() == SC_Register && !isLocalVarDeclOrParm()) |
1113 | return false; |
1114 | |
1115 | |
1116 | |
1117 | |
1118 | return getStorageClass() >= SC_Auto; |
1119 | } |
1120 | |
1121 | |
1122 | |
1123 | bool isStaticLocal() const { |
1124 | return (getStorageClass() == SC_Static || |
1125 | |
1126 | (getStorageClass() == SC_None && getTSCSpec() == TSCS_thread_local)) |
1127 | && !isFileVarDecl(); |
1128 | } |
1129 | |
1130 | |
1131 | |
1132 | bool hasExternalStorage() const { |
1133 | return getStorageClass() == SC_Extern || |
1134 | getStorageClass() == SC_PrivateExtern; |
1135 | } |
1136 | |
1137 | |
1138 | |
1139 | |
1140 | |
1141 | bool hasGlobalStorage() const { return !hasLocalStorage(); } |
1142 | |
1143 | |
1144 | StorageDuration getStorageDuration() const { |
1145 | return hasLocalStorage() ? SD_Automatic : |
1146 | getTSCSpec() ? SD_Thread : SD_Static; |
1147 | } |
1148 | |
1149 | |
1150 | LanguageLinkage getLanguageLinkage() const; |
1151 | |
1152 | |
1153 | bool isExternC() const; |
1154 | |
1155 | |
1156 | |
1157 | bool isInExternCContext() const; |
1158 | |
1159 | |
1160 | |
1161 | bool isInExternCXXContext() const; |
1162 | |
1163 | |
1164 | |
1165 | |
1166 | |
1167 | |
1168 | bool isLocalVarDecl() const { |
1169 | if (getKind() != Decl::Var && getKind() != Decl::Decomposition) |
1170 | return false; |
1171 | if (const DeclContext *DC = getLexicalDeclContext()) |
1172 | return DC->getRedeclContext()->isFunctionOrMethod(); |
1173 | return false; |
1174 | } |
1175 | |
1176 | |
1177 | bool isLocalVarDeclOrParm() const { |
1178 | return isLocalVarDecl() || getKind() == Decl::ParmVar; |
1179 | } |
1180 | |
1181 | |
1182 | bool isFunctionOrMethodVarDecl() const { |
1183 | if (getKind() != Decl::Var && getKind() != Decl::Decomposition) |
1184 | return false; |
1185 | const DeclContext *DC = getLexicalDeclContext()->getRedeclContext(); |
1186 | return DC->isFunctionOrMethod() && DC->getDeclKind() != Decl::Block; |
1187 | } |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | |
1198 | bool isStaticDataMember() const { |
1199 | |
1200 | return getKind() != Decl::ParmVar && getDeclContext()->isRecord(); |
1201 | } |
1202 | |
1203 | VarDecl *getCanonicalDecl() override; |
1204 | const VarDecl *getCanonicalDecl() const { |
1205 | return const_cast<VarDecl*>(this)->getCanonicalDecl(); |
1206 | } |
1207 | |
1208 | enum DefinitionKind { |
1209 | |
1210 | DeclarationOnly, |
1211 | |
1212 | |
1213 | TentativeDefinition, |
1214 | |
1215 | |
1216 | Definition |
1217 | }; |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | DefinitionKind isThisDeclarationADefinition(ASTContext &) const; |
1223 | DefinitionKind isThisDeclarationADefinition() const { |
1224 | return isThisDeclarationADefinition(getASTContext()); |
1225 | } |
1226 | |
1227 | |
1228 | DefinitionKind hasDefinition(ASTContext &) const; |
1229 | DefinitionKind hasDefinition() const { |
1230 | return hasDefinition(getASTContext()); |
1231 | } |
1232 | |
1233 | |
1234 | |
1235 | VarDecl *getActingDefinition(); |
1236 | const VarDecl *getActingDefinition() const { |
1237 | return const_cast<VarDecl*>(this)->getActingDefinition(); |
1238 | } |
1239 | |
1240 | |
1241 | VarDecl *getDefinition(ASTContext &); |
1242 | const VarDecl *getDefinition(ASTContext &C) const { |
1243 | return const_cast<VarDecl*>(this)->getDefinition(C); |
1244 | } |
1245 | VarDecl *getDefinition() { |
1246 | return getDefinition(getASTContext()); |
1247 | } |
1248 | const VarDecl *getDefinition() const { |
1249 | return const_cast<VarDecl*>(this)->getDefinition(); |
1250 | } |
1251 | |
1252 | |
1253 | |
1254 | bool isOutOfLine() const override; |
1255 | |
1256 | |
1257 | bool isFileVarDecl() const { |
1258 | Kind K = getKind(); |
1259 | if (K == ParmVar || K == ImplicitParam) |
1260 | return false; |
1261 | |
1262 | if (getLexicalDeclContext()->getRedeclContext()->isFileContext()) |
1263 | return true; |
1264 | |
1265 | if (isStaticDataMember()) |
1266 | return true; |
1267 | |
1268 | return false; |
1269 | } |
1270 | |
1271 | |
1272 | |
1273 | const Expr *getAnyInitializer() const { |
1274 | const VarDecl *D; |
1275 | return getAnyInitializer(D); |
1276 | } |
1277 | |
1278 | |
1279 | |
1280 | const Expr *getAnyInitializer(const VarDecl *&D) const; |
1281 | |
1282 | bool hasInit() const; |
1283 | const Expr *getInit() const { |
1284 | return const_cast<VarDecl *>(this)->getInit(); |
1285 | } |
1286 | Expr *getInit(); |
1287 | |
1288 | |
1289 | Stmt **getInitAddress(); |
1290 | |
1291 | void setInit(Expr *I); |
1292 | |
1293 | |
1294 | |
1295 | |
1296 | VarDecl *getInitializingDeclaration(); |
1297 | const VarDecl *getInitializingDeclaration() const { |
1298 | return const_cast<VarDecl *>(this)->getInitializingDeclaration(); |
1299 | } |
1300 | |
1301 | |
1302 | |
1303 | |
1304 | |
1305 | |
1306 | |
1307 | |
1308 | bool mightBeUsableInConstantExpressions(const ASTContext &C) const; |
1309 | |
1310 | |
1311 | |
1312 | |
1313 | bool isUsableInConstantExpressions(const ASTContext &C) const; |
1314 | |
1315 | EvaluatedStmt *ensureEvaluatedStmt() const; |
1316 | EvaluatedStmt *getEvaluatedStmt() const; |
1317 | |
1318 | |
1319 | |
1320 | |
1321 | APValue *evaluateValue() const; |
1322 | |
1323 | private: |
1324 | APValue *evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes, |
1325 | bool IsConstantInitialization) const; |
1326 | |
1327 | public: |
1328 | |
1329 | |
1330 | |
1331 | APValue *getEvaluatedValue() const; |
1332 | |
1333 | |
1334 | |
1335 | |
1336 | |
1337 | |
1338 | |
1339 | bool evaluateDestruction(SmallVectorImpl<PartialDiagnosticAt> &Notes) const; |
1340 | |
1341 | |
1342 | |
1343 | |
1344 | |
1345 | |
1346 | |
1347 | bool hasConstantInitialization() const; |
1348 | |
1349 | |
1350 | |
1351 | |
1352 | bool hasICEInitializer(const ASTContext &Context) const; |
1353 | |
1354 | |
1355 | |
1356 | |
1357 | bool checkForConstantInitialization( |
1358 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const; |
1359 | |
1360 | void setInitStyle(InitializationStyle Style) { |
1361 | VarDeclBits.InitStyle = Style; |
1362 | } |
1363 | |
1364 | |
1365 | |
1366 | |
1367 | |
1368 | |
1369 | |
1370 | |
1371 | |
1372 | |
1373 | |
1374 | InitializationStyle getInitStyle() const { |
1375 | return static_cast<InitializationStyle>(VarDeclBits.InitStyle); |
1376 | } |
1377 | |
1378 | |
1379 | bool isDirectInit() const { |
1380 | return getInitStyle() != CInit; |
1381 | } |
1382 | |
1383 | |
1384 | bool isThisDeclarationADemotedDefinition() const { |
1385 | return isa<ParmVarDecl>(this) ? false : |
1386 | NonParmVarDeclBits.IsThisDeclarationADemotedDefinition; |
1387 | } |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | void demoteThisDefinitionToDeclaration() { |
1395 | assert(isThisDeclarationADefinition() && "Not a definition!"); |
1396 | assert(!isa<ParmVarDecl>(this) && "Cannot demote ParmVarDecls!"); |
1397 | NonParmVarDeclBits.IsThisDeclarationADemotedDefinition = 1; |
1398 | } |
1399 | |
1400 | |
1401 | |
1402 | bool isExceptionVariable() const { |
1403 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.ExceptionVar; |
1404 | } |
1405 | void setExceptionVariable(bool EV) { |
1406 | assert(!isa<ParmVarDecl>(this)); |
1407 | NonParmVarDeclBits.ExceptionVar = EV; |
1408 | } |
1409 | |
1410 | |
1411 | |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | |
1417 | |
1418 | |
1419 | |
1420 | bool isNRVOVariable() const { |
1421 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.NRVOVariable; |
1422 | } |
1423 | void setNRVOVariable(bool NRVO) { |
1424 | assert(!isa<ParmVarDecl>(this)); |
1425 | NonParmVarDeclBits.NRVOVariable = NRVO; |
1426 | } |
1427 | |
1428 | |
1429 | |
1430 | bool isCXXForRangeDecl() const { |
1431 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.CXXForRangeDecl; |
1432 | } |
1433 | void setCXXForRangeDecl(bool FRD) { |
1434 | assert(!isa<ParmVarDecl>(this)); |
1435 | NonParmVarDeclBits.CXXForRangeDecl = FRD; |
1436 | } |
1437 | |
1438 | |
1439 | |
1440 | bool isObjCForDecl() const { |
1441 | return NonParmVarDeclBits.ObjCForDecl; |
1442 | } |
1443 | |
1444 | void setObjCForDecl(bool FRD) { |
1445 | NonParmVarDeclBits.ObjCForDecl = FRD; |
1446 | } |
1447 | |
1448 | |
1449 | |
1450 | |
1451 | |
1452 | |
1453 | |
1454 | |
1455 | bool isARCPseudoStrong() const { return VarDeclBits.ARCPseudoStrong; } |
1456 | void setARCPseudoStrong(bool PS) { VarDeclBits.ARCPseudoStrong = PS; } |
1457 | |
1458 | |
1459 | bool isInline() const { |
1460 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInline; |
1461 | } |
1462 | bool isInlineSpecified() const { |
1463 | return isa<ParmVarDecl>(this) ? false |
1464 | : NonParmVarDeclBits.IsInlineSpecified; |
1465 | } |
1466 | void setInlineSpecified() { |
1467 | assert(!isa<ParmVarDecl>(this)); |
1468 | NonParmVarDeclBits.IsInline = true; |
1469 | NonParmVarDeclBits.IsInlineSpecified = true; |
1470 | } |
1471 | void setImplicitlyInline() { |
1472 | assert(!isa<ParmVarDecl>(this)); |
1473 | NonParmVarDeclBits.IsInline = true; |
1474 | } |
1475 | |
1476 | |
1477 | bool isConstexpr() const { |
1478 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsConstexpr; |
1479 | } |
1480 | void setConstexpr(bool IC) { |
1481 | assert(!isa<ParmVarDecl>(this)); |
1482 | NonParmVarDeclBits.IsConstexpr = IC; |
1483 | } |
1484 | |
1485 | |
1486 | bool isInitCapture() const { |
1487 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInitCapture; |
1488 | } |
1489 | void setInitCapture(bool IC) { |
1490 | assert(!isa<ParmVarDecl>(this)); |
1491 | NonParmVarDeclBits.IsInitCapture = IC; |
1492 | } |
1493 | |
1494 | |
1495 | |
1496 | bool isParameterPack() const; |
1497 | |
1498 | |
1499 | |
1500 | bool isPreviousDeclInSameBlockScope() const { |
1501 | return isa<ParmVarDecl>(this) |
1502 | ? false |
1503 | : NonParmVarDeclBits.PreviousDeclInSameBlockScope; |
1504 | } |
1505 | void setPreviousDeclInSameBlockScope(bool Same) { |
1506 | assert(!isa<ParmVarDecl>(this)); |
1507 | NonParmVarDeclBits.PreviousDeclInSameBlockScope = Same; |
1508 | } |
1509 | |
1510 | |
1511 | |
1512 | |
1513 | bool isEscapingByref() const; |
1514 | |
1515 | |
1516 | |
1517 | bool isNonEscapingByref() const; |
1518 | |
1519 | void setEscapingByref() { |
1520 | NonParmVarDeclBits.EscapingByref = true; |
1521 | } |
1522 | |
1523 | |
1524 | bool hasDependentAlignment() const; |
1525 | |
1526 | |
1527 | |
1528 | VarDecl *getTemplateInstantiationPattern() const; |
1529 | |
1530 | |
1531 | |
1532 | |
1533 | VarDecl *getInstantiatedFromStaticDataMember() const; |
1534 | |
1535 | |
1536 | |
1537 | |
1538 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
1539 | |
1540 | |
1541 | |
1542 | |
1543 | TemplateSpecializationKind |
1544 | getTemplateSpecializationKindForInstantiation() const; |
1545 | |
1546 | |
1547 | |
1548 | |
1549 | SourceLocation getPointOfInstantiation() const; |
1550 | |
1551 | |
1552 | |
1553 | |
1554 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
1555 | |
1556 | |
1557 | |
1558 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
1559 | SourceLocation PointOfInstantiation = SourceLocation()); |
1560 | |
1561 | |
1562 | |
1563 | void setInstantiationOfStaticDataMember(VarDecl *VD, |
1564 | TemplateSpecializationKind TSK); |
1565 | |
1566 | |
1567 | |
1568 | |
1569 | |
1570 | |
1571 | |
1572 | |
1573 | |
1574 | |
1575 | |
1576 | |
1577 | VarTemplateDecl *getDescribedVarTemplate() const; |
1578 | |
1579 | void setDescribedVarTemplate(VarTemplateDecl *Template); |
1580 | |
1581 | |
1582 | |
1583 | |
1584 | bool isKnownToBeDefined() const; |
1585 | |
1586 | |
1587 | |
1588 | bool isNoDestroy(const ASTContext &) const; |
1589 | |
1590 | |
1591 | |
1592 | QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const; |
1593 | |
1594 | |
1595 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1596 | static bool classofKind(Kind K) { return K >= firstVar && K <= lastVar; } |
1597 | }; |
1598 | |
1599 | class ImplicitParamDecl : public VarDecl { |
1600 | void anchor() override; |
1601 | |
1602 | public: |
1603 | |
1604 | |
1605 | |
1606 | enum ImplicitParamKind : unsigned { |
1607 | |
1608 | ObjCSelf, |
1609 | |
1610 | |
1611 | ObjCCmd, |
1612 | |
1613 | |
1614 | CXXThis, |
1615 | |
1616 | |
1617 | CXXVTT, |
1618 | |
1619 | |
1620 | CapturedContext, |
1621 | |
1622 | |
1623 | Other, |
1624 | }; |
1625 | |
1626 | |
1627 | static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC, |
1628 | SourceLocation IdLoc, IdentifierInfo *Id, |
1629 | QualType T, ImplicitParamKind ParamKind); |
1630 | static ImplicitParamDecl *Create(ASTContext &C, QualType T, |
1631 | ImplicitParamKind ParamKind); |
1632 | |
1633 | static ImplicitParamDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1634 | |
1635 | ImplicitParamDecl(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, |
1636 | IdentifierInfo *Id, QualType Type, |
1637 | ImplicitParamKind ParamKind) |
1638 | : VarDecl(ImplicitParam, C, DC, IdLoc, IdLoc, Id, Type, |
1639 | nullptr, SC_None) { |
1640 | NonParmVarDeclBits.ImplicitParamKind = ParamKind; |
1641 | setImplicit(); |
1642 | } |
1643 | |
1644 | ImplicitParamDecl(ASTContext &C, QualType Type, ImplicitParamKind ParamKind) |
1645 | : VarDecl(ImplicitParam, C, nullptr, SourceLocation(), |
1646 | SourceLocation(), nullptr, Type, |
1647 | nullptr, SC_None) { |
1648 | NonParmVarDeclBits.ImplicitParamKind = ParamKind; |
1649 | setImplicit(); |
1650 | } |
1651 | |
1652 | |
1653 | ImplicitParamKind getParameterKind() const { |
1654 | return static_cast<ImplicitParamKind>(NonParmVarDeclBits.ImplicitParamKind); |
1655 | } |
1656 | |
1657 | |
1658 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1659 | static bool classofKind(Kind K) { return K == ImplicitParam; } |
1660 | }; |
1661 | |
1662 | |
1663 | class ParmVarDecl : public VarDecl { |
1664 | public: |
1665 | enum { MaxFunctionScopeDepth = 255 }; |
1666 | enum { MaxFunctionScopeIndex = 255 }; |
1667 | |
1668 | protected: |
1669 | ParmVarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1670 | SourceLocation IdLoc, IdentifierInfo *Id, QualType T, |
1671 | TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg) |
1672 | : VarDecl(DK, C, DC, StartLoc, IdLoc, Id, T, TInfo, S) { |
1673 | assert(ParmVarDeclBits.HasInheritedDefaultArg == false); |
1674 | assert(ParmVarDeclBits.DefaultArgKind == DAK_None); |
1675 | assert(ParmVarDeclBits.IsKNRPromoted == false); |
1676 | assert(ParmVarDeclBits.IsObjCMethodParam == false); |
1677 | setDefaultArg(DefArg); |
1678 | } |
1679 | |
1680 | public: |
1681 | static ParmVarDecl *Create(ASTContext &C, DeclContext *DC, |
1682 | SourceLocation StartLoc, |
1683 | SourceLocation IdLoc, IdentifierInfo *Id, |
1684 | QualType T, TypeSourceInfo *TInfo, |
1685 | StorageClass S, Expr *DefArg); |
1686 | |
1687 | static ParmVarDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1688 | |
1689 | SourceRange getSourceRange() const override LLVM_READONLY; |
1690 | |
1691 | void setObjCMethodScopeInfo(unsigned parameterIndex) { |
1692 | ParmVarDeclBits.IsObjCMethodParam = true; |
1693 | setParameterIndex(parameterIndex); |
1694 | } |
1695 | |
1696 | void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex) { |
1697 | assert(!ParmVarDeclBits.IsObjCMethodParam); |
1698 | |
1699 | ParmVarDeclBits.ScopeDepthOrObjCQuals = scopeDepth; |
1700 | assert(ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth |
1701 | && "truncation!"); |
1702 | |
1703 | setParameterIndex(parameterIndex); |
1704 | } |
1705 | |
1706 | bool isObjCMethodParameter() const { |
1707 | return ParmVarDeclBits.IsObjCMethodParam; |
1708 | } |
1709 | |
1710 | |
1711 | bool isDestroyedInCallee() const; |
1712 | |
1713 | unsigned getFunctionScopeDepth() const { |
1714 | if (ParmVarDeclBits.IsObjCMethodParam) return 0; |
1715 | return ParmVarDeclBits.ScopeDepthOrObjCQuals; |
1716 | } |
1717 | |
1718 | static constexpr unsigned getMaxFunctionScopeDepth() { |
1719 | return (1u << NumScopeDepthOrObjCQualsBits) - 1; |
1720 | } |
1721 | |
1722 | |
1723 | unsigned getFunctionScopeIndex() const { |
1724 | return getParameterIndex(); |
1725 | } |
1726 | |
1727 | ObjCDeclQualifier getObjCDeclQualifier() const { |
1728 | if (!ParmVarDeclBits.IsObjCMethodParam) return OBJC_TQ_None; |
1729 | return ObjCDeclQualifier(ParmVarDeclBits.ScopeDepthOrObjCQuals); |
1730 | } |
1731 | void setObjCDeclQualifier(ObjCDeclQualifier QTVal) { |
1732 | assert(ParmVarDeclBits.IsObjCMethodParam); |
1733 | ParmVarDeclBits.ScopeDepthOrObjCQuals = QTVal; |
1734 | } |
1735 | |
1736 | |
1737 | |
1738 | |
1739 | |
1740 | |
1741 | |
1742 | |
1743 | |
1744 | bool isKNRPromoted() const { |
1745 | return ParmVarDeclBits.IsKNRPromoted; |
1746 | } |
1747 | void setKNRPromoted(bool promoted) { |
1748 | ParmVarDeclBits.IsKNRPromoted = promoted; |
1749 | } |
1750 | |
1751 | Expr *getDefaultArg(); |
1752 | const Expr *getDefaultArg() const { |
1753 | return const_cast<ParmVarDecl *>(this)->getDefaultArg(); |
1754 | } |
1755 | |
1756 | void setDefaultArg(Expr *defarg); |
1757 | |
1758 | |
1759 | |
1760 | SourceRange getDefaultArgRange() const; |
1761 | void setUninstantiatedDefaultArg(Expr *arg); |
1762 | Expr *getUninstantiatedDefaultArg(); |
1763 | const Expr *getUninstantiatedDefaultArg() const { |
1764 | return const_cast<ParmVarDecl *>(this)->getUninstantiatedDefaultArg(); |
1765 | } |
1766 | |
1767 | |
1768 | |
1769 | bool hasDefaultArg() const; |
1770 | |
1771 | |
1772 | |
1773 | |
1774 | |
1775 | |
1776 | |
1777 | |
1778 | |
1779 | |
1780 | bool hasUnparsedDefaultArg() const { |
1781 | return ParmVarDeclBits.DefaultArgKind == DAK_Unparsed; |
1782 | } |
1783 | |
1784 | bool hasUninstantiatedDefaultArg() const { |
1785 | return ParmVarDeclBits.DefaultArgKind == DAK_Uninstantiated; |
1786 | } |
1787 | |
1788 | |
1789 | |
1790 | |
1791 | |
1792 | void setUnparsedDefaultArg() { |
1793 | ParmVarDeclBits.DefaultArgKind = DAK_Unparsed; |
1794 | } |
1795 | |
1796 | bool hasInheritedDefaultArg() const { |
1797 | return ParmVarDeclBits.HasInheritedDefaultArg; |
1798 | } |
1799 | |
1800 | void setHasInheritedDefaultArg(bool I = true) { |
1801 | ParmVarDeclBits.HasInheritedDefaultArg = I; |
1802 | } |
1803 | |
1804 | QualType getOriginalType() const; |
1805 | |
1806 | |
1807 | |
1808 | |
1809 | |
1810 | void setOwningFunction(DeclContext *FD) { setDeclContext(FD); } |
1811 | |
1812 | |
1813 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1814 | static bool classofKind(Kind K) { return K == ParmVar; } |
1815 | |
1816 | private: |
1817 | enum { ParameterIndexSentinel = (1 << NumParameterIndexBits) - 1 }; |
1818 | |
1819 | void setParameterIndex(unsigned parameterIndex) { |
1820 | if (parameterIndex >= ParameterIndexSentinel) { |
1821 | setParameterIndexLarge(parameterIndex); |
1822 | return; |
1823 | } |
1824 | |
1825 | ParmVarDeclBits.ParameterIndex = parameterIndex; |
1826 | assert(ParmVarDeclBits.ParameterIndex == parameterIndex && "truncation!"); |
1827 | } |
1828 | unsigned getParameterIndex() const { |
1829 | unsigned d = ParmVarDeclBits.ParameterIndex; |
1830 | return d == ParameterIndexSentinel ? getParameterIndexLarge() : d; |
1831 | } |
1832 | |
1833 | void setParameterIndexLarge(unsigned parameterIndex); |
1834 | unsigned getParameterIndexLarge() const; |
1835 | }; |
1836 | |
1837 | enum class MultiVersionKind { |
1838 | None, |
1839 | Target, |
1840 | CPUSpecific, |
1841 | CPUDispatch |
1842 | }; |
1843 | |
1844 | |
1845 | |
1846 | |
1847 | |
1848 | |
1849 | |
1850 | |
1851 | |
1852 | |
1853 | |
1854 | class FunctionDecl : public DeclaratorDecl, |
1855 | public DeclContext, |
1856 | public Redeclarable<FunctionDecl> { |
1857 | |
1858 | |
1859 | public: |
1860 | |
1861 | enum TemplatedKind { |
1862 | |
1863 | TK_NonTemplate, |
1864 | |
1865 | TK_FunctionTemplate, |
1866 | |
1867 | |
1868 | TK_MemberSpecialization, |
1869 | |
1870 | |
1871 | |
1872 | TK_FunctionTemplateSpecialization, |
1873 | |
1874 | |
1875 | TK_DependentFunctionTemplateSpecialization |
1876 | }; |
1877 | |
1878 | |
1879 | |
1880 | class DefaultedFunctionInfo final |
1881 | : llvm::TrailingObjects<DefaultedFunctionInfo, DeclAccessPair> { |
1882 | friend TrailingObjects; |
1883 | unsigned NumLookups; |
1884 | |
1885 | public: |
1886 | static DefaultedFunctionInfo *Create(ASTContext &Context, |
1887 | ArrayRef<DeclAccessPair> Lookups); |
1888 | |
1889 | |
1890 | ArrayRef<DeclAccessPair> getUnqualifiedLookups() const { |
1891 | return {getTrailingObjects<DeclAccessPair>(), NumLookups}; |
1892 | } |
1893 | }; |
1894 | |
1895 | private: |
1896 | |
1897 | |
1898 | |
1899 | ParmVarDecl **ParamInfo = nullptr; |
1900 | |
1901 | |
1902 | |
1903 | union { |
1904 | |
1905 | LazyDeclStmtPtr Body; |
1906 | |
1907 | DefaultedFunctionInfo *DefaultedInfo; |
1908 | }; |
1909 | |
1910 | unsigned ODRHash; |
1911 | |
1912 | |
1913 | |
1914 | |
1915 | |
1916 | |
1917 | |
1918 | |
1919 | SourceLocation EndRangeLoc; |
1920 | |
1921 | |
1922 | |
1923 | |
1924 | |
1925 | |
1926 | |
1927 | |
1928 | |
1929 | |
1930 | |
1931 | |
1932 | |
1933 | llvm::PointerUnion<FunctionTemplateDecl *, |
1934 | MemberSpecializationInfo *, |
1935 | FunctionTemplateSpecializationInfo *, |
1936 | DependentFunctionTemplateSpecializationInfo *> |
1937 | TemplateOrSpecialization; |
1938 | |
1939 | |
1940 | |
1941 | DeclarationNameLoc DNLoc; |
1942 | |
1943 | |
1944 | |
1945 | |
1946 | |
1947 | |
1948 | |
1949 | |
1950 | |
1951 | |
1952 | |
1953 | |
1954 | |
1955 | |
1956 | |
1957 | |
1958 | |
1959 | |
1960 | |
1961 | |
1962 | |
1963 | |
1964 | void setFunctionTemplateSpecialization(ASTContext &C, |
1965 | FunctionTemplateDecl *Template, |
1966 | const TemplateArgumentList *TemplateArgs, |
1967 | void *InsertPos, |
1968 | TemplateSpecializationKind TSK, |
1969 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
1970 | SourceLocation PointOfInstantiation); |
1971 | |
1972 | |
1973 | |
1974 | void setInstantiationOfMemberFunction(ASTContext &C, FunctionDecl *FD, |
1975 | TemplateSpecializationKind TSK); |
1976 | |
1977 | void setParams(ASTContext &C, ArrayRef<ParmVarDecl *> NewParamInfo); |
1978 | |
1979 | |
1980 | |
1981 | |
1982 | bool isDeletedBit() const { return FunctionDeclBits.IsDeleted; } |
1983 | |
1984 | |
1985 | bool hasODRHash() const { return FunctionDeclBits.HasODRHash; } |
1986 | |
1987 | |
1988 | void setHasODRHash(bool B = true) { FunctionDeclBits.HasODRHash = B; } |
1989 | |
1990 | protected: |
1991 | FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1992 | const DeclarationNameInfo &NameInfo, QualType T, |
1993 | TypeSourceInfo *TInfo, StorageClass S, bool isInlineSpecified, |
1994 | ConstexprSpecKind ConstexprKind, |
1995 | Expr *TrailingRequiresClause = nullptr); |
1996 | |
1997 | using redeclarable_base = Redeclarable<FunctionDecl>; |
1998 | |
1999 | FunctionDecl *getNextRedeclarationImpl() override { |
2000 | return getNextRedeclaration(); |
2001 | } |
2002 | |
2003 | FunctionDecl *getPreviousDeclImpl() override { |
2004 | return getPreviousDecl(); |
2005 | } |
2006 | |
2007 | FunctionDecl *getMostRecentDeclImpl() override { |
2008 | return getMostRecentDecl(); |
2009 | } |
2010 | |
2011 | public: |
2012 | friend class ASTDeclReader; |
2013 | friend class ASTDeclWriter; |
2014 | |
2015 | using redecl_range = redeclarable_base::redecl_range; |
2016 | using redecl_iterator = redeclarable_base::redecl_iterator; |
2017 | |
2018 | using redeclarable_base::redecls_begin; |
2019 | using redeclarable_base::redecls_end; |
2020 | using redeclarable_base::redecls; |
2021 | using redeclarable_base::getPreviousDecl; |
2022 | using redeclarable_base::getMostRecentDecl; |
2023 | using redeclarable_base::isFirstDecl; |
2024 | |
2025 | static FunctionDecl * |
2026 | Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
2027 | SourceLocation NLoc, DeclarationName N, QualType T, |
2028 | TypeSourceInfo *TInfo, StorageClass SC, bool isInlineSpecified = false, |
2029 | bool hasWrittenPrototype = true, |
2030 | ConstexprSpecKind ConstexprKind = ConstexprSpecKind::Unspecified, |
2031 | Expr *TrailingRequiresClause = nullptr) { |
2032 | DeclarationNameInfo NameInfo(N, NLoc); |
2033 | return FunctionDecl::Create(C, DC, StartLoc, NameInfo, T, TInfo, SC, |
2034 | isInlineSpecified, hasWrittenPrototype, |
2035 | ConstexprKind, TrailingRequiresClause); |
2036 | } |
2037 | |
2038 | static FunctionDecl *Create(ASTContext &C, DeclContext *DC, |
2039 | SourceLocation StartLoc, |
2040 | const DeclarationNameInfo &NameInfo, QualType T, |
2041 | TypeSourceInfo *TInfo, StorageClass SC, |
2042 | bool isInlineSpecified, bool hasWrittenPrototype, |
2043 | ConstexprSpecKind ConstexprKind, |
2044 | Expr *TrailingRequiresClause); |
2045 | |
2046 | static FunctionDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2047 | |
2048 | DeclarationNameInfo getNameInfo() const { |
2049 | return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); |
2050 | } |
2051 | |
2052 | void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, |
2053 | bool Qualified) const override; |
2054 | |
2055 | void setRangeEnd(SourceLocation E) { EndRangeLoc = E; } |
2056 | |
2057 | |
2058 | SourceLocation getEllipsisLoc() const { |
2059 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
2060 | if (FPT && FPT->isVariadic()) |
2061 | return FPT->getEllipsisLoc(); |
2062 | return SourceLocation(); |
2063 | } |
2064 | |
2065 | SourceRange getSourceRange() const override LLVM_READONLY; |
2066 | |
2067 | |
2068 | |
2069 | |
2070 | |
2071 | |
2072 | |
2073 | |
2074 | |
2075 | |
2076 | |
2077 | |
2078 | |
2079 | |
2080 | |
2081 | |
2082 | |
2083 | |
2084 | |
2085 | |
2086 | bool hasBody(const FunctionDecl *&Definition) const; |
2087 | |
2088 | bool hasBody() const override { |
2089 | const FunctionDecl* Definition; |
2090 | return hasBody(Definition); |
2091 | } |
2092 | |
2093 | |
2094 | |
2095 | bool hasTrivialBody() const; |
2096 | |
2097 | |
2098 | |
2099 | |
2100 | |
2101 | |
2102 | |
2103 | |
2104 | |
2105 | |
2106 | |
2107 | |
2108 | bool isDefined(const FunctionDecl *&Definition, |
2109 | bool CheckForPendingFriendDefinition = false) const; |
2110 | |
2111 | bool isDefined() const { |
2112 | const FunctionDecl* Definition; |
2113 | return isDefined(Definition); |
2114 | } |
2115 | |
2116 | |
2117 | FunctionDecl *getDefinition() { |
2118 | const FunctionDecl *Definition; |
2119 | if (isDefined(Definition)) |
2120 | return const_cast<FunctionDecl *>(Definition); |
2121 | return nullptr; |
2122 | } |
2123 | const FunctionDecl *getDefinition() const { |
2124 | return const_cast<FunctionDecl *>(this)->getDefinition(); |
2125 | } |
2126 | |
2127 | |
2128 | |
2129 | |
2130 | |
2131 | |
2132 | |
2133 | Stmt *getBody(const FunctionDecl *&Definition) const; |
2134 | |
2135 | Stmt *getBody() const override { |
2136 | const FunctionDecl* Definition; |
2137 | return getBody(Definition); |
2138 | } |
2139 | |
2140 | |
2141 | |
2142 | |
2143 | |
2144 | |
2145 | |
2146 | |
2147 | |
2148 | |
2149 | bool isThisDeclarationADefinition() const { |
2150 | return isDeletedAsWritten() || isDefaulted() || |
2151 | doesThisDeclarationHaveABody() || hasSkippedBody() || |
2152 | willHaveBody() || hasDefiningAttr(); |
2153 | } |
2154 | |
2155 | |
2156 | |
2157 | |
2158 | bool isThisDeclarationInstantiatedFromAFriendDefinition() const; |
2159 | |
2160 | |
2161 | bool doesThisDeclarationHaveABody() const { |
2162 | return (!FunctionDeclBits.HasDefaultedFunctionInfo && Body) || |
2163 | isLateTemplateParsed(); |
2164 | } |
2165 | |
2166 | void setBody(Stmt *B); |
2167 | void setLazyBody(uint64_t Offset) { |
2168 | FunctionDeclBits.HasDefaultedFunctionInfo = false; |
2169 | Body = LazyDeclStmtPtr(Offset); |
2170 | } |
2171 | |
2172 | void setDefaultedFunctionInfo(DefaultedFunctionInfo *Info); |
2173 | DefaultedFunctionInfo *getDefaultedFunctionInfo() const; |
2174 | |
2175 | |
2176 | bool isVariadic() const; |
2177 | |
2178 | |
2179 | bool isVirtualAsWritten() const { |
2180 | return FunctionDeclBits.IsVirtualAsWritten; |
2181 | } |
2182 | |
2183 | |
2184 | void setVirtualAsWritten(bool V) { FunctionDeclBits.IsVirtualAsWritten = V; } |
2185 | |
2186 | |
2187 | |
2188 | bool isPure() const { return FunctionDeclBits.IsPure; } |
2189 | void setPure(bool P = true); |
2190 | |
2191 | |
2192 | bool isLateTemplateParsed() const { |
2193 | return FunctionDeclBits.IsLateTemplateParsed; |
2194 | } |
2195 | |
2196 | |
2197 | void setLateTemplateParsed(bool ILT = true) { |
2198 | FunctionDeclBits.IsLateTemplateParsed = ILT; |
2199 | } |
2200 | |
2201 | |
2202 | |
2203 | |
2204 | |
2205 | bool isTrivial() const { return FunctionDeclBits.IsTrivial; } |
2206 | void setTrivial(bool IT) { FunctionDeclBits.IsTrivial = IT; } |
2207 | |
2208 | bool isTrivialForCall() const { return FunctionDeclBits.IsTrivialForCall; } |
2209 | void setTrivialForCall(bool IT) { FunctionDeclBits.IsTrivialForCall = IT; } |
2210 | |
2211 | |
2212 | |
2213 | bool isDefaulted() const { return FunctionDeclBits.IsDefaulted; } |
2214 | void setDefaulted(bool D = true) { FunctionDeclBits.IsDefaulted = D; } |
2215 | |
2216 | |
2217 | bool isExplicitlyDefaulted() const { |
2218 | return FunctionDeclBits.IsExplicitlyDefaulted; |
2219 | } |
2220 | |
2221 | |
2222 | void setExplicitlyDefaulted(bool ED = true) { |
2223 | FunctionDeclBits.IsExplicitlyDefaulted = ED; |
2224 | } |
2225 | |
2226 | |
2227 | |
2228 | bool isUserProvided() const { |
2229 | auto *DeclAsWritten = this; |
2230 | if (FunctionDecl *Pattern = getTemplateInstantiationPattern()) |
2231 | DeclAsWritten = Pattern; |
2232 | return !(DeclAsWritten->isDeleted() || |
2233 | DeclAsWritten->getCanonicalDecl()->isDefaulted()); |
2234 | } |
2235 | |
2236 | |
2237 | |
2238 | |
2239 | bool hasImplicitReturnZero() const { |
2240 | return FunctionDeclBits.HasImplicitReturnZero; |
2241 | } |
2242 | |
2243 | |
2244 | |
2245 | |
2246 | void setHasImplicitReturnZero(bool IRZ) { |
2247 | FunctionDeclBits.HasImplicitReturnZero = IRZ; |
2248 | } |
2249 | |
2250 | |
2251 | |
2252 | |
2253 | |
2254 | bool hasPrototype() const { |
2255 | return hasWrittenPrototype() || hasInheritedPrototype(); |
2256 | } |
2257 | |
2258 | |
2259 | bool hasWrittenPrototype() const { |
2260 | return FunctionDeclBits.HasWrittenPrototype; |
2261 | } |
2262 | |
2263 | |
2264 | void setHasWrittenPrototype(bool P = true) { |
2265 | FunctionDeclBits.HasWrittenPrototype = P; |
2266 | } |
2267 | |
2268 | |
2269 | |
2270 | bool hasInheritedPrototype() const { |
2271 | return FunctionDeclBits.HasInheritedPrototype; |
2272 | } |
2273 | |
2274 | |
2275 | |
2276 | void setHasInheritedPrototype(bool P = true) { |
2277 | FunctionDeclBits.HasInheritedPrototype = P; |
2278 | } |
2279 | |
2280 | |
2281 | bool isConstexpr() const { |
2282 | return getConstexprKind() != ConstexprSpecKind::Unspecified; |
2283 | } |
2284 | void setConstexprKind(ConstexprSpecKind CSK) { |
2285 | FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(CSK); |
2286 | } |
2287 | ConstexprSpecKind getConstexprKind() const { |
2288 | return static_cast<ConstexprSpecKind>(FunctionDeclBits.ConstexprKind); |
2289 | } |
2290 | bool isConstexprSpecified() const { |
2291 | return getConstexprKind() == ConstexprSpecKind::Constexpr; |
2292 | } |
2293 | bool isConsteval() const { |
2294 | return getConstexprKind() == ConstexprSpecKind::Consteval; |
2295 | } |
2296 | |
2297 | |
2298 | |
2299 | |
2300 | |
2301 | |
2302 | |
2303 | bool instantiationIsPending() const { |
2304 | return FunctionDeclBits.InstantiationIsPending; |
2305 | } |
2306 | |
2307 | |
2308 | |
2309 | void setInstantiationIsPending(bool IC) { |
2310 | FunctionDeclBits.InstantiationIsPending = IC; |
2311 | } |
2312 | |
2313 | |
2314 | bool usesSEHTry() const { return FunctionDeclBits.UsesSEHTry; } |
2315 | void setUsesSEHTry(bool UST) { FunctionDeclBits.UsesSEHTry = UST; } |
2316 | |
2317 | |
2318 | |
2319 | |
2320 | |
2321 | |
2322 | |
2323 | |
2324 | |
2325 | |
2326 | |
2327 | |
2328 | |
2329 | |
2330 | |
2331 | |
2332 | |
2333 | |
2334 | |
2335 | |
2336 | bool isDeleted() const { |
2337 | return getCanonicalDecl()->FunctionDeclBits.IsDeleted; |
2338 | } |
2339 | |
2340 | bool isDeletedAsWritten() const { |
2341 | return FunctionDeclBits.IsDeleted && !isDefaulted(); |
2342 | } |
2343 | |
2344 | void setDeletedAsWritten(bool D = true) { FunctionDeclBits.IsDeleted = D; } |
2345 | |
2346 | |
2347 | |
2348 | bool isMain() const; |
2349 | |
2350 | |
2351 | |
2352 | bool isMSVCRTEntryPoint() const; |
2353 | |
2354 | |
2355 | |
2356 | |
2357 | |
2358 | |
2359 | |
2360 | |
2361 | |
2362 | |
2363 | |
2364 | |
2365 | |
2366 | |
2367 | bool isReservedGlobalPlacementOperator() const; |
2368 | |
2369 | |
2370 | |
2371 | |
2372 | |
2373 | |
2374 | |
2375 | |
2376 | |
2377 | |
2378 | |
2379 | |
2380 | |
2381 | |
2382 | |
2383 | |
2384 | |
2385 | |
2386 | |
2387 | |
2388 | |
2389 | |
2390 | bool isReplaceableGlobalAllocationFunction( |
2391 | Optional<unsigned> *AlignmentParam = nullptr, |
2392 | bool *IsNothrow = nullptr) const; |
2393 | |
2394 | |
2395 | bool isInlineBuiltinDeclaration() const; |
2396 | |
2397 | |
2398 | bool isDestroyingOperatorDelete() const; |
2399 | |
2400 | |
2401 | LanguageLinkage getLanguageLinkage() const; |
2402 | |
2403 | |
2404 | |
2405 | bool isExternC() const; |
2406 | |
2407 | |
2408 | |
2409 | bool isInExternCContext() const; |
2410 | |
2411 | |
2412 | |
2413 | bool isInExternCXXContext() const; |
2414 | |
2415 | |
2416 | bool isGlobal() const; |
2417 | |
2418 | |
2419 | |
2420 | bool isNoReturn() const; |
2421 | |
2422 | |
2423 | bool hasSkippedBody() const { return FunctionDeclBits.HasSkippedBody; } |
2424 | void setHasSkippedBody(bool Skipped = true) { |
2425 | FunctionDeclBits.HasSkippedBody = Skipped; |
2426 | } |
2427 | |
2428 | |
2429 | bool willHaveBody() const { return FunctionDeclBits.WillHaveBody; } |
2430 | void setWillHaveBody(bool V = true) { FunctionDeclBits.WillHaveBody = V; } |
2431 | |
2432 | |
2433 | bool isMultiVersion() const { |
2434 | return getCanonicalDecl()->FunctionDeclBits.IsMultiVersion; |
2435 | } |
2436 | |
2437 | |
2438 | |
2439 | void setIsMultiVersion(bool V = true) { |
2440 | getCanonicalDecl()->FunctionDeclBits.IsMultiVersion = V; |
2441 | } |
2442 | |
2443 | |
2444 | |
2445 | |
2446 | MultiVersionKind getMultiVersionKind() const; |
2447 | |
2448 | |
2449 | |
2450 | |
2451 | bool isCPUDispatchMultiVersion() const; |
2452 | |
2453 | |
2454 | bool isCPUSpecificMultiVersion() const; |
2455 | |
2456 | |
2457 | |
2458 | bool isTargetMultiVersion() const; |
2459 | |
2460 | |
2461 | |
2462 | |
2463 | |
2464 | |
2465 | |
2466 | void getAssociatedConstraints(SmallVectorImpl<const Expr *> &AC) const { |
2467 | if (auto *TRC = getTrailingRequiresClause()) |
2468 | AC.push_back(TRC); |
2469 | } |
2470 | |
2471 | void setPreviousDeclaration(FunctionDecl * PrevDecl); |
2472 | |
2473 | FunctionDecl *getCanonicalDecl() override; |
2474 | const FunctionDecl *getCanonicalDecl() const { |
2475 | return const_cast<FunctionDecl*>(this)->getCanonicalDecl(); |
2476 | } |
2477 | |
2478 | unsigned getBuiltinID(bool ConsiderWrapperFunctions = false) const; |
2479 | |
2480 | |
2481 | ArrayRef<ParmVarDecl *> parameters() const { |
2482 | return {ParamInfo, getNumParams()}; |
2483 | } |
2484 | MutableArrayRef<ParmVarDecl *> parameters() { |
2485 | return {ParamInfo, getNumParams()}; |
2486 | } |
2487 | |
2488 | |
2489 | using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator; |
2490 | using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator; |
2491 | |
2492 | bool param_empty() const { return parameters().empty(); } |
2493 | param_iterator param_begin() { return parameters().begin(); } |
2494 | param_iterator param_end() { return parameters().end(); } |
2495 | param_const_iterator param_begin() const { return parameters().begin(); } |
2496 | param_const_iterator param_end() const { return parameters().end(); } |
2497 | size_t param_size() const { return parameters().size(); } |
2498 | |
2499 | |
2500 | |
2501 | |
2502 | unsigned getNumParams() const; |
2503 | |
2504 | const ParmVarDecl *getParamDecl(unsigned i) const { |
2505 | assert(i < getNumParams() && "Illegal param #"); |
2506 | return ParamInfo[i]; |
2507 | } |
2508 | ParmVarDecl *getParamDecl(unsigned i) { |
2509 | assert(i < getNumParams() && "Illegal param #"); |
2510 | return ParamInfo[i]; |
2511 | } |
2512 | void setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
2513 | setParams(getASTContext(), NewParamInfo); |
2514 | } |
2515 | |
2516 | |
2517 | |
2518 | |
2519 | unsigned getMinRequiredArguments() const; |
2520 | |
2521 | |
2522 | |
2523 | |
2524 | |
2525 | |
2526 | |
2527 | bool hasOneParamOrDefaultArgs() const; |
2528 | |
2529 | |
2530 | |
2531 | |
2532 | |
2533 | FunctionTypeLoc getFunctionTypeLoc() const; |
2534 | |
2535 | QualType getReturnType() const { |
2536 | return getType()->castAs<FunctionType>()->getReturnType(); |
2537 | } |
2538 | |
2539 | |
2540 | |
2541 | |
2542 | SourceRange getReturnTypeSourceRange() const; |
2543 | |
2544 | |
2545 | |
2546 | |
2547 | |
2548 | SourceRange getParametersSourceRange() const; |
2549 | |
2550 | |
2551 | |
2552 | QualType getDeclaredReturnType() const { |
2553 | auto *TSI = getTypeSourceInfo(); |
2554 | QualType T = TSI ? TSI->getType() : getType(); |
2555 | return T->castAs<FunctionType>()->getReturnType(); |
2556 | } |
2557 | |
2558 | |
2559 | ExceptionSpecificationType getExceptionSpecType() const { |
2560 | auto *TSI = getTypeSourceInfo(); |
2561 | QualType T = TSI ? TSI->getType() : getType(); |
2562 | const auto *FPT = T->getAs<FunctionProtoType>(); |
2563 | return FPT ? FPT->getExceptionSpecType() : EST_None; |
2564 | } |
2565 | |
2566 | |
2567 | |
2568 | SourceRange getExceptionSpecSourceRange() const; |
2569 | |
2570 | |
2571 | QualType getCallResultType() const { |
2572 | return getType()->castAs<FunctionType>()->getCallResultType( |
2573 | getASTContext()); |
2574 | } |
2575 | |
2576 | |
2577 | |
2578 | StorageClass getStorageClass() const { |
2579 | return static_cast<StorageClass>(FunctionDeclBits.SClass); |
2580 | } |
2581 | |
2582 | |
2583 | void setStorageClass(StorageClass SClass) { |
2584 | FunctionDeclBits.SClass = SClass; |
2585 | } |
2586 | |
2587 | |
2588 | |
2589 | bool isInlineSpecified() const { return FunctionDeclBits.IsInlineSpecified; } |
2590 | |
2591 | |
2592 | void setInlineSpecified(bool I) { |
2593 | FunctionDeclBits.IsInlineSpecified = I; |
2594 | FunctionDeclBits.IsInline = I; |
2595 | } |
2596 | |
2597 | |
2598 | void setImplicitlyInline(bool I = true) { FunctionDeclBits.IsInline = I; } |
2599 | |
2600 | |
2601 | |
2602 | |
2603 | bool isInlined() const { return FunctionDeclBits.IsInline; } |
2604 | |
2605 | bool isInlineDefinitionExternallyVisible() const; |
2606 | |
2607 | bool isMSExternInline() const; |
2608 | |
2609 | bool doesDeclarationForceExternallyVisibleDefinition() const; |
2610 | |
2611 | bool isStatic() const { return getStorageClass() == SC_Static; } |
2612 | |
2613 | |
2614 | |
2615 | bool isOverloadedOperator() const { |
2616 | return getOverloadedOperator() != OO_None; |
2617 | } |
2618 | |
2619 | OverloadedOperatorKind getOverloadedOperator() const; |
2620 | |
2621 | const IdentifierInfo *getLiteralIdentifier() const; |
2622 | |
2623 | |
2624 | |
2625 | |
2626 | |
2627 | |
2628 | |
2629 | |
2630 | |
2631 | |
2632 | |
2633 | |
2634 | |
2635 | |
2636 | |
2637 | |
2638 | |
2639 | |
2640 | |
2641 | |
2642 | |
2643 | |
2644 | FunctionDecl *getInstantiatedFromMemberFunction() const; |
2645 | |
2646 | |
2647 | TemplatedKind getTemplatedKind() const; |
2648 | |
2649 | |
2650 | |
2651 | |
2652 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
2653 | |
2654 | |
2655 | |
2656 | void setInstantiationOfMemberFunction(FunctionDecl *FD, |
2657 | TemplateSpecializationKind TSK) { |
2658 | setInstantiationOfMemberFunction(getASTContext(), FD, TSK); |
2659 | } |
2660 | |
2661 | |
2662 | |
2663 | |
2664 | |
2665 | |
2666 | |
2667 | |
2668 | |
2669 | |
2670 | |
2671 | |
2672 | |
2673 | FunctionTemplateDecl *getDescribedFunctionTemplate() const; |
2674 | |
2675 | void setDescribedFunctionTemplate(FunctionTemplateDecl *Template); |
2676 | |
2677 | |
2678 | |
2679 | bool isFunctionTemplateSpecialization() const { |
2680 | return getPrimaryTemplate() != nullptr; |
2681 | } |
2682 | |
2683 | |
2684 | |
2685 | |
2686 | FunctionTemplateSpecializationInfo *getTemplateSpecializationInfo() const; |
2687 | |
2688 | |
2689 | |
2690 | |
2691 | bool isImplicitlyInstantiable() const; |
2692 | |
2693 | |
2694 | |
2695 | bool isTemplateInstantiation() const; |
2696 | |
2697 | |
2698 | |
2699 | |
2700 | |
2701 | |
2702 | |
2703 | |
2704 | |
2705 | FunctionDecl * |
2706 | getTemplateInstantiationPattern(bool ForDefinition = true) const; |
2707 | |
2708 | |
2709 | |
2710 | |
2711 | |
2712 | |
2713 | FunctionTemplateDecl *getPrimaryTemplate() const; |
2714 | |
2715 | |
2716 | |
2717 | |
2718 | |
2719 | |
2720 | const TemplateArgumentList *getTemplateSpecializationArgs() const; |
2721 | |
2722 | |
2723 | |
2724 | |
2725 | |
2726 | |
2727 | |
2728 | |
2729 | const ASTTemplateArgumentListInfo* |
2730 | getTemplateSpecializationArgsAsWritten() const; |
2731 | |
2732 | |
2733 | |
2734 | |
2735 | |
2736 | |
2737 | |
2738 | |
2739 | |
2740 | |
2741 | |
2742 | |
2743 | |
2744 | |
2745 | |
2746 | |
2747 | |
2748 | |
2749 | |
2750 | |
2751 | void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template, |
2752 | const TemplateArgumentList *TemplateArgs, |
2753 | void *InsertPos, |
2754 | TemplateSpecializationKind TSK = TSK_ImplicitInstantiation, |
2755 | const TemplateArgumentListInfo *TemplateArgsAsWritten = nullptr, |
2756 | SourceLocation PointOfInstantiation = SourceLocation()) { |
2757 | setFunctionTemplateSpecialization(getASTContext(), Template, TemplateArgs, |
2758 | InsertPos, TSK, TemplateArgsAsWritten, |
2759 | PointOfInstantiation); |
2760 | } |
2761 | |
2762 | |
2763 | |
2764 | void setDependentTemplateSpecialization(ASTContext &Context, |
2765 | const UnresolvedSetImpl &Templates, |
2766 | const TemplateArgumentListInfo &TemplateArgs); |
2767 | |
2768 | DependentFunctionTemplateSpecializationInfo * |
2769 | getDependentSpecializationInfo() const; |
2770 | |
2771 | |
2772 | |
2773 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
2774 | |
2775 | |
2776 | |
2777 | TemplateSpecializationKind |
2778 | getTemplateSpecializationKindForInstantiation() const; |
2779 | |
2780 | |
2781 | |
2782 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
2783 | SourceLocation PointOfInstantiation = SourceLocation()); |
2784 | |
2785 | |
2786 | |
2787 | |
2788 | |
2789 | |
2790 | |
2791 | SourceLocation getPointOfInstantiation() const; |
2792 | |
2793 | |
2794 | |
2795 | bool isOutOfLine() const override; |
2796 | |
2797 | |
2798 | |
2799 | |
2800 | |
2801 | unsigned getMemoryFunctionKind() const; |
2802 | |
2803 | |
2804 | |
2805 | unsigned getODRHash(); |
2806 | |
2807 | |
2808 | |
2809 | unsigned getODRHash() const; |
2810 | |
2811 | |
2812 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
2813 | static bool classofKind(Kind K) { |
2814 | return K >= firstFunction && K <= lastFunction; |
2815 | } |
2816 | static DeclContext *castToDeclContext(const FunctionDecl *D) { |
2817 | return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D)); |
2818 | } |
2819 | static FunctionDecl *castFromDeclContext(const DeclContext *DC) { |
2820 | return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC)); |
2821 | } |
2822 | }; |
2823 | |
2824 | |
2825 | class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> { |
2826 | unsigned BitField : 1; |
2827 | unsigned Mutable : 1; |
2828 | mutable unsigned CachedFieldIndex : 30; |
2829 | |
2830 | |
2831 | |
2832 | |
2833 | |
2834 | enum InitStorageKind { |
2835 | |
2836 | |
2837 | |
2838 | ISK_NoInit = (unsigned) ICIS_NoInit, |
2839 | |
2840 | |
2841 | |
2842 | ISK_InClassCopyInit = (unsigned) ICIS_CopyInit, |
2843 | |
2844 | |
2845 | |
2846 | ISK_InClassListInit = (unsigned) ICIS_ListInit, |
2847 | |
2848 | |
2849 | |
2850 | ISK_CapturedVLAType, |
2851 | }; |
2852 | |
2853 | |
2854 | |
2855 | struct InitAndBitWidth { |
2856 | Expr *Init; |
2857 | Expr *BitWidth; |
2858 | }; |
2859 | |
2860 | |
2861 | |
2862 | |
2863 | |
2864 | |
2865 | |
2866 | |
2867 | |
2868 | |
2869 | llvm::PointerIntPair<void *, 2, InitStorageKind> InitStorage; |
2870 | |
2871 | protected: |
2872 | FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc, |
2873 | SourceLocation IdLoc, IdentifierInfo *Id, |
2874 | QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
2875 | InClassInitStyle InitStyle) |
2876 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
2877 | BitField(false), Mutable(Mutable), CachedFieldIndex(0), |
2878 | InitStorage(nullptr, (InitStorageKind) InitStyle) { |
2879 | if (BW) |
2880 | setBitWidth(BW); |
2881 | } |
2882 | |
2883 | public: |
2884 | friend class ASTDeclReader; |
2885 | friend class ASTDeclWriter; |
2886 | |
2887 | static FieldDecl *Create(const ASTContext &C, DeclContext *DC, |
2888 | SourceLocation StartLoc, SourceLocation IdLoc, |
2889 | IdentifierInfo *Id, QualType T, |
2890 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
2891 | InClassInitStyle InitStyle); |
2892 | |
2893 | static FieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2894 | |
2895 | |
2896 | |
2897 | unsigned getFieldIndex() const; |
2898 | |
2899 | |
2900 | bool isMutable() const { return Mutable; } |
2901 | |
2902 | |
2903 | bool isBitField() const { return BitField; } |
2904 | |
2905 | |
2906 | bool isUnnamedBitfield() const { return isBitField() && !getDeclName(); } |
2907 | |
2908 | |
2909 | |
2910 | |
2911 | |
2912 | bool isAnonymousStructOrUnion() const; |
2913 | |
2914 | Expr *getBitWidth() const { |
2915 | if (!BitField) |
2916 | return nullptr; |
2917 | void *Ptr = InitStorage.getPointer(); |
2918 | if (getInClassInitStyle()) |
2919 | return static_cast<InitAndBitWidth*>(Ptr)->BitWidth; |
2920 | return static_cast<Expr*>(Ptr); |
2921 | } |
2922 | |
2923 | unsigned getBitWidthValue(const ASTContext &Ctx) const; |
2924 | |
2925 | |
2926 | |
2927 | void setBitWidth(Expr *Width) { |
2928 | assert(!hasCapturedVLAType() && !BitField && |
2929 | "bit width or captured type already set"); |
2930 | assert(Width && "no bit width specified"); |
2931 | InitStorage.setPointer( |
2932 | InitStorage.getInt() |
2933 | ? new (getASTContext()) |
2934 | InitAndBitWidth{getInClassInitializer(), Width} |
2935 | : static_cast<void*>(Width)); |
2936 | BitField = true; |
2937 | } |
2938 | |
2939 | |
2940 | |
2941 | void removeBitWidth() { |
2942 | assert(isBitField() && "no bitfield width to remove"); |
2943 | InitStorage.setPointer(getInClassInitializer()); |
2944 | BitField = false; |
2945 | } |
2946 | |
2947 | |
2948 | |
2949 | |
2950 | bool isZeroLengthBitField(const ASTContext &Ctx) const; |
2951 | |
2952 | |
2953 | |
2954 | |
2955 | bool isZeroSize(const ASTContext &Ctx) const; |
2956 | |
2957 | |
2958 | InClassInitStyle getInClassInitStyle() const { |
2959 | InitStorageKind storageKind = InitStorage.getInt(); |
2960 | return (storageKind == ISK_CapturedVLAType |
2961 | ? ICIS_NoInit : (InClassInitStyle) storageKind); |
2962 | } |
2963 | |
2964 | |
2965 | bool hasInClassInitializer() const { |
2966 | return getInClassInitStyle() != ICIS_NoInit; |
2967 | } |
2968 | |
2969 | |
2970 | |
2971 | |
2972 | Expr *getInClassInitializer() const { |
2973 | if (!hasInClassInitializer()) |
2974 | return nullptr; |
2975 | void *Ptr = InitStorage.getPointer(); |
2976 | if (BitField) |
2977 | return static_cast<InitAndBitWidth*>(Ptr)->Init; |
2978 | return static_cast<Expr*>(Ptr); |
2979 | } |
2980 | |
2981 | |
2982 | void setInClassInitializer(Expr *Init) { |
2983 | assert(hasInClassInitializer() && !getInClassInitializer()); |
2984 | if (BitField) |
2985 | static_cast<InitAndBitWidth*>(InitStorage.getPointer())->Init = Init; |
2986 | else |
2987 | InitStorage.setPointer(Init); |
2988 | } |
2989 | |
2990 | |
2991 | void removeInClassInitializer() { |
2992 | assert(hasInClassInitializer() && "no initializer to remove"); |
2993 | InitStorage.setPointerAndInt(getBitWidth(), ISK_NoInit); |
2994 | } |
2995 | |
2996 | |
2997 | |
2998 | bool hasCapturedVLAType() const { |
2999 | return InitStorage.getInt() == ISK_CapturedVLAType; |
3000 | } |
3001 | |
3002 | |
3003 | const VariableArrayType *getCapturedVLAType() const { |
3004 | return hasCapturedVLAType() ? static_cast<const VariableArrayType *>( |
3005 | InitStorage.getPointer()) |
3006 | : nullptr; |
3007 | } |
3008 | |
3009 | |
3010 | void setCapturedVLAType(const VariableArrayType *VLAType); |
3011 | |
3012 | |
3013 | |
3014 | |
3015 | |
3016 | |
3017 | const RecordDecl *getParent() const { |
3018 | return dyn_cast<RecordDecl>(getDeclContext()); |
3019 | } |
3020 | |
3021 | RecordDecl *getParent() { |
3022 | return dyn_cast<RecordDecl>(getDeclContext()); |
3023 | } |
3024 | |
3025 | SourceRange getSourceRange() const override LLVM_READONLY; |
3026 | |
3027 | |
3028 | FieldDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3029 | const FieldDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3030 | |
3031 | |
3032 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3033 | static bool classofKind(Kind K) { return K >= firstField && K <= lastField; } |
3034 | }; |
3035 | |
3036 | |
3037 | |
3038 | |
3039 | |
3040 | class EnumConstantDecl : public ValueDecl, public Mergeable<EnumConstantDecl> { |
3041 | Stmt *Init; |
3042 | llvm::APSInt Val; |
3043 | |
3044 | protected: |
3045 | EnumConstantDecl(DeclContext *DC, SourceLocation L, |
3046 | IdentifierInfo *Id, QualType T, Expr *E, |
3047 | const llvm::APSInt &V) |
3048 | : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {} |
3049 | |
3050 | public: |
3051 | friend class StmtIteratorBase; |
3052 | |
3053 | static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC, |
3054 | SourceLocation L, IdentifierInfo *Id, |
3055 | QualType T, Expr *E, |
3056 | const llvm::APSInt &V); |
3057 | static EnumConstantDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3058 | |
3059 | const Expr *getInitExpr() const { return (const Expr*) Init; } |
3060 | Expr *getInitExpr() { return (Expr*) Init; } |
3061 | const llvm::APSInt &getInitVal() const { return Val; } |
3062 | |
3063 | void setInitExpr(Expr *E) { Init = (Stmt*) E; } |
3064 | void setInitVal(const llvm::APSInt &V) { Val = V; } |
3065 | |
3066 | SourceRange getSourceRange() const override LLVM_READONLY; |
3067 | |
3068 | |
3069 | EnumConstantDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3070 | const EnumConstantDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3071 | |
3072 | |
3073 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3074 | static bool classofKind(Kind K) { return K == EnumConstant; } |
3075 | }; |
3076 | |
3077 | |
3078 | |
3079 | class IndirectFieldDecl : public ValueDecl, |
3080 | public Mergeable<IndirectFieldDecl> { |
3081 | NamedDecl **Chaining; |
3082 | unsigned ChainingSize; |
3083 | |
3084 | IndirectFieldDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
3085 | DeclarationName N, QualType T, |
3086 | MutableArrayRef<NamedDecl *> CH); |
3087 | |
3088 | void anchor() override; |
3089 | |
3090 | public: |
3091 | friend class ASTDeclReader; |
3092 | |
3093 | static IndirectFieldDecl *Create(ASTContext &C, DeclContext *DC, |
3094 | SourceLocation L, IdentifierInfo *Id, |
3095 | QualType T, llvm::MutableArrayRef<NamedDecl *> CH); |
3096 | |
3097 | static IndirectFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3098 | |
3099 | using chain_iterator = ArrayRef<NamedDecl *>::const_iterator; |
3100 | |
3101 | ArrayRef<NamedDecl *> chain() const { |
3102 | return llvm::makeArrayRef(Chaining, ChainingSize); |
3103 | } |
3104 | chain_iterator chain_begin() const { return chain().begin(); } |
3105 | chain_iterator chain_end() const { return chain().end(); } |
3106 | |
3107 | unsigned getChainingSize() const { return ChainingSize; } |
3108 | |
3109 | FieldDecl *getAnonField() const { |
3110 | assert(chain().size() >= 2); |
3111 | return cast<FieldDecl>(chain().back()); |
3112 | } |
3113 | |
3114 | VarDecl *getVarDecl() const { |
3115 | assert(chain().size() >= 2); |
3116 | return dyn_cast<VarDecl>(chain().front()); |
3117 | } |
3118 | |
3119 | IndirectFieldDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3120 | const IndirectFieldDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3121 | |
3122 | |
3123 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3124 | static bool classofKind(Kind K) { return K == IndirectField; } |
3125 | }; |
3126 | |
3127 | |
3128 | class TypeDecl : public NamedDecl { |
3129 | friend class ASTContext; |
3130 | |
3131 | |
3132 | |
3133 | |
3134 | |
3135 | mutable const Type *TypeForDecl = nullptr; |
3136 | |
3137 | |
3138 | SourceLocation LocStart; |
3139 | |
3140 | void anchor() override; |
3141 | |
3142 | protected: |
3143 | TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id, |
3144 | SourceLocation StartL = SourceLocation()) |
3145 | : NamedDecl(DK, DC, L, Id), LocStart(StartL) {} |
3146 | |
3147 | public: |
3148 | |
3149 | |
3150 | |
3151 | |
3152 | const Type *getTypeForDecl() const { return TypeForDecl; } |
3153 | void setTypeForDecl(const Type *TD) { TypeForDecl = TD; } |
3154 | |
3155 | SourceLocation getBeginLoc() const LLVM_READONLY { return LocStart; } |
3156 | void setLocStart(SourceLocation L) { LocStart = L; } |
3157 | SourceRange getSourceRange() const override LLVM_READONLY { |
3158 | if (LocStart.isValid()) |
3159 | return SourceRange(LocStart, getLocation()); |
3160 | else |
3161 | return SourceRange(getLocation()); |
3162 | } |
3163 | |
3164 | |
3165 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3166 | static bool classofKind(Kind K) { return K >= firstType && K <= lastType; } |
3167 | }; |
3168 | |
3169 | |
3170 | class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> { |
3171 | struct alignas(8) ModedTInfo { |
3172 | TypeSourceInfo *first; |
3173 | QualType second; |
3174 | }; |
3175 | |
3176 | |
3177 | |
3178 | mutable llvm::PointerIntPair< |
3179 | llvm::PointerUnion<TypeSourceInfo *, ModedTInfo *>, 2> |
3180 | MaybeModedTInfo; |
3181 | |
3182 | void anchor() override; |
3183 | |
3184 | protected: |
3185 | TypedefNameDecl(Kind DK, ASTContext &C, DeclContext *DC, |
3186 | SourceLocation StartLoc, SourceLocation IdLoc, |
3187 | IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3188 | : TypeDecl(DK, DC, IdLoc, Id, StartLoc), redeclarable_base(C), |
3189 | MaybeModedTInfo(TInfo, 0) {} |
3190 | |
3191 | using redeclarable_base = Redeclarable<TypedefNameDecl>; |
3192 | |
3193 | TypedefNameDecl *getNextRedeclarationImpl() override { |
3194 | return getNextRedeclaration(); |
3195 | } |
3196 | |
3197 | TypedefNameDecl *getPreviousDeclImpl() override { |
3198 | return getPreviousDecl(); |
3199 | } |
3200 | |
3201 | TypedefNameDecl *getMostRecentDeclImpl() override { |
3202 | return getMostRecentDecl(); |
3203 | } |
3204 | |
3205 | public: |
3206 | using redecl_range = redeclarable_base::redecl_range; |
3207 | using redecl_iterator = redeclarable_base::redecl_iterator; |
3208 | |
3209 | using redeclarable_base::redecls_begin; |
3210 | using redeclarable_base::redecls_end; |
3211 | using redeclarable_base::redecls; |
3212 | using redeclarable_base::getPreviousDecl; |
3213 | using redeclarable_base::getMostRecentDecl; |
3214 | using redeclarable_base::isFirstDecl; |
3215 | |
3216 | bool isModed() const { |
3217 | return MaybeModedTInfo.getPointer().is<ModedTInfo *>(); |
3218 | } |
3219 | |
3220 | TypeSourceInfo *getTypeSourceInfo() const { |
3221 | return isModed() ? MaybeModedTInfo.getPointer().get<ModedTInfo *>()->first |
3222 | : MaybeModedTInfo.getPointer().get<TypeSourceInfo *>(); |
3223 | } |
3224 | |
3225 | QualType getUnderlyingType() const { |
3226 | return isModed() ? MaybeModedTInfo.getPointer().get<ModedTInfo *>()->second |
3227 | : MaybeModedTInfo.getPointer() |
3228 | .get<TypeSourceInfo *>() |
3229 | ->getType(); |
3230 | } |
3231 | |
3232 | void setTypeSourceInfo(TypeSourceInfo *newType) { |
3233 | MaybeModedTInfo.setPointer(newType); |
3234 | } |
3235 | |
3236 | void setModedTypeSourceInfo(TypeSourceInfo *unmodedTSI, QualType modedTy) { |
3237 | MaybeModedTInfo.setPointer(new (getASTContext(), 8) |
3238 | ModedTInfo({unmodedTSI, modedTy})); |
3239 | } |
3240 | |
3241 | |
3242 | TypedefNameDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3243 | const TypedefNameDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3244 | |
3245 | |
3246 | |
3247 | |
3248 | |
3249 | |
3250 | TagDecl *getAnonDeclWithTypedefName(bool AnyRedecl = false) const; |
3251 | |
3252 | |
3253 | |
3254 | bool isTransparentTag() const { |
3255 | if (MaybeModedTInfo.getInt()) |
3256 | return MaybeModedTInfo.getInt() & 0x2; |
3257 | return isTransparentTagSlow(); |
3258 | } |
3259 | |
3260 | |
3261 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3262 | static bool classofKind(Kind K) { |
3263 | return K >= firstTypedefName && K <= lastTypedefName; |
3264 | } |
3265 | |
3266 | private: |
3267 | bool isTransparentTagSlow() const; |
3268 | }; |
3269 | |
3270 | |
3271 | |
3272 | class TypedefDecl : public TypedefNameDecl { |
3273 | TypedefDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3274 | SourceLocation IdLoc, IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3275 | : TypedefNameDecl(Typedef, C, DC, StartLoc, IdLoc, Id, TInfo) {} |
3276 | |
3277 | public: |
3278 | static TypedefDecl *Create(ASTContext &C, DeclContext *DC, |
3279 | SourceLocation StartLoc, SourceLocation IdLoc, |
3280 | IdentifierInfo *Id, TypeSourceInfo *TInfo); |
3281 | static TypedefDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3282 | |
3283 | SourceRange getSourceRange() const override LLVM_READONLY; |
3284 | |
3285 | |
3286 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3287 | static bool classofKind(Kind K) { return K == Typedef; } |
3288 | }; |
3289 | |
3290 | |
3291 | |
3292 | class TypeAliasDecl : public TypedefNameDecl { |
3293 | |
3294 | TypeAliasTemplateDecl *Template; |
3295 | |
3296 | TypeAliasDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3297 | SourceLocation IdLoc, IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3298 | : TypedefNameDecl(TypeAlias, C, DC, StartLoc, IdLoc, Id, TInfo), |
3299 | Template(nullptr) {} |
3300 | |
3301 | public: |
3302 | static TypeAliasDecl *Create(ASTContext &C, DeclContext *DC, |
3303 | SourceLocation StartLoc, SourceLocation IdLoc, |
3304 | IdentifierInfo *Id, TypeSourceInfo *TInfo); |
3305 | static TypeAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3306 | |
3307 | SourceRange getSourceRange() const override LLVM_READONLY; |
3308 | |
3309 | TypeAliasTemplateDecl *getDescribedAliasTemplate() const { return Template; } |
3310 | void setDescribedAliasTemplate(TypeAliasTemplateDecl *TAT) { Template = TAT; } |
3311 | |
3312 | |
3313 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3314 | static bool classofKind(Kind K) { return K == TypeAlias; } |
3315 | }; |
3316 | |
3317 | |
3318 | class TagDecl : public TypeDecl, |
3319 | public DeclContext, |
3320 | public Redeclarable<TagDecl> { |
3321 | |
3322 | |
3323 | public: |
3324 | |
3325 | using TagKind = TagTypeKind; |
3326 | |
3327 | private: |
3328 | SourceRange BraceRange; |
3329 | |
3330 | |
3331 | |
3332 | using ExtInfo = QualifierInfo; |
3333 | |
3334 | |
3335 | |
3336 | |
3337 | |
3338 | |
3339 | |
3340 | |
3341 | |
3342 | llvm::PointerUnion<TypedefNameDecl *, ExtInfo *> TypedefNameDeclOrQualifier; |
3343 | |
3344 | bool hasExtInfo() const { return TypedefNameDeclOrQualifier.is<ExtInfo *>(); } |
3345 | ExtInfo *getExtInfo() { return TypedefNameDeclOrQualifier.get<ExtInfo *>(); } |
3346 | const ExtInfo *getExtInfo() const { |
3347 | return TypedefNameDeclOrQualifier.get<ExtInfo *>(); |
3348 | } |
3349 | |
3350 | protected: |
3351 | TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
3352 | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
3353 | SourceLocation StartL); |
3354 | |
3355 | using redeclarable_base = Redeclarable<TagDecl>; |
3356 | |
3357 | TagDecl *getNextRedeclarationImpl() override { |
3358 | return getNextRedeclaration(); |
3359 | } |
3360 | |
3361 | TagDecl *getPreviousDeclImpl() override { |
3362 | return getPreviousDecl(); |
3363 | } |
3364 | |
3365 | TagDecl *getMostRecentDeclImpl() override { |
3366 | return getMostRecentDecl(); |
3367 | } |
3368 | |
3369 | |
3370 | |
3371 | |
3372 | void completeDefinition(); |
3373 | |
3374 | |
3375 | void setBeingDefined(bool V = true) { TagDeclBits.IsBeingDefined = V; } |
3376 | |
3377 | |
3378 | |
3379 | |
3380 | |
3381 | void setMayHaveOutOfDateDef(bool V = true) { |
3382 | TagDeclBits.MayHaveOutOfDateDef = V; |
3383 | } |
3384 | |
3385 | public: |
3386 | friend class ASTDeclReader; |
3387 | friend class ASTDeclWriter; |
3388 | |
3389 | using redecl_range = redeclarable_base::redecl_range; |
3390 | using redecl_iterator = redeclarable_base::redecl_iterator; |
3391 | |
3392 | using redeclarable_base::redecls_begin; |
3393 | using redeclarable_base::redecls_end; |
3394 | using redeclarable_base::redecls; |
3395 | using redeclarable_base::getPreviousDecl; |
3396 | using redeclarable_base::getMostRecentDecl; |
3397 | using redeclarable_base::isFirstDecl; |
3398 | |
3399 | SourceRange getBraceRange() const { return BraceRange; } |
3400 | void setBraceRange(SourceRange R) { BraceRange = R; } |
3401 | |
3402 | |
3403 | |
3404 | SourceLocation getInnerLocStart() const { return getBeginLoc(); } |
3405 | |
3406 | |
3407 | |
3408 | SourceLocation getOuterLocStart() const; |
3409 | SourceRange getSourceRange() const override LLVM_READONLY; |
3410 | |
3411 | TagDecl *getCanonicalDecl() override; |
3412 | const TagDecl *getCanonicalDecl() const { |
3413 | return const_cast<TagDecl*>(this)->getCanonicalDecl(); |
3414 | } |
3415 | |
3416 | |
3417 | |
3418 | bool isThisDeclarationADefinition() const { |
3419 | return isCompleteDefinition(); |
3420 | } |
3421 | |
3422 | |
3423 | bool isCompleteDefinition() const { return TagDeclBits.IsCompleteDefinition; } |
3424 | |
3425 | |
3426 | void setCompleteDefinition(bool V = true) { |
3427 | TagDeclBits.IsCompleteDefinition = V; |
3428 | } |
3429 | |
3430 | |
3431 | |
3432 | bool isCompleteDefinitionRequired() const { |
3433 | return TagDeclBits.IsCompleteDefinitionRequired; |
3434 | } |
3435 | |
3436 | |
3437 | |
3438 | void setCompleteDefinitionRequired(bool V = true) { |
3439 | TagDeclBits.IsCompleteDefinitionRequired = V; |
3440 | } |
3441 | |
3442 | |
3443 | bool isBeingDefined() const { return TagDeclBits.IsBeingDefined; } |
3444 | |
3445 | |
3446 | |
3447 | bool isEmbeddedInDeclarator() const { |
3448 | return TagDeclBits.IsEmbeddedInDeclarator; |
3449 | } |
3450 | |
3451 | |
3452 | |
3453 | void setEmbeddedInDeclarator(bool isInDeclarator) { |
3454 | TagDeclBits.IsEmbeddedInDeclarator = isInDeclarator; |
3455 | } |
3456 | |
3457 | |
3458 | bool isFreeStanding() const { return TagDeclBits.IsFreeStanding; } |
3459 | |
3460 | |
3461 | void setFreeStanding(bool isFreeStanding = true) { |
3462 | TagDeclBits.IsFreeStanding = isFreeStanding; |
3463 | } |
3464 | |
3465 | |
3466 | |
3467 | |
3468 | |
3469 | bool mayHaveOutOfDateDef() const { return TagDeclBits.MayHaveOutOfDateDef; } |
3470 | |
3471 | |
3472 | |
3473 | |
3474 | bool isDependentType() const { return isDependentContext(); } |
3475 | |
3476 | |
3477 | |
3478 | |
3479 | |
3480 | |
3481 | void startDefinition(); |
3482 | |
3483 | |
3484 | |
3485 | |
3486 | |
3487 | |
3488 | |
3489 | |
3490 | |
3491 | TagDecl *getDefinition() const; |
3492 | |
3493 | StringRef getKindName() const { |
3494 | return TypeWithKeyword::getTagTypeKindName(getTagKind()); |
3495 | } |
3496 | |
3497 | TagKind getTagKind() const { |
3498 | return static_cast<TagKind>(TagDeclBits.TagDeclKind); |
3499 | } |
3500 | |
3501 | void setTagKind(TagKind TK) { TagDeclBits.TagDeclKind = TK; } |
3502 | |
3503 | bool isStruct() const { return getTagKind() == TTK_Struct; } |
3504 | bool isInterface() const { return getTagKind() == TTK_Interface; } |
3505 | bool isClass() const { return getTagKind() == TTK_Class; } |
3506 | bool isUnion() const { return getTagKind() == TTK_Union; } |
3507 | bool isEnum() const { return getTagKind() == TTK_Enum; } |
3508 | |
3509 | |
3510 | |
3511 | |
3512 | |
3513 | |
3514 | |
3515 | |
3516 | |
3517 | |
3518 | |
3519 | |
3520 | |
3521 | |
3522 | |
3523 | |
3524 | bool hasNameForLinkage() const { |
3525 | return (getDeclName() || getTypedefNameForAnonDecl()); |
3526 | } |
3527 | |
3528 | TypedefNameDecl *getTypedefNameForAnonDecl() const { |
3529 | return hasExtInfo() ? nullptr |
3530 | : TypedefNameDeclOrQualifier.get<TypedefNameDecl *>(); |
3531 | } |
3532 | |
3533 | void setTypedefNameForAnonDecl(TypedefNameDecl *TDD); |
3534 | |
3535 | |
3536 | |
3537 | NestedNameSpecifier *getQualifier() const { |
3538 | return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() |
3539 | : nullptr; |
3540 | } |
3541 | |
3542 | |
3543 | |
3544 | |
3545 | NestedNameSpecifierLoc getQualifierLoc() const { |
3546 | return hasExtInfo() ? getExtInfo()->QualifierLoc |
3547 | : NestedNameSpecifierLoc(); |
3548 | } |
3549 | |
3550 | void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); |
3551 | |
3552 | unsigned getNumTemplateParameterLists() const { |
3553 | return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; |
3554 | } |
3555 | |
3556 | TemplateParameterList *getTemplateParameterList(unsigned i) const { |
3557 | assert(i < getNumTemplateParameterLists()); |
3558 | return getExtInfo()->TemplParamLists[i]; |
3559 | } |
3560 | |
3561 | void setTemplateParameterListsInfo(ASTContext &Context, |
3562 | ArrayRef<TemplateParameterList *> TPLists); |
3563 | |
3564 | |
3565 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3566 | static bool classofKind(Kind K) { return K >= firstTag && K <= lastTag; } |
3567 | |
3568 | static DeclContext *castToDeclContext(const TagDecl *D) { |
3569 | return static_cast<DeclContext *>(const_cast<TagDecl*>(D)); |
3570 | } |
3571 | |
3572 | static TagDecl *castFromDeclContext(const DeclContext *DC) { |
3573 | return static_cast<TagDecl *>(const_cast<DeclContext*>(DC)); |
3574 | } |
3575 | }; |
3576 | |
3577 | |
3578 | |
3579 | |
3580 | class EnumDecl : public TagDecl { |
3581 | |
3582 | |
3583 | |
3584 | |
3585 | |
3586 | |
3587 | |
3588 | |
3589 | |
3590 | |
3591 | |
3592 | |
3593 | |
3594 | |
3595 | |
3596 | |
3597 | |
3598 | llvm::PointerUnion<const Type *, TypeSourceInfo *> IntegerType; |
3599 | |
3600 | |
3601 | |
3602 | |
3603 | |
3604 | QualType PromotionType; |
3605 | |
3606 | |
3607 | |
3608 | |
3609 | MemberSpecializationInfo *SpecializationInfo = nullptr; |
3610 | |
3611 | |
3612 | |
3613 | |
3614 | unsigned ODRHash; |
3615 | |
3616 | EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3617 | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
3618 | bool Scoped, bool ScopedUsingClassTag, bool Fixed); |
3619 | |
3620 | void anchor() override; |
3621 | |
3622 | void setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
3623 | TemplateSpecializationKind TSK); |
3624 | |
3625 | |
3626 | |
3627 | void setNumPositiveBits(unsigned Num) { |
3628 | EnumDeclBits.NumPositiveBits = Num; |
3629 | assert(EnumDeclBits.NumPositiveBits == Num && "can't store this bitcount"); |
3630 | } |
3631 | |
3632 | |
3633 | |
3634 | void setNumNegativeBits(unsigned Num) { EnumDeclBits.NumNegativeBits = Num; } |
3635 | |
3636 | public: |
3637 | |
3638 | |
3639 | void setScoped(bool Scoped = true) { EnumDeclBits.IsScoped = Scoped; } |
3640 | |
3641 | |
3642 | |
3643 | |
3644 | |
3645 | void setScopedUsingClassTag(bool ScopedUCT = true) { |
3646 | EnumDeclBits.IsScopedUsingClassTag = ScopedUCT; |
3647 | } |
3648 | |
3649 | |
3650 | |
3651 | void setFixed(bool Fixed = true) { EnumDeclBits.IsFixed = Fixed; } |
3652 | |
3653 | private: |
3654 | |
3655 | bool hasODRHash() const { return EnumDeclBits.HasODRHash; } |
3656 | void setHasODRHash(bool Hash = true) { EnumDeclBits.HasODRHash = Hash; } |
3657 | |
3658 | public: |
3659 | friend class ASTDeclReader; |
3660 | |
3661 | EnumDecl *getCanonicalDecl() override { |
3662 | return cast<EnumDecl>(TagDecl::getCanonicalDecl()); |
3663 | } |
3664 | const EnumDecl *getCanonicalDecl() const { |
3665 | return const_cast<EnumDecl*>(this)->getCanonicalDecl(); |
3666 | } |
3667 | |
3668 | EnumDecl *getPreviousDecl() { |
3669 | return cast_or_null<EnumDecl>( |
3670 | static_cast<TagDecl *>(this)->getPreviousDecl()); |
3671 | } |
3672 | const EnumDecl *getPreviousDecl() const { |
3673 | return const_cast<EnumDecl*>(this)->getPreviousDecl(); |
3674 | } |
3675 | |
3676 | EnumDecl *getMostRecentDecl() { |
3677 | return cast<EnumDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl()); |
3678 | } |
3679 | const EnumDecl *getMostRecentDecl() const { |
3680 | return const_cast<EnumDecl*>(this)->getMostRecentDecl(); |
3681 | } |
3682 | |
3683 | EnumDecl *getDefinition() const { |
3684 | return cast_or_null<EnumDecl>(TagDecl::getDefinition()); |
3685 | } |
3686 | |
3687 | static EnumDecl *Create(ASTContext &C, DeclContext *DC, |
3688 | SourceLocation StartLoc, SourceLocation IdLoc, |
3689 | IdentifierInfo *Id, EnumDecl *PrevDecl, |
3690 | bool IsScoped, bool IsScopedUsingClassTag, |
3691 | bool IsFixed); |
3692 | static EnumDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3693 | |
3694 | |
3695 | |
3696 | |
3697 | |
3698 | |
3699 | void completeDefinition(QualType NewType, |
3700 | QualType PromotionType, |
3701 | unsigned NumPositiveBits, |
3702 | unsigned NumNegativeBits); |
3703 | |
3704 | |
3705 | using enumerator_iterator = specific_decl_iterator<EnumConstantDecl>; |
3706 | using enumerator_range = |
3707 | llvm::iterator_range<specific_decl_iterator<EnumConstantDecl>>; |
3708 | |
3709 | enumerator_range enumerators() const { |
3710 | return enumerator_range(enumerator_begin(), enumerator_end()); |
3711 | } |
3712 | |
3713 | enumerator_iterator enumerator_begin() const { |
3714 | const EnumDecl *E = getDefinition(); |
3715 | if (!E) |
3716 | E = this; |
3717 | return enumerator_iterator(E->decls_begin()); |
3718 | } |
3719 | |
3720 | enumerator_iterator enumerator_end() const { |
3721 | const EnumDecl *E = getDefinition(); |
3722 | if (!E) |
3723 | E = this; |
3724 | return enumerator_iterator(E->decls_end()); |
3725 | } |
3726 | |
3727 | |
3728 | QualType getPromotionType() const { return PromotionType; } |
3729 | |
3730 | |
3731 | void setPromotionType(QualType T) { PromotionType = T; } |
3732 | |
3733 | |
3734 | |
3735 | |
3736 | QualType getIntegerType() const { |
3737 | if (!IntegerType) |
3738 | return QualType(); |
3739 | if (const Type *T = IntegerType.dyn_cast<const Type*>()) |
3740 | return QualType(T, 0); |
3741 | return IntegerType.get<TypeSourceInfo*>()->getType().getUnqualifiedType(); |
3742 | } |
3743 | |
3744 | |
3745 | void setIntegerType(QualType T) { IntegerType = T.getTypePtrOrNull(); } |
3746 | |
3747 | |
3748 | void setIntegerTypeSourceInfo(TypeSourceInfo *TInfo) { IntegerType = TInfo; } |
3749 | |
3750 | |
3751 | |
3752 | TypeSourceInfo *getIntegerTypeSourceInfo() const { |
3753 | return IntegerType.dyn_cast<TypeSourceInfo*>(); |
3754 | } |
3755 | |
3756 | |
3757 | |
3758 | SourceRange getIntegerTypeRange() const LLVM_READONLY; |
3759 | |
3760 | |
3761 | |
3762 | unsigned getNumPositiveBits() const { return EnumDeclBits.NumPositiveBits; } |
3763 | |
3764 | |
3765 | |
3766 | |
3767 | |
3768 | |
3769 | |
3770 | |
3771 | |
3772 | |
3773 | unsigned getNumNegativeBits() const { return EnumDeclBits.NumNegativeBits; } |
3774 | |
3775 | |
3776 | bool isScoped() const { return EnumDeclBits.IsScoped; } |
3777 | |
3778 | |
3779 | bool isScopedUsingClassTag() const { |
3780 | return EnumDeclBits.IsScopedUsingClassTag; |
3781 | } |
3782 | |
3783 | |
3784 | |
3785 | bool isFixed() const { return EnumDeclBits.IsFixed; } |
3786 | |
3787 | unsigned getODRHash(); |
3788 | |
3789 | |
3790 | bool isComplete() const { |
3791 | |
3792 | |
3793 | return isCompleteDefinition() || IntegerType; |
3794 | } |
3795 | |
3796 | |
3797 | |
3798 | bool isClosed() const; |
3799 | |
3800 | |
3801 | |
3802 | bool isClosedFlag() const; |
3803 | |
3804 | |
3805 | |
3806 | bool isClosedNonFlag() const; |
3807 | |
3808 | |
3809 | |
3810 | EnumDecl *getTemplateInstantiationPattern() const; |
3811 | |
3812 | |
3813 | |
3814 | |
3815 | EnumDecl *getInstantiatedFromMemberEnum() const; |
3816 | |
3817 | |
3818 | |
3819 | |
3820 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
3821 | |
3822 | |
3823 | |
3824 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
3825 | SourceLocation PointOfInstantiation = SourceLocation()); |
3826 | |
3827 | |
3828 | |
3829 | |
3830 | MemberSpecializationInfo *getMemberSpecializationInfo() const { |
3831 | return SpecializationInfo; |
3832 | } |
3833 | |
3834 | |
3835 | |
3836 | void setInstantiationOfMemberEnum(EnumDecl *ED, |
3837 | TemplateSpecializationKind TSK) { |
3838 | setInstantiationOfMemberEnum(getASTContext(), ED, TSK); |
3839 | } |
3840 | |
3841 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3842 | static bool classofKind(Kind K) { return K == Enum; } |
3843 | }; |
3844 | |
3845 | |
3846 | |
3847 | |
3848 | |
3849 | class RecordDecl : public TagDecl { |
3850 | |
3851 | |
3852 | public: |
3853 | friend class DeclContext; |
3854 | |
3855 | |
3856 | |
3857 | |
3858 | enum ArgPassingKind : unsigned { |
3859 | |
3860 | APK_CanPassInRegs, |
3861 | |
3862 | |
3863 | |
3864 | |
3865 | |
3866 | |
3867 | |
3868 | |
3869 | APK_CannotPassInRegs, |
3870 | |
3871 | |
3872 | |
3873 | |
3874 | APK_CanNeverPassInRegs |
3875 | }; |
3876 | |
3877 | protected: |
3878 | RecordDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
3879 | SourceLocation StartLoc, SourceLocation IdLoc, |
3880 | IdentifierInfo *Id, RecordDecl *PrevDecl); |
3881 | |
3882 | public: |
3883 | static RecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
3884 | SourceLocation StartLoc, SourceLocation IdLoc, |
3885 | IdentifierInfo *Id, RecordDecl* PrevDecl = nullptr); |
3886 | static RecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID); |
3887 | |
3888 | RecordDecl *getPreviousDecl() { |
3889 | return cast_or_null<RecordDecl>( |
3890 | static_cast<TagDecl *>(this)->getPreviousDecl()); |
3891 | } |
3892 | const RecordDecl *getPreviousDecl() const { |
3893 | return const_cast<RecordDecl*>(this)->getPreviousDecl(); |
3894 | } |
3895 | |
3896 | RecordDecl *getMostRecentDecl() { |
3897 | return cast<RecordDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl()); |
3898 | } |
3899 | const RecordDecl *getMostRecentDecl() const { |
3900 | return const_cast<RecordDecl*>(this)->getMostRecentDecl(); |
3901 | } |
3902 | |
3903 | bool hasFlexibleArrayMember() const { |
3904 | return RecordDeclBits.HasFlexibleArrayMember; |
3905 | } |
3906 | |
3907 | void setHasFlexibleArrayMember(bool V) { |
3908 | RecordDeclBits.HasFlexibleArrayMember = V; |
3909 | } |
3910 | |
3911 | |
3912 | |
3913 | |
3914 | |
3915 | |
3916 | |
3917 | |
3918 | |
3919 | |
3920 | |
3921 | |
3922 | bool isAnonymousStructOrUnion() const { |
3923 | return RecordDeclBits.AnonymousStructOrUnion; |
3924 | } |
3925 | |
3926 | void setAnonymousStructOrUnion(bool Anon) { |
3927 | RecordDeclBits.AnonymousStructOrUnion = Anon; |
3928 | } |
3929 | |
3930 | bool hasObjectMember() const { return RecordDeclBits.HasObjectMember; } |
3931 | void setHasObjectMember(bool val) { RecordDeclBits.HasObjectMember = val; } |
3932 | |
3933 | bool hasVolatileMember() const { return RecordDeclBits.HasVolatileMember; } |
3934 | |
3935 | void setHasVolatileMember(bool val) { |
3936 | RecordDeclBits.HasVolatileMember = val; |
3937 | } |
3938 | |
3939 | bool hasLoadedFieldsFromExternalStorage() const { |
3940 | return RecordDeclBits.LoadedFieldsFromExternalStorage; |
3941 | } |
3942 | |
3943 | void setHasLoadedFieldsFromExternalStorage(bool val) const { |
3944 | RecordDeclBits.LoadedFieldsFromExternalStorage = val; |
3945 | } |
3946 | |
3947 | |
3948 | bool isNonTrivialToPrimitiveDefaultInitialize() const { |
3949 | return RecordDeclBits.NonTrivialToPrimitiveDefaultInitialize; |
3950 | } |
3951 | |
3952 | void setNonTrivialToPrimitiveDefaultInitialize(bool V) { |
3953 | RecordDeclBits.NonTrivialToPrimitiveDefaultInitialize = V; |
3954 | } |
3955 | |
3956 | bool isNonTrivialToPrimitiveCopy() const { |
3957 | return RecordDeclBits.NonTrivialToPrimitiveCopy; |
3958 | } |
3959 | |
3960 | void setNonTrivialToPrimitiveCopy(bool V) { |
3961 | RecordDeclBits.NonTrivialToPrimitiveCopy = V; |
3962 | } |
3963 | |
3964 | bool isNonTrivialToPrimitiveDestroy() const { |
3965 | return RecordDeclBits.NonTrivialToPrimitiveDestroy; |
3966 | } |
3967 | |
3968 | void setNonTrivialToPrimitiveDestroy(bool V) { |
3969 | RecordDeclBits.NonTrivialToPrimitiveDestroy = V; |
3970 | } |
3971 | |
3972 | bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { |
3973 | return RecordDeclBits.HasNonTrivialToPrimitiveDefaultInitializeCUnion; |
3974 | } |
3975 | |
3976 | void setHasNonTrivialToPrimitiveDefaultInitializeCUnion(bool V) { |
3977 | RecordDeclBits.HasNonTrivialToPrimitiveDefaultInitializeCUnion = V; |
3978 | } |
3979 | |
3980 | bool hasNonTrivialToPrimitiveDestructCUnion() const { |
3981 | return RecordDeclBits.HasNonTrivialToPrimitiveDestructCUnion; |
3982 | } |
3983 | |
3984 | void setHasNonTrivialToPrimitiveDestructCUnion(bool V) { |
3985 | RecordDeclBits.HasNonTrivialToPrimitiveDestructCUnion = V; |
3986 | } |
3987 | |
3988 | bool hasNonTrivialToPrimitiveCopyCUnion() const { |
3989 | return RecordDeclBits.HasNonTrivialToPrimitiveCopyCUnion; |
3990 | } |
3991 | |
3992 | void setHasNonTrivialToPrimitiveCopyCUnion(bool V) { |
3993 | RecordDeclBits.HasNonTrivialToPrimitiveCopyCUnion = V; |
3994 | } |
3995 | |
3996 | |
3997 | |
3998 | |
3999 | bool canPassInRegisters() const { |
4000 | return getArgPassingRestrictions() == APK_CanPassInRegs; |
4001 | } |
4002 | |
4003 | ArgPassingKind getArgPassingRestrictions() const { |
4004 | return static_cast<ArgPassingKind>(RecordDeclBits.ArgPassingRestrictions); |
4005 | } |
4006 | |
4007 | void setArgPassingRestrictions(ArgPassingKind Kind) { |
4008 | RecordDeclBits.ArgPassingRestrictions = Kind; |
4009 | } |
4010 | |
4011 | bool isParamDestroyedInCallee() const { |
4012 | return RecordDeclBits.ParamDestroyedInCallee; |
4013 | } |
4014 | |
4015 | void setParamDestroyedInCallee(bool V) { |
4016 | RecordDeclBits.ParamDestroyedInCallee = V; |
4017 | } |
4018 | |
4019 | |
4020 | |
4021 | |
4022 | |
4023 | |
4024 | |
4025 | |
4026 | |
4027 | |
4028 | |
4029 | |
4030 | |
4031 | |
4032 | bool isInjectedClassName() const; |
4033 | |
4034 | |
4035 | |
4036 | bool isLambda() const; |
4037 | |
4038 | |
4039 | |
4040 | bool isCapturedRecord() const; |
4041 | |
4042 | |
4043 | |
4044 | void setCapturedRecord(); |
4045 | |
4046 | |
4047 | |
4048 | |
4049 | |
4050 | |
4051 | |
4052 | |
4053 | |
4054 | RecordDecl *getDefinition() const { |
4055 | return cast_or_null<RecordDecl>(TagDecl::getDefinition()); |
4056 | } |
4057 | |
4058 | |
4059 | |
4060 | |
4061 | bool isOrContainsUnion() const; |
4062 | |
4063 | |
4064 | |
4065 | |
4066 | using field_iterator = specific_decl_iterator<FieldDecl>; |
4067 | using field_range = llvm::iterator_range<specific_decl_iterator<FieldDecl>>; |
4068 | |
4069 | field_range fields() const { return field_range(field_begin(), field_end()); } |
4070 | field_iterator field_begin() const; |
4071 | |
4072 | field_iterator field_end() const { |
4073 | return field_iterator(decl_iterator()); |
4074 | } |
4075 | |
4076 | |
4077 | bool field_empty() const { |
4078 | return field_begin() == field_end(); |
4079 | } |
4080 | |
4081 | |
4082 | virtual void completeDefinition(); |
4083 | |
4084 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4085 | static bool classofKind(Kind K) { |
4086 | return K >= firstRecord && K <= lastRecord; |
4087 | } |
4088 | |
4089 | |
4090 | |
4091 | |
4092 | bool isMsStruct(const ASTContext &C) const; |
4093 | |
4094 | |
4095 | |
4096 | |
4097 | bool mayInsertExtraPadding(bool EmitRemark = false) const; |
4098 | |
4099 | |
4100 | |
4101 | const FieldDecl *findFirstNamedDataMember() const; |
4102 | |
4103 | private: |
4104 | |
4105 | void LoadFieldsFromExternalStorage() const; |
4106 | }; |
4107 | |
4108 | class FileScopeAsmDecl : public Decl { |
4109 | StringLiteral *AsmString; |
4110 | SourceLocation RParenLoc; |
4111 | |
4112 | FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring, |
4113 | SourceLocation StartL, SourceLocation EndL) |
4114 | : Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {} |
4115 | |
4116 | virtual void anchor(); |
4117 | |
4118 | public: |
4119 | static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC, |
4120 | StringLiteral *Str, SourceLocation AsmLoc, |
4121 | SourceLocation RParenLoc); |
4122 | |
4123 | static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4124 | |
4125 | SourceLocation getAsmLoc() const { return getLocation(); } |
4126 | SourceLocation getRParenLoc() const { return RParenLoc; } |
4127 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
4128 | SourceRange getSourceRange() const override LLVM_READONLY { |
4129 | return SourceRange(getAsmLoc(), getRParenLoc()); |
4130 | } |
4131 | |
4132 | const StringLiteral *getAsmString() const { return AsmString; } |
4133 | StringLiteral *getAsmString() { return AsmString; } |
4134 | void setAsmString(StringLiteral *Asm) { AsmString = Asm; } |
4135 | |
4136 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4137 | static bool classofKind(Kind K) { return K == FileScopeAsm; } |
4138 | }; |
4139 | |
4140 | |
4141 | |
4142 | |
4143 | class BlockDecl : public Decl, public DeclContext { |
4144 | |
4145 | |
4146 | public: |
4147 | |
4148 | |
4149 | class Capture { |
4150 | enum { |
4151 | flag_isByRef = 0x1, |
4152 | flag_isNested = 0x2 |
4153 | }; |
4154 | |
4155 | |
4156 | llvm::PointerIntPair<VarDecl*, 2> VariableAndFlags; |
4157 | |
4158 | |
4159 | |
4160 | |
4161 | Expr *CopyExpr; |
4162 | |
4163 | public: |
4164 | Capture(VarDecl *variable, bool byRef, bool nested, Expr *copy) |
4165 | : VariableAndFlags(variable, |
4166 | (byRef ? flag_isByRef : 0) | (nested ? flag_isNested : 0)), |
4167 | CopyExpr(copy) {} |
4168 | |
4169 | |
4170 | VarDecl *getVariable() const { return VariableAndFlags.getPointer(); } |
4171 | |
4172 | |
4173 | |
4174 | bool isByRef() const { return VariableAndFlags.getInt() & flag_isByRef; } |
4175 | |
4176 | bool isEscapingByref() const { |
4177 | return getVariable()->isEscapingByref(); |
4178 | } |
4179 | |
4180 | bool isNonEscapingByref() const { |
4181 | return getVariable()->isNonEscapingByref(); |
4182 | } |
4183 | |
4184 | |
4185 | |
4186 | bool isNested() const { return VariableAndFlags.getInt() & flag_isNested; } |
4187 | |
4188 | bool hasCopyExpr() const { return CopyExpr != nullptr; } |
4189 | Expr *getCopyExpr() const { return CopyExpr; } |
4190 | void setCopyExpr(Expr *e) { CopyExpr = e; } |
4191 | }; |
4192 | |
4193 | private: |
4194 | |
4195 | |
4196 | |
4197 | ParmVarDecl **ParamInfo = nullptr; |
4198 | unsigned NumParams = 0; |
4199 | |
4200 | Stmt *Body = nullptr; |
4201 | TypeSourceInfo *SignatureAsWritten = nullptr; |
4202 | |
4203 | const Capture *Captures = nullptr; |
4204 | unsigned NumCaptures = 0; |
4205 | |
4206 | unsigned ManglingNumber = 0; |
4207 | Decl *ManglingContextDecl = nullptr; |
4208 | |
4209 | protected: |
4210 | BlockDecl(DeclContext *DC, SourceLocation CaretLoc); |
4211 | |
4212 | public: |
4213 | static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L); |
4214 | static BlockDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4215 | |
4216 | SourceLocation getCaretLocation() const { return getLocation(); } |
4217 | |
4218 | bool isVariadic() const { return BlockDeclBits.IsVariadic; } |
4219 | void setIsVariadic(bool value) { BlockDeclBits.IsVariadic = value; } |
4220 | |
4221 | CompoundStmt *getCompoundBody() const { return (CompoundStmt*) Body; } |
4222 | Stmt *getBody() const override { return (Stmt*) Body; } |
4223 | void setBody(CompoundStmt *B) { Body = (Stmt*) B; } |
4224 | |
4225 | void setSignatureAsWritten(TypeSourceInfo *Sig) { SignatureAsWritten = Sig; } |
4226 | TypeSourceInfo *getSignatureAsWritten() const { return SignatureAsWritten; } |
4227 | |
4228 | |
4229 | ArrayRef<ParmVarDecl *> parameters() const { |
4230 | return {ParamInfo, getNumParams()}; |
4231 | } |
4232 | MutableArrayRef<ParmVarDecl *> parameters() { |
4233 | return {ParamInfo, getNumParams()}; |
4234 | } |
4235 | |
4236 | |
4237 | using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator; |
4238 | using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator; |
4239 | |
4240 | bool param_empty() const { return parameters().empty(); } |
4241 | param_iterator param_begin() { return parameters().begin(); } |
4242 | param_iterator param_end() { return parameters().end(); } |
4243 | param_const_iterator param_begin() const { return parameters().begin(); } |
4244 | param_const_iterator param_end() const { return parameters().end(); } |
4245 | size_t param_size() const { return parameters().size(); } |
4246 | |
4247 | unsigned getNumParams() const { return NumParams; } |
4248 | |
4249 | const ParmVarDecl *getParamDecl(unsigned i) const { |
4250 | assert(i < getNumParams() && "Illegal param #"); |
4251 | return ParamInfo[i]; |
4252 | } |
4253 | ParmVarDecl *getParamDecl(unsigned i) { |
4254 | assert(i < getNumParams() && "Illegal param #"); |
4255 | return ParamInfo[i]; |
4256 | } |
4257 | |
4258 | void setParams(ArrayRef<ParmVarDecl *> NewParamInfo); |
4259 | |
4260 | |
4261 | |
4262 | bool hasCaptures() const { return NumCaptures || capturesCXXThis(); } |
| 8 | | Assuming field 'NumCaptures' is not equal to 0 | |
|
| 9 | | Returning the value 1, which participates in a condition later | |
|
4263 | |
4264 | |
4265 | |
4266 | unsigned getNumCaptures() const { return NumCaptures; } |
4267 | |
4268 | using capture_const_iterator = ArrayRef<Capture>::const_iterator; |
4269 | |
4270 | ArrayRef<Capture> captures() const { return {Captures, NumCaptures}; } |
4271 | |
4272 | capture_const_iterator capture_begin() const { return captures().begin(); } |
4273 | capture_const_iterator capture_end() const { return captures().end(); } |
4274 | |
4275 | bool capturesCXXThis() const { return BlockDeclBits.CapturesCXXThis; } |
4276 | void setCapturesCXXThis(bool B = true) { BlockDeclBits.CapturesCXXThis = B; } |
4277 | |
4278 | bool blockMissingReturnType() const { |
4279 | return BlockDeclBits.BlockMissingReturnType; |
4280 | } |
4281 | |
4282 | void setBlockMissingReturnType(bool val = true) { |
4283 | BlockDeclBits.BlockMissingReturnType = val; |
4284 | } |
4285 | |
4286 | bool isConversionFromLambda() const { |
4287 | return BlockDeclBits.IsConversionFromLambda; |
4288 | } |
4289 | |
4290 | void setIsConversionFromLambda(bool val = true) { |
4291 | BlockDeclBits.IsConversionFromLambda = val; |
4292 | } |
4293 | |
4294 | bool doesNotEscape() const { return BlockDeclBits.DoesNotEscape; } |
4295 | void setDoesNotEscape(bool B = true) { BlockDeclBits.DoesNotEscape = B; } |
4296 | |
4297 | bool canAvoidCopyToHeap() const { |
4298 | return BlockDeclBits.CanAvoidCopyToHeap; |
4299 | } |
4300 | void setCanAvoidCopyToHeap(bool B = true) { |
4301 | BlockDeclBits.CanAvoidCopyToHeap = B; |
4302 | } |
4303 | |
4304 | bool capturesVariable(const VarDecl *var) const; |
4305 | |
4306 | void setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
4307 | bool CapturesCXXThis); |
4308 | |
4309 | unsigned getBlockManglingNumber() const { return ManglingNumber; } |
4310 | |
4311 | Decl *getBlockManglingContextDecl() const { return ManglingContextDecl; } |
4312 | |
4313 | void setBlockMangling(unsigned Number, Decl *Ctx) { |
4314 | ManglingNumber = Number; |
4315 | ManglingContextDecl = Ctx; |
4316 | } |
4317 | |
4318 | SourceRange getSourceRange() const override LLVM_READONLY; |
4319 | |
4320 | |
4321 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4322 | static bool classofKind(Kind K) { return K == Block; } |
4323 | static DeclContext *castToDeclContext(const BlockDecl *D) { |
4324 | return static_cast<DeclContext *>(const_cast<BlockDecl*>(D)); |
4325 | } |
4326 | static BlockDecl *castFromDeclContext(const DeclContext *DC) { |
4327 | return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC)); |
4328 | } |
4329 | }; |
4330 | |
4331 | |
4332 | class CapturedDecl final |
4333 | : public Decl, |
4334 | public DeclContext, |
4335 | private llvm::TrailingObjects<CapturedDecl, ImplicitParamDecl *> { |
4336 | protected: |
4337 | size_t numTrailingObjects(OverloadToken<ImplicitParamDecl>) { |
4338 | return NumParams; |
4339 | } |
4340 | |
4341 | private: |
4342 | |
4343 | unsigned NumParams; |
4344 | |
4345 | |
4346 | unsigned ContextParam; |
4347 | |
4348 | |
4349 | llvm::PointerIntPair<Stmt *, 1, bool> BodyAndNothrow; |
4350 | |
4351 | explicit CapturedDecl(DeclContext *DC, unsigned NumParams); |
4352 | |
4353 | ImplicitParamDecl *const *getParams() const { |
4354 | return getTrailingObjects<ImplicitParamDecl *>(); |
4355 | } |
4356 | |
4357 | ImplicitParamDecl **getParams() { |
4358 | return getTrailingObjects<ImplicitParamDecl *>(); |
4359 | } |
4360 | |
4361 | public: |
4362 | friend class ASTDeclReader; |
4363 | friend class ASTDeclWriter; |
4364 | friend TrailingObjects; |
4365 | |
4366 | static CapturedDecl *Create(ASTContext &C, DeclContext *DC, |
4367 | unsigned NumParams); |
4368 | static CapturedDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
4369 | unsigned NumParams); |
4370 | |
4371 | Stmt *getBody() const override; |
4372 | void setBody(Stmt *B); |
4373 | |
4374 | bool isNothrow() const; |
4375 | void setNothrow(bool Nothrow = true); |
4376 | |
4377 | unsigned getNumParams() const { return NumParams; } |
4378 | |
4379 | ImplicitParamDecl *getParam(unsigned i) const { |
4380 | assert(i < NumParams); |
4381 | return getParams()[i]; |
4382 | } |
4383 | void setParam(unsigned i, ImplicitParamDecl *P) { |
4384 | assert(i < NumParams); |
4385 | getParams()[i] = P; |
4386 | } |
4387 | |
4388 | |
4389 | ArrayRef<ImplicitParamDecl *> parameters() const { |
4390 | return {getParams(), getNumParams()}; |
4391 | } |
4392 | MutableArrayRef<ImplicitParamDecl *> parameters() { |
4393 | return {getParams(), getNumParams()}; |
4394 | } |
4395 | |
4396 | |
4397 | ImplicitParamDecl *getContextParam() const { |
4398 | assert(ContextParam < NumParams); |
4399 | return getParam(ContextParam); |
4400 | } |
4401 | void setContextParam(unsigned i, ImplicitParamDecl *P) { |
4402 | assert(i < NumParams); |
4403 | ContextParam = i; |
4404 | setParam(i, P); |
4405 | } |
4406 | unsigned getContextParamPosition() const { return ContextParam; } |
4407 | |
4408 | using param_iterator = ImplicitParamDecl *const *; |
4409 | using param_range = llvm::iterator_range<param_iterator>; |
4410 | |
4411 | |
4412 | param_iterator param_begin() const { return getParams(); } |
4413 | |
4414 | param_iterator param_end() const { return getParams() + NumParams; } |
4415 | |
4416 | |
4417 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4418 | static bool classofKind(Kind K) { return K == Captured; } |
4419 | static DeclContext *castToDeclContext(const CapturedDecl *D) { |
4420 | return static_cast<DeclContext *>(const_cast<CapturedDecl *>(D)); |
4421 | } |
4422 | static CapturedDecl *castFromDeclContext(const DeclContext *DC) { |
4423 | return static_cast<CapturedDecl *>(const_cast<DeclContext *>(DC)); |
4424 | } |
4425 | }; |
4426 | |
4427 | |
4428 | |
4429 | |
4430 | |
4431 | |
4432 | |
4433 | |
4434 | |
4435 | |
4436 | |
4437 | class ImportDecl final : public Decl, |
4438 | llvm::TrailingObjects<ImportDecl, SourceLocation> { |
4439 | friend class ASTContext; |
4440 | friend class ASTDeclReader; |
4441 | friend class ASTReader; |
4442 | friend TrailingObjects; |
4443 | |
4444 | |
4445 | Module *ImportedModule = nullptr; |
4446 | |
4447 | |
4448 | |
4449 | |
4450 | |
4451 | |
4452 | |
4453 | |
4454 | |
4455 | llvm::PointerIntPair<ImportDecl *, 1, bool> NextLocalImportAndComplete; |
4456 | |
4457 | ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, |
4458 | ArrayRef<SourceLocation> IdentifierLocs); |
4459 | |
4460 | ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, |
4461 | SourceLocation EndLoc); |
4462 | |
4463 | ImportDecl(EmptyShell Empty) : Decl(Import, Empty) {} |
4464 | |
4465 | bool isImportComplete() const { return NextLocalImportAndComplete.getInt(); } |
4466 | |
4467 | void setImportComplete(bool C) { NextLocalImportAndComplete.setInt(C); } |
4468 | |
4469 | |
4470 | |
4471 | ImportDecl *getNextLocalImport() const { |
4472 | return NextLocalImportAndComplete.getPointer(); |
4473 | } |
4474 | |
4475 | void setNextLocalImport(ImportDecl *Import) { |
4476 | NextLocalImportAndComplete.setPointer(Import); |
4477 | } |
4478 | |
4479 | public: |
4480 | |
4481 | static ImportDecl *Create(ASTContext &C, DeclContext *DC, |
4482 | SourceLocation StartLoc, Module *Imported, |
4483 | ArrayRef<SourceLocation> IdentifierLocs); |
4484 | |
4485 | |
4486 | |
4487 | static ImportDecl *CreateImplicit(ASTContext &C, DeclContext *DC, |
4488 | SourceLocation StartLoc, Module *Imported, |
4489 | SourceLocation EndLoc); |
4490 | |
4491 | |
4492 | static ImportDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
4493 | unsigned NumLocations); |
4494 | |
4495 | |
4496 | Module *getImportedModule() const { return ImportedModule; } |
4497 | |
4498 | |
4499 | |
4500 | |
4501 | |
4502 | |
4503 | ArrayRef<SourceLocation> getIdentifierLocs() const; |
4504 | |
4505 | SourceRange getSourceRange() const override LLVM_READONLY; |
4506 | |
4507 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4508 | static bool classofKind(Kind K) { return K == Import; } |
4509 | }; |
4510 | |
4511 | |
4512 | |
4513 | |
4514 | |
4515 | |
4516 | |
4517 | class ExportDecl final : public Decl, public DeclContext { |
4518 | virtual void anchor(); |
4519 | |
4520 | private: |
4521 | friend class ASTDeclReader; |
4522 | |
4523 | |
4524 | SourceLocation RBraceLoc; |
4525 | |
4526 | ExportDecl(DeclContext *DC, SourceLocation ExportLoc) |
4527 | : Decl(Export, DC, ExportLoc), DeclContext(Export), |
4528 | RBraceLoc(SourceLocation()) {} |
4529 | |
4530 | public: |
4531 | static ExportDecl *Create(ASTContext &C, DeclContext *DC, |
4532 | SourceLocation ExportLoc); |
4533 | static ExportDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4534 | |
4535 | SourceLocation getExportLoc() const { return getLocation(); } |
4536 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
4537 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
4538 | |
4539 | bool hasBraces() const { return RBraceLoc.isValid(); } |
4540 | |
4541 | SourceLocation getEndLoc() const LLVM_READONLY { |
4542 | if (hasBraces()) |
4543 | return RBraceLoc; |
4544 | |
4545 | |
4546 | return decls_empty() ? getLocation() : decls_begin()->getEndLoc(); |
4547 | } |
4548 | |
4549 | SourceRange getSourceRange() const override LLVM_READONLY { |
4550 | return SourceRange(getLocation(), getEndLoc()); |
4551 | } |
4552 | |
4553 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4554 | static bool classofKind(Kind K) { return K == Export; } |
4555 | static DeclContext *castToDeclContext(const ExportDecl *D) { |
4556 | return static_cast<DeclContext *>(const_cast<ExportDecl*>(D)); |
4557 | } |
4558 | static ExportDecl *castFromDeclContext(const DeclContext *DC) { |
4559 | return static_cast<ExportDecl *>(const_cast<DeclContext*>(DC)); |
4560 | } |
4561 | }; |
4562 | |
4563 | |
4564 | class EmptyDecl : public Decl { |
4565 | EmptyDecl(DeclContext *DC, SourceLocation L) : Decl(Empty, DC, L) {} |
4566 | |
4567 | virtual void anchor(); |
4568 | |
4569 | public: |
4570 | static EmptyDecl *Create(ASTContext &C, DeclContext *DC, |
4571 | SourceLocation L); |
4572 | static EmptyDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4573 | |
4574 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4575 | static bool classofKind(Kind K) { return K == Empty; } |
4576 | }; |
4577 | |
4578 | |
4579 | |
4580 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
4581 | const NamedDecl *ND) { |
4582 | PD.AddTaggedVal(reinterpret_cast<intptr_t>(ND), |
4583 | DiagnosticsEngine::ak_nameddecl); |
4584 | return PD; |
4585 | } |
4586 | |
4587 | template<typename decl_type> |
4588 | void Redeclarable<decl_type>::setPreviousDecl(decl_type *PrevDecl) { |
4589 | |
4590 | |
4591 | assert(RedeclLink.isFirst() && |
4592 | "setPreviousDecl on a decl already in a redeclaration chain"); |
4593 | |
4594 | if (PrevDecl) { |
4595 | |
4596 | |
4597 | |
4598 | First = PrevDecl->getFirstDecl(); |
4599 | assert(First->RedeclLink.isFirst() && "Expected first"); |
4600 | decl_type *MostRecent = First->getNextRedeclaration(); |
4601 | RedeclLink = PreviousDeclLink(cast<decl_type>(MostRecent)); |
4602 | |
4603 | |
4604 | |
4605 | static_cast<decl_type*>(this)->IdentifierNamespace |= |
4606 | MostRecent->getIdentifierNamespace() & |
4607 | (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type); |
4608 | } else { |
4609 | |
4610 | First = static_cast<decl_type*>(this); |
4611 | } |
4612 | |
4613 | |
4614 | First->RedeclLink.setLatest(static_cast<decl_type*>(this)); |
4615 | |
4616 | assert(!isa<NamedDecl>(static_cast<decl_type*>(this)) || |
4617 | cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid()); |
4618 | } |
4619 | |
4620 | |
4621 | |
4622 | |
4623 | |
4624 | |
4625 | |
4626 | inline bool IsEnumDeclComplete(EnumDecl *ED) { |
4627 | return ED->isComplete(); |
4628 | } |
4629 | |
4630 | |
4631 | |
4632 | |
4633 | |
4634 | inline bool IsEnumDeclScoped(EnumDecl *ED) { |
4635 | return ED->isScoped(); |
4636 | } |
4637 | |
4638 | |
4639 | |
4640 | |
4641 | static constexpr StringRef getOpenMPVariantManglingSeparatorStr() { |
4642 | return "$ompvariant"; |
4643 | } |
4644 | |
4645 | } |
4646 | |
4647 | #endif // LLVM_CLANG_AST_DECL_H |