diff options
author | Anders Carlsson <andersca@mac.com> | 2009-06-07 18:45:35 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-06-07 18:45:35 +0000 |
commit | fc0f021b492cf28ee7b3a6bd4445ae569e6f15de (patch) | |
tree | df509d322e1dcc5ba3dc0e80329c34e22592ef94 | |
parent | a106fea080c9a9fd50c9e5fc26b9752c43614639 (diff) |
Template instantiation support for Obj-C @encode expressions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73034 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/AST/ExprObjC.h | 3 | ||||
-rw-r--r-- | lib/Sema/Sema.h | 4 | ||||
-rw-r--r-- | lib/Sema/SemaExprObjC.cpp | 37 | ||||
-rw-r--r-- | lib/Sema/SemaTemplateInstantiateExpr.cpp | 14 | ||||
-rw-r--r-- | test/SemaTemplate/instantiate-objc-1.mm | 12 |
5 files changed, 53 insertions, 17 deletions
diff --git a/include/clang/AST/ExprObjC.h b/include/clang/AST/ExprObjC.h index 51b99610cd..a573025ba7 100644 --- a/include/clang/AST/ExprObjC.h +++ b/include/clang/AST/ExprObjC.h @@ -64,7 +64,8 @@ class ObjCEncodeExpr : public Expr { public: ObjCEncodeExpr(QualType T, QualType ET, SourceLocation at, SourceLocation rp) - : Expr(ObjCEncodeExprClass, T), EncType(ET), AtLoc(at), RParenLoc(rp) {} + : Expr(ObjCEncodeExprClass, T, ET->isDependentType(), + ET->isDependentType()), EncType(ET), AtLoc(at), RParenLoc(rp) {} explicit ObjCEncodeExpr(EmptyShell Empty) : Expr(ObjCEncodeExprClass, Empty){} diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index d3bfef67cb..c5582935db 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -1696,6 +1696,10 @@ public: virtual ExprResult ParseObjCStringLiteral(SourceLocation *AtLocs, ExprTy **Strings, unsigned NumStrings); + + Expr *BuildObjCEncodeExpression(SourceLocation AtLoc, + QualType EncodedType, + SourceLocation RParenLoc); virtual ExprResult ParseObjCEncodeExpression(SourceLocation AtLoc, SourceLocation EncodeLoc, SourceLocation LParenLoc, diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index eabc87d7f3..b6cf9d8e73 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -92,6 +92,29 @@ Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]); } +Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc, + QualType EncodedType, + SourceLocation RParenLoc) { + QualType StrTy; + if (EncodedType->isDependentType()) + StrTy = Context.DependentTy; + else { + std::string Str; + Context.getObjCEncodingForType(EncodedType, Str); + + // The type of @encode is the same as the type of the corresponding string, + // which is an array type. + StrTy = Context.CharTy; + // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). + if (getLangOptions().CPlusPlus) + StrTy.addConst(); + StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), + ArrayType::Normal, 0); + } + + return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc); +} + Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, SourceLocation EncodeLoc, SourceLocation LParenLoc, @@ -99,19 +122,7 @@ Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, SourceLocation RParenLoc) { QualType EncodedType = QualType::getFromOpaquePtr(ty); - std::string Str; - Context.getObjCEncodingForType(EncodedType, Str); - - // The type of @encode is the same as the type of the corresponding string, - // which is an array type. - QualType StrTy = Context.CharTy; - // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). - if (getLangOptions().CPlusPlus) - StrTy.addConst(); - StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), - ArrayType::Normal, 0); - - return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc); + return BuildObjCEncodeExpression(AtLoc, EncodedType, RParenLoc); } Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, diff --git a/lib/Sema/SemaTemplateInstantiateExpr.cpp b/lib/Sema/SemaTemplateInstantiateExpr.cpp index 5ba42f2e5a..9576627577 100644 --- a/lib/Sema/SemaTemplateInstantiateExpr.cpp +++ b/lib/Sema/SemaTemplateInstantiateExpr.cpp @@ -1222,9 +1222,17 @@ TemplateExprInstantiator::VisitObjCStringLiteral(ObjCStringLiteral *E) { } Sema::OwningExprResult -TemplateExprInstantiator::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { - assert(false && "FIXME: Template instantiations for ObjC expressions"); - return SemaRef.ExprError(); +TemplateExprInstantiator::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { + QualType EncodedType = SemaRef.InstantiateType(E->getEncodedType(), + TemplateArgs, + /*FIXME:*/E->getAtLoc(), + DeclarationName()); + if (EncodedType.isNull()) + return SemaRef.ExprError(); + + return SemaRef.Owned(SemaRef.BuildObjCEncodeExpression(E->getAtLoc(), + EncodedType, + E->getRParenLoc())); } Sema::OwningExprResult diff --git a/test/SemaTemplate/instantiate-objc-1.mm b/test/SemaTemplate/instantiate-objc-1.mm new file mode 100644 index 0000000000..d0321e9101 --- /dev/null +++ b/test/SemaTemplate/instantiate-objc-1.mm @@ -0,0 +1,12 @@ +// RUN: clang-cc -fsyntax-only -verify %s + +// @encode expressions + +template <typename T> struct Encode { + static const char *encode(T t) { + return @encode(T); + } +}; + +template struct Encode<int>; +template struct Encode<double>; |