[cfe-commits] r39253 - in /cfe/cfe/trunk: AST/Decl.cpp AST/Sema.h AST/SemaDecl.cpp Sema/Sema.h Sema/SemaDecl.cpp include/clang/AST/Decl.h include/clang/Parse/Action.h include/clang/Parse/DeclSpec.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:41:59 PDT 2007


Author: sabre
Date: Wed Jul 11 11:41:59 2007
New Revision: 39253

URL: http://llvm.org/viewvc/llvm-project?rev=39253&view=rev
Log:
Next big step in function parsing: create decl objects for parameters,
inserting them into the function body scope and registering them with the
corresponding FunctionDecl.

Modified:
    cfe/cfe/trunk/AST/Decl.cpp
    cfe/cfe/trunk/AST/Sema.h
    cfe/cfe/trunk/AST/SemaDecl.cpp
    cfe/cfe/trunk/Sema/Sema.h
    cfe/cfe/trunk/Sema/SemaDecl.cpp
    cfe/cfe/trunk/include/clang/AST/Decl.h
    cfe/cfe/trunk/include/clang/Parse/Action.h
    cfe/cfe/trunk/include/clang/Parse/DeclSpec.h

Modified: cfe/cfe/trunk/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Decl.cpp?rev=39253&r1=39252&r2=39253&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Decl.cpp (original)
+++ cfe/cfe/trunk/AST/Decl.cpp Wed Jul 11 11:41:59 2007
@@ -23,3 +23,20 @@
 const char *Decl::getName() const {
   return getIdentifier()->getName();
 }
+
+
+FunctionDecl::~FunctionDecl() {
+  delete[] ParamInfo;
+}
+
+unsigned FunctionDecl::getNumParams() const {
+  return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs();
+}
+
+void FunctionDecl::setParams(VarDecl **NewParamInfo, unsigned NumParams) {
+  assert(ParamInfo == 0 && "Already has param info!");
+  assert(NumParams == getNumParams() && "Parameter count mismatch!");
+  
+  ParamInfo = new VarDecl*[NumParams];
+  memcpy(ParamInfo, NewParamInfo, sizeof(VarDecl*)*NumParams);
+}

Modified: cfe/cfe/trunk/AST/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Sema.h?rev=39253&r1=39252&r2=39253&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Sema.h (original)
+++ cfe/cfe/trunk/AST/Sema.h Wed Jul 11 11:41:59 2007
@@ -24,9 +24,11 @@
   class ASTContext;
   class Preprocessor;
   class Decl;
+  class VarDecl;
   class TypeRef;
   class LangOptions;
   class FunctionDecl;
+  class DeclaratorChunk;
   
 /// Sema - This implements semantic analysis and AST building for C.
 class Sema : public Action {
@@ -66,8 +68,9 @@
   virtual DeclTy *isTypeName(const IdentifierInfo &II, Scope *S) const;
   virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
                                   DeclTy *LastInGroup);
-  virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D
-                                          /* TODO: FORMAL ARG INFO.*/);
+  VarDecl *ParseParamDeclarator(DeclaratorChunk &FI, unsigned ArgNo,
+                                Scope *FnBodyScope);
+  virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D);
   virtual DeclTy *ParseFunctionDefBody(DeclTy *Decl, StmtTy *Body);
   virtual void PopScope(SourceLocation Loc, Scope *S);
   

Modified: cfe/cfe/trunk/AST/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/SemaDecl.cpp?rev=39253&r1=39252&r2=39253&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/SemaDecl.cpp (original)
+++ cfe/cfe/trunk/AST/SemaDecl.cpp Wed Jul 11 11:41:59 2007
@@ -101,10 +101,34 @@
   return New;
 }
 
