Skip to content

Commit

Permalink
Introduce a type-safe enum for ForDefinition.
Browse files Browse the repository at this point in the history
llvm-svn: 288289
  • Loading branch information
rjmccall committed Nov 30, 2016
1 parent a5b7164 commit d195d4c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
5 changes: 3 additions & 2 deletions clang/lib/CodeGen/CGCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ llvm::Function *CodeGenModule::codegenCXXStructor(const CXXMethodDecl *MD,
getTypes().arrangeCXXStructorDeclaration(MD, Type);
auto *Fn = cast<llvm::Function>(
getAddrOfCXXStructor(MD, Type, &FnInfo, /*FnType=*/nullptr,
/*DontDefer=*/true, /*IsForDefinition=*/true));
/*DontDefer=*/true, ForDefinition));

GlobalDecl GD;
if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
Expand All @@ -239,7 +239,8 @@ llvm::Function *CodeGenModule::codegenCXXStructor(const CXXMethodDecl *MD,

llvm::Constant *CodeGenModule::getAddrOfCXXStructor(
const CXXMethodDecl *MD, StructorType Type, const CGFunctionInfo *FnInfo,
llvm::FunctionType *FnType, bool DontDefer, bool IsForDefinition) {
llvm::FunctionType *FnType, bool DontDefer,
ForDefinition_t IsForDefinition) {
GlobalDecl GD;
if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
GD = GlobalDecl(CD, toCXXCtorType(Type));
Expand Down
18 changes: 9 additions & 9 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ void CodeGenModule::EmitDeferred() {
// might had been created for another decl with the same mangled name but
// different type.
llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
GetAddrOfGlobal(D, /*IsForDefinition=*/true));
GetAddrOfGlobal(D, ForDefinition));

// In case of different address spaces, we may still get a cast, even with
// IsForDefinition equal to true. Query mangled names table to get
Expand Down Expand Up @@ -1864,7 +1864,7 @@ CodeGenModule::GetOrCreateLLVMFunction(StringRef MangledName,
GlobalDecl GD, bool ForVTable,
bool DontDefer, bool IsThunk,
llvm::AttributeSet ExtraAttrs,
bool IsForDefinition) {
ForDefinition_t IsForDefinition) {
const Decl *D = GD.getDecl();

// Lookup the entry, lazily creating it if necessary.
Expand Down Expand Up @@ -2024,7 +2024,7 @@ llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
llvm::Type *Ty,
bool ForVTable,
bool DontDefer,
bool IsForDefinition) {
ForDefinition_t IsForDefinition) {
// If there was no specific requested type, just convert it now.
if (!Ty) {
const auto *FD = cast<FunctionDecl>(GD.getDecl());
Expand Down Expand Up @@ -2103,7 +2103,7 @@ llvm::Constant *
CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
llvm::PointerType *Ty,
const VarDecl *D,
bool IsForDefinition) {
ForDefinition_t IsForDefinition) {
// Lookup the entry, lazily creating it if necessary.
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
if (Entry) {
Expand Down Expand Up @@ -2218,7 +2218,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,

llvm::Constant *
CodeGenModule::GetAddrOfGlobal(GlobalDecl GD,
bool IsForDefinition) {
ForDefinition_t IsForDefinition) {
if (isa<CXXConstructorDecl>(GD.getDecl()))
return getAddrOfCXXStructor(cast<CXXConstructorDecl>(GD.getDecl()),
getFromCtorType(GD.getCtorType()),
Expand Down Expand Up @@ -2295,7 +2295,7 @@ CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name,
/// variable with the same mangled name but some other type.
llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
llvm::Type *Ty,
bool IsForDefinition) {
ForDefinition_t IsForDefinition) {
assert(D->hasGlobalStorage() && "Not a global variable");
QualType ASTTy = D->getType();
if (!Ty)
Expand Down Expand Up @@ -2485,7 +2485,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,

llvm::Type* InitType = Init->getType();
llvm::Constant *Entry =
GetAddrOfGlobalVar(D, InitType, /*IsForDefinition=*/!IsTentative);
GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative));

// Strip off a bitcast if we got one back.
if (auto *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
Expand Down Expand Up @@ -2518,7 +2518,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,

// Make a new global with the correct type, this is now guaranteed to work.
GV = cast<llvm::GlobalVariable>(
GetAddrOfGlobalVar(D, InitType, /*IsForDefinition=*/!IsTentative));
GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)));

// Replace all uses of the old global with the new global
llvm::Constant *NewPtrForOldDecl =
Expand Down Expand Up @@ -2922,7 +2922,7 @@ void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
if (!GV || (GV->getType()->getElementType() != Ty))
GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false,
/*DontDefer=*/true,
/*IsForDefinition=*/true));
ForDefinition));

