changeset 39:d93fae3b865e

create __CbC_return declaration statement and return1's assignment statement automarically when we found __return
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Thu, 19 Dec 2013 00:11:02 +0900
parents f083f1411433
children e34ccae0bb26
files tools/clang/include/clang/Basic/TokenKinds.def tools/clang/include/clang/Parse/Parser.h tools/clang/lib/Parse/ParseDecl.cpp tools/clang/lib/Parse/ParseExpr.cpp tools/clang/lib/Parse/ParseStmt.cpp tools/clang/lib/Parse/Parser.cpp
diffstat 6 files changed, 160 insertions(+), 133 deletions(-) [+]
line wrap: on
line diff
--- a/tools/clang/include/clang/Basic/TokenKinds.def	Tue Dec 17 21:59:45 2013 +0900
+++ b/tools/clang/include/clang/Basic/TokenKinds.def	Thu Dec 19 00:11:02 2013 +0900
@@ -270,6 +270,7 @@
 #ifndef noCbC
 KEYWORD(__code                      , KEYALL)
 KEYWORD(__return                    , KEYALL)
+KEYWORD(__environment                    , KEYALL)
 #endif
 
 // C++ 2.11p1: Keywords.
--- a/tools/clang/include/clang/Parse/Parser.h	Tue Dec 17 21:59:45 2013 +0900
+++ b/tools/clang/include/clang/Parse/Parser.h	Thu Dec 19 00:11:02 2013 +0900
@@ -1502,6 +1502,10 @@
   /// A SmallVector of types.
   typedef SmallVector<ParsedType, 12> TypeVector;
 
+#ifndef noCbC // for CbC
+  StmtVector* Stmtsp; // for CbC.
+#endif
+
   StmtResult ParseStatement(SourceLocation *TrailingElseLoc = 0);
   StmtResult ParseStatementOrDeclaration(StmtVector &Stmts,
                                          bool OnlyStatement,
@@ -1533,6 +1537,8 @@
   StmtResult ParseGotoStatement();
 #ifndef noCbC
   StmtResult ParseCbCGotoStatement(ParsedAttributesWithRange &Attrs,StmtVector &Stmts);
+  StmtResult Create__returnStmt();
+  StmtResult CreateReturnAssignmentStmt();
 #endif
   StmtResult ParseContinueStatement();
   StmtResult ParseBreakStatement();
@@ -1668,13 +1674,7 @@
                 const ParsedTemplateInfo &TemplateInfo = ParsedTemplateInfo(),
                                   AccessSpecifier AS = AS_none,
                                   DeclSpecContext DSC = DSC_normal,
-                                  LateParsedAttrList *LateAttrs = 0
-#ifndef noCbC
-				  ,bool *isCbC_envKw = 0,
-				  unsigned Context = 0,
-				  DeclGroupPtrTy* retDecl = 0
-#endif
-				  );
+                                  LateParsedAttrList *LateAttrs = 0);
   bool DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
                                              DeclSpecContext DSContext,
                                              LateParsedAttrList *LateAttrs = 0);
--- a/tools/clang/lib/Parse/ParseDecl.cpp	Tue Dec 17 21:59:45 2013 +0900
+++ b/tools/clang/lib/Parse/ParseDecl.cpp	Thu Dec 19 00:11:02 2013 +0900
@@ -1493,16 +1493,7 @@
   ParsingDeclSpec DS(*this);
 
   DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
-#ifndef noCbC
-  bool isCbC_envKw = false;
-  DeclGroupPtrTy retDecl;
-  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext, 0, &isCbC_envKw, Context, &retDecl);
-  if(isCbC_envKw){
-    return retDecl;
-  }
-#else
   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
-#endif
 
   // If we had a free-standing type definition with a missing semicolon, we
   // may get this far before the problem becomes obvious.
@@ -2567,13 +2558,7 @@
                                         const ParsedTemplateInfo &TemplateInfo,
                                         AccessSpecifier AS,
                                         DeclSpecContext DSContext,
