Bug Summary

File:clang/lib/CodeGen/CodeGenModule.cpp
Warning:line 4300, column 17
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CodeGenModule.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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-12/lib/clang/12.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/build-llvm/tools/clang/lib/CodeGen -I /build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen -I /build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include -I /build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/build-llvm/include -I /build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-12/lib/clang/12.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/build-llvm/tools/clang/lib/CodeGen -fdebug-prefix-map=/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2021-01-22-054259-40355-1 -x c++ /build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp

/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp

1//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This coordinates the per-module state used while generating code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CodeGenModule.h"
14#include "CGBlocks.h"
15#include "CGCUDARuntime.h"
16#include "CGCXXABI.h"
17#include "CGCall.h"
18#include "CGDebugInfo.h"
19#include "CGObjCRuntime.h"
20#include "CGOpenCLRuntime.h"
21#include "CGOpenMPRuntime.h"
22#include "CGOpenMPRuntimeAMDGCN.h"
23#include "CGOpenMPRuntimeNVPTX.h"
24#include "CodeGenFunction.h"
25#include "CodeGenPGO.h"
26#include "ConstantEmitter.h"
27#include "CoverageMappingGen.h"
28#include "TargetInfo.h"
29#include "clang/AST/ASTContext.h"
30#include "clang/AST/CharUnits.h"
31#include "clang/AST/DeclCXX.h"
32#include "clang/AST/DeclObjC.h"
33#include "clang/AST/DeclTemplate.h"
34#include "clang/AST/Mangle.h"
35#include "clang/AST/RecordLayout.h"
36#include "clang/AST/RecursiveASTVisitor.h"
37#include "clang/AST/StmtVisitor.h"
38#include "clang/Basic/Builtins.h"
39#include "clang/Basic/CharInfo.h"
40#include "clang/Basic/CodeGenOptions.h"
41#include "clang/Basic/Diagnostic.h"
42#include "clang/Basic/FileManager.h"
43#include "clang/Basic/Module.h"
44#include "clang/Basic/SourceManager.h"
45#include "clang/Basic/TargetInfo.h"
46#include "clang/Basic/Version.h"
47#include "clang/CodeGen/ConstantInitBuilder.h"
48#include "clang/Frontend/FrontendDiagnostic.h"
49#include "llvm/ADT/StringSwitch.h"
50#include "llvm/ADT/Triple.h"
51#include "llvm/Analysis/TargetLibraryInfo.h"
52#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
53#include "llvm/IR/CallingConv.h"
54#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/Intrinsics.h"
56#include "llvm/IR/LLVMContext.h"
57#include "llvm/IR/Module.h"
58#include "llvm/IR/ProfileSummary.h"
59#include "llvm/ProfileData/InstrProfReader.h"
60#include "llvm/Support/CodeGen.h"
61#include "llvm/Support/CommandLine.h"
62#include "llvm/Support/ConvertUTF.h"
63#include "llvm/Support/ErrorHandling.h"
64#include "llvm/Support/MD5.h"
65#include "llvm/Support/TimeProfiler.h"
66
67using namespace clang;
68using namespace CodeGen;
69
70static llvm::cl::opt<bool> LimitedCoverage(
71 "limited-coverage-experimental", llvm::cl::ZeroOrMore, llvm::cl::Hidden,
72 llvm::cl::desc("Emit limited coverage mapping information (experimental)"),
73 llvm::cl::init(false));
74
75static const char AnnotationSection[] = "llvm.metadata";
76
77static CGCXXABI *createCXXABI(CodeGenModule &CGM) {
78 switch (CGM.getTarget().getCXXABI().getKind()) {
79 case TargetCXXABI::AppleARM64:
80 case TargetCXXABI::Fuchsia:
81 case TargetCXXABI::GenericAArch64:
82 case TargetCXXABI::GenericARM:
83 case TargetCXXABI::iOS:
84 case TargetCXXABI::WatchOS:
85 case TargetCXXABI::GenericMIPS:
86 case TargetCXXABI::GenericItanium:
87 case TargetCXXABI::WebAssembly:
88 case TargetCXXABI::XL:
89 return CreateItaniumCXXABI(CGM);
90 case TargetCXXABI::Microsoft:
91 return CreateMicrosoftCXXABI(CGM);
92 }
93
94 llvm_unreachable("invalid C++ ABI kind")::llvm::llvm_unreachable_internal("invalid C++ ABI kind", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 94)
;
95}
96
97CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
98 const PreprocessorOptions &PPO,
99 const CodeGenOptions &CGO, llvm::Module &M,
100 DiagnosticsEngine &diags,
101 CoverageSourceInfo *CoverageInfo)
102 : Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO),
103 PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
104 Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
105 VMContext(M.getContext()), Types(*this), VTables(*this),
106 SanitizerMD(new SanitizerMetadata(*this)) {
107
108 // Initialize the type cache.
109 llvm::LLVMContext &LLVMContext = M.getContext();
110 VoidTy = llvm::Type::getVoidTy(LLVMContext);
111 Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
112 Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
113 Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
114 Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
115 HalfTy = llvm::Type::getHalfTy(LLVMContext);
116 BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
117 FloatTy = llvm::Type::getFloatTy(LLVMContext);
118 DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
119 PointerWidthInBits = C.getTargetInfo().getPointerWidth(0);
120 PointerAlignInBytes =
121 C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity();
122 SizeSizeInBytes =
123 C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
124 IntAlignInBytes =
125 C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
126 IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
127 IntPtrTy = llvm::IntegerType::get(LLVMContext,
128 C.getTargetInfo().getMaxPointerWidth());
129 Int8PtrTy = Int8Ty->getPointerTo(0);
130 Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
131 AllocaInt8PtrTy = Int8Ty->getPointerTo(
132 M.getDataLayout().getAllocaAddrSpace());
133 ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace();
134
135 RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
136
137 if (LangOpts.ObjC)
138 createObjCRuntime();
139 if (LangOpts.OpenCL)
140 createOpenCLRuntime();
141 if (LangOpts.OpenMP)
142 createOpenMPRuntime();
143 if (LangOpts.CUDA)
144 createCUDARuntime();
145
146 // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
147 if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
148 (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
149 TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
150 getCXXABI().getMangleContext()));
151
152 // If debug info or coverage generation is enabled, create the CGDebugInfo
153 // object.
154 if (CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo ||
155 CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)
156 DebugInfo.reset(new CGDebugInfo(*this));
157
158 Block.GlobalUniqueCount = 0;
159
160 if (C.getLangOpts().ObjC)
161 ObjCData.reset(new ObjCEntrypoints());
162
163 if (CodeGenOpts.hasProfileClangUse()) {
164 auto ReaderOrErr = llvm::IndexedInstrProfReader::create(
165 CodeGenOpts.ProfileInstrumentUsePath, CodeGenOpts.ProfileRemappingFile);
166 if (auto E = ReaderOrErr.takeError()) {
167 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
168 "Could not read profile %0: %1");
169 llvm::handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) {
170 getDiags().Report(DiagID) << CodeGenOpts.ProfileInstrumentUsePath
171 << EI.message();
172 });
173 } else
174 PGOReader = std::move(ReaderOrErr.get());
175 }
176
177 // If coverage mapping generation is enabled, create the
178 // CoverageMappingModuleGen object.
179 if (CodeGenOpts.CoverageMapping)
180 CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo));
181}
182
183CodeGenModule::~CodeGenModule() {}
184
185void CodeGenModule::createObjCRuntime() {
186 // This is just isGNUFamily(), but we want to force implementors of
187 // new ABIs to decide how best to do this.
188 switch (LangOpts.ObjCRuntime.getKind()) {
189 case ObjCRuntime::GNUstep:
190 case ObjCRuntime::GCC:
191 case ObjCRuntime::ObjFW:
192 ObjCRuntime.reset(CreateGNUObjCRuntime(*this));
193 return;
194
195 case ObjCRuntime::FragileMacOSX:
196 case ObjCRuntime::MacOSX:
197 case ObjCRuntime::iOS:
198 case ObjCRuntime::WatchOS:
199 ObjCRuntime.reset(CreateMacObjCRuntime(*this));
200 return;
201 }
202 llvm_unreachable("bad runtime kind")::llvm::llvm_unreachable_internal("bad runtime kind", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 202)
;
203}
204
205void CodeGenModule::createOpenCLRuntime() {
206 OpenCLRuntime.reset(new CGOpenCLRuntime(*this));
207}
208
209void CodeGenModule::createOpenMPRuntime() {
210 // Select a specialized code generation class based on the target, if any.
211 // If it does not exist use the default implementation.
212 switch (getTriple().getArch()) {
213 case llvm::Triple::nvptx:
214 case llvm::Triple::nvptx64:
215 assert(getLangOpts().OpenMPIsDevice &&((getLangOpts().OpenMPIsDevice && "OpenMP NVPTX is only prepared to deal with device code."
) ? static_cast<void> (0) : __assert_fail ("getLangOpts().OpenMPIsDevice && \"OpenMP NVPTX is only prepared to deal with device code.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 216, __PRETTY_FUNCTION__))
216 "OpenMP NVPTX is only prepared to deal with device code.")((getLangOpts().OpenMPIsDevice && "OpenMP NVPTX is only prepared to deal with device code."
) ? static_cast<void> (0) : __assert_fail ("getLangOpts().OpenMPIsDevice && \"OpenMP NVPTX is only prepared to deal with device code.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 216, __PRETTY_FUNCTION__))
;
217 OpenMPRuntime.reset(new CGOpenMPRuntimeNVPTX(*this));
218 break;
219 case llvm::Triple::amdgcn:
220 assert(getLangOpts().OpenMPIsDevice &&((getLangOpts().OpenMPIsDevice && "OpenMP AMDGCN is only prepared to deal with device code."
) ? static_cast<void> (0) : __assert_fail ("getLangOpts().OpenMPIsDevice && \"OpenMP AMDGCN is only prepared to deal with device code.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 221, __PRETTY_FUNCTION__))
221 "OpenMP AMDGCN is only prepared to deal with device code.")((getLangOpts().OpenMPIsDevice && "OpenMP AMDGCN is only prepared to deal with device code."
) ? static_cast<void> (0) : __assert_fail ("getLangOpts().OpenMPIsDevice && \"OpenMP AMDGCN is only prepared to deal with device code.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 221, __PRETTY_FUNCTION__))
;
222 OpenMPRuntime.reset(new CGOpenMPRuntimeAMDGCN(*this));
223 break;
224 default:
225 if (LangOpts.OpenMPSimd)
226 OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this));
227 else
228 OpenMPRuntime.reset(new CGOpenMPRuntime(*this));
229 break;
230 }
231}
232
233void CodeGenModule::createCUDARuntime() {
234 CUDARuntime.reset(CreateNVCUDARuntime(*this));
235}
236
237void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
238 Replacements[Name] = C;
239}
240
241void CodeGenModule::applyReplacements() {
242 for (auto &I : Replacements) {
243 StringRef MangledName = I.first();
244 llvm::Constant *Replacement = I.second;
245 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
246 if (!Entry)
247 continue;
248 auto *OldF = cast<llvm::Function>(Entry);
249 auto *NewF = dyn_cast<llvm::Function>(Replacement);
250 if (!NewF) {
251 if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
252 NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
253 } else {
254 auto *CE = cast<llvm::ConstantExpr>(Replacement);
255 assert(CE->getOpcode() == llvm::Instruction::BitCast ||((CE->getOpcode() == llvm::Instruction::BitCast || CE->
getOpcode() == llvm::Instruction::GetElementPtr) ? static_cast
<void> (0) : __assert_fail ("CE->getOpcode() == llvm::Instruction::BitCast || CE->getOpcode() == llvm::Instruction::GetElementPtr"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 256, __PRETTY_FUNCTION__))
256 CE->getOpcode() == llvm::Instruction::GetElementPtr)((CE->getOpcode() == llvm::Instruction::BitCast || CE->
getOpcode() == llvm::Instruction::GetElementPtr) ? static_cast
<void> (0) : __assert_fail ("CE->getOpcode() == llvm::Instruction::BitCast || CE->getOpcode() == llvm::Instruction::GetElementPtr"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 256, __PRETTY_FUNCTION__))
;
257 NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
258 }
259 }
260
261 // Replace old with new, but keep the old order.
262 OldF->replaceAllUsesWith(Replacement);
263 if (NewF) {
264 NewF->removeFromParent();
265 OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(),
266 NewF);
267 }
268 OldF->eraseFromParent();
269 }
270}
271
272void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) {
273 GlobalValReplacements.push_back(std::make_pair(GV, C));
274}
275
276void CodeGenModule::applyGlobalValReplacements() {
277 for (auto &I : GlobalValReplacements) {
278 llvm::GlobalValue *GV = I.first;
279 llvm::Constant *C = I.second;
280
281 GV->replaceAllUsesWith(C);
282 GV->eraseFromParent();
283 }
284}
285
286// This is only used in aliases that we created and we know they have a
287// linear structure.
288static const llvm::GlobalObject *getAliasedGlobal(
289 const llvm::GlobalIndirectSymbol &GIS) {
290 llvm::SmallPtrSet<const llvm::GlobalIndirectSymbol*, 4> Visited;
291 const llvm::Constant *C = &GIS;
292 for (;;) {
293 C = C->stripPointerCasts();
294 if (auto *GO = dyn_cast<llvm::GlobalObject>(C))
295 return GO;
296 // stripPointerCasts will not walk over weak aliases.
297 auto *GIS2 = dyn_cast<llvm::GlobalIndirectSymbol>(C);
298 if (!GIS2)
299 return nullptr;
300 if (!Visited.insert(GIS2).second)
301 return nullptr;
302 C = GIS2->getIndirectSymbol();
303 }
304}
305
306void CodeGenModule::checkAliases() {
307 // Check if the constructed aliases are well formed. It is really unfortunate
308 // that we have to do this in CodeGen, but we only construct mangled names
309 // and aliases during codegen.
310 bool Error = false;
311 DiagnosticsEngine &Diags = getDiags();
312 for (const GlobalDecl &GD : Aliases) {
313 const auto *D = cast<ValueDecl>(GD.getDecl());
314 SourceLocation Location;
315 bool IsIFunc = D->hasAttr<IFuncAttr>();
316 if (const Attr *A = D->getDefiningAttr())
317 Location = A->getLocation();
318 else
319 llvm_unreachable("Not an alias or ifunc?")::llvm::llvm_unreachable_internal("Not an alias or ifunc?", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 319)
;
320 StringRef MangledName = getMangledName(GD);
321 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
322 auto *Alias = cast<llvm::GlobalIndirectSymbol>(Entry);
323 const llvm::GlobalValue *GV = getAliasedGlobal(*Alias);
324 if (!GV) {
325 Error = true;
326 Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc;
327 } else if (GV->isDeclaration()) {
328 Error = true;
329 Diags.Report(Location, diag::err_alias_to_undefined)
330 << IsIFunc << IsIFunc;
331 } else if (IsIFunc) {
332 // Check resolver function type.
333 llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>(
334 GV->getType()->getPointerElementType());
335 assert(FTy)((FTy) ? static_cast<void> (0) : __assert_fail ("FTy", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 335, __PRETTY_FUNCTION__))
;
336 if (!FTy->getReturnType()->isPointerTy())
337 Diags.Report(Location, diag::err_ifunc_resolver_return);
338 }
339
340 llvm::Constant *Aliasee = Alias->getIndirectSymbol();
341 llvm::GlobalValue *AliaseeGV;
342 if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
343 AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
344 else
345 AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
346
347 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
348 StringRef AliasSection = SA->getName();
349 if (AliasSection != AliaseeGV->getSection())
350 Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
351 << AliasSection << IsIFunc << IsIFunc;
352 }
353
354 // We have to handle alias to weak aliases in here. LLVM itself disallows
355 // this since the object semantics would not match the IL one. For
356 // compatibility with gcc we implement it by just pointing the alias
357 // to its aliasee's aliasee. We also warn, since the user is probably
358 // expecting the link to be weak.
359 if (auto GA = dyn_cast<llvm::GlobalIndirectSymbol>(AliaseeGV)) {
360 if (GA->isInterposable()) {
361 Diags.Report(Location, diag::warn_alias_to_weak_alias)
362 << GV->getName() << GA->getName() << IsIFunc;
363 Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
364 GA->getIndirectSymbol(), Alias->getType());
365 Alias->setIndirectSymbol(Aliasee);
366 }
367 }
368 }
369 if (!Error)
370 return;
371
372 for (const GlobalDecl &GD : Aliases) {
373 StringRef MangledName = getMangledName(GD);
374 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
375 auto *Alias = cast<llvm::GlobalIndirectSymbol>(Entry);
376 Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
377 Alias->eraseFromParent();
378 }
379}
380
381void CodeGenModule::clear() {
382 DeferredDeclsToEmit.clear();
383 if (OpenMPRuntime)
384 OpenMPRuntime->clear();
385}
386
387void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags,
388 StringRef MainFile) {
389 if (!hasDiagnostics())
390 return;
391 if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
392 if (MainFile.empty())
393 MainFile = "<stdin>";
394 Diags.Report(diag::warn_profile_data_unprofiled) << MainFile;
395 } else {
396 if (Mismatched > 0)
397 Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched;
398
399 if (Missing > 0)
400 Diags.Report(diag::warn_profile_data_missing) << Visited << Missing;
401 }
402}
403
404static void setVisibilityFromDLLStorageClass(const clang::LangOptions &LO,
405 llvm::Module &M) {
406 if (!LO.VisibilityFromDLLStorageClass)
407 return;
408
409 llvm::GlobalValue::VisibilityTypes DLLExportVisibility =
410 CodeGenModule::GetLLVMVisibility(LO.getDLLExportVisibility());
411 llvm::GlobalValue::VisibilityTypes NoDLLStorageClassVisibility =
412 CodeGenModule::GetLLVMVisibility(LO.getNoDLLStorageClassVisibility());
413 llvm::GlobalValue::VisibilityTypes ExternDeclDLLImportVisibility =
414 CodeGenModule::GetLLVMVisibility(LO.getExternDeclDLLImportVisibility());
415 llvm::GlobalValue::VisibilityTypes ExternDeclNoDLLStorageClassVisibility =
416 CodeGenModule::GetLLVMVisibility(
417 LO.getExternDeclNoDLLStorageClassVisibility());
418
419 for (llvm::GlobalValue &GV : M.global_values()) {
420 if (GV.hasAppendingLinkage() || GV.hasLocalLinkage())
421 continue;
422
423 // Reset DSO locality before setting the visibility. This removes
424 // any effects that visibility options and annotations may have
425 // had on the DSO locality. Setting the visibility will implicitly set
426 // appropriate globals to DSO Local; however, this will be pessimistic
427 // w.r.t. to the normal compiler IRGen.
428 GV.setDSOLocal(false);
429
430 if (GV.isDeclarationForLinker()) {
431 GV.setVisibility(GV.getDLLStorageClass() ==
432 llvm::GlobalValue::DLLImportStorageClass
433 ? ExternDeclDLLImportVisibility
434 : ExternDeclNoDLLStorageClassVisibility);
435 } else {
436 GV.setVisibility(GV.getDLLStorageClass() ==
437 llvm::GlobalValue::DLLExportStorageClass
438 ? DLLExportVisibility
439 : NoDLLStorageClassVisibility);
440 }
441
442 GV.setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
443 }
444}
445
446void CodeGenModule::Release() {
447 EmitDeferred();
448 EmitVTablesOpportunistically();
449 applyGlobalValReplacements();
450 applyReplacements();
451 checkAliases();
452 emitMultiVersionFunctions();
453 EmitCXXGlobalInitFunc();
454 EmitCXXGlobalCleanUpFunc();
455 registerGlobalDtorsWithAtExit();
456 EmitCXXThreadLocalInitFunc();
457 if (ObjCRuntime)
458 if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
459 AddGlobalCtor(ObjCInitFunction);
460 if (Context.getLangOpts().CUDA && !Context.getLangOpts().CUDAIsDevice &&
461 CUDARuntime) {
462 if (llvm::Function *CudaCtorFunction =
463 CUDARuntime->makeModuleCtorFunction())
464 AddGlobalCtor(CudaCtorFunction);
465 }
466 if (OpenMPRuntime) {
467 if (llvm::Function *OpenMPRequiresDirectiveRegFun =
468 OpenMPRuntime->emitRequiresDirectiveRegFun()) {
469 AddGlobalCtor(OpenMPRequiresDirectiveRegFun, 0);
470 }
471 OpenMPRuntime->createOffloadEntriesAndInfoMetadata();
472 OpenMPRuntime->clear();
473 }
474 if (PGOReader) {
475 getModule().setProfileSummary(
476 PGOReader->getSummary(/* UseCS */ false).getMD(VMContext),
477 llvm::ProfileSummary::PSK_Instr);
478 if (PGOStats.hasDiagnostics())
479 PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName);
480 }
481 EmitCtorList(GlobalCtors, "llvm.global_ctors");
482 EmitCtorList(GlobalDtors, "llvm.global_dtors");
483 EmitGlobalAnnotations();
484 EmitStaticExternCAliases();
485 EmitDeferredUnusedCoverageMappings();
486 if (CoverageMapping)
487 CoverageMapping->emit();
488 if (CodeGenOpts.SanitizeCfiCrossDso) {
489 CodeGenFunction(*this).EmitCfiCheckFail();
490 CodeGenFunction(*this).EmitCfiCheckStub();
491 }
492 emitAtAvailableLinkGuard();
493 if (Context.getTargetInfo().getTriple().isWasm() &&
494 !Context.getTargetInfo().getTriple().isOSEmscripten()) {
495 EmitMainVoidAlias();
496 }
497 emitLLVMUsed();
498 if (SanStats)
499 SanStats->finish();
500
501 if (CodeGenOpts.Autolink &&
502 (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
503 EmitModuleLinkOptions();
504 }
505
506 // On ELF we pass the dependent library specifiers directly to the linker
507 // without manipulating them. This is in contrast to other platforms where
508 // they are mapped to a specific linker option by the compiler. This
509 // difference is a result of the greater variety of ELF linkers and the fact
510 // that ELF linkers tend to handle libraries in a more complicated fashion
511 // than on other platforms. This forces us to defer handling the dependent
512 // libs to the linker.
513 //
514 // CUDA/HIP device and host libraries are different. Currently there is no
515 // way to differentiate dependent libraries for host or device. Existing
516 // usage of #pragma comment(lib, *) is intended for host libraries on
517 // Windows. Therefore emit llvm.dependent-libraries only for host.
518 if (!ELFDependentLibraries.empty() && !Context.getLangOpts().CUDAIsDevice) {
519 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries");
520 for (auto *MD : ELFDependentLibraries)
521 NMD->addOperand(MD);
522 }
523
524 // Record mregparm value now so it is visible through rest of codegen.
525 if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
526 getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
527 CodeGenOpts.NumRegisterParameters);
528
529 if (CodeGenOpts.DwarfVersion) {
530 getModule().addModuleFlag(llvm::Module::Max, "Dwarf Version",
531 CodeGenOpts.DwarfVersion);
532 }
533
534 if (Context.getLangOpts().SemanticInterposition)
535 // Require various optimization to respect semantic interposition.
536 getModule().setSemanticInterposition(1);
537
538 if (CodeGenOpts.EmitCodeView) {
539 // Indicate that we want CodeView in the metadata.
540 getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1);
541 }
542 if (CodeGenOpts.CodeViewGHash) {
543 getModule().addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1);
544 }
545 if (CodeGenOpts.ControlFlowGuard) {
546 // Function ID tables and checks for Control Flow Guard (cfguard=2).
547 getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 2);
548 } else if (CodeGenOpts.ControlFlowGuardNoChecks) {
549 // Function ID tables for Control Flow Guard (cfguard=1).
550 getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 1);
551 }
552 if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) {
553 // We don't support LTO with 2 with different StrictVTablePointers
554 // FIXME: we could support it by stripping all the information introduced
555 // by StrictVTablePointers.
556
557 getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1);
558
559 llvm::Metadata *Ops[2] = {
560 llvm::MDString::get(VMContext, "StrictVTablePointers"),
561 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
562 llvm::Type::getInt32Ty(VMContext), 1))};
563
564 getModule().addModuleFlag(llvm::Module::Require,
565 "StrictVTablePointersRequirement",
566 llvm::MDNode::get(VMContext, Ops));
567 }
568 if (getModuleDebugInfo())
569 // We support a single version in the linked module. The LLVM
570 // parser will drop debug info with a different version number
571 // (and warn about it, too).
572 getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version",
573 llvm::DEBUG_METADATA_VERSION);
574
575 // We need to record the widths of enums and wchar_t, so that we can generate
576 // the correct build attributes in the ARM backend. wchar_size is also used by
577 // TargetLibraryInfo.
578 uint64_t WCharWidth =
579 Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity();
580 getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth);
581
582 llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
583 if ( Arch == llvm::Triple::arm
584 || Arch == llvm::Triple::armeb
585 || Arch == llvm::Triple::thumb
586 || Arch == llvm::Triple::thumbeb) {
587 // The minimum width of an enum in bytes
588 uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
589 getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
590 }
591
592 if (Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64) {
593 StringRef ABIStr = Target.getABI();
594 llvm::LLVMContext &Ctx = TheModule.getContext();
595 getModule().addModuleFlag(llvm::Module::Error, "target-abi",
596 llvm::MDString::get(Ctx, ABIStr));
597 }
598
599 if (CodeGenOpts.SanitizeCfiCrossDso) {
600 // Indicate that we want cross-DSO control flow integrity checks.
601 getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
602 }
603
604 if (CodeGenOpts.WholeProgramVTables) {
605 // Indicate whether VFE was enabled for this module, so that the
606 // vcall_visibility metadata added under whole program vtables is handled
607 // appropriately in the optimizer.
608 getModule().addModuleFlag(llvm::Module::Error, "Virtual Function Elim",
609 CodeGenOpts.VirtualFunctionElimination);
610 }
611
612 if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) {
613 getModule().addModuleFlag(llvm::Module::Override,
614 "CFI Canonical Jump Tables",
615 CodeGenOpts.SanitizeCfiCanonicalJumpTables);
616 }
617
618 if (CodeGenOpts.CFProtectionReturn &&
619 Target.checkCFProtectionReturnSupported(getDiags())) {
620 // Indicate that we want to instrument return control flow protection.
621 getModule().addModuleFlag(llvm::Module::Override, "cf-protection-return",
622 1);
623 }
624
625 if (CodeGenOpts.CFProtectionBranch &&
626 Target.checkCFProtectionBranchSupported(getDiags())) {
627 // Indicate that we want to instrument branch control flow protection.
628 getModule().addModuleFlag(llvm::Module::Override, "cf-protection-branch",
629 1);
630 }
631
632 if (Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::aarch64_32 ||
633 Arch == llvm::Triple::aarch64_be) {
634 getModule().addModuleFlag(llvm::Module::Error,
635 "branch-target-enforcement",
636 LangOpts.BranchTargetEnforcement);
637
638 getModule().addModuleFlag(llvm::Module::Error, "sign-return-address",
639 LangOpts.hasSignReturnAddress());
640
641 getModule().addModuleFlag(llvm::Module::Error, "sign-return-address-all",
642 LangOpts.isSignReturnAddressScopeAll());
643
644 getModule().addModuleFlag(llvm::Module::Error,
645 "sign-return-address-with-bkey",
646 !LangOpts.isSignReturnAddressWithAKey());
647 }
648
649 if (!CodeGenOpts.MemoryProfileOutput.empty()) {
650 llvm::LLVMContext &Ctx = TheModule.getContext();
651 getModule().addModuleFlag(
652 llvm::Module::Error, "MemProfProfileFilename",
653 llvm::MDString::get(Ctx, CodeGenOpts.MemoryProfileOutput));
654 }
655
656 if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) {
657 // Indicate whether __nvvm_reflect should be configured to flush denormal
658 // floating point values to 0. (This corresponds to its "__CUDA_FTZ"
659 // property.)
660 getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
661 CodeGenOpts.FP32DenormalMode.Output !=
662 llvm::DenormalMode::IEEE);
663 }
664
665 // Emit OpenCL specific module metadata: OpenCL/SPIR version.
666 if (LangOpts.OpenCL) {
667 EmitOpenCLMetadata();
668 // Emit SPIR version.
669 if (getTriple().isSPIR()) {
670 // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
671 // opencl.spir.version named metadata.
672 // C++ is backwards compatible with OpenCL v2.0.
673 auto Version = LangOpts.OpenCLCPlusPlus ? 200 : LangOpts.OpenCLVersion;
674 llvm::Metadata *SPIRVerElts[] = {
675 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
676 Int32Ty, Version / 100)),
677 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
678 Int32Ty, (Version / 100 > 1) ? 0 : 2))};
679 llvm::NamedMDNode *SPIRVerMD =
680 TheModule.getOrInsertNamedMetadata("opencl.spir.version");
681 llvm::LLVMContext &Ctx = TheModule.getContext();
682 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
683 }
684 }
685
686 if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
687 assert(PLevel < 3 && "Invalid PIC Level")((PLevel < 3 && "Invalid PIC Level") ? static_cast
<void> (0) : __assert_fail ("PLevel < 3 && \"Invalid PIC Level\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 687, __PRETTY_FUNCTION__))
;
688 getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));
689 if (Context.getLangOpts().PIE)
690 getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel));
691 }
692
693 if (getCodeGenOpts().CodeModel.size() > 0) {
694 unsigned CM = llvm::StringSwitch<unsigned>(getCodeGenOpts().CodeModel)
695 .Case("tiny", llvm::CodeModel::Tiny)
696 .Case("small", llvm::CodeModel::Small)
697 .Case("kernel", llvm::CodeModel::Kernel)
698 .Case("medium", llvm::CodeModel::Medium)
699 .Case("large", llvm::CodeModel::Large)
700 .Default(~0u);
701 if (CM != ~0u) {
702 llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM);
703 getModule().setCodeModel(codeModel);
704 }
705 }
706
707 if (CodeGenOpts.NoPLT)
708 getModule().setRtLibUseGOT();
709
710 SimplifyPersonality();
711
712 if (getCodeGenOpts().EmitDeclMetadata)
713 EmitDeclMetadata();
714
715 if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes)
716 EmitCoverageFile();
717
718 if (CGDebugInfo *DI = getModuleDebugInfo())
719 DI->finalize();
720
721 if (getCodeGenOpts().EmitVersionIdentMetadata)
722 EmitVersionIdentMetadata();
723
724 if (!getCodeGenOpts().RecordCommandLine.empty())
725 EmitCommandLineMetadata();
726
727 getTargetCodeGenInfo().emitTargetMetadata(*this, MangledDeclNames);
728
729 EmitBackendOptionsMetadata(getCodeGenOpts());
730
731 // Set visibility from DLL storage class
732 // We do this at the end of LLVM IR generation; after any operation
733 // that might affect the DLL storage class or the visibility, and
734 // before anything that might act on these.
735 setVisibilityFromDLLStorageClass(LangOpts, getModule());
736}
737
738void CodeGenModule::EmitOpenCLMetadata() {
739 // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
740 // opencl.ocl.version named metadata node.
741 // C++ is backwards compatible with OpenCL v2.0.
742 // FIXME: We might need to add CXX version at some point too?
743 auto Version = LangOpts.OpenCLCPlusPlus ? 200 : LangOpts.OpenCLVersion;
744 llvm::Metadata *OCLVerElts[] = {
745 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
746 Int32Ty, Version / 100)),
747 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
748 Int32Ty, (Version % 100) / 10))};
749 llvm::NamedMDNode *OCLVerMD =
750 TheModule.getOrInsertNamedMetadata("opencl.ocl.version");
751 llvm::LLVMContext &Ctx = TheModule.getContext();
752 OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
753}
754
755void CodeGenModule::EmitBackendOptionsMetadata(
756 const CodeGenOptions CodeGenOpts) {
757 switch (getTriple().getArch()) {
758 default:
759 break;
760 case llvm::Triple::riscv32:
761 case llvm::Triple::riscv64:
762 getModule().addModuleFlag(llvm::Module::Error, "SmallDataLimit",
763 CodeGenOpts.SmallDataLimit);
764 break;
765 }
766}
767
768void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
769 // Make sure that this type is translated.
770 Types.UpdateCompletedType(TD);
771}
772
773void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
774 // Make sure that this type is translated.
775 Types.RefreshTypeCacheForClass(RD);
776}
777
778llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) {
779 if (!TBAA)
780 return nullptr;
781 return TBAA->getTypeInfo(QTy);
782}
783
784TBAAAccessInfo CodeGenModule::getTBAAAccessInfo(QualType AccessType) {
785 if (!TBAA)
786 return TBAAAccessInfo();
787 if (getLangOpts().CUDAIsDevice) {
788 // As CUDA builtin surface/texture types are replaced, skip generating TBAA
789 // access info.
790 if (AccessType->isCUDADeviceBuiltinSurfaceType()) {
791 if (getTargetCodeGenInfo().getCUDADeviceBuiltinSurfaceDeviceType() !=
792 nullptr)
793 return TBAAAccessInfo();
794 } else if (AccessType->isCUDADeviceBuiltinTextureType()) {
795 if (getTargetCodeGenInfo().getCUDADeviceBuiltinTextureDeviceType() !=
796 nullptr)
797 return TBAAAccessInfo();
798 }
799 }
800 return TBAA->getAccessInfo(AccessType);
801}
802
803TBAAAccessInfo
804CodeGenModule::getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
805 if (!TBAA)
806 return TBAAAccessInfo();
807 return TBAA->getVTablePtrAccessInfo(VTablePtrType);
808}
809
810llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) {
811 if (!TBAA)
812 return nullptr;
813 return TBAA->getTBAAStructInfo(QTy);
814}
815
816llvm::MDNode *CodeGenModule::getTBAABaseTypeInfo(QualType QTy) {
817 if (!TBAA)
818 return nullptr;
819 return TBAA->getBaseTypeInfo(QTy);
820}
821
822llvm::MDNode *CodeGenModule::getTBAAAccessTagInfo(TBAAAccessInfo Info) {
823 if (!TBAA)
824 return nullptr;
825 return TBAA->getAccessTagInfo(Info);
826}
827
828TBAAAccessInfo CodeGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
829 TBAAAccessInfo TargetInfo) {
830 if (!TBAA)
831 return TBAAAccessInfo();
832 return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo);
833}
834
835TBAAAccessInfo
836CodeGenModule::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
837 TBAAAccessInfo InfoB) {
838 if (!TBAA)
839 return TBAAAccessInfo();
840 return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB);
841}
842
843TBAAAccessInfo
844CodeGenModule::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
845 TBAAAccessInfo SrcInfo) {
846 if (!TBAA)
847 return TBAAAccessInfo();
848 return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo);
849}
850
851void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst,
852 TBAAAccessInfo TBAAInfo) {
853 if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo))
854 Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag);
855}
856
857void CodeGenModule::DecorateInstructionWithInvariantGroup(
858 llvm::Instruction *I, const CXXRecordDecl *RD) {
859 I->setMetadata(llvm::LLVMContext::MD_invariant_group,
860 llvm::MDNode::get(getLLVMContext(), {}));
861}
862
863void CodeGenModule::Error(SourceLocation loc, StringRef message) {
864 unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
865 getDiags().Report(Context.getFullLoc(loc), diagID) << message;
866}
867
868/// ErrorUnsupported - Print out an error that codegen doesn't support the
869/// specified stmt yet.
870void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) {
871 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
872 "cannot compile this %0 yet");
873 std::string Msg = Type;
874 getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID)
875 << Msg << S->getSourceRange();
876}
877
878/// ErrorUnsupported - Print out an error that codegen doesn't support the
879/// specified decl yet.
880void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) {
881 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
882 "cannot compile this %0 yet");
883 std::string Msg = Type;
884 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
885}
886
887llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
888 return llvm::ConstantInt::get(SizeTy, size.getQuantity());
889}
890
891void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
892 const NamedDecl *D) const {
893 if (GV->hasDLLImportStorageClass())
894 return;
895 // Internal definitions always have default visibility.
896 if (GV->hasLocalLinkage()) {
897 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
898 return;
899 }
900 if (!D)
901 return;
902 // Set visibility for definitions, and for declarations if requested globally
903 // or set explicitly.
904 LinkageInfo LV = D->getLinkageAndVisibility();
905 if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls ||
906 !GV->isDeclarationForLinker())
907 GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
908}
909
910static bool shouldAssumeDSOLocal(const CodeGenModule &CGM,
911 llvm::GlobalValue *GV) {
912 if (GV->hasLocalLinkage())
913 return true;
914
915 if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())
916 return true;
917
918 // DLLImport explicitly marks the GV as external.
919 if (GV->hasDLLImportStorageClass())
920 return false;
921
922 const llvm::Triple &TT = CGM.getTriple();
923 if (TT.isWindowsGNUEnvironment()) {
924 // In MinGW, variables without DLLImport can still be automatically
925 // imported from a DLL by the linker; don't mark variables that
926 // potentially could come from another DLL as DSO local.
927 if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) &&
928 !GV->isThreadLocal())
929 return false;
930 }
931
932 // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
933 // remain unresolved in the link, they can be resolved to zero, which is
934 // outside the current DSO.
935 if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage())
936 return false;
937
938 // Every other GV is local on COFF.
939 // Make an exception for windows OS in the triple: Some firmware builds use
940 // *-win32-macho triples. This (accidentally?) produced windows relocations
941 // without GOT tables in older clang versions; Keep this behaviour.
942 // FIXME: even thread local variables?
943 if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
944 return true;
945
946 const auto &CGOpts = CGM.getCodeGenOpts();
947 llvm::Reloc::Model RM = CGOpts.RelocationModel;
948 const auto &LOpts = CGM.getLangOpts();
949
950 if (TT.isOSBinFormatMachO()) {
951 if (RM == llvm::Reloc::Static)
952 return true;
953 return GV->isStrongDefinitionForLinker();
954 }
955
956 // Only handle COFF and ELF for now.
957 if (!TT.isOSBinFormatELF())
958 return false;
959
960 if (RM != llvm::Reloc::Static && !LOpts.PIE) {
961 // On ELF, if -fno-semantic-interposition is specified and the target
962 // supports local aliases, there will be neither CC1
963 // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set
964 // dso_local if using a local alias is preferable (can avoid GOT
965 // indirection).
966 if (!GV->canBenefitFromLocalAlias())
967 return false;
968 return !(CGM.getLangOpts().SemanticInterposition ||
969 CGM.getLangOpts().HalfNoSemanticInterposition);
970 }
971
972 // A definition cannot be preempted from an executable.
973 if (!GV->isDeclarationForLinker())
974 return true;
975
976 // Most PIC code sequences that assume that a symbol is local cannot produce a
977 // 0 if it turns out the symbol is undefined. While this is ABI and relocation
978 // depended, it seems worth it to handle it here.
979 if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage())
980 return false;
981
982 // PowerPC64 prefers TOC indirection to avoid copy relocations.
983 if (TT.isPPC64())
984 return false;
985
986 if (CGOpts.DirectAccessExternalData) {
987 // If -fdirect-access-external-data (default for -fno-pic), set dso_local
988 // for non-thread-local variables. If the symbol is not defined in the
989 // executable, a copy relocation will be needed at link time. dso_local is
990 // excluded for thread-local variables because they generally don't support
991 // copy relocations.
992 if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV))
993 if (!Var->isThreadLocal())
994 return true;
995
996 // -fno-pic sets dso_local on a function declaration to allow direct
997 // accesses when taking its address (similar to a data symbol). If the
998 // function is not defined in the executable, a canonical PLT entry will be
999 // needed at link time. -fno-direct-access-external-data can avoid the
1000 // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
1001 // it could just cause trouble without providing perceptible benefits.
1002 if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static)
1003 return true;
1004 }
1005
1006 // If we can use copy relocations we can assume it is local.
1007
1008 // Otherwise don't assume it is local.
1009 return false;
1010}
1011
1012void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const {
1013 GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV));
1014}
1015
1016void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
1017 GlobalDecl GD) const {
1018 const auto *D = dyn_cast<NamedDecl>(GD.getDecl());
1019 // C++ destructors have a few C++ ABI specific special cases.
1020 if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) {
1021 getCXXABI().setCXXDestructorDLLStorage(GV, Dtor, GD.getDtorType());
1022 return;
1023 }
1024 setDLLImportDLLExport(GV, D);
1025}
1026
1027void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
1028 const NamedDecl *D) const {
1029 if (D && D->isExternallyVisible()) {
1030 if (D->hasAttr<DLLImportAttr>())
1031 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
1032 else if (D->hasAttr<DLLExportAttr>() && !GV->isDeclarationForLinker())
1033 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
1034 }
1035}
1036
1037void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
1038 GlobalDecl GD) const {
1039 setDLLImportDLLExport(GV, GD);
1040 setGVPropertiesAux(GV, dyn_cast<NamedDecl>(GD.getDecl()));
1041}
1042
1043void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
1044 const NamedDecl *D) const {
1045 setDLLImportDLLExport(GV, D);
1046 setGVPropertiesAux(GV, D);
1047}
1048
1049void CodeGenModule::setGVPropertiesAux(llvm::GlobalValue *GV,
1050 const NamedDecl *D) const {
1051 setGlobalVisibility(GV, D);
1052 setDSOLocal(GV);
1053 GV->setPartition(CodeGenOpts.SymbolPartition);
1054}
1055
1056static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
1057 return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
1058 .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
1059 .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
1060 .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
1061 .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
1062}
1063
1064llvm::GlobalVariable::ThreadLocalMode
1065CodeGenModule::GetDefaultLLVMTLSModel() const {
1066 switch (CodeGenOpts.getDefaultTLSModel()) {
1067 case CodeGenOptions::GeneralDynamicTLSModel:
1068 return llvm::GlobalVariable::GeneralDynamicTLSModel;
1069 case CodeGenOptions::LocalDynamicTLSModel:
1070 return llvm::GlobalVariable::LocalDynamicTLSModel;
1071 case CodeGenOptions::InitialExecTLSModel:
1072 return llvm::GlobalVariable::InitialExecTLSModel;
1073 case CodeGenOptions::LocalExecTLSModel:
1074 return llvm::GlobalVariable::LocalExecTLSModel;
1075 }
1076 llvm_unreachable("Invalid TLS model!")::llvm::llvm_unreachable_internal("Invalid TLS model!", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1076)
;
1077}
1078
1079void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const {
1080 assert(D.getTLSKind() && "setting TLS mode on non-TLS var!")((D.getTLSKind() && "setting TLS mode on non-TLS var!"
) ? static_cast<void> (0) : __assert_fail ("D.getTLSKind() && \"setting TLS mode on non-TLS var!\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1080, __PRETTY_FUNCTION__))
;
1081
1082 llvm::GlobalValue::ThreadLocalMode TLM;
1083 TLM = GetDefaultLLVMTLSModel();
1084
1085 // Override the TLS model if it is explicitly specified.
1086 if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
1087 TLM = GetLLVMTLSModel(Attr->getModel());
1088 }
1089
1090 GV->setThreadLocalMode(TLM);
1091}
1092
1093static std::string getCPUSpecificMangling(const CodeGenModule &CGM,
1094 StringRef Name) {
1095 const TargetInfo &Target = CGM.getTarget();
1096 return (Twine('.') + Twine(Target.CPUSpecificManglingCharacter(Name))).str();
1097}
1098
1099static void AppendCPUSpecificCPUDispatchMangling(const CodeGenModule &CGM,
1100 const CPUSpecificAttr *Attr,
1101 unsigned CPUIndex,
1102 raw_ostream &Out) {
1103 // cpu_specific gets the current name, dispatch gets the resolver if IFunc is
1104 // supported.
1105 if (Attr)
1106 Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName());
1107 else if (CGM.getTarget().supportsIFunc())
1108 Out << ".resolver";
1109}
1110
1111static void AppendTargetMangling(const CodeGenModule &CGM,
1112 const TargetAttr *Attr, raw_ostream &Out) {
1113 if (Attr->isDefaultVersion())
1114 return;
1115
1116 Out << '.';
1117 const TargetInfo &Target = CGM.getTarget();
1118 ParsedTargetAttr Info =
1119 Attr->parse([&Target](StringRef LHS, StringRef RHS) {
1120 // Multiversioning doesn't allow "no-${feature}", so we can
1121 // only have "+" prefixes here.
1122 assert(LHS.startswith("+") && RHS.startswith("+") &&((LHS.startswith("+") && RHS.startswith("+") &&
"Features should always have a prefix.") ? static_cast<void
> (0) : __assert_fail ("LHS.startswith(\"+\") && RHS.startswith(\"+\") && \"Features should always have a prefix.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1123, __PRETTY_FUNCTION__))
1123 "Features should always have a prefix.")((LHS.startswith("+") && RHS.startswith("+") &&
"Features should always have a prefix.") ? static_cast<void
> (0) : __assert_fail ("LHS.startswith(\"+\") && RHS.startswith(\"+\") && \"Features should always have a prefix.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1123, __PRETTY_FUNCTION__))
;
1124 return Target.multiVersionSortPriority(LHS.substr(1)) >
1125 Target.multiVersionSortPriority(RHS.substr(1));
1126 });
1127
1128 bool IsFirst = true;
1129
1130 if (!Info.Architecture.empty()) {
1131 IsFirst = false;
1132 Out << "arch_" << Info.Architecture;
1133 }
1134
1135 for (StringRef Feat : Info.Features) {
1136 if (!IsFirst)
1137 Out << '_';
1138 IsFirst = false;
1139 Out << Feat.substr(1);
1140 }
1141}
1142
1143static std::string getMangledNameImpl(const CodeGenModule &CGM, GlobalDecl GD,
1144 const NamedDecl *ND,
1145 bool OmitMultiVersionMangling = false) {
1146 SmallString<256> Buffer;
1147 llvm::raw_svector_ostream Out(Buffer);
1148 MangleContext &MC = CGM.getCXXABI().getMangleContext();
1149 if (MC.shouldMangleDeclName(ND))
1150 MC.mangleName(GD.getWithDecl(ND), Out);
1151 else {
1152 IdentifierInfo *II = ND->getIdentifier();
1153 assert(II && "Attempt to mangle unnamed decl.")((II && "Attempt to mangle unnamed decl.") ? static_cast
<void> (0) : __assert_fail ("II && \"Attempt to mangle unnamed decl.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1153, __PRETTY_FUNCTION__))
;
1154 const auto *FD = dyn_cast<FunctionDecl>(ND);
1155
1156 if (FD &&
1157 FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
1158 Out << "__regcall3__" << II->getName();
1159 } else if (FD && FD->hasAttr<CUDAGlobalAttr>() &&
1160 GD.getKernelReferenceKind() == KernelReferenceKind::Stub) {
1161 Out << "__device_stub__" << II->getName();
1162 } else {
1163 Out << II->getName();
1164 }
1165 }
1166
1167 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
1168 if (FD->isMultiVersion() && !OmitMultiVersionMangling) {
1169 switch (FD->getMultiVersionKind()) {
1170 case MultiVersionKind::CPUDispatch:
1171 case MultiVersionKind::CPUSpecific:
1172 AppendCPUSpecificCPUDispatchMangling(CGM,
1173 FD->getAttr<CPUSpecificAttr>(),
1174 GD.getMultiVersionIndex(), Out);
1175 break;
1176 case MultiVersionKind::Target:
1177 AppendTargetMangling(CGM, FD->getAttr<TargetAttr>(), Out);
1178 break;
1179 case MultiVersionKind::None:
1180 llvm_unreachable("None multiversion type isn't valid here")::llvm::llvm_unreachable_internal("None multiversion type isn't valid here"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1180)
;
1181 }
1182 }
1183
1184 return std::string(Out.str());
1185}
1186
1187void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD,
1188 const FunctionDecl *FD) {
1189 if (!FD->isMultiVersion())
1190 return;
1191
1192 // Get the name of what this would be without the 'target' attribute. This
1193 // allows us to lookup the version that was emitted when this wasn't a
1194 // multiversion function.
1195 std::string NonTargetName =
1196 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
1197 GlobalDecl OtherGD;
1198 if (lookupRepresentativeDecl(NonTargetName, OtherGD)) {
1199 assert(OtherGD.getCanonicalDecl()((OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->
isMultiVersion() && "Other GD should now be a multiversioned function"
) ? static_cast<void> (0) : __assert_fail ("OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->isMultiVersion() && \"Other GD should now be a multiversioned function\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1203, __PRETTY_FUNCTION__))
1200 .getDecl()((OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->
isMultiVersion() && "Other GD should now be a multiversioned function"
) ? static_cast<void> (0) : __assert_fail ("OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->isMultiVersion() && \"Other GD should now be a multiversioned function\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1203, __PRETTY_FUNCTION__))
1201 ->getAsFunction()((OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->
isMultiVersion() && "Other GD should now be a multiversioned function"
) ? static_cast<void> (0) : __assert_fail ("OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->isMultiVersion() && \"Other GD should now be a multiversioned function\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1203, __PRETTY_FUNCTION__))
1202 ->isMultiVersion() &&((OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->
isMultiVersion() && "Other GD should now be a multiversioned function"
) ? static_cast<void> (0) : __assert_fail ("OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->isMultiVersion() && \"Other GD should now be a multiversioned function\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1203, __PRETTY_FUNCTION__))
1203 "Other GD should now be a multiversioned function")((OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->
isMultiVersion() && "Other GD should now be a multiversioned function"
) ? static_cast<void> (0) : __assert_fail ("OtherGD.getCanonicalDecl() .getDecl() ->getAsFunction() ->isMultiVersion() && \"Other GD should now be a multiversioned function\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1203, __PRETTY_FUNCTION__))
;
1204 // OtherFD is the version of this function that was mangled BEFORE
1205 // becoming a MultiVersion function. It potentially needs to be updated.
1206 const FunctionDecl *OtherFD = OtherGD.getCanonicalDecl()
1207 .getDecl()
1208 ->getAsFunction()
1209 ->getMostRecentDecl();
1210 std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD);
1211 // This is so that if the initial version was already the 'default'
1212 // version, we don't try to update it.
1213 if (OtherName != NonTargetName) {
1214 // Remove instead of erase, since others may have stored the StringRef
1215 // to this.
1216 const auto ExistingRecord = Manglings.find(NonTargetName);
1217 if (ExistingRecord != std::end(Manglings))
1218 Manglings.remove(&(*ExistingRecord));
1219 auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD));
1220 MangledDeclNames[OtherGD.getCanonicalDecl()] = Result.first->first();
1221 if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName))
1222 Entry->setName(OtherName);
1223 }
1224 }
1225}
1226
1227StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
1228 GlobalDecl CanonicalGD = GD.getCanonicalDecl();
1229
1230 // Some ABIs don't have constructor variants. Make sure that base and
1231 // complete constructors get mangled the same.
1232 if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) {
1233 if (!getTarget().getCXXABI().hasConstructorVariants()) {
1234 CXXCtorType OrigCtorType = GD.getCtorType();
1235 assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete)((OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete)
? static_cast<void> (0) : __assert_fail ("OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1235, __PRETTY_FUNCTION__))
;
1236 if (OrigCtorType == Ctor_Base)
1237 CanonicalGD = GlobalDecl(CD, Ctor_Complete);
1238 }
1239 }
1240
1241 auto FoundName = MangledDeclNames.find(CanonicalGD);
1242 if (FoundName != MangledDeclNames.end())
1243 return FoundName->second;
1244
1245 // Keep the first result in the case of a mangling collision.
1246 const auto *ND = cast<NamedDecl>(GD.getDecl());
1247 std::string MangledName = getMangledNameImpl(*this, GD, ND);
1248
1249 // Ensure either we have different ABIs between host and device compilations,
1250 // says host compilation following MSVC ABI but device compilation follows
1251 // Itanium C++ ABI or, if they follow the same ABI, kernel names after
1252 // mangling should be the same after name stubbing. The later checking is
1253 // very important as the device kernel name being mangled in host-compilation
1254 // is used to resolve the device binaries to be executed. Inconsistent naming
1255 // result in undefined behavior. Even though we cannot check that naming
1256 // directly between host- and device-compilations, the host- and
1257 // device-mangling in host compilation could help catching certain ones.
1258 assert(!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() ||((!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr
>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo
() && (getContext().getAuxTargetInfo()->getCXXABI(
) != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime
().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind
(KernelReferenceKind::Kernel), ND)) ? static_cast<void>
(0) : __assert_fail ("!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo() && (getContext().getAuxTargetInfo()->getCXXABI() != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), ND)"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1267, __PRETTY_FUNCTION__))
1259 getLangOpts().CUDAIsDevice ||((!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr
>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo
() && (getContext().getAuxTargetInfo()->getCXXABI(
) != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime
().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind
(KernelReferenceKind::Kernel), ND)) ? static_cast<void>
(0) : __assert_fail ("!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo() && (getContext().getAuxTargetInfo()->getCXXABI() != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), ND)"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1267, __PRETTY_FUNCTION__))
1260 (getContext().getAuxTargetInfo() &&((!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr
>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo
() && (getContext().getAuxTargetInfo()->getCXXABI(
) != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime
().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind
(KernelReferenceKind::Kernel), ND)) ? static_cast<void>
(0) : __assert_fail ("!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo() && (getContext().getAuxTargetInfo()->getCXXABI() != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), ND)"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1267, __PRETTY_FUNCTION__))
1261 (getContext().getAuxTargetInfo()->getCXXABI() !=((!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr
>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo
() && (getContext().getAuxTargetInfo()->getCXXABI(
) != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime
().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind
(KernelReferenceKind::Kernel), ND)) ? static_cast<void>
(0) : __assert_fail ("!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo() && (getContext().getAuxTargetInfo()->getCXXABI() != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), ND)"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1267, __PRETTY_FUNCTION__))
1262 getContext().getTargetInfo().getCXXABI())) ||((!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr
>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo
() && (getContext().getAuxTargetInfo()->getCXXABI(
) != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime
().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind
(KernelReferenceKind::Kernel), ND)) ? static_cast<void>
(0) : __assert_fail ("!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo() && (getContext().getAuxTargetInfo()->getCXXABI() != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), ND)"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1267, __PRETTY_FUNCTION__))
1263 getCUDARuntime().getDeviceSideName(ND) ==((!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr
>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo
() && (getContext().getAuxTargetInfo()->getCXXABI(
) != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime
().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind
(KernelReferenceKind::Kernel), ND)) ? static_cast<void>
(0) : __assert_fail ("!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo() && (getContext().getAuxTargetInfo()->getCXXABI() != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), ND)"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1267, __PRETTY_FUNCTION__))
1264 getMangledNameImpl(((!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr
>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo
() && (getContext().getAuxTargetInfo()->getCXXABI(
) != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime
().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind
(KernelReferenceKind::Kernel), ND)) ? static_cast<void>
(0) : __assert_fail ("!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo() && (getContext().getAuxTargetInfo()->getCXXABI() != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), ND)"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1267, __PRETTY_FUNCTION__))
1265 *this,((!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr
>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo
() && (getContext().getAuxTargetInfo()->getCXXABI(
) != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime
().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind
(KernelReferenceKind::Kernel), ND)) ? static_cast<void>
(0) : __assert_fail ("!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo() && (getContext().getAuxTargetInfo()->getCXXABI() != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), ND)"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1267, __PRETTY_FUNCTION__))
1266 GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel),((!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr
>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo
() && (getContext().getAuxTargetInfo()->getCXXABI(
) != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime
().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind
(KernelReferenceKind::Kernel), ND)) ? static_cast<void>
(0) : __assert_fail ("!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo() && (getContext().getAuxTargetInfo()->getCXXABI() != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), ND)"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1267, __PRETTY_FUNCTION__))
1267 ND))((!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr
>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo
() && (getContext().getAuxTargetInfo()->getCXXABI(
) != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime
().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind
(KernelReferenceKind::Kernel), ND)) ? static_cast<void>
(0) : __assert_fail ("!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || getLangOpts().CUDAIsDevice || (getContext().getAuxTargetInfo() && (getContext().getAuxTargetInfo()->getCXXABI() != getContext().getTargetInfo().getCXXABI())) || getCUDARuntime().getDeviceSideName(ND) == getMangledNameImpl( *this, GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), ND)"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1267, __PRETTY_FUNCTION__))
;
1268
1269 auto Result = Manglings.insert(std::make_pair(MangledName, GD));
1270 return MangledDeclNames[CanonicalGD] = Result.first->first();
1271}
1272
1273StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD,
1274 const BlockDecl *BD) {
1275 MangleContext &MangleCtx = getCXXABI().getMangleContext();
1276 const Decl *D = GD.getDecl();
1277
1278 SmallString<256> Buffer;
1279 llvm::raw_svector_ostream Out(Buffer);
1280 if (!D)
1281 MangleCtx.mangleGlobalBlock(BD,
1282 dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
1283 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
1284 MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
1285 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D))
1286 MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
1287 else
1288 MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
1289
1290 auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
1291 return Result.first->first();
1292}
1293
1294llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
1295 return getModule().getNamedValue(Name);
1296}
1297
1298/// AddGlobalCtor - Add a function to the list that will be called before
1299/// main() runs.
1300void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority,
1301 llvm::Constant *AssociatedData) {
1302 // FIXME: Type coercion of void()* types.
1303 GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData));
1304}
1305
1306/// AddGlobalDtor - Add a function to the list that will be called
1307/// when the module is unloaded.
1308void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority,
1309 bool IsDtorAttrFunc) {
1310 if (CodeGenOpts.RegisterGlobalDtorsWithAtExit &&
1311 (!getContext().getTargetInfo().getTriple().isOSAIX() || IsDtorAttrFunc)) {
1312 DtorsUsingAtExit[Priority].push_back(Dtor);
1313 return;
1314 }
1315
1316 // FIXME: Type coercion of void()* types.
1317 GlobalDtors.push_back(Structor(Priority, Dtor, nullptr));
1318}
1319
1320void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
1321 if (Fns.empty()) return;
1322
1323 // Ctor function type is void()*.
1324 llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
1325 llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy,
1326 TheModule.getDataLayout().getProgramAddressSpace());
1327
1328 // Get the type of a ctor entry, { i32, void ()*, i8* }.
1329 llvm::StructType *CtorStructTy = llvm::StructType::get(
1330 Int32Ty, CtorPFTy, VoidPtrTy);
1331
1332 // Construct the constructor and destructor arrays.
1333 ConstantInitBuilder builder(*this);
1334 auto ctors = builder.beginArray(CtorStructTy);
1335 for (const auto &I : Fns) {
1336 auto ctor = ctors.beginStruct(CtorStructTy);
1337 ctor.addInt(Int32Ty, I.Priority);
1338 ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy));
1339 if (I.AssociatedData)
1340 ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy));
1341 else
1342 ctor.addNullPointer(VoidPtrTy);
1343 ctor.finishAndAddTo(ctors);
1344 }
1345
1346 auto list =
1347 ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
1348 /*constant*/ false,
1349 llvm::GlobalValue::AppendingLinkage);
1350
1351 // The LTO linker doesn't seem to like it when we set an alignment
1352 // on appending variables. Take it off as a workaround.
1353 list->setAlignment(llvm::None);
1354
1355 Fns.clear();
1356}
1357
1358llvm::GlobalValue::LinkageTypes
1359CodeGenModule::getFunctionLinkage(GlobalDecl GD) {
1360 const auto *D = cast<FunctionDecl>(GD.getDecl());
1361
1362 GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
1363
1364 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D))
1365 return getCXXABI().getCXXDestructorLinkage(Linkage, Dtor, GD.getDtorType());
1366
1367 if (isa<CXXConstructorDecl>(D) &&
1368 cast<CXXConstructorDecl>(D)->isInheritingConstructor() &&
1369 Context.getTargetInfo().getCXXABI().isMicrosoft()) {
1370 // Our approach to inheriting constructors is fundamentally different from
1371 // that used by the MS ABI, so keep our inheriting constructor thunks
1372 // internal rather than trying to pick an unambiguous mangling for them.
1373 return llvm::GlobalValue::InternalLinkage;
1374 }
1375
1376 return getLLVMLinkageForDeclarator(D, Linkage, /*IsConstantVariable=*/false);
1377}
1378
1379llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
1380 llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD);
1381 if (!MDS) return nullptr;
1382
1383 return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
1384}
1385
1386void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD,
1387 const CGFunctionInfo &Info,
1388 llvm::Function *F) {
1389 unsigned CallingConv;
1390 llvm::AttributeList PAL;
1391 ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv, false);
1392 F->setAttributes(PAL);
1393 F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
1394}
1395
1396static void removeImageAccessQualifier(std::string& TyName) {
1397 std::string ReadOnlyQual("__read_only");
1398 std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual);
1399 if (ReadOnlyPos != std::string::npos)
1400 // "+ 1" for the space after access qualifier.
1401 TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1);
1402 else {
1403 std::string WriteOnlyQual("__write_only");
1404 std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual);
1405 if (WriteOnlyPos != std::string::npos)
1406 TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1);
1407 else {
1408 std::string ReadWriteQual("__read_write");
1409 std::string::size_type ReadWritePos = TyName.find(ReadWriteQual);
1410 if (ReadWritePos != std::string::npos)
1411 TyName.erase(ReadWritePos, ReadWriteQual.size() + 1);
1412 }
1413 }
1414}
1415
1416// Returns the address space id that should be produced to the
1417// kernel_arg_addr_space metadata. This is always fixed to the ids
1418// as specified in the SPIR 2.0 specification in order to differentiate
1419// for example in clGetKernelArgInfo() implementation between the address
1420// spaces with targets without unique mapping to the OpenCL address spaces
1421// (basically all single AS CPUs).
1422static unsigned ArgInfoAddressSpace(LangAS AS) {
1423 switch (AS) {
1424 case LangAS::opencl_global:
1425 return 1;
1426 case LangAS::opencl_constant:
1427 return 2;
1428 case LangAS::opencl_local:
1429 return 3;
1430 case LangAS::opencl_generic:
1431 return 4; // Not in SPIR 2.0 specs.
1432 case LangAS::opencl_global_device:
1433 return 5;
1434 case LangAS::opencl_global_host:
1435 return 6;
1436 default:
1437 return 0; // Assume private.
1438 }
1439}
1440
1441void CodeGenModule::GenOpenCLArgMetadata(llvm::Function *Fn,
1442 const FunctionDecl *FD,
1443 CodeGenFunction *CGF) {
1444 assert(((FD && CGF) || (!FD && !CGF)) &&((((FD && CGF) || (!FD && !CGF)) && "Incorrect use - FD and CGF should either be both null or not!"
) ? static_cast<void> (0) : __assert_fail ("((FD && CGF) || (!FD && !CGF)) && \"Incorrect use - FD and CGF should either be both null or not!\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1445, __PRETTY_FUNCTION__))
1445 "Incorrect use - FD and CGF should either be both null or not!")((((FD && CGF) || (!FD && !CGF)) && "Incorrect use - FD and CGF should either be both null or not!"
) ? static_cast<void> (0) : __assert_fail ("((FD && CGF) || (!FD && !CGF)) && \"Incorrect use - FD and CGF should either be both null or not!\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 1445, __PRETTY_FUNCTION__))
;
1446 // Create MDNodes that represent the kernel arg metadata.
1447 // Each MDNode is a list in the form of "key", N number of values which is
1448 // the same number of values as their are kernel arguments.
1449
1450 const PrintingPolicy &Policy = Context.getPrintingPolicy();
1451
1452 // MDNode for the kernel argument address space qualifiers.
1453 SmallVector<llvm::Metadata *, 8> addressQuals;
1454
1455 // MDNode for the kernel argument access qualifiers (images only).
1456 SmallVector<llvm::Metadata *, 8> accessQuals;
1457
1458 // MDNode for the kernel argument type names.
1459 SmallVector<llvm::Metadata *, 8> argTypeNames;
1460
1461 // MDNode for the kernel argument base type names.
1462 SmallVector<llvm::Metadata *, 8> argBaseTypeNames;
1463
1464 // MDNode for the kernel argument type qualifiers.
1465 SmallVector<llvm::Metadata *, 8> argTypeQuals;
1466
1467 // MDNode for the kernel argument names.
1468 SmallVector<llvm::Metadata *, 8> argNames;
1469
1470 if (FD && CGF)
1471 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
1472 const ParmVarDecl *parm = FD->getParamDecl(i);
1473 QualType ty = parm->getType();
1474 std::string typeQuals;
1475
1476 if (ty->isPointerType()) {
1477 QualType pointeeTy = ty->getPointeeType();
1478
1479 // Get address qualifier.
1480 addressQuals.push_back(
1481 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(
1482 ArgInfoAddressSpace(pointeeTy.getAddressSpace()))));
1483
1484 // Get argument type name.
1485 std::string typeName =
1486 pointeeTy.getUnqualifiedType().getAsString(Policy) + "*";
1487
1488 // Turn "unsigned type" to "utype"
1489 std::string::size_type pos = typeName.find("unsigned");
1490 if (pointeeTy.isCanonical() && pos != std::string::npos)
1491 typeName.erase(pos + 1, 8);
1492
1493 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
1494
1495 std::string baseTypeName =
1496 pointeeTy.getUnqualifiedType().getCanonicalType().getAsString(
1497 Policy) +
1498 "*";
1499
1500 // Turn "unsigned type" to "utype"
1501 pos = baseTypeName.find("unsigned");
1502 if (pos != std::string::npos)
1503 baseTypeName.erase(pos + 1, 8);
1504
1505 argBaseTypeNames.push_back(
1506 llvm::MDString::get(VMContext, baseTypeName));
1507
1508 // Get argument type qualifiers:
1509 if (ty.isRestrictQualified())
1510 typeQuals = "restrict";
1511 if (pointeeTy.isConstQualified() ||
1512 (pointeeTy.getAddressSpace() == LangAS::opencl_constant))
1513 typeQuals += typeQuals.empty() ? "const" : " const";
1514 if (pointeeTy.isVolatileQualified())
1515 typeQuals += typeQuals.empty() ? "volatile" : " volatile";
1516 } else {
1517 uint32_t AddrSpc = 0;
1518 bool isPipe = ty->isPipeType();
1519 if (ty->isImageType() || isPipe)
1520 AddrSpc = ArgInfoAddressSpace(LangAS::opencl_global);
1521
1522 addressQuals.push_back(
1523 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc)));
1524
1525 // Get argument type name.
1526 std::string typeName;
1527 if (isPipe)
1528 typeName = ty.getCanonicalType()
1529 ->castAs<PipeType>()
1530 ->getElementType()
1531 .getAsString(Policy);
1532 else
1533 typeName = ty.getUnqualifiedType().getAsString(Policy);
1534
1535 // Turn "unsigned type" to "utype"
1536 std::string::size_type pos = typeName.find("unsigned");
1537 if (ty.isCanonical() && pos != std::string::npos)
1538 typeName.erase(pos + 1, 8);
1539
1540 std::string baseTypeName;
1541 if (isPipe)
1542 baseTypeName = ty.getCanonicalType()
1543 ->castAs<PipeType>()
1544 ->getElementType()
1545 .getCanonicalType()
1546 .getAsString(Policy);
1547 else
1548 baseTypeName =
1549 ty.getUnqualifiedType().getCanonicalType().getAsString(Policy);
1550
1551 // Remove access qualifiers on images
1552 // (as they are inseparable from type in clang implementation,
1553 // but OpenCL spec provides a special query to get access qualifier
1554 // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER):
1555 if (ty->isImageType()) {
1556 removeImageAccessQualifier(typeName);
1557 removeImageAccessQualifier(baseTypeName);
1558 }
1559
1560 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
1561
1562 // Turn "unsigned type" to "utype"
1563 pos = baseTypeName.find("unsigned");
1564 if (pos != std::string::npos)
1565 baseTypeName.erase(pos + 1, 8);
1566
1567 argBaseTypeNames.push_back(
1568 llvm::MDString::get(VMContext, baseTypeName));
1569
1570 if (isPipe)
1571 typeQuals = "pipe";
1572 }
1573
1574 argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals));
1575
1576 // Get image and pipe access qualifier:
1577 if (ty->isImageType() || ty->isPipeType()) {
1578 const Decl *PDecl = parm;
1579 if (auto *TD = dyn_cast<TypedefType>(ty))
1580 PDecl = TD->getDecl();
1581 const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
1582 if (A && A->isWriteOnly())
1583 accessQuals.push_back(llvm::MDString::get(VMContext, "write_only"));
1584 else if (A && A->isReadWrite())
1585 accessQuals.push_back(llvm::MDString::get(VMContext, "read_write"));
1586 else
1587 accessQuals.push_back(llvm::MDString::get(VMContext, "read_only"));
1588 } else
1589 accessQuals.push_back(llvm::MDString::get(VMContext, "none"));
1590
1591 // Get argument name.
1592 argNames.push_back(llvm::MDString::get(VMContext, parm->getName()));
1593 }
1594
1595 Fn->setMetadata("kernel_arg_addr_space",
1596 llvm::MDNode::get(VMContext, addressQuals));
1597 Fn->setMetadata("kernel_arg_access_qual",
1598 llvm::MDNode::get(VMContext, accessQuals));
1599 Fn->setMetadata("kernel_arg_type",
1600 llvm::MDNode::get(VMContext, argTypeNames));
1601 Fn->setMetadata("kernel_arg_base_type",
1602 llvm::MDNode::get(VMContext, argBaseTypeNames));
1603 Fn->setMetadata("kernel_arg_type_qual",
1604 llvm::MDNode::get(VMContext, argTypeQuals));
1605 if (getCodeGenOpts().EmitOpenCLArgMetadata)
1606 Fn->setMetadata("kernel_arg_name",
1607 llvm::MDNode::get(VMContext, argNames));
1608}
1609
1610/// Determines whether the language options require us to model
1611/// unwind exceptions. We treat -fexceptions as mandating this
1612/// except under the fragile ObjC ABI with only ObjC exceptions
1613/// enabled. This means, for example, that C with -fexceptions
1614/// enables this.
1615static bool hasUnwindExceptions(const LangOptions &LangOpts) {
1616 // If exceptions are completely disabled, obviously this is false.
1617 if (!LangOpts.Exceptions) return false;
1618
1619 // If C++ exceptions are enabled, this is true.
1620 if (LangOpts.CXXExceptions) return true;
1621
1622 // If ObjC exceptions are enabled, this depends on the ABI.
1623 if (LangOpts.ObjCExceptions) {
1624 return LangOpts.ObjCRuntime.hasUnwindExceptions();
1625 }
1626
1627 return true;
1628}
1629
1630static bool requiresMemberFunctionPointerTypeMetadata(CodeGenModule &CGM,
1631 const CXXMethodDecl *MD) {
1632 // Check that the type metadata can ever actually be used by a call.
1633 if (!CGM.getCodeGenOpts().LTOUnit ||
1634 !CGM.HasHiddenLTOVisibility(MD->getParent()))
1635 return false;
1636
1637 // Only functions whose address can be taken with a member function pointer
1638 // need this sort of type metadata.
1639 return !MD->isStatic() && !MD->isVirtual() && !isa<CXXConstructorDecl>(MD) &&
1640 !isa<CXXDestructorDecl>(MD);
1641}
1642
1643std::vector<const CXXRecordDecl *>
1644CodeGenModule::getMostBaseClasses(const CXXRecordDecl *RD) {
1645 llvm::SetVector<const CXXRecordDecl *> MostBases;
1646
1647 std::function<void (const CXXRecordDecl *)> CollectMostBases;
1648 CollectMostBases = [&](const CXXRecordDecl *RD) {
1649 if (RD->getNumBases() == 0)
1650 MostBases.insert(RD);
1651 for (const CXXBaseSpecifier &B : RD->bases())
1652 CollectMostBases(B.getType()->getAsCXXRecordDecl());
1653 };
1654 CollectMostBases(RD);
1655 return MostBases.takeVector();
1656}
1657
1658void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
1659 llvm::Function *F) {
1660 llvm::AttrBuilder B;
1661
1662 if (CodeGenOpts.UnwindTables)
1663 B.addAttribute(llvm::Attribute::UWTable);
1664
1665 if (CodeGenOpts.StackClashProtector)
1666 B.addAttribute("probe-stack", "inline-asm");
1667
1668 if (!hasUnwindExceptions(LangOpts))
1669 B.addAttribute(llvm::Attribute::NoUnwind);
1670
1671 if (!D || !D->hasAttr<NoStackProtectorAttr>()) {
1672 if (LangOpts.getStackProtector() == LangOptions::SSPOn)
1673 B.addAttribute(llvm::Attribute::StackProtect);
1674 else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
1675 B.addAttribute(llvm::Attribute::StackProtectStrong);
1676 else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
1677 B.addAttribute(llvm::Attribute::StackProtectReq);
1678 }
1679
1680 if (!D) {
1681 // If we don't have a declaration to control inlining, the function isn't
1682 // explicitly marked as alwaysinline for semantic reasons, and inlining is
1683 // disabled, mark the function as noinline.
1684 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
1685 CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining)
1686 B.addAttribute(llvm::Attribute::NoInline);
1687
1688 F->addAttributes(llvm::AttributeList::FunctionIndex, B);
1689 return;
1690 }
1691
1692 // Track whether we need to add the optnone LLVM attribute,
1693 // starting with the default for this optimization level.
1694 bool ShouldAddOptNone =
1695 !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0;
1696 // We can't add optnone in the following cases, it won't pass the verifier.
1697 ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>();
1698 ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>();
1699
1700 // Add optnone, but do so only if the function isn't always_inline.
1701 if ((ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) &&
1702 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
1703 B.addAttribute(llvm::Attribute::OptimizeNone);
1704
1705 // OptimizeNone implies noinline; we should not be inlining such functions.
1706 B.addAttribute(llvm::Attribute::NoInline);
1707
1708 // We still need to handle naked functions even though optnone subsumes
1709 // much of their semantics.
1710 if (D->hasAttr<NakedAttr>())
1711 B.addAttribute(llvm::Attribute::Naked);
1712
1713 // OptimizeNone wins over OptimizeForSize and MinSize.
1714 F->removeFnAttr(llvm::Attribute::OptimizeForSize);
1715 F->removeFnAttr(llvm::Attribute::MinSize);
1716 } else if (D->hasAttr<NakedAttr>()) {
1717 // Naked implies noinline: we should not be inlining such functions.
1718 B.addAttribute(llvm::Attribute::Naked);
1719 B.addAttribute(llvm::Attribute::NoInline);
1720 } else if (D->hasAttr<NoDuplicateAttr>()) {
1721 B.addAttribute(llvm::Attribute::NoDuplicate);
1722 } else if (D->hasAttr<NoInlineAttr>() && !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
1723 // Add noinline if the function isn't always_inline.
1724 B.addAttribute(llvm::Attribute::NoInline);
1725 } else if (D->hasAttr<AlwaysInlineAttr>() &&
1726 !F->hasFnAttribute(llvm::Attribute::NoInline)) {
1727 // (noinline wins over always_inline, and we can't specify both in IR)
1728 B.addAttribute(llvm::Attribute::AlwaysInline);
1729 } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
1730 // If we're not inlining, then force everything that isn't always_inline to
1731 // carry an explicit noinline attribute.
1732 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
1733 B.addAttribute(llvm::Attribute::NoInline);
1734 } else {
1735 // Otherwise, propagate the inline hint attribute and potentially use its
1736 // absence to mark things as noinline.
1737 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
1738 // Search function and template pattern redeclarations for inline.
1739 auto CheckForInline = [](const FunctionDecl *FD) {
1740 auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
1741 return Redecl->isInlineSpecified();
1742 };
1743 if (any_of(FD->redecls(), CheckRedeclForInline))
1744 return true;
1745 const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
1746 if (!Pattern)
1747 return false;
1748 return any_of(Pattern->redecls(), CheckRedeclForInline);
1749 };
1750 if (CheckForInline(FD)) {
1751 B.addAttribute(llvm::Attribute::InlineHint);
1752 } else if (CodeGenOpts.getInlining() ==
1753 CodeGenOptions::OnlyHintInlining &&
1754 !FD->isInlined() &&
1755 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
1756 B.addAttribute(llvm::Attribute::NoInline);
1757 }
1758 }
1759 }
1760
1761 // Add other optimization related attributes if we are optimizing this
1762 // function.
1763 if (!D->hasAttr<OptimizeNoneAttr>()) {
1764 if (D->hasAttr<ColdAttr>()) {
1765 if (!ShouldAddOptNone)
1766 B.addAttribute(llvm::Attribute::OptimizeForSize);
1767 B.addAttribute(llvm::Attribute::Cold);
1768 }
1769 if (D->hasAttr<HotAttr>())
1770 B.addAttribute(llvm::Attribute::Hot);
1771 if (D->hasAttr<MinSizeAttr>())
1772 B.addAttribute(llvm::Attribute::MinSize);
1773 }
1774
1775 F->addAttributes(llvm::AttributeList::FunctionIndex, B);
1776
1777 unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
1778 if (alignment)
1779 F->setAlignment(llvm::Align(alignment));
1780
1781 if (!D->hasAttr<AlignedAttr>())
1782 if (LangOpts.FunctionAlignment)
1783 F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
1784
1785 // Some C++ ABIs require 2-byte alignment for member functions, in order to
1786 // reserve a bit for differentiating between virtual and non-virtual member
1787 // functions. If the current target's C++ ABI requires this and this is a
1788 // member function, set its alignment accordingly.
1789 if (getTarget().getCXXABI().areMemberFunctionsAligned()) {
1790 if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
1791 F->setAlignment(llvm::Align(2));
1792 }
1793
1794 // In the cross-dso CFI mode with canonical jump tables, we want !type
1795 // attributes on definitions only.
1796 if (CodeGenOpts.SanitizeCfiCrossDso &&
1797 CodeGenOpts.SanitizeCfiCanonicalJumpTables) {
1798 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
1799 // Skip available_externally functions. They won't be codegen'ed in the
1800 // current module anyway.
1801 if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally)
1802 CreateFunctionTypeMetadataForIcall(FD, F);
1803 }
1804 }
1805
1806 // Emit type metadata on member functions for member function pointer checks.
1807 // These are only ever necessary on definitions; we're guaranteed that the
1808 // definition will be present in the LTO unit as a result of LTO visibility.
1809 auto *MD = dyn_cast<CXXMethodDecl>(D);
1810 if (MD && requiresMemberFunctionPointerTypeMetadata(*this, MD)) {
1811 for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) {
1812 llvm::Metadata *Id =
1813 CreateMetadataIdentifierForType(Context.getMemberPointerType(
1814 MD->getType(), Context.getRecordType(Base).getTypePtr()));
1815 F->addTypeMetadata(0, Id);
1816 }
1817 }
1818}
1819
1820void CodeGenModule::setLLVMFunctionFEnvAttributes(const FunctionDecl *D,
1821 llvm::Function *F) {
1822 if (D->hasAttr<StrictFPAttr>()) {
1823 llvm::AttrBuilder FuncAttrs;
1824 FuncAttrs.addAttribute("strictfp");
1825 F->addAttributes(llvm::AttributeList::FunctionIndex, FuncAttrs);
1826 }
1827}
1828
1829void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) {
1830 const Decl *D = GD.getDecl();
1831 if (dyn_cast_or_null<NamedDecl>(D))
1832 setGVProperties(GV, GD);
1833 else
1834 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1835
1836 if (D && D->hasAttr<UsedAttr>())
1837 addUsedGlobal(GV);
1838
1839 if (CodeGenOpts.KeepStaticConsts && D && isa<VarDecl>(D)) {
1840 const auto *VD = cast<VarDecl>(D);
1841 if (VD->getType().isConstQualified() &&
1842 VD->getStorageDuration() == SD_Static)
1843 addUsedGlobal(GV);
1844 }
1845}
1846
1847bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
1848 llvm::AttrBuilder &Attrs) {
1849 // Add target-cpu and target-features attributes to functions. If
1850 // we have a decl for the function and it has a target attribute then
1851 // parse that and add it to the feature set.
1852 StringRef TargetCPU = getTarget().getTargetOpts().CPU;
1853 StringRef TuneCPU = getTarget().getTargetOpts().TuneCPU;
1854 std::vector<std::string> Features;
1855 const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl());
1856 FD = FD ? FD->getMostRecentDecl() : FD;
1857 const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr;
1858 const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr;
1859 bool AddedAttr = false;
1860 if (TD || SD) {
1861 llvm::StringMap<bool> FeatureMap;
1862 getContext().getFunctionFeatureMap(FeatureMap, GD);
1863
1864 // Produce the canonical string for this set of features.
1865 for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap)
1866 Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str());
1867
1868 // Now add the target-cpu and target-features to the function.
1869 // While we populated the feature map above, we still need to
1870 // get and parse the target attribute so we can get the cpu for
1871 // the function.
1872 if (TD) {
1873 ParsedTargetAttr ParsedAttr = TD->parse();
1874 if (!ParsedAttr.Architecture.empty() &&
1875 getTarget().isValidCPUName(ParsedAttr.Architecture)) {
1876 TargetCPU = ParsedAttr.Architecture;
1877 TuneCPU = ""; // Clear the tune CPU.
1878 }
1879 if (!ParsedAttr.Tune.empty() &&
1880 getTarget().isValidCPUName(ParsedAttr.Tune))
1881 TuneCPU = ParsedAttr.Tune;
1882 }
1883 } else {
1884 // Otherwise just add the existing target cpu and target features to the
1885 // function.
1886 Features = getTarget().getTargetOpts().Features;
1887 }
1888
1889 if (!TargetCPU.empty()) {
1890 Attrs.addAttribute("target-cpu", TargetCPU);
1891 AddedAttr = true;
1892 }
1893 if (!TuneCPU.empty()) {
1894 Attrs.addAttribute("tune-cpu", TuneCPU);
1895 AddedAttr = true;
1896 }
1897 if (!Features.empty()) {
1898 llvm::sort(Features);
1899 Attrs.addAttribute("target-features", llvm::join(Features, ","));
1900 AddedAttr = true;
1901 }
1902
1903 return AddedAttr;
1904}
1905
1906void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
1907 llvm::GlobalObject *GO) {
1908 const Decl *D = GD.getDecl();
1909 SetCommonAttributes(GD, GO);
1910
1911 if (D) {
1912 if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) {
1913 if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
1914 GV->addAttribute("bss-section", SA->getName());
1915 if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>())
1916 GV->addAttribute("data-section", SA->getName());
1917 if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
1918 GV->addAttribute("rodata-section", SA->getName());
1919 if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
1920 GV->addAttribute("relro-section", SA->getName());
1921 }
1922
1923 if (auto *F = dyn_cast<llvm::Function>(GO)) {
1924 if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
1925 if (!D->getAttr<SectionAttr>())
1926 F->addFnAttr("implicit-section-name", SA->getName());
1927
1928 llvm::AttrBuilder Attrs;
1929 if (GetCPUAndFeaturesAttributes(GD, Attrs)) {
1930 // We know that GetCPUAndFeaturesAttributes will always have the
1931 // newest set, since it has the newest possible FunctionDecl, so the
1932 // new ones should replace the old.
1933 llvm::AttrBuilder RemoveAttrs;
1934 RemoveAttrs.addAttribute("target-cpu");
1935 RemoveAttrs.addAttribute("target-features");
1936 RemoveAttrs.addAttribute("tune-cpu");
1937 F->removeAttributes(llvm::AttributeList::FunctionIndex, RemoveAttrs);
1938 F->addAttributes(llvm::AttributeList::FunctionIndex, Attrs);
1939 }
1940 }
1941
1942 if (const auto *CSA = D->getAttr<CodeSegAttr>())
1943 GO->setSection(CSA->getName());
1944 else if (const auto *SA = D->getAttr<SectionAttr>())
1945 GO->setSection(SA->getName());
1946 }
1947
1948 getTargetCodeGenInfo().setTargetAttributes(D, GO, *this);
1949}
1950
1951void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD,
1952 llvm::Function *F,
1953 const CGFunctionInfo &FI) {
1954 const Decl *D = GD.getDecl();
1955 SetLLVMFunctionAttributes(GD, FI, F);
1956 SetLLVMFunctionAttributesForDefinition(D, F);
1957
1958 F->setLinkage(llvm::Function::InternalLinkage);
1959
1960 setNonAliasAttributes(GD, F);
1961}
1962
1963static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
1964 // Set linkage and visibility in case we never see a definition.
1965 LinkageInfo LV = ND->getLinkageAndVisibility();
1966 // Don't set internal linkage on declarations.
1967 // "extern_weak" is overloaded in LLVM; we probably should have
1968 // separate linkage types for this.
1969 if (isExternallyVisible(LV.getLinkage()) &&
1970 (ND->hasAttr<WeakAttr>() || ND->isWeakImported()))
1971 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
1972}
1973
1974void CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
1975 llvm::Function *F) {
1976 // Only if we are checking indirect calls.
1977 if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
1978 return;
1979
1980 // Non-static class methods are handled via vtable or member function pointer
1981 // checks elsewhere.
1982 if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
1983 return;
1984
1985 llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
1986 F->addTypeMetadata(0, MD);
1987 F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
1988
1989 // Emit a hash-based bit set entry for cross-DSO calls.
1990 if (CodeGenOpts.SanitizeCfiCrossDso)
1991 if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
1992 F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
1993}
1994
1995void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
1996 bool IsIncompleteFunction,
1997 bool IsThunk) {
1998
1999 if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) {
2000 // If this is an intrinsic function, set the function's attributes
2001 // to the intrinsic's attributes.
2002 F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID));
2003 return;
2004 }
2005
2006 const auto *FD = cast<FunctionDecl>(GD.getDecl());
2007
2008 if (!IsIncompleteFunction)
2009 SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F);
2010
2011 // Add the Returned attribute for "this", except for iOS 5 and earlier
2012 // where substantial code, including the libstdc++ dylib, was compiled with
2013 // GCC and does not actually return "this".
2014 if (!IsThunk && getCXXABI().HasThisReturn(GD) &&
2015 !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) {
2016 assert(!F->arg_empty() &&((!F->arg_empty() && F->arg_begin()->getType
() ->canLosslesslyBitCastTo(F->getReturnType()) &&
"unexpected this return") ? static_cast<void> (0) : __assert_fail
("!F->arg_empty() && F->arg_begin()->getType() ->canLosslesslyBitCastTo(F->getReturnType()) && \"unexpected this return\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2019, __PRETTY_FUNCTION__))
2017 F->arg_begin()->getType()((!F->arg_empty() && F->arg_begin()->getType
() ->canLosslesslyBitCastTo(F->getReturnType()) &&
"unexpected this return") ? static_cast<void> (0) : __assert_fail
("!F->arg_empty() && F->arg_begin()->getType() ->canLosslesslyBitCastTo(F->getReturnType()) && \"unexpected this return\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2019, __PRETTY_FUNCTION__))
2018 ->canLosslesslyBitCastTo(F->getReturnType()) &&((!F->arg_empty() && F->arg_begin()->getType
() ->canLosslesslyBitCastTo(F->getReturnType()) &&
"unexpected this return") ? static_cast<void> (0) : __assert_fail
("!F->arg_empty() && F->arg_begin()->getType() ->canLosslesslyBitCastTo(F->getReturnType()) && \"unexpected this return\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2019, __PRETTY_FUNCTION__))
2019 "unexpected this return")((!F->arg_empty() && F->arg_begin()->getType
() ->canLosslesslyBitCastTo(F->getReturnType()) &&
"unexpected this return") ? static_cast<void> (0) : __assert_fail
("!F->arg_empty() && F->arg_begin()->getType() ->canLosslesslyBitCastTo(F->getReturnType()) && \"unexpected this return\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2019, __PRETTY_FUNCTION__))
;
2020 F->addAttribute(1, llvm::Attribute::Returned);
2021 }
2022
2023 // Only a few attributes are set on declarations; these may later be
2024 // overridden by a definition.
2025
2026 setLinkageForGV(F, FD);
2027 setGVProperties(F, FD);
2028
2029 // Setup target-specific attributes.
2030 if (!IsIncompleteFunction && F->isDeclaration())
2031 getTargetCodeGenInfo().setTargetAttributes(FD, F, *this);
2032
2033 if (const auto *CSA = FD->getAttr<CodeSegAttr>())
2034 F->setSection(CSA->getName());
2035 else if (const auto *SA = FD->getAttr<SectionAttr>())
2036 F->setSection(SA->getName());
2037
2038 // If we plan on emitting this inline builtin, we can't treat it as a builtin.
2039 if (FD->isInlineBuiltinDeclaration()) {
2040 const FunctionDecl *FDBody;
2041 bool HasBody = FD->hasBody(FDBody);
2042 (void)HasBody;
2043 assert(HasBody && "Inline builtin declarations should always have an "((HasBody && "Inline builtin declarations should always have an "
"available body!") ? static_cast<void> (0) : __assert_fail
("HasBody && \"Inline builtin declarations should always have an \" \"available body!\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2044, __PRETTY_FUNCTION__))
2044 "available body!")((HasBody && "Inline builtin declarations should always have an "
"available body!") ? static_cast<void> (0) : __assert_fail
("HasBody && \"Inline builtin declarations should always have an \" \"available body!\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2044, __PRETTY_FUNCTION__))
;
2045 if (shouldEmitFunction(FDBody))
2046 F->addAttribute(llvm::AttributeList::FunctionIndex,
2047 llvm::Attribute::NoBuiltin);
2048 }
2049
2050 if (FD->isReplaceableGlobalAllocationFunction()) {
2051 // A replaceable global allocation function does not act like a builtin by
2052 // default, only if it is invoked by a new-expression or delete-expression.
2053 F->addAttribute(llvm::AttributeList::FunctionIndex,
2054 llvm::Attribute::NoBuiltin);
2055 }
2056
2057 if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))
2058 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2059 else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
2060 if (MD->isVirtual())
2061 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2062
2063 // Don't emit entries for function declarations in the cross-DSO mode. This
2064 // is handled with better precision by the receiving DSO. But if jump tables
2065 // are non-canonical then we need type metadata in order to produce the local
2066 // jump table.
2067 if (!CodeGenOpts.SanitizeCfiCrossDso ||
2068 !CodeGenOpts.SanitizeCfiCanonicalJumpTables)
2069 CreateFunctionTypeMetadataForIcall(FD, F);
2070
2071 if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
2072 getOpenMPRuntime().emitDeclareSimdFunction(FD, F);
2073
2074 if (const auto *CB = FD->getAttr<CallbackAttr>()) {
2075 // Annotate the callback behavior as metadata:
2076 // - The callback callee (as argument number).
2077 // - The callback payloads (as argument numbers).
2078 llvm::LLVMContext &Ctx = F->getContext();
2079 llvm::MDBuilder MDB(Ctx);
2080
2081 // The payload indices are all but the first one in the encoding. The first
2082 // identifies the callback callee.
2083 int CalleeIdx = *CB->encoding_begin();
2084 ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end());
2085 F->addMetadata(llvm::LLVMContext::MD_callback,
2086 *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
2087 CalleeIdx, PayloadIndices,
2088 /* VarArgsArePassed */ false)}));
2089 }
2090}
2091
2092void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
2093 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&(((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
"Only globals with definition can force usage.") ? static_cast
<void> (0) : __assert_fail ("(isa<llvm::Function>(GV) || !GV->isDeclaration()) && \"Only globals with definition can force usage.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2094, __PRETTY_FUNCTION__))
2094 "Only globals with definition can force usage.")(((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
"Only globals with definition can force usage.") ? static_cast
<void> (0) : __assert_fail ("(isa<llvm::Function>(GV) || !GV->isDeclaration()) && \"Only globals with definition can force usage.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2094, __PRETTY_FUNCTION__))
;
2095 LLVMUsed.emplace_back(GV);
2096}
2097
2098void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
2099 assert(!GV->isDeclaration() &&((!GV->isDeclaration() && "Only globals with definition can force usage."
) ? static_cast<void> (0) : __assert_fail ("!GV->isDeclaration() && \"Only globals with definition can force usage.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2100, __PRETTY_FUNCTION__))
2100 "Only globals with definition can force usage.")((!GV->isDeclaration() && "Only globals with definition can force usage."
) ? static_cast<void> (0) : __assert_fail ("!GV->isDeclaration() && \"Only globals with definition can force usage.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2100, __PRETTY_FUNCTION__))
;
2101 LLVMCompilerUsed.emplace_back(GV);
2102}
2103
2104static void emitUsed(CodeGenModule &CGM, StringRef Name,
2105 std::vector<llvm::WeakTrackingVH> &List) {
2106 // Don't create llvm.used if there is no need.
2107 if (List.empty())
2108 return;
2109
2110 // Convert List to what ConstantArray needs.
2111 SmallVector<llvm::Constant*, 8> UsedArray;
2112 UsedArray.resize(List.size());
2113 for (unsigned i = 0, e = List.size(); i != e; ++i) {
2114 UsedArray[i] =
2115 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
2116 cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
2117 }
2118
2119 if (UsedArray.empty())
2120 return;
2121 llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
2122
2123 auto *GV = new llvm::GlobalVariable(
2124 CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
2125 llvm::ConstantArray::get(ATy, UsedArray), Name);
2126
2127 GV->setSection("llvm.metadata");
2128}
2129
2130void CodeGenModule::emitLLVMUsed() {
2131 emitUsed(*this, "llvm.used", LLVMUsed);
2132 emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
2133}
2134
2135void CodeGenModule::AppendLinkerOptions(StringRef Opts) {
2136 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
2137 LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2138}
2139
2140void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
2141 llvm::SmallString<32> Opt;
2142 getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt);
2143 if (Opt.empty())
2144 return;
2145 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2146 LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2147}
2148
2149void CodeGenModule::AddDependentLib(StringRef Lib) {
2150 auto &C = getLLVMContext();
2151 if (getTarget().getTriple().isOSBinFormatELF()) {
2152 ELFDependentLibraries.push_back(
2153 llvm::MDNode::get(C, llvm::MDString::get(C, Lib)));
2154 return;
2155 }
2156
2157 llvm::SmallString<24> Opt;
2158 getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
2159 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2160 LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
2161}
2162
2163/// Add link options implied by the given module, including modules
2164/// it depends on, using a postorder walk.
2165static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
2166 SmallVectorImpl<llvm::MDNode *> &Metadata,
2167 llvm::SmallPtrSet<Module *, 16> &Visited) {
2168 // Import this module's parent.
2169 if (Mod->Parent && Visited.insert(Mod->Parent).second) {
2170 addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
2171 }
2172
2173 // Import this module's dependencies.
2174 for (unsigned I = Mod->Imports.size(); I > 0; --I) {
2175 if (Visited.insert(Mod->Imports[I - 1]).second)
2176 addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited);
2177 }
2178
2179 // Add linker options to link against the libraries/frameworks
2180 // described by this module.
2181 llvm::LLVMContext &Context = CGM.getLLVMContext();
2182 bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF();
2183
2184 // For modules that use export_as for linking, use that module
2185 // name instead.
2186 if (Mod->UseExportAsModuleLinkName)
2187 return;
2188
2189 for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) {
2190 // Link against a framework. Frameworks are currently Darwin only, so we
2191 // don't to ask TargetCodeGenInfo for the spelling of the linker option.
2192 if (Mod->LinkLibraries[I-1].IsFramework) {
2193 llvm::Metadata *Args[2] = {
2194 llvm::MDString::get(Context, "-framework"),
2195 llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library)};
2196
2197 Metadata.push_back(llvm::MDNode::get(Context, Args));
2198 continue;
2199 }
2200
2201 // Link against a library.
2202 if (IsELF) {
2203 llvm::Metadata *Args[2] = {
2204 llvm::MDString::get(Context, "lib"),
2205 llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library),
2206 };
2207 Metadata.push_back(llvm::MDNode::get(Context, Args));
2208 } else {
2209 llvm::SmallString<24> Opt;
2210 CGM.getTargetCodeGenInfo().getDependentLibraryOption(
2211 Mod->LinkLibraries[I - 1].Library, Opt);
2212 auto *OptString = llvm::MDString::get(Context, Opt);
2213 Metadata.push_back(llvm::MDNode::get(Context, OptString));
2214 }
2215 }
2216}
2217
2218void CodeGenModule::EmitModuleLinkOptions() {
2219 // Collect the set of all of the modules we want to visit to emit link
2220 // options, which is essentially the imported modules and all of their
2221 // non-explicit child modules.
2222 llvm::SetVector<clang::Module *> LinkModules;
2223 llvm::SmallPtrSet<clang::Module *, 16> Visited;
2224 SmallVector<clang::Module *, 16> Stack;
2225
2226 // Seed the stack with imported modules.
2227 for (Module *M : ImportedModules) {
2228 // Do not add any link flags when an implementation TU of a module imports
2229 // a header of that same module.
2230 if (M->getTopLevelModuleName() == getLangOpts().CurrentModule &&
2231 !getLangOpts().isCompilingModule())
2232 continue;
2233 if (Visited.insert(M).second)
2234 Stack.push_back(M);
2235 }
2236
2237 // Find all of the modules to import, making a little effort to prune
2238 // non-leaf modules.
2239 while (!Stack.empty()) {
2240 clang::Module *Mod = Stack.pop_back_val();
2241
2242 bool AnyChildren = false;
2243
2244 // Visit the submodules of this module.
2245 for (const auto &SM : Mod->submodules()) {
2246 // Skip explicit children; they need to be explicitly imported to be
2247 // linked against.
2248 if (SM->IsExplicit)
2249 continue;
2250
2251 if (Visited.insert(SM).second) {
2252 Stack.push_back(SM);
2253 AnyChildren = true;
2254 }
2255 }
2256
2257 // We didn't find any children, so add this module to the list of
2258 // modules to link against.
2259 if (!AnyChildren) {
2260 LinkModules.insert(Mod);
2261 }
2262 }
2263
2264 // Add link options for all of the imported modules in reverse topological
2265 // order. We don't do anything to try to order import link flags with respect
2266 // to linker options inserted by things like #pragma comment().
2267 SmallVector<llvm::MDNode *, 16> MetadataArgs;
2268 Visited.clear();
2269 for (Module *M : LinkModules)
2270 if (Visited.insert(M).second)
2271 addLinkOptionsPostorder(*this, M, MetadataArgs, Visited);
2272 std::reverse(MetadataArgs.begin(), MetadataArgs.end());
2273 LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
2274
2275 // Add the linker options metadata flag.
2276 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options");
2277 for (auto *MD : LinkerOptionsMetadata)
2278 NMD->addOperand(MD);
2279}
2280
2281void CodeGenModule::EmitDeferred() {
2282 // Emit deferred declare target declarations.
2283 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
2284 getOpenMPRuntime().emitDeferredTargetDecls();
2285
2286 // Emit code for any potentially referenced deferred decls. Since a
2287 // previously unused static decl may become used during the generation of code
2288 // for a static function, iterate until no changes are made.
2289
2290 if (!DeferredVTables.empty()) {
2291 EmitDeferredVTables();
2292
2293 // Emitting a vtable doesn't directly cause more vtables to
2294 // become deferred, although it can cause functions to be
2295 // emitted that then need those vtables.
2296 assert(DeferredVTables.empty())((DeferredVTables.empty()) ? static_cast<void> (0) : __assert_fail
("DeferredVTables.empty()", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2296, __PRETTY_FUNCTION__))
;
2297 }
2298
2299 // Emit CUDA/HIP static device variables referenced by host code only.
2300 if (getLangOpts().CUDA)
2301 for (auto V : getContext().CUDAStaticDeviceVarReferencedByHost)
2302 DeferredDeclsToEmit.push_back(V);
2303
2304 // Stop if we're out of both deferred vtables and deferred declarations.
2305 if (DeferredDeclsToEmit.empty())
2306 return;
2307
2308 // Grab the list of decls to emit. If EmitGlobalDefinition schedules more
2309 // work, it will not interfere with this.
2310 std::vector<GlobalDecl> CurDeclsToEmit;
2311 CurDeclsToEmit.swap(DeferredDeclsToEmit);
2312
2313 for (GlobalDecl &D : CurDeclsToEmit) {
2314 // We should call GetAddrOfGlobal with IsForDefinition set to true in order
2315 // to get GlobalValue with exactly the type we need, not something that
2316 // might had been created for another decl with the same mangled name but
2317 // different type.
2318 llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
2319 GetAddrOfGlobal(D, ForDefinition));
2320
2321 // In case of different address spaces, we may still get a cast, even with
2322 // IsForDefinition equal to true. Query mangled names table to get
2323 // GlobalValue.
2324 if (!GV)
2325 GV = GetGlobalValue(getMangledName(D));
2326
2327 // Make sure GetGlobalValue returned non-null.
2328 assert(GV)((GV) ? static_cast<void> (0) : __assert_fail ("GV", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2328, __PRETTY_FUNCTION__))
;
2329
2330 // Check to see if we've already emitted this. This is necessary
2331 // for a couple of reasons: first, decls can end up in the
2332 // deferred-decls queue multiple times, and second, decls can end
2333 // up with definitions in unusual ways (e.g. by an extern inline
2334 // function acquiring a strong function redefinition). Just
2335 // ignore these cases.
2336 if (!GV->isDeclaration())
2337 continue;
2338
2339 // If this is OpenMP, check if it is legal to emit this global normally.
2340 if (LangOpts.OpenMP && OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(D))
2341 continue;
2342
2343 // Otherwise, emit the definition and move on to the next one.
2344 EmitGlobalDefinition(D, GV);
2345
2346 // If we found out that we need to emit more decls, do that recursively.
2347 // This has the advantage that the decls are emitted in a DFS and related
2348 // ones are close together, which is convenient for testing.
2349 if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) {
2350 EmitDeferred();
2351 assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty())((DeferredVTables.empty() && DeferredDeclsToEmit.empty
()) ? static_cast<void> (0) : __assert_fail ("DeferredVTables.empty() && DeferredDeclsToEmit.empty()"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2351, __PRETTY_FUNCTION__))
;
2352 }
2353 }
2354}
2355
2356void CodeGenModule::EmitVTablesOpportunistically() {
2357 // Try to emit external vtables as available_externally if they have emitted
2358 // all inlined virtual functions. It runs after EmitDeferred() and therefore
2359 // is not allowed to create new references to things that need to be emitted
2360 // lazily. Note that it also uses fact that we eagerly emitting RTTI.
2361
2362 assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables())(((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables
()) && "Only emit opportunistic vtables with optimizations"
) ? static_cast<void> (0) : __assert_fail ("(OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables()) && \"Only emit opportunistic vtables with optimizations\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2363, __PRETTY_FUNCTION__))
2363 && "Only emit opportunistic vtables with optimizations")(((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables
()) && "Only emit opportunistic vtables with optimizations"
) ? static_cast<void> (0) : __assert_fail ("(OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables()) && \"Only emit opportunistic vtables with optimizations\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2363, __PRETTY_FUNCTION__))
;
2364
2365 for (const CXXRecordDecl *RD : OpportunisticVTables) {
2366 assert(getVTables().isVTableExternal(RD) &&((getVTables().isVTableExternal(RD) && "This queue should only contain external vtables"
) ? static_cast<void> (0) : __assert_fail ("getVTables().isVTableExternal(RD) && \"This queue should only contain external vtables\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2367, __PRETTY_FUNCTION__))
2367 "This queue should only contain external vtables")((getVTables().isVTableExternal(RD) && "This queue should only contain external vtables"
) ? static_cast<void> (0) : __assert_fail ("getVTables().isVTableExternal(RD) && \"This queue should only contain external vtables\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2367, __PRETTY_FUNCTION__))
;
2368 if (getCXXABI().canSpeculativelyEmitVTable(RD))
2369 VTables.GenerateClassData(RD);
2370 }
2371 OpportunisticVTables.clear();
2372}
2373
2374void CodeGenModule::EmitGlobalAnnotations() {
2375 if (Annotations.empty())
2376 return;
2377
2378 // Create a new global variable for the ConstantStruct in the Module.
2379 llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
2380 Annotations[0]->getType(), Annotations.size()), Annotations);
2381 auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
2382 llvm::GlobalValue::AppendingLinkage,
2383 Array, "llvm.global.annotations");
2384 gv->setSection(AnnotationSection);
2385}
2386
2387llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
2388 llvm::Constant *&AStr = AnnotationStrings[Str];
2389 if (AStr)
2390 return AStr;
2391
2392 // Not found yet, create a new global.
2393 llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
2394 auto *gv =
2395 new llvm::GlobalVariable(getModule(), s->getType(), true,
2396 llvm::GlobalValue::PrivateLinkage, s, ".str");
2397 gv->setSection(AnnotationSection);
2398 gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2399 AStr = gv;
2400 return gv;
2401}
2402
2403llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) {
2404 SourceManager &SM = getContext().getSourceManager();
2405 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
2406 if (PLoc.isValid())
2407 return EmitAnnotationString(PLoc.getFilename());
2408 return EmitAnnotationString(SM.getBufferName(Loc));
2409}
2410
2411llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) {
2412 SourceManager &SM = getContext().getSourceManager();
2413 PresumedLoc PLoc = SM.getPresumedLoc(L);
2414 unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
2415 SM.getExpansionLineNumber(L);
2416 return llvm::ConstantInt::get(Int32Ty, LineNo);
2417}
2418
2419llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) {
2420 ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()};
2421 if (Exprs.empty())
2422 return llvm::ConstantPointerNull::get(Int8PtrTy);
2423
2424 llvm::FoldingSetNodeID ID;
2425 for (Expr *E : Exprs) {
2426 ID.Add(cast<clang::ConstantExpr>(E)->getAPValueResult());
2427 }
2428 llvm::Constant *&Lookup = AnnotationArgs[ID.ComputeHash()];
2429 if (Lookup)
2430 return Lookup;
2431
2432 llvm::SmallVector<llvm::Constant *, 4> LLVMArgs;
2433 LLVMArgs.reserve(Exprs.size());
2434 ConstantEmitter ConstEmiter(*this);
2435 llvm::transform(Exprs, std::back_inserter(LLVMArgs), [&](const Expr *E) {
2436 const auto *CE = cast<clang::ConstantExpr>(E);
2437 return ConstEmiter.emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(),
2438 CE->getType());
2439 });
2440 auto *Struct = llvm::ConstantStruct::getAnon(LLVMArgs);
2441 auto *GV = new llvm::GlobalVariable(getModule(), Struct->getType(), true,
2442 llvm::GlobalValue::PrivateLinkage, Struct,
2443 ".args");
2444 GV->setSection(AnnotationSection);
2445 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2446 auto *Bitcasted = llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
2447
2448 Lookup = Bitcasted;
2449 return Bitcasted;
2450}
2451
2452llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
2453 const AnnotateAttr *AA,
2454 SourceLocation L) {
2455 // Get the globals for file name, annotation, and the line number.
2456 llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
2457 *UnitGV = EmitAnnotationUnit(L),
2458 *LineNoCst = EmitAnnotationLineNo(L),
2459 *Args = EmitAnnotationArgs(AA);
2460
2461 llvm::Constant *ASZeroGV = GV;
2462 if (GV->getAddressSpace() != 0) {
2463 ASZeroGV = llvm::ConstantExpr::getAddrSpaceCast(
2464 GV, GV->getValueType()->getPointerTo(0));
2465 }
2466
2467 // Create the ConstantStruct for the global annotation.
2468 llvm::Constant *Fields[] = {
2469 llvm::ConstantExpr::getBitCast(ASZeroGV, Int8PtrTy),
2470 llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy),
2471 llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy),
2472 LineNoCst,
2473 Args,
2474 };
2475 return llvm::ConstantStruct::getAnon(Fields);
2476}
2477
2478void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D,
2479 llvm::GlobalValue *GV) {
2480 assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute")((D->hasAttr<AnnotateAttr>() && "no annotate attribute"
) ? static_cast<void> (0) : __assert_fail ("D->hasAttr<AnnotateAttr>() && \"no annotate attribute\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2480, __PRETTY_FUNCTION__))
;
2481 // Get the struct elements for these annotations.
2482 for (const auto *I : D->specific_attrs<AnnotateAttr>())
2483 Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
2484}
2485
2486bool CodeGenModule::isInSanitizerBlacklist(SanitizerMask Kind,
2487 llvm::Function *Fn,
2488 SourceLocation Loc) const {
2489 const auto &SanitizerBL = getContext().getSanitizerBlacklist();
2490 // Blacklist by function name.
2491 if (SanitizerBL.isBlacklistedFunction(Kind, Fn->getName()))
2492 return true;
2493 // Blacklist by location.
2494 if (Loc.isValid())
2495 return SanitizerBL.isBlacklistedLocation(Kind, Loc);
2496 // If location is unknown, this may be a compiler-generated function. Assume
2497 // it's located in the main file.
2498 auto &SM = Context.getSourceManager();
2499 if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
2500 return SanitizerBL.isBlacklistedFile(Kind, MainFile->getName());
2501 }
2502 return false;
2503}
2504
2505bool CodeGenModule::isInSanitizerBlacklist(llvm::GlobalVariable *GV,
2506 SourceLocation Loc, QualType Ty,
2507 StringRef Category) const {
2508 // For now globals can be blacklisted only in ASan and KASan.
2509 const SanitizerMask EnabledAsanMask =
2510 LangOpts.Sanitize.Mask &
2511 (SanitizerKind::Address | SanitizerKind::KernelAddress |
2512 SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress |
2513 SanitizerKind::MemTag);
2514 if (!EnabledAsanMask)
2515 return false;
2516 const auto &SanitizerBL = getContext().getSanitizerBlacklist();
2517 if (SanitizerBL.isBlacklistedGlobal(EnabledAsanMask, GV->getName(), Category))
2518 return true;
2519 if (SanitizerBL.isBlacklistedLocation(EnabledAsanMask, Loc, Category))
2520 return true;
2521 // Check global type.
2522 if (!Ty.isNull()) {
2523 // Drill down the array types: if global variable of a fixed type is
2524 // blacklisted, we also don't instrument arrays of them.
2525 while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr()))
2526 Ty = AT->getElementType();
2527 Ty = Ty.getCanonicalType().getUnqualifiedType();
2528 // We allow to blacklist only record types (classes, structs etc.)
2529 if (Ty->isRecordType()) {
2530 std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy());
2531 if (SanitizerBL.isBlacklistedType(EnabledAsanMask, TypeStr, Category))
2532 return true;
2533 }
2534 }
2535 return false;
2536}
2537
2538bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
2539 StringRef Category) const {
2540 const auto &XRayFilter = getContext().getXRayFilter();
2541 using ImbueAttr = XRayFunctionFilter::ImbueAttribute;
2542 auto Attr = ImbueAttr::NONE;
2543 if (Loc.isValid())
2544 Attr = XRayFilter.shouldImbueLocation(Loc, Category);
2545 if (Attr == ImbueAttr::NONE)
2546 Attr = XRayFilter.shouldImbueFunction(Fn->getName());
2547 switch (Attr) {
2548 case ImbueAttr::NONE:
2549 return false;
2550 case ImbueAttr::ALWAYS:
2551 Fn->addFnAttr("function-instrument", "xray-always");
2552 break;
2553 case ImbueAttr::ALWAYS_ARG1:
2554 Fn->addFnAttr("function-instrument", "xray-always");
2555 Fn->addFnAttr("xray-log-args", "1");
2556 break;
2557 case ImbueAttr::NEVER:
2558 Fn->addFnAttr("function-instrument", "xray-never");
2559 break;
2560 }
2561 return true;
2562}
2563
2564bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) {
2565 // Never defer when EmitAllDecls is specified.
2566 if (LangOpts.EmitAllDecls)
2567 return true;
2568
2569 if (CodeGenOpts.KeepStaticConsts) {
2570 const auto *VD = dyn_cast<VarDecl>(Global);
2571 if (VD && VD->getType().isConstQualified() &&
2572 VD->getStorageDuration() == SD_Static)
2573 return true;
2574 }
2575
2576 return getContext().DeclMustBeEmitted(Global);
2577}
2578
2579bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
2580 if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
2581 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
2582 // Implicit template instantiations may change linkage if they are later
2583 // explicitly instantiated, so they should not be emitted eagerly.
2584 return false;
2585 // In OpenMP 5.0 function may be marked as device_type(nohost) and we should
2586 // not emit them eagerly unless we sure that the function must be emitted on
2587 // the host.
2588 if (LangOpts.OpenMP >= 50 && !LangOpts.OpenMPSimd &&
2589 !LangOpts.OpenMPIsDevice &&
2590 !OMPDeclareTargetDeclAttr::getDeviceType(FD) &&
2591 !FD->isUsed(/*CheckUsedAttr=*/false) && !FD->isReferenced())
2592 return false;
2593 }
2594 if (const auto *VD = dyn_cast<VarDecl>(Global))
2595 if (Context.getInlineVariableDefinitionKind(VD) ==
2596 ASTContext::InlineVariableDefinitionKind::WeakUnknown)
2597 // A definition of an inline constexpr static data member may change
2598 // linkage later if it's redeclared outside the class.
2599 return false;
2600 // If OpenMP is enabled and threadprivates must be generated like TLS, delay
2601 // codegen for global variables, because they may be marked as threadprivate.
2602 if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
2603 getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) &&
2604 !isTypeConstant(Global->getType(), false) &&
2605 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global))
2606 return false;
2607
2608 return true;
2609}
2610
2611ConstantAddress CodeGenModule::GetAddrOfMSGuidDecl(const MSGuidDecl *GD) {
2612 StringRef Name = getMangledName(GD);
2613
2614 // The UUID descriptor should be pointer aligned.
2615 CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes);
2616
2617 // Look for an existing global.
2618 if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
2619 return ConstantAddress(GV, Alignment);
2620
2621 ConstantEmitter Emitter(*this);
2622 llvm::Constant *Init;
2623
2624 APValue &V = GD->getAsAPValue();
2625 if (!V.isAbsent()) {
2626 // If possible, emit the APValue version of the initializer. In particular,
2627 // this gets the type of the constant right.
2628 Init = Emitter.emitForInitializer(
2629 GD->getAsAPValue(), GD->getType().getAddressSpace(), GD->getType());
2630 } else {
2631 // As a fallback, directly construct the constant.
2632 // FIXME: This may get padding wrong under esoteric struct layout rules.
2633 // MSVC appears to create a complete type 'struct __s_GUID' that it
2634 // presumably uses to represent these constants.
2635 MSGuidDecl::Parts Parts = GD->getParts();
2636 llvm::Constant *Fields[4] = {
2637 llvm::ConstantInt::get(Int32Ty, Parts.Part1),
2638 llvm::ConstantInt::get(Int16Ty, Parts.Part2),
2639 llvm::ConstantInt::get(Int16Ty, Parts.Part3),
2640 llvm::ConstantDataArray::getRaw(
2641 StringRef(reinterpret_cast<char *>(Parts.Part4And5), 8), 8,
2642 Int8Ty)};
2643 Init = llvm::ConstantStruct::getAnon(Fields);
2644 }
2645
2646 auto *GV = new llvm::GlobalVariable(
2647 getModule(), Init->getType(),
2648 /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
2649 if (supportsCOMDAT())
2650 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
2651 setDSOLocal(GV);
2652
2653 llvm::Constant *Addr = GV;
2654 if (!V.isAbsent()) {
2655 Emitter.finalize(GV);
2656 } else {
2657 llvm::Type *Ty = getTypes().ConvertTypeForMem(GD->getType());
2658 Addr = llvm::ConstantExpr::getBitCast(
2659 GV, Ty->getPointerTo(GV->getAddressSpace()));
2660 }
2661 return ConstantAddress(Addr, Alignment);
2662}
2663
2664ConstantAddress CodeGenModule::GetAddrOfTemplateParamObject(
2665 const TemplateParamObjectDecl *TPO) {
2666 StringRef Name = getMangledName(TPO);
2667 CharUnits Alignment = getNaturalTypeAlignment(TPO->getType());
2668
2669 if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
2670 return ConstantAddress(GV, Alignment);
2671
2672 ConstantEmitter Emitter(*this);
2673 llvm::Constant *Init = Emitter.emitForInitializer(
2674 TPO->getValue(), TPO->getType().getAddressSpace(), TPO->getType());
2675
2676 if (!Init) {
2677 ErrorUnsupported(TPO, "template parameter object");
2678 return ConstantAddress::invalid();
2679 }
2680
2681 auto *GV = new llvm::GlobalVariable(
2682 getModule(), Init->getType(),
2683 /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
2684 if (supportsCOMDAT())
2685 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
2686 Emitter.finalize(GV);
2687
2688 return ConstantAddress(GV, Alignment);
2689}
2690
2691ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
2692 const AliasAttr *AA = VD->getAttr<AliasAttr>();
2693 assert(AA && "No alias?")((AA && "No alias?") ? static_cast<void> (0) : __assert_fail
("AA && \"No alias?\"", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2693, __PRETTY_FUNCTION__))
;
2694
2695 CharUnits Alignment = getContext().getDeclAlign(VD);
2696 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
2697
2698 // See if there is already something with the target's name in the module.
2699 llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
2700 if (Entry) {
2701 unsigned AS = getContext().getTargetAddressSpace(VD->getType());
2702 auto Ptr = llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS));
2703 return ConstantAddress(Ptr, Alignment);
2704 }
2705
2706 llvm::Constant *Aliasee;
2707 if (isa<llvm::FunctionType>(DeclTy))
2708 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
2709 GlobalDecl(cast<FunctionDecl>(VD)),
2710 /*ForVTable=*/false);
2711 else
2712 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
2713 llvm::PointerType::getUnqual(DeclTy),
2714 nullptr);
2715
2716 auto *F = cast<llvm::GlobalValue>(Aliasee);
2717 F->setLinkage(llvm::Function::ExternalWeakLinkage);
2718 WeakRefReferences.insert(F);
2719
2720 return ConstantAddress(Aliasee, Alignment);
2721}
2722
2723void CodeGenModule::EmitGlobal(GlobalDecl GD) {
2724 const auto *Global = cast<ValueDecl>(GD.getDecl());
2725
2726 // Weak references don't produce any output by themselves.
2727 if (Global->hasAttr<WeakRefAttr>())
2728 return;
2729
2730 // If this is an alias definition (which otherwise looks like a declaration)
2731 // emit it now.
2732 if (Global->hasAttr<AliasAttr>())
2733 return EmitAliasDefinition(GD);
2734
2735 // IFunc like an alias whose value is resolved at runtime by calling resolver.
2736 if (Global->hasAttr<IFuncAttr>())
2737 return emitIFuncDefinition(GD);
2738
2739 // If this is a cpu_dispatch multiversion function, emit the resolver.
2740 if (Global->hasAttr<CPUDispatchAttr>())
2741 return emitCPUDispatchDefinition(GD);
2742
2743 // If this is CUDA, be selective about which declarations we emit.
2744 if (LangOpts.CUDA) {
2745 if (LangOpts.CUDAIsDevice) {
2746 if (!Global->hasAttr<CUDADeviceAttr>() &&
2747 !Global->hasAttr<CUDAGlobalAttr>() &&
2748 !Global->hasAttr<CUDAConstantAttr>() &&
2749 !Global->hasAttr<CUDASharedAttr>() &&
2750 !Global->getType()->isCUDADeviceBuiltinSurfaceType() &&
2751 !Global->getType()->isCUDADeviceBuiltinTextureType())
2752 return;
2753 } else {
2754 // We need to emit host-side 'shadows' for all global
2755 // device-side variables because the CUDA runtime needs their
2756 // size and host-side address in order to provide access to
2757 // their device-side incarnations.
2758
2759 // So device-only functions are the only things we skip.
2760 if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() &&
2761 Global->hasAttr<CUDADeviceAttr>())
2762 return;
2763
2764 assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) &&(((isa<FunctionDecl>(Global) || isa<VarDecl>(Global
)) && "Expected Variable or Function") ? static_cast<
void> (0) : __assert_fail ("(isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) && \"Expected Variable or Function\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2765, __PRETTY_FUNCTION__))
2765 "Expected Variable or Function")(((isa<FunctionDecl>(Global) || isa<VarDecl>(Global
)) && "Expected Variable or Function") ? static_cast<
void> (0) : __assert_fail ("(isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) && \"Expected Variable or Function\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2765, __PRETTY_FUNCTION__))
;
2766 }
2767 }
2768
2769 if (LangOpts.OpenMP) {
2770 // If this is OpenMP, check if it is legal to emit this global normally.
2771 if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
2772 return;
2773 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) {
2774 if (MustBeEmitted(Global))
2775 EmitOMPDeclareReduction(DRD);
2776 return;
2777 } else if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) {
2778 if (MustBeEmitted(Global))
2779 EmitOMPDeclareMapper(DMD);
2780 return;
2781 }
2782 }
2783
2784 // Ignore declarations, they will be emitted on their first use.
2785 if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
2786 // Forward declarations are emitted lazily on first use.
2787 if (!FD->doesThisDeclarationHaveABody()) {
2788 if (!FD->doesDeclarationForceExternallyVisibleDefinition())
2789 return;
2790
2791 StringRef MangledName = getMangledName(GD);
2792
2793 // Compute the function info and LLVM type.
2794 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
2795 llvm::Type *Ty = getTypes().GetFunctionType(FI);
2796
2797 GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
2798 /*DontDefer=*/false);
2799 return;
2800 }
2801 } else {
2802 const auto *VD = cast<VarDecl>(Global);
2803 assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.")((VD->isFileVarDecl() && "Cannot emit local var decl as global."
) ? static_cast<void> (0) : __assert_fail ("VD->isFileVarDecl() && \"Cannot emit local var decl as global.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2803, __PRETTY_FUNCTION__))
;
2804 if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
2805 !Context.isMSStaticDataMemberInlineDefinition(VD)) {
2806 if (LangOpts.OpenMP) {
2807 // Emit declaration of the must-be-emitted declare target variable.
2808 if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2809 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
2810 bool UnifiedMemoryEnabled =
2811 getOpenMPRuntime().hasRequiresUnifiedSharedMemory();
2812 if (*Res == OMPDeclareTargetDeclAttr::MT_To &&
2813 !UnifiedMemoryEnabled) {
2814 (void)GetAddrOfGlobalVar(VD);
2815 } else {
2816 assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||((((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr
::MT_To && UnifiedMemoryEnabled)) && "Link clause or to clause with unified memory expected."
) ? static_cast<void> (0) : __assert_fail ("((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To && UnifiedMemoryEnabled)) && \"Link clause or to clause with unified memory expected.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2819, __PRETTY_FUNCTION__))
2817 (*Res == OMPDeclareTargetDeclAttr::MT_To &&((((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr
::MT_To && UnifiedMemoryEnabled)) && "Link clause or to clause with unified memory expected."
) ? static_cast<void> (0) : __assert_fail ("((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To && UnifiedMemoryEnabled)) && \"Link clause or to clause with unified memory expected.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2819, __PRETTY_FUNCTION__))
2818 UnifiedMemoryEnabled)) &&((((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr
::MT_To && UnifiedMemoryEnabled)) && "Link clause or to clause with unified memory expected."
) ? static_cast<void> (0) : __assert_fail ("((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To && UnifiedMemoryEnabled)) && \"Link clause or to clause with unified memory expected.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2819, __PRETTY_FUNCTION__))
2819 "Link clause or to clause with unified memory expected.")((((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr
::MT_To && UnifiedMemoryEnabled)) && "Link clause or to clause with unified memory expected."
) ? static_cast<void> (0) : __assert_fail ("((*Res == OMPDeclareTargetDeclAttr::MT_Link) || (*Res == OMPDeclareTargetDeclAttr::MT_To && UnifiedMemoryEnabled)) && \"Link clause or to clause with unified memory expected.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2819, __PRETTY_FUNCTION__))
;
2820 (void)getOpenMPRuntime().getAddrOfDeclareTargetVar(VD);
2821 }
2822
2823 return;
2824 }
2825 }
2826 // If this declaration may have caused an inline variable definition to
2827 // change linkage, make sure that it's emitted.
2828 if (Context.getInlineVariableDefinitionKind(VD) ==
2829 ASTContext::InlineVariableDefinitionKind::Strong)
2830 GetAddrOfGlobalVar(VD);
2831 return;
2832 }
2833 }
2834
2835 // Defer code generation to first use when possible, e.g. if this is an inline
2836 // function. If the global must always be emitted, do it eagerly if possible
2837 // to benefit from cache locality.
2838 if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) {
2839 // Emit the definition if it can't be deferred.
2840 EmitGlobalDefinition(GD);
2841 return;
2842 }
2843
2844 // If we're deferring emission of a C++ variable with an
2845 // initializer, remember the order in which it appeared in the file.
2846 if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
2847 cast<VarDecl>(Global)->hasInit()) {
2848 DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
2849 CXXGlobalInits.push_back(nullptr);
2850 }
2851
2852 StringRef MangledName = getMangledName(GD);
2853 if (GetGlobalValue(MangledName) != nullptr) {
2854 // The value has already been used and should therefore be emitted.
2855 addDeferredDeclToEmit(GD);
2856 } else if (MustBeEmitted(Global)) {
2857 // The value must be emitted, but cannot be emitted eagerly.
2858 assert(!MayBeEmittedEagerly(Global))((!MayBeEmittedEagerly(Global)) ? static_cast<void> (0)
: __assert_fail ("!MayBeEmittedEagerly(Global)", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 2858, __PRETTY_FUNCTION__))
;
2859 addDeferredDeclToEmit(GD);
2860 } else {
2861 // Otherwise, remember that we saw a deferred decl with this name. The
2862 // first use of the mangled name will cause it to move into
2863 // DeferredDeclsToEmit.
2864 DeferredDecls[MangledName] = GD;
2865 }
2866}
2867
2868// Check if T is a class type with a destructor that's not dllimport.
2869static bool HasNonDllImportDtor(QualType T) {
2870 if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>())
2871 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
2872 if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
2873 return true;
2874
2875 return false;
2876}
2877
2878namespace {
2879 struct FunctionIsDirectlyRecursive
2880 : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> {
2881 const StringRef Name;
2882 const Builtin::Context &BI;
2883 FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C)
2884 : Name(N), BI(C) {}
2885
2886 bool VisitCallExpr(const CallExpr *E) {
2887 const FunctionDecl *FD = E->getDirectCallee();
2888 if (!FD)
2889 return false;
2890 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
2891 if (Attr && Name == Attr->getLabel())
2892 return true;
2893 unsigned BuiltinID = FD->getBuiltinID();
2894 if (!BuiltinID || !BI.isLibFunction(BuiltinID))
2895 return false;
2896 StringRef BuiltinName = BI.getName(BuiltinID);
2897 if (BuiltinName.startswith("__builtin_") &&
2898 Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
2899 return true;
2900 }
2901 return false;
2902 }
2903
2904 bool VisitStmt(const Stmt *S) {
2905 for (const Stmt *Child : S->children())
2906 if (Child && this->Visit(Child))
2907 return true;
2908 return false;
2909 }
2910 };
2911
2912 // Make sure we're not referencing non-imported vars or functions.
2913 struct DLLImportFunctionVisitor
2914 : public RecursiveASTVisitor<DLLImportFunctionVisitor> {
2915 bool SafeToInline = true;
2916
2917 bool shouldVisitImplicitCode() const { return true; }
2918
2919 bool VisitVarDecl(VarDecl *VD) {
2920 if (VD->getTLSKind()) {
2921 // A thread-local variable cannot be imported.
2922 SafeToInline = false;
2923 return SafeToInline;
2924 }
2925
2926 // A variable definition might imply a destructor call.
2927 if (VD->isThisDeclarationADefinition())
2928 SafeToInline = !HasNonDllImportDtor(VD->getType());
2929
2930 return SafeToInline;
2931 }
2932
2933 bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2934 if (const auto *D = E->getTemporary()->getDestructor())
2935 SafeToInline = D->hasAttr<DLLImportAttr>();
2936 return SafeToInline;
2937 }
2938
2939 bool VisitDeclRefExpr(DeclRefExpr *E) {
2940 ValueDecl *VD = E->getDecl();
2941 if (isa<FunctionDecl>(VD))
2942 SafeToInline = VD->hasAttr<DLLImportAttr>();
2943 else if (VarDecl *V = dyn_cast<VarDecl>(VD))
2944 SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>();
2945 return SafeToInline;
2946 }
2947
2948 bool VisitCXXConstructExpr(CXXConstructExpr *E) {
2949 SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>();
2950 return SafeToInline;
2951 }
2952
2953 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
2954 CXXMethodDecl *M = E->getMethodDecl();
2955 if (!M) {
2956 // Call through a pointer to member function. This is safe to inline.
2957 SafeToInline = true;
2958 } else {
2959 SafeToInline = M->hasAttr<DLLImportAttr>();
2960 }
2961 return SafeToInline;
2962 }
2963
2964 bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2965 SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
2966 return SafeToInline;
2967 }
2968
2969 bool VisitCXXNewExpr(CXXNewExpr *E) {
2970 SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>();
2971 return SafeToInline;
2972 }
2973 };
2974}
2975
2976// isTriviallyRecursive - Check if this function calls another
2977// decl that, because of the asm attribute or the other decl being a builtin,
2978// ends up pointing to itself.
2979bool
2980CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
2981 StringRef Name;
2982 if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
2983 // asm labels are a special kind of mangling we have to support.
2984 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
2985 if (!Attr)
2986 return false;
2987 Name = Attr->getLabel();
2988 } else {
2989 Name = FD->getName();
2990 }
2991
2992 FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
2993 const Stmt *Body = FD->getBody();
2994 return Body ? Walker.Visit(Body) : false;
2995}
2996
2997bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
2998 if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
2999 return true;
3000 const auto *F = cast<FunctionDecl>(GD.getDecl());
3001 if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
3002 return false;
3003
3004 if (F->hasAttr<DLLImportAttr>()) {
3005 // Check whether it would be safe to inline this dllimport function.
3006 DLLImportFunctionVisitor Visitor;
3007 Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F));
3008 if (!Visitor.SafeToInline)
3009 return false;
3010
3011 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) {
3012 // Implicit destructor invocations aren't captured in the AST, so the
3013 // check above can't see them. Check for them manually here.
3014 for (const Decl *Member : Dtor->getParent()->decls())
3015 if (isa<FieldDecl>(Member))
3016 if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType()))
3017 return false;
3018 for (const CXXBaseSpecifier &B : Dtor->getParent()->bases())
3019 if (HasNonDllImportDtor(B.getType()))
3020 return false;
3021 }
3022 }
3023
3024 // PR9614. Avoid cases where the source code is lying to us. An available
3025 // externally function should have an equivalent function somewhere else,
3026 // but a function that calls itself through asm label/`__builtin_` trickery is
3027 // clearly not equivalent to the real implementation.
3028 // This happens in glibc's btowc and in some configure checks.
3029 return !isTriviallyRecursive(F);
3030}
3031
3032bool CodeGenModule::shouldOpportunisticallyEmitVTables() {
3033 return CodeGenOpts.OptimizationLevel > 0;
3034}
3035
3036void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD,
3037 llvm::GlobalValue *GV) {
3038 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3039
3040 if (FD->isCPUSpecificMultiVersion()) {
3041 auto *Spec = FD->getAttr<CPUSpecificAttr>();
3042 for (unsigned I = 0; I < Spec->cpus_size(); ++I)
3043 EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
3044 // Requires multiple emits.
3045 } else
3046 EmitGlobalFunctionDefinition(GD, GV);
3047}
3048
3049void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
3050 const auto *D = cast<ValueDecl>(GD.getDecl());
3051
3052 PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
3053 Context.getSourceManager(),
3054 "Generating code for declaration");
3055
3056 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3057 // At -O0, don't generate IR for functions with available_externally
3058 // linkage.
3059 if (!shouldEmitFunction(GD))
3060 return;
3061
3062 llvm::TimeTraceScope TimeScope("CodeGen Function", [&]() {
3063 std::string Name;
3064 llvm::raw_string_ostream OS(Name);
3065 FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(),
3066 /*Qualified=*/true);
3067 return Name;
3068 });
3069
3070 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
3071 // Make sure to emit the definition(s) before we emit the thunks.
3072 // This is necessary for the generation of certain thunks.
3073 if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method))
3074 ABI->emitCXXStructor(GD);
3075 else if (FD->isMultiVersion())
3076 EmitMultiVersionFunctionDefinition(GD, GV);
3077 else
3078 EmitGlobalFunctionDefinition(GD, GV);
3079
3080 if (Method->isVirtual())
3081 getVTables().EmitThunks(GD);
3082
3083 return;
3084 }
3085
3086 if (FD->isMultiVersion())
3087 return EmitMultiVersionFunctionDefinition(GD, GV);
3088 return EmitGlobalFunctionDefinition(GD, GV);
3089 }
3090
3091 if (const auto *VD = dyn_cast<VarDecl>(D))
3092 return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
3093
3094 llvm_unreachable("Invalid argument to EmitGlobalDefinition()")::llvm::llvm_unreachable_internal("Invalid argument to EmitGlobalDefinition()"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3094)
;
3095}
3096
3097static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
3098 llvm::Function *NewFn);
3099
3100static unsigned
3101TargetMVPriority(const TargetInfo &TI,
3102 const CodeGenFunction::MultiVersionResolverOption &RO) {
3103 unsigned Priority = 0;
3104 for (StringRef Feat : RO.Conditions.Features)
3105 Priority = std::max(Priority, TI.multiVersionSortPriority(Feat));
3106
3107 if (!RO.Conditions.Architecture.empty())
3108 Priority = std::max(
3109 Priority, TI.multiVersionSortPriority(RO.Conditions.Architecture));
3110 return Priority;
3111}
3112
3113void CodeGenModule::emitMultiVersionFunctions() {
3114 for (GlobalDecl GD : MultiVersionFuncs) {
3115 SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options;
3116 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
3117 getContext().forEachMultiversionedFunctionVersion(
3118 FD, [this, &GD, &Options](const FunctionDecl *CurFD) {
3119 GlobalDecl CurGD{
3120 (CurFD->isDefined() ? CurFD->getDefinition() : CurFD)};
3121 StringRef MangledName = getMangledName(CurGD);
3122 llvm::Constant *Func = GetGlobalValue(MangledName);
3123 if (!Func) {
3124 if (CurFD->isDefined()) {
3125 EmitGlobalFunctionDefinition(CurGD, nullptr);
3126 Func = GetGlobalValue(MangledName);
3127 } else {
3128 const CGFunctionInfo &FI =
3129 getTypes().arrangeGlobalDeclaration(GD);
3130 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
3131 Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
3132 /*DontDefer=*/false, ForDefinition);
3133 }
3134 assert(Func && "This should have just been created")((Func && "This should have just been created") ? static_cast
<void> (0) : __assert_fail ("Func && \"This should have just been created\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3134, __PRETTY_FUNCTION__))
;
3135 }
3136
3137 const auto *TA = CurFD->getAttr<TargetAttr>();
3138 llvm::SmallVector<StringRef, 8> Feats;
3139 TA->getAddedFeatures(Feats);
3140
3141 Options.emplace_back(cast<llvm::Function>(Func),
3142 TA->getArchitecture(), Feats);
3143 });
3144
3145 llvm::Function *ResolverFunc;
3146 const TargetInfo &TI = getTarget();
3147
3148 if (TI.supportsIFunc() || FD->isTargetMultiVersion()) {
3149 ResolverFunc = cast<llvm::Function>(
3150 GetGlobalValue((getMangledName(GD) + ".resolver").str()));
3151 ResolverFunc->setLinkage(llvm::Function::WeakODRLinkage);
3152 } else {
3153 ResolverFunc = cast<llvm::Function>(GetGlobalValue(getMangledName(GD)));
3154 }
3155
3156 if (supportsCOMDAT())
3157 ResolverFunc->setComdat(
3158 getModule().getOrInsertComdat(ResolverFunc->getName()));
3159
3160 llvm::stable_sort(
3161 Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS,
3162 const CodeGenFunction::MultiVersionResolverOption &RHS) {
3163 return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS);
3164 });
3165 CodeGenFunction CGF(*this);
3166 CGF.EmitMultiVersionResolver(ResolverFunc, Options);
3167 }
3168}
3169
3170void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
3171 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3172 assert(FD && "Not a FunctionDecl?")((FD && "Not a FunctionDecl?") ? static_cast<void>
(0) : __assert_fail ("FD && \"Not a FunctionDecl?\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3172, __PRETTY_FUNCTION__))
;
3173 const auto *DD = FD->getAttr<CPUDispatchAttr>();
3174 assert(DD && "Not a cpu_dispatch Function?")((DD && "Not a cpu_dispatch Function?") ? static_cast
<void> (0) : __assert_fail ("DD && \"Not a cpu_dispatch Function?\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3174, __PRETTY_FUNCTION__))
;
3175 llvm::Type *DeclTy = getTypes().ConvertType(FD->getType());
3176
3177 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) {
3178 const CGFunctionInfo &FInfo = getTypes().arrangeCXXMethodDeclaration(CXXFD);
3179 DeclTy = getTypes().GetFunctionType(FInfo);
3180 }
3181
3182 StringRef ResolverName = getMangledName(GD);
3183
3184 llvm::Type *ResolverType;
3185 GlobalDecl ResolverGD;
3186 if (getTarget().supportsIFunc())
3187 ResolverType = llvm::FunctionType::get(
3188 llvm::PointerType::get(DeclTy,
3189 Context.getTargetAddressSpace(FD->getType())),
3190 false);
3191 else {
3192 ResolverType = DeclTy;
3193 ResolverGD = GD;
3194 }
3195
3196 auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction(
3197 ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false));
3198 ResolverFunc->setLinkage(llvm::Function::WeakODRLinkage);
3199 if (supportsCOMDAT())
3200 ResolverFunc->setComdat(
3201 getModule().getOrInsertComdat(ResolverFunc->getName()));
3202
3203 SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options;
3204 const TargetInfo &Target = getTarget();
3205 unsigned Index = 0;
3206 for (const IdentifierInfo *II : DD->cpus()) {
3207 // Get the name of the target function so we can look it up/create it.
3208 std::string MangledName = getMangledNameImpl(*this, GD, FD, true) +
3209 getCPUSpecificMangling(*this, II->getName());
3210
3211 llvm::Constant *Func = GetGlobalValue(MangledName);
3212
3213 if (!Func) {
3214 GlobalDecl ExistingDecl = Manglings.lookup(MangledName);
3215 if (ExistingDecl.getDecl() &&
3216 ExistingDecl.getDecl()->getAsFunction()->isDefined()) {
3217 EmitGlobalFunctionDefinition(ExistingDecl, nullptr);
3218 Func = GetGlobalValue(MangledName);
3219 } else {
3220 if (!ExistingDecl.getDecl())
3221 ExistingDecl = GD.getWithMultiVersionIndex(Index);
3222
3223 Func = GetOrCreateLLVMFunction(
3224 MangledName, DeclTy, ExistingDecl,
3225 /*ForVTable=*/false, /*DontDefer=*/true,
3226 /*IsThunk=*/false, llvm::AttributeList(), ForDefinition);
3227 }
3228 }
3229
3230 llvm::SmallVector<StringRef, 32> Features;
3231 Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features);
3232 llvm::transform(Features, Features.begin(),
3233 [](StringRef Str) { return Str.substr(1); });
3234 Features.erase(std::remove_if(
3235 Features.begin(), Features.end(), [&Target](StringRef Feat) {
3236 return !Target.validateCpuSupports(Feat);
3237 }), Features.end());
3238 Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features);
3239 ++Index;
3240 }
3241
3242 llvm::sort(
3243 Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS,
3244 const CodeGenFunction::MultiVersionResolverOption &RHS) {
3245 return CodeGenFunction::GetX86CpuSupportsMask(LHS.Conditions.Features) >
3246 CodeGenFunction::GetX86CpuSupportsMask(RHS.Conditions.Features);
3247 });
3248
3249 // If the list contains multiple 'default' versions, such as when it contains
3250 // 'pentium' and 'generic', don't emit the call to the generic one (since we
3251 // always run on at least a 'pentium'). We do this by deleting the 'least
3252 // advanced' (read, lowest mangling letter).
3253 while (Options.size() > 1 &&
3254 CodeGenFunction::GetX86CpuSupportsMask(
3255 (Options.end() - 2)->Conditions.Features) == 0) {
3256 StringRef LHSName = (Options.end() - 2)->Function->getName();
3257 StringRef RHSName = (Options.end() - 1)->Function->getName();
3258 if (LHSName.compare(RHSName) < 0)
3259 Options.erase(Options.end() - 2);
3260 else
3261 Options.erase(Options.end() - 1);
3262 }
3263
3264 CodeGenFunction CGF(*this);
3265 CGF.EmitMultiVersionResolver(ResolverFunc, Options);
3266
3267 if (getTarget().supportsIFunc()) {
3268 std::string AliasName = getMangledNameImpl(
3269 *this, GD, FD, /*OmitMultiVersionMangling=*/true);
3270 llvm::Constant *AliasFunc = GetGlobalValue(AliasName);
3271 if (!AliasFunc) {
3272 auto *IFunc = cast<llvm::GlobalIFunc>(GetOrCreateLLVMFunction(
3273 AliasName, DeclTy, GD, /*ForVTable=*/false, /*DontDefer=*/true,
3274 /*IsThunk=*/false, llvm::AttributeList(), NotForDefinition));
3275 auto *GA = llvm::GlobalAlias::create(
3276 DeclTy, 0, getFunctionLinkage(GD), AliasName, IFunc, &getModule());
3277 GA->setLinkage(llvm::Function::WeakODRLinkage);
3278 SetCommonAttributes(GD, GA);
3279 }
3280 }
3281}
3282
3283/// If a dispatcher for the specified mangled name is not in the module, create
3284/// and return an llvm Function with the specified type.
3285llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(
3286 GlobalDecl GD, llvm::Type *DeclTy, const FunctionDecl *FD) {
3287 std::string MangledName =
3288 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
3289
3290 // Holds the name of the resolver, in ifunc mode this is the ifunc (which has
3291 // a separate resolver).
3292 std::string ResolverName = MangledName;
3293 if (getTarget().supportsIFunc())
3294 ResolverName += ".ifunc";
3295 else if (FD->isTargetMultiVersion())
3296 ResolverName += ".resolver";
3297
3298 // If this already exists, just return that one.
3299 if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName))
3300 return ResolverGV;
3301
3302 // Since this is the first time we've created this IFunc, make sure
3303 // that we put this multiversioned function into the list to be
3304 // replaced later if necessary (target multiversioning only).
3305 if (!FD->isCPUDispatchMultiVersion() && !FD->isCPUSpecificMultiVersion())
3306 MultiVersionFuncs.push_back(GD);
3307
3308 if (getTarget().supportsIFunc()) {
3309 llvm::Type *ResolverType = llvm::FunctionType::get(
3310 llvm::PointerType::get(
3311 DeclTy, getContext().getTargetAddressSpace(FD->getType())),
3312 false);
3313 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
3314 MangledName + ".resolver", ResolverType, GlobalDecl{},
3315 /*ForVTable=*/false);
3316 llvm::GlobalIFunc *GIF = llvm::GlobalIFunc::create(
3317 DeclTy, 0, llvm::Function::WeakODRLinkage, "", Resolver, &getModule());
3318 GIF->setName(ResolverName);
3319 SetCommonAttributes(FD, GIF);
3320
3321 return GIF;
3322 }
3323
3324 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
3325 ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false);
3326 assert(isa<llvm::GlobalValue>(Resolver) &&((isa<llvm::GlobalValue>(Resolver) && "Resolver should be created for the first time"
) ? static_cast<void> (0) : __assert_fail ("isa<llvm::GlobalValue>(Resolver) && \"Resolver should be created for the first time\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3327, __PRETTY_FUNCTION__))
3327 "Resolver should be created for the first time")((isa<llvm::GlobalValue>(Resolver) && "Resolver should be created for the first time"
) ? static_cast<void> (0) : __assert_fail ("isa<llvm::GlobalValue>(Resolver) && \"Resolver should be created for the first time\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3327, __PRETTY_FUNCTION__))
;
3328 SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver));
3329 return Resolver;
3330}
3331
3332/// GetOrCreateLLVMFunction - If the specified mangled name is not in the
3333/// module, create and return an llvm Function with the specified type. If there
3334/// is something in the module with the specified name, return it potentially
3335/// bitcasted to the right type.
3336///
3337/// If D is non-null, it specifies a decl that correspond to this. This is used
3338/// to set the attributes on the function when it is first created.
3339llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
3340 StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable,
3341 bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs,
3342 ForDefinition_t IsForDefinition) {
3343 const Decl *D = GD.getDecl();
3344
3345 // Any attempts to use a MultiVersion function should result in retrieving
3346 // the iFunc instead. Name Mangling will handle the rest of the changes.
3347 if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) {
3348 // For the device mark the function as one that should be emitted.
3349 if (getLangOpts().OpenMPIsDevice && OpenMPRuntime &&
3350 !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() &&
3351 !DontDefer && !IsForDefinition) {
3352 if (const FunctionDecl *FDDef = FD->getDefinition()) {
3353 GlobalDecl GDDef;
3354 if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef))
3355 GDDef = GlobalDecl(CD, GD.getCtorType());
3356 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef))
3357 GDDef = GlobalDecl(DD, GD.getDtorType());
3358 else
3359 GDDef = GlobalDecl(FDDef);
3360 EmitGlobal(GDDef);
3361 }
3362 }
3363
3364 if (FD->isMultiVersion()) {
3365 if (FD->hasAttr<TargetAttr>())
3366 UpdateMultiVersionNames(GD, FD);
3367 if (!IsForDefinition)
3368 return GetOrCreateMultiVersionResolver(GD, Ty, FD);
3369 }
3370 }
3371
3372 // Lookup the entry, lazily creating it if necessary.
3373 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
3374 if (Entry) {
3375 if (WeakRefReferences.erase(Entry)) {
3376 const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
3377 if (FD && !FD->hasAttr<WeakAttr>())
3378 Entry->setLinkage(llvm::Function::ExternalLinkage);
3379 }
3380
3381 // Handle dropped DLL attributes.
3382 if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) {
3383 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
3384 setDSOLocal(Entry);
3385 }
3386
3387 // If there are two attempts to define the same mangled name, issue an
3388 // error.
3389 if (IsForDefinition && !Entry->isDeclaration()) {
3390 GlobalDecl OtherGD;
3391 // Check that GD is not yet in DiagnosedConflictingDefinitions is required
3392 // to make sure that we issue an error only once.
3393 if (lookupRepresentativeDecl(MangledName, OtherGD) &&
3394 (GD.getCanonicalDecl().getDecl() !=
3395 OtherGD.getCanonicalDecl().getDecl()) &&
3396 DiagnosedConflictingDefinitions.insert(GD).second) {
3397 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
3398 << MangledName;
3399 getDiags().Report(OtherGD.getDecl()->getLocation(),
3400 diag::note_previous_definition);
3401 }
3402 }
3403
3404 if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) &&
3405 (Entry->getValueType() == Ty)) {
3406 return Entry;
3407 }
3408
3409 // Make sure the result is of the correct type.
3410 // (If function is requested for a definition, we always need to create a new
3411 // function, not just return a bitcast.)
3412 if (!IsForDefinition)
3413 return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo());
3414 }
3415
3416 // This function doesn't have a complete type (for example, the return
3417 // type is an incomplete struct). Use a fake type instead, and make
3418 // sure not to try to set attributes.
3419 bool IsIncompleteFunction = false;
3420
3421 llvm::FunctionType *FTy;
3422 if (isa<llvm::FunctionType>(Ty)) {
3423 FTy = cast<llvm::FunctionType>(Ty);
3424 } else {
3425 FTy = llvm::FunctionType::get(VoidTy, false);
3426 IsIncompleteFunction = true;
3427 }
3428
3429 llvm::Function *F =
3430 llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
3431 Entry ? StringRef() : MangledName, &getModule());
3432
3433 // If we already created a function with the same mangled name (but different
3434 // type) before, take its name and add it to the list of functions to be
3435 // replaced with F at the end of CodeGen.
3436 //
3437 // This happens if there is a prototype for a function (e.g. "int f()") and
3438 // then a definition of a different type (e.g. "int f(int x)").
3439 if (Entry) {
3440 F->takeName(Entry);
3441
3442 // This might be an implementation of a function without a prototype, in
3443 // which case, try to do special replacement of calls which match the new
3444 // prototype. The really key thing here is that we also potentially drop
3445 // arguments from the call site so as to make a direct call, which makes the
3446 // inliner happier and suppresses a number of optimizer warnings (!) about
3447 // dropping arguments.
3448 if (!Entry->use_empty()) {
3449 ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F);
3450 Entry->removeDeadConstantUsers();
3451 }
3452
3453 llvm::Constant *BC = llvm::ConstantExpr::getBitCast(
3454 F, Entry->getValueType()->getPointerTo());
3455 addGlobalValReplacement(Entry, BC);
3456 }
3457
3458 assert(F->getName() == MangledName && "name was uniqued!")((F->getName() == MangledName && "name was uniqued!"
) ? static_cast<void> (0) : __assert_fail ("F->getName() == MangledName && \"name was uniqued!\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3458, __PRETTY_FUNCTION__))
;
3459 if (D)
3460 SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
3461 if (ExtraAttrs.hasAttributes(llvm::AttributeList::FunctionIndex)) {
3462 llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeList::FunctionIndex);
3463 F->addAttributes(llvm::AttributeList::FunctionIndex, B);
3464 }
3465
3466 if (!DontDefer) {
3467 // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
3468 // each other bottoming out with the base dtor. Therefore we emit non-base
3469 // dtors on usage, even if there is no dtor definition in the TU.
3470 if (D && isa<CXXDestructorDecl>(D) &&
3471 getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
3472 GD.getDtorType()))
3473 addDeferredDeclToEmit(GD);
3474
3475 // This is the first use or definition of a mangled name. If there is a
3476 // deferred decl with this name, remember that we need to emit it at the end
3477 // of the file.
3478 auto DDI = DeferredDecls.find(MangledName);
3479 if (DDI != DeferredDecls.end()) {
3480 // Move the potentially referenced deferred decl to the
3481 // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
3482 // don't need it anymore).
3483 addDeferredDeclToEmit(DDI->second);
3484 DeferredDecls.erase(DDI);
3485
3486 // Otherwise, there are cases we have to worry about where we're
3487 // using a declaration for which we must emit a definition but where
3488 // we might not find a top-level definition:
3489 // - member functions defined inline in their classes
3490 // - friend functions defined inline in some class
3491 // - special member functions with implicit definitions
3492 // If we ever change our AST traversal to walk into class methods,
3493 // this will be unnecessary.
3494 //
3495 // We also don't emit a definition for a function if it's going to be an
3496 // entry in a vtable, unless it's already marked as used.
3497 } else if (getLangOpts().CPlusPlus && D) {
3498 // Look for a declaration that's lexically in a record.
3499 for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
3500 FD = FD->getPreviousDecl()) {
3501 if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
3502 if (FD->doesThisDeclarationHaveABody()) {
3503 addDeferredDeclToEmit(GD.getWithDecl(FD));
3504 break;
3505 }
3506 }
3507 }
3508 }
3509 }
3510
3511 // Make sure the result is of the requested type.
3512 if (!IsIncompleteFunction) {
3513 assert(F->getFunctionType() == Ty)((F->getFunctionType() == Ty) ? static_cast<void> (0
) : __assert_fail ("F->getFunctionType() == Ty", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3513, __PRETTY_FUNCTION__))
;
3514 return F;
3515 }
3516
3517 llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
3518 return llvm::ConstantExpr::getBitCast(F, PTy);
3519}
3520
3521/// GetAddrOfFunction - Return the address of the given function. If Ty is
3522/// non-null, then this function will use the specified type if it has to
3523/// create it (this occurs when we see a definition of the function).
3524llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
3525 llvm::Type *Ty,
3526 bool ForVTable,
3527 bool DontDefer,
3528 ForDefinition_t IsForDefinition) {
3529 assert(!cast<FunctionDecl>(GD.getDecl())->isConsteval() &&((!cast<FunctionDecl>(GD.getDecl())->isConsteval() &&
"consteval function should never be emitted") ? static_cast<
void> (0) : __assert_fail ("!cast<FunctionDecl>(GD.getDecl())->isConsteval() && \"consteval function should never be emitted\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3530, __PRETTY_FUNCTION__))
3530 "consteval function should never be emitted")((!cast<FunctionDecl>(GD.getDecl())->isConsteval() &&
"consteval function should never be emitted") ? static_cast<
void> (0) : __assert_fail ("!cast<FunctionDecl>(GD.getDecl())->isConsteval() && \"consteval function should never be emitted\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3530, __PRETTY_FUNCTION__))
;
3531 // If there was no specific requested type, just convert it now.
3532 if (!Ty) {
3533 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3534 Ty = getTypes().ConvertType(FD->getType());
3535 }
3536
3537 // Devirtualized destructor calls may come through here instead of via
3538 // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
3539 // of the complete destructor when necessary.
3540 if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) {
3541 if (getTarget().getCXXABI().isMicrosoft() &&
3542 GD.getDtorType() == Dtor_Complete &&
3543 DD->getParent()->getNumVBases() == 0)
3544 GD = GlobalDecl(DD, Dtor_Base);
3545 }
3546
3547 StringRef MangledName = getMangledName(GD);
3548 return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer,
3549 /*IsThunk=*/false, llvm::AttributeList(),
3550 IsForDefinition);
3551}
3552
3553static const FunctionDecl *
3554GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) {
3555 TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
3556 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
3557
3558 IdentifierInfo &CII = C.Idents.get(Name);
3559 for (const auto &Result : DC->lookup(&CII))
3560 if (const auto FD = dyn_cast<FunctionDecl>(Result))
3561 return FD;
3562
3563 if (!C.getLangOpts().CPlusPlus)
3564 return nullptr;
3565
3566 // Demangle the premangled name from getTerminateFn()
3567 IdentifierInfo &CXXII =
3568 (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ")
3569 ? C.Idents.get("terminate")
3570 : C.Idents.get(Name);
3571
3572 for (const auto &N : {"__cxxabiv1", "std"}) {
3573 IdentifierInfo &NS = C.Idents.get(N);
3574 for (const auto &Result : DC->lookup(&NS)) {
3575 NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result);
3576 if (auto LSD = dyn_cast<LinkageSpecDecl>(Result))
3577 for (const auto &Result : LSD->lookup(&NS))
3578 if ((ND = dyn_cast<NamespaceDecl>(Result)))
3579 break;
3580
3581 if (ND)
3582 for (const auto &Result : ND->lookup(&CXXII))
3583 if (const auto *FD = dyn_cast<FunctionDecl>(Result))
3584 return FD;
3585 }
3586 }
3587
3588 return nullptr;
3589}
3590
3591/// CreateRuntimeFunction - Create a new runtime function with the specified
3592/// type and name.
3593llvm::FunctionCallee
3594CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
3595 llvm::AttributeList ExtraAttrs, bool Local,
3596 bool AssumeConvergent) {
3597 if (AssumeConvergent) {
3598 ExtraAttrs =
3599 ExtraAttrs.addAttribute(VMContext, llvm::AttributeList::FunctionIndex,
3600 llvm::Attribute::Convergent);
3601 }
3602
3603 llvm::Constant *C =
3604 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
3605 /*DontDefer=*/false, /*IsThunk=*/false,
3606 ExtraAttrs);
3607
3608 if (auto *F = dyn_cast<llvm::Function>(C)) {
3609 if (F->empty()) {
3610 F->setCallingConv(getRuntimeCC());
3611
3612 // In Windows Itanium environments, try to mark runtime functions
3613 // dllimport. For Mingw and MSVC, don't. We don't really know if the user
3614 // will link their standard library statically or dynamically. Marking
3615 // functions imported when they are not imported can cause linker errors
3616 // and warnings.
3617 if (!Local && getTriple().isWindowsItaniumEnvironment() &&
3618 !getCodeGenOpts().LTOVisibilityPublicStd) {
3619 const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name);
3620 if (!FD || FD->hasAttr<DLLImportAttr>()) {
3621 F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
3622 F->setLinkage(llvm::GlobalValue::ExternalLinkage);
3623 }
3624 }
3625 setDSOLocal(F);
3626 }
3627 }
3628
3629 return {FTy, C};
3630}
3631
3632/// isTypeConstant - Determine whether an object of this type can be emitted
3633/// as a constant.
3634///
3635/// If ExcludeCtor is true, the duration when the object's constructor runs
3636/// will not be considered. The caller will need to verify that the object is
3637/// not written to during its construction.
3638bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
3639 if (!Ty.isConstant(Context) && !Ty->isReferenceType())
3640 return false;
3641
3642 if (Context.getLangOpts().CPlusPlus) {
3643 if (const CXXRecordDecl *Record
3644 = Context.getBaseElementType(Ty)->getAsCXXRecordDecl())
3645 return ExcludeCtor && !Record->hasMutableFields() &&
3646 Record->hasTrivialDestructor();
3647 }
3648
3649 return true;
3650}
3651
3652/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
3653/// create and return an llvm GlobalVariable with the specified type. If there
3654/// is something in the module with the specified name, return it potentially
3655/// bitcasted to the right type.
3656///
3657/// If D is non-null, it specifies a decl that correspond to this. This is used
3658/// to set the attributes on the global when it is first created.
3659///
3660/// If IsForDefinition is true, it is guaranteed that an actual global with
3661/// type Ty will be returned, not conversion of a variable with the same
3662/// mangled name but some other type.
3663llvm::Constant *
3664CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
3665 llvm::PointerType *Ty,
3666 const VarDecl *D,
3667 ForDefinition_t IsForDefinition) {
3668 // Lookup the entry, lazily creating it if necessary.
3669 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
3670 if (Entry) {
3671 if (WeakRefReferences.erase(Entry)) {
3672 if (D && !D->hasAttr<WeakAttr>())
3673 Entry->setLinkage(llvm::Function::ExternalLinkage);
3674 }
3675
3676 // Handle dropped DLL attributes.
3677 if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>())
3678 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
3679
3680 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
3681 getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);
3682
3683 if (Entry->getType() == Ty)
3684 return Entry;
3685
3686 // If there are two attempts to define the same mangled name, issue an
3687 // error.
3688 if (IsForDefinition && !Entry->isDeclaration()) {
3689 GlobalDecl OtherGD;
3690 const VarDecl *OtherD;
3691
3692 // Check that D is not yet in DiagnosedConflictingDefinitions is required
3693 // to make sure that we issue an error only once.
3694 if (D && lookupRepresentativeDecl(MangledName, OtherGD) &&
3695 (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) &&
3696 (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) &&
3697 OtherD->hasInit() &&
3698 DiagnosedConflictingDefinitions.insert(D).second) {
3699 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
3700 << MangledName;
3701 getDiags().Report(OtherGD.getDecl()->getLocation(),
3702 diag::note_previous_definition);
3703 }
3704 }
3705
3706 // Make sure the result is of the correct type.
3707 if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace())
3708 return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty);
3709
3710 // (If global is requested for a definition, we always need to create a new
3711 // global, not just return a bitcast.)
3712 if (!IsForDefinition)
3713 return llvm::ConstantExpr::getBitCast(Entry, Ty);
3714 }
3715
3716 auto AddrSpace = GetGlobalVarAddressSpace(D);
3717 auto TargetAddrSpace = getContext().getTargetAddressSpace(AddrSpace);
3718
3719 auto *GV = new llvm::GlobalVariable(
3720 getModule(), Ty->getElementType(), false,
3721 llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr,
3722 llvm::GlobalVariable::NotThreadLocal, TargetAddrSpace);
3723
3724 // If we already created a global with the same mangled name (but different
3725 // type) before, take its name and remove it from its parent.
3726 if (Entry) {
3727 GV->takeName(Entry);
3728
3729 if (!Entry->use_empty()) {
3730 llvm::Constant *NewPtrForOldDecl =
3731 llvm::ConstantExpr::getBitCast(GV, Entry->getType());
3732 Entry->replaceAllUsesWith(NewPtrForOldDecl);
3733 }
3734
3735 Entry->eraseFromParent();
3736 }
3737
3738 // This is the first use or definition of a mangled name. If there is a
3739 // deferred decl with this name, remember that we need to emit it at the end
3740 // of the file.
3741 auto DDI = DeferredDecls.find(MangledName);
3742 if (DDI != DeferredDecls.end()) {
3743 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
3744 // list, and remove it from DeferredDecls (since we don't need it anymore).
3745 addDeferredDeclToEmit(DDI->second);
3746 DeferredDecls.erase(DDI);
3747 }
3748
3749 // Handle things which are present even on external declarations.
3750 if (D) {
3751 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd)
3752 getOpenMPRuntime().registerTargetGlobalVariable(D, GV);
3753
3754 // FIXME: This code is overly simple and should be merged with other global
3755 // handling.
3756 GV->setConstant(isTypeConstant(D->getType(), false));
3757
3758 GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
3759
3760 setLinkageForGV(GV, D);
3761
3762 if (D->getTLSKind()) {
3763 if (D->getTLSKind() == VarDecl::TLS_Dynamic)
3764 CXXThreadLocals.push_back(D);
3765 setTLSMode(GV, *D);
3766 }
3767
3768 setGVProperties(GV, D);
3769
3770 // If required by the ABI, treat declarations of static data members with
3771 // inline initializers as definitions.
3772 if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
3773 EmitGlobalVarDefinition(D);
3774 }
3775
3776 // Emit section information for extern variables.
3777 if (D->hasExternalStorage()) {
3778 if (const SectionAttr *SA = D->getAttr<SectionAttr>())
3779 GV->setSection(SA->getName());
3780 }
3781
3782 // Handle XCore specific ABI requirements.
3783 if (getTriple().getArch() == llvm::Triple::xcore &&
3784 D->getLanguageLinkage() == CLanguageLinkage &&
3785 D->getType().isConstant(Context) &&
3786 isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
3787 GV->setSection(".cp.rodata");
3788
3789 // Check if we a have a const declaration with an initializer, we may be
3790 // able to emit it as available_externally to expose it's value to the
3791 // optimizer.
3792 if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() &&
3793 D->getType().isConstQualified() && !GV->hasInitializer() &&
3794 !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) {
3795 const auto *Record =
3796 Context.getBaseElementType(D->getType())->getAsCXXRecordDecl();
3797 bool HasMutableFields = Record && Record->hasMutableFields();
3798 if (!HasMutableFields) {
3799 const VarDecl *InitDecl;
3800 const Expr *InitExpr = D->getAnyInitializer(InitDecl);
3801 if (InitExpr) {
3802 ConstantEmitter emitter(*this);
3803 llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl);
3804 if (Init) {
3805 auto *InitType = Init->getType();
3806 if (GV->getValueType() != InitType) {
3807 // The type of the initializer does not match the definition.
3808 // This happens when an initializer has a different type from
3809 // the type of the global (because of padding at the end of a
3810 // structure for instance).
3811 GV->setName(StringRef());
3812 // Make a new global with the correct type, this is now guaranteed
3813 // to work.
3814 auto *NewGV = cast<llvm::GlobalVariable>(
3815 GetAddrOfGlobalVar(D, InitType, IsForDefinition)
3816 ->stripPointerCasts());
3817
3818 // Erase the old global, since it is no longer used.
3819 GV->eraseFromParent();
3820 GV = NewGV;
3821 } else {
3822 GV->setInitializer(Init);
3823 GV->setConstant(true);
3824 GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
3825 }
3826 emitter.finalize(GV);
3827 }
3828 }
3829 }
3830 }
3831 }
3832
3833 if (GV->isDeclaration())
3834 getTargetCodeGenInfo().setTargetAttributes(D, GV, *this);
3835
3836 LangAS ExpectedAS =
3837 D ? D->getType().getAddressSpace()
3838 : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
3839 assert(getContext().getTargetAddressSpace(ExpectedAS) ==((getContext().getTargetAddressSpace(ExpectedAS) == Ty->getPointerAddressSpace
()) ? static_cast<void> (0) : __assert_fail ("getContext().getTargetAddressSpace(ExpectedAS) == Ty->getPointerAddressSpace()"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3840, __PRETTY_FUNCTION__))
3840 Ty->getPointerAddressSpace())((getContext().getTargetAddressSpace(ExpectedAS) == Ty->getPointerAddressSpace
()) ? static_cast<void> (0) : __assert_fail ("getContext().getTargetAddressSpace(ExpectedAS) == Ty->getPointerAddressSpace()"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3840, __PRETTY_FUNCTION__))
;
3841 if (AddrSpace != ExpectedAS)
3842 return getTargetCodeGenInfo().performAddrSpaceCast(*this, GV, AddrSpace,
3843 ExpectedAS, Ty);
3844
3845 return GV;
3846}
3847
3848llvm::Constant *
3849CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition) {
3850 const Decl *D = GD.getDecl();
3851
3852 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
3853 return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
3854 /*DontDefer=*/false, IsForDefinition);
3855
3856 if (isa<CXXMethodDecl>(D)) {
3857 auto FInfo =
3858 &getTypes().arrangeCXXMethodDeclaration(cast<CXXMethodDecl>(D));
3859 auto Ty = getTypes().GetFunctionType(*FInfo);
3860 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
3861 IsForDefinition);
3862 }
3863
3864 if (isa<FunctionDecl>(D)) {
3865 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
3866 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
3867 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
3868 IsForDefinition);
3869 }
3870
3871 return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, IsForDefinition);
3872}
3873
3874llvm::GlobalVariable *CodeGenModule::CreateOrReplaceCXXRuntimeVariable(
3875 StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage,
3876 unsigned Alignment) {
3877 llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
3878 llvm::GlobalVariable *OldGV = nullptr;
3879
3880 if (GV) {
3881 // Check if the variable has the right type.
3882 if (GV->getValueType() == Ty)
3883 return GV;
3884
3885 // Because C++ name mangling, the only way we can end up with an already
3886 // existing global with the same name is if it has been declared extern "C".
3887 assert(GV->isDeclaration() && "Declaration has wrong type!")((GV->isDeclaration() && "Declaration has wrong type!"
) ? static_cast<void> (0) : __assert_fail ("GV->isDeclaration() && \"Declaration has wrong type!\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3887, __PRETTY_FUNCTION__))
;
3888 OldGV = GV;
3889 }
3890
3891 // Create a new variable.
3892 GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
3893 Linkage, nullptr, Name);
3894
3895 if (OldGV) {
3896 // Replace occurrences of the old variable if needed.
3897 GV->takeName(OldGV);
3898
3899 if (!OldGV->use_empty()) {
3900 llvm::Constant *NewPtrForOldDecl =
3901 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
3902 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
3903 }
3904
3905 OldGV->eraseFromParent();
3906 }
3907
3908 if (supportsCOMDAT() && GV->isWeakForLinker() &&
3909 !GV->hasAvailableExternallyLinkage())
3910 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3911
3912 GV->setAlignment(llvm::MaybeAlign(Alignment));
3913
3914 return GV;
3915}
3916
3917/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
3918/// given global variable. If Ty is non-null and if the global doesn't exist,
3919/// then it will be created with the specified type instead of whatever the
3920/// normal requested type would be. If IsForDefinition is true, it is guaranteed
3921/// that an actual global with type Ty will be returned, not conversion of a
3922/// variable with the same mangled name but some other type.
3923llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
3924 llvm::Type *Ty,
3925 ForDefinition_t IsForDefinition) {
3926 assert(D->hasGlobalStorage() && "Not a global variable")((D->hasGlobalStorage() && "Not a global variable"
) ? static_cast<void> (0) : __assert_fail ("D->hasGlobalStorage() && \"Not a global variable\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3926, __PRETTY_FUNCTION__))
;
3927 QualType ASTTy = D->getType();
3928 if (!Ty)
3929 Ty = getTypes().ConvertTypeForMem(ASTTy);
3930
3931 llvm::PointerType *PTy =
3932 llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
3933
3934 StringRef MangledName = getMangledName(D);
3935 return GetOrCreateLLVMGlobal(MangledName, PTy, D, IsForDefinition);
3936}
3937
3938/// CreateRuntimeVariable - Create a new runtime global variable with the
3939/// specified type and name.
3940llvm::Constant *
3941CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
3942 StringRef Name) {
3943 auto PtrTy =
3944 getContext().getLangOpts().OpenCL
3945 ? llvm::PointerType::get(
3946 Ty, getContext().getTargetAddressSpace(LangAS::opencl_global))
3947 : llvm::PointerType::getUnqual(Ty);
3948 auto *Ret = GetOrCreateLLVMGlobal(Name, PtrTy, nullptr);
3949 setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
3950 return Ret;
3951}
3952
3953void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
3954 assert(!D->getInit() && "Cannot emit definite definitions here!")((!D->getInit() && "Cannot emit definite definitions here!"
) ? static_cast<void> (0) : __assert_fail ("!D->getInit() && \"Cannot emit definite definitions here!\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3954, __PRETTY_FUNCTION__))
;
3955
3956 StringRef MangledName = getMangledName(D);
3957 llvm::GlobalValue *GV = GetGlobalValue(MangledName);
3958
3959 // We already have a definition, not declaration, with the same mangled name.
3960 // Emitting of declaration is not required (and actually overwrites emitted
3961 // definition).
3962 if (GV && !GV->isDeclaration())
3963 return;
3964
3965 // If we have not seen a reference to this variable yet, place it into the
3966 // deferred declarations table to be emitted if needed later.
3967 if (!MustBeEmitted(D) && !GV) {
3968 DeferredDecls[MangledName] = D;
3969 return;
3970 }
3971
3972 // The tentative definition is the only definition.
3973 EmitGlobalVarDefinition(D);
3974}
3975
3976void CodeGenModule::EmitExternalDeclaration(const VarDecl *D) {
3977 EmitExternalVarDeclaration(D);
3978}
3979
3980CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
3981 return Context.toCharUnitsFromBits(
3982 getDataLayout().getTypeStoreSizeInBits(Ty));
3983}
3984
3985LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) {
3986 LangAS AddrSpace = LangAS::Default;
3987 if (LangOpts.OpenCL) {
3988 AddrSpace = D ? D->getType().getAddressSpace() : LangAS::opencl_global;
3989 assert(AddrSpace == LangAS::opencl_global ||((AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::
opencl_global_device || AddrSpace == LangAS::opencl_global_host
|| AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS
::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace
) ? static_cast<void> (0) : __assert_fail ("AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::opencl_global_device || AddrSpace == LangAS::opencl_global_host || AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3994, __PRETTY_FUNCTION__))
3990 AddrSpace == LangAS::opencl_global_device ||((AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::
opencl_global_device || AddrSpace == LangAS::opencl_global_host
|| AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS
::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace
) ? static_cast<void> (0) : __assert_fail ("AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::opencl_global_device || AddrSpace == LangAS::opencl_global_host || AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3994, __PRETTY_FUNCTION__))
3991 AddrSpace == LangAS::opencl_global_host ||((AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::
opencl_global_device || AddrSpace == LangAS::opencl_global_host
|| AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS
::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace
) ? static_cast<void> (0) : __assert_fail ("AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::opencl_global_device || AddrSpace == LangAS::opencl_global_host || AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3994, __PRETTY_FUNCTION__))
3992 AddrSpace == LangAS::opencl_constant ||((AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::
opencl_global_device || AddrSpace == LangAS::opencl_global_host
|| AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS
::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace
) ? static_cast<void> (0) : __assert_fail ("AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::opencl_global_device || AddrSpace == LangAS::opencl_global_host || AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3994, __PRETTY_FUNCTION__))
3993 AddrSpace == LangAS::opencl_local ||((AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::
opencl_global_device || AddrSpace == LangAS::opencl_global_host
|| AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS
::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace
) ? static_cast<void> (0) : __assert_fail ("AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::opencl_global_device || AddrSpace == LangAS::opencl_global_host || AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3994, __PRETTY_FUNCTION__))
3994 AddrSpace >= LangAS::FirstTargetAddressSpace)((AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::
opencl_global_device || AddrSpace == LangAS::opencl_global_host
|| AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS
::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace
) ? static_cast<void> (0) : __assert_fail ("AddrSpace == LangAS::opencl_global || AddrSpace == LangAS::opencl_global_device || AddrSpace == LangAS::opencl_global_host || AddrSpace == LangAS::opencl_constant || AddrSpace == LangAS::opencl_local || AddrSpace >= LangAS::FirstTargetAddressSpace"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 3994, __PRETTY_FUNCTION__))
;
3995 return AddrSpace;
3996 }
3997
3998 if (LangOpts.CUDA && LangOpts.CUDAIsDevice) {
3999 if (D && D->hasAttr<CUDAConstantAttr>())
4000 return LangAS::cuda_constant;
4001 else if (D && D->hasAttr<CUDASharedAttr>())
4002 return LangAS::cuda_shared;
4003 else if (D && D->hasAttr<CUDADeviceAttr>())
4004 return LangAS::cuda_device;
4005 else if (D && D->getType().isConstQualified())
4006 return LangAS::cuda_constant;
4007 else
4008 return LangAS::cuda_device;
4009 }
4010
4011 if (LangOpts.OpenMP) {
4012 LangAS AS;
4013 if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
4014 return AS;
4015 }
4016 return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D);
4017}
4018
4019LangAS CodeGenModule::getStringLiteralAddressSpace() const {
4020 // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
4021 if (LangOpts.OpenCL)
4022 return LangAS::opencl_constant;
4023 if (auto AS = getTarget().getConstantAddressSpace())
4024 return AS.getValue();
4025 return LangAS::Default;
4026}
4027
4028// In address space agnostic languages, string literals are in default address
4029// space in AST. However, certain targets (e.g. amdgcn) request them to be
4030// emitted in constant address space in LLVM IR. To be consistent with other
4031// parts of AST, string literal global variables in constant address space
4032// need to be casted to default address space before being put into address
4033// map and referenced by other part of CodeGen.
4034// In OpenCL, string literals are in constant address space in AST, therefore
4035// they should not be casted to default address space.
4036static llvm::Constant *
4037castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM,
4038 llvm::GlobalVariable *GV) {
4039 llvm::Constant *Cast = GV;
4040 if (!CGM.getLangOpts().OpenCL) {
4041 if (auto AS = CGM.getTarget().getConstantAddressSpace()) {
4042 if (AS != LangAS::Default)
4043 Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast(
4044 CGM, GV, AS.getValue(), LangAS::Default,
4045 GV->getValueType()->getPointerTo(
4046 CGM.getContext().getTargetAddressSpace(LangAS::Default)));
4047 }
4048 }
4049 return Cast;
4050}
4051
4052template<typename SomeDecl>
4053void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D,
4054 llvm::GlobalValue *GV) {
4055 if (!getLangOpts().CPlusPlus)
4056 return;
4057
4058 // Must have 'used' attribute, or else inline assembly can't rely on
4059 // the name existing.
4060 if (!D->template hasAttr<UsedAttr>())
4061 return;
4062
4063 // Must have internal linkage and an ordinary name.
4064 if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage)
4065 return;
4066
4067 // Must be in an extern "C" context. Entities declared directly within
4068 // a record are not extern "C" even if the record is in such a context.
4069 const SomeDecl *First = D->getFirstDecl();
4070 if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
4071 return;
4072
4073 // OK, this is an internal linkage entity inside an extern "C" linkage
4074 // specification. Make a note of that so we can give it the "expected"
4075 // mangled name if nothing else is using that name.
4076 std::pair<StaticExternCMap::iterator, bool> R =
4077 StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
4078
4079 // If we have multiple internal linkage entities with the same name
4080 // in extern "C" regions, none of them gets that name.
4081 if (!R.second)
4082 R.first->second = nullptr;
4083}
4084
4085static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
4086 if (!CGM.supportsCOMDAT())
4087 return false;
4088
4089 // Do not set COMDAT attribute for CUDA/HIP stub functions to prevent
4090 // them being "merged" by the COMDAT Folding linker optimization.
4091 if (D.hasAttr<CUDAGlobalAttr>())
4092 return false;
4093
4094 if (D.hasAttr<SelectAnyAttr>())
4095 return true;
4096
4097 GVALinkage Linkage;
4098 if (auto *VD = dyn_cast<VarDecl>(&D))
4099 Linkage = CGM.getContext().GetGVALinkageForVariable(VD);
4100 else
4101 Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
4102
4103 switch (Linkage) {
4104 case GVA_Internal:
4105 case GVA_AvailableExternally:
4106 case GVA_StrongExternal:
4107 return false;
4108 case GVA_DiscardableODR:
4109 case GVA_StrongODR:
4110 return true;
4111 }
4112 llvm_unreachable("No such linkage")::llvm::llvm_unreachable_internal("No such linkage", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4112)
;
4113}
4114
4115void CodeGenModule::maybeSetTrivialComdat(const Decl &D,
4116 llvm::GlobalObject &GO) {
4117 if (!shouldBeInCOMDAT(*this, D))
4118 return;
4119 GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
4120}
4121
4122/// Pass IsTentative as true if you want to create a tentative definition.
4123void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
4124 bool IsTentative) {
4125 // OpenCL global variables of sampler type are translated to function calls,
4126 // therefore no need to be translated.
4127 QualType ASTTy = D->getType();
4128 if (getLangOpts().OpenCL && ASTTy->isSamplerT())
1
Assuming field 'OpenCL' is 0
4129 return;
4130
4131 // If this is OpenMP device, check if it is legal to emit this global
4132 // normally.
4133 if (LangOpts.OpenMPIsDevice && OpenMPRuntime &&
2
Assuming field 'OpenMPIsDevice' is 0
3
Taking false branch
4134 OpenMPRuntime->emitTargetGlobalVariable(D))
4135 return;
4136
4137 llvm::Constant *Init = nullptr;
4138 bool NeedsGlobalCtor = false;
4139 bool NeedsGlobalDtor =
4140 D->needsDestruction(getContext()) == QualType::DK_cxx_destructor;
4
Assuming the condition is false
4141
4142 const VarDecl *InitDecl;
4143 const Expr *InitExpr = D->getAnyInitializer(InitDecl);
4144
4145 Optional<ConstantEmitter> emitter;
4146
4147 // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
4148 // as part of their declaration." Sema has already checked for
4149 // error cases, so we just need to set Init to UndefValue.
4150 bool IsCUDASharedVar =
4151 getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>();
5
Assuming field 'CUDAIsDevice' is 0
4152 // Shadows of initialized device-side global variables are also left
4153 // undefined.
4154 bool IsCUDAShadowVar =
4155 !getLangOpts().CUDAIsDevice
5.1
Field 'CUDAIsDevice' is 0
5.1
Field 'CUDAIsDevice' is 0
&&
4156 (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() ||
4157 D->hasAttr<CUDASharedAttr>());
4158 bool IsCUDADeviceShadowVar =
4159 getLangOpts().CUDAIsDevice
5.2
Field 'CUDAIsDevice' is 0
5.2
Field 'CUDAIsDevice' is 0
&&
4160 (D->getType()->isCUDADeviceBuiltinSurfaceType() ||
4161 D->getType()->isCUDADeviceBuiltinTextureType());
4162 // HIP pinned shadow of initialized host-side global variables are also
4163 // left undefined.
4164 if (getLangOpts().CUDA &&
6
Assuming field 'CUDA' is 0
4165 (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar))
4166 Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy));
4167 else if (D->hasAttr<LoaderUninitializedAttr>())
7
Calling 'Decl::hasAttr'
9
Returning from 'Decl::hasAttr'
10
Taking false branch
4168 Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy));
4169 else if (!InitExpr) {
11
Assuming 'InitExpr' is non-null
12
Taking false branch
4170 // This is a tentative definition; tentative definitions are
4171 // implicitly initialized with { 0 }.
4172 //
4173 // Note that tentative definitions are only emitted at the end of
4174 // a translation unit, so they should never have incomplete
4175 // type. In addition, EmitTentativeDefinition makes sure that we
4176 // never attempt to emit a tentative definition if a real one
4177 // exists. A use may still exists, however, so we still may need
4178 // to do a RAUW.
4179 assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type")((!ASTTy->isIncompleteType() && "Unexpected incomplete type"
) ? static_cast<void> (0) : __assert_fail ("!ASTTy->isIncompleteType() && \"Unexpected incomplete type\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4179, __PRETTY_FUNCTION__))
;
4180 Init = EmitNullConstant(D->getType());
4181 } else {
4182 initializedGlobalDecl = GlobalDecl(D);
4183 emitter.emplace(*this);
4184 Init = emitter->tryEmitForInitializer(*InitDecl);
4185
4186 if (!Init) {
13
Assuming 'Init' is non-null
14
Taking false branch
4187 QualType T = InitExpr->getType();
4188 if (D->getType()->isReferenceType())
4189 T = D->getType();
4190
4191 if (getLangOpts().CPlusPlus) {
4192 Init = EmitNullConstant(T);
4193 NeedsGlobalCtor = true;
4194 } else {
4195 ErrorUnsupported(D, "static initializer");
4196 Init = llvm::UndefValue::get(getTypes().ConvertType(T));
4197 }
4198 } else {
4199 // We don't need an initializer, so remove the entry for the delayed
4200 // initializer position (just in case this entry was delayed) if we
4201 // also don't need to register a destructor.
4202 if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
15
Assuming field 'CPlusPlus' is 0
4203 DelayedCXXInitPosition.erase(D);
4204 }
4205 }
4206
4207 llvm::Type* InitType = Init->getType();
4208 llvm::Constant *Entry =
4209 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative));
16
Assuming 'IsTentative' is true
4210
4211 // Strip off pointer casts if we got them.
4212 Entry = Entry->stripPointerCasts();
4213
4214 // Entry is now either a Function or GlobalVariable.
4215 auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
17
Assuming 'Entry' is a 'GlobalVariable'
4216
4217 // We have a definition after a declaration with the wrong type.
4218 // We must make a new GlobalVariable* and update everything that used OldGV
4219 // (a declaration or tentative definition) with the new GlobalVariable*
4220 // (which will be a definition).
4221 //
4222 // This happens if there is a prototype for a global (e.g.
4223 // "extern int x[];") and then a definition of a different type (e.g.
4224 // "int x[10];"). This also happens when an initializer has a different type
4225 // from the type of the global (this happens with unions).
4226 if (!GV
17.1
'GV' is non-null
17.1
'GV' is non-null
|| GV->getValueType() != InitType ||
18
Assuming the condition is false
20
Taking false branch
4227 GV->getType()->getAddressSpace() !=
19
Assuming the condition is false
4228 getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) {
4229
4230 // Move the old entry aside so that we'll create a new one.
4231 Entry->setName(StringRef());
4232
4233 // Make a new global with the correct type, this is now guaranteed to work.
4234 GV = cast<llvm::GlobalVariable>(
4235 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))
4236 ->stripPointerCasts());
4237
4238 // Replace all uses of the old global with the new global
4239 llvm::Constant *NewPtrForOldDecl =
4240 llvm::ConstantExpr::getBitCast(GV, Entry->getType());
4241 Entry->replaceAllUsesWith(NewPtrForOldDecl);
4242
4243 // Erase the old global, since it is no longer used.
4244 cast<llvm::GlobalValue>(Entry)->eraseFromParent();
4245 }
4246
4247 MaybeHandleStaticInExternC(D, GV);
4248
4249 if (D->hasAttr<AnnotateAttr>())
21
Taking false branch
4250 AddGlobalAnnotations(D, GV);
4251
4252 // Set the llvm linkage type as appropriate.
4253 llvm::GlobalValue::LinkageTypes Linkage =
4254 getLLVMLinkageVarDefinition(D, GV->isConstant());
4255
4256 // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
4257 // the device. [...]"
4258 // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
4259 // __device__, declares a variable that: [...]
4260 // Is accessible from all the threads within the grid and from the host
4261 // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
4262 // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
4263 if (GV
21.1
'GV' is non-null
21.1
'GV' is non-null
&& LangOpts.CUDA) {
22
Assuming field 'CUDA' is not equal to 0
23
Taking true branch
4264 if (LangOpts.CUDAIsDevice) {
24
Assuming field 'CUDAIsDevice' is 0
25
Taking false branch
4265 if (Linkage != llvm::GlobalValue::InternalLinkage &&
4266 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()))
4267 GV->setExternallyInitialized(true);
4268 } else {
4269 // Host-side shadows of external declarations of device-side
4270 // global variables become internal definitions. These have to
4271 // be internal in order to prevent name conflicts with global
4272 // host variables with the same name in a different TUs.
4273 if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()) {
26
Calling 'Decl::hasAttr'
28
Returning from 'Decl::hasAttr'
29
Calling 'Decl::hasAttr'
31
Returning from 'Decl::hasAttr'
32
Taking false branch
4274 Linkage = llvm::GlobalValue::InternalLinkage;
4275 // Shadow variables and their properties must be registered with CUDA
4276 // runtime. Skip Extern global variables, which will be registered in
4277 // the TU where they are defined.
4278 //
4279 // Don't register a C++17 inline variable. The local symbol can be
4280 // discarded and referencing a discarded local symbol from outside the
4281 // comdat (__cuda_register_globals) is disallowed by the ELF spec.
4282 // TODO: Reject __device__ constexpr and __device__ inline in Sema.
4283 if (!D->hasExternalStorage() && !D->isInline())
4284 getCUDARuntime().registerDeviceVar(D, *GV, !D->hasDefinition(),
4285 D->hasAttr<CUDAConstantAttr>());
4286 } else if (D->hasAttr<CUDASharedAttr>()) {
33
Calling 'Decl::hasAttr'
35
Returning from 'Decl::hasAttr'
36
Taking false branch
4287 // __shared__ variables are odd. Shadows do get created, but
4288 // they are not registered with the CUDA runtime, so they
4289 // can't really be used to access their device-side
4290 // counterparts. It's not clear yet whether it's nvcc's bug or
4291 // a feature, but we've got to do the same for compatibility.
4292 Linkage = llvm::GlobalValue::InternalLinkage;
4293 } else if (D->getType()->isCUDADeviceBuiltinSurfaceType() ||
37
Assuming the condition is true
38
Taking true branch
4294 D->getType()->isCUDADeviceBuiltinTextureType()) {
4295 // Builtin surfaces and textures and their template arguments are
4296 // also registered with CUDA runtime.
4297 Linkage = llvm::GlobalValue::InternalLinkage;
4298 const ClassTemplateSpecializationDecl *TD =
4299 cast<ClassTemplateSpecializationDecl>(
4300 D->getType()->getAs<RecordType>()->getDecl());
39
Assuming the object is not a 'RecordType'
40
Called C++ object pointer is null
4301 const TemplateArgumentList &Args = TD->getTemplateArgs();
4302 if (TD->hasAttr<CUDADeviceBuiltinSurfaceTypeAttr>()) {
4303 assert(Args.size() == 2 &&((Args.size() == 2 && "Unexpected number of template arguments of CUDA device "
"builtin surface type.") ? static_cast<void> (0) : __assert_fail
("Args.size() == 2 && \"Unexpected number of template arguments of CUDA device \" \"builtin surface type.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4305, __PRETTY_FUNCTION__))
4304 "Unexpected number of template arguments of CUDA device "((Args.size() == 2 && "Unexpected number of template arguments of CUDA device "
"builtin surface type.") ? static_cast<void> (0) : __assert_fail
("Args.size() == 2 && \"Unexpected number of template arguments of CUDA device \" \"builtin surface type.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4305, __PRETTY_FUNCTION__))
4305 "builtin surface type.")((Args.size() == 2 && "Unexpected number of template arguments of CUDA device "
"builtin surface type.") ? static_cast<void> (0) : __assert_fail
("Args.size() == 2 && \"Unexpected number of template arguments of CUDA device \" \"builtin surface type.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4305, __PRETTY_FUNCTION__))
;
4306 auto SurfType = Args[1].getAsIntegral();
4307 if (!D->hasExternalStorage())
4308 getCUDARuntime().registerDeviceSurf(D, *GV, !D->hasDefinition(),
4309 SurfType.getSExtValue());
4310 } else {
4311 assert(Args.size() == 3 &&((Args.size() == 3 && "Unexpected number of template arguments of CUDA device "
"builtin texture type.") ? static_cast<void> (0) : __assert_fail
("Args.size() == 3 && \"Unexpected number of template arguments of CUDA device \" \"builtin texture type.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4313, __PRETTY_FUNCTION__))
4312 "Unexpected number of template arguments of CUDA device "((Args.size() == 3 && "Unexpected number of template arguments of CUDA device "
"builtin texture type.") ? static_cast<void> (0) : __assert_fail
("Args.size() == 3 && \"Unexpected number of template arguments of CUDA device \" \"builtin texture type.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4313, __PRETTY_FUNCTION__))
4313 "builtin texture type.")((Args.size() == 3 && "Unexpected number of template arguments of CUDA device "
"builtin texture type.") ? static_cast<void> (0) : __assert_fail
("Args.size() == 3 && \"Unexpected number of template arguments of CUDA device \" \"builtin texture type.\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4313, __PRETTY_FUNCTION__))
;
4314 auto TexType = Args[1].getAsIntegral();
4315 auto Normalized = Args[2].getAsIntegral();
4316 if (!D->hasExternalStorage())
4317 getCUDARuntime().registerDeviceTex(D, *GV, !D->hasDefinition(),
4318 TexType.getSExtValue(),
4319 Normalized.getZExtValue());
4320 }
4321 }
4322 }
4323 }
4324
4325 GV->setInitializer(Init);
4326 if (emitter)
4327 emitter->finalize(GV);
4328
4329 // If it is safe to mark the global 'constant', do so now.
4330 GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
4331 isTypeConstant(D->getType(), true));
4332
4333 // If it is in a read-only section, mark it 'constant'.
4334 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
4335 const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
4336 if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
4337 GV->setConstant(true);
4338 }
4339
4340 GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
4341
4342 // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
4343 // function is only defined alongside the variable, not also alongside
4344 // callers. Normally, all accesses to a thread_local go through the
4345 // thread-wrapper in order to ensure initialization has occurred, underlying
4346 // variable will never be used other than the thread-wrapper, so it can be
4347 // converted to internal linkage.
4348 //
4349 // However, if the variable has the 'constinit' attribute, it _can_ be
4350 // referenced directly, without calling the thread-wrapper, so the linkage
4351 // must not be changed.
4352 //
4353 // Additionally, if the variable isn't plain external linkage, e.g. if it's
4354 // weak or linkonce, the de-duplication semantics are important to preserve,
4355 // so we don't change the linkage.
4356 if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
4357 Linkage == llvm::GlobalValue::ExternalLinkage &&
4358 Context.getTargetInfo().getTriple().isOSDarwin() &&
4359 !D->hasAttr<ConstInitAttr>())
4360 Linkage = llvm::GlobalValue::InternalLinkage;
4361
4362 GV->setLinkage(Linkage);
4363 if (D->hasAttr<DLLImportAttr>())
4364 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
4365 else if (D->hasAttr<DLLExportAttr>())
4366 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
4367 else
4368 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
4369
4370 if (Linkage == llvm::GlobalVariable::CommonLinkage) {
4371 // common vars aren't constant even if declared const.
4372 GV->setConstant(false);
4373 // Tentative definition of global variables may be initialized with
4374 // non-zero null pointers. In this case they should have weak linkage
4375 // since common linkage must have zero initializer and must not have
4376 // explicit section therefore cannot have non-zero initial value.
4377 if (!GV->getInitializer()->isNullValue())
4378 GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
4379 }
4380
4381 setNonAliasAttributes(D, GV);
4382
4383 if (D->getTLSKind() && !GV->isThreadLocal()) {
4384 if (D->getTLSKind() == VarDecl::TLS_Dynamic)
4385 CXXThreadLocals.push_back(D);
4386 setTLSMode(GV, *D);
4387 }
4388
4389 maybeSetTrivialComdat(*D, *GV);
4390
4391 // Emit the initializer function if necessary.
4392 if (NeedsGlobalCtor || NeedsGlobalDtor)
4393 EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
4394
4395 SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor);
4396
4397 // Emit global variable debug information.
4398 if (CGDebugInfo *DI = getModuleDebugInfo())
4399 if (getCodeGenOpts().hasReducedDebugInfo())
4400 DI->EmitGlobalVariable(GV, D);
4401}
4402
4403void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
4404 if (CGDebugInfo *DI = getModuleDebugInfo())
4405 if (getCodeGenOpts().hasReducedDebugInfo()) {
4406 QualType ASTTy = D->getType();
4407 llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
4408 llvm::PointerType *PTy =
4409 llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
4410 llvm::Constant *GV = GetOrCreateLLVMGlobal(D->getName(), PTy, D);
4411 DI->EmitExternalVariable(
4412 cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
4413 }
4414}
4415
4416static bool isVarDeclStrongDefinition(const ASTContext &Context,
4417 CodeGenModule &CGM, const VarDecl *D,
4418 bool NoCommon) {
4419 // Don't give variables common linkage if -fno-common was specified unless it
4420 // was overridden by a NoCommon attribute.
4421 if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
4422 return true;
4423
4424 // C11 6.9.2/2:
4425 // A declaration of an identifier for an object that has file scope without
4426 // an initializer, and without a storage-class specifier or with the
4427 // storage-class specifier static, constitutes a tentative definition.
4428 if (D->getInit() || D->hasExternalStorage())
4429 return true;
4430
4431 // A variable cannot be both common and exist in a section.
4432 if (D->hasAttr<SectionAttr>())
4433 return true;
4434
4435 // A variable cannot be both common and exist in a section.
4436 // We don't try to determine which is the right section in the front-end.
4437 // If no specialized section name is applicable, it will resort to default.
4438 if (D->hasAttr<PragmaClangBSSSectionAttr>() ||
4439 D->hasAttr<PragmaClangDataSectionAttr>() ||
4440 D->hasAttr<PragmaClangRelroSectionAttr>() ||
4441 D->hasAttr<PragmaClangRodataSectionAttr>())
4442 return true;
4443
4444 // Thread local vars aren't considered common linkage.
4445 if (D->getTLSKind())
4446 return true;
4447
4448 // Tentative definitions marked with WeakImportAttr are true definitions.
4449 if (D->hasAttr<WeakImportAttr>())
4450 return true;
4451
4452 // A variable cannot be both common and exist in a comdat.
4453 if (shouldBeInCOMDAT(CGM, *D))
4454 return true;
4455
4456 // Declarations with a required alignment do not have common linkage in MSVC
4457 // mode.
4458 if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4459 if (D->hasAttr<AlignedAttr>())
4460 return true;
4461 QualType VarType = D->getType();
4462 if (Context.isAlignmentRequired(VarType))
4463 return true;
4464
4465 if (const auto *RT = VarType->getAs<RecordType>()) {
4466 const RecordDecl *RD = RT->getDecl();
4467 for (const FieldDecl *FD : RD->fields()) {
4468 if (FD->isBitField())
4469 continue;
4470 if (FD->hasAttr<AlignedAttr>())
4471 return true;
4472 if (Context.isAlignmentRequired(FD->getType()))
4473 return true;
4474 }
4475 }
4476 }
4477
4478 // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
4479 // common symbols, so symbols with greater alignment requirements cannot be
4480 // common.
4481 // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
4482 // alignments for common symbols via the aligncomm directive, so this
4483 // restriction only applies to MSVC environments.
4484 if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
4485 Context.getTypeAlignIfKnown(D->getType()) >
4486 Context.toBits(CharUnits::fromQuantity(32)))
4487 return true;
4488
4489 return false;
4490}
4491
4492llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator(
4493 const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) {
4494 if (Linkage == GVA_Internal)
4495 return llvm::Function::InternalLinkage;
4496
4497 if (D->hasAttr<WeakAttr>()) {
4498 if (IsConstantVariable)
4499 return llvm::GlobalVariable::WeakODRLinkage;
4500 else
4501 return llvm::GlobalVariable::WeakAnyLinkage;
4502 }
4503
4504 if (const auto *FD = D->getAsFunction())
4505 if (FD->isMultiVersion() && Linkage == GVA_AvailableExternally)
4506 return llvm::GlobalVariable::LinkOnceAnyLinkage;
4507
4508 // We are guaranteed to have a strong definition somewhere else,
4509 // so we can use available_externally linkage.
4510 if (Linkage == GVA_AvailableExternally)
4511 return llvm::GlobalValue::AvailableExternallyLinkage;
4512
4513 // Note that Apple's kernel linker doesn't support symbol
4514 // coalescing, so we need to avoid linkonce and weak linkages there.
4515 // Normally, this means we just map to internal, but for explicit
4516 // instantiations we'll map to external.
4517
4518 // In C++, the compiler has to emit a definition in every translation unit
4519 // that references the function. We should use linkonce_odr because
4520 // a) if all references in this translation unit are optimized away, we
4521 // don't need to codegen it. b) if the function persists, it needs to be
4522 // merged with other definitions. c) C++ has the ODR, so we know the
4523 // definition is dependable.
4524 if (Linkage == GVA_DiscardableODR)
4525 return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage
4526 : llvm::Function::InternalLinkage;
4527
4528 // An explicit instantiation of a template has weak linkage, since
4529 // explicit instantiations can occur in multiple translation units
4530 // and must all be equivalent. However, we are not allowed to
4531 // throw away these explicit instantiations.
4532 //
4533 // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU,
4534 // so say that CUDA templates are either external (for kernels) or internal.
4535 // This lets llvm perform aggressive inter-procedural optimizations. For
4536 // -fgpu-rdc case, device function calls across multiple TU's are allowed,
4537 // therefore we need to follow the normal linkage paradigm.
4538 if (Linkage == GVA_StrongODR) {
4539 if (getLangOpts().AppleKext)
4540 return llvm::Function::ExternalLinkage;
4541 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
4542 !getLangOpts().GPURelocatableDeviceCode)
4543 return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage
4544 : llvm::Function::InternalLinkage;
4545 return llvm::Function::WeakODRLinkage;
4546 }
4547
4548 // C++ doesn't have tentative definitions and thus cannot have common
4549 // linkage.
4550 if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) &&
4551 !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D),
4552 CodeGenOpts.NoCommon))
4553 return llvm::GlobalVariable::CommonLinkage;
4554
4555 // selectany symbols are externally visible, so use weak instead of
4556 // linkonce. MSVC optimizes away references to const selectany globals, so
4557 // all definitions should be the same and ODR linkage should be used.
4558 // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
4559 if (D->hasAttr<SelectAnyAttr>())
4560 return llvm::GlobalVariable::WeakODRLinkage;
4561
4562 // Otherwise, we have strong external linkage.
4563 assert(Linkage == GVA_StrongExternal)((Linkage == GVA_StrongExternal) ? static_cast<void> (0
) : __assert_fail ("Linkage == GVA_StrongExternal", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4563, __PRETTY_FUNCTION__))
;
4564 return llvm::GlobalVariable::ExternalLinkage;
4565}
4566
4567llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition(
4568 const VarDecl *VD, bool IsConstant) {
4569 GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD);
4570 return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant);
4571}
4572
4573/// Replace the uses of a function that was declared with a non-proto type.
4574/// We want to silently drop extra arguments from call sites
4575static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
4576 llvm::Function *newFn) {
4577 // Fast path.
4578 if (old->use_empty()) return;
4579
4580 llvm::Type *newRetTy = newFn->getReturnType();
4581 SmallVector<llvm::Value*, 4> newArgs;
4582 SmallVector<llvm::OperandBundleDef, 1> newBundles;
4583
4584 for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
4585 ui != ue; ) {
4586 llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
4587 llvm::User *user = use->getUser();
4588
4589 // Recognize and replace uses of bitcasts. Most calls to
4590 // unprototyped functions will use bitcasts.
4591 if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
4592 if (bitcast->getOpcode() == llvm::Instruction::BitCast)
4593 replaceUsesOfNonProtoConstant(bitcast, newFn);
4594 continue;
4595 }
4596
4597 // Recognize calls to the function.
4598 llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user);
4599 if (!callSite) continue;
4600 if (!callSite->isCallee(&*use))
4601 continue;
4602
4603 // If the return types don't match exactly, then we can't
4604 // transform this call unless it's dead.
4605 if (callSite->getType() != newRetTy && !callSite->use_empty())
4606 continue;
4607
4608 // Get the call site's attribute list.
4609 SmallVector<llvm::AttributeSet, 8> newArgAttrs;
4610 llvm::AttributeList oldAttrs = callSite->getAttributes();
4611
4612 // If the function was passed too few arguments, don't transform.
4613 unsigned newNumArgs = newFn->arg_size();
4614 if (callSite->arg_size() < newNumArgs)
4615 continue;
4616
4617 // If extra arguments were passed, we silently drop them.
4618 // If any of the types mismatch, we don't transform.
4619 unsigned argNo = 0;
4620 bool dontTransform = false;
4621 for (llvm::Argument &A : newFn->args()) {
4622 if (callSite->getArgOperand(argNo)->getType() != A.getType()) {
4623 dontTransform = true;
4624 break;
4625 }
4626
4627 // Add any parameter attributes.
4628 newArgAttrs.push_back(oldAttrs.getParamAttributes(argNo));
4629 argNo++;
4630 }
4631 if (dontTransform)
4632 continue;
4633
4634 // Okay, we can transform this. Create the new call instruction and copy
4635 // over the required information.
4636 newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo);
4637
4638 // Copy over any operand bundles.
4639 callSite->getOperandBundlesAsDefs(newBundles);
4640
4641 llvm::CallBase *newCall;
4642 if (dyn_cast<llvm::CallInst>(callSite)) {
4643 newCall =
4644 llvm::CallInst::Create(newFn, newArgs, newBundles, "", callSite);
4645 } else {
4646 auto *oldInvoke = cast<llvm::InvokeInst>(callSite);
4647 newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(),
4648 oldInvoke->getUnwindDest(), newArgs,
4649 newBundles, "", callSite);
4650 }
4651 newArgs.clear(); // for the next iteration
4652
4653 if (!newCall->getType()->isVoidTy())
4654 newCall->takeName(callSite);
4655 newCall->setAttributes(llvm::AttributeList::get(
4656 newFn->getContext(), oldAttrs.getFnAttributes(),
4657 oldAttrs.getRetAttributes(), newArgAttrs));
4658 newCall->setCallingConv(callSite->getCallingConv());
4659
4660 // Finally, remove the old call, replacing any uses with the new one.
4661 if (!callSite->use_empty())
4662 callSite->replaceAllUsesWith(newCall);
4663
4664 // Copy debug location attached to CI.
4665 if (callSite->getDebugLoc())
4666 newCall->setDebugLoc(callSite->getDebugLoc());
4667
4668 callSite->eraseFromParent();
4669 }
4670}
4671
4672/// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
4673/// implement a function with no prototype, e.g. "int foo() {}". If there are
4674/// existing call uses of the old function in the module, this adjusts them to
4675/// call the new function directly.
4676///
4677/// This is not just a cleanup: the always_inline pass requires direct calls to
4678/// functions to be able to inline them. If there is a bitcast in the way, it
4679/// won't inline them. Instcombine normally deletes these calls, but it isn't
4680/// run at -O0.
4681static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
4682 llvm::Function *NewFn) {
4683 // If we're redefining a global as a function, don't transform it.
4684 if (!isa<llvm::Function>(Old)) return;
4685
4686 replaceUsesOfNonProtoConstant(Old, NewFn);
4687}
4688
4689void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
4690 auto DK = VD->isThisDeclarationADefinition();
4691 if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>())
4692 return;
4693
4694 TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind();
4695 // If we have a definition, this might be a deferred decl. If the
4696 // instantiation is explicit, make sure we emit it at the end.
4697 if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition)
4698 GetAddrOfGlobalVar(VD);
4699
4700 EmitTopLevelDecl(VD);
4701}
4702
4703void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
4704 llvm::GlobalValue *GV) {
4705 const auto *D = cast<FunctionDecl>(GD.getDecl());
4706
4707 // Compute the function info and LLVM type.
4708 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
4709 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4710
4711 // Get or create the prototype for the function.
4712 if (!GV || (GV->getValueType() != Ty))
4713 GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false,
4714 /*DontDefer=*/true,
4715 ForDefinition));
4716
4717 // Already emitted.
4718 if (!GV->isDeclaration())
4719 return;
4720
4721 // We need to set linkage and visibility on the function before
4722 // generating code for it because various parts of IR generation
4723 // want to propagate this information down (e.g. to local static
4724 // declarations).
4725 auto *Fn = cast<llvm::Function>(GV);
4726 setFunctionLinkage(GD, Fn);
4727
4728 // FIXME: this is redundant with part of setFunctionDefinitionAttributes
4729 setGVProperties(Fn, GD);
4730
4731 MaybeHandleStaticInExternC(D, Fn);
4732
4733 maybeSetTrivialComdat(*D, *Fn);
4734
4735 // Set CodeGen attributes that represent floating point environment.
4736 setLLVMFunctionFEnvAttributes(D, Fn);
4737
4738 CodeGenFunction(*this).GenerateCode(GD, Fn, FI);
4739
4740 setNonAliasAttributes(GD, Fn);
4741 SetLLVMFunctionAttributesForDefinition(D, Fn);
4742
4743 if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
4744 AddGlobalCtor(Fn, CA->getPriority());
4745 if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
4746 AddGlobalDtor(Fn, DA->getPriority(), true);
4747 if (D->hasAttr<AnnotateAttr>())
4748 AddGlobalAnnotations(D, Fn);
4749}
4750
4751void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
4752 const auto *D = cast<ValueDecl>(GD.getDecl());
4753 const AliasAttr *AA = D->getAttr<AliasAttr>();
4754 assert(AA && "Not an alias?")((AA && "Not an alias?") ? static_cast<void> (0
) : __assert_fail ("AA && \"Not an alias?\"", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4754, __PRETTY_FUNCTION__))
;
4755
4756 StringRef MangledName = getMangledName(GD);
4757
4758 if (AA->getAliasee() == MangledName) {
4759 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
4760 return;
4761 }
4762
4763 // If there is a definition in the module, then it wins over the alias.
4764 // This is dubious, but allow it to be safe. Just ignore the alias.
4765 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4766 if (Entry && !Entry->isDeclaration())
4767 return;
4768
4769 Aliases.push_back(GD);
4770
4771 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
4772
4773 // Create a reference to the named value. This ensures that it is emitted
4774 // if a deferred decl.
4775 llvm::Constant *Aliasee;
4776 llvm::GlobalValue::LinkageTypes LT;
4777 if (isa<llvm::FunctionType>(DeclTy)) {
4778 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
4779 /*ForVTable=*/false);
4780 LT = getFunctionLinkage(GD);
4781 } else {
4782 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
4783 llvm::PointerType::getUnqual(DeclTy),
4784 /*D=*/nullptr);
4785 if (const auto *VD = dyn_cast<VarDecl>(GD.getDecl()))
4786 LT = getLLVMLinkageVarDefinition(VD, D->getType().isConstQualified());
4787 else
4788 LT = getFunctionLinkage(GD);
4789 }
4790
4791 // Create the new alias itself, but don't set a name yet.
4792 unsigned AS = Aliasee->getType()->getPointerAddressSpace();
4793 auto *GA =
4794 llvm::GlobalAlias::create(DeclTy, AS, LT, "", Aliasee, &getModule());
4795
4796 if (Entry) {
4797 if (GA->getAliasee() == Entry) {
4798 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
4799 return;
4800 }
4801
4802 assert(Entry->isDeclaration())((Entry->isDeclaration()) ? static_cast<void> (0) : __assert_fail
("Entry->isDeclaration()", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4802, __PRETTY_FUNCTION__))
;
4803
4804 // If there is a declaration in the module, then we had an extern followed
4805 // by the alias, as in:
4806 // extern int test6();
4807 // ...
4808 // int test6() __attribute__((alias("test7")));
4809 //
4810 // Remove it and replace uses of it with the alias.
4811 GA->takeName(Entry);
4812
4813 Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
4814 Entry->getType()));
4815 Entry->eraseFromParent();
4816 } else {
4817 GA->setName(MangledName);
4818 }
4819
4820 // Set attributes which are particular to an alias; this is a
4821 // specialization of the attributes which may be set on a global
4822 // variable/function.
4823 if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
4824 D->isWeakImported()) {
4825 GA->setLinkage(llvm::Function::WeakAnyLinkage);
4826 }
4827
4828 if (const auto *VD = dyn_cast<VarDecl>(D))
4829 if (VD->getTLSKind())
4830 setTLSMode(GA, *VD);
4831
4832 SetCommonAttributes(GD, GA);
4833}
4834
4835void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
4836 const auto *D = cast<ValueDecl>(GD.getDecl());
4837 const IFuncAttr *IFA = D->getAttr<IFuncAttr>();
4838 assert(IFA && "Not an ifunc?")((IFA && "Not an ifunc?") ? static_cast<void> (
0) : __assert_fail ("IFA && \"Not an ifunc?\"", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4838, __PRETTY_FUNCTION__))
;
4839
4840 StringRef MangledName = getMangledName(GD);
4841
4842 if (IFA->getResolver() == MangledName) {
4843 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
4844 return;
4845 }
4846
4847 // Report an error if some definition overrides ifunc.
4848 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4849 if (Entry && !Entry->isDeclaration()) {
4850 GlobalDecl OtherGD;
4851 if (lookupRepresentativeDecl(MangledName, OtherGD) &&
4852 DiagnosedConflictingDefinitions.insert(GD).second) {
4853 Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name)
4854 << MangledName;
4855 Diags.Report(OtherGD.getDecl()->getLocation(),
4856 diag::note_previous_definition);
4857 }
4858 return;
4859 }
4860
4861 Aliases.push_back(GD);
4862
4863 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
4864 llvm::Constant *Resolver =
4865 GetOrCreateLLVMFunction(IFA->getResolver(), DeclTy, GD,
4866 /*ForVTable=*/false);
4867 llvm::GlobalIFunc *GIF =
4868 llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage,
4869 "", Resolver, &getModule());
4870 if (Entry) {
4871 if (GIF->getResolver() == Entry) {
4872 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
4873 return;
4874 }
4875 assert(Entry->isDeclaration())((Entry->isDeclaration()) ? static_cast<void> (0) : __assert_fail
("Entry->isDeclaration()", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 4875, __PRETTY_FUNCTION__))
;
4876
4877 // If there is a declaration in the module, then we had an extern followed
4878 // by the ifunc, as in:
4879 // extern int test();
4880 // ...
4881 // int test() __attribute__((ifunc("resolver")));
4882 //
4883 // Remove it and replace uses of it with the ifunc.
4884 GIF->takeName(Entry);
4885
4886 Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GIF,
4887 Entry->getType()));
4888 Entry->eraseFromParent();
4889 } else
4890 GIF->setName(MangledName);
4891
4892 SetCommonAttributes(GD, GIF);
4893}
4894
4895llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
4896 ArrayRef<llvm::Type*> Tys) {
4897 return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID,
4898 Tys);
4899}
4900
4901static llvm::StringMapEntry<llvm::GlobalVariable *> &
4902GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map,
4903 const StringLiteral *Literal, bool TargetIsLSB,
4904 bool &IsUTF16, unsigned &StringLength) {
4905 StringRef String = Literal->getString();
4906 unsigned NumBytes = String.size();
4907
4908 // Check for simple case.
4909 if (!Literal->containsNonAsciiOrNull()) {
4910 StringLength = NumBytes;
4911 return *Map.insert(std::make_pair(String, nullptr)).first;
4912 }
4913
4914 // Otherwise, convert the UTF8 literals into a string of shorts.
4915 IsUTF16 = true;
4916
4917 SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
4918 const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data();
4919 llvm::UTF16 *ToPtr = &ToBuf[0];
4920
4921 (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr,
4922 ToPtr + NumBytes, llvm::strictConversion);
4923
4924 // ConvertUTF8toUTF16 returns the length in ToPtr.
4925 StringLength = ToPtr - &ToBuf[0];
4926
4927 // Add an explicit null.
4928 *ToPtr = 0;
4929 return *Map.insert(std::make_pair(
4930 StringRef(reinterpret_cast<const char *>(ToBuf.data()),
4931 (StringLength + 1) * 2),
4932 nullptr)).first;
4933}
4934
4935ConstantAddress
4936CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
4937 unsigned StringLength = 0;
4938 bool isUTF16 = false;
4939 llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
4940 GetConstantCFStringEntry(CFConstantStringMap, Literal,
4941 getDataLayout().isLittleEndian(), isUTF16,
4942 StringLength);
4943
4944 if (auto *C = Entry.second)
4945 return ConstantAddress(C, CharUnits::fromQuantity(C->getAlignment()));
4946
4947 llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
4948 llvm::Constant *Zeros[] = { Zero, Zero };
4949
4950 const ASTContext &Context = getContext();
4951 const llvm::Triple &Triple = getTriple();
4952
4953 const auto CFRuntime = getLangOpts().CFRuntime;
4954 const bool IsSwiftABI =
4955 static_cast<unsigned>(CFRuntime) >=
4956 static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift);
4957 const bool IsSwift4_1 = CFRuntime == LangOptions::CoreFoundationABI::Swift4_1;
4958
4959 // If we don't already have it, get __CFConstantStringClassReference.
4960 if (!CFConstantStringClassRef) {
4961 const char *CFConstantStringClassName = "__CFConstantStringClassReference";
4962 llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
4963 Ty = llvm::ArrayType::get(Ty, 0);
4964
4965 switch (CFRuntime) {
4966 default: break;
4967 case LangOptions::CoreFoundationABI::Swift: LLVM_FALLTHROUGH[[gnu::fallthrough]];
4968 case LangOptions::CoreFoundationABI::Swift5_0:
4969 CFConstantStringClassName =
4970 Triple.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN"
4971 : "$s10Foundation19_NSCFConstantStringCN";
4972 Ty = IntPtrTy;
4973 break;
4974 case LangOptions::CoreFoundationABI::Swift4_2:
4975 CFConstantStringClassName =
4976 Triple.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN"
4977 : "$S10Foundation19_NSCFConstantStringCN";
4978 Ty = IntPtrTy;
4979 break;
4980 case LangOptions::CoreFoundationABI::Swift4_1:
4981 CFConstantStringClassName =
4982 Triple.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN"
4983 : "__T010Foundation19_NSCFConstantStringCN";
4984 Ty = IntPtrTy;
4985 break;
4986 }
4987
4988 llvm::Constant *C = CreateRuntimeVariable(Ty, CFConstantStringClassName);
4989
4990 if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) {
4991 llvm::GlobalValue *GV = nullptr;
4992
4993 if ((GV = dyn_cast<llvm::GlobalValue>(C))) {
4994 IdentifierInfo &II = Context.Idents.get(GV->getName());
4995 TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();
4996 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
4997
4998 const VarDecl *VD = nullptr;
4999 for (const auto &Result : DC->lookup(&II))
5000 if ((VD = dyn_cast<VarDecl>(Result)))
5001 break;
5002
5003 if (Triple.isOSBinFormatELF()) {
5004 if (!VD)
5005 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5006 } else {
5007 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5008 if (!VD || !VD->hasAttr<DLLExportAttr>())
5009 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
5010 else
5011 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
5012 }
5013
5014 setDSOLocal(GV);
5015 }
5016 }
5017
5018 // Decay array -> ptr
5019 CFConstantStringClassRef =
5020 IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty)
5021 : llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros);
5022 }
5023
5024 QualType CFTy = Context.getCFConstantStringType();
5025
5026 auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy));
5027
5028 ConstantInitBuilder Builder(*this);
5029 auto Fields = Builder.beginStruct(STy);
5030
5031 // Class pointer.
5032 Fields.add(cast<llvm::ConstantExpr>(CFConstantStringClassRef));
5033
5034 // Flags.
5035 if (IsSwiftABI) {
5036 Fields.addInt(IntPtrTy, IsSwift4_1 ? 0x05 : 0x01);
5037 Fields.addInt(Int64Ty, isUTF16 ? 0x07d0 : 0x07c8);
5038 } else {
5039 Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8);
5040 }
5041
5042 // String pointer.
5043 llvm::Constant *C = nullptr;
5044 if (isUTF16) {
5045 auto Arr = llvm::makeArrayRef(
5046 reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())),
5047 Entry.first().size() / 2);
5048 C = llvm::ConstantDataArray::get(VMContext, Arr);
5049 } else {
5050 C = llvm::ConstantDataArray::getString(VMContext, Entry.first());
5051 }
5052
5053 // Note: -fwritable-strings doesn't make the backing store strings of
5054 // CFStrings writable. (See <rdar://problem/10657500>)
5055 auto *GV =
5056 new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
5057 llvm::GlobalValue::PrivateLinkage, C, ".str");
5058 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
5059 // Don't enforce the target's minimum global alignment, since the only use
5060 // of the string is via this class initializer.
5061 CharUnits Align = isUTF16 ? Context.getTypeAlignInChars(Context.ShortTy)
5062 : Context.getTypeAlignInChars(Context.CharTy);
5063 GV->setAlignment(Align.getAsAlign());
5064
5065 // FIXME: We set the section explicitly to avoid a bug in ld64 224.1.
5066 // Without it LLVM can merge the string with a non unnamed_addr one during
5067 // LTO. Doing that changes the section it ends in, which surprises ld64.
5068 if (Triple.isOSBinFormatMachO())
5069 GV->setSection(isUTF16 ? "__TEXT,__ustring"
5070 : "__TEXT,__cstring,cstring_literals");
5071 // Make sure the literal ends up in .rodata to allow for safe ICF and for
5072 // the static linker to adjust permissions to read-only later on.
5073 else if (Triple.isOSBinFormatELF())
5074 GV->setSection(".rodata");
5075
5076 // String.
5077 llvm::Constant *Str =
5078 llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros);
5079
5080 if (isUTF16)
5081 // Cast the UTF16 string to the correct type.
5082 Str = llvm::ConstantExpr::getBitCast(Str, Int8PtrTy);
5083 Fields.add(Str);
5084
5085 // String length.
5086 llvm::IntegerType *LengthTy =
5087 llvm::IntegerType::get(getModule().getContext(),
5088 Context.getTargetInfo().getLongWidth());
5089 if (IsSwiftABI) {
5090 if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 ||
5091 CFRuntime == LangOptions::CoreFoundationABI::Swift4_2)
5092 LengthTy = Int32Ty;
5093 else
5094 LengthTy = IntPtrTy;
5095 }
5096 Fields.addInt(LengthTy, StringLength);
5097
5098 // Swift ABI requires 8-byte alignment to ensure that the _Atomic(uint64_t) is
5099 // properly aligned on 32-bit platforms.
5100 CharUnits Alignment =
5101 IsSwiftABI ? Context.toCharUnitsFromBits(64) : getPointerAlign();
5102
5103 // The struct.
5104 GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment,
5105 /*isConstant=*/false,
5106 llvm::GlobalVariable::PrivateLinkage);
5107 GV->addAttribute("objc_arc_inert");
5108 switch (Triple.getObjectFormat()) {
5109 case llvm::Triple::UnknownObjectFormat:
5110 llvm_unreachable("unknown file format")::llvm::llvm_unreachable_internal("unknown file format", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 5110)
;
5111 case llvm::Triple::GOFF:
5112 llvm_unreachable("GOFF is not yet implemented")::llvm::llvm_unreachable_internal("GOFF is not yet implemented"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 5112)
;
5113 case llvm::Triple::XCOFF:
5114 llvm_unreachable("XCOFF is not yet implemented")::llvm::llvm_unreachable_internal("XCOFF is not yet implemented"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 5114)
;
5115 case llvm::Triple::COFF:
5116 case llvm::Triple::ELF:
5117 case llvm::Triple::Wasm:
5118 GV->setSection("cfstring");
5119 break;
5120 case llvm::Triple::MachO:
5121 GV->setSection("__DATA,__cfstring");
5122 break;
5123 }
5124 Entry.second = GV;
5125
5126 return ConstantAddress(GV, Alignment);
5127}
5128
5129bool CodeGenModule::getExpressionLocationsEnabled() const {
5130 return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo;
5131}
5132
5133QualType CodeGenModule::getObjCFastEnumerationStateType() {
5134 if (ObjCFastEnumerationStateType.isNull()) {
5135 RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState");
5136 D->startDefinition();
5137
5138 QualType FieldTypes[] = {
5139 Context.UnsignedLongTy,
5140 Context.getPointerType(Context.getObjCIdType()),
5141 Context.getPointerType(Context.UnsignedLongTy),
5142 Context.getConstantArrayType(Context.UnsignedLongTy,
5143 llvm::APInt(32, 5), nullptr, ArrayType::Normal, 0)
5144 };
5145
5146 for (size_t i = 0; i < 4; ++i) {
5147 FieldDecl *Field = FieldDecl::Create(Context,
5148 D,
5149 SourceLocation(),
5150 SourceLocation(), nullptr,
5151 FieldTypes[i], /*TInfo=*/nullptr,
5152 /*BitWidth=*/nullptr,
5153 /*Mutable=*/false,
5154 ICIS_NoInit);
5155 Field->setAccess(AS_public);
5156 D->addDecl(Field);
5157 }
5158
5159 D->completeDefinition();
5160 ObjCFastEnumerationStateType = Context.getTagDeclType(D);
5161 }
5162
5163 return ObjCFastEnumerationStateType;
5164}
5165
5166llvm::Constant *
5167CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) {
5168 assert(!E->getType()->isPointerType() && "Strings are always arrays")((!E->getType()->isPointerType() && "Strings are always arrays"
) ? static_cast<void> (0) : __assert_fail ("!E->getType()->isPointerType() && \"Strings are always arrays\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 5168, __PRETTY_FUNCTION__))
;
5169
5170 // Don't emit it as the address of the string, emit the string data itself
5171 // as an inline array.
5172 if (E->getCharByteWidth() == 1) {
5173 SmallString<64> Str(E->getString());
5174
5175 // Resize the string to the right size, which is indicated by its type.
5176 const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
5177 Str.resize(CAT->getSize().getZExtValue());
5178 return llvm::ConstantDataArray::getString(VMContext, Str, false);
5179 }
5180
5181 auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
5182 llvm::Type *ElemTy = AType->getElementType();
5183 unsigned NumElements = AType->getNumElements();
5184
5185 // Wide strings have either 2-byte or 4-byte elements.
5186 if (ElemTy->getPrimitiveSizeInBits() == 16) {
5187 SmallVector<uint16_t, 32> Elements;
5188 Elements.reserve(NumElements);
5189
5190 for(unsigned i = 0, e = E->getLength(); i != e; ++i)
5191 Elements.push_back(E->getCodeUnit(i));
5192 Elements.resize(NumElements);
5193 return llvm::ConstantDataArray::get(VMContext, Elements);
5194 }
5195
5196 assert(ElemTy->getPrimitiveSizeInBits() == 32)((ElemTy->getPrimitiveSizeInBits() == 32) ? static_cast<
void> (0) : __assert_fail ("ElemTy->getPrimitiveSizeInBits() == 32"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 5196, __PRETTY_FUNCTION__))
;
5197 SmallVector<uint32_t, 32> Elements;
5198 Elements.reserve(NumElements);
5199
5200 for(unsigned i = 0, e = E->getLength(); i != e; ++i)
5201 Elements.push_back(E->getCodeUnit(i));
5202 Elements.resize(NumElements);
5203 return llvm::ConstantDataArray::get(VMContext, Elements);
5204}
5205
5206static llvm::GlobalVariable *
5207GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT,
5208 CodeGenModule &CGM, StringRef GlobalName,
5209 CharUnits Alignment) {
5210 unsigned AddrSpace = CGM.getContext().getTargetAddressSpace(
5211 CGM.getStringLiteralAddressSpace());
5212
5213 llvm::Module &M = CGM.getModule();
5214 // Create a global variable for this string
5215 auto *GV = new llvm::GlobalVariable(
5216 M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName,
5217 nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace);
5218 GV->setAlignment(Alignment.getAsAlign());
5219 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
5220 if (GV->isWeakForLinker()) {
5221 assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals")((CGM.supportsCOMDAT() && "Only COFF uses weak string literals"
) ? static_cast<void> (0) : __assert_fail ("CGM.supportsCOMDAT() && \"Only COFF uses weak string literals\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 5221, __PRETTY_FUNCTION__))
;
5222 GV->setComdat(M.getOrInsertComdat(GV->getName()));
5223 }
5224 CGM.setDSOLocal(GV);
5225
5226 return GV;
5227}
5228
5229/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
5230/// constant array for the given string literal.
5231ConstantAddress
5232CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
5233 StringRef Name) {
5234 CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType());
5235
5236 llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
5237 llvm::GlobalVariable **Entry = nullptr;
5238 if (!LangOpts.WritableStrings) {
5239 Entry = &ConstantStringMap[C];
5240 if (auto GV = *Entry) {
5241 if (Alignment.getQuantity() > GV->getAlignment())
5242 GV->setAlignment(Alignment.getAsAlign());
5243 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
5244 Alignment);
5245 }
5246 }
5247
5248 SmallString<256> MangledNameBuffer;
5249 StringRef GlobalVariableName;
5250 llvm::GlobalValue::LinkageTypes LT;
5251
5252 // Mangle the string literal if that's how the ABI merges duplicate strings.
5253 // Don't do it if they are writable, since we don't want writes in one TU to
5254 // affect strings in another.
5255 if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) &&
5256 !LangOpts.WritableStrings) {
5257 llvm::raw_svector_ostream Out(MangledNameBuffer);
5258 getCXXABI().getMangleContext().mangleStringLiteral(S, Out);
5259 LT = llvm::GlobalValue::LinkOnceODRLinkage;
5260 GlobalVariableName = MangledNameBuffer;
5261 } else {
5262 LT = llvm::GlobalValue::PrivateLinkage;
5263 GlobalVariableName = Name;
5264 }
5265
5266 auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment);
5267 if (Entry)
5268 *Entry = GV;
5269
5270 SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>",
5271 QualType());
5272
5273 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
5274 Alignment);
5275}
5276
5277/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
5278/// array for the given ObjCEncodeExpr node.
5279ConstantAddress
5280CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
5281 std::string Str;
5282 getContext().getObjCEncodingForType(E->getEncodedType(), Str);
5283
5284 return GetAddrOfConstantCString(Str);
5285}
5286
5287/// GetAddrOfConstantCString - Returns a pointer to a character array containing
5288/// the literal and a terminating '\0' character.
5289/// The result has pointer to array type.
5290ConstantAddress CodeGenModule::GetAddrOfConstantCString(
5291 const std::string &Str, const char *GlobalName) {
5292 StringRef StrWithNull(Str.c_str(), Str.size() + 1);
5293 CharUnits Alignment =
5294 getContext().getAlignOfGlobalVarInChars(getContext().CharTy);
5295
5296 llvm::Constant *C =
5297 llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);
5298
5299 // Don't share any string literals if strings aren't constant.
5300 llvm::GlobalVariable **Entry = nullptr;
5301 if (!LangOpts.WritableStrings) {
5302 Entry = &ConstantStringMap[C];
5303 if (auto GV = *Entry) {
5304 if (Alignment.getQuantity() > GV->getAlignment())
5305 GV->setAlignment(Alignment.getAsAlign());
5306 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
5307 Alignment);
5308 }
5309 }
5310
5311 // Get the default prefix if a name wasn't specified.
5312 if (!GlobalName)
5313 GlobalName = ".str";
5314 // Create a global variable for this.
5315 auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,
5316 GlobalName, Alignment);
5317 if (Entry)
5318 *Entry = GV;
5319
5320 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
5321 Alignment);
5322}
5323
5324ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary(
5325 const MaterializeTemporaryExpr *E, const Expr *Init) {
5326 assert((E->getStorageDuration() == SD_Static ||(((E->getStorageDuration() == SD_Static || E->getStorageDuration
() == SD_Thread) && "not a global temporary") ? static_cast
<void> (0) : __assert_fail ("(E->getStorageDuration() == SD_Static || E->getStorageDuration() == SD_Thread) && \"not a global temporary\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 5327, __PRETTY_FUNCTION__))
5327 E->getStorageDuration() == SD_Thread) && "not a global temporary")(((E->getStorageDuration() == SD_Static || E->getStorageDuration
() == SD_Thread) && "not a global temporary") ? static_cast
<void> (0) : __assert_fail ("(E->getStorageDuration() == SD_Static || E->getStorageDuration() == SD_Thread) && \"not a global temporary\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 5327, __PRETTY_FUNCTION__))
;
5328 const auto *VD = cast<VarDecl>(E->getExtendingDecl());
5329
5330 // If we're not materializing a subobject of the temporary, keep the
5331 // cv-qualifiers from the type of the MaterializeTemporaryExpr.
5332 QualType MaterializedType = Init->getType();
5333 if (Init == E->getSubExpr())
5334 MaterializedType = E->getType();
5335
5336 CharUnits Align = getContext().getTypeAlignInChars(MaterializedType);
5337
5338 if (llvm::Constant *Slot = MaterializedGlobalTemporaryMap[E])
5339 return ConstantAddress(Slot, Align);
5340
5341 // FIXME: If an externally-visible declaration extends multiple temporaries,
5342 // we need to give each temporary the same name in every translation unit (and
5343 // we also need to make the temporaries externally-visible).
5344 SmallString<256> Name;
5345 llvm::raw_svector_ostream Out(Name);
5346 getCXXABI().getMangleContext().mangleReferenceTemporary(
5347 VD, E->getManglingNumber(), Out);
5348
5349 APValue *Value = nullptr;
5350 if (E->getStorageDuration() == SD_Static && VD && VD->evaluateValue()) {
5351 // If the initializer of the extending declaration is a constant
5352 // initializer, we should have a cached constant initializer for this
5353 // temporary. Note that this might have a different value from the value
5354 // computed by evaluating the initializer if the surrounding constant
5355 // expression modifies the temporary.
5356 Value = E->getOrCreateValue(false);
5357 }
5358
5359 // Try evaluating it now, it might have a constant initializer.
5360 Expr::EvalResult EvalResult;
5361 if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
5362 !EvalResult.hasSideEffects())
5363 Value = &EvalResult.Val;
5364
5365 LangAS AddrSpace =
5366 VD ? GetGlobalVarAddressSpace(VD) : MaterializedType.getAddressSpace();
5367
5368 Optional<ConstantEmitter> emitter;
5369 llvm::Constant *InitialValue = nullptr;
5370 bool Constant = false;
5371 llvm::Type *Type;
5372 if (Value) {
5373 // The temporary has a constant initializer, use it.
5374 emitter.emplace(*this);
5375 InitialValue = emitter->emitForInitializer(*Value, AddrSpace,
5376 MaterializedType);
5377 Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value);
5378 Type = InitialValue->getType();
5379 } else {
5380 // No initializer, the initialization will be provided when we
5381 // initialize the declaration which performed lifetime extension.
5382 Type = getTypes().ConvertTypeForMem(MaterializedType);
5383 }
5384
5385 // Create a global variable for this lifetime-extended temporary.
5386 llvm::GlobalValue::LinkageTypes Linkage =
5387 getLLVMLinkageVarDefinition(VD, Constant);
5388 if (Linkage == llvm::GlobalVariable::ExternalLinkage) {
5389 const VarDecl *InitVD;
5390 if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) &&
5391 isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) {
5392 // Temporaries defined inside a class get linkonce_odr linkage because the
5393 // class can be defined in multiple translation units.
5394 Linkage = llvm::GlobalVariable::LinkOnceODRLinkage;
5395 } else {
5396 // There is no need for this temporary to have external linkage if the
5397 // VarDecl has external linkage.
5398 Linkage = llvm::GlobalVariable::InternalLinkage;
5399 }
5400 }
5401 auto TargetAS = getContext().getTargetAddressSpace(AddrSpace);
5402 auto *GV = new llvm::GlobalVariable(
5403 getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(),
5404 /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
5405 if (emitter) emitter->finalize(GV);
5406 setGVProperties(GV, VD);
5407 GV->setAlignment(Align.getAsAlign());
5408 if (supportsCOMDAT() && GV->isWeakForLinker())
5409 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
5410 if (VD->getTLSKind())
5411 setTLSMode(GV, *VD);
5412 llvm::Constant *CV = GV;
5413 if (AddrSpace != LangAS::Default)
5414 CV = getTargetCodeGenInfo().performAddrSpaceCast(
5415 *this, GV, AddrSpace, LangAS::Default,
5416 Type->getPointerTo(
5417 getContext().getTargetAddressSpace(LangAS::Default)));
5418 MaterializedGlobalTemporaryMap[E] = CV;
5419 return ConstantAddress(CV, Align);
5420}
5421
5422/// EmitObjCPropertyImplementations - Emit information for synthesized
5423/// properties for an implementation.
5424void CodeGenModule::EmitObjCPropertyImplementations(const
5425 ObjCImplementationDecl *D) {
5426 for (const auto *PID : D->property_impls()) {
5427 // Dynamic is just for type-checking.
5428 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
5429 ObjCPropertyDecl *PD = PID->getPropertyDecl();
5430
5431 // Determine which methods need to be implemented, some may have
5432 // been overridden. Note that ::isPropertyAccessor is not the method
5433 // we want, that just indicates if the decl came from a
5434 // property. What we want to know is if the method is defined in
5435 // this implementation.
5436 auto *Getter = PID->getGetterMethodDecl();
5437 if (!Getter || Getter->isSynthesizedAccessorStub())
5438 CodeGenFunction(*this).GenerateObjCGetter(
5439 const_cast<ObjCImplementationDecl *>(D), PID);
5440 auto *Setter = PID->getSetterMethodDecl();
5441 if (!PD->isReadOnly() && (!Setter || Setter->isSynthesizedAccessorStub()))
5442 CodeGenFunction(*this).GenerateObjCSetter(
5443 const_cast<ObjCImplementationDecl *>(D), PID);
5444 }
5445 }
5446}
5447
5448static bool needsDestructMethod(ObjCImplementationDecl *impl) {
5449 const ObjCInterfaceDecl *iface = impl->getClassInterface();
5450 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
5451 ivar; ivar = ivar->getNextIvar())
5452 if (ivar->getType().isDestructedType())
5453 return true;
5454
5455 return false;
5456}
5457
5458static bool AllTrivialInitializers(CodeGenModule &CGM,
5459 ObjCImplementationDecl *D) {
5460 CodeGenFunction CGF(CGM);
5461 for (ObjCImplementationDecl::init_iterator B = D->init_begin(),
5462 E = D->init_end(); B != E; ++B) {
5463 CXXCtorInitializer *CtorInitExp = *B;
5464 Expr *Init = CtorInitExp->getInit();
5465 if (!CGF.isTrivialInitializer(Init))
5466 return false;
5467 }
5468 return true;
5469}
5470
5471/// EmitObjCIvarInitializations - Emit information for ivar initialization
5472/// for an implementation.
5473void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
5474 // We might need a .cxx_destruct even if we don't have any ivar initializers.
5475 if (needsDestructMethod(D)) {
5476 IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
5477 Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
5478 ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(
5479 getContext(), D->getLocation(), D->getLocation(), cxxSelector,
5480 getContext().VoidTy, nullptr, D,
5481 /*isInstance=*/true, /*isVariadic=*/false,
5482 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
5483 /*isImplicitlyDeclared=*/true,
5484 /*isDefined=*/false, ObjCMethodDecl::Required);
5485 D->addInstanceMethod(DTORMethod);
5486 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
5487 D->setHasDestructors(true);
5488 }
5489
5490 // If the implementation doesn't have any ivar initializers, we don't need
5491 // a .cxx_construct.
5492 if (D->getNumIvarInitializers() == 0 ||
5493 AllTrivialInitializers(*this, D))
5494 return;
5495
5496 IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
5497 Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
5498 // The constructor returns 'self'.
5499 ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(
5500 getContext(), D->getLocation(), D->getLocation(), cxxSelector,
5501 getContext().getObjCIdType(), nullptr, D, /*isInstance=*/true,
5502 /*isVariadic=*/false,
5503 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
5504 /*isImplicitlyDeclared=*/true,
5505 /*isDefined=*/false, ObjCMethodDecl::Required);
5506 D->addInstanceMethod(CTORMethod);
5507 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
5508 D->setHasNonZeroConstructors(true);
5509}
5510
5511// EmitLinkageSpec - Emit all declarations in a linkage spec.
5512void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
5513 if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
5514 LSD->getLanguage() != LinkageSpecDecl::lang_cxx) {
5515 ErrorUnsupported(LSD, "linkage spec");
5516 return;
5517 }
5518
5519 EmitDeclContext(LSD);
5520}
5521
5522void CodeGenModule::EmitDeclContext(const DeclContext *DC) {
5523 for (auto *I : DC->decls()) {
5524 // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
5525 // are themselves considered "top-level", so EmitTopLevelDecl on an
5526 // ObjCImplDecl does not recursively visit them. We need to do that in
5527 // case they're nested inside another construct (LinkageSpecDecl /
5528 // ExportDecl) that does stop them from being considered "top-level".
5529 if (auto *OID = dyn_cast<ObjCImplDecl>(I)) {
5530 for (auto *M : OID->methods())
5531 EmitTopLevelDecl(M);
5532 }
5533
5534 EmitTopLevelDecl(I);
5535 }
5536}
5537
5538/// EmitTopLevelDecl - Emit code for a single top level declaration.
5539void CodeGenModule::EmitTopLevelDecl(Decl *D) {
5540 // Ignore dependent declarations.
5541 if (D->isTemplated())
5542 return;
5543
5544 // Consteval function shouldn't be emitted.
5545 if (auto *FD = dyn_cast<FunctionDecl>(D))
5546 if (FD->isConsteval())
5547 return;
5548
5549 switch (D->getKind()) {
5550 case Decl::CXXConversion:
5551 case Decl::CXXMethod:
5552 case Decl::Function:
5553 EmitGlobal(cast<FunctionDecl>(D));
5554 // Always provide some coverage mapping
5555 // even for the functions that aren't emitted.
5556 AddDeferredUnusedCoverageMapping(D);
5557 break;
5558
5559 case Decl::CXXDeductionGuide:
5560 // Function-like, but does not result in code emission.
5561 break;
5562
5563 case Decl::Var:
5564 case Decl::Decomposition:
5565 case Decl::VarTemplateSpecialization:
5566 EmitGlobal(cast<VarDecl>(D));
5567 if (auto *DD = dyn_cast<DecompositionDecl>(D))
5568 for (auto *B : DD->bindings())
5569 if (auto *HD = B->getHoldingVar())
5570 EmitGlobal(HD);
5571 break;
5572
5573 // Indirect fields from global anonymous structs and unions can be
5574 // ignored; only the actual variable requires IR gen support.
5575 case Decl::IndirectField:
5576 break;
5577
5578 // C++ Decls
5579 case Decl::Namespace:
5580 EmitDeclContext(cast<NamespaceDecl>(D));
5581 break;
5582 case Decl::ClassTemplateSpecialization: {
5583 const auto *Spec = cast<ClassTemplateSpecializationDecl>(D);
5584 if (CGDebugInfo *DI = getModuleDebugInfo())
5585 if (Spec->getSpecializationKind() ==
5586 TSK_ExplicitInstantiationDefinition &&
5587 Spec->hasDefinition())
5588 DI->completeTemplateDefinition(*Spec);
5589 } LLVM_FALLTHROUGH[[gnu::fallthrough]];
5590 case Decl::CXXRecord: {
5591 CXXRecordDecl *CRD = cast<CXXRecordDecl>(D);
5592 if (CGDebugInfo *DI = getModuleDebugInfo()) {
5593 if (CRD->hasDefinition())
5594 DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D)));
5595 if (auto *ES = D->getASTContext().getExternalSource())
5596 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
5597 DI->completeUnusedClass(*CRD);
5598 }
5599 // Emit any static data members, they may be definitions.
5600 for (auto *I : CRD->decls())
5601 if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I))
5602 EmitTopLevelDecl(I);
5603 break;
5604 }
5605 // No code generation needed.
5606 case Decl::UsingShadow:
5607 case Decl::ClassTemplate:
5608 case Decl::VarTemplate:
5609 case Decl::Concept:
5610 case Decl::VarTemplatePartialSpecialization:
5611 case Decl::FunctionTemplate:
5612 case Decl::TypeAliasTemplate:
5613 case Decl::Block:
5614 case Decl::Empty:
5615 case Decl::Binding:
5616 break;
5617 case Decl::Using: // using X; [C++]
5618 if (CGDebugInfo *DI = getModuleDebugInfo())
5619 DI->EmitUsingDecl(cast<UsingDecl>(*D));
5620 break;
5621 case Decl::NamespaceAlias:
5622 if (CGDebugInfo *DI = getModuleDebugInfo())
5623 DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
5624 break;
5625 case Decl::UsingDirective: // using namespace X; [C++]
5626 if (CGDebugInfo *DI = getModuleDebugInfo())
5627 DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
5628 break;
5629 case Decl::CXXConstructor:
5630 getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D));
5631 break;
5632 case Decl::CXXDestructor:
5633 getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D));
5634 break;
5635
5636 case Decl::StaticAssert:
5637 // Nothing to do.
5638 break;
5639
5640 // Objective-C Decls
5641
5642 // Forward declarations, no (immediate) code generation.
5643 case Decl::ObjCInterface:
5644 case Decl::ObjCCategory:
5645 break;
5646
5647 case Decl::ObjCProtocol: {
5648 auto *Proto = cast<ObjCProtocolDecl>(D);
5649 if (Proto->isThisDeclarationADefinition())
5650 ObjCRuntime->GenerateProtocol(Proto);
5651 break;
5652 }
5653
5654 case Decl::ObjCCategoryImpl:
5655 // Categories have properties but don't support synthesize so we
5656 // can ignore them here.
5657 ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
5658 break;
5659
5660 case Decl::ObjCImplementation: {
5661 auto *OMD = cast<ObjCImplementationDecl>(D);
5662 EmitObjCPropertyImplementations(OMD);
5663 EmitObjCIvarInitializations(OMD);
5664 ObjCRuntime->GenerateClass(OMD);
5665 // Emit global variable debug information.
5666 if (CGDebugInfo *DI = getModuleDebugInfo())
5667 if (getCodeGenOpts().hasReducedDebugInfo())
5668 DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
5669 OMD->getClassInterface()), OMD->getLocation());
5670 break;
5671 }
5672 case Decl::ObjCMethod: {
5673 auto *OMD = cast<ObjCMethodDecl>(D);
5674 // If this is not a prototype, emit the body.
5675 if (OMD->getBody())
5676 CodeGenFunction(*this).GenerateObjCMethod(OMD);
5677 break;
5678 }
5679 case Decl::ObjCCompatibleAlias:
5680 ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
5681 break;
5682
5683 case Decl::PragmaComment: {
5684 const auto *PCD = cast<PragmaCommentDecl>(D);
5685 switch (PCD->getCommentKind()) {
5686 case PCK_Unknown:
5687 llvm_unreachable("unexpected pragma comment kind")::llvm::llvm_unreachable_internal("unexpected pragma comment kind"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 5687)
;
5688 case PCK_Linker:
5689 AppendLinkerOptions(PCD->getArg());
5690 break;
5691 case PCK_Lib:
5692 AddDependentLib(PCD->getArg());
5693 break;
5694 case PCK_Compiler:
5695 case PCK_ExeStr:
5696 case PCK_User:
5697 break; // We ignore all of these.
5698 }
5699 break;
5700 }
5701
5702 case Decl::PragmaDetectMismatch: {
5703 const auto *PDMD = cast<PragmaDetectMismatchDecl>(D);
5704 AddDetectMismatch(PDMD->getName(), PDMD->getValue());
5705 break;
5706 }
5707
5708 case Decl::LinkageSpec:
5709 EmitLinkageSpec(cast<LinkageSpecDecl>(D));
5710 break;
5711
5712 case Decl::FileScopeAsm: {
5713 // File-scope asm is ignored during device-side CUDA compilation.
5714 if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
5715 break;
5716 // File-scope asm is ignored during device-side OpenMP compilation.
5717 if (LangOpts.OpenMPIsDevice)
5718 break;
5719 auto *AD = cast<FileScopeAsmDecl>(D);
5720 getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
5721 break;
5722 }
5723
5724 case Decl::Import: {
5725 auto *Import = cast<ImportDecl>(D);
5726
5727 // If we've already imported this module, we're done.
5728 if (!ImportedModules.insert(Import->getImportedModule()))
5729 break;
5730
5731 // Emit debug information for direct imports.
5732 if (!Import->getImportedOwningModule()) {
5733 if (CGDebugInfo *DI = getModuleDebugInfo())
5734 DI->EmitImportDecl(*Import);
5735 }
5736
5737 // Find all of the submodules and emit the module initializers.
5738 llvm::SmallPtrSet<clang::Module *, 16> Visited;
5739 SmallVector<clang::Module *, 16> Stack;
5740 Visited.insert(Import->getImportedModule());
5741 Stack.push_back(Import->getImportedModule());
5742
5743 while (!Stack.empty()) {
5744 clang::Module *Mod = Stack.pop_back_val();
5745 if (!EmittedModuleInitializers.insert(Mod).second)
5746 continue;
5747
5748 for (auto *D : Context.getModuleInitializers(Mod))
5749 EmitTopLevelDecl(D);
5750
5751 // Visit the submodules of this module.
5752 for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(),
5753 SubEnd = Mod->submodule_end();
5754 Sub != SubEnd; ++Sub) {
5755 // Skip explicit children; they need to be explicitly imported to emit
5756 // the initializers.
5757 if ((*Sub)->IsExplicit)
5758 continue;
5759
5760 if (Visited.insert(*Sub).second)
5761 Stack.push_back(*Sub);
5762 }
5763 }
5764 break;
5765 }
5766
5767 case Decl::Export:
5768 EmitDeclContext(cast<ExportDecl>(D));
5769 break;
5770
5771 case Decl::OMPThreadPrivate:
5772 EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D));
5773 break;
5774
5775 case Decl::OMPAllocate:
5776 break;
5777
5778 case Decl::OMPDeclareReduction:
5779 EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D));
5780 break;
5781
5782 case Decl::OMPDeclareMapper:
5783 EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(D));
5784 break;
5785
5786 case Decl::OMPRequires:
5787 EmitOMPRequiresDecl(cast<OMPRequiresDecl>(D));
5788 break;
5789
5790 case Decl::Typedef:
5791 case Decl::TypeAlias: // using foo = bar; [C++11]
5792 if (CGDebugInfo *DI = getModuleDebugInfo())
5793 DI->EmitAndRetainType(
5794 getContext().getTypedefType(cast<TypedefNameDecl>(D)));
5795 break;
5796
5797 case Decl::Record:
5798 if (CGDebugInfo *DI = getModuleDebugInfo())
5799 if (cast<RecordDecl>(D)->getDefinition())
5800 DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D)));
5801 break;
5802
5803 case Decl::Enum:
5804 if (CGDebugInfo *DI = getModuleDebugInfo())
5805 if (cast<EnumDecl>(D)->getDefinition())
5806 DI->EmitAndRetainType(getContext().getEnumType(cast<EnumDecl>(D)));
5807 break;
5808
5809 default:
5810 // Make sure we handled everything we should, every other kind is a
5811 // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind
5812 // function. Need to recode Decl::Kind to do that easily.
5813 assert(isa<TypeDecl>(D) && "Unsupported decl kind")((isa<TypeDecl>(D) && "Unsupported decl kind") ?
static_cast<void> (0) : __assert_fail ("isa<TypeDecl>(D) && \"Unsupported decl kind\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 5813, __PRETTY_FUNCTION__))
;
5814 break;
5815 }
5816}
5817
5818void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) {
5819 // Do we need to generate coverage mapping?
5820 if (!CodeGenOpts.CoverageMapping)
5821 return;
5822 switch (D->getKind()) {
5823 case Decl::CXXConversion:
5824 case Decl::CXXMethod:
5825 case Decl::Function:
5826 case Decl::ObjCMethod:
5827 case Decl::CXXConstructor:
5828 case Decl::CXXDestructor: {
5829 if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody())
5830 break;
5831 SourceManager &SM = getContext().getSourceManager();
5832 if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc()))
5833 break;
5834 auto I = DeferredEmptyCoverageMappingDecls.find(D);
5835 if (I == DeferredEmptyCoverageMappingDecls.end())
5836 DeferredEmptyCoverageMappingDecls[D] = true;
5837 break;
5838 }
5839 default:
5840 break;
5841 };
5842}
5843
5844void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) {
5845 // Do we need to generate coverage mapping?
5846 if (!CodeGenOpts.CoverageMapping)
5847 return;
5848 if (const auto *Fn = dyn_cast<FunctionDecl>(D)) {
5849 if (Fn->isTemplateInstantiation())
5850 ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern());
5851 }
5852 auto I = DeferredEmptyCoverageMappingDecls.find(D);
5853 if (I == DeferredEmptyCoverageMappingDecls.end())
5854 DeferredEmptyCoverageMappingDecls[D] = false;
5855 else
5856 I->second = false;
5857}
5858
5859void CodeGenModule::EmitDeferredUnusedCoverageMappings() {
5860 // We call takeVector() here to avoid use-after-free.
5861 // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because
5862 // we deserialize function bodies to emit coverage info for them, and that
5863 // deserializes more declarations. How should we handle that case?
5864 for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) {
5865 if (!Entry.second)
5866 continue;
5867 const Decl *D = Entry.first;
5868 switch (D->getKind()) {
5869 case Decl::CXXConversion:
5870 case Decl::CXXMethod:
5871 case Decl::Function:
5872 case Decl::ObjCMethod: {
5873 CodeGenPGO PGO(*this);
5874 GlobalDecl GD(cast<FunctionDecl>(D));
5875 PGO.emitEmptyCounterMapping(D, getMangledName(GD),
5876 getFunctionLinkage(GD));
5877 break;
5878 }
5879 case Decl::CXXConstructor: {
5880 CodeGenPGO PGO(*this);
5881 GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base);
5882 PGO.emitEmptyCounterMapping(D, getMangledName(GD),
5883 getFunctionLinkage(GD));
5884 break;
5885 }
5886 case Decl::CXXDestructor: {
5887 CodeGenPGO PGO(*this);
5888 GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base);
5889 PGO.emitEmptyCounterMapping(D, getMangledName(GD),
5890 getFunctionLinkage(GD));
5891 break;
5892 }
5893 default:
5894 break;
5895 };
5896 }
5897}
5898
5899void CodeGenModule::EmitMainVoidAlias() {
5900 // In order to transition away from "__original_main" gracefully, emit an
5901 // alias for "main" in the no-argument case so that libc can detect when
5902 // new-style no-argument main is in used.
5903 if (llvm::Function *F = getModule().getFunction("main")) {
5904 if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() &&
5905 F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth()))
5906 addUsedGlobal(llvm::GlobalAlias::create("__main_void", F));
5907 }
5908}
5909
5910/// Turns the given pointer into a constant.
5911static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
5912 const void *Ptr) {
5913 uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
5914 llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
5915 return llvm::ConstantInt::get(i64, PtrInt);
5916}
5917
5918static void EmitGlobalDeclMetadata(CodeGenModule &CGM,
5919 llvm::NamedMDNode *&GlobalMetadata,
5920 GlobalDecl D,
5921 llvm::GlobalValue *Addr) {
5922 if (!GlobalMetadata)
5923 GlobalMetadata =
5924 CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
5925
5926 // TODO: should we report variant information for ctors/dtors?
5927 llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr),
5928 llvm::ConstantAsMetadata::get(GetPointerConstant(
5929 CGM.getLLVMContext(), D.getDecl()))};
5930 GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
5931}
5932
5933/// For each function which is declared within an extern "C" region and marked
5934/// as 'used', but has internal linkage, create an alias from the unmangled
5935/// name to the mangled name if possible. People expect to be able to refer
5936/// to such functions with an unmangled name from inline assembly within the
5937/// same translation unit.
5938void CodeGenModule::EmitStaticExternCAliases() {
5939 if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases())
5940 return;
5941 for (auto &I : StaticExternCValues) {
5942 IdentifierInfo *Name = I.first;
5943 llvm::GlobalValue *Val = I.second;
5944 if (Val && !getModule().getNamedValue(Name->getName()))
5945 addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val));
5946 }
5947}
5948
5949bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName,
5950 GlobalDecl &Result) const {
5951 auto Res = Manglings.find(MangledName);
5952 if (Res == Manglings.end())
5953 return false;
5954 Result = Res->getValue();
5955 return true;
5956}
5957
5958/// Emits metadata nodes associating all the global values in the
5959/// current module with the Decls they came from. This is useful for
5960/// projects using IR gen as a subroutine.
5961///
5962/// Since there's currently no way to associate an MDNode directly
5963/// with an llvm::GlobalValue, we create a global named metadata
5964/// with the name 'clang.global.decl.ptrs'.
5965void CodeGenModule::EmitDeclMetadata() {
5966 llvm::NamedMDNode *GlobalMetadata = nullptr;
5967
5968 for (auto &I : MangledDeclNames) {
5969 llvm::GlobalValue *Addr = getModule().getNamedValue(I.second);
5970 // Some mangled names don't necessarily have an associated GlobalValue
5971 // in this module, e.g. if we mangled it for DebugInfo.
5972 if (Addr)
5973 EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr);
5974 }
5975}
5976
5977/// Emits metadata nodes for all the local variables in the current
5978/// function.
5979void CodeGenFunction::EmitDeclMetadata() {
5980 if (LocalDeclMap.empty()) return;
5981
5982 llvm::LLVMContext &Context = getLLVMContext();
5983
5984 // Find the unique metadata ID for this name.
5985 unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
5986
5987 llvm::NamedMDNode *GlobalMetadata = nullptr;
5988
5989 for (auto &I : LocalDeclMap) {
5990 const Decl *D = I.first;
5991 llvm::Value *Addr = I.second.getPointer();
5992 if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
5993 llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
5994 Alloca->setMetadata(
5995 DeclPtrKind, llvm::MDNode::get(
5996 Context, llvm::ValueAsMetadata::getConstant(DAddr)));
5997 } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
5998 GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
5999 EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
6000 }
6001 }
6002}
6003
6004void CodeGenModule::EmitVersionIdentMetadata() {
6005 llvm::NamedMDNode *IdentMetadata =
6006 TheModule.getOrInsertNamedMetadata("llvm.ident");
6007 std::string Version = getClangFullVersion();
6008 llvm::LLVMContext &Ctx = TheModule.getContext();
6009
6010 llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)};
6011 IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
6012}
6013
6014void CodeGenModule::EmitCommandLineMetadata() {
6015 llvm::NamedMDNode *CommandLineMetadata =
6016 TheModule.getOrInsertNamedMetadata("llvm.commandline");
6017 std::string CommandLine = getCodeGenOpts().RecordCommandLine;
6018 llvm::LLVMContext &Ctx = TheModule.getContext();
6019
6020 llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)};
6021 CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode));
6022}
6023
6024void CodeGenModule::EmitCoverageFile() {
6025 if (getCodeGenOpts().CoverageDataFile.empty() &&
6026 getCodeGenOpts().CoverageNotesFile.empty())
6027 return;
6028
6029 llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu");
6030 if (!CUNode)
6031 return;
6032
6033 llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
6034 llvm::LLVMContext &Ctx = TheModule.getContext();
6035 auto *CoverageDataFile =
6036 llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile);
6037 auto *CoverageNotesFile =
6038 llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile);
6039 for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
6040 llvm::MDNode *CU = CUNode->getOperand(i);
6041 llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU};
6042 GCov->addOperand(llvm::MDNode::get(Ctx, Elts));
6043 }
6044}
6045
6046llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
6047 bool ForEH) {
6048 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
6049 // FIXME: should we even be calling this method if RTTI is disabled
6050 // and it's not for EH?
6051 if ((!ForEH && !getLangOpts().RTTI) || getLangOpts().CUDAIsDevice ||
6052 (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
6053 getTriple().isNVPTX()))
6054 return llvm::Constant::getNullValue(Int8PtrTy);
6055
6056 if (ForEH && Ty->isObjCObjectPointerType() &&
6057 LangOpts.ObjCRuntime.isGNUFamily())
6058 return ObjCRuntime->GetEHType(Ty);
6059
6060 return getCXXABI().getAddrOfRTTIDescriptor(Ty);
6061}
6062
6063void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
6064 // Do not emit threadprivates in simd-only mode.
6065 if (LangOpts.OpenMP && LangOpts.OpenMPSimd)
6066 return;
6067 for (auto RefExpr : D->varlists()) {
6068 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl());
6069 bool PerformInit =
6070 VD->getAnyInitializer() &&
6071 !VD->getAnyInitializer()->isConstantInitializer(getContext(),
6072 /*ForRef=*/false);
6073
6074 Address Addr(GetAddrOfGlobalVar(VD), getContext().getDeclAlign(VD));
6075 if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition(
6076 VD, Addr, RefExpr->getBeginLoc(), PerformInit))
6077 CXXGlobalInits.push_back(InitFunction);
6078 }
6079}
6080
6081llvm::Metadata *
6082CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
6083 StringRef Suffix) {
6084 llvm::Metadata *&InternalId = Map[T.getCanonicalType()];
6085 if (InternalId)
6086 return InternalId;
6087
6088 if (isExternallyVisible(T->getLinkage())) {
6089 std::string OutName;
6090 llvm::raw_string_ostream Out(OutName);
6091 getCXXABI().getMangleContext().mangleTypeName(T, Out);
6092 Out << Suffix;
6093
6094 InternalId = llvm::MDString::get(getLLVMContext(), Out.str());
6095 } else {
6096 InternalId = llvm::MDNode::getDistinct(getLLVMContext(),
6097 llvm::ArrayRef<llvm::Metadata *>());
6098 }
6099
6100 return InternalId;
6101}
6102
6103llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) {
6104 return CreateMetadataIdentifierImpl(T, MetadataIdMap, "");
6105}
6106
6107llvm::Metadata *
6108CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
6109 return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual");
6110}
6111
6112// Generalize pointer types to a void pointer with the qualifiers of the
6113// originally pointed-to type, e.g. 'const char *' and 'char * const *'
6114// generalize to 'const void *' while 'char *' and 'const char **' generalize to
6115// 'void *'.
6116static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
6117 if (!Ty->isPointerType())
6118 return Ty;
6119
6120 return Ctx.getPointerType(
6121 QualType(Ctx.VoidTy).withCVRQualifiers(
6122 Ty->getPointeeType().getCVRQualifiers()));
6123}
6124
6125// Apply type generalization to a FunctionType's return and argument types
6126static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) {
6127 if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
6128 SmallVector<QualType, 8> GeneralizedParams;
6129 for (auto &Param : FnType->param_types())
6130 GeneralizedParams.push_back(GeneralizeType(Ctx, Param));
6131
6132 return Ctx.getFunctionType(
6133 GeneralizeType(Ctx, FnType->getReturnType()),
6134 GeneralizedParams, FnType->getExtProtoInfo());
6135 }
6136
6137 if (auto *FnType = Ty->getAs<FunctionNoProtoType>())
6138 return Ctx.getFunctionNoProtoType(
6139 GeneralizeType(Ctx, FnType->getReturnType()));
6140
6141 llvm_unreachable("Encountered unknown FunctionType")::llvm::llvm_unreachable_internal("Encountered unknown FunctionType"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/lib/CodeGen/CodeGenModule.cpp"
, 6141)
;
6142}
6143
6144llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) {
6145 return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T),
6146 GeneralizedMetadataIdMap, ".generalized");
6147}
6148
6149/// Returns whether this module needs the "all-vtables" type identifier.
6150bool CodeGenModule::NeedAllVtablesTypeId() const {
6151 // Returns true if at least one of vtable-based CFI checkers is enabled and
6152 // is not in the trapping mode.
6153 return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) &&
6154 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) ||
6155 (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) &&
6156 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) ||
6157 (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) &&
6158 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) ||
6159 (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) &&
6160 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast)));
6161}
6162
6163void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable,
6164 CharUnits Offset,
6165 const CXXRecordDecl *RD) {
6166 llvm::Metadata *MD =
6167 CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
6168 VTable->addTypeMetadata(Offset.getQuantity(), MD);
6169
6170 if (CodeGenOpts.SanitizeCfiCrossDso)
6171 if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
6172 VTable->addTypeMetadata(Offset.getQuantity(),
6173 llvm::ConstantAsMetadata::get(CrossDsoTypeId));
6174
6175 if (NeedAllVtablesTypeId()) {
6176 llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables");
6177 VTable->addTypeMetadata(Offset.getQuantity(), MD);
6178 }
6179}
6180
6181llvm::SanitizerStatReport &CodeGenModule::getSanStats() {
6182 if (!SanStats)
6183 SanStats = std::make_unique<llvm::SanitizerStatReport>(&getModule());
6184
6185 return *SanStats;
6186}
6187llvm::Value *
6188CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E,
6189 CodeGenFunction &CGF) {
6190 llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType());
6191 auto SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr());
6192 auto FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false);
6193 return CGF.Builder.CreateCall(CreateRuntimeFunction(FTy,
6194 "__translate_sampler_initializer"),
6195 {C});
6196}
6197
6198CharUnits CodeGenModule::getNaturalPointeeTypeAlignment(
6199 QualType T, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) {
6200 return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo,
6201 /* forPointeeType= */ true);
6202}
6203
6204CharUnits CodeGenModule::getNaturalTypeAlignment(QualType T,
6205 LValueBaseInfo *BaseInfo,
6206 TBAAAccessInfo *TBAAInfo,
6207 bool forPointeeType) {
6208 if (TBAAInfo)
6209 *TBAAInfo = getTBAAAccessInfo(T);
6210
6211 // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown. But
6212 // that doesn't return the information we need to compute BaseInfo.
6213
6214 // Honor alignment typedef attributes even on incomplete types.
6215 // We also honor them straight for C++ class types, even as pointees;
6216 // there's an expressivity gap here.
6217 if (auto TT = T->getAs<TypedefType>()) {
6218 if (auto Align = TT->getDecl()->getMaxAlignment()) {
6219 if (BaseInfo)
6220 *BaseInfo = LValueBaseInfo(AlignmentSource::AttributedType);
6221 return getContext().toCharUnitsFromBits(Align);
6222 }
6223 }
6224
6225 bool AlignForArray = T->isArrayType();
6226
6227 // Analyze the base element type, so we don't get confused by incomplete
6228 // array types.
6229 T = getContext().getBaseElementType(T);
6230
6231 if (T->isIncompleteType()) {
6232 // We could try to replicate the logic from
6233 // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the
6234 // type is incomplete, so it's impossible to test. We could try to reuse
6235 // getTypeAlignIfKnown, but that doesn't return the information we need
6236 // to set BaseInfo. So just ignore the possibility that the alignment is
6237 // greater than one.
6238 if (BaseInfo)
6239 *BaseInfo = LValueBaseInfo(AlignmentSource::Type);
6240 return CharUnits::One();
6241 }
6242
6243 if (BaseInfo)
6244 *BaseInfo = LValueBaseInfo(AlignmentSource::Type);
6245
6246 CharUnits Alignment;
6247 const CXXRecordDecl *RD;
6248 if (T.getQualifiers().hasUnaligned()) {
6249 Alignment = CharUnits::One();
6250 } else if (forPointeeType && !AlignForArray &&
6251 (RD = T->getAsCXXRecordDecl())) {
6252 // For C++ class pointees, we don't know whether we're pointing at a
6253 // base or a complete object, so we generally need to use the
6254 // non-virtual alignment.
6255 Alignment = getClassPointerAlignment(RD);
6256 } else {
6257 Alignment = getContext().getTypeAlignInChars(T);
6258 }
6259
6260 // Cap to the global maximum type alignment unless the alignment
6261 // was somehow explicit on the type.
6262 if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) {
6263 if (Alignment.getQuantity() > MaxAlign &&
6264 !getContext().isAlignmentRequired(T))
6265 Alignment = CharUnits::fromQuantity(MaxAlign);
6266 }
6267 return Alignment;
6268}
6269
6270bool CodeGenModule::stopAutoInit() {
6271 unsigned StopAfter = getContext().getLangOpts().TrivialAutoVarInitStopAfter;
6272 if (StopAfter) {
6273 // This number is positive only when -ftrivial-auto-var-init-stop-after=* is
6274 // used
6275 if (NumAutoVarInit >= StopAfter) {
6276 return true;
6277 }
6278 if (!NumAutoVarInit) {
6279 unsigned DiagID = getDiags().getCustomDiagID(
6280 DiagnosticsEngine::Warning,
6281 "-ftrivial-auto-var-init-stop-after=%0 has been enabled to limit the "
6282 "number of times ftrivial-auto-var-init=%1 gets applied.");
6283 getDiags().Report(DiagID)
6284 << StopAfter
6285 << (getContext().getLangOpts().getTrivialAutoVarInit() ==
6286 LangOptions::TrivialAutoVarInitKind::Zero
6287 ? "zero"
6288 : "pattern");
6289 }
6290 ++NumAutoVarInit;
6291 }
6292 return false;
6293}

/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h

1//===- DeclBase.h - Base Classes for representing declarations --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the Decl and DeclContext interfaces.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_DECLBASE_H
14#define LLVM_CLANG_AST_DECLBASE_H
15
16#include "clang/AST/ASTDumperUtils.h"
17#include "clang/AST/AttrIterator.h"
18#include "clang/AST/DeclarationName.h"
19#include "clang/Basic/IdentifierTable.h"
20#include "clang/Basic/LLVM.h"
21#include "clang/Basic/SourceLocation.h"
22#include "clang/Basic/Specifiers.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/PointerIntPair.h"
25#include "llvm/ADT/PointerUnion.h"
26#include "llvm/ADT/iterator.h"
27#include "llvm/ADT/iterator_range.h"
28#include "llvm/Support/Casting.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/PrettyStackTrace.h"
31#include "llvm/Support/VersionTuple.h"
32#include <algorithm>
33#include <cassert>
34#include <cstddef>
35#include <iterator>
36#include <string>
37#include <type_traits>
38#include <utility>
39
40namespace clang {
41
42class ASTContext;
43class ASTMutationListener;
44class Attr;
45class BlockDecl;
46class DeclContext;
47class ExternalSourceSymbolAttr;
48class FunctionDecl;
49class FunctionType;
50class IdentifierInfo;
51enum Linkage : unsigned char;
52class LinkageSpecDecl;
53class Module;
54class NamedDecl;
55class ObjCCategoryDecl;
56class ObjCCategoryImplDecl;
57class ObjCContainerDecl;
58class ObjCImplDecl;
59class ObjCImplementationDecl;
60class ObjCInterfaceDecl;
61class ObjCMethodDecl;
62class ObjCProtocolDecl;
63struct PrintingPolicy;
64class RecordDecl;
65class SourceManager;
66class Stmt;
67class StoredDeclsMap;
68class TemplateDecl;
69class TemplateParameterList;
70class TranslationUnitDecl;
71class UsingDirectiveDecl;
72
73/// Captures the result of checking the availability of a
74/// declaration.
75enum AvailabilityResult {
76 AR_Available = 0,
77 AR_NotYetIntroduced,
78 AR_Deprecated,
79 AR_Unavailable
80};
81
82/// Decl - This represents one declaration (or definition), e.g. a variable,
83/// typedef, function, struct, etc.
84///
85/// Note: There are objects tacked on before the *beginning* of Decl
86/// (and its subclasses) in its Decl::operator new(). Proper alignment
87/// of all subclasses (not requiring more than the alignment of Decl) is
88/// asserted in DeclBase.cpp.
89class alignas(8) Decl {
90public:
91 /// Lists the kind of concrete classes of Decl.
92 enum Kind {
93#define DECL(DERIVED, BASE) DERIVED,
94#define ABSTRACT_DECL(DECL)
95#define DECL_RANGE(BASE, START, END) \
96 first##BASE = START, last##BASE = END,
97#define LAST_DECL_RANGE(BASE, START, END) \
98 first##BASE = START, last##BASE = END
99#include "clang/AST/DeclNodes.inc"
100 };
101
102 /// A placeholder type used to construct an empty shell of a
103 /// decl-derived type that will be filled in later (e.g., by some
104 /// deserialization method).
105 struct EmptyShell {};
106
107 /// IdentifierNamespace - The different namespaces in which
108 /// declarations may appear. According to C99 6.2.3, there are
109 /// four namespaces, labels, tags, members and ordinary
110 /// identifiers. C++ describes lookup completely differently:
111 /// certain lookups merely "ignore" certain kinds of declarations,
112 /// usually based on whether the declaration is of a type, etc.
113 ///
114 /// These are meant as bitmasks, so that searches in
115 /// C++ can look into the "tag" namespace during ordinary lookup.
116 ///
117 /// Decl currently provides 15 bits of IDNS bits.
118 enum IdentifierNamespace {
119 /// Labels, declared with 'x:' and referenced with 'goto x'.
120 IDNS_Label = 0x0001,
121
122 /// Tags, declared with 'struct foo;' and referenced with
123 /// 'struct foo'. All tags are also types. This is what
124 /// elaborated-type-specifiers look for in C.
125 /// This also contains names that conflict with tags in the
126 /// same scope but that are otherwise ordinary names (non-type
127 /// template parameters and indirect field declarations).
128 IDNS_Tag = 0x0002,
129
130 /// Types, declared with 'struct foo', typedefs, etc.
131 /// This is what elaborated-type-specifiers look for in C++,
132 /// but note that it's ill-formed to find a non-tag.
133 IDNS_Type = 0x0004,
134
135 /// Members, declared with object declarations within tag
136 /// definitions. In C, these can only be found by "qualified"
137 /// lookup in member expressions. In C++, they're found by
138 /// normal lookup.
139 IDNS_Member = 0x0008,
140
141 /// Namespaces, declared with 'namespace foo {}'.
142 /// Lookup for nested-name-specifiers find these.
143 IDNS_Namespace = 0x0010,
144
145 /// Ordinary names. In C, everything that's not a label, tag,
146 /// member, or function-local extern ends up here.
147 IDNS_Ordinary = 0x0020,
148
149 /// Objective C \@protocol.
150 IDNS_ObjCProtocol = 0x0040,
151
152 /// This declaration is a friend function. A friend function
153 /// declaration is always in this namespace but may also be in
154 /// IDNS_Ordinary if it was previously declared.
155 IDNS_OrdinaryFriend = 0x0080,
156
157 /// This declaration is a friend class. A friend class
158 /// declaration is always in this namespace but may also be in
159 /// IDNS_Tag|IDNS_Type if it was previously declared.
160 IDNS_TagFriend = 0x0100,
161
162 /// This declaration is a using declaration. A using declaration
163 /// *introduces* a number of other declarations into the current
164 /// scope, and those declarations use the IDNS of their targets,
165 /// but the actual using declarations go in this namespace.
166 IDNS_Using = 0x0200,
167
168 /// This declaration is a C++ operator declared in a non-class
169 /// context. All such operators are also in IDNS_Ordinary.
170 /// C++ lexical operator lookup looks for these.
171 IDNS_NonMemberOperator = 0x0400,
172
173 /// This declaration is a function-local extern declaration of a
174 /// variable or function. This may also be IDNS_Ordinary if it
175 /// has been declared outside any function. These act mostly like
176 /// invisible friend declarations, but are also visible to unqualified
177 /// lookup within the scope of the declaring function.
178 IDNS_LocalExtern = 0x0800,
179
180 /// This declaration is an OpenMP user defined reduction construction.
181 IDNS_OMPReduction = 0x1000,
182
183 /// This declaration is an OpenMP user defined mapper.
184 IDNS_OMPMapper = 0x2000,
185 };
186
187 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
188 /// parameter types in method declarations. Other than remembering
189 /// them and mangling them into the method's signature string, these
190 /// are ignored by the compiler; they are consumed by certain
191 /// remote-messaging frameworks.
192 ///
193 /// in, inout, and out are mutually exclusive and apply only to
194 /// method parameters. bycopy and byref are mutually exclusive and
195 /// apply only to method parameters (?). oneway applies only to
196 /// results. All of these expect their corresponding parameter to
197 /// have a particular type. None of this is currently enforced by
198 /// clang.
199 ///
200 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
201 enum ObjCDeclQualifier {
202 OBJC_TQ_None = 0x0,
203 OBJC_TQ_In = 0x1,
204 OBJC_TQ_Inout = 0x2,
205 OBJC_TQ_Out = 0x4,
206 OBJC_TQ_Bycopy = 0x8,
207 OBJC_TQ_Byref = 0x10,
208 OBJC_TQ_Oneway = 0x20,
209
210 /// The nullability qualifier is set when the nullability of the
211 /// result or parameter was expressed via a context-sensitive
212 /// keyword.
213 OBJC_TQ_CSNullability = 0x40
214 };
215
216 /// The kind of ownership a declaration has, for visibility purposes.
217 /// This enumeration is designed such that higher values represent higher
218 /// levels of name hiding.
219 enum class ModuleOwnershipKind : unsigned {
220 /// This declaration is not owned by a module.
221 Unowned,
222
223 /// This declaration has an owning module, but is globally visible
224 /// (typically because its owning module is visible and we know that
225 /// modules cannot later become hidden in this compilation).
226 /// After serialization and deserialization, this will be converted
227 /// to VisibleWhenImported.
228 Visible,
229
230 /// This declaration has an owning module, and is visible when that
231 /// module is imported.
232 VisibleWhenImported,
233
234 /// This declaration has an owning module, but is only visible to
235 /// lookups that occur within that module.
236 ModulePrivate
237 };
238
239protected:
240 /// The next declaration within the same lexical
241 /// DeclContext. These pointers form the linked list that is
242 /// traversed via DeclContext's decls_begin()/decls_end().
243 ///
244 /// The extra two bits are used for the ModuleOwnershipKind.
245 llvm::PointerIntPair<Decl *, 2, ModuleOwnershipKind> NextInContextAndBits;
246
247private:
248 friend class DeclContext;
249
250 struct MultipleDC {
251 DeclContext *SemanticDC;
252 DeclContext *LexicalDC;
253 };
254
255 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
256 /// For declarations that don't contain C++ scope specifiers, it contains
257 /// the DeclContext where the Decl was declared.
258 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
259 /// with the context where it semantically belongs (SemanticDC) and the
260 /// context where it was lexically declared (LexicalDC).
261 /// e.g.:
262 ///
263 /// namespace A {
264 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
265 /// }
266 /// void A::f(); // SemanticDC == namespace 'A'
267 /// // LexicalDC == global namespace
268 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
269
270 bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
271 bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
272
273 MultipleDC *getMultipleDC() const {
274 return DeclCtx.get<MultipleDC*>();
275 }
276
277 DeclContext *getSemanticDC() const {
278 return DeclCtx.get<DeclContext*>();
279 }
280
281 /// Loc - The location of this decl.
282 SourceLocation Loc;
283
284 /// DeclKind - This indicates which class this is.
285 unsigned DeclKind : 7;
286
287 /// InvalidDecl - This indicates a semantic error occurred.
288 unsigned InvalidDecl : 1;
289
290 /// HasAttrs - This indicates whether the decl has attributes or not.
291 unsigned HasAttrs : 1;
292
293 /// Implicit - Whether this declaration was implicitly generated by
294 /// the implementation rather than explicitly written by the user.
295 unsigned Implicit : 1;
296
297 /// Whether this declaration was "used", meaning that a definition is
298 /// required.
299 unsigned Used : 1;
300
301 /// Whether this declaration was "referenced".
302 /// The difference with 'Used' is whether the reference appears in a
303 /// evaluated context or not, e.g. functions used in uninstantiated templates
304 /// are regarded as "referenced" but not "used".
305 unsigned Referenced : 1;
306
307 /// Whether this declaration is a top-level declaration (function,
308 /// global variable, etc.) that is lexically inside an objc container
309 /// definition.
310 unsigned TopLevelDeclInObjCContainer : 1;
311
312 /// Whether statistic collection is enabled.
313 static bool StatisticsEnabled;
314
315protected:
316 friend class ASTDeclReader;
317 friend class ASTDeclWriter;
318 friend class ASTNodeImporter;
319 friend class ASTReader;
320 friend class CXXClassMemberWrapper;
321 friend class LinkageComputer;
322 template<typename decl_type> friend class Redeclarable;
323
324 /// Access - Used by C++ decls for the access specifier.
325 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
326 unsigned Access : 2;
327
328 /// Whether this declaration was loaded from an AST file.
329 unsigned FromASTFile : 1;
330
331 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
332 unsigned IdentifierNamespace : 14;
333
334 /// If 0, we have not computed the linkage of this declaration.
335 /// Otherwise, it is the linkage + 1.
336 mutable unsigned CacheValidAndLinkage : 3;
337
338 /// Allocate memory for a deserialized declaration.
339 ///
340 /// This routine must be used to allocate memory for any declaration that is
341 /// deserialized from a module file.
342 ///
343 /// \param Size The size of the allocated object.
344 /// \param Ctx The context in which we will allocate memory.
345 /// \param ID The global ID of the deserialized declaration.
346 /// \param Extra The amount of extra space to allocate after the object.
347 void *operator new(std::size_t Size, const ASTContext &Ctx, unsigned ID,
348 std::size_t Extra = 0);
349
350 /// Allocate memory for a non-deserialized declaration.
351 void *operator new(std::size_t Size, const ASTContext &Ctx,
352 DeclContext *Parent, std::size_t Extra = 0);
353
354private:
355 bool AccessDeclContextSanity() const;
356
357 /// Get the module ownership kind to use for a local lexical child of \p DC,
358 /// which may be either a local or (rarely) an imported declaration.
359 static ModuleOwnershipKind getModuleOwnershipKindForChildOf(DeclContext *DC) {
360 if (DC) {
361 auto *D = cast<Decl>(DC);
362 auto MOK = D->getModuleOwnershipKind();
363 if (MOK != ModuleOwnershipKind::Unowned &&
364 (!D->isFromASTFile() || D->hasLocalOwningModuleStorage()))
365 return MOK;
366 // If D is not local and we have no local module storage, then we don't
367 // need to track module ownership at all.
368 }
369 return ModuleOwnershipKind::Unowned;
370 }
371
372public:
373 Decl() = delete;
374 Decl(const Decl&) = delete;
375 Decl(Decl &&) = delete;
376 Decl &operator=(const Decl&) = delete;
377 Decl &operator=(Decl&&) = delete;
378
379protected:
380 Decl(Kind DK, DeclContext *DC, SourceLocation L)
381 : NextInContextAndBits(nullptr, getModuleOwnershipKindForChildOf(DC)),
382 DeclCtx(DC), Loc(L), DeclKind(DK), InvalidDecl(false), HasAttrs(false),
383 Implicit(false), Used(false), Referenced(false),
384 TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0),
385 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
386 CacheValidAndLinkage(0) {
387 if (StatisticsEnabled) add(DK);
388 }
389
390 Decl(Kind DK, EmptyShell Empty)
391 : DeclKind(DK), InvalidDecl(false), HasAttrs(false), Implicit(false),
392 Used(false), Referenced(false), TopLevelDeclInObjCContainer(false),
393 Access(AS_none), FromASTFile(0),
394 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
395 CacheValidAndLinkage(0) {
396 if (StatisticsEnabled) add(DK);
397 }
398
399 virtual ~Decl();
400
401 /// Update a potentially out-of-date declaration.
402 void updateOutOfDate(IdentifierInfo &II) const;
403
404 Linkage getCachedLinkage() const {
405 return Linkage(CacheValidAndLinkage - 1);
406 }
407
408 void setCachedLinkage(Linkage L) const {
409 CacheValidAndLinkage = L + 1;
410 }
411
412 bool hasCachedLinkage() const {
413 return CacheValidAndLinkage;
414 }
415
416public:
417 /// Source range that this declaration covers.
418 virtual SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__)) {
419 return SourceRange(getLocation(), getLocation());
420 }
421
422 SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) {
423 return getSourceRange().getBegin();
424 }
425
426 SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) {
427 return getSourceRange().getEnd();
428 }
429
430 SourceLocation getLocation() const { return Loc; }
431 void setLocation(SourceLocation L) { Loc = L; }
432
433 Kind getKind() const { return static_cast<Kind>(DeclKind); }
434 const char *getDeclKindName() const;
435
436 Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
437 const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
438
439 DeclContext *getDeclContext() {
440 if (isInSemaDC())
441 return getSemanticDC();
442 return getMultipleDC()->SemanticDC;
443 }
444 const DeclContext *getDeclContext() const {
445 return const_cast<Decl*>(this)->getDeclContext();
446 }
447
448 /// Find the innermost non-closure ancestor of this declaration,
449 /// walking up through blocks, lambdas, etc. If that ancestor is
450 /// not a code context (!isFunctionOrMethod()), returns null.
451 ///
452 /// A declaration may be its own non-closure context.
453 Decl *getNonClosureContext();
454 const Decl *getNonClosureContext() const {
455 return const_cast<Decl*>(this)->getNonClosureContext();
456 }
457
458 TranslationUnitDecl *getTranslationUnitDecl();
459 const TranslationUnitDecl *getTranslationUnitDecl() const {
460 return const_cast<Decl*>(this)->getTranslationUnitDecl();
461 }
462
463 bool isInAnonymousNamespace() const;
464
465 bool isInStdNamespace() const;
466
467 ASTContext &getASTContext() const LLVM_READONLY__attribute__((__pure__));
468
469 /// Helper to get the language options from the ASTContext.
470 /// Defined out of line to avoid depending on ASTContext.h.
471 const LangOptions &getLangOpts() const LLVM_READONLY__attribute__((__pure__));
472
473 void setAccess(AccessSpecifier AS) {
474 Access = AS;
475 assert(AccessDeclContextSanity())((AccessDeclContextSanity()) ? static_cast<void> (0) : __assert_fail
("AccessDeclContextSanity()", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 475, __PRETTY_FUNCTION__))
;
476 }
477
478 AccessSpecifier getAccess() const {
479 assert(AccessDeclContextSanity())((AccessDeclContextSanity()) ? static_cast<void> (0) : __assert_fail
("AccessDeclContextSanity()", "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 479, __PRETTY_FUNCTION__))
;
480 return AccessSpecifier(Access);
481 }
482
483 /// Retrieve the access specifier for this declaration, even though
484 /// it may not yet have been properly set.
485 AccessSpecifier getAccessUnsafe() const {
486 return AccessSpecifier(Access);
487 }
488
489 bool hasAttrs() const { return HasAttrs; }
490
491 void setAttrs(const AttrVec& Attrs) {
492 return setAttrsImpl(Attrs, getASTContext());
493 }
494
495 AttrVec &getAttrs() {
496 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
497 }
498
499 const AttrVec &getAttrs() const;
500 void dropAttrs();
501 void addAttr(Attr *A);
502
503 using attr_iterator = AttrVec::const_iterator;
504 using attr_range = llvm::iterator_range<attr_iterator>;
505
506 attr_range attrs() const {
507 return attr_range(attr_begin(), attr_end());
508 }
509
510 attr_iterator attr_begin() const {
511 return hasAttrs() ? getAttrs().begin() : nullptr;
512 }
513 attr_iterator attr_end() const {
514 return hasAttrs() ? getAttrs().end() : nullptr;
515 }
516
517 template <typename T>
518 void dropAttr() {
519 if (!HasAttrs) return;
520
521 AttrVec &Vec = getAttrs();
522 llvm::erase_if(Vec, [](Attr *A) { return isa<T>(A); });
523
524 if (Vec.empty())
525 HasAttrs = false;
526 }
527
528 template <typename T>
529 llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const {
530 return llvm::make_range(specific_attr_begin<T>(), specific_attr_end<T>());
531 }
532
533 template <typename T>
534 specific_attr_iterator<T> specific_attr_begin() const {
535 return specific_attr_iterator<T>(attr_begin());
536 }
537
538 template <typename T>
539 specific_attr_iterator<T> specific_attr_end() const {
540 return specific_attr_iterator<T>(attr_end());
541 }
542
543 template<typename T> T *getAttr() const {
544 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : nullptr;
545 }
546
547 template<typename T> bool hasAttr() const {
548 return hasAttrs() && hasSpecificAttr<T>(getAttrs());
8
Returning zero, which participates in a condition later
27
Returning zero, which participates in a condition later
30
Returning zero, which participates in a condition later
34
Returning zero, which participates in a condition later
549 }
550
551 /// getMaxAlignment - return the maximum alignment specified by attributes
552 /// on this decl, 0 if there are none.
553 unsigned getMaxAlignment() const;
554
555 /// setInvalidDecl - Indicates the Decl had a semantic error. This
556 /// allows for graceful error recovery.
557 void setInvalidDecl(bool Invalid = true);
558 bool isInvalidDecl() const { return (bool) InvalidDecl; }
559
560 /// isImplicit - Indicates whether the declaration was implicitly
561 /// generated by the implementation. If false, this declaration
562 /// was written explicitly in the source code.
563 bool isImplicit() const { return Implicit; }
564 void setImplicit(bool I = true) { Implicit = I; }
565
566 /// Whether *any* (re-)declaration of the entity was used, meaning that
567 /// a definition is required.
568 ///
569 /// \param CheckUsedAttr When true, also consider the "used" attribute
570 /// (in addition to the "used" bit set by \c setUsed()) when determining
571 /// whether the function is used.
572 bool isUsed(bool CheckUsedAttr = true) const;
573
574 /// Set whether the declaration is used, in the sense of odr-use.
575 ///
576 /// This should only be used immediately after creating a declaration.
577 /// It intentionally doesn't notify any listeners.
578 void setIsUsed() { getCanonicalDecl()->Used = true; }
579
580 /// Mark the declaration used, in the sense of odr-use.
581 ///
582 /// This notifies any mutation listeners in addition to setting a bit
583 /// indicating the declaration is used.
584 void markUsed(ASTContext &C);
585
586 /// Whether any declaration of this entity was referenced.
587 bool isReferenced() const;
588
589 /// Whether this declaration was referenced. This should not be relied
590 /// upon for anything other than debugging.
591 bool isThisDeclarationReferenced() const { return Referenced; }
592
593 void setReferenced(bool R = true) { Referenced = R; }
594
595 /// Whether this declaration is a top-level declaration (function,
596 /// global variable, etc.) that is lexically inside an objc container
597 /// definition.
598 bool isTopLevelDeclInObjCContainer() const {
599 return TopLevelDeclInObjCContainer;
600 }
601
602 void setTopLevelDeclInObjCContainer(bool V = true) {
603 TopLevelDeclInObjCContainer = V;
604 }
605
606 /// Looks on this and related declarations for an applicable
607 /// external source symbol attribute.
608 ExternalSourceSymbolAttr *getExternalSourceSymbolAttr() const;
609
610 /// Whether this declaration was marked as being private to the
611 /// module in which it was defined.
612 bool isModulePrivate() const {
613 return getModuleOwnershipKind() == ModuleOwnershipKind::ModulePrivate;
614 }
615
616 /// Return true if this declaration has an attribute which acts as
617 /// definition of the entity, such as 'alias' or 'ifunc'.
618 bool hasDefiningAttr() const;
619
620 /// Return this declaration's defining attribute if it has one.
621 const Attr *getDefiningAttr() const;
622
623protected:
624 /// Specify that this declaration was marked as being private
625 /// to the module in which it was defined.
626 void setModulePrivate() {
627 // The module-private specifier has no effect on unowned declarations.
628 // FIXME: We should track this in some way for source fidelity.
629 if (getModuleOwnershipKind() == ModuleOwnershipKind::Unowned)
630 return;
631 setModuleOwnershipKind(ModuleOwnershipKind::ModulePrivate);
632 }
633
634public:
635 /// Set the FromASTFile flag. This indicates that this declaration
636 /// was deserialized and not parsed from source code and enables
637 /// features such as module ownership information.
638 void setFromASTFile() {
639 FromASTFile = true;
640 }
641
642 /// Set the owning module ID. This may only be called for
643 /// deserialized Decls.
644 void setOwningModuleID(unsigned ID) {
645 assert(isFromASTFile() && "Only works on a deserialized declaration")((isFromASTFile() && "Only works on a deserialized declaration"
) ? static_cast<void> (0) : __assert_fail ("isFromASTFile() && \"Only works on a deserialized declaration\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 645, __PRETTY_FUNCTION__))
;
646 *((unsigned*)this - 2) = ID;
647 }
648
649public:
650 /// Determine the availability of the given declaration.
651 ///
652 /// This routine will determine the most restrictive availability of
653 /// the given declaration (e.g., preferring 'unavailable' to
654 /// 'deprecated').
655 ///
656 /// \param Message If non-NULL and the result is not \c
657 /// AR_Available, will be set to a (possibly empty) message
658 /// describing why the declaration has not been introduced, is
659 /// deprecated, or is unavailable.
660 ///
661 /// \param EnclosingVersion The version to compare with. If empty, assume the
662 /// deployment target version.
663 ///
664 /// \param RealizedPlatform If non-NULL and the availability result is found
665 /// in an available attribute it will set to the platform which is written in
666 /// the available attribute.
667 AvailabilityResult
668 getAvailability(std::string *Message = nullptr,
669 VersionTuple EnclosingVersion = VersionTuple(),
670 StringRef *RealizedPlatform = nullptr) const;
671
672 /// Retrieve the version of the target platform in which this
673 /// declaration was introduced.
674 ///
675 /// \returns An empty version tuple if this declaration has no 'introduced'
676 /// availability attributes, or the version tuple that's specified in the
677 /// attribute otherwise.
678 VersionTuple getVersionIntroduced() const;
679
680 /// Determine whether this declaration is marked 'deprecated'.
681 ///
682 /// \param Message If non-NULL and the declaration is deprecated,
683 /// this will be set to the message describing why the declaration
684 /// was deprecated (which may be empty).
685 bool isDeprecated(std::string *Message = nullptr) const {
686 return getAvailability(Message) == AR_Deprecated;
687 }
688
689 /// Determine whether this declaration is marked 'unavailable'.
690 ///
691 /// \param Message If non-NULL and the declaration is unavailable,
692 /// this will be set to the message describing why the declaration
693 /// was made unavailable (which may be empty).
694 bool isUnavailable(std::string *Message = nullptr) const {
695 return getAvailability(Message) == AR_Unavailable;
696 }
697
698 /// Determine whether this is a weak-imported symbol.
699 ///
700 /// Weak-imported symbols are typically marked with the
701 /// 'weak_import' attribute, but may also be marked with an
702 /// 'availability' attribute where we're targing a platform prior to
703 /// the introduction of this feature.
704 bool isWeakImported() const;
705
706 /// Determines whether this symbol can be weak-imported,
707 /// e.g., whether it would be well-formed to add the weak_import
708 /// attribute.
709 ///
710 /// \param IsDefinition Set to \c true to indicate that this
711 /// declaration cannot be weak-imported because it has a definition.
712 bool canBeWeakImported(bool &IsDefinition) const;
713
714 /// Determine whether this declaration came from an AST file (such as
715 /// a precompiled header or module) rather than having been parsed.
716 bool isFromASTFile() const { return FromASTFile; }
717
718 /// Retrieve the global declaration ID associated with this
719 /// declaration, which specifies where this Decl was loaded from.
720 unsigned getGlobalID() const {
721 if (isFromASTFile())
722 return *((const unsigned*)this - 1);
723 return 0;
724 }
725
726 /// Retrieve the global ID of the module that owns this particular
727 /// declaration.
728 unsigned getOwningModuleID() const {
729 if (isFromASTFile())
730 return *((const unsigned*)this - 2);
731 return 0;
732 }
733
734private:
735 Module *getOwningModuleSlow() const;
736
737protected:
738 bool hasLocalOwningModuleStorage() const;
739
740public:
741 /// Get the imported owning module, if this decl is from an imported
742 /// (non-local) module.
743 Module *getImportedOwningModule() const {
744 if (!isFromASTFile() || !hasOwningModule())
745 return nullptr;
746
747 return getOwningModuleSlow();
748 }
749
750 /// Get the local owning module, if known. Returns nullptr if owner is
751 /// not yet known or declaration is not from a module.
752 Module *getLocalOwningModule() const {
753 if (isFromASTFile() || !hasOwningModule())
754 return nullptr;
755
756 assert(hasLocalOwningModuleStorage() &&((hasLocalOwningModuleStorage() && "owned local decl but no local module storage"
) ? static_cast<void> (0) : __assert_fail ("hasLocalOwningModuleStorage() && \"owned local decl but no local module storage\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 757, __PRETTY_FUNCTION__))
757 "owned local decl but no local module storage")((hasLocalOwningModuleStorage() && "owned local decl but no local module storage"
) ? static_cast<void> (0) : __assert_fail ("hasLocalOwningModuleStorage() && \"owned local decl but no local module storage\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 757, __PRETTY_FUNCTION__))
;
758 return reinterpret_cast<Module *const *>(this)[-1];
759 }
760 void setLocalOwningModule(Module *M) {
761 assert(!isFromASTFile() && hasOwningModule() &&((!isFromASTFile() && hasOwningModule() && hasLocalOwningModuleStorage
() && "should not have a cached owning module") ? static_cast
<void> (0) : __assert_fail ("!isFromASTFile() && hasOwningModule() && hasLocalOwningModuleStorage() && \"should not have a cached owning module\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 763, __PRETTY_FUNCTION__))
762 hasLocalOwningModuleStorage() &&((!isFromASTFile() && hasOwningModule() && hasLocalOwningModuleStorage
() && "should not have a cached owning module") ? static_cast
<void> (0) : __assert_fail ("!isFromASTFile() && hasOwningModule() && hasLocalOwningModuleStorage() && \"should not have a cached owning module\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 763, __PRETTY_FUNCTION__))
763 "should not have a cached owning module")((!isFromASTFile() && hasOwningModule() && hasLocalOwningModuleStorage
() && "should not have a cached owning module") ? static_cast
<void> (0) : __assert_fail ("!isFromASTFile() && hasOwningModule() && hasLocalOwningModuleStorage() && \"should not have a cached owning module\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 763, __PRETTY_FUNCTION__))
;
764 reinterpret_cast<Module **>(this)[-1] = M;
765 }
766
767 /// Is this declaration owned by some module?
768 bool hasOwningModule() const {
769 return getModuleOwnershipKind() != ModuleOwnershipKind::Unowned;
770 }
771
772 /// Get the module that owns this declaration (for visibility purposes).
773 Module *getOwningModule() const {
774 return isFromASTFile() ? getImportedOwningModule() : getLocalOwningModule();
775 }
776
777 /// Get the module that owns this declaration for linkage purposes.
778 /// There only ever is such a module under the C++ Modules TS.
779 ///
780 /// \param IgnoreLinkage Ignore the linkage of the entity; assume that
781 /// all declarations in a global module fragment are unowned.
782 Module *getOwningModuleForLinkage(bool IgnoreLinkage = false) const;
783
784 /// Determine whether this declaration is definitely visible to name lookup,
785 /// independent of whether the owning module is visible.
786 /// Note: The declaration may be visible even if this returns \c false if the
787 /// owning module is visible within the query context. This is a low-level
788 /// helper function; most code should be calling Sema::isVisible() instead.
789 bool isUnconditionallyVisible() const {
790 return (int)getModuleOwnershipKind() <= (int)ModuleOwnershipKind::Visible;
791 }
792
793 /// Set that this declaration is globally visible, even if it came from a
794 /// module that is not visible.
795 void setVisibleDespiteOwningModule() {
796 if (!isUnconditionallyVisible())
797 setModuleOwnershipKind(ModuleOwnershipKind::Visible);
798 }
799
800 /// Get the kind of module ownership for this declaration.
801 ModuleOwnershipKind getModuleOwnershipKind() const {
802 return NextInContextAndBits.getInt();
803 }
804
805 /// Set whether this declaration is hidden from name lookup.
806 void setModuleOwnershipKind(ModuleOwnershipKind MOK) {
807 assert(!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned &&((!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned &&
MOK != ModuleOwnershipKind::Unowned && !isFromASTFile
() && !hasLocalOwningModuleStorage()) && "no storage available for owning module for this declaration"
) ? static_cast<void> (0) : __assert_fail ("!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && \"no storage available for owning module for this declaration\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 810, __PRETTY_FUNCTION__))
808 MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() &&((!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned &&
MOK != ModuleOwnershipKind::Unowned && !isFromASTFile
() && !hasLocalOwningModuleStorage()) && "no storage available for owning module for this declaration"
) ? static_cast<void> (0) : __assert_fail ("!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && \"no storage available for owning module for this declaration\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 810, __PRETTY_FUNCTION__))
809 !hasLocalOwningModuleStorage()) &&((!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned &&
MOK != ModuleOwnershipKind::Unowned && !isFromASTFile
() && !hasLocalOwningModuleStorage()) && "no storage available for owning module for this declaration"
) ? static_cast<void> (0) : __assert_fail ("!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && \"no storage available for owning module for this declaration\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 810, __PRETTY_FUNCTION__))
810 "no storage available for owning module for this declaration")((!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned &&
MOK != ModuleOwnershipKind::Unowned && !isFromASTFile
() && !hasLocalOwningModuleStorage()) && "no storage available for owning module for this declaration"
) ? static_cast<void> (0) : __assert_fail ("!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned && MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() && !hasLocalOwningModuleStorage()) && \"no storage available for owning module for this declaration\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 810, __PRETTY_FUNCTION__))
;
811 NextInContextAndBits.setInt(MOK);
812 }
813
814 unsigned getIdentifierNamespace() const {
815 return IdentifierNamespace;
816 }
817
818 bool isInIdentifierNamespace(unsigned NS) const {
819 return getIdentifierNamespace() & NS;
820 }
821
822 static unsigned getIdentifierNamespaceForKind(Kind DK);
823
824 bool hasTagIdentifierNamespace() const {
825 return isTagIdentifierNamespace(getIdentifierNamespace());
826 }
827
828 static bool isTagIdentifierNamespace(unsigned NS) {
829 // TagDecls have Tag and Type set and may also have TagFriend.
830 return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
831 }
832
833 /// getLexicalDeclContext - The declaration context where this Decl was
834 /// lexically declared (LexicalDC). May be different from
835 /// getDeclContext() (SemanticDC).
836 /// e.g.:
837 ///
838 /// namespace A {
839 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
840 /// }
841 /// void A::f(); // SemanticDC == namespace 'A'
842 /// // LexicalDC == global namespace
843 DeclContext *getLexicalDeclContext() {
844 if (isInSemaDC())
845 return getSemanticDC();
846 return getMultipleDC()->LexicalDC;
847 }
848 const DeclContext *getLexicalDeclContext() const {
849 return const_cast<Decl*>(this)->getLexicalDeclContext();
850 }
851
852 /// Determine whether this declaration is declared out of line (outside its
853 /// semantic context).
854 virtual bool isOutOfLine() const;
855
856 /// setDeclContext - Set both the semantic and lexical DeclContext
857 /// to DC.
858 void setDeclContext(DeclContext *DC);
859
860 void setLexicalDeclContext(DeclContext *DC);
861
862 /// Determine whether this declaration is a templated entity (whether it is
863 // within the scope of a template parameter).
864 bool isTemplated() const;
865
866 /// Determine the number of levels of template parameter surrounding this
867 /// declaration.
868 unsigned getTemplateDepth() const;
869
870 /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
871 /// scoped decl is defined outside the current function or method. This is
872 /// roughly global variables and functions, but also handles enums (which
873 /// could be defined inside or outside a function etc).
874 bool isDefinedOutsideFunctionOrMethod() const {
875 return getParentFunctionOrMethod() == nullptr;
876 }
877
878 /// Determine whether a substitution into this declaration would occur as
879 /// part of a substitution into a dependent local scope. Such a substitution
880 /// transitively substitutes into all constructs nested within this
881 /// declaration.
882 ///
883 /// This recognizes non-defining declarations as well as members of local
884 /// classes and lambdas:
885 /// \code
886 /// template<typename T> void foo() { void bar(); }
887 /// template<typename T> void foo2() { class ABC { void bar(); }; }
888 /// template<typename T> inline int x = [](){ return 0; }();
889 /// \endcode
890 bool isInLocalScopeForInstantiation() const;
891
892 /// If this decl is defined inside a function/method/block it returns
893 /// the corresponding DeclContext, otherwise it returns null.
894 const DeclContext *getParentFunctionOrMethod() const;
895 DeclContext *getParentFunctionOrMethod() {
896 return const_cast<DeclContext*>(
897 const_cast<const Decl*>(this)->getParentFunctionOrMethod());
898 }
899
900 /// Retrieves the "canonical" declaration of the given declaration.
901 virtual Decl *getCanonicalDecl() { return this; }
902 const Decl *getCanonicalDecl() const {
903 return const_cast<Decl*>(this)->getCanonicalDecl();
904 }
905
906 /// Whether this particular Decl is a canonical one.
907 bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
908
909protected:
910 /// Returns the next redeclaration or itself if this is the only decl.
911 ///
912 /// Decl subclasses that can be redeclared should override this method so that
913 /// Decl::redecl_iterator can iterate over them.
914 virtual Decl *getNextRedeclarationImpl() { return this; }
915
916 /// Implementation of getPreviousDecl(), to be overridden by any
917 /// subclass that has a redeclaration chain.
918 virtual Decl *getPreviousDeclImpl() { return nullptr; }
919
920 /// Implementation of getMostRecentDecl(), to be overridden by any
921 /// subclass that has a redeclaration chain.
922 virtual Decl *getMostRecentDeclImpl() { return this; }
923
924public:
925 /// Iterates through all the redeclarations of the same decl.
926 class redecl_iterator {
927 /// Current - The current declaration.
928 Decl *Current = nullptr;
929 Decl *Starter;
930
931 public:
932 using value_type = Decl *;
933 using reference = const value_type &;
934 using pointer = const value_type *;
935 using iterator_category = std::forward_iterator_tag;
936 using difference_type = std::ptrdiff_t;
937
938 redecl_iterator() = default;
939 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) {}
940
941 reference operator*() const { return Current; }
942 value_type operator->() const { return Current; }
943
944 redecl_iterator& operator++() {
945 assert(Current && "Advancing while iterator has reached end")((Current && "Advancing while iterator has reached end"
) ? static_cast<void> (0) : __assert_fail ("Current && \"Advancing while iterator has reached end\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 945, __PRETTY_FUNCTION__))
;
946 // Get either previous decl or latest decl.
947 Decl *Next = Current->getNextRedeclarationImpl();
948 assert(Next && "Should return next redeclaration or itself, never null!")((Next && "Should return next redeclaration or itself, never null!"
) ? static_cast<void> (0) : __assert_fail ("Next && \"Should return next redeclaration or itself, never null!\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 948, __PRETTY_FUNCTION__))
;
949 Current = (Next != Starter) ? Next : nullptr;
950 return *this;
951 }
952
953 redecl_iterator operator++(int) {
954 redecl_iterator tmp(*this);
955 ++(*this);
956 return tmp;
957 }
958
959 friend bool operator==(redecl_iterator x, redecl_iterator y) {
960 return x.Current == y.Current;
961 }
962
963 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
964 return x.Current != y.Current;
965 }
966 };
967
968 using redecl_range = llvm::iterator_range<redecl_iterator>;
969
970 /// Returns an iterator range for all the redeclarations of the same
971 /// decl. It will iterate at least once (when this decl is the only one).
972 redecl_range redecls() const {
973 return redecl_range(redecls_begin(), redecls_end());
974 }
975
976 redecl_iterator redecls_begin() const {
977 return redecl_iterator(const_cast<Decl *>(this));
978 }
979
980 redecl_iterator redecls_end() const { return redecl_iterator(); }
981
982 /// Retrieve the previous declaration that declares the same entity
983 /// as this declaration, or NULL if there is no previous declaration.
984 Decl *getPreviousDecl() { return getPreviousDeclImpl(); }
985
986 /// Retrieve the previous declaration that declares the same entity
987 /// as this declaration, or NULL if there is no previous declaration.
988 const Decl *getPreviousDecl() const {
989 return const_cast<Decl *>(this)->getPreviousDeclImpl();
990 }
991
992 /// True if this is the first declaration in its redeclaration chain.
993 bool isFirstDecl() const {
994 return getPreviousDecl() == nullptr;
995 }
996
997 /// Retrieve the most recent declaration that declares the same entity
998 /// as this declaration (which may be this declaration).
999 Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); }
1000
1001 /// Retrieve the most recent declaration that declares the same entity
1002 /// as this declaration (which may be this declaration).
1003 const Decl *getMostRecentDecl() const {
1004 return const_cast<Decl *>(this)->getMostRecentDeclImpl();
1005 }
1006
1007 /// getBody - If this Decl represents a declaration for a body of code,
1008 /// such as a function or method definition, this method returns the
1009 /// top-level Stmt* of that body. Otherwise this method returns null.
1010 virtual Stmt* getBody() const { return nullptr; }
1011
1012 /// Returns true if this \c Decl represents a declaration for a body of
1013 /// code, such as a function or method definition.
1014 /// Note that \c hasBody can also return true if any redeclaration of this
1015 /// \c Decl represents a declaration for a body of code.
1016 virtual bool hasBody() const { return getBody() != nullptr; }
1017
1018 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
1019 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
1020 SourceLocation getBodyRBrace() const;
1021
1022 // global temp stats (until we have a per-module visitor)
1023 static void add(Kind k);
1024 static void EnableStatistics();
1025 static void PrintStats();
1026
1027 /// isTemplateParameter - Determines whether this declaration is a
1028 /// template parameter.
1029 bool isTemplateParameter() const;
1030
1031 /// isTemplateParameter - Determines whether this declaration is a
1032 /// template parameter pack.
1033 bool isTemplateParameterPack() const;
1034
1035 /// Whether this declaration is a parameter pack.
1036 bool isParameterPack() const;
1037
1038 /// returns true if this declaration is a template
1039 bool isTemplateDecl() const;
1040
1041 /// Whether this declaration is a function or function template.
1042 bool isFunctionOrFunctionTemplate() const {
1043 return (DeclKind >= Decl::firstFunction &&
1044 DeclKind <= Decl::lastFunction) ||
1045 DeclKind == FunctionTemplate;
1046 }
1047
1048 /// If this is a declaration that describes some template, this
1049 /// method returns that template declaration.
1050 ///
1051 /// Note that this returns nullptr for partial specializations, because they
1052 /// are not modeled as TemplateDecls. Use getDescribedTemplateParams to handle
1053 /// those cases.
1054 TemplateDecl *getDescribedTemplate() const;
1055
1056 /// If this is a declaration that describes some template or partial
1057 /// specialization, this returns the corresponding template parameter list.
1058 const TemplateParameterList *getDescribedTemplateParams() const;
1059
1060 /// Returns the function itself, or the templated function if this is a
1061 /// function template.
1062 FunctionDecl *getAsFunction() LLVM_READONLY__attribute__((__pure__));
1063
1064 const FunctionDecl *getAsFunction() const {
1065 return const_cast<Decl *>(this)->getAsFunction();
1066 }
1067
1068 /// Changes the namespace of this declaration to reflect that it's
1069 /// a function-local extern declaration.
1070 ///
1071 /// These declarations appear in the lexical context of the extern
1072 /// declaration, but in the semantic context of the enclosing namespace
1073 /// scope.
1074 void setLocalExternDecl() {
1075 Decl *Prev = getPreviousDecl();
1076 IdentifierNamespace &= ~IDNS_Ordinary;
1077
1078 // It's OK for the declaration to still have the "invisible friend" flag or
1079 // the "conflicts with tag declarations in this scope" flag for the outer
1080 // scope.
1081 assert((IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 &&(((IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag
)) == 0 && "namespace is not ordinary") ? static_cast
<void> (0) : __assert_fail ("(IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 && \"namespace is not ordinary\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1082, __PRETTY_FUNCTION__))
1082 "namespace is not ordinary")(((IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag
)) == 0 && "namespace is not ordinary") ? static_cast
<void> (0) : __assert_fail ("(IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 && \"namespace is not ordinary\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1082, __PRETTY_FUNCTION__))
;
1083
1084 IdentifierNamespace |= IDNS_LocalExtern;
1085 if (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary)
1086 IdentifierNamespace |= IDNS_Ordinary;
1087 }
1088
1089 /// Determine whether this is a block-scope declaration with linkage.
1090 /// This will either be a local variable declaration declared 'extern', or a
1091 /// local function declaration.
1092 bool isLocalExternDecl() {
1093 return IdentifierNamespace & IDNS_LocalExtern;
1094 }
1095
1096 /// Changes the namespace of this declaration to reflect that it's
1097 /// the object of a friend declaration.
1098 ///
1099 /// These declarations appear in the lexical context of the friending
1100 /// class, but in the semantic context of the actual entity. This property
1101 /// applies only to a specific decl object; other redeclarations of the
1102 /// same entity may not (and probably don't) share this property.
1103 void setObjectOfFriendDecl(bool PerformFriendInjection = false) {
1104 unsigned OldNS = IdentifierNamespace;
1105 assert((OldNS & (IDNS_Tag | IDNS_Ordinary |(((OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend
| IDNS_LocalExtern | IDNS_NonMemberOperator)) && "namespace includes neither ordinary nor tag"
) ? static_cast<void> (0) : __assert_fail ("(OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes neither ordinary nor tag\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1108, __PRETTY_FUNCTION__))
1106 IDNS_TagFriend | IDNS_OrdinaryFriend |(((OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend
| IDNS_LocalExtern | IDNS_NonMemberOperator)) && "namespace includes neither ordinary nor tag"
) ? static_cast<void> (0) : __assert_fail ("(OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes neither ordinary nor tag\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1108, __PRETTY_FUNCTION__))
1107 IDNS_LocalExtern | IDNS_NonMemberOperator)) &&(((OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend
| IDNS_LocalExtern | IDNS_NonMemberOperator)) && "namespace includes neither ordinary nor tag"
) ? static_cast<void> (0) : __assert_fail ("(OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes neither ordinary nor tag\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1108, __PRETTY_FUNCTION__))
1108 "namespace includes neither ordinary nor tag")(((OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend
| IDNS_LocalExtern | IDNS_NonMemberOperator)) && "namespace includes neither ordinary nor tag"
) ? static_cast<void> (0) : __assert_fail ("(OldNS & (IDNS_Tag | IDNS_Ordinary | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes neither ordinary nor tag\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1108, __PRETTY_FUNCTION__))
;
1109 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |((!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend
| IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator
)) && "namespace includes other than ordinary or tag"
) ? static_cast<void> (0) : __assert_fail ("!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes other than ordinary or tag\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1112, __PRETTY_FUNCTION__))
1110 IDNS_TagFriend | IDNS_OrdinaryFriend |((!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend
| IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator
)) && "namespace includes other than ordinary or tag"
) ? static_cast<void> (0) : __assert_fail ("!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes other than ordinary or tag\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1112, __PRETTY_FUNCTION__))
1111 IDNS_LocalExtern | IDNS_NonMemberOperator)) &&((!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend
| IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator
)) && "namespace includes other than ordinary or tag"
) ? static_cast<void> (0) : __assert_fail ("!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes other than ordinary or tag\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1112, __PRETTY_FUNCTION__))
1112 "namespace includes other than ordinary or tag")((!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend
| IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator
)) && "namespace includes other than ordinary or tag"
) ? static_cast<void> (0) : __assert_fail ("!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type | IDNS_TagFriend | IDNS_OrdinaryFriend | IDNS_LocalExtern | IDNS_NonMemberOperator)) && \"namespace includes other than ordinary or tag\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1112, __PRETTY_FUNCTION__))
;
1113
1114 Decl *Prev = getPreviousDecl();
1115 IdentifierNamespace &= ~(IDNS_Ordinary | IDNS_Tag | IDNS_Type);
1116
1117 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
1118 IdentifierNamespace |= IDNS_TagFriend;
1119 if (PerformFriendInjection ||
1120 (Prev && Prev->getIdentifierNamespace() & IDNS_Tag))
1121 IdentifierNamespace |= IDNS_Tag | IDNS_Type;
1122 }
1123
1124 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend |
1125 IDNS_LocalExtern | IDNS_NonMemberOperator)) {
1126 IdentifierNamespace |= IDNS_OrdinaryFriend;
1127 if (PerformFriendInjection ||
1128 (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary))
1129 IdentifierNamespace |= IDNS_Ordinary;
1130 }
1131 }
1132
1133 enum FriendObjectKind {
1134 FOK_None, ///< Not a friend object.
1135 FOK_Declared, ///< A friend of a previously-declared entity.
1136 FOK_Undeclared ///< A friend of a previously-undeclared entity.
1137 };
1138
1139 /// Determines whether this declaration is the object of a
1140 /// friend declaration and, if so, what kind.
1141 ///
1142 /// There is currently no direct way to find the associated FriendDecl.
1143 FriendObjectKind getFriendObjectKind() const {
1144 unsigned mask =
1145 (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
1146 if (!mask) return FOK_None;
1147 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? FOK_Declared
1148 : FOK_Undeclared);
1149 }
1150
1151 /// Specifies that this declaration is a C++ overloaded non-member.
1152 void setNonMemberOperator() {
1153 assert(getKind() == Function || getKind() == FunctionTemplate)((getKind() == Function || getKind() == FunctionTemplate) ? static_cast
<void> (0) : __assert_fail ("getKind() == Function || getKind() == FunctionTemplate"
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1153, __PRETTY_FUNCTION__))
;
1154 assert((IdentifierNamespace & IDNS_Ordinary) &&(((IdentifierNamespace & IDNS_Ordinary) && "visible non-member operators should be in ordinary namespace"
) ? static_cast<void> (0) : __assert_fail ("(IdentifierNamespace & IDNS_Ordinary) && \"visible non-member operators should be in ordinary namespace\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1155, __PRETTY_FUNCTION__))
1155 "visible non-member operators should be in ordinary namespace")(((IdentifierNamespace & IDNS_Ordinary) && "visible non-member operators should be in ordinary namespace"
) ? static_cast<void> (0) : __assert_fail ("(IdentifierNamespace & IDNS_Ordinary) && \"visible non-member operators should be in ordinary namespace\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 1155, __PRETTY_FUNCTION__))
;
1156 IdentifierNamespace |= IDNS_NonMemberOperator;
1157 }
1158
1159 static bool classofKind(Kind K) { return true; }
1160 static DeclContext *castToDeclContext(const Decl *);
1161 static Decl *castFromDeclContext(const DeclContext *);
1162
1163 void print(raw_ostream &Out, unsigned Indentation = 0,
1164 bool PrintInstantiation = false) const;
1165 void print(raw_ostream &Out, const PrintingPolicy &Policy,
1166 unsigned Indentation = 0, bool PrintInstantiation = false) const;
1167 static void printGroup(Decl** Begin, unsigned NumDecls,
1168 raw_ostream &Out, const PrintingPolicy &Policy,
1169 unsigned Indentation = 0);
1170
1171 // Debuggers don't usually respect default arguments.
1172 void dump() const;
1173
1174 // Same as dump(), but forces color printing.
1175 void dumpColor() const;
1176
1177 void dump(raw_ostream &Out, bool Deserialize = false,
1178 ASTDumpOutputFormat OutputFormat = ADOF_Default) const;
1179
1180 /// \return Unique reproducible object identifier
1181 int64_t getID() const;
1182
1183 /// Looks through the Decl's underlying type to extract a FunctionType
1184 /// when possible. Will return null if the type underlying the Decl does not
1185 /// have a FunctionType.
1186 const FunctionType *getFunctionType(bool BlocksToo = true) const;
1187
1188private:
1189 void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
1190 void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
1191 ASTContext &Ctx);
1192
1193protected:
1194 ASTMutationListener *getASTMutationListener() const;
1195};
1196
1197/// Determine whether two declarations declare the same entity.
1198inline bool declaresSameEntity(const Decl *D1, const Decl *D2) {
1199 if (!D1 || !D2)
1200 return false;
1201
1202 if (D1 == D2)
1203 return true;
1204
1205 return D1->getCanonicalDecl() == D2->getCanonicalDecl();
1206}
1207
1208/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
1209/// doing something to a specific decl.
1210class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
1211 const Decl *TheDecl;
1212 SourceLocation Loc;
1213 SourceManager &SM;
1214 const char *Message;
1215
1216public:
1217 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
1218 SourceManager &sm, const char *Msg)
1219 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
1220
1221 void print(raw_ostream &OS) const override;
1222};
1223
1224/// The results of name lookup within a DeclContext. This is either a
1225/// single result (with no stable storage) or a collection of results (with
1226/// stable storage provided by the lookup table).
1227class DeclContextLookupResult {
1228 using ResultTy = ArrayRef<NamedDecl *>;
1229
1230 ResultTy Result;
1231
1232 // If there is only one lookup result, it would be invalidated by
1233 // reallocations of the name table, so store it separately.
1234 NamedDecl *Single = nullptr;
1235
1236 static NamedDecl *const SingleElementDummyList;
1237
1238public:
1239 DeclContextLookupResult() = default;
1240 DeclContextLookupResult(ArrayRef<NamedDecl *> Result)
1241 : Result(Result) {}
1242 DeclContextLookupResult(NamedDecl *Single)
1243 : Result(SingleElementDummyList), Single(Single) {}
1244
1245 class iterator;
1246
1247 using IteratorBase =
1248 llvm::iterator_adaptor_base<iterator, ResultTy::iterator,
1249 std::random_access_iterator_tag, NamedDecl *>;
1250
1251 class iterator : public IteratorBase {
1252 value_type SingleElement;
1253
1254 public:
1255 explicit iterator(pointer Pos, value_type Single = nullptr)
1256 : IteratorBase(Pos), SingleElement(Single) {}
1257
1258 reference operator*() const {
1259 return SingleElement ? SingleElement : IteratorBase::operator*();
1260 }
1261 };
1262
1263 using const_iterator = iterator;
1264 using pointer = iterator::pointer;
1265 using reference = iterator::reference;
1266
1267 iterator begin() const { return iterator(Result.begin(), Single); }
1268 iterator end() const { return iterator(Result.end(), Single); }
1269
1270 bool empty() const { return Result.empty(); }
1271 pointer data() const { return Single ? &Single : Result.data(); }
1272 size_t size() const { return Single ? 1 : Result.size(); }
1273 reference front() const { return Single ? Single : Result.front(); }
1274 reference back() const { return Single ? Single : Result.back(); }
1275 reference operator[](size_t N) const { return Single ? Single : Result[N]; }
1276
1277 // FIXME: Remove this from the interface
1278 DeclContextLookupResult slice(size_t N) const {
1279 DeclContextLookupResult Sliced = Result.slice(N);
1280 Sliced.Single = Single;
1281 return Sliced;
1282 }
1283};
1284
1285/// DeclContext - This is used only as base class of specific decl types that
1286/// can act as declaration contexts. These decls are (only the top classes
1287/// that directly derive from DeclContext are mentioned, not their subclasses):
1288///
1289/// TranslationUnitDecl
1290/// ExternCContext
1291/// NamespaceDecl
1292/// TagDecl
1293/// OMPDeclareReductionDecl
1294/// OMPDeclareMapperDecl
1295/// FunctionDecl
1296/// ObjCMethodDecl
1297/// ObjCContainerDecl
1298/// LinkageSpecDecl
1299/// ExportDecl
1300/// BlockDecl
1301/// CapturedDecl
1302class DeclContext {
1303 /// For makeDeclVisibleInContextImpl
1304 friend class ASTDeclReader;
1305 /// For reconcileExternalVisibleStorage, CreateStoredDeclsMap,
1306 /// hasNeedToReconcileExternalVisibleStorage
1307 friend class ExternalASTSource;
1308 /// For CreateStoredDeclsMap
1309 friend class DependentDiagnostic;
1310 /// For hasNeedToReconcileExternalVisibleStorage,
1311 /// hasLazyLocalLexicalLookups, hasLazyExternalLexicalLookups
1312 friend class ASTWriter;
1313
1314 // We use uint64_t in the bit-fields below since some bit-fields
1315 // cross the unsigned boundary and this breaks the packing.
1316
1317 /// Stores the bits used by DeclContext.
1318 /// If modified NumDeclContextBit, the ctor of DeclContext and the accessor
1319 /// methods in DeclContext should be updated appropriately.
1320 class DeclContextBitfields {
1321 friend class DeclContext;
1322 /// DeclKind - This indicates which class this is.
1323 uint64_t DeclKind : 7;
1324
1325 /// Whether this declaration context also has some external
1326 /// storage that contains additional declarations that are lexically
1327 /// part of this context.
1328 mutable uint64_t ExternalLexicalStorage : 1;
1329
1330 /// Whether this declaration context also has some external
1331 /// storage that contains additional declarations that are visible
1332 /// in this context.
1333 mutable uint64_t ExternalVisibleStorage : 1;
1334
1335 /// Whether this declaration context has had externally visible
1336 /// storage added since the last lookup. In this case, \c LookupPtr's
1337 /// invariant may not hold and needs to be fixed before we perform
1338 /// another lookup.
1339 mutable uint64_t NeedToReconcileExternalVisibleStorage : 1;
1340
1341 /// If \c true, this context may have local lexical declarations
1342 /// that are missing from the lookup table.
1343 mutable uint64_t HasLazyLocalLexicalLookups : 1;
1344
1345 /// If \c true, the external source may have lexical declarations
1346 /// that are missing from the lookup table.
1347 mutable uint64_t HasLazyExternalLexicalLookups : 1;
1348
1349 /// If \c true, lookups should only return identifier from
1350 /// DeclContext scope (for example TranslationUnit). Used in
1351 /// LookupQualifiedName()
1352 mutable uint64_t UseQualifiedLookup : 1;
1353 };
1354
1355 /// Number of bits in DeclContextBitfields.
1356 enum { NumDeclContextBits = 13 };
1357
1358 /// Stores the bits used by TagDecl.
1359 /// If modified NumTagDeclBits and the accessor
1360 /// methods in TagDecl should be updated appropriately.
1361 class TagDeclBitfields {
1362 friend class TagDecl;
1363 /// For the bits in DeclContextBitfields
1364 uint64_t : NumDeclContextBits;
1365
1366 /// The TagKind enum.
1367 uint64_t TagDeclKind : 3;
1368
1369 /// True if this is a definition ("struct foo {};"), false if it is a
1370 /// declaration ("struct foo;"). It is not considered a definition
1371 /// until the definition has been fully processed.
1372 uint64_t IsCompleteDefinition : 1;
1373
1374 /// True if this is currently being defined.
1375 uint64_t IsBeingDefined : 1;
1376
1377 /// True if this tag declaration is "embedded" (i.e., defined or declared
1378 /// for the very first time) in the syntax of a declarator.
1379 uint64_t IsEmbeddedInDeclarator : 1;
1380
1381 /// True if this tag is free standing, e.g. "struct foo;".
1382 uint64_t IsFreeStanding : 1;
1383
1384 /// Indicates whether it is possible for declarations of this kind
1385 /// to have an out-of-date definition.
1386 ///
1387 /// This option is only enabled when modules are enabled.
1388 uint64_t MayHaveOutOfDateDef : 1;
1389
1390 /// Has the full definition of this type been required by a use somewhere in
1391 /// the TU.
1392 uint64_t IsCompleteDefinitionRequired : 1;
1393 };
1394
1395 /// Number of non-inherited bits in TagDeclBitfields.
1396 enum { NumTagDeclBits = 9 };
1397
1398 /// Stores the bits used by EnumDecl.
1399 /// If modified NumEnumDeclBit and the accessor
1400 /// methods in EnumDecl should be updated appropriately.
1401 class EnumDeclBitfields {
1402 friend class EnumDecl;
1403 /// For the bits in DeclContextBitfields.
1404 uint64_t : NumDeclContextBits;
1405 /// For the bits in TagDeclBitfields.
1406 uint64_t : NumTagDeclBits;
1407
1408 /// Width in bits required to store all the non-negative
1409 /// enumerators of this enum.
1410 uint64_t NumPositiveBits : 8;
1411
1412 /// Width in bits required to store all the negative
1413 /// enumerators of this enum.
1414 uint64_t NumNegativeBits : 8;
1415
1416 /// True if this tag declaration is a scoped enumeration. Only
1417 /// possible in C++11 mode.
1418 uint64_t IsScoped : 1;
1419
1420 /// If this tag declaration is a scoped enum,
1421 /// then this is true if the scoped enum was declared using the class
1422 /// tag, false if it was declared with the struct tag. No meaning is
1423 /// associated if this tag declaration is not a scoped enum.
1424 uint64_t IsScopedUsingClassTag : 1;
1425
1426 /// True if this is an enumeration with fixed underlying type. Only
1427 /// possible in C++11, Microsoft extensions, or Objective C mode.
1428 uint64_t IsFixed : 1;
1429
1430 /// True if a valid hash is stored in ODRHash.
1431 uint64_t HasODRHash : 1;
1432 };
1433
1434 /// Number of non-inherited bits in EnumDeclBitfields.
1435 enum { NumEnumDeclBits = 20 };
1436
1437 /// Stores the bits used by RecordDecl.
1438 /// If modified NumRecordDeclBits and the accessor
1439 /// methods in RecordDecl should be updated appropriately.
1440 class RecordDeclBitfields {
1441 friend class RecordDecl;
1442 /// For the bits in DeclContextBitfields.
1443 uint64_t : NumDeclContextBits;
1444 /// For the bits in TagDeclBitfields.
1445 uint64_t : NumTagDeclBits;
1446
1447 /// This is true if this struct ends with a flexible
1448 /// array member (e.g. int X[]) or if this union contains a struct that does.
1449 /// If so, this cannot be contained in arrays or other structs as a member.
1450 uint64_t HasFlexibleArrayMember : 1;
1451
1452 /// Whether this is the type of an anonymous struct or union.
1453 uint64_t AnonymousStructOrUnion : 1;
1454
1455 /// This is true if this struct has at least one member
1456 /// containing an Objective-C object pointer type.
1457 uint64_t HasObjectMember : 1;
1458
1459 /// This is true if struct has at least one member of
1460 /// 'volatile' type.
1461 uint64_t HasVolatileMember : 1;
1462
1463 /// Whether the field declarations of this record have been loaded
1464 /// from external storage. To avoid unnecessary deserialization of
1465 /// methods/nested types we allow deserialization of just the fields
1466 /// when needed.
1467 mutable uint64_t LoadedFieldsFromExternalStorage : 1;
1468
1469 /// Basic properties of non-trivial C structs.
1470 uint64_t NonTrivialToPrimitiveDefaultInitialize : 1;
1471 uint64_t NonTrivialToPrimitiveCopy : 1;
1472 uint64_t NonTrivialToPrimitiveDestroy : 1;
1473
1474 /// The following bits indicate whether this is or contains a C union that
1475 /// is non-trivial to default-initialize, destruct, or copy. These bits
1476 /// imply the associated basic non-triviality predicates declared above.
1477 uint64_t HasNonTrivialToPrimitiveDefaultInitializeCUnion : 1;
1478 uint64_t HasNonTrivialToPrimitiveDestructCUnion : 1;
1479 uint64_t HasNonTrivialToPrimitiveCopyCUnion : 1;
1480
1481 /// Indicates whether this struct is destroyed in the callee.
1482 uint64_t ParamDestroyedInCallee : 1;
1483
1484 /// Represents the way this type is passed to a function.
1485 uint64_t ArgPassingRestrictions : 2;
1486 };
1487
1488 /// Number of non-inherited bits in RecordDeclBitfields.
1489 enum { NumRecordDeclBits = 14 };
1490
1491 /// Stores the bits used by OMPDeclareReductionDecl.
1492 /// If modified NumOMPDeclareReductionDeclBits and the accessor
1493 /// methods in OMPDeclareReductionDecl should be updated appropriately.
1494 class OMPDeclareReductionDeclBitfields {
1495 friend class OMPDeclareReductionDecl;
1496 /// For the bits in DeclContextBitfields
1497 uint64_t : NumDeclContextBits;
1498
1499 /// Kind of initializer,
1500 /// function call or omp_priv<init_expr> initializtion.
1501 uint64_t InitializerKind : 2;
1502 };
1503
1504 /// Number of non-inherited bits in OMPDeclareReductionDeclBitfields.
1505 enum { NumOMPDeclareReductionDeclBits = 2 };
1506
1507 /// Stores the bits used by FunctionDecl.
1508 /// If modified NumFunctionDeclBits and the accessor
1509 /// methods in FunctionDecl and CXXDeductionGuideDecl
1510 /// (for IsCopyDeductionCandidate) should be updated appropriately.
1511 class FunctionDeclBitfields {
1512 friend class FunctionDecl;
1513 /// For IsCopyDeductionCandidate
1514 friend class CXXDeductionGuideDecl;
1515 /// For the bits in DeclContextBitfields.
1516 uint64_t : NumDeclContextBits;
1517
1518 uint64_t SClass : 3;
1519 uint64_t IsInline : 1;
1520 uint64_t IsInlineSpecified : 1;
1521
1522 uint64_t IsVirtualAsWritten : 1;
1523 uint64_t IsPure : 1;
1524 uint64_t HasInheritedPrototype : 1;
1525 uint64_t HasWrittenPrototype : 1;
1526 uint64_t IsDeleted : 1;
1527 /// Used by CXXMethodDecl
1528 uint64_t IsTrivial : 1;
1529
1530 /// This flag indicates whether this function is trivial for the purpose of
1531 /// calls. This is meaningful only when this function is a copy/move
1532 /// constructor or a destructor.
1533 uint64_t IsTrivialForCall : 1;
1534
1535 uint64_t IsDefaulted : 1;
1536 uint64_t IsExplicitlyDefaulted : 1;
1537 uint64_t HasDefaultedFunctionInfo : 1;
1538 uint64_t HasImplicitReturnZero : 1;
1539 uint64_t IsLateTemplateParsed : 1;
1540
1541 /// Kind of contexpr specifier as defined by ConstexprSpecKind.
1542 uint64_t ConstexprKind : 2;
1543 uint64_t InstantiationIsPending : 1;
1544
1545 /// Indicates if the function uses __try.
1546 uint64_t UsesSEHTry : 1;
1547
1548 /// Indicates if the function was a definition
1549 /// but its body was skipped.
1550 uint64_t HasSkippedBody : 1;
1551
1552 /// Indicates if the function declaration will
1553 /// have a body, once we're done parsing it.
1554 uint64_t WillHaveBody : 1;
1555
1556 /// Indicates that this function is a multiversioned
1557 /// function using attribute 'target'.
1558 uint64_t IsMultiVersion : 1;
1559
1560 /// [C++17] Only used by CXXDeductionGuideDecl. Indicates that
1561 /// the Deduction Guide is the implicitly generated 'copy
1562 /// deduction candidate' (is used during overload resolution).
1563 uint64_t IsCopyDeductionCandidate : 1;
1564
1565 /// Store the ODRHash after first calculation.
1566 uint64_t HasODRHash : 1;
1567
1568 /// Indicates if the function uses Floating Point Constrained Intrinsics
1569 uint64_t UsesFPIntrin : 1;
1570 };
1571
1572 /// Number of non-inherited bits in FunctionDeclBitfields.
1573 enum { NumFunctionDeclBits = 27 };
1574
1575 /// Stores the bits used by CXXConstructorDecl. If modified
1576 /// NumCXXConstructorDeclBits and the accessor
1577 /// methods in CXXConstructorDecl should be updated appropriately.
1578 class CXXConstructorDeclBitfields {
1579 friend class CXXConstructorDecl;
1580 /// For the bits in DeclContextBitfields.
1581 uint64_t : NumDeclContextBits;
1582 /// For the bits in FunctionDeclBitfields.
1583 uint64_t : NumFunctionDeclBits;
1584
1585 /// 24 bits to fit in the remaining available space.
1586 /// Note that this makes CXXConstructorDeclBitfields take
1587 /// exactly 64 bits and thus the width of NumCtorInitializers
1588 /// will need to be shrunk if some bit is added to NumDeclContextBitfields,
1589 /// NumFunctionDeclBitfields or CXXConstructorDeclBitfields.
1590 uint64_t NumCtorInitializers : 21;
1591 uint64_t IsInheritingConstructor : 1;
1592
1593 /// Whether this constructor has a trail-allocated explicit specifier.
1594 uint64_t HasTrailingExplicitSpecifier : 1;
1595 /// If this constructor does't have a trail-allocated explicit specifier.
1596 /// Whether this constructor is explicit specified.
1597 uint64_t IsSimpleExplicit : 1;
1598 };
1599
1600 /// Number of non-inherited bits in CXXConstructorDeclBitfields.
1601 enum {
1602 NumCXXConstructorDeclBits = 64 - NumDeclContextBits - NumFunctionDeclBits
1603 };
1604
1605 /// Stores the bits used by ObjCMethodDecl.
1606 /// If modified NumObjCMethodDeclBits and the accessor
1607 /// methods in ObjCMethodDecl should be updated appropriately.
1608 class ObjCMethodDeclBitfields {
1609 friend class ObjCMethodDecl;
1610
1611 /// For the bits in DeclContextBitfields.
1612 uint64_t : NumDeclContextBits;
1613
1614 /// The conventional meaning of this method; an ObjCMethodFamily.
1615 /// This is not serialized; instead, it is computed on demand and
1616 /// cached.
1617 mutable uint64_t Family : ObjCMethodFamilyBitWidth;
1618
1619 /// instance (true) or class (false) method.
1620 uint64_t IsInstance : 1;
1621 uint64_t IsVariadic : 1;
1622
1623 /// True if this method is the getter or setter for an explicit property.
1624 uint64_t IsPropertyAccessor : 1;
1625
1626 /// True if this method is a synthesized property accessor stub.
1627 uint64_t IsSynthesizedAccessorStub : 1;
1628
1629 /// Method has a definition.
1630 uint64_t IsDefined : 1;
1631
1632 /// Method redeclaration in the same interface.
1633 uint64_t IsRedeclaration : 1;
1634
1635 /// Is redeclared in the same interface.
1636 mutable uint64_t HasRedeclaration : 1;
1637
1638 /// \@required/\@optional
1639 uint64_t DeclImplementation : 2;
1640
1641 /// in, inout, etc.
1642 uint64_t objcDeclQualifier : 7;
1643
1644 /// Indicates whether this method has a related result type.
1645 uint64_t RelatedResultType : 1;
1646
1647 /// Whether the locations of the selector identifiers are in a
1648 /// "standard" position, a enum SelectorLocationsKind.
1649 uint64_t SelLocsKind : 2;
1650
1651 /// Whether this method overrides any other in the class hierarchy.
1652 ///
1653 /// A method is said to override any method in the class's
1654 /// base classes, its protocols, or its categories' protocols, that has
1655 /// the same selector and is of the same kind (class or instance).
1656 /// A method in an implementation is not considered as overriding the same
1657 /// method in the interface or its categories.
1658 uint64_t IsOverriding : 1;
1659
1660 /// Indicates if the method was a definition but its body was skipped.
1661 uint64_t HasSkippedBody : 1;
1662 };
1663
1664 /// Number of non-inherited bits in ObjCMethodDeclBitfields.
1665 enum { NumObjCMethodDeclBits = 24 };
1666
1667 /// Stores the bits used by ObjCContainerDecl.
1668 /// If modified NumObjCContainerDeclBits and the accessor
1669 /// methods in ObjCContainerDecl should be updated appropriately.
1670 class ObjCContainerDeclBitfields {
1671 friend class ObjCContainerDecl;
1672 /// For the bits in DeclContextBitfields
1673 uint32_t : NumDeclContextBits;
1674
1675 // Not a bitfield but this saves space.
1676 // Note that ObjCContainerDeclBitfields is full.
1677 SourceLocation AtStart;
1678 };
1679
1680 /// Number of non-inherited bits in ObjCContainerDeclBitfields.
1681 /// Note that here we rely on the fact that SourceLocation is 32 bits
1682 /// wide. We check this with the static_assert in the ctor of DeclContext.
1683 enum { NumObjCContainerDeclBits = 64 - NumDeclContextBits };
1684
1685 /// Stores the bits used by LinkageSpecDecl.
1686 /// If modified NumLinkageSpecDeclBits and the accessor
1687 /// methods in LinkageSpecDecl should be updated appropriately.
1688 class LinkageSpecDeclBitfields {
1689 friend class LinkageSpecDecl;
1690 /// For the bits in DeclContextBitfields.
1691 uint64_t : NumDeclContextBits;
1692
1693 /// The language for this linkage specification with values
1694 /// in the enum LinkageSpecDecl::LanguageIDs.
1695 uint64_t Language : 3;
1696
1697 /// True if this linkage spec has braces.
1698 /// This is needed so that hasBraces() returns the correct result while the
1699 /// linkage spec body is being parsed. Once RBraceLoc has been set this is
1700 /// not used, so it doesn't need to be serialized.
1701 uint64_t HasBraces : 1;
1702 };
1703
1704 /// Number of non-inherited bits in LinkageSpecDeclBitfields.
1705 enum { NumLinkageSpecDeclBits = 4 };
1706
1707 /// Stores the bits used by BlockDecl.
1708 /// If modified NumBlockDeclBits and the accessor
1709 /// methods in BlockDecl should be updated appropriately.
1710 class BlockDeclBitfields {
1711 friend class BlockDecl;
1712 /// For the bits in DeclContextBitfields.
1713 uint64_t : NumDeclContextBits;
1714
1715 uint64_t IsVariadic : 1;
1716 uint64_t CapturesCXXThis : 1;
1717 uint64_t BlockMissingReturnType : 1;
1718 uint64_t IsConversionFromLambda : 1;
1719
1720 /// A bit that indicates this block is passed directly to a function as a
1721 /// non-escaping parameter.
1722 uint64_t DoesNotEscape : 1;
1723
1724 /// A bit that indicates whether it's possible to avoid coying this block to
1725 /// the heap when it initializes or is assigned to a local variable with
1726 /// automatic storage.
1727 uint64_t CanAvoidCopyToHeap : 1;
1728 };
1729
1730 /// Number of non-inherited bits in BlockDeclBitfields.
1731 enum { NumBlockDeclBits = 5 };
1732
1733 /// Pointer to the data structure used to lookup declarations
1734 /// within this context (or a DependentStoredDeclsMap if this is a
1735 /// dependent context). We maintain the invariant that, if the map
1736 /// contains an entry for a DeclarationName (and we haven't lazily
1737 /// omitted anything), then it contains all relevant entries for that
1738 /// name (modulo the hasExternalDecls() flag).
1739 mutable StoredDeclsMap *LookupPtr = nullptr;
1740
1741protected:
1742 /// This anonymous union stores the bits belonging to DeclContext and classes
1743 /// deriving from it. The goal is to use otherwise wasted
1744 /// space in DeclContext to store data belonging to derived classes.
1745 /// The space saved is especially significient when pointers are aligned
1746 /// to 8 bytes. In this case due to alignment requirements we have a
1747 /// little less than 8 bytes free in DeclContext which we can use.
1748 /// We check that none of the classes in this union is larger than
1749 /// 8 bytes with static_asserts in the ctor of DeclContext.
1750 union {
1751 DeclContextBitfields DeclContextBits;
1752 TagDeclBitfields TagDeclBits;
1753 EnumDeclBitfields EnumDeclBits;
1754 RecordDeclBitfields RecordDeclBits;
1755 OMPDeclareReductionDeclBitfields OMPDeclareReductionDeclBits;
1756 FunctionDeclBitfields FunctionDeclBits;
1757 CXXConstructorDeclBitfields CXXConstructorDeclBits;
1758 ObjCMethodDeclBitfields ObjCMethodDeclBits;
1759 ObjCContainerDeclBitfields ObjCContainerDeclBits;
1760 LinkageSpecDeclBitfields LinkageSpecDeclBits;
1761 BlockDeclBitfields BlockDeclBits;
1762
1763 static_assert(sizeof(DeclContextBitfields) <= 8,
1764 "DeclContextBitfields is larger than 8 bytes!");
1765 static_assert(sizeof(TagDeclBitfields) <= 8,
1766 "TagDeclBitfields is larger than 8 bytes!");
1767 static_assert(sizeof(EnumDeclBitfields) <= 8,
1768 "EnumDeclBitfields is larger than 8 bytes!");
1769 static_assert(sizeof(RecordDeclBitfields) <= 8,
1770 "RecordDeclBitfields is larger than 8 bytes!");
1771 static_assert(sizeof(OMPDeclareReductionDeclBitfields) <= 8,
1772 "OMPDeclareReductionDeclBitfields is larger than 8 bytes!");
1773 static_assert(sizeof(FunctionDeclBitfields) <= 8,
1774 "FunctionDeclBitfields is larger than 8 bytes!");
1775 static_assert(sizeof(CXXConstructorDeclBitfields) <= 8,
1776 "CXXConstructorDeclBitfields is larger than 8 bytes!");
1777 static_assert(sizeof(ObjCMethodDeclBitfields) <= 8,
1778 "ObjCMethodDeclBitfields is larger than 8 bytes!");
1779 static_assert(sizeof(ObjCContainerDeclBitfields) <= 8,
1780 "ObjCContainerDeclBitfields is larger than 8 bytes!");
1781 static_assert(sizeof(LinkageSpecDeclBitfields) <= 8,
1782 "LinkageSpecDeclBitfields is larger than 8 bytes!");
1783 static_assert(sizeof(BlockDeclBitfields) <= 8,
1784 "BlockDeclBitfields is larger than 8 bytes!");
1785 };
1786
1787 /// FirstDecl - The first declaration stored within this declaration
1788 /// context.
1789 mutable Decl *FirstDecl = nullptr;
1790
1791 /// LastDecl - The last declaration stored within this declaration
1792 /// context. FIXME: We could probably cache this value somewhere
1793 /// outside of the DeclContext, to reduce the size of DeclContext by
1794 /// another pointer.
1795 mutable Decl *LastDecl = nullptr;
1796
1797 /// Build up a chain of declarations.
1798 ///
1799 /// \returns the first/last pair of declarations.
1800 static std::pair<Decl *, Decl *>
1801 BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
1802
1803 DeclContext(Decl::Kind K);
1804
1805public:
1806 ~DeclContext();
1807
1808 Decl::Kind getDeclKind() const {
1809 return static_cast<Decl::Kind>(DeclContextBits.DeclKind);
1810 }
1811
1812 const char *getDeclKindName() const;
1813
1814 /// getParent - Returns the containing DeclContext.
1815 DeclContext *getParent() {
1816 return cast<Decl>(this)->getDeclContext();
1817 }
1818 const DeclContext *getParent() const {
1819 return const_cast<DeclContext*>(this)->getParent();
1820 }
1821
1822 /// getLexicalParent - Returns the containing lexical DeclContext. May be
1823 /// different from getParent, e.g.:
1824 ///
1825 /// namespace A {
1826 /// struct S;
1827 /// }
1828 /// struct A::S {}; // getParent() == namespace 'A'
1829 /// // getLexicalParent() == translation unit
1830 ///
1831 DeclContext *getLexicalParent() {
1832 return cast<Decl>(this)->getLexicalDeclContext();
1833 }
1834 const DeclContext *getLexicalParent() const {
1835 return const_cast<DeclContext*>(this)->getLexicalParent();
1836 }
1837
1838 DeclContext *getLookupParent();
1839
1840 const DeclContext *getLookupParent() const {
1841 return const_cast<DeclContext*>(this)->getLookupParent();
1842 }
1843
1844 ASTContext &getParentASTContext() const {
1845 return cast<Decl>(this)->getASTContext();
1846 }
1847
1848 bool isClosure() const { return getDeclKind() == Decl::Block; }
1849
1850 /// Return this DeclContext if it is a BlockDecl. Otherwise, return the
1851 /// innermost enclosing BlockDecl or null if there are no enclosing blocks.
1852 const BlockDecl *getInnermostBlockDecl() const;
1853
1854 bool isObjCContainer() const {
1855 switch (getDeclKind()) {
1856 case Decl::ObjCCategory:
1857 case Decl::ObjCCategoryImpl:
1858 case Decl::ObjCImplementation:
1859 case Decl::ObjCInterface:
1860 case Decl::ObjCProtocol:
1861 return true;
1862 default:
1863 return false;
1864 }
1865 }
1866
1867 bool isFunctionOrMethod() const {
1868 switch (getDeclKind()) {
1869 case Decl::Block:
1870 case Decl::Captured:
1871 case Decl::ObjCMethod:
1872 return true;
1873 default:
1874 return getDeclKind() >= Decl::firstFunction &&
1875 getDeclKind() <= Decl::lastFunction;
1876 }
1877 }
1878
1879 /// Test whether the context supports looking up names.
1880 bool isLookupContext() const {
1881 return !isFunctionOrMethod() && getDeclKind() != Decl::LinkageSpec &&
1882 getDeclKind() != Decl::Export;
1883 }
1884
1885 bool isFileContext() const {
1886 return getDeclKind() == Decl::TranslationUnit ||
1887 getDeclKind() == Decl::Namespace;
1888 }
1889
1890 bool isTranslationUnit() const {
1891 return getDeclKind() == Decl::TranslationUnit;
1892 }
1893
1894 bool isRecord() const {
1895 return getDeclKind() >= Decl::firstRecord &&
1896 getDeclKind() <= Decl::lastRecord;
1897 }
1898
1899 bool isNamespace() const { return getDeclKind() == Decl::Namespace; }
1900
1901 bool isStdNamespace() const;
1902
1903 bool isInlineNamespace() const;
1904
1905 /// Determines whether this context is dependent on a
1906 /// template parameter.
1907 bool isDependentContext() const;
1908
1909 /// isTransparentContext - Determines whether this context is a
1910 /// "transparent" context, meaning that the members declared in this
1911 /// context are semantically declared in the nearest enclosing
1912 /// non-transparent (opaque) context but are lexically declared in
1913 /// this context. For example, consider the enumerators of an
1914 /// enumeration type:
1915 /// @code
1916 /// enum E {
1917 /// Val1
1918 /// };
1919 /// @endcode
1920 /// Here, E is a transparent context, so its enumerator (Val1) will
1921 /// appear (semantically) that it is in the same context of E.
1922 /// Examples of transparent contexts include: enumerations (except for
1923 /// C++0x scoped enums), and C++ linkage specifications.
1924 bool isTransparentContext() const;
1925
1926 /// Determines whether this context or some of its ancestors is a
1927 /// linkage specification context that specifies C linkage.
1928 bool isExternCContext() const;
1929
1930 /// Retrieve the nearest enclosing C linkage specification context.
1931 const LinkageSpecDecl *getExternCContext() const;
1932
1933 /// Determines whether this context or some of its ancestors is a
1934 /// linkage specification context that specifies C++ linkage.
1935 bool isExternCXXContext() const;
1936
1937 /// Determine whether this declaration context is equivalent
1938 /// to the declaration context DC.
1939 bool Equals(const DeclContext *DC) const {
1940 return DC && this->getPrimaryContext() == DC->getPrimaryContext();
1941 }
1942
1943 /// Determine whether this declaration context encloses the
1944 /// declaration context DC.
1945 bool Encloses(const DeclContext *DC) const;
1946
1947 /// Find the nearest non-closure ancestor of this context,
1948 /// i.e. the innermost semantic parent of this context which is not
1949 /// a closure. A context may be its own non-closure ancestor.
1950 Decl *getNonClosureAncestor();
1951 const Decl *getNonClosureAncestor() const {
1952 return const_cast<DeclContext*>(this)->getNonClosureAncestor();
1953 }
1954
1955 /// getPrimaryContext - There may be many different
1956 /// declarations of the same entity (including forward declarations
1957 /// of classes, multiple definitions of namespaces, etc.), each with
1958 /// a different set of declarations. This routine returns the
1959 /// "primary" DeclContext structure, which will contain the
1960 /// information needed to perform name lookup into this context.
1961 DeclContext *getPrimaryContext();
1962 const DeclContext *getPrimaryContext() const {
1963 return const_cast<DeclContext*>(this)->getPrimaryContext();
1964 }
1965
1966 /// getRedeclContext - Retrieve the context in which an entity conflicts with
1967 /// other entities of the same name, or where it is a redeclaration if the
1968 /// two entities are compatible. This skips through transparent contexts.
1969 DeclContext *getRedeclContext();
1970 const DeclContext *getRedeclContext() const {
1971 return const_cast<DeclContext *>(this)->getRedeclContext();
1972 }
1973
1974 /// Retrieve the nearest enclosing namespace context.
1975 DeclContext *getEnclosingNamespaceContext();
1976 const DeclContext *getEnclosingNamespaceContext() const {
1977 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
1978 }
1979
1980 /// Retrieve the outermost lexically enclosing record context.
1981 RecordDecl *getOuterLexicalRecordContext();
1982 const RecordDecl *getOuterLexicalRecordContext() const {
1983 return const_cast<DeclContext *>(this)->getOuterLexicalRecordContext();
1984 }
1985
1986 /// Test if this context is part of the enclosing namespace set of
1987 /// the context NS, as defined in C++0x [namespace.def]p9. If either context
1988 /// isn't a namespace, this is equivalent to Equals().
1989 ///
1990 /// The enclosing namespace set of a namespace is the namespace and, if it is
1991 /// inline, its enclosing namespace, recursively.
1992 bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
1993
1994 /// Collects all of the declaration contexts that are semantically
1995 /// connected to this declaration context.
1996 ///
1997 /// For declaration contexts that have multiple semantically connected but
1998 /// syntactically distinct contexts, such as C++ namespaces, this routine
1999 /// retrieves the complete set of such declaration contexts in source order.
2000 /// For example, given:
2001 ///
2002 /// \code
2003 /// namespace N {
2004 /// int x;
2005 /// }
2006 /// namespace N {
2007 /// int y;
2008 /// }
2009 /// \endcode
2010 ///
2011 /// The \c Contexts parameter will contain both definitions of N.
2012 ///
2013 /// \param Contexts Will be cleared and set to the set of declaration
2014 /// contexts that are semanticaly connected to this declaration context,
2015 /// in source order, including this context (which may be the only result,
2016 /// for non-namespace contexts).
2017 void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts);
2018
2019 /// decl_iterator - Iterates through the declarations stored
2020 /// within this context.
2021 class decl_iterator {
2022 /// Current - The current declaration.
2023 Decl *Current = nullptr;
2024
2025 public:
2026 using value_type = Decl *;
2027 using reference = const value_type &;
2028 using pointer = const value_type *;
2029 using iterator_category = std::forward_iterator_tag;
2030 using difference_type = std::ptrdiff_t;
2031
2032 decl_iterator() = default;
2033 explicit decl_iterator(Decl *C) : Current(C) {}
2034
2035 reference operator*() const { return Current; }
2036
2037 // This doesn't meet the iterator requirements, but it's convenient
2038 value_type operator->() const { return Current; }
2039
2040 decl_iterator& operator++() {
2041 Current = Current->getNextDeclInContext();
2042 return *this;
2043 }
2044
2045 decl_iterator operator++(int) {
2046 decl_iterator tmp(*this);
2047 ++(*this);
2048 return tmp;
2049 }
2050
2051 friend bool operator==(decl_iterator x, decl_iterator y) {
2052 return x.Current == y.Current;
2053 }
2054
2055 friend bool operator!=(decl_iterator x, decl_iterator y) {
2056 return x.Current != y.Current;
2057 }
2058 };
2059
2060 using decl_range = llvm::iterator_range<decl_iterator>;
2061
2062 /// decls_begin/decls_end - Iterate over the declarations stored in
2063 /// this context.
2064 decl_range decls() const { return decl_range(decls_begin(), decls_end()); }
2065 decl_iterator decls_begin() const;
2066 decl_iterator decls_end() const { return decl_iterator(); }
2067 bool decls_empty() const;
2068
2069 /// noload_decls_begin/end - Iterate over the declarations stored in this
2070 /// context that are currently loaded; don't attempt to retrieve anything
2071 /// from an external source.
2072 decl_range noload_decls() const {
2073 return decl_range(noload_decls_begin(), noload_decls_end());
2074 }
2075 decl_iterator noload_decls_begin() const { return decl_iterator(FirstDecl); }
2076 decl_iterator noload_decls_end() const { return decl_iterator(); }
2077
2078 /// specific_decl_iterator - Iterates over a subrange of
2079 /// declarations stored in a DeclContext, providing only those that
2080 /// are of type SpecificDecl (or a class derived from it). This
2081 /// iterator is used, for example, to provide iteration over just
2082 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
2083 template<typename SpecificDecl>
2084 class specific_decl_iterator {
2085 /// Current - The current, underlying declaration iterator, which
2086 /// will either be NULL or will point to a declaration of
2087 /// type SpecificDecl.
2088 DeclContext::decl_iterator Current;
2089
2090 /// SkipToNextDecl - Advances the current position up to the next
2091 /// declaration of type SpecificDecl that also meets the criteria
2092 /// required by Acceptable.
2093 void SkipToNextDecl() {
2094 while (*Current && !isa<SpecificDecl>(*Current))
2095 ++Current;
2096 }
2097
2098 public:
2099 using value_type = SpecificDecl *;
2100 // TODO: Add reference and pointer types (with some appropriate proxy type)
2101 // if we ever have a need for them.
2102 using reference = void;
2103 using pointer = void;
2104 using difference_type =
2105 std::iterator_traits<DeclContext::decl_iterator>::difference_type;
2106 using iterator_category = std::forward_iterator_tag;
2107
2108 specific_decl_iterator() = default;
2109
2110 /// specific_decl_iterator - Construct a new iterator over a
2111 /// subset of the declarations the range [C,
2112 /// end-of-declarations). If A is non-NULL, it is a pointer to a
2113 /// member function of SpecificDecl that should return true for
2114 /// all of the SpecificDecl instances that will be in the subset
2115 /// of iterators. For example, if you want Objective-C instance
2116 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
2117 /// &ObjCMethodDecl::isInstanceMethod.
2118 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
2119 SkipToNextDecl();
2120 }
2121
2122 value_type operator*() const { return cast<SpecificDecl>(*Current); }
2123
2124 // This doesn't meet the iterator requirements, but it's convenient
2125 value_type operator->() const { return **this; }
2126
2127 specific_decl_iterator& operator++() {
2128 ++Current;
2129 SkipToNextDecl();
2130 return *this;
2131 }
2132
2133 specific_decl_iterator operator++(int) {
2134 specific_decl_iterator tmp(*this);
2135 ++(*this);
2136 return tmp;
2137 }
2138
2139 friend bool operator==(const specific_decl_iterator& x,
2140 const specific_decl_iterator& y) {
2141 return x.Current == y.Current;
2142 }
2143
2144 friend bool operator!=(const specific_decl_iterator& x,
2145 const specific_decl_iterator& y) {
2146 return x.Current != y.Current;
2147 }
2148 };
2149
2150 /// Iterates over a filtered subrange of declarations stored
2151 /// in a DeclContext.
2152 ///
2153 /// This iterator visits only those declarations that are of type
2154 /// SpecificDecl (or a class derived from it) and that meet some
2155 /// additional run-time criteria. This iterator is used, for
2156 /// example, to provide access to the instance methods within an
2157 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
2158 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
2159 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
2160 class filtered_decl_iterator {
2161 /// Current - The current, underlying declaration iterator, which
2162 /// will either be NULL or will point to a declaration of
2163 /// type SpecificDecl.
2164 DeclContext::decl_iterator Current;
2165
2166 /// SkipToNextDecl - Advances the current position up to the next
2167 /// declaration of type SpecificDecl that also meets the criteria
2168 /// required by Acceptable.
2169 void SkipToNextDecl() {
2170 while (*Current &&
2171 (!isa<SpecificDecl>(*Current) ||
2172 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
2173 ++Current;
2174 }
2175
2176 public:
2177 using value_type = SpecificDecl *;
2178 // TODO: Add reference and pointer types (with some appropriate proxy type)
2179 // if we ever have a need for them.
2180 using reference = void;
2181 using pointer = void;
2182 using difference_type =
2183 std::iterator_traits<DeclContext::decl_iterator>::difference_type;
2184 using iterator_category = std::forward_iterator_tag;
2185
2186 filtered_decl_iterator() = default;
2187
2188 /// filtered_decl_iterator - Construct a new iterator over a
2189 /// subset of the declarations the range [C,
2190 /// end-of-declarations). If A is non-NULL, it is a pointer to a
2191 /// member function of SpecificDecl that should return true for
2192 /// all of the SpecificDecl instances that will be in the subset
2193 /// of iterators. For example, if you want Objective-C instance
2194 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
2195 /// &ObjCMethodDecl::isInstanceMethod.
2196 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
2197 SkipToNextDecl();
2198 }
2199
2200 value_type operator*() const { return cast<SpecificDecl>(*Current); }
2201 value_type operator->() const { return cast<SpecificDecl>(*Current); }
2202
2203 filtered_decl_iterator& operator++() {
2204 ++Current;
2205 SkipToNextDecl();
2206 return *this;
2207 }
2208
2209 filtered_decl_iterator operator++(int) {
2210 filtered_decl_iterator tmp(*this);
2211 ++(*this);
2212 return tmp;
2213 }
2214
2215 friend bool operator==(const filtered_decl_iterator& x,
2216 const filtered_decl_iterator& y) {
2217 return x.Current == y.Current;
2218 }
2219
2220 friend bool operator!=(const filtered_decl_iterator& x,
2221 const filtered_decl_iterator& y) {
2222 return x.Current != y.Current;
2223 }
2224 };
2225
2226 /// Add the declaration D into this context.
2227 ///
2228 /// This routine should be invoked when the declaration D has first
2229 /// been declared, to place D into the context where it was
2230 /// (lexically) defined. Every declaration must be added to one
2231 /// (and only one!) context, where it can be visited via
2232 /// [decls_begin(), decls_end()). Once a declaration has been added
2233 /// to its lexical context, the corresponding DeclContext owns the
2234 /// declaration.
2235 ///
2236 /// If D is also a NamedDecl, it will be made visible within its
2237 /// semantic context via makeDeclVisibleInContext.
2238 void addDecl(Decl *D);
2239
2240 /// Add the declaration D into this context, but suppress
2241 /// searches for external declarations with the same name.
2242 ///
2243 /// Although analogous in function to addDecl, this removes an
2244 /// important check. This is only useful if the Decl is being
2245 /// added in response to an external search; in all other cases,
2246 /// addDecl() is the right function to use.
2247 /// See the ASTImporter for use cases.
2248 void addDeclInternal(Decl *D);
2249
2250 /// Add the declaration D to this context without modifying
2251 /// any lookup tables.
2252 ///
2253 /// This is useful for some operations in dependent contexts where
2254 /// the semantic context might not be dependent; this basically
2255 /// only happens with friends.
2256 void addHiddenDecl(Decl *D);
2257
2258 /// Removes a declaration from this context.
2259 void removeDecl(Decl *D);
2260
2261 /// Checks whether a declaration is in this context.
2262 bool containsDecl(Decl *D) const;
2263
2264 /// Checks whether a declaration is in this context.
2265 /// This also loads the Decls from the external source before the check.
2266 bool containsDeclAndLoad(Decl *D) const;
2267
2268 using lookup_result = DeclContextLookupResult;
2269 using lookup_iterator = lookup_result::iterator;
2270
2271 /// lookup - Find the declarations (if any) with the given Name in
2272 /// this context. Returns a range of iterators that contains all of
2273 /// the declarations with this name, with object, function, member,
2274 /// and enumerator names preceding any tag name. Note that this
2275 /// routine will not look into parent contexts.
2276 lookup_result lookup(DeclarationName Name) const;
2277
2278 /// Find the declarations with the given name that are visible
2279 /// within this context; don't attempt to retrieve anything from an
2280 /// external source.
2281 lookup_result noload_lookup(DeclarationName Name);
2282
2283 /// A simplistic name lookup mechanism that performs name lookup
2284 /// into this declaration context without consulting the external source.
2285 ///
2286 /// This function should almost never be used, because it subverts the
2287 /// usual relationship between a DeclContext and the external source.
2288 /// See the ASTImporter for the (few, but important) use cases.
2289 ///
2290 /// FIXME: This is very inefficient; replace uses of it with uses of
2291 /// noload_lookup.
2292 void localUncachedLookup(DeclarationName Name,
2293 SmallVectorImpl<NamedDecl *> &Results);
2294
2295 /// Makes a declaration visible within this context.
2296 ///
2297 /// This routine makes the declaration D visible to name lookup
2298 /// within this context and, if this is a transparent context,
2299 /// within its parent contexts up to the first enclosing
2300 /// non-transparent context. Making a declaration visible within a
2301 /// context does not transfer ownership of a declaration, and a
2302 /// declaration can be visible in many contexts that aren't its
2303 /// lexical context.
2304 ///
2305 /// If D is a redeclaration of an existing declaration that is
2306 /// visible from this context, as determined by
2307 /// NamedDecl::declarationReplaces, the previous declaration will be
2308 /// replaced with D.
2309 void makeDeclVisibleInContext(NamedDecl *D);
2310
2311 /// all_lookups_iterator - An iterator that provides a view over the results
2312 /// of looking up every possible name.
2313 class all_lookups_iterator;
2314
2315 using lookups_range = llvm::iterator_range<all_lookups_iterator>;
2316
2317 lookups_range lookups() const;
2318 // Like lookups(), but avoids loading external declarations.
2319 // If PreserveInternalState, avoids building lookup data structures too.
2320 lookups_range noload_lookups(bool PreserveInternalState) const;
2321
2322 /// Iterators over all possible lookups within this context.
2323 all_lookups_iterator lookups_begin() const;
2324 all_lookups_iterator lookups_end() const;
2325
2326 /// Iterators over all possible lookups within this context that are
2327 /// currently loaded; don't attempt to retrieve anything from an external
2328 /// source.
2329 all_lookups_iterator noload_lookups_begin() const;
2330 all_lookups_iterator noload_lookups_end() const;
2331
2332 struct udir_iterator;
2333
2334 using udir_iterator_base =
2335 llvm::iterator_adaptor_base<udir_iterator, lookup_iterator,
2336 std::random_access_iterator_tag,
2337 UsingDirectiveDecl *>;
2338
2339 struct udir_iterator : udir_iterator_base {
2340 udir_iterator(lookup_iterator I) : udir_iterator_base(I) {}
2341
2342 UsingDirectiveDecl *operator*() const;
2343 };
2344
2345 using udir_range = llvm::iterator_range<udir_iterator>;
2346
2347 udir_range using_directives() const;
2348
2349 // These are all defined in DependentDiagnostic.h.
2350 class ddiag_iterator;
2351
2352 using ddiag_range = llvm::iterator_range<DeclContext::ddiag_iterator>;
2353
2354 inline ddiag_range ddiags() const;
2355
2356 // Low-level accessors
2357
2358 /// Mark that there are external lexical declarations that we need
2359 /// to include in our lookup table (and that are not available as external
2360 /// visible lookups). These extra lookup results will be found by walking
2361 /// the lexical declarations of this context. This should be used only if
2362 /// setHasExternalLexicalStorage() has been called on any decl context for
2363 /// which this is the primary context.
2364 void setMustBuildLookupTable() {
2365 assert(this == getPrimaryContext() &&((this == getPrimaryContext() && "should only be called on primary context"
) ? static_cast<void> (0) : __assert_fail ("this == getPrimaryContext() && \"should only be called on primary context\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 2366, __PRETTY_FUNCTION__))
2366 "should only be called on primary context")((this == getPrimaryContext() && "should only be called on primary context"
) ? static_cast<void> (0) : __assert_fail ("this == getPrimaryContext() && \"should only be called on primary context\""
, "/build/llvm-toolchain-snapshot-12~++20210121111113+bee486851c1a/clang/include/clang/AST/DeclBase.h"
, 2366, __PRETTY_FUNCTION__))
;
2367 DeclContextBits.HasLazyExternalLexicalLookups = true;
2368 }
2369
2370 /// Retrieve the internal representation of the lookup structure.
2371 /// This may omit some names if we are lazily building the structure.
2372 StoredDeclsMap *getLookupPtr() const { return LookupPtr; }
2373
2374 /// Ensure the lookup structure is fully-built and return it.
2375 StoredDeclsMap *buildLookup();
2376
2377 /// Whether this DeclContext has external storage containing
2378 /// additional declarations that are lexically in this context.
2379 bool hasExternalLexicalStorage() const {
2380 return DeclContextBits.ExternalLexicalStorage;
2381 }
2382
2383 /// State whether this DeclContext has external storage for
2384 /// declarations lexically in this context.
2385 void setHasExternalLexicalStorage(bool ES = true) const {
2386 DeclContextBits.ExternalLexicalStorage = ES;
2387 }
2388
2389 /// Whether this DeclContext has external storage containing
2390 /// additional declarations that are visible in this context.
2391 bool hasExternalVisibleStorage() const {
2392 return DeclContextBits.ExternalVisibleStorage;
2393 }
2394
2395 /// State whether this DeclContext has external storage for
2396 /// declarations visible in this context.
2397 void setHasExternalVisibleStorage(bool ES = true) const {
2398 DeclContextBits.ExternalVisibleStorage = ES;
2399 if (ES && LookupPtr)
2400 DeclContextBits.NeedToReconcileExternalVisibleStorage = true;
2401 }
2402
2403 /// Determine whether the given declaration is stored in the list of
2404 /// declarations lexically within this context.
2405 bool isDeclInLexicalTraversal(const Decl *D) const {
2406 return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl ||
2407 D == LastDecl);
2408 }
2409
2410 bool setUseQualifiedLookup(bool use = true) const {
2411 bool old_value = DeclContextBits.UseQualifiedLookup;
2412 DeclContextBits.UseQualifiedLookup = use;
2413 return old_value;
2414 }
2415
2416 bool shouldUseQualifiedLookup() const {
2417 return DeclContextBits.UseQualifiedLookup;
2418 }
2419
2420 static bool classof(const Decl *D);
2421 static bool classof(const DeclContext *D) { return true; }
2422
2423 void dumpDeclContext() const;
2424 void dumpLookups() const;
2425 void dumpLookups(llvm::raw_ostream &OS, bool DumpDecls = false,
2426 bool Deserialize = false) const;
2427
2428private:
2429 /// Whether this declaration context has had externally visible
2430 /// storage added since the last lookup. In this case, \c LookupPtr's
2431 /// invariant may not hold and needs to be fixed before we perform
2432 /// another lookup.
2433 bool hasNeedToReconcileExternalVisibleStorage() const {
2434 return DeclContextBits.NeedToReconcileExternalVisibleStorage;
2435 }
2436
2437 /// State that this declaration context has had externally visible
2438 /// storage added since the last lookup. In this case, \c LookupPtr's
2439 /// invariant may not hold and needs to be fixed before we perform
2440 /// another lookup.
2441 void setNeedToReconcileExternalVisibleStorage(bool Need = true) const {
2442 DeclContextBits.NeedToReconcileExternalVisibleStorage = Need;
2443 }
2444
2445 /// If \c true, this context may have local lexical declarations
2446 /// that are missing from the lookup table.
2447 bool hasLazyLocalLexicalLookups() const {
2448 return DeclContextBits.HasLazyLocalLexicalLookups;
2449 }
2450
2451 /// If \c true, this context may have local lexical declarations
2452 /// that are missing from the lookup table.
2453 void setHasLazyLocalLexicalLookups(bool HasLLLL = true) const {
2454 DeclContextBits.HasLazyLocalLexicalLookups = HasLLLL;
2455 }
2456
2457 /// If \c true, the external source may have lexical declarations
2458 /// that are missing from the lookup table.
2459 bool hasLazyExternalLexicalLookups() const {
2460 return DeclContextBits.HasLazyExternalLexicalLookups;
2461 }
2462
2463 /// If \c true, the external source may have lexical declarations
2464 /// that are missing from the lookup table.
2465 void setHasLazyExternalLexicalLookups(bool HasLELL = true) const {
2466 DeclContextBits.HasLazyExternalLexicalLookups = HasLELL;
2467 }
2468
2469 void reconcileExternalVisibleStorage() const;
2470 bool LoadLexicalDeclsFromExternalStorage() const;
2471
2472 /// Makes a declaration visible within this context, but
2473 /// suppresses searches for external declarations with the same
2474 /// name.
2475 ///
2476 /// Analogous to makeDeclVisibleInContext, but for the exclusive
2477 /// use of addDeclInternal().
2478 void makeDeclVisibleInContextInternal(NamedDecl *D);
2479
2480 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
2481
2482 void loadLazyLocalLexicalLookups();
2483 void buildLookupImpl(DeclContext *DCtx, bool Internal);
2484 void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
2485 bool Rediscoverable);
2486 void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal);
2487};
2488
2489inline bool Decl::isTemplateParameter() const {
2490 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
2491 getKind() == TemplateTemplateParm;
2492}
2493
2494// Specialization selected when ToTy is not a known subclass of DeclContext.
2495template <class ToTy,
2496 bool IsKnownSubtype = ::std::is_base_of<DeclContext, ToTy>::value>
2497struct cast_convert_decl_context {
2498 static const ToTy *doit(const DeclContext *Val) {
2499 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
2500 }
2501
2502 static ToTy *doit(DeclContext *Val) {
2503 return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
2504 }
2505};
2506
2507// Specialization selected when ToTy is a known subclass of DeclContext.
2508template <class ToTy>
2509struct cast_convert_decl_context<ToTy, true> {
2510 static const ToTy *doit(const DeclContext *Val) {
2511 return static_cast<const ToTy*>(Val);
2512 }
2513
2514 static ToTy *doit(DeclContext *Val) {
2515 return static_cast<ToTy*>(Val);
2516 }
2517};
2518
2519} // namespace clang
2520
2521namespace llvm {
2522
2523/// isa<T>(DeclContext*)
2524template <typename To>
2525struct isa_impl<To, ::clang::DeclContext> {
2526 static bool doit(const ::clang::DeclContext &Val) {
2527 return To::classofKind(Val.getDeclKind());
2528 }
2529};
2530
2531/// cast<T>(DeclContext*)
2532template<class ToTy>
2533struct cast_convert_val<ToTy,
2534 const ::clang::DeclContext,const ::clang::DeclContext> {
2535 static const ToTy &doit(const ::clang::DeclContext &Val) {
2536 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
2537 }
2538};
2539
2540template<class ToTy>
2541struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
2542 static ToTy &doit(::clang::DeclContext &Val) {
2543 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
2544 }
2545};
2546
2547template<class ToTy>
2548struct cast_convert_val<ToTy,
2549 const ::clang::DeclContext*, const ::clang::DeclContext*> {
2550 static const ToTy *doit(const ::clang::DeclContext *Val) {
2551 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
2552 }
2553};
2554
2555template<class ToTy>
2556struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
2557 static ToTy *doit(::clang::DeclContext *Val) {
2558 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
2559 }
2560};
2561
2562/// Implement cast_convert_val for Decl -> DeclContext conversions.
2563template<class FromTy>
2564struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
2565 static ::clang::DeclContext &doit(const FromTy &Val) {
2566 return *FromTy::castToDeclContext(&Val);
2567 }
2568};
2569
2570template<class FromTy>
2571struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
2572 static ::clang::DeclContext *doit(const FromTy *Val) {
2573 return FromTy::castToDeclContext(Val);
2574 }
2575};
2576
2577template<class FromTy>
2578struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
2579 static const ::clang::DeclContext &doit(const FromTy &Val) {
2580 return *FromTy::castToDeclContext(&Val);
2581 }
2582};
2583
2584template<class FromTy>
2585struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
2586 static const ::clang::DeclContext *doit(const FromTy *Val) {
2587 return FromTy::castToDeclContext(Val);
2588 }
2589};
2590
2591} // namespace llvm
2592
2593#endif // LLVM_CLANG_AST_DECLBASE_H