source: src/CodeGen/FixNames.cc @ f42fc13

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since f42fc13 was f42fc13, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Reorganize FixNames/FixMain? to avoid storing main_signature for so long.

  • Property mode set to 100644
File size: 2.7 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 : Fri Oct 29 14:06:00 2021
13// Update Count     : 22
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
35namespace 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        void fixNames( std::list< Declaration* > & translationUnit ) {
49                PassVisitor<FixNames> fixer;
50                acceptAll( translationUnit, fixer );
51        }
52
53        void FixNames::fixDWT( DeclarationWithType * dwt ) {
54                if ( dwt->get_name() != "" ) {
55                        if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) {
56                                if (!useNewAST) {
57                                        dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) );
58                                }
59                                dwt->set_scopeLevel( scopeLevel );
60                        } // if
61                } // if
62        }
63
64        void FixNames::postvisit( ObjectDecl * objectDecl ) {
65                fixDWT( objectDecl );
66        }
67
68        void FixNames::postvisit( FunctionDecl * functionDecl ) {
69                fixDWT( functionDecl );
70
71                if ( FixMain::isMain( functionDecl ) ) {
72                        int nargs = functionDecl->get_functionType()->get_parameters().size();
73                        if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
74                                SemanticError(functionDecl, "Main expected to have 0, 2 or 3 arguments\n");
75                        }
76                        functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) );
77                }
78        }
79
80        void FixNames::previsit( CompoundStmt * ) {
81                scopeLevel++;
82                GuardAction( [this](){ scopeLevel--; } );
83        }
84} // namespace CodeGen
85
86// Local Variables: //
87// tab-width: 4 //
88// mode: c++ //
89// compile-command: "make install" //
90// End: //
Note: See TracBrowser for help on using the repository browser.