[cfe-dev] Lexical vs Semantic DeclContext

John McCall rjmccall at apple.com
Tue Jun 26 09:46:16 PDT 2012

On Jun 26, 2012, at 9:19 AM, Vassil Vassilev wrote:
>   Clang internals manual says:
>   "Each declaration has two potentially different declaration contexts: a lexical context, which corresponds to the source-centric view of the declaration context, and a semantic context, which corresponds to the semantics-centric view."
>   When debugging the code snippet:
>   enum E {e1 = -12}
>   In the debugger I see:
> #1  0x00007ffff60cd4cb in clang::Sema::ActOnEnumConstant (this=0x44e6b0, S=0x4099c0, theEnumDecl=0x4e5a70, lastEnumConst=0x0, IdLoc=..., Id=0x46a240, Attr=0x0, EqualLoc=..., 
>     Val=0x4e5b40) at /home/vvassilev/workspace/llvm/src/tools/clang/lib/Sema/SemaDecl.cpp:10229
> 10229        PushOnScopeChains(New, S);
> (gdb) p New
> $99 = (clang::EnumConstantDecl *) 0x4e5b60
> (gdb) p New->getDeclContext ()
> $100 = (clang::DeclContext *) 0x4e5aa8
> (gdb) p New->getLexicalDeclContext ()
> $101 = (clang::DeclContext *) 0x4e5aa8
>  I would expect the lexical and the semantic DeclContext to be different since "E" is transparent context. Could somebody explain why is that? Is that expected?

This is expected:  the enum constant is still semantically a member of the enum decl, even though its name is visible to lookup in the surrounding context.  Lexical DCs are designed more for declarations that clearly don't semantically belong to the context they appear in — for example, an out-of-line definition of a member function in C++.

A C/C++ local extern declaration is another example of this, although, unfortunately, we don't actually currently model them that way.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120626/351dabf4/attachment.html>

More information about the cfe-dev mailing list