Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/FixNames.cc

    r0270824 rf326f99  
    1414//
    1515
    16 #include <memory>
    17 
    1816#include "FixNames.h"
    1917#include "SynTree/Declaration.h"
     
    2220#include "SymTab/Mangler.h"
    2321#include "OperatorTable.h"
    24 
    25 extern std::unique_ptr<FunctionDecl> translation_unit_main_signature;
    2622
    2723namespace CodeGen {
     
    3228
    3329                virtual void visit( CompoundStmt *compoundStmt );
     30
    3431          private:
    3532                int scopeLevel = 1;
     
    3734                void fixDWT( DeclarationWithType *dwt );
    3835        };
    39 
    40         std::string mangle_main() {
    41                 FunctionType* main_type;
    42                 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl(
    43                         "main",
    44                         DeclarationNode::NoStorageClass,
    45                         LinkageSpec::Cforall,
    46                         main_type = new FunctionType( Type::Qualifiers(), true ),
    47                         nullptr, false, false
    48                 ) };
    49                 main_type->get_returnVals().push_back(
    50                         new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
    51                 );
    52 
    53                 auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
    54                 // std::cerr << name << std::endl;
    55                 return name;
    56         }
    57         std::string mangle_main_args() {
    58                 FunctionType* main_type;
    59                 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl(
    60                         "main",
    61                         DeclarationNode::NoStorageClass,
    62                         LinkageSpec::Cforall,
    63                         main_type = new FunctionType( Type::Qualifiers(), false ),
    64                         nullptr, false, false
    65                 ) };
    66                 main_type->get_returnVals().push_back(
    67                         new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
    68                 );
    69 
    70                 mainDecl->get_functionType()->get_parameters().push_back(
    71                         new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
    72                 );
    73 
    74                 mainDecl->get_functionType()->get_parameters().push_back(
    75                         new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0,
    76                         new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
    77                         nullptr )
    78                 );
    79 
    80                 auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
    81                 // std::cerr << name << std::endl;
    82                 return name;
    83         }
    84 
    85         bool is_main(const std::string& name) {
    86                 static std::string mains[] = {
    87                         mangle_main(),
    88                         mangle_main_args()
    89                 };
    90 
    91                 for(const auto& m : mains) {
    92                         if( name == m ) return true;
    93                 }
    94                 return false;
    95         }
    9636
    9737        void fixNames( std::list< Declaration* > translationUnit ) {
     
    11757                Visitor::visit( functionDecl );
    11858                fixDWT( functionDecl );
    119 
    120                 if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
    121                         if(translation_unit_main_signature) {
    122                                 throw SemanticError("Multiple definition of main routine\n", functionDecl);
    123                         }
    124 
    125                         functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), "0") ) ) );
    126                         translation_unit_main_signature.reset( functionDecl->clone() );
    127                 }
    12859        }
    12960
Note: See TracChangeset for help on using the changeset viewer.