-                                        LateParsedAttrList *LateAttrs
-#ifndef noCbC
-				        ,bool *isCbC_envKw,
-				        unsigned Context,
-					DeclGroupPtrTy* retDecl
-#endif
-					) {
+                                        LateParsedAttrList *LateAttrs) {
   if (DS.getSourceRange().isInvalid()) {
     DS.SetRangeStart(Tok.getLocation());
     DS.SetRangeEnd(Tok.getLocation());
@@ -3149,93 +3134,6 @@
       isInvalid = (DS.SetTypeSpecType(DeclSpec::TST___code, Loc, PrevSpec, DiagID) || isInvalid__CbC_env);
       break;
     }
-    case tok::kw___return: {
-      *isCbC_envKw = true;
-      ParsingDeclSpec PDS(*this);
-      DeclSpec &__retDS = PDS;
-      if (__retDS.getSourceRange().isInvalid()) {
-	__retDS.SetRangeStart(Tok.getLocation());
-	__retDS.SetRangeEnd(Tok.getLocation());
-      }
-      // ^ for init ParsingDeclSpec
-
-      isInvalid = __retDS.SetTypeSpecType(DeclSpec::TST___code, Loc, PrevSpec, DiagID);
-      if (isInvalid) {
-	assert(PrevSpec && "Method did not return previous specifier!");
-	assert(DiagID);
-
-	if (DiagID == diag::ext_duplicate_declspec)
-	  Diag(Tok, DiagID)
-	    << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
-	else
-	  Diag(Tok, DiagID) << PrevSpec;
-      }
-      __retDS.SetRangeEnd(Tok.getLocation());
-      __retDS.Finish(Diags, PP);
-      if(getCurScope()->isClassScope()){ //in the struct or the union.
-	
-      } else {
-	ParsingDeclarator D(*this, PDS, static_cast<Declarator::TheContext>(Context));
-	//	ParsedAttributes attrs(AttrFactory);
-	//	SourceLocation EllipsisLoc = D.getEllipsisLoc();
-	D.setEllipsisLoc(SourceLocation());
-	bool hadGroupingParens = D.hasGroupingParens();
-	D.setGroupingParens(true);
-	IdentifierInfo* II = CreateIdentifierInfo("__CbC_return",12, Loc); // CreateIdentifierInfo(name,length of the name, SourceLocation);
-	D.SetRangeEnd(Loc);
-	DeclSpec DS_(AttrFactory);
-	// ->ParseTypeQualifierListOpt                                                                                          
-	DS_.Finish(Diags, PP);
-	D.SetIdentifier(II, Loc);	// ParseDeclaratorInternal<-                                                                                            
-	D.AddTypeInfo(DeclaratorChunk::getPointer(DS_.getTypeQualifiers(), Loc,DS_.getConstSpecLoc(),DS_.getVolatileSpecLoc(),
-						  DS_.getRestrictSpecLoc()),DS_.getAttributes(),SourceLocation());
-	D.setGroupingParens(hadGroupingParens);
-	// ParseDirectDeclarator<-                                                                                              
-
-	ParseScope PrototypeScope(this,Scope::FunctionPrototypeScope|Scope::DeclScope|
-				  (D.isFunctionDeclaratorAFunctionDeclaration() ? Scope::FunctionDeclarationScope : 0));
-	bool HasProto = false;
-	SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
-	SourceLocation EllipsisLoc;
-	DeclSpec DS__(AttrFactory);
-	bool RefQualifierIsLValueRef = true;
-	SourceLocation RefQualifierLoc;
-	SourceLocation ConstQualifierLoc;
-	SourceLocation VolatileQualifierLoc;
-	ExceptionSpecificationType ESpecType = EST_None;
-	SourceRange ESpecRange;
-	SmallVector<ParsedType, 2> DynamicExceptions;
-	SmallVector<SourceRange, 2> DynamicExceptionRanges;
-	ExprResult NoexceptExpr;
-	ParsedAttributes FnAttrs(AttrFactory);
-	TypeResult TrailingReturnType;
-	Actions.ActOnStartFunctionDeclarator();
-	Actions.ActOnEndFunctionDeclarator();
-	D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,false,Loc,ParamInfo.data(),
-						   ParamInfo.size(),EllipsisLoc, Loc,DS__.getTypeQualifiers(),
-						   RefQualifierIsLValueRef,RefQualifierLoc, ConstQualifierLoc,
-						   VolatileQualifierLoc,SourceLocation(),ESpecType, ESpecRange.getBegin(),
-						   DynamicExceptions.data(),DynamicExceptionRanges.data(),
-						   DynamicExceptions.size(),
-						   NoexceptExpr.isUsable() ? NoexceptExpr.get() : 0,Loc, Loc, D,TrailingReturnType),
-		      FnAttrs, Loc);
-	PrototypeScope.Exit();
-	// ParseDeclGroup<-ParseDeclarator<-ParseDeclaratorInternal<-ParseDirectDeclarator<-                                                            
-
-	SmallVector<Decl *, 8> DeclsInGroup;
-	Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
-	D.complete(FirstDecl);
-	DeclsInGroup.push_back(FirstDecl);
-
-	*retDecl = Actions.FinalizeDeclaratorGroup(getCurScope(), DS_, DeclsInGroup);
-	// ParseSimpleDeclaration<-                                                                                                                     
-	ConsumeToken();// eat __return
-	ConsumeToken();// eat semi
-
-	return;
-      }
-      break;
-    }
 #endif
     case tok::kw_char:
       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
