changeset 44:aabc64b7263e

create two declaration statements, env_buf and retval.
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Wed, 01 Jan 2014 02:12:01 +0900
parents 7116d17d6428
children 9ebfb52ddd9b
files tools/clang/include/clang/Parse/Parser.h tools/clang/lib/Parse/ParseCbC.cpp tools/clang/lib/Parse/ParseExpr.cpp
diffstat 3 files changed, 103 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/tools/clang/include/clang/Parse/Parser.h	Mon Dec 30 00:46:58 2013 +0900
+++ b/tools/clang/include/clang/Parse/Parser.h	Wed Jan 01 02:12:01 2014 +0900
@@ -1504,8 +1504,7 @@
 
 #ifndef noCbC // for CbC
   StmtVector* Stmtsp;
-  StringRef jmp_bufSR;
-  StringRef retvalSR;
+  IdentifierInfo *bufII, *retvalII;
 #endif
 
   StmtResult ParseStatement(SourceLocation *TrailingElseLoc = 0);
@@ -1543,6 +1542,9 @@
   StmtResult CreateSjForContinuationWithEnv();
   StmtResult CreateAssignmentStmt(IdentifierInfo* LHSII = 0, IdentifierInfo* RHSII = 0, unsigned LHSFlags = 0,
 				  unsigned RHSFlags = 0, IdentifierInfo* extraLHSII = 0, IdentifierInfo* extraRHSII = 0);
+  void CreateArrayDecl(ParsingDeclarator &D, SourceLocation Loc);
+  StmtResult CreateDeclStmt(IdentifierInfo *II, bool isArray);
+  IdentifierInfo* CreateSingleIdentifierInfo(const char* Name, int length, SourceLocation Loc);
 #endif
   StmtResult ParseContinueStatement();
   StmtResult ParseBreakStatement();
--- a/tools/clang/lib/Parse/ParseCbC.cpp	Mon Dec 30 00:46:58 2013 +0900
+++ b/tools/clang/lib/Parse/ParseCbC.cpp	Wed Jan 01 02:12:01 2014 +0900
@@ -24,9 +24,12 @@
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/ADT/SmallString.h"
 #include "clang/Sema/Lookup.h"
+#include "clang/Lex/LiteralSupport.h"
 
-#define __CBC_ENVIRONMENT_NAME "__CbC_environment"
-#define __CBC_ENVIRONMENT_LENGTH 17
+#include <cmath>
+#include <sstream>
+
+#include "CbCHelper.h"
 
 using namespace clang;
 
@@ -87,7 +90,6 @@
 StmtResult Parser::Create__returnStmt(){
   SourceLocation Loc = Tok.getLocation();
   ParsedAttributesWithRange __retAttrs(AttrFactory);
-  SourceLocation DeclStart = Loc, DeclEnd = Loc;
   ParsingDeclSpec PDS(*this);
   DeclSpec &__retDS = PDS;
   const char *PrevSpec = 0;
@@ -103,7 +105,7 @@
     assert(DiagID);
     if (DiagID == diag::ext_duplicate_declspec)
       Diag(Tok, DiagID)
-	<< PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
+	<< PrevSpec << FixItHint::CreateRemoval(Loc);
     else
       Diag(Tok, DiagID) << PrevSpec;
   }
@@ -113,7 +115,7 @@
   D.setEllipsisLoc(SourceLocation());
   bool hadGroupingParens = D.hasGroupingParens();
   D.setGroupingParens(true);
-  IdentifierInfo* II = CreateIdentifierInfo("__CbC_return",12, Loc); // CreateIdentifierInfo(name,length of the name, SourceLocation);
+  IdentifierInfo* II = CreateIdentifierInfo(__CBC_RETURN_NAME, __CBC_RETURN_LENGTH, Loc);
   D.SetRangeEnd(Loc);
   DeclSpec DS(AttrFactory);
 
@@ -155,7 +157,7 @@
   DeclsInGroup.push_back(FirstDecl);
   
   DeclGroupPtrTy __retDecl = Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