// Already emitted.
if (!GV->isDeclaration())
Expand Down
23 changes: 17 additions & 6 deletions clang/lib/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ class FunctionArgList;
class CoverageMappingModuleGen;
class TargetCodeGenInfo;

enum ForDefinition_t : bool {
NotForDefinition = false,
ForDefinition = true
};

struct OrderGlobalInits {
unsigned int priority;
unsigned int lex_order;
Expand Down Expand Up @@ -676,7 +681,9 @@ class CodeGenModule : public CodeGenTypeCache {
llvm_unreachable("unknown visibility!");
}

llvm::Constant *GetAddrOfGlobal(GlobalDecl GD, bool IsForDefinition = false);
llvm::Constant *GetAddrOfGlobal(GlobalDecl GD,
ForDefinition_t IsForDefinition
= NotForDefinition);

/// Will return a global variable of the given type. If a variable with a
/// different type already exists then a new variable with the right type
Expand Down Expand Up @@ -706,14 +713,16 @@ class CodeGenModule : public CodeGenTypeCache {
/// the same mangled name but some other type.
llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
llvm::Type *Ty = nullptr,
bool IsForDefinition = false);
ForDefinition_t IsForDefinition
= NotForDefinition);

/// Return the address of the given function. If Ty is non-null, then this
/// function will use the specified type if it has to create it.
llvm::Constant *GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty = nullptr,
bool ForVTable = false,
bool DontDefer = false,
bool IsForDefinition = false);
ForDefinition_t IsForDefinition
= NotForDefinition);

/// Get the address of the RTTI descriptor for the given type.
llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
Expand Down Expand Up @@ -821,7 +830,8 @@ class CodeGenModule : public CodeGenTypeCache {
getAddrOfCXXStructor(const CXXMethodDecl *MD, StructorType Type,
const CGFunctionInfo *FnInfo = nullptr,
llvm::FunctionType *FnType = nullptr,
bool DontDefer = false, bool IsForDefinition = false);
bool DontDefer = false,
ForDefinition_t IsForDefinition = NotForDefinition);

/// Given a builtin id for a function like "__builtin_fabsf", return a
/// Function* for "fabsf".
Expand Down Expand Up @@ -1151,12 +1161,13 @@ class CodeGenModule : public CodeGenTypeCache {
bool ForVTable, bool DontDefer = false,
bool IsThunk = false,
llvm::AttributeSet ExtraAttrs = llvm::AttributeSet(),
bool IsForDefinition = false);
ForDefinition_t IsForDefinition = NotForDefinition);

llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
llvm::PointerType *PTy,
const VarDecl *D,
bool IsForDefinition = false);
ForDefinition_t IsForDefinition
= NotForDefinition);

void setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO);

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/ModuleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace {
}

llvm::Constant *GetAddrOfGlobal(GlobalDecl global, bool isForDefinition) {
return Builder->GetAddrOfGlobal(global, isForDefinition);
return Builder->GetAddrOfGlobal(global, ForDefinition_t(isForDefinition));
}

void Initialize(ASTContext &Context) override {
Expand Down

0 comments on commit d195d4c

Please sign in to comment.