changeset 62:fe2728d9537d

add comments
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Sun, 02 Feb 2014 20:03:31 +0900
parents 7f1f0f7e0569
children 0d1cf6cb7029
files tools/clang/include/clang/Parse/Parser.h tools/clang/lib/Parse/ParseCbC.cpp
diffstat 2 files changed, 104 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/tools/clang/include/clang/Parse/Parser.h	Sun Feb 02 17:54:49 2014 +0900
+++ b/tools/clang/include/clang/Parse/Parser.h	Sun Feb 02 20:03:31 2014 +0900
@@ -1703,13 +1703,13 @@
   void ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Callback);
 
 #ifndef noCbC
-  bool CreateRetFunction();
+  bool CreateRetCS();
   void Create__CbC_envStruct(SourceLocation Loc, AccessSpecifier AS);
   IdentifierInfo* CreateIdentifierInfo(const char* Name, SourceLocation Loc);
   Decl* Create__CbC_envBody(Decl* TagDecl, DeclSpec::TST T, SourceLocation Loc, const char* Name);
-  ExprResult LookupAndDeclareName(IdentifierInfo *II = 0, bool IsAddressOfOperand = false);
+  ExprResult LookupNameAndBuildExpr(IdentifierInfo *II = 0, bool IsAddressOfOperand = false);
   ExprResult LookupMemberAndBuildExpr(IdentifierInfo *II, Expr* Base, bool IsArrow);
-  StmtResult CreateSjForContinuationWithEnv();
+  StmtResult CreateSjForContinuationWithTheEnv();
   StmtResult CreateAssignmentStmt(IdentifierInfo* LHSII = 0, IdentifierInfo* RHSII = 0, bool LHSisMemberAccess = false,
 				  bool RHShasAmp = false, IdentifierInfo* extraLHSII = 0, IdentifierInfo* extraRHSII = 0);
   StmtResult CreateDeclStmt(IdentifierInfo *II = 0, bool isRetCS = false, bool copyType = false, DeclSpec::TST valueType = DeclSpec::TST_int, IdentifierInfo* Name = 0);
--- a/tools/clang/lib/Parse/ParseCbC.cpp	Sun Feb 02 17:54:49 2014 +0900
+++ b/tools/clang/lib/Parse/ParseCbC.cpp	Sun Feb 02 20:03:31 2014 +0900
@@ -19,6 +19,9 @@
 
 using namespace clang;
 
