changeset 43:7116d17d6428

Two assignment statements, __CbC_environment.env = i_buf and _CbC_environment.ret_p = &retval, were createed automarically when we found __return.
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Mon, 30 Dec 2013 00:46:58 +0900
parents 3e178477409f
children aabc64b7263e
files tools/clang/include/clang/Parse/Parser.h tools/clang/lib/Parse/ParseCbC.cpp tools/clang/lib/Parse/ParseExpr.cpp
diffstat 3 files changed, 81 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/tools/clang/include/clang/Parse/Parser.h	Fri Dec 27 00:08:21 2013 +0900
+++ b/tools/clang/include/clang/Parse/Parser.h	Mon Dec 30 00:46:58 2013 +0900
@@ -1503,7 +1503,9 @@
   typedef SmallVector<ParsedType, 12> TypeVector;
 
 #ifndef noCbC // for CbC
-  StmtVector* Stmtsp; // for CbC.
+  StmtVector* Stmtsp;
+  StringRef jmp_bufSR;
+  StringRef retvalSR;
 #endif
 
   StmtResult ParseStatement(SourceLocation *TrailingElseLoc = 0);
@@ -1538,8 +1540,9 @@
 #ifndef noCbC
   StmtResult ParseCbCGotoStatement(ParsedAttributesWithRange &Attrs,StmtVector &Stmts);
   StmtResult Create__returnStmt();
-  StmtResult CreateReturnAssignmentStmt();
   StmtResult CreateSjForContinuationWithEnv();
+  StmtResult CreateAssignmentStmt(IdentifierInfo* LHSII = 0, IdentifierInfo* RHSII = 0, unsigned LHSFlags = 0,
+				  unsigned RHSFlags = 0, IdentifierInfo* extraLHSII = 0, IdentifierInfo* extraRHSII = 0);
 #endif
   StmtResult ParseContinueStatement();
   StmtResult ParseBreakStatement();
@@ -1711,6 +1714,17 @@
 			    const char* PrevSpec, unsigned* DiagID, const char* Name, int NameLen);
   ExprResult LookupAndDeclareName(IdentifierInfo *II);
   ExprResult LookupMemberAndBuildExpr(IdentifierInfo *II, Expr* Base, bool IsArrow);
+  enum AssignmentFlags {
+    HasStar   = 0x01,
+    HasAmp    = 0x02,
+    HasPeriod = 0x04,
+    HasArrow  = 0x08
+  };
+  
+  bool hasStar(unsigned F) { return F & HasStar; }
+  bool hasAmp(unsigned F) { return F & HasAmp; }
+  bool hasPeriod(unsigned F) { return F & HasPeriod; }
+  bool hasArrow(unsigned F) { return F & HasArrow; }
 #endif
 
   bool isDeclarationSpecifier(bool DisambiguatingWithExpression = false);
--- a/tools/clang/lib/Parse/ParseCbC.cpp	Fri Dec 27 00:08:21 2013 +0900
+++ b/tools/clang/lib/Parse/ParseCbC.cpp	Mon Dec 30 00:46:58 2013 +0900
@@ -1,3 +1,5 @@
+#ifndef noCbC
+
 #include "clang/Parse/Parser.h"
 #include "RAIIObjectsForParser.h"
 #include "clang/AST/ASTContext.h"
@@ -22,6 +24,10 @@
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/ADT/SmallString.h"
 #include "clang/Sema/Lookup.h"
+
+#define __CBC_ENVIRONMENT_NAME "__CbC_environment"
+#define __CBC_ENVIRONMENT_LENGTH 17
+
 using namespace clang;
 
 namespace ExternalSpace { // from ParseExpr.cpp , ParseStmt.cpp
@@ -77,6 +83,7 @@
 };
 }
 
