source: src/CodeGen/FixNames.cc @ 8bea701

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 8bea701 was 16ba4a6, checked in by Fangren Yu <f37yu@…>, 4 years ago

factor out resolver calls in pre-resolution stage

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[51587aa]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[f326f99]7// FixNames.cc --
[51587aa]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[07de76b]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Dec 13 23:39:14 2019
13// Update Count     : 21
[51587aa]14//
[51b7345]15
16#include "FixNames.h"
[bf2438c]17
18#include <memory>                  // for unique_ptr
19#include <string>                  // for string, operator!=, operator==
20
[c50d54d]21#include "Common/PassVisitor.h"
[bf2438c]22#include "Common/SemanticError.h"  // for SemanticError
23#include "FixMain.h"               // for FixMain
24#include "SymTab/Mangler.h"        // for Mangler
[07de76b]25#include "SynTree/LinkageSpec.h"   // for Cforall, isMangled
[bf2438c]26#include "SynTree/Constant.h"      // for Constant
27#include "SynTree/Declaration.h"   // for FunctionDecl, ObjectDecl, Declarat...
28#include "SynTree/Expression.h"    // for ConstantExpr
29#include "SynTree/Label.h"         // for Label, noLabels
30#include "SynTree/Statement.h"     // for ReturnStmt, CompoundStmt
31#include "SynTree/Type.h"          // for Type, BasicType, Type::Qualifiers
32#include "SynTree/Visitor.h"       // for Visitor, acceptAll
[16ba4a6]33#include "CompilationState.h"
[0270824]34
[51b7345]35namespace CodeGen {
[c50d54d]36        class FixNames : public WithGuards {
[51587aa]37          public:
[c50d54d]38                void postvisit( ObjectDecl *objectDecl );
39                void postvisit( FunctionDecl *functionDecl );
[f326f99]40
[c50d54d]41                void previsit( CompoundStmt *compoundStmt );
[f326f99]42          private:
43                int scopeLevel = 1;
44
45                void fixDWT( DeclarationWithType *dwt );
[51587aa]46        };
47
[0270824]48        std::string mangle_main() {
49                FunctionType* main_type;
[68fe077a]50                std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,
[a7c90d4]51                                                                                                                                   main_type = new FunctionType( Type::Qualifiers(), true ), nullptr )
52                                };
[bf2438c]53                main_type->get_returnVals().push_back(
[68fe077a]54                        new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
[0270824]55                );
56
[dd020c0]57                auto && name = SymTab::Mangler::mangle( mainDecl.get() );
[0270824]58                // std::cerr << name << std::endl;
[6d611fb]59                return std::move(name);
[0270824]60        }
61        std::string mangle_main_args() {
62                FunctionType* main_type;
[bf2438c]63                std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,
[a7c90d4]64                                                                                                                                   main_type = new FunctionType( Type::Qualifiers(), false ), nullptr )
65                                };
[bf2438c]66                main_type->get_returnVals().push_back(
[68fe077a]67                        new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
[0270824]68                );
69
[fa4c094]70                main_type->get_parameters().push_back(
[68fe077a]71                        new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
[0270824]72                );
73
[fa4c094]74                main_type->get_parameters().push_back(
[bf2438c]75                        new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
76                        new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
[0270824]77                        nullptr )
78                );
79
80                auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
81                // std::cerr << name << std::endl;
[6d611fb]82                return std::move(name);
[0270824]83        }
84
85        bool is_main(const std::string& name) {
[bf2438c]86                static std::string mains[] = {
87                        mangle_main(),
[0270824]88                        mangle_main_args()
89                };
90
91                for(const auto& m : mains) {
92                        if( name == m ) return true;
93                }
94                return false;
95        }
96
[c50d54d]97        void fixNames( std::list< Declaration* > & translationUnit ) {
98                PassVisitor<FixNames> fixer;
[51587aa]99                acceptAll( translationUnit, fixer );
100        }
101
[c50d54d]102        void FixNames::fixDWT( DeclarationWithType * dwt ) {
[51587aa]103                if ( dwt->get_name() != "" ) {
[fa4805f]104                        if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) {
[16ba4a6]105                                if (!useNewAST) {
106                                        dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) );
107                                }
[f326f99]108                                dwt->set_scopeLevel( scopeLevel );
[51587aa]109                        } // if
110                } // if
111        }
112
[c50d54d]113        void FixNames::postvisit( ObjectDecl * objectDecl ) {
[51587aa]114                fixDWT( objectDecl );
115        }
116
[c50d54d]117        void FixNames::postvisit( FunctionDecl * functionDecl ) {
[51587aa]118                fixDWT( functionDecl );
[0270824]119
120                if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
[13de47bc]121                        int nargs = functionDecl->get_functionType()->get_parameters().size();
122                        if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
[a16764a6]123                                SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n");
[0270824]124                        }
[ba3706f]125                        functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) );
[13de47bc]126                        CodeGen::FixMain::registerMain( functionDecl );
[0270824]127                }
[51587aa]128        }
[f326f99]129
[c50d54d]130        void FixNames::previsit( CompoundStmt * ) {
[f326f99]131                scopeLevel++;
[c50d54d]132                GuardAction( [this](){ scopeLevel--; } );
[f326f99]133        }
[51b7345]134} // namespace CodeGen
[51587aa]135
136// Local Variables: //
137// tab-width: 4 //
138// mode: c++ //
139// compile-command: "make install" //
140// End: //
Note: See TracBrowser for help on using the repository browser.