From: Anders Carlsson Date: Sun, 15 Mar 2009 20:12:13 +0000 (+0000) Subject: (Hopefully) instantiate dependent array types correctly. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=76b1c842c3932d3f83b3abf999dd9622e3e5fb12;p=clang (Hopefully) instantiate dependent array types correctly. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67032 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index 38a6919be3..3afa4ee6b2 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -488,6 +488,9 @@ static void InitializePredefinedMacros(Preprocessor &PP, else if (0) // STDC94 ? DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); + if (PP.getLangOptions().CPlusPlus0x) + DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__"); + if (PP.getLangOptions().Freestanding) DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0"); else diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp index b7f0bbc923..dbdc5a8a71 100644 --- a/lib/Sema/SemaTemplateInstantiate.cpp +++ b/lib/Sema/SemaTemplateInstantiate.cpp @@ -283,9 +283,27 @@ QualType TemplateTypeInstantiator:: InstantiateDependentSizedArrayType(const DependentSizedArrayType *T, unsigned Quals) const { - // FIXME: Implement this - assert(false && "Cannot instantiate DependentSizedArrayType yet"); - return QualType(); + Expr *ArraySize = T->getSizeExpr(); + assert(ArraySize->isValueDependent() && + "dependent sized array types must have value dependent size expr"); + + // Instantiate the element type if needed + QualType ElementType = T->getElementType(); + if (ElementType->isDependentType()) { + ElementType = Instantiate(ElementType); + if (ElementType.isNull()) + return QualType(); + } + + // Instantiate the size expression + Sema::OwningExprResult InstantiatedArraySize = + SemaRef.InstantiateExpr(ArraySize, TemplateArgs, NumTemplateArgs); + if (InstantiatedArraySize.isInvalid()) + return QualType(); + + return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), + (Expr *)InstantiatedArraySize.release(), + T->getIndexTypeQualifier(), Loc, Entity); } QualType diff --git a/test/SemaTemplate/instantiate-array.cpp b/test/SemaTemplate/instantiate-array.cpp new file mode 100644 index 0000000000..d67645e041 --- /dev/null +++ b/test/SemaTemplate/instantiate-array.cpp @@ -0,0 +1,28 @@ +// RUN: clang -fsyntax-only -verify %s -std=c++0x + +#ifndef __GXX_EXPERIMENTAL_CXX0X__ +#define __CONCAT(__X, __Y) __CONCAT1(__X, __Y) +#define __CONCAT1(__X, __Y) __X ## __Y + +#define static_assert(__b, __m) \ + typedef int __CONCAT(__sa, __LINE__)[__b ? 1 : -1] +#endif + +template class IntArray { + int elems[N]; +}; + +static_assert(sizeof(IntArray<10>) == sizeof(int) * 10, "Array size mismatch"); +static_assert(sizeof(IntArray<1>) == sizeof(int) * 1, "Array size mismatch"); + +template class TenElementArray { + int elems[10]; +}; + +static_assert(sizeof(TenElementArray) == sizeof(int) * 10, "Array size mismatch"); + +template class Array { + T elems[N]; +}; + +static_assert(sizeof(Array) == sizeof(int) * 10, "Array size mismatch");