| 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 : Fri Oct 29 15:49:00 2021 | 
|---|
| 13 | // Update Count     : 23 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "FixNames.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include <memory>                  // for unique_ptr | 
|---|
| 19 | #include <string>                  // for string, operator!=, operator== | 
|---|
| 20 |  | 
|---|
| 21 | #include "AST/Chain.hpp" | 
|---|
| 22 | #include "AST/Expr.hpp" | 
|---|
| 23 | #include "AST/Pass.hpp" | 
|---|
| 24 | #include "Common/PassVisitor.h" | 
|---|
| 25 | #include "Common/SemanticError.h"  // for SemanticError | 
|---|
| 26 | #include "FixMain.h"               // for FixMain | 
|---|
| 27 | #include "SymTab/Mangler.h"        // for Mangler | 
|---|
| 28 | #include "SynTree/LinkageSpec.h"   // for Cforall, isMangled | 
|---|
| 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 | 
|---|
| 36 | #include "CompilationState.h" | 
|---|
| 37 |  | 
|---|
| 38 | namespace CodeGen { | 
|---|
| 39 | class FixNames : public WithGuards { | 
|---|
| 40 | public: | 
|---|
| 41 | void postvisit( ObjectDecl *objectDecl ); | 
|---|
| 42 | void postvisit( FunctionDecl *functionDecl ); | 
|---|
| 43 |  | 
|---|
| 44 | void previsit( CompoundStmt *compoundStmt ); | 
|---|
| 45 | private: | 
|---|
| 46 | int scopeLevel = 1; | 
|---|
| 47 |  | 
|---|
| 48 | void fixDWT( DeclarationWithType *dwt ); | 
|---|
| 49 | }; | 
|---|
| 50 |  | 
|---|
| 51 | void fixNames( std::list< Declaration* > & translationUnit ) { | 
|---|
| 52 | PassVisitor<FixNames> fixer; | 
|---|
| 53 | acceptAll( translationUnit, fixer ); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | void FixNames::fixDWT( DeclarationWithType * dwt ) { | 
|---|
| 57 | if ( dwt->get_name() != "" ) { | 
|---|
| 58 | if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) { | 
|---|
| 59 | if (!useNewAST) { | 
|---|
| 60 | dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) ); | 
|---|
| 61 | } | 
|---|
| 62 | dwt->set_scopeLevel( scopeLevel ); | 
|---|
| 63 | } // if | 
|---|
| 64 | } // if | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | void FixNames::postvisit( ObjectDecl * objectDecl ) { | 
|---|
| 68 | fixDWT( objectDecl ); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | void FixNames::postvisit( FunctionDecl * functionDecl ) { | 
|---|
| 72 | fixDWT( functionDecl ); | 
|---|
| 73 |  | 
|---|
| 74 | if ( FixMain::isMain( functionDecl ) ) { | 
|---|
| 75 | int nargs = functionDecl->get_functionType()->get_parameters().size(); | 
|---|
| 76 | if( !(nargs == 0 || nargs == 2 || nargs == 3) ) { | 
|---|
| 77 | SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n"); | 
|---|
| 78 | } | 
|---|
| 79 | functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) ); | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void FixNames::previsit( CompoundStmt * ) { | 
|---|
| 84 | scopeLevel++; | 
|---|
| 85 | GuardAction( [this](){ scopeLevel--; } ); | 
|---|
| 86 | } | 
|---|
| 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 |  | 
|---|
| 139 | } // namespace CodeGen | 
|---|
| 140 |  | 
|---|
| 141 | // Local Variables: // | 
|---|
| 142 | // tab-width: 4 // | 
|---|
| 143 | // mode: c++ // | 
|---|
| 144 | // compile-command: "make install" // | 
|---|
| 145 | // End: // | 
|---|