clang 23.0.0git
CIRGenDeclCXX.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 contains code dealing with code generation of C++ declarations
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenCXXABI.h"
14#include "CIRGenFunction.h"
15#include "CIRGenModule.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/Mangle.h"
20
21using namespace clang;
22using namespace clang::CIRGen;
23
25 cir::GlobalOp globalOp,
26 bool performInit) {
27 // If we've been asked to forbid guard variables, emit an error now.
28 // This diagnostic is hard-coded for Darwin's use case; we can find
29 // better phrasing if someone else needs it.
30 if (cgm.getCodeGenOpts().ForbidGuardVariables)
31 cgm.error(varDecl.getLocation(), "guard variables are forbidden");
32
33 // Compute the mangled guard variable name and set the static_local attribute
34 // BEFORE emitting initialization. This ensures that GetGlobalOps created
35 // during initialization (e.g., in the ctor region) will see the attribute
36 // and be marked with static_local accordingly.
37 llvm::SmallString<256> guardName;
38 {
39 llvm::raw_svector_ostream out(guardName);
40 cgm.getCXXABI().getMangleContext().mangleStaticGuardVariable(&varDecl, out);
41 }
42
43 // Mark the global as static local with the guard name. The emission of the
44 // guard/acquire is done during LoweringPrepare.
45 auto guardAttr = mlir::StringAttr::get(&cgm.getMLIRContext(), guardName);
46 if (!varDecl.isStaticLocal())
47 cgm.errorNYI(
48 varDecl.getSourceRange(),
49 "Static local guard attr only valid on static local variables");
50 globalOp.setStaticLocalGuardAttr(
51 cir::StaticLocalGuardAttr::get(&cgm.getMLIRContext(), guardAttr));
52
53 // Emit the initializer and add a global destructor if appropriate.
54 // TODO(cir): classic codegen calls emitCXXGlobalVarDeclInit for this as well,
55 // and this is meant to handle cases with weak linkage (see comment in
56 // emitCXXGlobalVarDeclInitFunc). At one point we'll have to do some level of
57 // split here depending on whether this is a global (which should/can have
58 // ctor/dtor regions), or should have in-function initialization.
59 cgm.emitCXXStaticLocalVarDeclInit(&varDecl, globalOp, performInit);
60}
61
63 cir::GlobalOp addr,
64 bool performInit) {
66
68
69 // TODO(cir): Classic codegen calls emitCXXGuardedInit in the following case:
70 // template<typename T> struct Templ {
71 // static T f;
72 // };
73 // template<typename T> T Templ<T>::f = get_i();
74 // auto func() {
75 // Templ<int> t;
76 // return decltype(t)::f;
77 // }
78 //
79 // However, at the moment it is only suitable for static-local variables, so
80 // we will have to modify it to work for this case as well.
81 emitCXXGlobalVarDeclInit(vd, addr, performInit);
82}
Defines the clang::LangOptions interface.
void emitCXXGuardedInit(const VarDecl &varDecl, cir::GlobalOp globalOp, bool performInit)
Emit a guarded initializer for a static local variable.
void emitCXXGlobalVarDeclInit(const VarDecl *varDecl, cir::GlobalOp addr, bool performInit)
Emit the function that initializes the specified global.
void emitCXXGlobalVarDeclInitFunc(const VarDecl *vd, cir::GlobalOp addr, bool performInit)
Represents a variable declaration or definition.
Definition Decl.h:924
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
The JSON file list parser is used to communicate input to InstallAPI.
static bool deferredCXXGlobalInit()
static bool cudaSupport()