[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
|
---|
[f42fc13] | 11 | // Last Modified By : Andrew Beach
|
---|
[0c577f7] | 12 | // Last Modified On : Fri Oct 29 15:49:00 2021
|
---|
| 13 | // Update Count : 23
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include "FixNames.h"
|
---|
[bf2438c] | 17 |
|
---|
| 18 | #include <memory> // for unique_ptr
|
---|
| 19 | #include <string> // for string, operator!=, operator==
|
---|
| 20 |
|
---|
[0c577f7] | 21 | #include "AST/Chain.hpp"
|
---|
| 22 | #include "AST/Expr.hpp"
|
---|
| 23 | #include "AST/Pass.hpp"
|
---|
[c50d54d] | 24 | #include "Common/PassVisitor.h"
|
---|
[bf2438c] | 25 | #include "Common/SemanticError.h" // for SemanticError
|
---|
| 26 | #include "FixMain.h" // for FixMain
|
---|
| 27 | #include "SymTab/Mangler.h" // for Mangler
|
---|
[07de76b] | 28 | #include "SynTree/LinkageSpec.h" // for Cforall, isMangled
|
---|
[bf2438c] | 29 | #include "SynTree/Constant.h" // for Constant
|
---|
| 30 | #include "SynTree/Declaration.h" // for FunctionDecl, ObjectDecl, Declarat...
|
---|
| 31 | #include "SynTree/Expression.h" // for ConstantExpr
|
---|
| 32 | #include "SynTree/Label.h" // for Label, noLabels
|
---|
| 33 | #include "SynTree/Statement.h" // for ReturnStmt, CompoundStmt
|
---|
| 34 | #include "SynTree/Type.h" // for Type, BasicType, Type::Qualifiers
|
---|
| 35 | #include "SynTree/Visitor.h" // for Visitor, acceptAll
|
---|
[16ba4a6f] | 36 | #include "CompilationState.h"
|
---|
[0270824] | 37 |
|
---|
[51b73452] | 38 | namespace CodeGen {
|
---|
[c50d54d] | 39 | class FixNames : public WithGuards {
|
---|
[51587aa] | 40 | public:
|
---|
[c50d54d] | 41 | void postvisit( ObjectDecl *objectDecl );
|
---|
| 42 | void postvisit( FunctionDecl *functionDecl );
|
---|
[f326f99] | 43 |
|
---|
[c50d54d] | 44 | void previsit( CompoundStmt *compoundStmt );
|
---|
[f326f99] | 45 | private:
|
---|
| 46 | int scopeLevel = 1;
|
---|
| 47 |
|
---|
| 48 | void fixDWT( DeclarationWithType *dwt );
|
---|
[51587aa] | 49 | };
|
---|
| 50 |
|
---|
[c50d54d] | 51 | void fixNames( std::list< Declaration* > & translationUnit ) {
|
---|
| 52 | PassVisitor<FixNames> fixer;
|
---|
[51587aa] | 53 | acceptAll( translationUnit, fixer );
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[c50d54d] | 56 | void FixNames::fixDWT( DeclarationWithType * dwt ) {
|
---|
[51587aa] | 57 | if ( dwt->get_name() != "" ) {
|
---|
[fa4805f] | 58 | if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) {
|
---|
[16ba4a6f] | 59 | if (!useNewAST) {
|
---|
| 60 | dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) );
|
---|
| 61 | }
|
---|
[f326f99] | 62 | dwt->set_scopeLevel( scopeLevel );
|
---|
[51587aa] | 63 | } // if
|
---|
| 64 | } // if
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[c50d54d] | 67 | void FixNames::postvisit( ObjectDecl * objectDecl ) {
|
---|
[51587aa] | 68 | fixDWT( objectDecl );
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[c50d54d] | 71 | void FixNames::postvisit( FunctionDecl * functionDecl ) {
|
---|
[51587aa] | 72 | fixDWT( functionDecl );
|
---|
[0270824] | 73 |
|
---|
[f42fc13] | 74 | if ( FixMain::isMain( functionDecl ) ) {
|
---|
[13de47bc] | 75 | int nargs = functionDecl->get_functionType()->get_parameters().size();
|
---|
| 76 | if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
|
---|
[a16764a6] | 77 | SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n");
|
---|
[0270824] | 78 | }
|
---|
[ba3706f] | 79 | functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) );
|
---|
[0270824] | 80 | }
|
---|
[51587aa] | 81 | }
|
---|
[f326f99] | 82 |
|
---|
[c50d54d] | 83 | void FixNames::previsit( CompoundStmt * ) {
|
---|
[f326f99] | 84 | scopeLevel++;
|
---|
[c50d54d] | 85 | GuardAction( [this](){ scopeLevel--; } );
|
---|
[f326f99] | 86 | }
|
---|
[0c577f7] | 87 |
|
---|
| 88 | /// Does work with the main function and scopeLevels.
|
---|
| 89 | class FixNames_new : public ast::WithGuards {
|
---|
| 90 | int scopeLevel = 1;
|
---|
| 91 |
|
---|
| 92 | bool shouldSetScopeLevel( const ast::DeclWithType * dwt ) {
|
---|
| 93 | return !dwt->name.empty() && dwt->linkage.is_mangled
|
---|
| 94 | && dwt->scopeLevel != scopeLevel;
|
---|
| 95 | }
|
---|
| 96 | public:
|
---|
| 97 | const ast::ObjectDecl *postvisit( const ast::ObjectDecl *objectDecl ) {
|
---|
| 98 | if ( shouldSetScopeLevel( objectDecl ) ) {
|
---|
| 99 | return ast::mutate_field( objectDecl, &ast::ObjectDecl::scopeLevel, scopeLevel );
|
---|
| 100 | }
|
---|
| 101 | return objectDecl;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | const ast::FunctionDecl *postvisit( const ast::FunctionDecl *functionDecl ) {
|
---|
| 105 | // This store is used to ensure a maximum of one call to mutate.
|
---|
| 106 | ast::FunctionDecl * mutDecl = nullptr;
|
---|
| 107 |
|
---|
| 108 | if ( shouldSetScopeLevel( functionDecl ) ) {
|
---|
| 109 | mutDecl = ast::mutate( functionDecl );
|
---|
| 110 | mutDecl->scopeLevel = scopeLevel;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | if ( FixMain::isMain( functionDecl ) ) {
|
---|
| 114 | if ( !mutDecl ) { mutDecl = ast::mutate( functionDecl ); }
|
---|
| 115 |
|
---|
| 116 | int nargs = mutDecl->params.size();
|
---|
| 117 | if ( 0 != nargs && 2 != nargs && 3 != nargs ) {
|
---|
| 118 | SemanticError( functionDecl, "Main expected to have 0, 2 or 3 arguments\n" );
|
---|
| 119 | }
|
---|
| 120 | ast::chain_mutate( mutDecl->stmts )->kids.push_back(
|
---|
| 121 | new ast::ReturnStmt(
|
---|
| 122 | mutDecl->location,
|
---|
| 123 | ast::ConstantExpr::from_int( mutDecl->location, 0 )
|
---|
| 124 | )
|
---|
| 125 | );
|
---|
| 126 | }
|
---|
| 127 | return mutDecl ? mutDecl : functionDecl;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | void previsit( const ast::CompoundStmt * ) {
|
---|
| 131 | GuardValue( scopeLevel ) += 1;
|
---|
| 132 | }
|
---|
| 133 | };
|
---|
| 134 |
|
---|
| 135 | void fixNames( ast::TranslationUnit & translationUnit ) {
|
---|
| 136 | ast::Pass<FixNames_new>::run( translationUnit );
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[51b73452] | 139 | } // namespace CodeGen
|
---|
[51587aa] | 140 |
|
---|
| 141 | // Local Variables: //
|
---|
| 142 | // tab-width: 4 //
|
---|
| 143 | // mode: c++ //
|
---|
| 144 | // compile-command: "make install" //
|
---|
| 145 | // End: //
|
---|