From 4f8d123e3e2c260de3377208106ddba87cee28b4 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 22 May 2008 00:50:06 +0000 Subject: [PATCH] Move getAccessedFieldNo out of lib/AST/Expr.cpp into lib/CodeGen/CGExpr.cpp and to change include/clang/AST/Attr.h to use its own enum for visibility types instead of using llvm::GlobalValue::VisibilityTypes. These changes eliminate dependencies in the AST library on LLVM's VMCore library. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51398 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Attr.h | 15 +++++++++++---- include/clang/AST/Expr.h | 4 ---- lib/AST/Expr.cpp | 10 ---------- lib/CodeGen/CGExpr.cpp | 20 +++++++++++++++----- lib/CodeGen/CodeGenFunction.cpp | 2 +- lib/CodeGen/CodeGenFunction.h | 5 +++++ lib/CodeGen/CodeGenModule.cpp | 20 +++++++++++++++++++- lib/CodeGen/CodeGenModule.h | 6 ++++++ lib/Sema/SemaDecl.cpp | 10 +++++----- 9 files changed, 62 insertions(+), 30 deletions(-) diff --git a/include/clang/AST/Attr.h b/include/clang/AST/Attr.h index abdcfb4b05..7840d59c3a 100644 --- a/include/clang/AST/Attr.h +++ b/include/clang/AST/Attr.h @@ -14,7 +14,6 @@ #ifndef LLVM_CLANG_AST_ATTR_H #define LLVM_CLANG_AST_ATTR_H -#include "llvm/GlobalValue.h" #include #include @@ -166,12 +165,20 @@ public: }; class VisibilityAttr : public Attr { - llvm::GlobalValue::VisibilityTypes VisibilityType; public: - VisibilityAttr(llvm::GlobalValue::VisibilityTypes v) : Attr(Visibility), + /// @brief An enumeration for the kinds of visibility of symbols. + enum VisibilityTypes { + DefaultVisibility = 0, + HiddenVisibility, + ProtectedVisibility + }; +private: + VisibilityTypes VisibilityType; +public: + VisibilityAttr(VisibilityTypes v) : Attr(Visibility), VisibilityType(v) {} - llvm::GlobalValue::VisibilityTypes getVisibility() const { return VisibilityType; } + VisibilityTypes getVisibility() const { return VisibilityType; } // Implement isa/cast/dyncast/etc. diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 6f2ff83f5f..9760b3652f 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -723,10 +723,6 @@ public: /// aggregate Constant of ConstantInt(s). void getEncodedElementAccess(llvm::SmallVectorImpl &Elts) const; - /// getAccessedFieldNo - Given an encoded value and a result number, return - /// the input field number being accessed. - static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts); - virtual SourceRange getSourceRange() const { return SourceRange(getBase()->getLocStart(), AccessorLoc); } diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index e7babf4264..e21686cf4e 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -16,7 +16,6 @@ #include "clang/AST/StmtVisitor.h" #include "clang/Basic/IdentifierTable.h" #include "clang/Basic/TargetInfo.h" -#include "llvm/Constants.h" using namespace clang; //===----------------------------------------------------------------------===// @@ -1082,15 +1081,6 @@ void ExtVectorElementExpr::getEncodedElementAccess( } } -unsigned -ExtVectorElementExpr::getAccessedFieldNo(unsigned Idx, - const llvm::Constant *Elts) { - if (isa(Elts)) - return 0; - - return cast(Elts->getOperand(Idx))->getZExtValue(); -} - // constructor for instance messages. ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, QualType retType, ObjCMethodDecl *mproto, diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 7fa2e37d9e..b124e34be3 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -60,6 +60,16 @@ RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc, return RValue::getAggregate(AggLoc); } +/// getAccessedFieldNo - Given an encoded value and a result number, return +/// the input field number being accessed. +unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx, + const llvm::Constant *Elts) { + if (isa(Elts)) + return 0; + + return cast(Elts->getOperand(Idx))->getZExtValue(); +} + //===----------------------------------------------------------------------===// // LValue Expression Emission @@ -197,7 +207,7 @@ RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV, // extracting a single element. Just codegen as an extractelement. const VectorType *ExprVT = ExprType->getAsVectorType(); if (!ExprVT) { - unsigned InIdx = ExtVectorElementExpr::getAccessedFieldNo(0, Elts); + unsigned InIdx = getAccessedFieldNo(0, Elts); llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp")); } @@ -211,7 +221,7 @@ RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV, if (NumResultElts == NumSourceElts) { llvm::SmallVector Mask; for (unsigned i = 0; i != NumResultElts; ++i) { - unsigned InIdx = ExtVectorElementExpr::getAccessedFieldNo(i, Elts); + unsigned InIdx = getAccessedFieldNo(i, Elts); Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx)); } @@ -227,7 +237,7 @@ RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV, // Extract/Insert each element of the result. for (unsigned i = 0; i != NumResultElts; ++i) { - unsigned InIdx = ExtVectorElementExpr::getAccessedFieldNo(i, Elts); + unsigned InIdx = getAccessedFieldNo(i, Elts); llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); Elt = Builder.CreateExtractElement(Vec, Elt, "tmp"); @@ -338,13 +348,13 @@ void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp"); - unsigned Idx = ExtVectorElementExpr::getAccessedFieldNo(i, Elts); + unsigned Idx = getAccessedFieldNo(i, Elts); llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx); Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp"); } } else { // If the Src is a scalar (not a vector) it must be updating one element. - unsigned InIdx = ExtVectorElementExpr::getAccessedFieldNo(0, Elts); + unsigned InIdx = getAccessedFieldNo(0, Elts); llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx); Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp"); } diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp index 9c5c45de5e..e4332f6e7f 100644 --- a/lib/CodeGen/CodeGenFunction.cpp +++ b/lib/CodeGen/CodeGenFunction.cpp @@ -171,7 +171,7 @@ void CodeGenFunction::GenerateCode(const FunctionDecl *FD) { CurFn->setCallingConv(llvm::CallingConv::Fast); if (const VisibilityAttr *attr = FD->getAttr()) - CurFn->setVisibility(attr->getVisibility()); + CodeGenModule::setVisibility(CurFn, attr->getVisibility()); // FIXME: else handle -fvisibility diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 0693781bce..63d89a78c7 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -350,6 +350,11 @@ public: /// GetAddrOfStaticLocalVar - Return the address of a static local variable. llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD); + + /// getAccessedFieldNo - Given an encoded value and a result number, return + /// the input field number being accessed. + static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts); + //===--------------------------------------------------------------------===// // Declaration Emission //===--------------------------------------------------------------------===// diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 7bf1e01b27..ec61d667ee 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -82,6 +82,24 @@ void CodeGenModule::WarnUnsupported(const Decl *D, const char *Type) { &Msg, 1); } +/// setVisibility - Set the visibility for the given LLVM GlobalValue +/// according to the given clang AST visibility value. +void CodeGenModule::setVisibility(llvm::GlobalValue *GV, + VisibilityAttr::VisibilityTypes Vis) { + switch (Vis) { + default: assert(0 && "Unknown visibility!"); + case VisibilityAttr::DefaultVisibility: + GV->setVisibility(llvm::GlobalValue::DefaultVisibility); + break; + case VisibilityAttr::HiddenVisibility: + GV->setVisibility(llvm::GlobalValue::HiddenVisibility); + break; + case VisibilityAttr::ProtectedVisibility: + GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); + break; + } +} + /// AddGlobalCtor - Add a function to the list that will be called before /// main() runs. void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor) { @@ -467,7 +485,7 @@ void CodeGenModule::EmitGlobalVarInit(const VarDecl *D) { GV->setInitializer(Init); if (const VisibilityAttr *attr = D->getAttr()) - GV->setVisibility(attr->getVisibility()); + setVisibility(GV, attr->getVisibility()); // FIXME: else handle -fvisibility // Set the llvm linkage type as appropriate. diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index 8a3069e648..bc216e4f48 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -16,6 +16,7 @@ #include "CodeGenTypes.h" #include "CGObjCRuntime.h" +#include "clang/AST/Attr.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringMap.h" @@ -130,6 +131,11 @@ public: /// specified decl yet. void WarnUnsupported(const Decl *D, const char *Type); + /// setVisibility - Set the visibility for the given LLVM GlobalValue + /// according to the given clang AST visibility value. + static void setVisibility(llvm::GlobalValue *GV, + VisibilityAttr::VisibilityTypes); + private: /// ReplaceMapValuesWith - This is a really slow and bad function that /// searches for any entries in GlobalDeclMap that point to OldVal, changing diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index b0b50bab96..35f12c7207 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -2621,16 +2621,16 @@ void Sema::HandleVisibilityAttribute(Decl *d, AttributeList *rawAttr) { const char *TypeStr = Str->getStrData(); unsigned TypeLen = Str->getByteLength(); - llvm::GlobalValue::VisibilityTypes type; + VisibilityAttr::VisibilityTypes type; if (TypeLen == 7 && !memcmp(TypeStr, "default", 7)) - type = llvm::GlobalValue::DefaultVisibility; + type = VisibilityAttr::DefaultVisibility; else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6)) - type = llvm::GlobalValue::HiddenVisibility; + type = VisibilityAttr::HiddenVisibility; else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8)) - type = llvm::GlobalValue::HiddenVisibility; // FIXME + type = VisibilityAttr::HiddenVisibility; // FIXME else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9)) - type = llvm::GlobalValue::ProtectedVisibility; + type = VisibilityAttr::ProtectedVisibility; else { Diag(rawAttr->getLoc(), diag::warn_attribute_type_not_supported, "visibility", TypeStr);