diff --git a/clang/Parse/Parser.cpp b/clang/Parse/Parser.cpp index f57ecb579cd3..82dc17068e9a 100644 --- a/clang/Parse/Parser.cpp +++ b/clang/Parse/Parser.cpp @@ -13,11 +13,20 @@ #include "clang/Parse/Parser.h" #include "clang/Parse/Declarations.h" +#include "clang/Parse/Scope.h" using namespace llvm; using namespace clang; Parser::Parser(Preprocessor &pp, ParserActions &actions) - : PP(pp), Actions(actions), Diags(PP.getDiagnostics()) {} + : PP(pp), Actions(actions), Diags(PP.getDiagnostics()) { + // Create the global scope, install it as the current scope. + CurScope = new Scope(0); +} + +Parser::~Parser() { + delete CurScope; +} + void Parser::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { diff --git a/clang/Parse/Scope.cpp b/clang/Parse/Scope.cpp new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/clang/clang.xcodeproj/project.pbxproj b/clang/clang.xcodeproj/project.pbxproj index b9e8bd0f19b4..e9c5120b020c 100644 --- a/clang/clang.xcodeproj/project.pbxproj +++ b/clang/clang.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ DE06B73E0A8307640050E87E /* LangOptions.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE06B73D0A8307640050E87E /* LangOptions.h */; }; + DE06BEC90A854E390050E87E /* Scope.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE06BEC80A854E390050E87E /* Scope.cpp */; }; + DE06BECB0A854E4B0050E87E /* Scope.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE06BECA0A854E4B0050E87E /* Scope.h */; }; DE1F22030A7D852A00FBF588 /* Parser.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE1F22020A7D852A00FBF588 /* Parser.h */; }; DE1F22200A7D879000FBF588 /* ParserActions.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE1F221F0A7D879000FBF588 /* ParserActions.h */; }; DE1F24700A7DC99000FBF588 /* Actions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE1F246D0A7DC99000FBF588 /* Actions.cpp */; }; @@ -98,6 +100,7 @@ DE1F22200A7D879000FBF588 /* ParserActions.h in CopyFiles */, DE1F24820A7DCD3800FBF588 /* Declarations.h in CopyFiles */, DE06B73E0A8307640050E87E /* LangOptions.h in CopyFiles */, + DE06BECB0A854E4B0050E87E /* Scope.h in CopyFiles */, ); runOnlyForDeploymentPostprocessing = 1; }; @@ -106,6 +109,8 @@ /* Begin PBXFileReference section */ 8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; }; DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = ""; }; + DE06BEC80A854E390050E87E /* Scope.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = Scope.cpp; path = Parse/Scope.cpp; sourceTree = ""; }; + DE06BECA0A854E4B0050E87E /* Scope.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Scope.h; path = clang/Parse/Scope.h; sourceTree = ""; }; DE1F22020A7D852A00FBF588 /* Parser.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Parser.h; path = clang/Parse/Parser.h; sourceTree = ""; }; DE1F221F0A7D879000FBF588 /* ParserActions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = ParserActions.h; path = clang/Parse/ParserActions.h; sourceTree = ""; }; DE1F246D0A7DC99000FBF588 /* Actions.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = Actions.cpp; path = Parse/Actions.cpp; sourceTree = ""; }; @@ -206,6 +211,7 @@ DE1F24810A7DCD3800FBF588 /* Declarations.h */, DE1F22020A7D852A00FBF588 /* Parser.h */, DE1F221F0A7D879000FBF588 /* ParserActions.h */, + DE06BECA0A854E4B0050E87E /* Scope.h */, ); name = Parse; sourceTree = ""; @@ -217,6 +223,7 @@ DE1F257A0A7DD86800FBF588 /* Declarations.cpp */, DE1F246E0A7DC99000FBF588 /* Parse.cpp */, DE1F246F0A7DC99000FBF588 /* ParseDeclarations.cpp */, + DE06BEC80A854E390050E87E /* Scope.cpp */, ); name = Parse; sourceTree = ""; @@ -370,6 +377,7 @@ DE1F24710A7DC99000FBF588 /* Parse.cpp in Sources */, DE1F24720A7DC99000FBF588 /* ParseDeclarations.cpp in Sources */, DE1F257B0A7DD86800FBF588 /* Declarations.cpp in Sources */, + DE06BEC90A854E390050E87E /* Scope.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index edbd8b9359d1..70a860196d1e 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -21,22 +21,24 @@ namespace llvm { namespace clang { class ParserActions; class DeclSpec; + class Scope; /// Parser - This implements a parser for the C family of languages. After /// parsing units of the grammar, productions are invoked to handle whatever has /// been read. /// class Parser { - // SYMBOL TABLE Preprocessor &PP; ParserActions &Actions; Diagnostic &Diags; + Scope *CurScope; /// Tok - The current token we are peeking head. All parsing methods assume /// that this is valid. LexerToken Tok; public: Parser(Preprocessor &PP, ParserActions &Actions); + ~Parser(); const LangOptions &getLang() const { return PP.getLangOptions(); } diff --git a/clang/include/clang/Parse/Scope.h b/clang/include/clang/Parse/Scope.h new file mode 100644 index 000000000000..c0c532943ef6 --- /dev/null +++ b/clang/include/clang/Parse/Scope.h @@ -0,0 +1,51 @@ +//===--- Scope.h - Scope interface ------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Chris Lattner and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the Scope interface. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_PARSE_SCOPE_H +#define LLVM_CLANG_PARSE_SCOPE_H + +#include "llvm/ADT/SmallVector.h" + +namespace llvm { +namespace clang { + class Decl; + +/// Scope - A scope is a transient data structure that is used while parsing the +/// program. It assists with resolving identifiers to the appropriate +/// declaration. +/// +class Scope { + /// The parent scope for this scope. This is null for the translation-unit + /// scope. + Scope *Parent; + + /// Depth - This is the depth of this scope. The translation-unit scope has + /// depth 0. + unsigned Depth; + + /// DeclsInScope - This keeps track of all declarations in this scope. When + /// the declaration is added to the scope, it is set as the current + /// declaration for the identifier in the IdentifierTable. When the scope is + /// popped, these declarations are removed from the IdentifierTable's notion + /// of current declaration. + SmallVector DeclsInScope; +public: + Scope(Scope *parent) : Parent(parent), Depth(Parent ? Parent->Depth+1 : 0) { + } + +}; + +} // end namespace clang +} // end namespace llvm + +#endif