]> granicus.if.org Git - clang/commitdiff
(Hopefully) instantiate dependent array types correctly.
authorAnders Carlsson <andersca@mac.com>
Sun, 15 Mar 2009 20:12:13 +0000 (20:12 +0000)
committerAnders Carlsson <andersca@mac.com>
Sun, 15 Mar 2009 20:12:13 +0000 (20:12 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67032 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Lex/Preprocessor.cpp
lib/Sema/SemaTemplateInstantiate.cpp
test/SemaTemplate/instantiate-array.cpp [new file with mode: 0644]

index 38a6919be37bc67549c446197a8b400078426df5..3afa4ee6b239f7cb5b71f6965447ad886d98b3cd 100644 (file)
@@ -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
index b7f0bbc923ae7465114f866d0373cff038e6275c..dbdc5a8a7169fc9d06e959b82534e87b4a5072bd 100644 (file)
@@ -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 (file)
index 0000000..d67645e
--- /dev/null
@@ -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 <int N> 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 <typename T> class TenElementArray {
+  int elems[10];
+};
+
+static_assert(sizeof(TenElementArray<int>) == sizeof(int) * 10, "Array size mismatch");
+
+template<typename T, int N> class Array {
+  T elems[N];
+};
+
+static_assert(sizeof(Array<int, 10>) == sizeof(int) * 10, "Array size mismatch");