Modules | Files | Inheritance Tree | Inheritance Graph | Name Index | Config
File: Synopsis/Parser/C++/syn/builder.hh
    1| // Synopsis C++ Parser: builder.hh header file
    2| // The Builder class, which builds an AST. Used by the SWalker which calls the
    3| // appropriate Builder member functions
    4| 
    5| // $Id: builder.hh,v 1.32 2003/01/27 06:53:37 chalky Exp $
    6| //
    7| // This file is a part of Synopsis.
    8| // Copyright (C) 2002 Stephen Davies
    9| //
   10| // Synopsis is free software; you can redistribute it and/or modify it
   11| // under the terms of the GNU General Public License as published by
   12| // the Free Software Foundation; either version 2 of the License, or
   13| // (at your option) any later version.
   14| //
   15| // This program is distributed in the hope that it will be useful,
   16| // but WITHOUT ANY WARRANTY; without even the implied warranty of
   17| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   18| // General Public License for more details.
   19| //
   20| // You should have received a copy of the GNU General Public License
   21| // along with this program; if not, write to the Free Software
   22| // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   23| // 02111-1307, USA.
   24| 
   25| #ifndef H_SYNOPSIS_CPP_BUILDER
   26| #define H_SYNOPSIS_CPP_BUILDER
   27| 
   28| #include <map>
   29| #include "ast.hh"
   30| #include "common.hh"
   31| 
   32| // Forward declare some Types::Types
   33| namespace Types
   34| {
   35| class Type;
   36| class Base;
   37| class Named;
   38| class Unknown;
   39| class TemplateType;
   40| class FuncPtr;
   41| class Dependent;
   42| }
   43| 
   44| // Forward declare the SWalker class
   45| class SWalker;
   46| 
   47| // Forward declare the Lookup class
   48| class Lookup;
   49| 
   50| class ScopeInfo;
   51| typedef std::vector<ScopeInfo*> ScopeSearch// TODO: move to common
   52| 
   53| //. Enumeration of namespace types for use in Builder::start_namespace()
   54| enum NamespaceType
   55| {
   56|     NamespaceNamed//.< Normal, named, namespace. name is its given name
   57|     NamespaceAnon//.< An anonymous namespace. name is the filename
   58|     NamespaceUnique//.< A unique namespace. name is the type (for, while, etc.)
   59|     NamespaceTemplate//.< A template namespace. name is empty
   60| };
   61| 
   62| 
   63| //. AST Builder.
   64| //. This class manages the building of an AST, including queries on the
   65| //. existing AST such as name and type lookups. The building functions are
   66| //. called by SWalker as it walks the parse tree.
   67| class Builder
   68| {
   69|     friend class Lookup;
   70| public:
   71|     //. Constructor
   72|     Builder(AST::SourceFile* file);
   73| 
   74|     //. Destructor. Recursively destroys all AST objects
   75|     ~Builder();
   76| 
   77|     //. Sets the swalker
   78|     void set_swalker(SWalker* swalker)
   79|     {
   80|         m_swalker = swalker;
   81|     }
   82| 
   83|     //. Changes the current accessability for the current scope
   84|     void set_access(AST::Access);
   85| 
   86|     //. Returns the current file
   87|     AST::SourceFilefile() const
   88|     {
   89|         return m_file;
   90|     }
   91|     //. Changes the current file
   92|     void set_file(AST::SourceFile*);
   93| 
   94|     //. Returns the list of builtin decls ("__null_t", "true", etc.)
   95|     const AST::Declaration::vectorbuiltin_decls() const;
   96| 
   97|     //
   98|     // State Methods
   99|     //
  100| 
  101|     //. Returns the current scope
  102|     AST::Scopescope()
  103|     {
  104|         return m_scope;
  105|     }
  106| 
  107|     //. Returns the current ScopeInfo for the current Scope
  108|     ScopeInfo* scopeinfo()
  109|     {
  110|         return m_scopes.back();
  111|     }
  112| 
  113|     //. Returns the global scope
  114|     AST::Scopeglobal()
  115|     {
  116|         return m_global;
  117|     }
  118| 
  119|     //. Returns the Lookup object for the builder
  120|     Lookup* lookup()
  121|     {
  122|         return m_lookup;
  123|     }
  124| 
  125|     //
  126|     // AST Methods
  127|     //
  128| 
  129|     //. Add the given Declaration to the current scope. If is_template is true,
  130|     //. then it is added to the parent of the current scope, assuming that the
  131|     //. current scope is the temporary template scope
  132|     void add(AST::Declaration* declaration, bool is_template = false);
  133| 
  134|     //. Add the given non-declaration type to the current scope
  135|     void add(Types::Named* named);
  136| 
  137|     //. Adds the given Macros to the global scope. This method should only be
  138|     //. called once, with the macros stored in order from the preprocessing
  139|     //. stage.
  140|     void add_macros(const std::vector<AST::Macro*>&);
  141| 
  142|     //. Construct and open a new Namespace. The Namespace becomes the
  143|     //. current scope, and the old one is pushed onto the stack. If name is
  144|     //. empty then a unique name is generated of the form `ns1
  145|     AST::Namespacestart_namespace(const std::string& name, NamespaceType type);
  146| 
  147|     //. End the current namespace and pop the previous Scope off the stack
  148|     void end_namespace();
  149| 
  150|     //. Starts a new template namespace
  151|     AST::Namespacestart_template();
  152| 
  153|     //. End the current template namespace
  154|     void end_template();
  155| 
  156|     //. Construct and open a new Class. The Class becomes the current scope,
  157|     //. and the old one is pushed onto the stack. The type argument is the
  158|     //. type, ie: "class" or "struct". This is tested to determine the default
  159|     //. accessability. If this is a template class, the templ_params vector must
  160|     //. be non-null pointer
  161|     AST::Classstart_class(intconst std::string& type, const std::string& name,
  162|                             AST::Parameter::vector* templ_params);
  163| 
  164|     //. Construct and open a new Class with a qualified name
  165|     AST::Classstart_class(intconst std::string& type, const ScopedName& names);
  166| 
  167|     //. Update the search to include base classes. Call this method after
  168|     //. startClass(), and after filling in the parents() vector of the returned
  169|     //. AST::Class object. After calling this method, name and type lookups
  170|     //. will correctly search the base classes of this class.
  171|     void update_class_base_search();
  172| 
  173|     //. End the current class and pop the previous Scope off the stack
  174|     void end_class();
  175| 
  176|     //. Start function impl scope
  177|     void start_function_impl(const ScopedName& name);
  178| 
  179|     //. End function impl scope
  180|     void end_function_impl();
  181| 
  182|     //. Add an function
  183|     AST::Functionadd_function(intconst std::string& name,
  184|                                 const std::vector<std::string>& premod, Types::Type* ret,
  185|                                 const std::string& realname, AST::Parameter::vector* templ_params);
  186| 
  187|     //. Add a variable
  188|     AST::Variableadd_variable(intconst std::string& name, Types::Type* vtype, bool constr, const std::string& type);
  189| 
  190|     //. Add a variable to represent 'this', iff we are in a method
  191|     void add_this_variable();
  192| 
  193|     //. Add a typedef
  194|     AST::Typedefadd_typedef(intconst std::string& name, Types::Type* alias, bool constr);
  195| 
  196|     //. Add an enumerator
  197|     AST::Enumeratoradd_enumerator(intconst std::string& name, const std::string& value);
  198| 
  199|     //. Add an enum
  200|     AST::Enumadd_enum(intconst std::string& name, const AST::Enumerator::vector &);
  201| 
  202|     //. Add a tail comment. This will be a dummy declaration with an empty name
  203|     //. and type "dummy"
  204|     AST::Declarationadd_tail_comment(int line);
  205| 
  206|     //
  207|     // Using methods
  208|     //
  209| 
  210|     //. Add a namespace using declaration.
  211|     void add_using_namespace(Types::Named* type);
  212| 
  213|     //. Add a namespace alias using declaration.
  214|     void add_aliased_using_namespace(Types::Named* type, const std::string& alias);
  215| 
  216|     //. Add a using declaration.
  217|     void add_using_declaration(Types::Named* type);
  218| 
  219| 
  220|     //. Maps a scoped name into a vector of scopes and the final type. Returns
  221|     //. true on success.
  222|     bool mapName(const ScopedName& name, std::vector<AST::Scope*>&, Types::Named*&);
  223| 
  224|     //. Create a Base type for the given name in the current scope
  225|     Types::Base* create_base(const std::string& name);
  226| 
  227|     //. Create a Dependent type for the given name in the current scope
  228|     Types::Dependent* create_dependent(const std::string& name);
  229| 
  230|     //. Create an Unknown type for the given name in the current scope
  231|     Types::Unknown* create_unknown(const std::string& name);
  232| 
  233|     //. Create a Template type for the given name in the current scope
  234|     Types::Template* create_template(const std::string& name, const std::vector<Types::Type*>&);
  235| 
  236|     //. Add an Unknown decl for given name if it doesnt already exist
  237|     Types::Unknown* add_unknown(const std::string& name);
  238| 
  239|     //. Add an Templated Forward decl for given name if it doesnt already exist
  240|     AST::Forwardadd_forward(int lineno, const std::string& name, AST::Parameter::vector* templ_params);
  241| 
  242| private:
  243|     //. Current file
  244|     AST::SourceFilem_file;
  245| 
  246|     //. The global scope object
  247|     AST::Scopem_global;
  248| 
  249|     //. Current scope object
  250|     AST::Scopem_scope;
  251| 
  252|     //. A counter used to generate unique namespace names
  253|     int m_unique;
  254| 
  255|     //. The stack of Builder::Scopes
  256|     std::vector<ScopeInfo*> m_scopes;
  257| 
  258|     //. Private data which uses map
  259|     struct Private;
  260|     //. Private data which uses map instance
  261|     Private* m;
  262| 
  263|     //. Return a ScopeInfo* for the given Declaration. This method first looks for
  264|     //. an existing Scope* in the Private map.
  265|     ScopeInfo* find_info(AST::Scope*);
  266| 
  267|     //. Utility method to recursively add base classes to given search
  268|     void add_class_bases(AST::Class* clas, ScopeSearch& search);
  269| 
  270|     //. Formats the search of the given Scope for logging
  271|     std::string dump_search(ScopeInfo* scope);
  272| 
  273|     //. Recursively adds 'target' as using in 'scope'
  274|     void do_add_using_namespace(ScopeInfo* target, ScopeInfo* scope);
  275| 
  276|     //. A class that compares Scopes
  277|     class EqualScope;
  278| 
  279|     //. A pointer to the SWalker. This is set explicitly by the SWalker during
  280|     //. its constructor (which takes a Builder).
  281|     SWalker* m_swalker;
  282| 
  283|     //. A pointer to the Lookup
  284|     Lookup* m_lookup;
  285| 
  286| };
  287| 
  288| #endif
  289| // vim: set ts=8 sts=4 sw=4 et: