[cfe-commits] r61295 - in /cfe/trunk: include/clang/AST/Type.h lib/AST/Type.cpp lib/CodeGen/CGExpr.cpp

Anders Carlsson andersca at mac.com
Sat Dec 20 16:11:24 PST 2008


Author: andersca
Date: Sat Dec 20 18:11:23 2008
New Revision: 61295

URL: http://llvm.org/viewvc/llvm-project?rev=61295&view=rev
Log:
Handle VLA indexing

Modified:
    cfe/trunk/include/clang/AST/Type.h
    cfe/trunk/lib/AST/Type.cpp
    cfe/trunk/lib/CodeGen/CGExpr.cpp

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=61295&r1=61294&r2=61295&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Sat Dec 20 18:11:23 2008
@@ -826,6 +826,10 @@
     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);

Modified: cfe/trunk/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Type.cpp?rev=61295&r1=61294&r2=61295&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Sat Dec 20 18:11:23 2008
@@ -803,7 +803,6 @@
   return isa<EnumDecl>(TT->getDecl());
 }
 
-
 //===----------------------------------------------------------------------===//
 // Type Printing
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=61295&r1=61294&r2=61295&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Sat Dec 20 18:11:23 2008
@@ -706,8 +706,25 @@
 
   // 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"),





More information about the cfe-commits mailing list