+
 StmtResult Parser::Create__returnStmt(){
   SourceLocation Loc = Tok.getLocation();
   ParsedAttributesWithRange __retAttrs(AttrFactory);
@@ -151,33 +158,42 @@
   return Actions.ActOnDeclStmt(__retDecl, DeclStart, DeclEnd);
 }
 
-StmtResult Parser::CreateReturnAssignmentStmt(){
-  ExprResult Res,LHS,RHS;
-  Token LHSToken,Next;
-  IdentifierInfo *II;
+StmtResult Parser::CreateAssignmentStmt(IdentifierInfo* LHSII,IdentifierInfo* RHSII,unsigned LHSFlags,unsigned RHSFlags,
+					IdentifierInfo* extraLHSII,IdentifierInfo* extraRHSII){
+  ExprResult Expr,LHS,RHS;
+  
+  Token Next,LHSToken;
   SourceLocation Loc = Tok.getLocation();
   CXXScopeSpec SS;
-  II = CreateIdentifierInfo("__CbC_return", 12, Loc);
   Next.startToken();
-  Next.setLocation(Loc);
-  Next.setKind(tok::equal);
+  Next.setKind(tok::period);
   LHSToken.startToken();
   LHSToken.setLocation(Loc);
-  LHSToken.setLength(II->getLength());
-  LHSToken.setIdentifierInfo(II);
-  LHSToken.setKind(tok::identifier);
-
+  LHSToken.setIdentifierInfo(LHSII);
+  LHSToken.setKind(tok::annot_primary_expr);
   ExternalSpace::StatementFilterCCC Validator(Next);
-  Sema::NameClassification Classification = Actions.ClassifyName(getCurScope(), SS, II, Loc, Next, false, SS.isEmpty() ? &Validator : 0);
-
-  LHSToken.setKind(tok::annot_primary_expr);
+  Sema::NameClassification Classification = Actions.ClassifyName(getCurScope(), SS, LHSII, Loc, Next, false, SS.isEmpty() ? &Validator : 0);
   setExprAnnotation(LHSToken, Classification.getExpression());
   LHSToken.setAnnotationEndLoc(Loc);
   PP.AnnotateCachedTokens(LHSToken);
+  
   LHS = getExprAnnotation(LHSToken);
-  RHS = LookupAndDeclareName(CreateIdentifierInfo("return1", 7, Loc));
-  Res = Actions.CreateBuiltinBinOp(Loc, BO_Assign, LHS.take(), RHS.take());
-  return Actions.ActOnExprStmt(Res);
+
+  if ( hasPeriod(LHSFlags) ) 
+    LHS = LookupMemberAndBuildExpr(extraLHSII, LHS.take(), false);
+  else if ( hasArrow(LHSFlags) ) 
+    LHS = LookupMemberAndBuildExpr(extraLHSII, LHS.take(), true); // It is not sure.
+  
+  RHS = LookupAndDeclareName(RHSII);
+  if ( hasAmp(RHSFlags) )
+    RHS = Actions.ActOnUnaryOp(getCurScope(), Loc, tok::amp, RHS.get());
+
+  else if ( hasStar(RHSFlags) )
+    RHS = Actions.ActOnUnaryOp(getCurScope(), Loc, tok::star, RHS.get()); // It is not sure.
+
+  Expr = Actions.ActOnBinOp(getCurScope(), Loc,tok::equal,LHS.take(),RHS.take());
+  
+  return Actions.ActOnExprStmt(Expr);
 }
 
 
@@ -192,7 +208,7 @@
   ExprVector ArgExprs;
   ExprResult __envExprRes = CondExp.get();
 
-  __envExprRes = LookupAndDeclareName(CreateIdentifierInfo("__CbC_environment", 17, Loc));
+  __envExprRes = LookupAndDeclareName(CreateIdentifierInfo(__CBC_ENVIRONMENT_NAME, __CBC_ENVIRONMENT_LENGTH, Loc));
   __envExprRes = LookupMemberAndBuildExpr(CreateIdentifierInfo("env", 3, Loc), __envExprRes.take(), false);
 
   ArgExprs.push_back(__envExprRes.release());
@@ -238,7 +254,7 @@
 							     pointerDS.getRestrictSpecLoc()),
 				 pointerDS.getAttributes(),SourceLocation());
 
