source: src/CodeGen/FixNames.cc @ aaa1a99a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since aaa1a99a was aaa1a99a, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Fixed copy paste error

  • Property mode set to 100644
File size: 4.2 KB
Line 
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//
7// FixNames.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Mon Apr 11 15:38:10 2016
13// Update Count     : 1
14//
15
16#include <memory>
17
18#include "FixNames.h"
19#include "SynTree/Declaration.h"
20#include "SynTree/Expression.h"
21#include "SynTree/Visitor.h"
22#include "SymTab/Mangler.h"
23#include "OperatorTable.h"
24#include "FixMain.h"
25
26namespace CodeGen {
27        class FixNames : public Visitor {
28          public:
29                virtual void visit( ObjectDecl *objectDecl );
30                virtual void visit( FunctionDecl *functionDecl );
31
32                virtual void visit( CompoundStmt *compoundStmt );
33          private:
34                int scopeLevel = 1;
35
36                void fixDWT( DeclarationWithType *dwt );
37        };
38
39        std::string mangle_main() {
40                FunctionType* main_type;
41                std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl(
42                        "main", 
43                        DeclarationNode::NoStorageClass, 
44                        LinkageSpec::Cforall, 
45                        main_type = new FunctionType( Type::Qualifiers(), true ), 
46                        nullptr, false, false
47                ) };
48                main_type->get_returnVals().push_back( 
49                        new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
50                );
51
52                auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
53                // std::cerr << name << std::endl;
54                return name;
55        }
56        std::string mangle_main_args() {
57                FunctionType* main_type;
58                std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl(
59                        "main", 
60                        DeclarationNode::NoStorageClass, 
61                        LinkageSpec::Cforall, 
62                        main_type = new FunctionType( Type::Qualifiers(), false ), 
63                        nullptr, false, false
64                ) };
65                main_type->get_returnVals().push_back( 
66                        new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
67                );
68
69                mainDecl->get_functionType()->get_parameters().push_back( 
70                        new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
71                );
72
73                mainDecl->get_functionType()->get_parameters().push_back( 
74                        new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, 
75                        new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ), 
76                        nullptr )
77                );
78
79                auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
80                // std::cerr << name << std::endl;
81                return name;
82        }
83
84        bool is_main(const std::string& name) {
85                static std::string mains[] = { 
86                        mangle_main(), 
87                        mangle_main_args()
88                };
89
90                for(const auto& m : mains) {
91                        if( name == m ) return true;
92                }
93                return false;
94        }
95
96        void fixNames( std::list< Declaration* > translationUnit ) {
97                FixNames fixer;
98                acceptAll( translationUnit, fixer );
99        }
100
101        void FixNames::fixDWT( DeclarationWithType *dwt ) {
102                if ( dwt->get_name() != "" ) {
103                        if ( LinkageSpec::isDecoratable( dwt->get_linkage() ) ) {
104                                dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) );
105                                dwt->set_scopeLevel( scopeLevel );
106                        } // if
107                } // if
108        }
109
110        void FixNames::visit( ObjectDecl *objectDecl ) {
111                Visitor::visit( objectDecl );
112                fixDWT( objectDecl );
113        }
114
115        void FixNames::visit( FunctionDecl *functionDecl ) {
116                Visitor::visit( functionDecl );
117                fixDWT( functionDecl );
118
119                if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
120                        int nargs = functionDecl->get_functionType()->get_parameters().size();
121                        if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
122                                throw SemanticError("Main expected to have 0, 2 or 3 arguments\n", functionDecl); 
123                        }
124                        functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), "0") ) ) );
125                        CodeGen::FixMain::registerMain( functionDecl );
126                }
127        }
128
129        void FixNames::visit( CompoundStmt *compoundStmt ) {
130                scopeLevel++;
131                Visitor::visit( compoundStmt );
132                scopeLevel--;
133        }
134} // namespace CodeGen
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.