@@ -4188,7 +4086,6 @@
   case tok::kw_void:
 #ifndef noCbC
   case tok::kw___code:
-  case tok::kw___return:
 #endif
   case tok::kw_char:
   case tok::kw_wchar_t:
@@ -4432,7 +4329,6 @@
   case tok::kw_void:
 #ifndef noCbC
   case tok::kw___code:
-  case tok::kw___return:
 #endif
   case tok::kw_char:
   case tok::kw_wchar_t:
@@ -5994,7 +5890,6 @@
   case tok::kw___pixel:
 #ifndef noCbC
   case tok::kw___code:
-  case tok::kw___return:
 #endif
     Tok.setKind(tok::kw___vector);
     return true;
@@ -6030,7 +5925,6 @@
     case tok::kw___pixel:
 #ifndef noCbC
   case tok::kw___code:
-  case tok::kw___return:
 #endif
       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
       return true;
@@ -6159,15 +6053,7 @@
   IdentifierInfo *II;
   II = PP.getIdentifierInfo(StringRef(TokenForII.getRawIdentifierData(),TokenForII.getLength()));
   TokenForII.setIdentifierInfo(II);
-  /*
-  if(Tok.getKind() == tok::kw___return){
-    TokenForII.setKind(tok::identifier);
-    //    II->setTokenID(tok::identifier);
-  }
-  else
-  */
-    TokenForII.setKind(II->getTokenID());
-  
+  TokenForII.setKind(II->getTokenID());
   return II;
 }
 
--- a/tools/clang/lib/Parse/ParseExpr.cpp	Tue Dec 17 21:59:45 2013 +0900
+++ b/tools/clang/lib/Parse/ParseExpr.cpp	Thu Dec 19 00:11:02 2013 +0900
@@ -2420,7 +2420,30 @@
     if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
       Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
       Expr = ParseBraceInitializer();
-    } else
+    } 
+#ifndef noCbC
+    else if (Tok.is(tok::kw___return)){
+      StmtResult __returnDeclaration,assginmentStmt;
+      __returnDeclaration = Create__returnStmt();
+      if (__returnDeclaration.isUsable())
+      	Stmtsp->push_back(__returnDeclaration.release());
+      assginmentStmt = CreateReturnAssignmentStmt();
+      if (assginmentStmt.isUsable())
+      	Stmtsp->push_back(assginmentStmt.release());
+      
+      IdentifierInfo &II = *CreateIdentifierInfo("__CbC_return", 12, Tok.getLocation()); // (name, length of the name, location of the name)
+      SourceLocation ILoc = ConsumeToken();
+      UnqualifiedId Name;
+      Name.setIdentifier(&II, ILoc);
+      CXXScopeSpec ScopeSpec;
+      SourceLocation TemplateKWLoc;
+
+      CastExpressionIdValidator Validator(false, true); // (isTypeCast != NotTypeCast, isTypeCast != IsTypeCast)
+      Expr = ParseRHSOfBinaryExpression(ParsePostfixExpressionSuffix(Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc, Name, Tok.is(tok::l_paren), /* isAddressOfOperand = */false, &Validator)),prec::Assignment);
+      
+    }
+#endif
+    else
       Expr = ParseAssignmentExpression();
 
     if (Tok.is(tok::ellipsis))
