Modules | Files | Inheritance Tree | Inheritance Graph | Name Index | Config
File: Synopsis/Parser/C++/syn/lookup.hh
    1| // Synopsis C++ Parser: lookup.hh header file
    2| // The Lookup class, which uses Builder's state (including search
    3| // dictionaries) to find names using the proper lookup rules.
    4| 
    5| // $Id: lookup.hh,v 1.4 2002/11/17 12:11:44 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_LOOKUP
   26| #define H_SYNOPSIS_CPP_LOOKUP
   27| 
   28| #include "ast.hh"
   29| 
   30| // Forward declare some Types::Type's
   31| namespace Types
   32| {
   33| class Type;
   34| class Base;
   35| class Named;
   36| class Unknown;
   37| class TemplateType;
   38| class FuncPtr;
   39| }
   40| 
   41| // Forward declare the Builder class
   42| class Builder;
   43| 
   44| class ScopeInfo;
   45| typedef std::vector<ScopeInfo*> ScopeSearch// TODO: move to common
   46| 
   47| //. AST Builder.
   48| //. This class manages the building of an AST, including queries on the
   49| //. existing AST such as name and type lookups. The building operations are
   50| //. called by SWalker as it walks the parse tree.
   51| class Lookup
   52| {
   53| public:
   54|     //. Constructor
   55|     Lookup(Builder*);
   56| 
   57|     //. Destructor. Recursively destroys all AST objects
   58|     ~Lookup();
   59| 
   60|     //. Changes the current accessability for the current scope
   61|     void set_access(AST::Access);
   62| 
   63| 
   64|     //
   65|     // AST Methods
   66|     //
   67| 
   68|     //. Returns the current scope
   69|     AST::Scopescope();
   70| 
   71|     //. Returns the global scope
   72|     AST::Scopeglobal();
   73| 
   74| 
   75|     //
   76|     // Type Methods
   77|     //
   78| 
   79|     //. Looks up the name in the current scope. This method always succeeds --
   80|     //. if the name is not found it forward declares it.
   81|     Types::Named* lookupType(const std::string& name, bool func_okay = false);
   82| 
   83|     //. Looks up the qualified name in the current scope. This method always
   84|     //. succeeds -- if the name is not found it forwards declares it.
   85|     //. @param names The list of identifiers given
   86|     //. @param fuc_okay If true, multiple declarations will not cause an error (needs fixing)
   87|     //. @param scope If set determines the scope to start lookup from, else the
   88|     //. current scope is used
   89|     Types::Named* lookupType(const ScopedName& names, bool func_okay=false, AST::Scope* scope=NULL);
   90| 
   91|     //. Looks up the name in the scope of the given scope. This method may
   92|     //. return a NULL ptr if the lookup failed.
   93|     Types::Named* lookupType(const std::string& name, AST::Scope* scope);
   94| 
   95|     //. Looks up the function in the given scope with the given args.
   96|     AST::FunctionlookupFunc(const std::string& , AST::Scope*, const std::vector<Types::Type*>&);
   97| 
   98|     //. Looks up the function operator in the current scope with the given
   99|     //. types. May return NULL if builtin operator or no operator is found.
  100|     AST::FunctionlookupOperator(const std::string& oper, Types::Type* left_type, Types::Type* right_type);
  101| 
  102|     //. Maps a scoped name into a vector of scopes and the final type. Returns
  103|     //. true on success.
  104|     bool mapName(const ScopedName& name, std::vector<AST::Scope*>&, Types::Named*&);
  105| 
  106|     //. Returns the types for an array operator on the given type with an
  107|     //. argument of the given type. If a function is used then it is stored in
  108|     //. the function ptr ref given, else the ptr is set to NULL.
  109|     Types::Type* arrayOperator(Types::Type* object, Types::Type* arg, AST::Function*&);
  110| 
  111|     //. Resolves the final type of the given type. If the given type is an
  112|     //. Unknown, it checks to see if the type has been defined yet and returns
  113|     //. that instead.
  114|     Types::Named* resolveType(Types::Named* maybe_unknown);
  115| 
  116| private:
  117|     //. Looks up the name in the current scope. This method may fail and
  118|     //. return a NULL ptr.
  119|     Types::Named* lookup(const std::string& name, bool func_okay = false);
  120| 
  121|     //. Searches for name in the list of Scopes. This method may return NULL
  122|     //. if the name is not found.
  123|     Types::Named* lookup(const std::string& name, const ScopeSearch&, bool func_okay = false) throw ();
  124| 
  125|     //. Searches for name in the given qualified scope. This method may return
  126|     //. NULL if the name is not found. Lookup proceeds according to the spec:
  127|     //. if 'scope' is a Class scope, then scope and all base classes are
  128|     //. searched, else if it's a 'namespace' scope then all usings are checked.
  129|     Types::Named* lookupQual(const std::string& name, const ScopeInfo*, bool func_okay = false);
  130| 
  131|     //. Return a ScopeInfo* for the given Declaration. This method first looks for
  132|     //. an existing Scope* in the Private map.
  133|     ScopeInfo* find_info(AST::Scope*);
  134| 
  135|     //. Utility class to add all functions with the given name in the given
  136|     //. Scope's dictionary to the given vector. May throw an error if the
  137|     //. types looked up are not functions.
  138|     void findFunctions(const std::string&, ScopeInfo*, std::vector<AST::Function*>&);
  139| 
  140|     //. Determines the best function from the given list for the given
  141|     //. arguments using heuristics. Returns the function and stores the cost
  142|     AST::FunctionbestFunction(const std::vector<AST::Function*>&, const std::vector<Types::Type*>&, int& cost);
  143| 
  144|     //. Formats the search of the given Scope for logging
  145|     std::string dumpSearch(ScopeInfo* scope);
  146| 
  147|     //. A pointer to the Builder.
  148|     Builder* m_builder;
  149| 
  150| };
  151| 
  152| #endif
  153| // vim: set ts=8 sts=4 sw=4 et: