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 : Peter A. Buhr |
---|
12 | // Last Modified On : Fri Dec 13 23:39:14 2019 |
---|
13 | // Update Count : 21 |
---|
14 | // |
---|
15 | |
---|
16 | #include "FixNames.h" |
---|
17 | |
---|
18 | #include <memory> // for unique_ptr |
---|
19 | #include <string> // for string, operator!=, operator== |
---|
20 | |
---|
21 | #include "Common/PassVisitor.h" |
---|
22 | #include "Common/SemanticError.h" // for SemanticError |
---|
23 | #include "FixMain.h" // for FixMain |
---|
24 | #include "SymTab/Mangler.h" // for Mangler |
---|
25 | #include "SynTree/LinkageSpec.h" // for Cforall, isMangled |
---|
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 |
---|
33 | #include "CompilationState.h" |
---|
34 | |
---|
35 | namespace CodeGen { |
---|
36 | class FixNames : public WithGuards { |
---|
37 | public: |
---|
38 | void postvisit( ObjectDecl *objectDecl ); |
---|
39 | void postvisit( FunctionDecl *functionDecl ); |
---|
40 | |
---|
41 | void previsit( CompoundStmt *compoundStmt ); |
---|
42 | private: |
---|
43 | int scopeLevel = 1; |
---|
44 | |
---|
45 | void fixDWT( DeclarationWithType *dwt ); |
---|
46 | }; |
---|
47 | |
---|
48 | std::string mangle_main() { |
---|
49 | FunctionType* main_type; |
---|
50 | std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall, |
---|
51 | main_type = new FunctionType( Type::Qualifiers(), true ), nullptr ) |
---|
52 | }; |
---|
53 | main_type->get_returnVals().push_back( |
---|
54 | new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) |
---|
55 | ); |
---|
56 | |
---|
57 | auto && name = SymTab::Mangler::mangle( mainDecl.get() ); |
---|
58 | // std::cerr << name << std::endl; |
---|
59 | return std::move(name); |
---|
60 | } |
---|
61 | std::string mangle_main_args() { |
---|
62 | FunctionType* main_type; |
---|
63 | std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall, |
---|
64 | main_type = new FunctionType( Type::Qualifiers(), false ), nullptr ) |
---|
65 | }; |
---|
66 | main_type->get_returnVals().push_back( |
---|
67 | new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) |
---|
68 | ); |
---|
69 | |
---|
70 | main_type->get_parameters().push_back( |
---|
71 | new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) |
---|
72 | ); |
---|
73 | |
---|
74 | main_type->get_parameters().push_back( |
---|
75 | new ObjectDecl( "", Type::StorageClasses(), 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 std::move(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 | } |
---|
96 | |
---|
97 | void fixNames( std::list< Declaration* > & translationUnit ) { |
---|
98 | PassVisitor<FixNames> fixer; |
---|
99 | acceptAll( translationUnit, fixer ); |
---|
100 | } |
---|
101 | |
---|
102 | void FixNames::fixDWT( DeclarationWithType * dwt ) { |
---|
103 | if ( dwt->get_name() != "" ) { |
---|
104 | if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) { |
---|
105 | if (!useNewAST) { |
---|
106 | dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) ); |
---|
107 | } |
---|
108 | dwt->set_scopeLevel( scopeLevel ); |
---|
109 | } // if |
---|
110 | } // if |
---|
111 | } |
---|
112 | |
---|
113 | void FixNames::postvisit( ObjectDecl * objectDecl ) { |
---|
114 | fixDWT( objectDecl ); |
---|
115 | } |
---|
116 | |
---|
117 | void FixNames::postvisit( FunctionDecl * functionDecl ) { |
---|
118 | fixDWT( functionDecl ); |
---|
119 | |
---|
120 | if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) { |
---|
121 | int nargs = functionDecl->get_functionType()->get_parameters().size(); |
---|
122 | if( !(nargs == 0 || nargs == 2 || nargs == 3) ) { |
---|
123 | SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n"); |
---|
124 | } |
---|
125 | functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) ); |
---|
126 | CodeGen::FixMain::registerMain( functionDecl ); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | void FixNames::previsit( CompoundStmt * ) { |
---|
131 | scopeLevel++; |
---|
132 | GuardAction( [this](){ scopeLevel--; } ); |
---|
133 | } |
---|
134 | } // namespace CodeGen |
---|
135 | |
---|
136 | // Local Variables: // |
---|
137 | // tab-width: 4 // |
---|
138 | // mode: c++ // |
---|
139 | // compile-command: "make install" // |
---|
140 | // End: // |
---|