LCOV - code coverage report
Current view: top level - include/as2js - compiler.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 10 0.0 %
Date: 2014-11-22 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #ifndef AS2JS_COMPILER_H
       2             : #define AS2JS_COMPILER_H
       3             : /* compiler.h -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       4             : 
       5             : /*
       6             : 
       7             : Copyright (c) 2005-2014 Made to Order Software Corp.
       8             : 
       9             : http://snapwebsites.org/project/as2js
      10             : 
      11             : Permission is hereby granted, free of charge, to any
      12             : person obtaining a copy of this software and
      13             : associated documentation files (the "Software"), to
      14             : deal in the Software without restriction, including
      15             : without limitation the rights to use, copy, modify,
      16             : merge, publish, distribute, sublicense, and/or sell
      17             : copies of the Software, and to permit persons to whom
      18             : the Software is furnished to do so, subject to the
      19             : following conditions:
      20             : 
      21             : The above copyright notice and this permission notice
      22             : shall be included in all copies or substantial
      23             : portions of the Software.
      24             : 
      25             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      26             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      27             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      28             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      29             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      30             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      31             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      32             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      33             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      34             : SOFTWARE.
      35             : 
      36             : */
      37             : 
      38             : #include    "as2js/optimizer.h"
      39             : #include    "as2js/stream.h"
      40             : #include    "as2js/options.h"
      41             : 
      42             : #include    <controlled_vars/controlled_vars_need_init.h>
      43             : 
      44             : 
      45             : namespace as2js
      46             : {
      47             : 
      48             : 
      49             : 
      50             : 
      51             : // Once a program was parsed, you need to compile it. This
      52             : // mainly means resolving the references (i.e. identifiers)
      53             : // which may generate the loading of libraries specified in
      54             : // import instructions (note that some import instructions
      55             : // are automatic for the global and native environments.)
      56             : //
      57             : // The code, after you ran the parser looks like this:
      58             : //
      59             : //    Compiler *compiler = Compiler::CreateCompiler();
      60             : //    // this is the same options as for the parser
      61             : //    compiler->SetOptions(options);
      62             : //    error_count = compiler->Compile(root);
      63             : //
      64             : // The Compile() function returns the number of errors
      65             : // encountered while compiling. The root parameter is
      66             : // what was returned by the Parse() function of the
      67             : // Parser object.
      68             : class Compiler
      69             : {
      70             : public:
      71             :     typedef std::shared_ptr<Compiler>   pointer_t;
      72             : 
      73             :                                 Compiler(Options::pointer_t options);
      74             :     virtual                     ~Compiler();
      75             : 
      76             :     InputRetriever::pointer_t   set_input_retriever(InputRetriever::pointer_t retriever);
      77             :     int                         compile(Node::pointer_t& root);
      78             : 
      79             : private:
      80             :     typedef controlled_vars::need_init<time_t>      mtime_t;
      81             :     typedef uint32_t                                search_error_t;
      82             :     typedef uint32_t                                search_flag_t;
      83             : 
      84             :     static search_error_t const SEARCH_ERROR_NONE                   = 0x00000000;
      85             :     static search_error_t const SEARCH_ERROR_PRIVATE                = 0x00000001;
      86             :     static search_error_t const SEARCH_ERROR_PROTECTED              = 0x00000002;
      87             :     static search_error_t const SEARCH_ERROR_PROTOTYPE              = 0x00000004;
      88             :     static search_error_t const SEARCH_ERROR_WRONG_PRIVATE          = 0x00000008;
      89             :     static search_error_t const SEARCH_ERROR_WRONG_PROTECTED        = 0x00000010;
      90             :     static search_error_t const SEARCH_ERROR_PRIVATE_PACKAGE        = 0x00000020;
      91             :     static search_error_t const SEARCH_ERROR_EXPECTED_STATIC_MEMBER = 0x00000040;
      92             : 
      93             :     static search_flag_t const  SEARCH_FLAG_NO_PARSING              = 0x00000001;    // avoid parsing variables
      94             :     static search_flag_t const  SEARCH_FLAG_GETTER                  = 0x00000002;    // accept getters (reading)
      95             :     static search_flag_t const  SEARCH_FLAG_SETTER                  = 0x00000004;    // accept setters (writing)
      96             :     static search_flag_t const  SEARCH_FLAG_PACKAGE_MUST_EXIST      = 0x00000008;    // weather the package has to exist
      97             : 
      98             :     typedef std::map<String, Node::pointer_t>   module_map_t;
      99             : 
     100             :     // automate the restoration of the error flags
     101             :     class RestoreFlags
     102             :     {
     103             :     public:
     104           0 :                 RestoreFlags(Compiler *compiler)
     105             :                 {
     106           0 :                     f_compiler = compiler;
     107           0 :                     f_org_flags = f_compiler->get_err_flags();
     108           0 :                     f_compiler->set_err_flags(0);
     109           0 :                 }
     110           0 :                 ~RestoreFlags()
     111             :                 {
     112           0 :                     f_compiler->set_err_flags(f_org_flags);
     113           0 :                 }
     114             : 
     115             :     private:
     116             :         Compiler *      f_compiler;
     117             :         int             f_org_flags;
     118             :     };
     119             : 
     120             : 
     121             :     // functions used to load the internal imports
     122             :     void                internal_imports();
     123             :     Node::pointer_t     load_module(char const *module, char const *file);
     124             :     void                load_internal_packages(char const *module);
     125             :     void                read_db();
     126             :     void                write_db();
     127             :     bool                find_module(String const& filename, Node::pointer_t& result);
     128             :     void                find_packages_add_database_entry(String const& package_name, Node::pointer_t& element, char const *type);
     129             :     void                find_packages_save_package_elements(Node::pointer_t package, String const& package_name);
     130             :     void                find_packages_directive_list(Node::pointer_t list);
     131             :     void                find_packages(Node::pointer_t program);
     132             :     String              get_package_filename(char const *package_info);
     133             : 
     134             :     void                add_variable(Node::pointer_t variable);
     135             :     bool                are_objects_derived_from_one_another(Node::pointer_t derived_class, Node::pointer_t super_class, Node::pointer_t& /*out*/ the_super_class);
     136             :     void                assignment_operator(Node::pointer_t expr);
     137             :     bool                best_param_match(Node::pointer_t& /*in,out*/ best, Node::pointer_t match);
     138             :     bool                best_param_match_derived_from(Node::pointer_t& /*in,out*/ best, Node::pointer_t match);
     139             :     void                binary_operator(Node::pointer_t& expr);
     140             :     void                break_continue(Node::pointer_t& break_node);
     141             :     void                call_add_missing_params(Node::pointer_t call, Node::pointer_t params);
     142             :     void                can_instantiate_type(Node::pointer_t expr);
     143             :     void                case_directive(Node::pointer_t& case_node);
     144             :     void                catch_directive(Node::pointer_t& catch_node);
     145             :     bool                check_field(Node::pointer_t link, Node::pointer_t field, int& funcs, Node::pointer_t& /*out*/ resolution, Node::pointer_t params, int const search_flags);
     146             :     bool                check_final_functions(Node::pointer_t& function_node, Node::pointer_t& class_node);
     147             :     bool                check_function(Node::pointer_t function_node, Node::pointer_t& /*out*/ resolution, String const& name, Node::pointer_t params, int const search_flags);
     148             :     int                 check_function_with_params(Node::pointer_t function_node, Node::pointer_t params);
     149             :     bool                check_import(Node::pointer_t& child, Node::pointer_t& /*out*/ resolution, String const& name, Node::pointer_t params, int const search_flags);
     150             :     void                check_member(Node::pointer_t ref, Node::pointer_t field, Node::pointer_t field_name);
     151             :     bool                check_name(Node::pointer_t list, int idx, Node::pointer_t& /*out*/ resolution, Node::pointer_t id, Node::pointer_t params, int const search_flags);
     152             :     void                check_super_validity(Node::pointer_t expr);
     153             :     void                check_this_validity(Node::pointer_t expr);
     154             :     bool                check_unique_functions(Node::pointer_t function_node, Node::pointer_t class_node, bool const all_levels);
     155             :     void                class_directive(Node::pointer_t& class_node);
     156             :     Node::pointer_t     class_of_member(Node::pointer_t parent);
     157             :     bool                compare_parameters(Node::pointer_t& lfunction, Node::pointer_t& rfunction);
     158             :     void                declare_class(Node::pointer_t class_node);
     159             :     void                default_directive(Node::pointer_t& default_node);
     160             :     bool                define_function_type(Node::pointer_t func);
     161             :     void                directive(Node::pointer_t& directive);
     162             :     Node::pointer_t     directive_list(Node::pointer_t directive_list);
     163             :     void                do_directive(Node::pointer_t& do_node);
     164             :     void                enum_directive(Node::pointer_t& enum_node);
     165             :     void                expression(Node::pointer_t expr, Node::pointer_t params = Node::pointer_t());
     166             :     bool                expression_new(Node::pointer_t expr);
     167             :     void                extend_class(Node::pointer_t class_node, Node::pointer_t extend_name);
     168             :     void                finally(Node::pointer_t& finally_node);
     169             :     bool                find_any_field(Node::pointer_t link, Node::pointer_t field, int& funcs, Node::pointer_t& resolution, Node::pointer_t params, int const search_flags);
     170             :     Node::depth_t       find_class(Node::pointer_t class_type, Node::pointer_t type, Node::depth_t depth);
     171             :     bool                find_external_package(Node::pointer_t import, String const& name, Node::pointer_t& /*out*/ program_node);
     172             :     bool                find_field(Node::pointer_t link, Node::pointer_t field, int& funcs, Node::pointer_t& resolution, Node::pointer_t params, int const search_flags);
     173             :     bool                find_final_functions(Node::pointer_t& function, Node::pointer_t& super);
     174             :     bool                find_in_extends(Node::pointer_t link, Node::pointer_t field, int& funcs, Node::pointer_t& resolution, Node::pointer_t params, int const search_flags);
     175             :     void                find_labels(Node::pointer_t function, Node::pointer_t node);
     176             :     bool                find_member(Node::pointer_t member, Node::pointer_t& /*out*/ resolution, Node::pointer_t params, int search_flags);
     177             :     bool                find_overloaded_function(Node::pointer_t class_node, Node::pointer_t function);
     178             :     Node::pointer_t     find_package(Node::pointer_t list, String const& name);
     179             :     bool                find_package_item(Node::pointer_t program, Node::pointer_t import, Node::pointer_t& /*out*/ resolution, String const& name, Node::pointer_t params, int const search_flags);
     180             :     void                for_directive(Node::pointer_t& for_node);
     181             :     bool                funcs_name(int& funcs, Node::pointer_t resolution, bool const increment = true);
     182             :     void                function(Node::pointer_t function_node);
     183             :     bool                get_attribute(Node::pointer_t node, Node::attribute_t const a);
     184             :     unsigned long       get_attributes(Node::pointer_t& node);
     185           0 :     search_error_t      get_err_flags() const { return f_err_flags; }
     186             :     void                goto_directive(Node::pointer_t& goto_node);
     187             :     bool                has_abstract_functions(Node::pointer_t class_node, Node::pointer_t list, Node::pointer_t& /*out*/ func);
     188             :     void                identifier_to_attrs(Node::pointer_t node, Node::pointer_t a);
     189             :     void                if_directive(Node::pointer_t& if_node);
     190             :     void                import(Node::pointer_t& import);
     191             :     bool                is_constructor(Node::pointer_t func);
     192             :     bool                is_derived_from(Node::pointer_t derived_class, Node::pointer_t super_class);
     193             :     bool                is_dynamic_class(Node::pointer_t class_node);
     194             :     bool                is_function_abstract(Node::pointer_t function);
     195             :     bool                is_function_overloaded(Node::pointer_t class_node, Node::pointer_t function);
     196             :     void                link_type(Node::pointer_t type);
     197             :     Node::depth_t       match_type(Node::pointer_t t1, Node::pointer_t t2);
     198             :     void                node_to_attrs(Node::pointer_t node, Node::pointer_t a);
     199             :     void                object_literal(Node::pointer_t expr);
     200             :     void                parameters(Node::pointer_t parameters_node);
     201             :     void                prepare_attributes(Node::pointer_t node);
     202             :     void                print_search_errors(const Node::pointer_t name);
     203             :     void                program(Node::pointer_t program_node);
     204             :     bool                replace_constant_variable(Node::pointer_t& /*in,out*/ replace, Node::pointer_t resolution);
     205             :     bool                resolve_call(Node::pointer_t call);
     206             :     bool                resolve_field(Node::pointer_t object, Node::pointer_t field, Node::pointer_t& resolution, Node::pointer_t params, int const search_flags);
     207             :     void                resolve_internal_type(Node::pointer_t parent, char const *type, Node::pointer_t& /*out*/ resolution);
     208             :     void                resolve_member(Node::pointer_t expr, Node::pointer_t params, int const search_flags);
     209             :     bool                resolve_name(Node::pointer_t list, Node::pointer_t id, Node::pointer_t& /*out*/ resolution, Node::pointer_t params, int const search_flags);
     210             :     Node::pointer_t     return_directive(Node::pointer_t return_node);
     211             :     bool                select_best_func(Node::pointer_t params, Node::pointer_t& /*out*/ resolution);
     212           0 :     void                set_err_flags(search_error_t flags) { f_err_flags = flags; }
     213             :     bool                special_identifier(Node::pointer_t expr);
     214             :     void                switch_directive(Node::pointer_t& switch_node);
     215             :     void                throw_directive(Node::pointer_t& throw_node);
     216             :     void                try_directive(Node::pointer_t& try_node);
     217             :     void                type_expr(Node::pointer_t expr);
     218             :     void                unary_operator(Node::pointer_t expr);
     219             :     void                use_namespace(Node::pointer_t& use_namespace_node);
     220             :     void                var(Node::pointer_t var_node);
     221             :     void                variable(Node::pointer_t variable_node, bool side_effects_only);
     222             :     void                variable_to_attrs(Node::pointer_t node, Node::pointer_t var);
     223             :     void                while_directive(Node::pointer_t& while_node);
     224             :     void                with(Node::pointer_t& with_node);
     225             : 
     226             :     mtime_t                     f_time;         // time when the compiler is created
     227             :     Options::pointer_t          f_options;
     228             :     Node::pointer_t             f_program;
     229             :     InputRetriever::pointer_t   f_input_retriever;
     230             :     controlled_vars::auto_init<search_error_t, 0>  f_err_flags;    // when searching a name and it doesn't get resolve, emit these errors
     231             :     Node::pointer_t             f_scope;        // with() and use namespace list
     232             :     module_map_t                f_modules;      // already loaded files (external modules)
     233             : };
     234             : 
     235             : 
     236             : 
     237             : 
     238             : 
     239             : }
     240             : // namespace as2js
     241             : #endif
     242             : // #ifndef AS2JS_COMPILER_H
     243             : 
     244             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10