source: src/CodeGen/FixNames.cc@ 70c2df8

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 70c2df8 was 68fe077a, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

move type StorageClasses from DeclarationNode to Type

  • Property mode set to 100644
File size: 4.1 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 : Peter A. Buhr
12// Last Modified On : Thu Mar 16 07:50:30 2017
13// Update Count : 16
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( "main", Type::StorageClasses(), LinkageSpec::Cforall,
42 main_type = new FunctionType( Type::Qualifiers(), true ), nullptr )
43 };
44 main_type->get_returnVals().push_back(
45 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
46 );
47
48 auto && name = SymTab::Mangler::mangle( mainDecl.get() );
49 // std::cerr << name << std::endl;
50 return name;
51 }
52 std::string mangle_main_args() {
53 FunctionType* main_type;
54 std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,
55 main_type = new FunctionType( Type::Qualifiers(), false ), nullptr )
56 };
57 main_type->get_returnVals().push_back(
58 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
59 );
60
61 mainDecl->get_functionType()->get_parameters().push_back(
62 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
63 );
64
65 mainDecl->get_functionType()->get_parameters().push_back(
66 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
67 new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
68 nullptr )
69 );
70
71 auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
72 // std::cerr << name << std::endl;
73 return name;
74 }
75
76 bool is_main(const std::string& name) {
77 static std::string mains[] = {
78 mangle_main(),
79 mangle_main_args()
80 };
81
82 for(const auto& m : mains) {
83 if( name == m ) return true;
84 }
85 return false;
86 }
87
88 void fixNames( std::list< Declaration* > translationUnit ) {
89 FixNames fixer;
90 acceptAll( translationUnit, fixer );
91 }
92
93 void FixNames::fixDWT( DeclarationWithType *dwt ) {
94 if ( dwt->get_name() != "" ) {
95 if ( LinkageSpec::isDecoratable( dwt->get_linkage() ) ) {
96 dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) );
97 dwt->set_scopeLevel( scopeLevel );
98 } // if
99 } // if
100 }
101
102 void FixNames::visit( ObjectDecl *objectDecl ) {
103 Visitor::visit( objectDecl );
104 fixDWT( objectDecl );
105 }
106
107 void FixNames::visit( FunctionDecl *functionDecl ) {
108 Visitor::visit( functionDecl );
109 fixDWT( functionDecl );
110
111 if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
112 int nargs = functionDecl->get_functionType()->get_parameters().size();
113 if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
114 throw SemanticError("Main expected to have 0, 2 or 3 arguments\n", functionDecl);
115 }
116 functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), "0") ) ) );
117 CodeGen::FixMain::registerMain( functionDecl );
118 }
119 }
120
121 void FixNames::visit( CompoundStmt *compoundStmt ) {
122 scopeLevel++;
123 Visitor::visit( compoundStmt );
124 scopeLevel--;
125 }
126} // namespace CodeGen
127
128// Local Variables: //
129// tab-width: 4 //
130// mode: c++ //
131// compile-command: "make install" //
132// End: //
Note: See TracBrowser for help on using the repository browser.