From: Nico Weber Date: Thu, 11 Oct 2012 10:13:44 +0000 (+0000) Subject: Add codegen support for __uuidof(). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c5f804636e367ef744fd24cf88f7c956a5af0434;p=clang Add codegen support for __uuidof(). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@165710 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/ExprCXX.h b/include/clang/AST/ExprCXX.h index 1d0e3a005b..ac02d3ba56 100644 --- a/include/clang/AST/ExprCXX.h +++ b/include/clang/AST/ExprCXX.h @@ -625,6 +625,9 @@ public: } static bool classof(const CXXUuidofExpr *) { return true; } + /// Grabs __declspec(uuid()) off a type, or returns 0 if there is none. + static UuidAttr *GetUuidAttrOfType(QualType QT); + // Iterators child_range children() { if (isTypeOperand()) return child_range(); diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp index e6076f007f..38e9dd1ba5 100644 --- a/lib/AST/ExprCXX.cpp +++ b/lib/AST/ExprCXX.cpp @@ -51,6 +51,26 @@ QualType CXXUuidofExpr::getTypeOperand() const { .getUnqualifiedType(); } +// static +UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT) { + // Optionally remove one level of pointer, reference or array indirection. + const Type *Ty = QT.getTypePtr(); + if (QT->isPointerType() || QT->isReferenceType()) + Ty = QT->getPointeeType().getTypePtr(); + else if (QT->isArrayType()) + Ty = cast(QT)->getElementType().getTypePtr(); + + // Loop all record redeclaration looking for an uuid attribute. + CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); + for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(), + E = RD->redecls_end(); I != E; ++I) { + if (UuidAttr *Uuid = I->getAttr()) + return Uuid; + } + + return 0; +} + // CXXScalarValueInitExpr SourceRange CXXScalarValueInitExpr::getSourceRange() const { SourceLocation Start = RParenLoc; diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 8c9a80ace2..ec521a988b 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -720,7 +720,7 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { llvm_unreachable("cannot emit a property reference directly"); case Expr::ObjCSelectorExprClass: - return EmitObjCSelectorLValue(cast(E)); + return EmitObjCSelectorLValue(cast(E)); case Expr::ObjCIsaExprClass: return EmitObjCIsaExpr(cast(E)); case Expr::BinaryOperatorClass: @@ -757,6 +757,8 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { return EmitCXXConstructLValue(cast(E)); case Expr::CXXBindTemporaryExprClass: return EmitCXXBindTemporaryLValue(cast(E)); + case Expr::CXXUuidofExprClass: + return EmitCXXUuidofLValue(cast(E)); case Expr::LambdaExprClass: return EmitLambdaLValue(cast(E)); @@ -2859,6 +2861,14 @@ CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) { return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType()); } +llvm::Value *CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) { + return CGM.GetAddrOfUuidDescriptor(E); +} + +LValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) { + return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType()); +} + LValue CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) { AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp index 919ad64cf2..0a20c08b15 100644 --- a/lib/CodeGen/CGExprConstant.cpp +++ b/lib/CodeGen/CGExprConstant.cpp @@ -995,6 +995,9 @@ public: T = Typeid->getExprOperand()->getType(); return CGM.GetAddrOfRTTIDescriptor(T); } + case Expr::CXXUuidofExprClass: { + return CGM.GetAddrOfUuidDescriptor(cast(E)); + } } return 0; diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 11ee36ba0c..f24191b871 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -1841,6 +1841,7 @@ public: llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E); llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE); + llvm::Value* EmitCXXUuidofExpr(const CXXUuidofExpr *E); void MaybeEmitStdInitializerListCleanup(llvm::Value *loc, const Expr *init); void EmitStdInitializerListCleanup(llvm::Value *loc, @@ -2210,6 +2211,7 @@ public: LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E); LValue EmitLambdaLValue(const LambdaExpr *E); LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E); + LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E); LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E); LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index e979e52fad..c799e4aaac 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -832,6 +832,49 @@ bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { return !getContext().DeclMustBeEmitted(Global); } +llvm::Constant *CodeGenModule::GetAddrOfUuidDescriptor( + const CXXUuidofExpr* E) { + // Sema has verified that IIDSource has a __declspec(uuid()), and that its + // well-formed. + StringRef Uuid; + if (E->isTypeOperand()) + Uuid = CXXUuidofExpr::GetUuidAttrOfType(E->getTypeOperand())->getGuid(); + else { + // Special case: __uuidof(0) means an all-zero GUID. + Expr *Op = E->getExprOperand(); + if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) + Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid(); + else + Uuid = "00000000-0000-0000-0000-000000000000"; + } + std::string Name = "__uuid_" + Uuid.str(); + + // Look for an existing global. + if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name)) + return GV; + + llvm::Constant *Init = EmitUuidofInitializer(Uuid, E->getType()); + assert(Init && "failed to initialize as constant"); + + // GUIDs are assumed to be 16 bytes, spread over 4-2-2-8 bytes. However, the + // first field is declared as "long", which for many targets is 8 bytes. + // Those architectures are not supported. (With the MS abi, long is always 4 + // bytes.) + llvm::Type *GuidType = getTypes().ConvertType(E->getType()); + if (Init->getType() != GuidType) { + DiagnosticsEngine &Diags = getDiags(); + unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, + "__uuidof codegen is not supported on this architecture"); + Diags.Report(E->getExprLoc(), DiagID) << E->getSourceRange(); + Init = llvm::UndefValue::get(GuidType); + } + + llvm::GlobalVariable *GV = new llvm::GlobalVariable(getModule(), GuidType, + /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, Init, Name); + GV->setUnnamedAddr(true); + return GV; +} + llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { const AliasAttr *AA = VD->getAttr(); assert(AA && "No alias?"); @@ -1286,7 +1329,7 @@ CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name, // Because C++ name mangling, the only way we can end up with an already // existing global with the same name is if it has been declared extern "C". - assert(GV->isDeclaration() && "Declaration has wrong type!"); + assert(GV->isDeclaration() && "Declaration has wrong type!"); OldGV = GV; } @@ -2756,3 +2799,40 @@ void CodeGenModule::EmitCoverageFile() { } } } + +llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid, + QualType GuidType) { + // Sema has checked that all uuid strings are of the form + // "12345678-1234-1234-1234-1234567890ab". + assert(Uuid.size() == 36); + const char *Uuidstr = Uuid.data(); + for (int i = 0; i < 36; ++i) { + if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuidstr[i] == '-'); + else assert(isxdigit(Uuidstr[i])); + } + + llvm::APInt Field0(32, StringRef(Uuidstr , 8), 16); + llvm::APInt Field1(16, StringRef(Uuidstr + 9, 4), 16); + llvm::APInt Field2(16, StringRef(Uuidstr + 14, 4), 16); + llvm::APInt Field3Values[] = { + llvm::APInt(8, StringRef(Uuidstr + 19, 2), 16), + llvm::APInt(8, StringRef(Uuidstr + 21, 2), 16), + llvm::APInt(8, StringRef(Uuidstr + 24, 2), 16), + llvm::APInt(8, StringRef(Uuidstr + 26, 2), 16), + llvm::APInt(8, StringRef(Uuidstr + 28, 2), 16), + llvm::APInt(8, StringRef(Uuidstr + 30, 2), 16), + llvm::APInt(8, StringRef(Uuidstr + 32, 2), 16), + llvm::APInt(8, StringRef(Uuidstr + 34, 2), 16), + }; + + APValue InitStruct(APValue::UninitStruct(), /*NumBases=*/0, /*NumFields=*/4); + InitStruct.getStructField(0) = APValue(llvm::APSInt(Field0)); + InitStruct.getStructField(1) = APValue(llvm::APSInt(Field1)); + InitStruct.getStructField(2) = APValue(llvm::APSInt(Field2)); + APValue& Arr = InitStruct.getStructField(3); + Arr = APValue(APValue::UninitArray(), 8, 8); + for (int t = 0; t < 8; ++t) + Arr.getArrayInitializedElt(t) = APValue(llvm::APSInt(Field3Values[t])); + + return EmitConstantValue(InitStruct, GuidType); +} diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index 923b268dac..d51a4670ba 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -549,6 +549,9 @@ public: /// for the given type. llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false); + /// GetAddrOfUuidDescriptor - Get the address of a uuid descriptor . + llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E); + /// GetAddrOfThunk - Get the address of the thunk for the given global decl. llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk); @@ -985,6 +988,9 @@ private: /// to emit the .gcno and .gcda files in a way that persists in .bc files. void EmitCoverageFile(); + /// Emits the initializer for a uuidof string. + llvm::Constant *EmitUuidofInitializer(StringRef uuidstr, QualType IIDType); + /// MayDeferGeneration - Determine if the given decl can be emitted /// lazily; this is only relevant for definitions. The given decl /// must be either a function or var decl. diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index ad4a17f99c..bed6cb0b5a 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -410,33 +410,13 @@ Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); } -/// Retrieve the UuidAttr associated with QT. -static UuidAttr *GetUuidAttrOfType(QualType QT) { - // Optionally remove one level of pointer, reference or array indirection. - const Type *Ty = QT.getTypePtr(); - if (QT->isPointerType() || QT->isReferenceType()) - Ty = QT->getPointeeType().getTypePtr(); - else if (QT->isArrayType()) - Ty = cast(QT)->getElementType().getTypePtr(); - - // Loop all record redeclaration looking for an uuid attribute. - CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); - for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(), - E = RD->redecls_end(); I != E; ++I) { - if (UuidAttr *Uuid = I->getAttr()) - return Uuid; - } - - return 0; -} - /// \brief Build a Microsoft __uuidof expression with a type operand. ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, SourceLocation TypeidLoc, TypeSourceInfo *Operand, SourceLocation RParenLoc) { if (!Operand->getType()->isDependentType()) { - if (!GetUuidAttrOfType(Operand->getType())) + if (!CXXUuidofExpr::GetUuidAttrOfType(Operand->getType())) return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); } @@ -452,7 +432,7 @@ ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, Expr *E, SourceLocation RParenLoc) { if (!E->getType()->isDependentType()) { - if (!GetUuidAttrOfType(E->getType()) && + if (!CXXUuidofExpr::GetUuidAttrOfType(E->getType()) && !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); } diff --git a/test/CodeGenCXX/microsoft-uuidof-unsupported-target.cpp b/test/CodeGenCXX/microsoft-uuidof-unsupported-target.cpp new file mode 100644 index 0000000000..4f68aa3477 --- /dev/null +++ b/test/CodeGenCXX/microsoft-uuidof-unsupported-target.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -emit-llvm %s -o - -triple=x86_64-apple-macosx10.8.0 -fms-extensions -verify + +typedef struct _GUID +{ + unsigned long Data1; + unsigned short Data2; + unsigned short Data3; + unsigned char Data4[8]; +} GUID; + +struct __declspec(uuid("87654321-4321-4321-4321-ba0987654321")) S { }; + +GUID g = __uuidof(S); // expected-error {{__uuidof codegen is not supported on this architecture}} diff --git a/test/CodeGenCXX/microsoft-uuidof.cpp b/test/CodeGenCXX/microsoft-uuidof.cpp new file mode 100644 index 0000000000..ceefacfbc7 --- /dev/null +++ b/test/CodeGenCXX/microsoft-uuidof.cpp @@ -0,0 +1,72 @@ +// RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-win32 -fms-extensions | FileCheck %s + +typedef struct _GUID +{ + unsigned long Data1; + unsigned short Data2; + unsigned short Data3; + unsigned char Data4[8]; +} GUID; + +struct __declspec(uuid("12345678-1234-1234-1234-1234567890ab")) S1 { } s1; +struct __declspec(uuid("87654321-4321-4321-4321-ba0987654321")) S2 { }; + +// This gets initialized in a static initializer. +// CHECK: @g = global %struct._GUID zeroinitializer, align 4 +GUID g = __uuidof(S1); + +// First global use of __uuidof(S1) forces the creation of the global. +// CHECK: @__uuid_12345678-1234-1234-1234-1234567890ab = private unnamed_addr constant %struct._GUID { i32 305419896, i16 4660, i16 4660, [8 x i8] c"\124\124Vx\90\AB" } +// CHECK: @gr = constant %struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab, align 4 +const GUID& gr = __uuidof(S1); + +// CHECK: @gp = global %struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab, align 4 +const GUID* gp = &__uuidof(S1); + +// Special case: _uuidof(0) +// CHECK: @zeroiid = constant %struct._GUID* @__uuid_00000000-0000-0000-0000-000000000000, align 4 +const GUID& zeroiid = __uuidof(0); + +// __uuidof(S2) hasn't been used globally yet, so it's emitted when it's used +// in a function and is emitted at the end of the globals section. +// CHECK: @__uuid_87654321-4321-4321-4321-ba0987654321 = private unnamed_addr constant %struct._GUID { i32 -2023406815, i16 17185, i16 17185, [8 x i8] c"C!\BA\09\87eC!" } + +// The static initializer for g. +// CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* bitcast (%struct._GUID* @g to i8*), i8* bitcast (%struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab to i8*), i32 16, i32 4, i1 false) + +void fun() { + // CHECK: %s1_1 = alloca %struct._GUID, align 4 + // CHECK: %s1_2 = alloca %struct._GUID, align 4 + // CHECK: %s1_3 = alloca %struct._GUID, align 4 + + // CHECK: %0 = bitcast %struct._GUID* %s1_1 to i8* + // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast (%struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab to i8*), i32 16, i32 4, i1 false) + GUID s1_1 = __uuidof(S1); + + // CHECK: %1 = bitcast %struct._GUID* %s1_2 to i8* + // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %1, i8* bitcast (%struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab to i8*), i32 16, i32 4, i1 false) + GUID s1_2 = __uuidof(S1); + + // CHECK: %2 = bitcast %struct._GUID* %s1_3 to i8* + // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %2, i8* bitcast (%struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab to i8*), i32 16, i32 4, i1 false) + GUID s1_3 = __uuidof(s1); +} + +void gun() { + // CHECK: %s2_1 = alloca %struct._GUID, align 4 + // CHECK: %s2_2 = alloca %struct._GUID, align 4 + // CHECK: %r = alloca %struct._GUID*, align 4 + // CHECK: %p = alloca %struct._GUID*, align 4 + // CHECK: %zeroiid = alloca %struct._GUID*, align 4 + GUID s2_1 = __uuidof(S2); + GUID s2_2 = __uuidof(S2); + + // CHECK: store %struct._GUID* @__uuid_87654321-4321-4321-4321-ba0987654321, %struct._GUID** %r, align 4 + const GUID& r = __uuidof(S2); + // CHECK: store %struct._GUID* @__uuid_87654321-4321-4321-4321-ba0987654321, %struct._GUID** %p, align 4 + const GUID* p = &__uuidof(S2); + + // Special case _uuidof(0), local scope version. + // CHECK: store %struct._GUID* @__uuid_00000000-0000-0000-0000-000000000000, %struct._GUID** %zeroiid, align 4 + const GUID& zeroiid = __uuidof(0); +}