+VarDecl *
+Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
+                           Scope *FnScope) {
+  const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
 
+  IdentifierInfo *II = PI.Ident;
+  Decl *PrevDecl = 0;
+  
+  if (II) {
+    PrevDecl = II->getFETokenInfo<Decl>();
+    
+    // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
+  }
+  
+  VarDecl *New = new VarDecl(II, static_cast<Type*>(PI.TypeInfo), PrevDecl);
+  
+  // If this has an identifier, add it to the scope stack.
+  if (II) {
+    // If PrevDecl includes conflicting name here, emit a diagnostic.
+    II->setFETokenInfo(New);
+    FnScope->AddDecl(II);
+  }
 
-Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *S, Declarator &D
-                                            /* TODO: FORMAL ARG INFO.*/) {
+  return New;
+}
+  
+
+Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
   assert(CurFunctionDecl == 0 && "Function parsing confused");
   assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
          "Not a function declarator!");
@@ -131,14 +155,18 @@
     
   }
   
-  FunctionDecl *FD = static_cast<FunctionDecl*>(ParseDeclarator(S, D, 0, 0));
-  CurFunctionDecl = FD;
+  Scope *GlobalScope = FnBodyScope->getParent();
   
-  // Since this is a function definition, remember the names of the arguments in
-  // the FunctionDecl.
+  FunctionDecl *FD =
+    static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
+  CurFunctionDecl = FD;
   
-  // FIXME: TODO.  Add to FunctionDecl, install declarators into current scope.
+  // Create Decl objects for each parameter, adding them to the FunctionDecl.
+  SmallVector<VarDecl*, 16> Params;
+  for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
+    Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i, FnBodyScope));
   
+  FD->setParams(&Params[0], Params.size());
   
   return FD;
 }

Modified: cfe/cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/Sema.h?rev=39253&r1=39252&r2=39253&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/Sema.h (original)
+++ cfe/cfe/trunk/Sema/Sema.h Wed Jul 11 11:41:59 2007
@@ -24,9 +24,11 @@
   class ASTContext;
   class Preprocessor;
   class Decl;
+  class VarDecl;
   class TypeRef;
   class LangOptions;
   class FunctionDecl;
+  class DeclaratorChunk;
   
 /// Sema - This implements semantic analysis and AST building for C.
 class Sema : public Action {
@@ -66,8 +68,9 @@
   virtual DeclTy *isTypeName(const IdentifierInfo &II, Scope *S) const;
   virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
                                   DeclTy *LastInGroup);
-  virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D
-                                          /* TODO: FORMAL ARG INFO.*/);
+  VarDecl *ParseParamDeclarator(DeclaratorChunk &FI, unsigned ArgNo,
+                                Scope *FnBodyScope);
+  virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D);
   virtual DeclTy *ParseFunctionDefBody(DeclTy *Decl, StmtTy *Body);
   virtual void PopScope(SourceLocation Loc, Scope *S);
   

Modified: cfe/cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/SemaDecl.cpp?rev=39253&r1=39252&r2=39253&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaDecl.cpp Wed Jul 11 11:41:59 2007
@@ -101,10 +101,34 @@
   return New;
 }
 
+VarDecl *
+Sema::ParseParamDeclarator(DeclaratorChunk &FTI, unsigned ArgNo,
+                           Scope *FnScope) {
+  const DeclaratorChunk::ParamInfo &PI = FTI.Fun.ArgInfo[ArgNo];
 
+  IdentifierInfo *II = PI.Ident;
+  Decl *PrevDecl = 0;
+  
+  if (II) {
+    PrevDecl = II->getFETokenInfo<Decl>();
+    
+    // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
+  }
+  
+  VarDecl *New = new VarDecl(II, static_cast<Type*>(PI.TypeInfo), PrevDecl);
+  
+  // If this has an identifier, add it to the scope stack.
+  if (II) {
+    // If PrevDecl includes conflicting name here, emit a diagnostic.
+    II->setFETokenInfo(New);
+    FnScope->AddDecl(II);
+  }
 
-Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *S, Declarator &D
-                                            /* TODO: FORMAL ARG INFO.*/) {
+  return New;
+}
+  
+
+Sema::DeclTy *Sema::ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
   assert(CurFunctionDecl == 0 && "Function parsing confused");
   assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
          "Not a function declarator!");