-  return Actions.ActOnDeclStmt(__retDecl, DeclStart, DeclEnd);
+  return Actions.ActOnDeclStmt(__retDecl, Loc, Loc);
 }
 
 StmtResult Parser::CreateAssignmentStmt(IdentifierInfo* LHSII,IdentifierInfo* RHSII,unsigned LHSFlags,unsigned RHSFlags,
@@ -196,6 +198,73 @@
   return Actions.ActOnExprStmt(Expr);
 }
 
+IdentifierInfo* Parser::CreateSingleIdentifierInfo(const char* Name, int length, SourceLocation Loc){
+  IdentifierInfo *II;
+  int i = 0;
+  std::ostringstream os;
+
+  while (true) {
+    i++;
+    os << Name << i;
+    II = CreateIdentifierInfo(os.str().c_str(),length + (int)(floor(log10(i))+1),Loc);
+    LookupResult Previous(Actions, II, Loc, Actions.LookupOrdinaryName, Actions.ForRedeclaration);
+    if (!Actions.LookupName(Previous, getCurScope()))
+      return II;
+  }
+}
+
+StmtResult Parser::CreateDeclStmt(IdentifierInfo *II, bool isArray){
+  SourceLocation Loc = Tok.getLocation();
+  DeclGroupPtrTy DeclGPT;
+  ParsingDeclSpec DS(*this);
+    
+  if (DS.getSourceRange().isInvalid()) {
+    DS.SetRangeStart(Loc);
+    DS.SetRangeEnd(Loc);
+  }
+
+  bool isInvalid = false;
+  const char *PrevSpec = 0;
+  unsigned DiagID = 0;
+  isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
+  DS.SetRangeEnd(Loc);
+  DS.Finish(Diags, PP);
+  ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Declarator::BlockContext));
+  DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
+  D.SetIdentifier(II, Loc);
+    
+  if(isArray)
+    CreateArrayDecl(D, Loc);
+
+  SmallVector<Decl *, 8> DeclsInGroup;
+  Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
+  D.complete(FirstDecl);
+  DeclsInGroup.push_back(FirstDecl);
+  DeclGPT =  Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
+  return Actions.ActOnDeclStmt(DeclGPT, Loc, Loc);
+}
+
+void Parser::CreateArrayDecl(ParsingDeclarator &D, SourceLocation Loc){
+  ExprResult ExprRes;
+  ParsedAttributesWithRange attrs(AttrFactory);
+  SmallString<128> SpellingBuffer;
+  SpellingBuffer.resize(__JMP_BUF_SIZE_LENGTH + 1);
+  StringRef TokSpelling = StringRef(__JMP_BUF_SIZE,__JMP_BUF_SIZE_LENGTH);
+  NumericLiteralParser Literal(TokSpelling, Loc, PP);
+  Expr *sizeRes;
+  QualType Ty;
+  unsigned MaxWidth = Actions.Context.getTargetInfo().getIntMaxTWidth();
+  llvm::APInt ResultVal(MaxWidth, 0);
+  Literal.GetIntegerValue(ResultVal);
+  unsigned Width = 0;
+  unsigned IntSize = Actions.Context.getTargetInfo().getIntWidth();
+  Ty = Actions.Context.IntTy;
+  Width = IntSize;
+  ResultVal = ResultVal.trunc(Width);
+  sizeRes = IntegerLiteral::Create(Actions.Context, ResultVal, Ty, Loc);
+  ExprRes = Actions.Owned(sizeRes);
+  D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,ExprRes.release(),Loc,Loc),attrs,Loc);
+}
 
 StmtResult Parser::CreateSjForContinuationWithEnv(){
   SourceLocation Loc = Tok.getLocation();
@@ -221,7 +290,7 @@
     
   StmtResult StmtRes;
   ParseScope CompoundScope(this, Scope::DeclScope);
-  PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),Tok.getLocation(),"in create setjmp statement for CbC");
+  PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),Loc,"in create setjmp statement for CbC");
   StmtVector innerStmts;
   StmtResult innerStmtRes;
   ExprResult innerExprRes;
@@ -236,7 +305,7 @@
   const char *PrevSpec = 0;
   unsigned DiagID = 0;
   isInvalid = CastDS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