-  innerExprRes = LookupAndDeclareName(CreateIdentifierInfo("__CbC_environment", 17, Loc));
+  innerExprRes = LookupAndDeclareName(CreateIdentifierInfo(__CBC_ENVIRONMENT_NAME, __CBC_ENVIRONMENT_LENGTH, Loc));
   innerExprRes = LookupMemberAndBuildExpr(CreateIdentifierInfo("ret_p",5,Loc), innerExprRes.take(), false);
   Expr *CastExpr = innerExprRes.take();
   TypeSourceInfo *castTInfo = Actions.GetTypeForDeclaratorCast(CastDeclaratorInfo, CastExpr->getType());
@@ -304,3 +320,4 @@
   return Actions.BuildMemberReferenceExpr(Base, Base->getType(), Loc, IsArrow, SS, TemplateKWLoc, 0, R, TemplateArgs,false, &ExtraArgs);
 
 }
+#endif
--- a/tools/clang/lib/Parse/ParseExpr.cpp	Fri Dec 27 00:08:21 2013 +0900
+++ b/tools/clang/lib/Parse/ParseExpr.cpp	Mon Dec 30 00:46:58 2013 +0900
@@ -30,6 +30,7 @@
 #include "clang/Sema/TypoCorrection.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
+
 using namespace clang;
 
 /// \brief Simple precedence-based parser for binary/ternary operators.
@@ -2423,19 +2424,35 @@
     } 
 #ifndef noCbC
     else if (Tok.is(tok::kw___return)){
-      StmtResult __returnDeclaration,assginmentStmt,setjmpStmt;
-
-      __returnDeclaration = Create__returnStmt();
-      if (__returnDeclaration.isUsable())
-      	Stmtsp->push_back(__returnDeclaration.release());
+      StmtResult Res;
+      SourceLocation Loc = Tok.getLocation();
+      Res = Create__returnStmt();
+      if (Res.isUsable())
+      	Stmtsp->push_back(Res.release());
       
-      setjmpStmt = CreateSjForContinuationWithEnv();
-      if (setjmpStmt.isUsable())
-	Stmtsp->push_back(setjmpStmt.release());
+
+      Res = CreateAssignmentStmt(CreateIdentifierInfo("__CbC_environment", 17, Loc),
+					    CreateIdentifierInfo("retval", 6, Loc),
+					    HasPeriod, HasAmp,
+					    CreateIdentifierInfo("ret_p", 5, Loc));
+      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));
+      if (Res.isUsable())
+      	Stmtsp->push_back(Res.release());
+
+      Res = CreateSjForContinuationWithEnv();
+      if (Res.isUsable())
+	Stmtsp->push_back(Res.release());
       
-      assginmentStmt = CreateReturnAssignmentStmt();
-      if (assginmentStmt.isUsable())
-      	Stmtsp->push_back(assginmentStmt.release());
+      Res = CreateAssignmentStmt(CreateIdentifierInfo("__CbC_return", 12, 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)
       SourceLocation ILoc = ConsumeToken();
@@ -2444,7 +2461,7 @@
       CXXScopeSpec ScopeSpec;
       SourceLocation TemplateKWLoc;
 
-      CastExpressionIdValidator Validator(false, true); // (isTypeCast != NotTypeCast, isTypeCast != IsTypeCast)
+      CastExpressionIdValidator Validator(false, true);
       Expr = ParseRHSOfBinaryExpression(ParsePostfixExpressionSuffix(Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc, Name, Tok.is(tok::l_paren), /* isAddressOfOperand = */false, &Validator)),prec::Assignment);
       
     }