--- a/tools/clang/lib/Parse/ParseStmt.cpp	Tue Dec 17 21:59:45 2013 +0900
+++ b/tools/clang/lib/Parse/ParseStmt.cpp	Thu Dec 19 00:11:02 2013 +0900
@@ -35,6 +35,11 @@
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/ADT/SmallString.h"
+
+#ifndef noCbC
+#include "clang/Sema/Lookup.h"
+#endif
+
 using namespace clang;
 
 //===----------------------------------------------------------------------===//
@@ -1718,6 +1723,7 @@
       return StmtError();
     }
 
+    Stmtsp = &Stmts;
     gotoRes = ParseExprStatement();
 
     // don't need return because this code segment caller isn't code segment.
@@ -2791,3 +2797,123 @@
   }
   Braces.consumeClose();
 }
+
+#ifndef noCbC
+StmtResult Parser::Create__returnStmt(){
+  SourceLocation Loc = Tok.getLocation();
+  unsigned Context = Declarator::BlockContext;
+  ParsedAttributesWithRange __retAttrs(AttrFactory);
+  SourceLocation DeclStart = Loc, DeclEnd = Loc;
+  ParsingDeclSpec PDS(*this);
+  DeclSpec &__retDS = PDS;
+  const char *PrevSpec = 0;
+  unsigned DiagID = 0;
+  bool isInvalid = false;
+  if (__retDS.getSourceRange().isInvalid()) {
+    __retDS.SetRangeStart(Loc);
+    __retDS.SetRangeEnd(Loc);
+  }
+  isInvalid = __retDS.SetTypeSpecType(DeclSpec::TST___code, Loc, PrevSpec, DiagID);
+  if (isInvalid) {
+    assert(PrevSpec && "Method did not return previous specifier!");
+    assert(DiagID);
+    if (DiagID == diag::ext_duplicate_declspec)
+      Diag(Tok, DiagID)
+	<< PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
+    else
+      Diag(Tok, DiagID) << PrevSpec;
+  }
+  __retDS.SetRangeEnd(Loc);
+  __retDS.Finish(Diags, PP);
+  ParsingDeclarator D(*this, PDS, static_cast<Declarator::TheContext>(Context));
+  D.setEllipsisLoc(SourceLocation());
+  bool hadGroupingParens = D.hasGroupingParens();
+  D.setGroupingParens(true);
+  IdentifierInfo* II = CreateIdentifierInfo("__CbC_return",12, Loc); // CreateIdentifierInfo(name,length of the name, SourceLocation);
+  D.SetRangeEnd(Loc);
+  DeclSpec DS_(AttrFactory);
+
+  DS_.Finish(Diags, PP);
+  D.SetIdentifier(II, Loc);
+  
+  D.AddTypeInfo(DeclaratorChunk::getPointer(DS_.getTypeQualifiers(), Loc,DS_.getConstSpecLoc(),DS_.getVolatileSpecLoc(),
+					    DS_.getRestrictSpecLoc()),DS_.getAttributes(),SourceLocation());
+  D.setGroupingParens(hadGroupingParens);
+
+  
+  ParseScope PrototypeScope(this,Scope::FunctionPrototypeScope|Scope::DeclScope|
+  			    (D.isFunctionDeclaratorAFunctionDeclaration() ? Scope::FunctionDeclarationScope : 0));
+  bool HasProto = false;
+  SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
+  SourceLocation EllipsisLoc, RefQualifierLoc, ConstQualifierLoc, VolatileQualifierLoc;
+  DeclSpec DS__(AttrFactory);
+  bool RefQualifierIsLValueRef = true;
+  ExceptionSpecificationType ESpecType = EST_None;
+  SourceRange ESpecRange;
+  SmallVector<ParsedType, 2> DynamicExceptions;
+  SmallVector<SourceRange, 2> DynamicExceptionRanges;
+  ExprResult NoexceptExpr;
+  ParsedAttributes FnAttrs(AttrFactory);
+  TypeResult TrailingReturnType;
+  D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,false,Loc,ParamInfo.data(),
+					     ParamInfo.size(),EllipsisLoc, Loc,DS__.getTypeQualifiers(),
+					     RefQualifierIsLValueRef,RefQualifierLoc, ConstQualifierLoc,
+					     VolatileQualifierLoc,SourceLocation(),ESpecType, ESpecRange.getBegin(),
+					     DynamicExceptions.data(),DynamicExceptionRanges.data(),
+					     DynamicExceptions.size(),
+					     NoexceptExpr.isUsable() ? NoexceptExpr.get() : 0,Loc, Loc, D,TrailingReturnType),
+		FnAttrs, Loc);
+  PrototypeScope.Exit();
+  
+  SmallVector<Decl *, 8> DeclsInGroup;
+  Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
+  D.complete(FirstDecl);
+  DeclsInGroup.push_back(FirstDecl);
+  
+  DeclGroupPtrTy __retDecl = Actions.FinalizeDeclaratorGroup(getCurScope(), DS_, DeclsInGroup);
+  return Actions.ActOnDeclStmt(__retDecl, DeclStart, DeclEnd);
+}
+
+StmtResult Parser::CreateReturnAssignmentStmt(){
+  ExprResult Res,LHS,RHS;
+  Token LHSToken,Next;
+  IdentifierInfo *II;
+  SourceLocation Loc = Tok.getLocation();
+  CXXScopeSpec SS;
+  II = CreateIdentifierInfo("__CbC_return", 12, Loc);
+  Next.startToken();
+  Next.setLocation(Loc);
+  Next.setKind(tok::equal);
+  LHSToken.startToken();
+  LHSToken.setLocation(Loc);
+  LHSToken.setLength(II->getLength());
+  LHSToken.setIdentifierInfo(II);
+  LHSToken.setKind(tok::identifier);
+
+  StatementFilterCCC Validator(Next);
+  Sema::NameClassification Classification = Actions.ClassifyName(getCurScope(), SS, II, Loc, Next, false, SS.isEmpty() ? &Validator : 0);
+
+  LHSToken.setKind(tok::annot_primary_expr);
+  setExprAnnotation(LHSToken, Classification.getExpression());
+  LHSToken.setAnnotationEndLoc(Loc);
+  PP.AnnotateCachedTokens(LHSToken);
+  LHS = getExprAnnotation(LHSToken);
+  IdentifierInfo &return1II = *CreateIdentifierInfo("return1", 7, Loc);
+  UnqualifiedId Name;
+  Name.setIdentifier(&return1II, Loc);
+  
+  DeclarationNameInfo NameInfo;
+  const TemplateArgumentListInfo *TemplateArgs;
+  TemplateArgumentListInfo TemplateArgsBuffer;
+  Actions.DecomposeUnqualifiedId(Name, TemplateArgsBuffer, NameInfo, TemplateArgs);
+  DeclarationName DName = NameInfo.getName();
+  IdentifierInfo *IIFromName = DName.getAsIdentifierInfo();
+  LookupResult LR(Actions, NameInfo, Actions.LookupOrdinaryName);
+  bool IvarLookupFollowUp = IIFromName && !SS.isSet() && Actions.getCurMethodDecl();
+  Actions.LookupParsedName(LR, getCurScope(), &SS, !IvarLookupFollowUp);
+  RHS = Actions.BuildDeclarationNameExpr(SS, LR, /* ADL = */ false);
+  
+  Res = Actions.CreateBuiltinBinOp(Loc, BO_Assign, LHS.take(), RHS.take());
+  return Actions.ActOnExprStmt(Res);
+}
+#endif
--- a/tools/clang/lib/Parse/Parser.cpp	Tue Dec 17 21:59:45 2013 +0900
+++ b/tools/clang/lib/Parse/Parser.cpp	Thu Dec 19 00:11:02 2013 +0900
@@ -887,16 +887,7 @@
                                        ParsingDeclSpec &DS,
                                        AccessSpecifier AS) {
   // Parse the common declaration-specifiers piece.
-#ifndef noCbC
-  bool isCbC_envKw = false;
-  DeclGroupPtrTy retDecl;
-  ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSC_top_level, 0, &isCbC_envKw, Declarator::FileContext, &retDecl);
-  if(isCbC_envKw){
-    return retDecl;
-  }
-#else
   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
-#endif
 
   // If we had a free-standing type definition with a missing semicolon, we
   // may get this far before the problem becomes obvious.