+/// The class which belong to this namespace is from other files' namespace.
+/// Because those namespaces are unnamed namespaces, we can't access them.
+/// So create this namespace and copy classes from those namespaces.
 namespace ExternalSpace { // from ParseExpr.cpp , ParseStmt.cpp
 class CastExpressionIdValidator : public CorrectionCandidateCallback {
  public:
@@ -73,6 +76,21 @@
 }
 
 
+/// Prepare__retForGotoWithTheEnvExpr - Prepare __CbC_return, code segment for returning and some necessary statements.
+/// It is called when the parser find __return and statements are put into complex statement.
+/// 
+/// examples which are created:
+///   complex statement:
+///         ({
+///           __code (*__CbC_return)(return_type, void*);
+///           __CbC_return = code_segment_for_return;
+///           __CbC_return;
+///         });
+///   code segment:
+///         __code ret(return_type retval, void *env){
+///           *(return_type)((struct CbC_environment *)(env))->ret_p = n;
+///             longjmp((int*)(((struct __CbC_environment *)env)->env),1);
+///         }
 ExprResult Parser::Prepare__retForGotoWithTheEnvExpr(){
   StmtResult innerRes;
   SourceLocation Loc = Tok.getLocation();
@@ -87,7 +105,7 @@
   StmtVector CompoundStmts; 
 
   // create code segment for return to C's function
-  if (CreateRetFunction()) { // error check : function type is void or not.
+  if (CreateRetCS()) { // error check : function type is void or not.
     Diag(Tok, diag::err_cannot_use_goto_with_env);
     return ExprError();
   }
@@ -112,6 +130,26 @@
   return Actions.ActOnStmtExpr(Loc, CompoundStmtRes.take(), Loc);
 }
 
+/// Prepare__envForGotoWithTheEnvExpr - Prepare __CbC_environment, struct __CbC_env and some necessary statements.
+/// It is called when the parser find __environment and statements are put into complex statement.
+/// 
+/// examples which are created:
+///   complex statement:
+///         ({
+///           struct __CbC_env __CbC_environment;
+///           jmp_buf env_buf;
+///           return_type retval;
+///           __CbC_environment.ret_p = &retval;
+///           __CbC_environment.env = &env_buf
+///           if (setjmp(__CbC_environment.env)){
+///             return retval;
+///           }
+///           &__CbC_environment;
+///         });
+///   struct __CbC_env:
+///         struct __CbC_env{
+///           void *ret_p,*env;
+///         }
 ExprResult Parser::Prepare__envForGotoWithTheEnvExpr(){
   StmtResult innerRes;
   SourceLocation Loc = Tok.getLocation();
@@ -153,7 +191,7 @@
     CompoundStmts.push_back(innerRes.release());
 
   // create statements of setjmp
-  innerRes = CreateSjForContinuationWithEnv();
+  innerRes = CreateSjForContinuationWithTheEnv();
   if (innerRes.isUsable())
     CompoundStmts.push_back(innerRes.release());
   
@@ -168,7 +206,7 @@
 }
 
 /// CreateAssignmentStmt - Create assignment statement such as "aaa = bbb;", "auaua = llll;", etc.
-/// It can create  kinds of statement.
+/// It can create 4 kinds of statement.
 /// 1. common assignment statement:
 ///         variable '=' variable ';'
 /// 2. LHS variable is member of struct:
@@ -201,7 +239,7 @@
   if (LHSisMemberAccess) 
     LHS = LookupMemberAndBuildExpr(extraLHSII, LHS.take(), false);
   
-  RHS = LookupAndDeclareName(RHSII);
+  RHS = LookupNameAndBuildExpr(RHSII);
   if (RHShasAmp)
     RHS = Actions.ActOnUnaryOp(getCurScope(), Loc, tok::amp, RHS.get());
 
@@ -210,6 +248,9 @@
   return Actions.ActOnExprStmt(Expr);
 }
 
+/// CreateDeclStmt - Create declaration statement such as "int aaa;".
+/// If isRetCS is true, create code segment for return to C's function. And Name is name of code segment.
+/// If copyType is true, type of variable is copied from callee.
 StmtResult Parser::CreateDeclStmt(IdentifierInfo *II, bool isRetCS, bool copyType, DeclSpec::TST valueType, IdentifierInfo* Name){
   SourceLocation Loc = Tok.getLocation();
   DeclGroupPtrTy DeclGPT;
@@ -288,17 +329,16 @@
   return Actions.ActOnDeclStmt(DeclGPT, Loc, Loc);
 }
 
-/*
- * This function imitated Parser::ParseDeclarationAfterDeclaratorAndAttributes() and Sema::ActOnDeclarator().
- * The origins get Type from Declarator but this function get Type from current function.
- * It is useful for CbC to create statements for the continuation with the environments.
- */
+
+/// handleDeclAndChangeDeclType - This function imitated Parser::ParseDeclarationAfterDeclaratorAndAttributes() and Sema::ActOnDeclarator().
+/// The origins get Type from Declarator but this function get Type from current function.
+/// It is useful for CbC to create statements for the continuation with the environments.
 Decl* Parser::HandleDeclAndChangeDeclType(Declarator &D) {
   D.setFunctionDefinitionKind(FDK_Declaration);
   DeclarationNameInfo NameInfo = Actions.GetNameForDeclarator(D);
   DeclContext *DC = Actions.CurContext;
-  QualType R = Actions.getCurFunctionDecl()->getResultType();
-  TypeSourceInfo *TInfo = Actions.Context.CreateTypeSourceInfo(R);
+  QualType R = Actions.getCurFunctionDecl()->getResultType(); // copy a  type
+  TypeSourceInfo *TInfo = Actions.Context.CreateTypeSourceInfo(R); // copy a type infomation
   Scope *S = getCurScope();
   LookupResult Previous(Actions, NameInfo, Actions.LookupOrdinaryName, Actions.ForRedeclaration);
   bool IsLinkageLookup = false;
@@ -352,18 +392,24 @@
   return New;
 }
 
-StmtResult Parser::CreateSjForContinuationWithEnv(){
+
+/// CreateSjForContinuationWithEnv - Create statements of setjmp for continuation with the environment.
+///   code example:
+///         if (setjmp(__CbC_environment.env)){
+///           return retval;
+///         }
+StmtResult Parser::CreateSjForContinuationWithTheEnv(){
   SourceLocation Loc = Tok.getLocation();
   StmtResult IfRes;
   ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, true/* C99 or CXX */);
   ExprResult CondExp;
   Decl *CondVar = 0;
 
-  CondExp = LookupAndDeclareName(CreateIdentifierInfo("setjmp", Loc));
+  CondExp = LookupNameAndBuildExpr(CreateIdentifierInfo("setjmp", Loc));
   ExprVector ArgExprs;
   ExprResult __envExprRes = CondExp.get();
 
-  __envExprRes = LookupAndDeclareName(CreateIdentifierInfo(__CBC_ENVIRONMENT_NAME, Loc));
+  __envExprRes = LookupNameAndBuildExpr(CreateIdentifierInfo(__CBC_ENVIRONMENT_NAME, Loc));
   __envExprRes = LookupMemberAndBuildExpr(CreateIdentifierInfo(__CBC_STRUCT_ENV_NAME, Loc), __envExprRes.take(), false);
 
   ArgExprs.push_back(__envExprRes.release());
@@ -380,7 +426,7 @@
   StmtVector innerStmts;
   StmtResult innerStmtRes;
   ExprResult innerExprRes;
-  innerExprRes = LookupAndDeclareName(CreateUniqueIdentifierInfo(__CBC_RETVAL_NAME, Loc));
+  innerExprRes = LookupNameAndBuildExpr(CreateUniqueIdentifierInfo(__CBC_RETVAL_NAME, Loc));
   innerStmtRes = Actions.ActOnReturnStmt(Loc, innerExprRes.take());
   if (innerStmtRes.isUsable())
     innerStmts.push_back(innerStmtRes.release());
@@ -393,7 +439,9 @@
   return IfRes;
 }
 
-ExprResult Parser::LookupAndDeclareName(IdentifierInfo *II, bool IsAddressOfOperand){
+
+/// LookupNameAndBuildExpr - Look up name, create ExprResult and return it.
+ExprResult Parser::LookupNameAndBuildExpr(IdentifierInfo *II, bool IsAddressOfOperand){
   SourceLocation Loc = Tok.getLocation();
   UnqualifiedId Name;
   CXXScopeSpec SS;
@@ -403,6 +451,8 @@
   return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name, false, IsAddressOfOperand, &Validator);
 }
 
+/// LookupNameAndBuildExpr - Look up member name, create ExprResult and return it.
+/// If IsArrow is true, the name is accessed by arrow operand.
 ExprResult Parser::LookupMemberAndBuildExpr(IdentifierInfo *II, Expr* Base, bool IsArrow){
   SourceLocation Loc = Tok.getLocation();
   ExprResult Res;
@@ -428,6 +478,10 @@
 
 /// Create__CbC_envStruct - This method create "struct __CbC_env" which is used to continuation with environment.
 /// If the __CbC_env has been already defined, it doesn't create __CbC_env again.
+///   The example of struct which is created :
+///        struct __CbC_env{
+///          void *ret_p,*env;
+///        };
 void Parser::Create__CbC_envStruct(SourceLocation Loc, AccessSpecifier AS) {
 
   IdentifierInfo *Name = CreateIdentifierInfo(__CBC_STRUCT_NAME, Loc);
@@ -492,6 +546,7 @@
   Actions.FunctionScopes.push_back(SavedFSI);
 }
 
+/// Create__CbC_envBody - Create void type pointer ret_p and env which are member of __CbC_env.
 Decl* Parser::Create__CbC_envBody(Decl* TagDecl, DeclSpec::TST T, SourceLocation Loc, const char* Name){
   ParsingDeclSpec PDS(*this);
   setTST(&PDS, T);
@@ -513,6 +568,9 @@
   return Field;
 }
 
+/// CreateIdentifierInfo - Create IdentifierInfo from char pointer.
+///   usage : 
+///          IdentifierInfo *II = CreateIdentifierInfo(IIName, Location);
 IdentifierInfo* Parser::CreateIdentifierInfo(const char* Name, SourceLocation Loc) {
   int length = strlen(Name);
   Token TokenForII;
@@ -528,6 +586,10 @@
   return II;
 }
 
+/// CreateUniqueIdentifierInfo - Create unique IdentifierInfo.
+/// IdentifierInfos have unique name which were created by this function.
+/// Naming conventions is current function's name '..' variable name
+/// For example, if current function's name is 'main' and variable name is 'auaua', IdentifierInfo's name is 'main..auaua'.
 IdentifierInfo* Parser::CreateUniqueIdentifierInfo(const char* Name, SourceLocation Loc){
   IdentifierInfo *II;
   std::ostringstream os;
@@ -536,7 +598,14 @@
   return II;
 }
 
-bool Parser::CreateRetFunction(){
+
+/// CreateRetCS - Create code segment which is used for continuation with the environment.
+///   create these codes:
+///         __code ret(return_type retval, void *env){
+///           *(return_type)((struct CbC_environment *)(env))->ret_p = n;
+///             longjmp((int*)(((struct __CbC_environment *)env)->env),1);
+///         }
+bool Parser::CreateRetCS(){
   FunctionDecl *CurFunctionDecl = Actions.getCurFunctionDecl();
   QualType CurFuncResQT = CurFunctionDecl->getResultType();
   if (CurFuncResQT.getTypePtr()->isVoidType()) // this function cannot use continuation with the environment.
@@ -622,7 +691,7 @@
 		       starDS.getAttributes(),
 		       SourceLocation());
   ExprVector ArgExprs2;
-  LHS = LookupAndDeclareName(envII);
+  LHS = LookupNameAndBuildExpr(envII);
   ArgExprs2.push_back(LHS.release());
   LHS = Actions.ActOnParenListExpr(Loc, Loc, ArgExprs2);
   Expr *envCastExpr = LHS.take();
@@ -640,7 +709,7 @@
   LHS = Actions.BuildCStyleCastExpr(Loc, CurFuncTypesPointerTI, Loc, ret_pCastExpr);
   LHS = Actions.ActOnUnaryOp(getCurScope(), Loc, tok::star, LHS.get());
   ExprResult RHS;
-  RHS = LookupAndDeclareName(retvalII);
+  RHS = LookupNameAndBuildExpr(retvalII);
 			    
   retvalAssginmentExpr = Actions.ActOnBinOp(getCurScope(), Loc, tok::equal, LHS.take(), RHS.take());
   innerR = Actions.ActOnExprStmt(retvalAssginmentExpr);
@@ -665,7 +734,7 @@
 					      ljDS.getRestrictSpecLoc()),
 		  ljDS.getAttributes(),
 		  SourceLocation());
-  ljLHS = LookupAndDeclareName(envII);
+  ljLHS = LookupNameAndBuildExpr(envII);
   Expr *ljCastExpr = ljLHS.take();
   TypeSourceInfo *ljCastTInfo = Actions.GetTypeForDeclaratorCast(ljD, ljCastExpr->getType());
   ljLHS = Actions.BuildCStyleCastExpr(Loc, ljCastTInfo, Loc, ljCastExpr);
@@ -691,9 +760,8 @@
   return false;
 }
 