-  CastDS.SetRangeEnd(Tok.getLocation());
+  CastDS.SetRangeEnd(Loc);
   isInvalid = false;
   DiagID = 0;
   ProhibitAttributes(attrs);
--- a/tools/clang/lib/Parse/ParseExpr.cpp	Mon Dec 30 00:46:58 2013 +0900
+++ b/tools/clang/lib/Parse/ParseExpr.cpp	Wed Jan 01 02:12:01 2014 +0900
@@ -31,6 +31,10 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
 
+#ifndef noCbC
+#include "CbCHelper.h"
+#endif
+
 using namespace clang;
 
 /// \brief Simple precedence-based parser for binary/ternary operators.
@@ -2426,22 +2430,28 @@
     else if (Tok.is(tok::kw___return)){
       StmtResult Res;
       SourceLocation Loc = Tok.getLocation();
+      IdentifierInfo *II;
       Res = Create__returnStmt();
       if (Res.isUsable())
       	Stmtsp->push_back(Res.release());
-      
 
-      Res = CreateAssignmentStmt(CreateIdentifierInfo("__CbC_environment", 17, Loc),
-					    CreateIdentifierInfo("retval", 6, Loc),
-					    HasPeriod, HasAmp,
-					    CreateIdentifierInfo("ret_p", 5, Loc));
+      bufII = CreateSingleIdentifierInfo(__CBC_BUF_NAME, __CBC_BUF_LENGTH, Loc);
+      Res = CreateDeclStmt(bufII, true);
       if (Res.isUsable())
       	Stmtsp->push_back(Res.release());
 
-      Res = CreateAssignmentStmt(CreateIdentifierInfo("__CbC_environment", 17, Loc),
-					    CreateIdentifierInfo("i_buf", 5, Loc),
-					    HasPeriod, 0,
-					    CreateIdentifierInfo("env", 3, Loc));
+      retvalII = CreateSingleIdentifierInfo(__CBC_RETVAL_NAME, __CBC_RETVAL_LENGTH, Loc);
+      Res = CreateDeclStmt(retvalII, false);
+      if (Res.isUsable())
+      	Stmtsp->push_back(Res.release());
+
+      Res = CreateAssignmentStmt(CreateIdentifierInfo(__CBC_ENVIRONMENT_NAME, __CBC_ENVIRONMENT_LENGTH, Loc),
+				 retvalII, HasPeriod, HasAmp, CreateIdentifierInfo("ret_p", 5, Loc));
+      if (Res.isUsable())
+      	Stmtsp->push_back(Res.release());
+
+      Res = CreateAssignmentStmt(CreateIdentifierInfo(__CBC_ENVIRONMENT_NAME, __CBC_ENVIRONMENT_LENGTH, Loc),
+				 bufII, HasPeriod, 0, CreateIdentifierInfo("env", 3, Loc));
       if (Res.isUsable())
       	Stmtsp->push_back(Res.release());
 
@@ -2449,15 +2459,15 @@
       if (Res.isUsable())
 	Stmtsp->push_back(Res.release());
       
-      Res = CreateAssignmentStmt(CreateIdentifierInfo("__CbC_return", 12, Loc),
+      Res = CreateAssignmentStmt(CreateIdentifierInfo(__CBC_RETURN_NAME, __CBC_RETURN_LENGTH, Loc),
 					    CreateIdentifierInfo("return1", 7, Loc));
       if (Res.isUsable())
       	Stmtsp->push_back(Res.release());
       
-      IdentifierInfo &II = *CreateIdentifierInfo("__CbC_return", 12, Tok.getLocation()); // (name, length of the name, location of the name)
+      II = CreateIdentifierInfo(__CBC_RETURN_NAME, __CBC_RETURN_LENGTH, Tok.getLocation());
       SourceLocation ILoc = ConsumeToken();
       UnqualifiedId Name;
-      Name.setIdentifier(&II, ILoc);
+      Name.setIdentifier(II, ILoc);
       CXXScopeSpec ScopeSpec;
       SourceLocation TemplateKWLoc;