@@ -131,14 +155,18 @@
     
   }
   
-  FunctionDecl *FD = static_cast<FunctionDecl*>(ParseDeclarator(S, D, 0, 0));
-  CurFunctionDecl = FD;
+  Scope *GlobalScope = FnBodyScope->getParent();
   
-  // Since this is a function definition, remember the names of the arguments in
-  // the FunctionDecl.
+  FunctionDecl *FD =
+    static_cast<FunctionDecl*>(ParseDeclarator(GlobalScope, D, 0, 0));
+  CurFunctionDecl = FD;
   
-  // FIXME: TODO.  Add to FunctionDecl, install declarators into current scope.
+  // Create Decl objects for each parameter, adding them to the FunctionDecl.
+  SmallVector<VarDecl*, 16> Params;
+  for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
+    Params.push_back(ParseParamDeclarator(D.getTypeObject(0), i, FnBodyScope));
   
+  FD->setParams(&Params[0], Params.size());
   
   return FD;
 }

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

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Decl.h Wed Jul 11 11:41:59 2007
@@ -118,7 +118,12 @@
 /// FunctionDecl - An instance of this class is created to represent a function
 /// declaration or definition.
 class FunctionDecl : public ObjectDecl {
-  // FIXME: Args etc.
+  /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
+  /// parameters of this function.  This is null if a prototype or if there are
+  /// no formals.  TODO: we could allocate this space immediately after the
+  /// FunctionDecl object to save an allocation like FunctionType does.
+  VarDecl **ParamInfo;
+  
   Stmt *Body;  // Null if a prototype.
   
   /// DeclChain - Linked list of declarations that are defined inside this
@@ -126,14 +131,18 @@
   Decl *DeclChain;
 public:
   FunctionDecl(IdentifierInfo *Id, TypeRef T, Decl *Next)
-    : ObjectDecl(Function, Id, T, Next), Body(0), DeclChain(0) {}
+    : ObjectDecl(Function, Id, T, Next), ParamInfo(0), Body(0), DeclChain(0) {}
+  virtual ~FunctionDecl();
   
   Stmt *getBody() const { return Body; }
   void setBody(Stmt *B) { Body = B; }
   
   Decl *getDeclChain() const { return DeclChain; }
   void setDeclChain(Decl *D) { DeclChain = D; }
-  
+
+  unsigned getNumParams() const;
+  void setParams(VarDecl **NewParamInfo, unsigned NumParams);
+    
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { return D->getKind() == Function; }
   static bool classof(const FunctionDecl *D) { return true; }

Modified: cfe/cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Parse/Action.h?rev=39253&r1=39252&r2=39253&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Action.h Wed Jul 11 11:41:59 2007
@@ -98,11 +98,11 @@
   }
 
   /// ParseStartOfFunctionDef - This is called at the start of a function
-  /// definition, instead of calling ParseDeclarator.
-  virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D
-                                          /* TODO: FORMAL ARG INFO.*/) {
+  /// definition, instead of calling ParseDeclarator.  The Declarator includes
+  /// information about formal arguments that are part of this function.
+  virtual DeclTy *ParseStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
     // Default to ParseDeclarator.
-    return ParseDeclarator(S, D, 0, 0);
+    return ParseDeclarator(FnBodyScope, D, 0, 0);
   }
 
   /// ParseFunctionDefBody - This is called when a function body has completed

Modified: cfe/cfe/trunk/include/clang/Parse/DeclSpec.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Parse/DeclSpec.h?rev=39253&r1=39252&r2=39253&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/DeclSpec.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/DeclSpec.h Wed Jul 11 11:41:59 2007
@@ -259,7 +259,6 @@
   /// Parameter type lists will have type info (if the actions module provides
   /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'.
   struct ParamInfo {
-    /// Ident - In a K&R 
     IdentifierInfo *Ident;
     SourceLocation IdentLoc;
     Action::TypeTy *TypeInfo;





More information about the cfe-commits mailing list