diff options
author | Chris Lattner <sabre@nondot.org> | 2009-02-24 22:18:39 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-02-24 22:18:39 +0000 |
commit | eaf2bb89eb2aad3b80673de30febe52df43c10ec (patch) | |
tree | f6b8b9fd6f0e42047f23937bb34139ddface6a4b /lib/CodeGen | |
parent | 8459132b0302cde7eb2a21eb2c6ffca9e66e3aea (diff) |
first wave of fixes for @encode sema support. This is part of PR3648.
The big difference here is that (like string literal) @encode has
array type, not pointer type.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65391 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen')
-rw-r--r-- | lib/CodeGen/CGExpr.cpp | 34 | ||||
-rw-r--r-- | lib/CodeGen/CGExprConstant.cpp | 27 | ||||
-rw-r--r-- | lib/CodeGen/CGExprScalar.cpp | 21 | ||||
-rw-r--r-- | lib/CodeGen/CodeGenFunction.h | 1 | ||||
-rw-r--r-- | lib/CodeGen/CodeGenModule.cpp | 28 | ||||
-rw-r--r-- | lib/CodeGen/CodeGenModule.h | 5 |
6 files changed, 74 insertions, 42 deletions
diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 0449900a0f..e353ab49c3 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -148,6 +148,8 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { return EmitPredefinedLValue(cast<PredefinedExpr>(E)); case Expr::StringLiteralClass: return EmitStringLiteralLValue(cast<StringLiteral>(E)); + case Expr::ObjCEncodeExprClass: + return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E)); case Expr::CXXConditionDeclExprClass: return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E)); @@ -668,7 +670,8 @@ LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { default: assert(0 && "Unknown unary operator lvalue!"); case UnaryOperator::Deref: { - QualType T = E->getSubExpr()->getType()->getAsPointerType()->getPointeeType(); + QualType T = + E->getSubExpr()->getType()->getAsPointerType()->getPointeeType(); LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), ExprTy->getAsPointerType()->getPointeeType() .getCVRQualifiers(), @@ -697,22 +700,27 @@ LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 0); } +LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) { + return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E), 0); +} + + LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) { std::string GlobalVarName; switch (Type) { - default: - assert(0 && "Invalid type"); - case PredefinedExpr::Func: - GlobalVarName = "__func__."; - break; - case PredefinedExpr::Function: - GlobalVarName = "__FUNCTION__."; - break; - case PredefinedExpr::PrettyFunction: - // FIXME:: Demangle C++ method names - GlobalVarName = "__PRETTY_FUNCTION__."; - break; + default: + assert(0 && "Invalid type"); + case PredefinedExpr::Func: + GlobalVarName = "__func__."; + break; + case PredefinedExpr::Function: + GlobalVarName = "__FUNCTION__."; + break; + case PredefinedExpr::PrettyFunction: + // FIXME:: Demangle C++ method names + GlobalVarName = "__PRETTY_FUNCTION__."; + break; } std::string FunctionName; diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp index 1141c0da8a..487b274f1c 100644 --- a/lib/CodeGen/CGExprConstant.cpp +++ b/lib/CodeGen/CGExprConstant.cpp @@ -76,7 +76,10 @@ public: cast<llvm::ArrayType>(ConvertType(ILE->getType())); unsigned NumInitElements = ILE->getNumInits(); // FIXME: Check for wide strings - if (NumInitElements > 0 && isa<StringLiteral>(ILE->getInit(0)) && + // FIXME: Check for NumInitElements exactly equal to 1?? + if (NumInitElements > 0 && + (isa<StringLiteral>(ILE->getInit(0)) || + isa<ObjCEncodeExpr>(ILE->getInit(0))) && ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType()) return Visit(ILE->getInit(0)); const llvm::Type *ElemTy = AType->getElementType(); @@ -346,12 +349,26 @@ public: llvm::Constant *VisitStringLiteral(StringLiteral *E) { assert(!E->getType()->isPointerType() && "Strings are always arrays"); - // Otherwise this must be a string initializing an array in a static - // initializer. Don't emit it as the address of the string, emit the string - // data itself as an inline array. + // This must be a string initializing an array in a static initializer. + // Don't emit it as the address of the string, emit the string data itself + // as an inline array. return llvm::ConstantArray::get(CGM.GetStringForStringLiteral(E), false); } + llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) { + // This must be an @encode initializing an array in a static initializer. + // Don't emit it as the address of the string, emit the string data itself + // as an inline array. + std::string Str; + CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str); + const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType()); + + // Resize the string to the right size, adding zeros at the end, or + // truncating as needed. + Str.resize(CAT->getSize().getZExtValue(), '\0'); + return llvm::ConstantArray::get(Str, false); + } + llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { return Visit(E->getSubExpr()); } @@ -398,6 +415,8 @@ public: } case Expr::StringLiteralClass: return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E)); + case Expr::ObjCEncodeExprClass: + return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E)); case Expr::ObjCStringLiteralClass: { ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E); std::string S(SL->getString()->getStrData(), diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index a271e76bf1..4a2fe997d3 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -165,6 +165,10 @@ public: return EmitLoadOfLValue(E); } Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); } + Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { + return EmitLValue(E).getAddress(); + } + Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); } Value *VisitInitListExpr(InitListExpr *E) { @@ -329,7 +333,6 @@ public: Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { return CGF.EmitObjCStringLiteral(E); } - Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E); }; } // end anonymous namespace. @@ -1385,22 +1388,6 @@ Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { return Builder.CreateLoad(ArgPtr); } -Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { - std::string str; - CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str); - - llvm::Constant *C = llvm::ConstantArray::get(str); - C = new llvm::GlobalVariable(C->getType(), true, - llvm::GlobalValue::InternalLinkage, - C, ".str", &CGF.CGM.getModule()); - llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); - llvm::Constant *Zeros[] = { Zero, Zero }; - C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); - - return C; -} - - Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) { llvm::Constant *C = CGF.BuildBlockLiteralTmp(BE); return C; diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index e2b4fedfd5..f13e79ae1d 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -628,6 +628,7 @@ public: LValue EmitVAArgExprLValue(const VAArgExpr *E); LValue EmitDeclRefLValue(const DeclRefExpr *E); LValue EmitStringLiteralLValue(const StringLiteral *E); + LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E); LValue EmitPredefinedFunctionName(unsigned Type); LValue EmitPredefinedLValue(const PredefinedExpr *E); LValue EmitUnaryOpLValue(const UnaryOperator *E); diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index ccba324aa0..95490df46b 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -1098,6 +1098,21 @@ CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { return GetAddrOfConstantString(GetStringForStringLiteral(S)); } +/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant +/// array for the given ObjCEncodeExpr node. +llvm::Constant * +CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { + std::string Str; + getContext().getObjCEncodingForType(E->getEncodedType(), Str); + + llvm::Constant *C = llvm::ConstantArray::get(Str); + C = new llvm::GlobalVariable(C->getType(), true, + llvm::GlobalValue::InternalLinkage, + C, ".str", &getModule()); + return C; +} + + /// GenerateWritableString -- Creates storage for a string literal. static llvm::Constant *GenerateStringLiteral(const std::string &str, bool constant, @@ -1107,13 +1122,10 @@ static llvm::Constant *GenerateStringLiteral(const std::string &str, llvm::Constant *C = llvm::ConstantArray::get(str, false); // Create a global variable for this string - C = new llvm::GlobalVariable(C->getType(), constant, - llvm::GlobalValue::InternalLinkage, - C, - GlobalName ? GlobalName : ".str", - &CGM.getModule()); - - return C; + return new llvm::GlobalVariable(C->getType(), constant, + llvm::GlobalValue::InternalLinkage, + C, GlobalName ? GlobalName : ".str", + &CGM.getModule()); } /// GetAddrOfConstantString - Returns a pointer to a character array @@ -1134,7 +1146,7 @@ llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); if (Entry.getValue()) - return Entry.getValue(); + return Entry.getValue(); // Create a global variable for this. llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index 913e4bc4b1..3a98bb7d43 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -41,6 +41,7 @@ namespace clang { class ObjCImplementationDecl; class ObjCCategoryImplDecl; class ObjCProtocolDecl; + class ObjCEncodeExpr; class BlockExpr; class Decl; class Expr; @@ -212,6 +213,10 @@ public: /// for the given string literal. llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S); + /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant + /// array for the given ObjCEncodeExpr node. + llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *); + /// GetAddrOfConstantString - Returns a pointer to a character array /// containing the literal. This contents are exactly that of the given /// string, i.e. it will not be null terminated automatically; see |