[cfe-dev] how does clang tackle DeclContext, like ForContext
Eric Lu
eirc.lew at gmail.com
Mon May 11 22:22:01 PDT 2015
Thank you, Nikola.
I will try what you said.
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.
On Tue, May 12, 2015 at 12:35 PM, Nikola Smiljanic <popizdeh at gmail.com>
wrote:
> Adding back cfe-dev
>
> 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.
>
> On Tue, May 12, 2015 at 1:01 PM, Eric Lu <eirc.lew at gmail.com> wrote:
>
>> Then, how to create the DeclContext chain.
>> If I want to create a new variable declaration in for loop in the AST,
>> how to add the variable into the ForContext?
>>
>> Now, I do like below, but it is said the variable not declared.
>>
>> StmtResult Sema::ActOnForInitDeclStmt(IdentifierInfo &II, SourceLocation
>> StartLoc, SourceLocation EndLoc)
>> const char *PrevSpec; // ? Do what?
>> unsigned DiagID;
>> AttributeFactory Attrs;
>> DeclSpec DS(Attrs );
>> DS.SetTypeSpecType(DeclSpec::TST_int, StartLoc, PrevSpec, DiagID,
>> getPrintingPolicy() );
>> Declarator D(DS, Declarator::ForContext);
>> D.SetIdentifier(&II, StartLoc );
>>
>> DeclContext *DC = CurContext; // TBD.
>> Scope *Scp = getScopeForContext(DC);
>> TypeSourceInfo *TInfo = GetTypeForDeclarator(D, Scp);
>> QualType R = TInfo->getType();
>> DeclSpec::SCS SCSpec = D.getDeclSpec().getStorageClassSpec();
>>
>> VarDecl::StorageClass SC = SC_None;
>> VarDecl *VDecl = VarDecl::Create(Context, DC, D.getLocStart(),
>> D.getIdentifierLoc(),&II, R, TInfo, SC);
>> Decl *Var = VDecl;
>>
>> SmallVector<Decl*, 8> Decls;
>> Decls.push_back(Var);
>> DeclGroupPtrTy DGP = BuildDeclaratorGroup( Decls );
>> DeclGroupRef DGR = DGP.get();
>> DeclStmt *VDS = new (Context) DeclStmt(DGR, StartLoc,
>> StartLoc);
>>
>>
>>
>>
>>
>>
>>
>> On Sat, May 9, 2015 at 10:23 AM, Nikola Smiljanic <popizdeh at gmail.com>
>> wrote:
>>
>>> By performing a name lookup into appropriate Scope. Scopes are linked in
>>> a chain and name lookup traverses them starting from the inner one.
>>>
>>> On Sat, May 9, 2015 at 3:09 AM, Eric Lu <eirc.lew at gmail.com> wrote:
>>>
>>>> Hi,
>>>>
>>>> In a C for loop like:
>>>> int a[10];
>>>> for(int i = 0; i < 10; i++){
>>>> a[i] = i + 1;
>>>> }
>>>>
>>>> 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?
>>>>
>>>> In other words, how does the ForContext affect the later parsing of
>>>> variable "i" ?
>>>>
>>>> --
>>>> Best Regards!
>>>> Eric Lew
>>>>
>>>> _______________________________________________
>>>> cfe-dev mailing list
>>>> cfe-dev at cs.uiuc.edu
>>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>>>
>>>>
>>>
>>
>>
>> --
>> Best Regards!
>> Eric Lew
>>
>
>
--
Best Regards!
Eric Lew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150512/fa48bff7/attachment.html>
More information about the cfe-dev
mailing list