]> granicus.if.org Git - clang/commitdiff
Handle VLA indexing
authorAnders Carlsson <andersca@mac.com>
Sun, 21 Dec 2008 00:11:23 +0000 (00:11 +0000)
committerAnders Carlsson <andersca@mac.com>
Sun, 21 Dec 2008 00:11:23 +0000 (00:11 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61295 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Type.h
lib/AST/Type.cpp
lib/CodeGen/CGExpr.cpp

index ffcc9f6457b507e51d91a47e111bf9f570ccbe61..93d31c7e7d179abc31534e4dcd0c8022f709a293 100644 (file)
@@ -826,6 +826,10 @@ public:
     assert (0 && "Cannnot unique VariableArrayTypes.");
   }
   
+  /// Returns the innermost element type of a VAT - for example
+  /// will return "int" for int[n][m].
+  QualType getBaseType() const;
+  
 protected:  
   virtual void EmitImpl(llvm::Serializer& S) const;
   static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
index 5909c976aa6aaf0ca7bd016ff3ad66c8f95c9179..b9d97ced13b5608a520c8b51a814e87f521095ba 100644 (file)
@@ -803,7 +803,6 @@ bool EnumType::classof(const TagType *TT) {
   return isa<EnumDecl>(TT->getDecl());
 }
 
-
 //===----------------------------------------------------------------------===//
 // Type Printing
 //===----------------------------------------------------------------------===//
index b085de304aac65b75a9caf08ce93886bd6a04df1..3bf6b2f8091f66fe8a6ef96a1e9f3ec54afb72da 100644 (file)
@@ -706,8 +706,25 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
 
   // We know that the pointer points to a type of the correct size, unless the
   // size is a VLA.
-  if (!E->getType()->isConstantSizeType())
-    return EmitUnsupportedLValue(E, "VLA index");
+  if (const VariableArrayType *VAT = 
+        getContext().getAsVariableArrayType(E->getType())) {
+    llvm::Value *VLASize = VLASizeMap[VAT];
+    
+    Idx = Builder.CreateMul(Idx, VLASize);
+    
+    QualType BaseType = VAT->getElementType();
+    
+    // Divide by the element size.
+    while (const VariableArrayType *AT = 
+           getContext().getAsVariableArrayType(BaseType))
+      BaseType = AT->getElementType();
+  
+    uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
+    Idx = Builder.CreateUDiv(Idx,
+                             llvm::ConstantInt::get(Idx->getType(), 
+                                                    BaseTypeSize));
+  }
+  
   QualType ExprTy = getContext().getCanonicalType(E->getBase()->getType());
 
   return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"),