-/* Create ExprResult from IdentifierInfo. 
- * It is used when II is a not primary expression such as not primary types, a function's name, etc.
- */
+/// IIToExpr - Create ExprResult from IdentifierInfo. 
+/// It is used when II is a not primary expression such as not primary types, a function's name, etc.
 ExprResult Parser::IIToExpr(IdentifierInfo *II, tok::TokenKind Kind){
   SourceLocation Loc = Tok.getLocation();
   Token Next,IITok;
@@ -711,10 +779,15 @@
   return getExprAnnotation(IITok);
 }
 
+/// CreateComplexStmtRet - Create return value for complex statements.
+///
+///   ({ /* some statements */
+///      return_value; )};
+///     ^^^^^^^^^^^^^ Create it.
 StmtResult Parser::CreateComplexStmtRet(IdentifierInfo *II, bool IsAddressOfOperand){
   ExprResult ER;
   if (IsAddressOfOperand) {
-    ER = LookupAndDeclareName(II, true);
+    ER = LookupNameAndBuildExpr(II, true);
     ER = Actions.ActOnUnaryOp(getCurScope(), Tok.getLocation(), tok::amp, ER.get());
   }
   else
@@ -722,6 +795,10 @@
   return Actions.ActOnExprStmt(ER);
 }
 
+/// CreateParam - Create paramator for functions.
+/// 
+/// int funcname(int aua) { 
+///              ^^^^^^^ Create it.
 ParmVarDecl* Parser::CreateParam(IdentifierInfo *II, int pointerNum, DeclSpec::TST T){
   SourceLocation Loc = Tok.getLocation();
   DeclSpec DS(AttrFactory);