source: src/CodeGen/FixNames.cc@ 2472a19

new-env with_gc
Last change on this file since 2472a19 was ff29f08, checked in by Aaron Moss <a3moss@…>, 7 years ago

Merge remote-tracking branch 'origin/master' into with_gc

  • Property mode set to 100644
File size: 4.4 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 : Andrew Beach
12// Last Modified On : Wed Jun 28 15:26:00 2017
13// Update Count : 20
14//
15
16#include "FixNames.h"
17
18#include <string> // for string, operator!=, operator==
19
20#include "Common/PassVisitor.h"
21#include "Common/SemanticError.h" // for SemanticError
22#include "FixMain.h" // for FixMain
23#include "Parser/LinkageSpec.h" // for Cforall, isMangled
24#include "SymTab/Mangler.h" // for Mangler
25#include "SynTree/Constant.h" // for Constant
26#include "SynTree/Declaration.h" // for FunctionDecl, ObjectDecl, Declarat...
27#include "SynTree/Expression.h" // for ConstantExpr
28#include "SynTree/Label.h" // for Label, noLabels
29#include "SynTree/Statement.h" // for ReturnStmt, CompoundStmt
30#include "SynTree/Type.h" // for Type, BasicType, Type::Qualifiers
31#include "SynTree/Visitor.h" // for Visitor, acceptAll
32
33namespace CodeGen {
34 class FixNames : public WithGuards {
35 public:
36 void postvisit( ObjectDecl *objectDecl );
37 void postvisit( FunctionDecl *functionDecl );
38
39 void previsit( CompoundStmt *compoundStmt );
40 private:
41 int scopeLevel = 1;
42
43 void fixDWT( DeclarationWithType *dwt );
44 };
45
46 std::string mangle_main() {
47 FunctionType* main_type;
48 FunctionDecl* mainDecl = new FunctionDecl{
49 "main", Type::StorageClasses(), LinkageSpec::Cforall,
50 main_type = new FunctionType{ Type::Qualifiers(), true }, nullptr };
51 main_type->get_returnVals().push_back(
52 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
53 );
54
55 auto && name = SymTab::Mangler::mangle( mainDecl );
56 // std::cerr << name << std::endl;
57 return std::move(name);
58 }
59 std::string mangle_main_args() {
60 FunctionType* main_type;
61 FunctionDecl* mainDecl = new FunctionDecl{
62 "main", Type::StorageClasses(), LinkageSpec::Cforall,
63 main_type = new FunctionType{ Type::Qualifiers(), false }, nullptr };
64 main_type->get_returnVals().push_back(
65 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
66 );
67
68 main_type->get_parameters().push_back(
69 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
70 );
71
72 main_type->get_parameters().push_back(
73 new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0,
74 new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
75 nullptr )
76 );
77
78 auto&& name = SymTab::Mangler::mangle( mainDecl );
79 // std::cerr << name << std::endl;
80 return std::move(name);
81 }
82
83 bool is_main(const std::string& name) {
84 static std::string mains[] = {
85 mangle_main(),
86 mangle_main_args()
87 };
88
89 for(const auto& m : mains) {
90 if( name == m ) return true;
91 }
92 return false;
93 }
94
95 void fixNames( std::list< Declaration* > & translationUnit ) {
96 PassVisitor<FixNames> fixer;
97 acceptAll( translationUnit, fixer );
98 }
99
100 void FixNames::fixDWT( DeclarationWithType * dwt ) {
101 if ( dwt->get_name() != "" ) {
102 if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) {
103 dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) );
104 dwt->set_scopeLevel( scopeLevel );
105 } // if
106 } // if
107 }
108
109 void FixNames::postvisit( ObjectDecl * objectDecl ) {
110 fixDWT( objectDecl );
111 }
112
113 void FixNames::postvisit( FunctionDecl * functionDecl ) {
114 fixDWT( functionDecl );
115
116 if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
117 int nargs = functionDecl->get_functionType()->get_parameters().size();
118 if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
119 SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n");
120 }
121 functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) );
122 CodeGen::FixMain::registerMain( functionDecl );
123 }
124 }
125
126 void FixNames::previsit( CompoundStmt * ) {
127 scopeLevel++;
128 GuardAction( [this](){ scopeLevel--; } );
129 }
130} // namespace CodeGen
131
132// Local Variables: //
133// tab-width: 4 //
134// mode: c++ //
135// compile-command: "make install" //
136// End: //
Note: See TracBrowser for help on using the repository browser.