Modules | Files | Inheritance Tree | Inheritance Graph | Name Index | Config
File: Synopsis/Parser/C++/syn/dict.hh
    1| // Synopsis C++ Parser: dict.hh header file
    2| // The Dictionary class which is used to store and lookup types for each scope
    3| 
    4| // $Id: dict.hh,v 1.11 2002/11/17 12:11:43 chalky Exp $
    5| //
    6| // This file is a part of Synopsis.
    7| // Copyright (C) 2002 Stephen Davies
    8| //
    9| // Synopsis is free software; you can redistribute it and/or modify it
   10| // under the terms of the GNU General Public License as published by
   11| // the Free Software Foundation; either version 2 of the License, or
   12| // (at your option) any later version.
   13| //
   14| // This program is distributed in the hope that it will be useful,
   15| // but WITHOUT ANY WARRANTY; without even the implied warranty of
   16| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   17| // General Public License for more details.
   18| //
   19| // You should have received a copy of the GNU General Public License
   20| // along with this program; if not, write to the Free Software
   21| // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   22| // 02111-1307, USA.
   23| 
   24| #ifndef H_SYNOPSIS_CPP_DICT
   25| #define H_SYNOPSIS_CPP_DICT
   26| 
   27| #include <vector>
   28| #include <string>
   29| #include "common.hh"
   30| 
   31| // Forward declaration of Type::Named
   32| namespace Types
   33| {
   34| class Named;
   35| }
   36| 
   37| // Forward declaration of AST::Declaration
   38| namespace AST
   39| {
   40| class Declaration;
   41| }
   42| 
   43| //. Dictionary of named declarations with lookup.
   44| //. This class maintains a dictionary of names, which index types,
   45| //. supposedly declared in the scope that has this dictionary. There may be
   46| //. only one declaration per name, except in the case of function names.
   47| class Dictionary : public cleanup
   48| {
   49| public:
   50|     //. Constructor
   51|     Dictionary();
   52|     //. Destructor
   53|     ~Dictionary();
   54| 
   55|     //. The type of multiple entries. We don't want to include type.hh just for
   56|     //. this, so this is a workaround
   57|     typedef std::vector<Types::Named*> Type_vector;
   58| 
   59|     //. Exception thrown when multiple declarations are found when one is
   60|     //. expected. The list of declarations is stored in the exception.
   61|     struct MultipleError
   62|     {
   63|         // The vector of types that *were* found. This is returned so that whoever
   64|         // catches the error can proceed straight away
   65|         Type_vector types;
   66|     };
   67| 
   68|     //. Exception thrown when a name is not found in lookup*()
   69|     struct KeyError
   70|     {
   71|         //. Constructor
   72|         KeyError(const std::string& n)
   73|                 : name(n)
   74|         { }
   75|         //. The name which was not found
   76|         std::string name;
   77|     };
   78| 
   79|     //. Returns true if name is in dict
   80|     bool has_key(const std::string& name);
   81| 
   82|     //. Lookup a name in the dictionary. If more than one declaration has this
   83|     //. name then an exception is thrown.
   84|     Types::Named* lookup(const std::string& name);// throw (MultipleError, KeyError);
   85| 
   86|     //. Lookup a name in the dictionary expecting multiple decls. Use this
   87|     //. method if you expect to find more than one declaration, eg importing
   88|     //. names via a using statement.
   89|     Type_vector lookupMultiple(const std::string& name) throw (KeyError);
   90| 
   91|     //. Add a declaration to the dictionary. The name() is extracted from the
   92|     //. declaration and its last string used as the key. The declaration is
   93|     //. stored as a Type::Declared which is created inside this method.
   94|     void insert(AST::Declaration* decl);
   95| 
   96|     //. Add a named type to the dictionary. The name() is extracted from the
   97|     //. type and its last string used as they key.
   98|     void insert(Types::Named* named);
   99| 
  100|     //. Dump the contents for debugging
  101|     void dump();
  102| 
  103| 
  104| private:
  105|     struct Data;
  106|     //. The private data. This is a forward declared * to speed compilation since
  107|     //. std::map is a large template.
  108|     Data*  m;
  109| };
  110| 
  111| #endif // header guard
  112| // vim: set ts=8 sts=4 sw=4 et: