Modules | Files | Inheritance Tree | Inheritance Graph | Name Index | Config
File: Synopsis/Parser/C++/syn/swalker.hh
    1| // Synopsis C++ Parser: swalker.hh header file
    2| // The SWalker class, which walks over the Parse-tree generated by OpenC++ and
    3| // converts it into a Synopsis AST using the Builder class.
    4| 
    5| // $Id: swalker.hh,v 1.28 2002/12/09 04:01:02 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_SWALKER
   26| #define H_SYNOPSIS_CPP_SWALKER
   27| 
   28| #include <occ/ptree.h>
   29| #include <occ/walker.h>
   30| // Stupid occ
   31| #undef Scope
   32| 
   33| #include <vector>
   34| #include <string>
   35| 
   36| // Forward declarations
   37| class Builder;
   38| class Program;
   39| class Decoder;
   40| class TypeFormatter;
   41| class LinkStore;
   42| class Lookup;
   43| class FileFilter;
   44| 
   45| namespace AST
   46| {
   47| class SourceFile;
   48| class Parameter;
   49| class Inheritance;
   50| class Declaration;
   51| class Function;
   52| class Scope;
   53| }
   54| namespace Types
   55| {
   56| class Type;
   57| }
   58| 
   59| //. A walker that creates an AST. All Translate* methods have been overridden
   60| //. to remove the translation code.
   61| class SWalker : public Walker
   62| {
   63| public:
   64|     //. Constructor
   65|     SWalker(FileFilter*, Parser*, Builder*, Program*);
   66|     virtual ~SWalker();
   67| 
   68|     //. Sets extract tails to true.
   69|     //. This will cause the parser to create dummy declarations for comments
   70|     //. before close braces or the end of the file
   71|     void set_extract_tails(bool value)
   72|     {
   73|         m_extract_tails = value;
   74|     }
   75|     //. Sets store links to true.
   76|     //. This will cause the whole ptree to be traversed, and any linkable
   77|     //. identifiers found will be stored
   78|     void set_store_links(LinkStore*);
   79| 
   80|     //. Get a name from the ptree
   81|     std::string parse_name(Ptree*) const;
   82| 
   83|     //. Get the Parser object
   84|     Parser* parser()
   85|     {
   86|         return m_parser;
   87|     }
   88|     //. Get the Program object
   89|     Programprogram()
   90|     {
   91|         return m_program;
   92|     }
   93|     //. Get the Builder object
   94|     Builder* builder()
   95|     {
   96|         return m_builder;
   97|     }
   98|     //. Get the TypeFormatter object
   99|     TypeFormatter* type_formatter()
  100|     {
  101|         return m_type_formatter;
  102|     }
  103| #if 0
  104|     //. Returns true if the current filename from the last getLine or
  105|     //. updateLineNumber call is equal to the main source filename
  106|     bool is_main_file()
  107|     {
  108|         return (m_filename == m_source);
  109|     }
  110| #endif
  111| 
  112|     //. Get the line number of the given Ptree node
  113|     int line_of_ptree(Ptree*);
  114|     //. Update the line number
  115|     void update_line_number(Ptree*);
  116| 
  117|     void add_comments(AST::Declaration* decl, Ptree* comments);
  118|     void add_comments(AST::Declaration* decl, CommentedLeaf* node);
  119|     void add_comments(AST::Declaration* decl, PtreeDeclarator* node);
  120|     void add_comments(AST::Declaration* decl, PtreeDeclaration* decl);
  121|     void add_comments(AST::Declaration* decl, PtreeNamespaceSpec* decl);
  122|     //. Traverses left side of tree till it finds a leaf, and if that is a
  123|     //. CommentedLeaf then it adds those comments as spans
  124|     void find_comments(Ptree* node);
  125| 
  126|     // Takes the (maybe nil) args list and puts them in m_params
  127|     void TranslateFunctionArgs(Ptree* args);
  128|     void TranslateParameters(Ptree* p_params, std::vector<AST::Parameter*>& params);
  129|     void TranslateFunctionName(char* encname, std::string& realname, Types::Type*& returnType);
  130|     PtreeTranslateDeclarator(Ptree*);
  131|     PtreeTranslateFunctionDeclarator(Ptree*, bool is_const);
  132|     PtreeTranslateVariableDeclarator(Ptree*, bool is_const);
  133|     void TranslateTypedefDeclarator(Ptree* node);
  134|     std::vector<AST::Inheritance*> TranslateInheritanceSpec(Ptree *node);
  135|     //. Returns a formatter string of the parameters. The idea is that this
  136|     //. string will be appended to the function name to form the 'name' of the
  137|     //. function.
  138|     std::string format_parameters(std::vector<AST::Parameter*>& params);
  139| 
  140|     //. Translates the template parameters and creates the template type.
  141|     void SWalker::TranslateTemplateParams(Ptree* params);
  142| 
  143|     // default translation
  144|     virtual PtreeTranslatePtree(Ptree*);
  145| 
  146|     //. Overridden to catch exceptions
  147|     void Translate(Ptree*);
  148| 
  149|     virtual PtreeTranslateTypedef(Ptree*);
  150|     virtual PtreeTranslateTemplateDecl(Ptree*);
  151|     virtual PtreeTranslateTemplateInstantiation(Ptree*);
  152|     //virtual Ptree* TranslateTemplateInstantiation(Ptree*, Ptree*, Ptree*, Class*);
  153|     virtual PtreeTranslateExternTemplate(Ptree*);
  154|     virtual PtreeTranslateTemplateClass(Ptree*, Ptree*);
  155|     virtual PtreeTranslateTemplateFunction(Ptree*, Ptree*);
  156|     virtual PtreeTranslateMetaclassDecl(Ptree*);
  157|     virtual PtreeTranslateLinkageSpec(Ptree*);
  158|     virtual PtreeTranslateNamespaceSpec(Ptree*);
  159|     virtual PtreeTranslateUsing(Ptree*);
  160|     virtual PtreeTranslateDeclaration(Ptree*);
  161|     virtual PtreeTranslateStorageSpecifiers(Ptree*);
  162|     virtual PtreeTranslateDeclarators(Ptree*);
  163|     virtual PtreeTranslateArgDeclList(boolPtree*, Ptree*);
  164|     virtual PtreeTranslateInitializeArgs(PtreeDeclarator*, Ptree*);
  165|     virtual PtreeTranslateAssignInitializer(PtreeDeclarator*, Ptree*);
  166| 
  167|     virtual PtreeTranslateFunctionImplementation(Ptree*);
  168| 
  169|     virtual PtreeTranslateFunctionBody(Ptree*);
  170|     virtual PtreeTranslateBrace(Ptree*);
  171|     virtual PtreeTranslateBlock(Ptree*);
  172|     //virtual Ptree* TranslateClassBody(Ptree*, Ptree*, Class*);
  173| 
  174|     virtual PtreeTranslateClassSpec(Ptree*);
  175|     //virtual Class* MakeClassMetaobject(Ptree*, Ptree*, Ptree*);
  176|     //virtual Ptree* TranslateClassSpec(Ptree*, Ptree*, Ptree*, Class*);
  177|     virtual PtreeTranslateEnumSpec(Ptree*);
  178| 
  179|     virtual PtreeTranslateAccessSpec(Ptree*);
  180|     virtual PtreeTranslateAccessDecl(Ptree*);
  181|     virtual PtreeTranslateUserAccessSpec(Ptree*);
  182| 
  183|     virtual PtreeTranslateIf(Ptree*);
  184|     virtual PtreeTranslateSwitch(Ptree*);
  185|     virtual PtreeTranslateWhile(Ptree*);
  186|     virtual PtreeTranslateDo(Ptree*);
  187|     virtual PtreeTranslateFor(Ptree*);
  188|     virtual PtreeTranslateTry(Ptree*);
  189|     virtual PtreeTranslateBreak(Ptree*);
  190|     virtual PtreeTranslateContinue(Ptree*);
  191|     virtual PtreeTranslateReturn(Ptree*);
  192|     virtual PtreeTranslateGoto(Ptree*);
  193|     virtual PtreeTranslateCase(Ptree*);
  194|     virtual PtreeTranslateDefault(Ptree*);
  195|     virtual PtreeTranslateLabel(Ptree*);
  196|     virtual PtreeTranslateExprStatement(Ptree*);
  197| 
  198|     virtual PtreeTranslateTypespecifier(Ptree*);
  199|     virtual PtreeTranslateTypeof(Ptree*, Ptree* declarations);
  200| 
  201|     virtual PtreeTranslateComma(Ptree*);
  202|     virtual PtreeTranslateAssign(Ptree*);
  203|     virtual PtreeTranslateCond(Ptree*);
  204|     virtual PtreeTranslateInfix(Ptree*);
  205|     virtual PtreeTranslatePm(Ptree*);
  206|     virtual PtreeTranslateCast(Ptree*);
  207|     virtual PtreeTranslateUnary(Ptree*);
  208|     virtual PtreeTranslateThrow(Ptree*);
  209|     virtual PtreeTranslateSizeof(Ptree*);
  210|     virtual PtreeTranslateNew(Ptree*);
  211|     virtual PtreeTranslateNew3(Ptree* type);
  212|     virtual PtreeTranslateDelete(Ptree*);
  213|     virtual PtreeTranslateThis(Ptree*);
  214|     virtual PtreeTranslateVariable(Ptree*);
  215|     virtual PtreeTranslateFstyleCast(Ptree*);
  216|     virtual PtreeTranslateArray(Ptree*);
  217|     virtual PtreeTranslateFuncall(Ptree*);    // and fstyle cast
  218|     virtual PtreeTranslatePostfix(Ptree*);
  219|     virtual PtreeTranslateUserStatement(Ptree*);
  220|     virtual PtreeTranslateDotMember(Ptree*);
  221|     virtual PtreeTranslateArrowMember(Ptree*);
  222|     virtual PtreeTranslateParen(Ptree*);
  223|     virtual PtreeTranslateStaticUserStatement(Ptree*);
  224| 
  225|     AST::SourceFile* current_file() const
  226|     {
  227|         return m_file;
  228|     }
  229|     int current_lineno() const
  230|     {
  231|         return m_lineno;
  232|     }
  233|     static SWalker *instance()
  234|     {
  235|         return g_swalker;
  236|     }
  237| private:
  238|     // the 'current' walker is a debugging aid.
  239|     static SWalkerg_swalker;
  240| 
  241|     Parser* m_parser;
  242|     Builder* m_builder;
  243|     FileFilter* m_filter;
  244|     Programm_program;
  245|     Decoder* m_decoder;
  246|     Lookup* m_lookup;
  247| 
  248|     //. A pointer to the currect declaration ptree, if any, used to get the
  249|     //. return type and modifiers, etc.
  250|     Ptreem_declaration;
  251|     //. A pointer to the current template parameters, if any, used to get the
  252|     //. template parameters and set in the declaration. Should be NULL if not
  253|     //. in a template.
  254|     std::vector<AST::Parameter*>* m_template;
  255|     //. This pointer is used as a comparison to avoid redundant effort. Do
  256|     //. not use it to get the filename, since the string is a reference to the
  257|     //. preprocessed file in memory and is *not* null terminated!
  258|     charm_filename_ptr;
  259|     //. The current file, set by update_line_number
  260|     AST::SourceFile* m_file;
  261|     int m_lineno;
  262| 
  263|     //. True if should try and extract tail comments before }'s
  264|     bool m_extract_tails;
  265|     //. Storage for links. This is only set if we should be storing links, so
  266|     //. it must be checked before every use
  267|     LinkStore* m_links;
  268|     //. True if this TranslateDeclarator should try to store the decl type
  269|     bool m_store_decl;
  270| 
  271|     //. A dummy name used for tail comments
  272|     std::vector<std::stringm_dummyname;
  273| 
  274|     //. An instance of TypeFormatter for formatting types
  275|     TypeFormatter* m_type_formatter;
  276| 
  277|     //. The current function, if in a function block
  278|     AST::Function* m_function;
  279|     //. The params found before a function block. These may be different from
  280|     //. the ones that are in the original declaration(s), but it is these names
  281|     //. we need for referencing names inside the block, so a reference is stored
  282|     //. here.
  283|     std::vector<AST::Parameter*> m_param_cache;
  284|     //. The types accumulated for function parameters in function calls
  285|     std::vector<Types::Type*> m_params;
  286|     //. The type returned from the expression-type translators
  287|     Types::Type* m_type;
  288|     //. The Scope to use for name lookups, or NULL to use enclosing default
  289|     //. scope rules. This is for when we are at a Variable, and already know it
  290|     //. must be part of a given class (eg, foo->bar .. bar must be in foo's
  291|     //. class)
  292|     AST::Scope* m_scope;
  293| 
  294|     //. The state of postfix translation. This is needed for constructs like
  295|     //. foo->var versus var or foo->var(). The function call resolution needs
  296|     //. to be done in the final TranslateVariable, since that's where the last
  297|     //. name (which is to be linked) is handled.
  298|     enum Postfix_Flag
  299|     {
  300|         Postfix_Var//.< Lookup as a variable
  301|         Postfix_Func//.< Lookup as a function, using m_params for parameters
  302|     m_postfix_flag;
  303| 
  304|     //. Info about one stored function impl. Function impls must be stored
  305|     //. till the end of a class.
  306|     struct FuncImplCache
  307|     {
  308|         AST::Function* func;
  309|         std::vector<AST::Parameter*> params;
  310|         Ptreebody;
  311|     };
  312|     //. A vector of function impls
  313|     typedef std::vector<FuncImplCacheFuncImplVec;
  314|     //. A stack of function impl vectors
  315|     typedef std::vector<FuncImplVecFuncImplStack;
  316|     //. The stack of function impl vectors
  317|     FuncImplStack m_func_impl_stack;
  318|     void SWalker::TranslateFuncImplCache(const FuncImplCache& cache);
  319| 
  320|     //. Finds the column given the start ptr and the current position. The
  321|     //. derived column number is processed with the link_map before returning,
  322|     //. so -1 may be returned to indicate "inside macro".
  323|     int find_col(const char* start, const char* find);
  324| 
  325| };
  326| 
  327| #endif // header guard
  328| // vim: set ts=8 sts=4 sw=4 et: