<div dir="ltr">Thank you, Nikola.<div><br></div><div>I will try what you said.</div><div><br></div><div>Yes, after source-2-source, I can get the DeclStmt, but the reference to the new-variable in the loop body is said have not been declared. </div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 12, 2015 at 12:35 PM, Nikola Smiljanic <span dir="ltr"><<a href="mailto:popizdeh@gmail.com" target="_blank">popizdeh@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Adding back cfe-dev</div><div><br></div>If you're tying to introduce a new variable I suggest you look at what happens with 'i' in 'for (int i;;)'. I don't think your code is adding the VarDecl to the AST, you're building the node but how does it connect to existing nodes? You probably don't want to do all this work manually anyway, I'm not sure if ActOnVariableDeclarator is the right function but it might do most of the work for you. I'm not sure how IdentifierResolver, IdentifierTable and other machinery is supposed to be updated by one of Sema functions likely does it.</div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 12, 2015 at 1:01 PM, Eric Lu <span dir="ltr"><<a href="mailto:eirc.lew@gmail.com" target="_blank">eirc.lew@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Then, how to create the DeclContext chain.<div>If I want to create a new variable declaration in for loop in the AST, how to add the variable into the ForContext? </div><div><br></div><div>Now, I do like below, but it is said the variable not declared.</div><div><br></div><div>StmtResult Sema::ActOnForInitDeclStmt(<span style="background-color:rgb(255,0,0)">IdentifierInfo &II</span>, SourceLocation StartLoc, SourceLocation EndLoc)<br></div><div><div>const char *PrevSpec; // ? Do what?</div><div>  unsigned DiagID;</div><div>  AttributeFactory Attrs;</div><div>  DeclSpec DS(Attrs );</div><div>  DS.SetTypeSpecType(DeclSpec::TST_int, StartLoc, PrevSpec, DiagID,</div><div>      getPrintingPolicy() );</div><div>  <span style="background-color:rgb(255,0,0)">Declarator D(DS, Declarator::ForContext);</span></div><div>  D.SetIdentifier(&II, StartLoc );</div><div><br></div><div>  DeclContext *DC = CurContext; // TBD.</div><div>  Scope *Scp = getScopeForContext(DC);</div><div>  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, Scp);</div><div>  QualType R = TInfo->getType();</div><div>  DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec();</div><div><br></div><div>  VarDecl::StorageClass SC = SC_None;</div><div>  VarDecl *VDecl = VarDecl::Create(Context, DC, D.getLocStart(), D.getIdentifierLoc(),&II, R, TInfo, SC);</div><div>  Decl *Var = VDecl;</div><div><br></div><div>  SmallVector<Decl*, 8> Decls;</div><div>  Decls.push_back(Var);</div><div>  DeclGroupPtrTy DGP = BuildDeclaratorGroup( Decls );</div><div>  DeclGroupRef DGR = DGP.get();</div><div>  DeclStmt *<span style="background-color:rgb(255,0,0)">VDS </span>= new (Context) DeclStmt(DGR, StartLoc,</div><div>     StartLoc);</div><div><br></div><div><br></div></div><div><div><div><br></div><div><br></div><div><br><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, May 9, 2015 at 10:23 AM, Nikola Smiljanic <span dir="ltr"><<a href="mailto:popizdeh@gmail.com" target="_blank">popizdeh@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">By performing a name lookup into appropriate Scope. Scopes are linked in a chain and name lookup traverses them starting from the inner one.</div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Sat, May 9, 2015 at 3:09 AM, Eric Lu <span dir="ltr"><<a href="mailto:eirc.lew@gmail.com" target="_blank">eirc.lew@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div><div dir="ltr">Hi,<div><br></div><div>In a C for loop like:</div><div>int a[10];</div><div>for(int i = 0; i < 10; i++){</div><div>   a[i] = i + 1;</div><div>}</div><div><br></div><div>The variable "i" is declared in the ForContext in ParseForStatement(), and then when parse body, I.E., the expression "a[i]" later, how does it know that "i" is declared?</div><div><br></div><div>In other words, how does the ForContext affect the later parsing of variable "i" ?</div><span><font color="#888888"><div><div><br></div>-- <br><div>Best Regards!<br>Eric Lew</div>
</div></font></span></div>
<br></div></div>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div>Best Regards!<br>Eric Lew</div>
</div></div></div></div></div></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Best Regards!<br>Eric Lew</div>
</div>