| 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 | // FunctionDecl.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 : Mon Dec 16 15:11:20 2019 | 
|---|
| 13 | // Update Count     : 77 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <cassert>               // for assert | 
|---|
| 17 | #include <list>                  // for list | 
|---|
| 18 | #include <ostream>               // for operator<<, ostream, basic_ostream | 
|---|
| 19 | #include <string>                // for operator<<, string, char_traits, ope... | 
|---|
| 20 |  | 
|---|
| 21 | #include "Attribute.h"           // for Attribute | 
|---|
| 22 | #include "CodeGen/FixMain.h"     // for FixMain | 
|---|
| 23 | #include "Common/utility.h"      // for maybeClone, printAll | 
|---|
| 24 | #include "Declaration.h"         // for FunctionDecl, FunctionDecl::Parent | 
|---|
| 25 | #include "Expression.h" | 
|---|
| 26 | #include "LinkageSpec.h"         // for Spec, linkageName, Cforall | 
|---|
| 27 | #include "Statement.h"           // for CompoundStmt | 
|---|
| 28 | #include "Type.h"                // for Type, FunctionType, Type::FuncSpecif... | 
|---|
| 29 | #include "DeclReplacer.h" | 
|---|
| 30 |  | 
|---|
| 31 | extern bool translation_unit_nomain; | 
|---|
| 32 |  | 
|---|
| 33 | FunctionDecl::FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements, std::list< Attribute * > attributes, Type::FuncSpecifiers fs ) | 
|---|
| 34 | : Parent( name, scs, linkage, attributes, fs ), type( type ), statements( statements ) { | 
|---|
| 35 | // hack forcing the function "main" to have Cforall linkage to replace main even if it is inside an extern | 
|---|
| 36 | if ( name == "main" ) { | 
|---|
| 37 | set_linkage( CodeGen::FixMain::mainLinkage() ); | 
|---|
| 38 | } // if | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | FunctionDecl::FunctionDecl( const FunctionDecl &other ) | 
|---|
| 42 | : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ) { | 
|---|
| 43 |  | 
|---|
| 44 | DeclReplacer::DeclMap declMap; | 
|---|
| 45 | for ( auto p : group_iterate( other.type->parameters, type->parameters ) ) { | 
|---|
| 46 | declMap[ std::get<0>(p) ] = std::get<1>(p); | 
|---|
| 47 | } | 
|---|
| 48 | for ( auto p : group_iterate( other.type->returnVals, type->returnVals ) ) { | 
|---|
| 49 | declMap[ std::get<0>(p) ] = std::get<1>(p); | 
|---|
| 50 | } | 
|---|
| 51 | if ( ! declMap.empty() ) { | 
|---|
| 52 | DeclReplacer::replace( this, declMap ); | 
|---|
| 53 | } | 
|---|
| 54 | cloneAll( other.withExprs, withExprs ); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | FunctionDecl::~FunctionDecl() { | 
|---|
| 58 | delete type; | 
|---|
| 59 | delete statements; | 
|---|
| 60 | deleteAll( withExprs ); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | FunctionDecl * FunctionDecl::newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements ) { | 
|---|
| 64 | return new FunctionDecl( name, Type::StorageClasses(), LinkageSpec::C, type, statements ); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | void FunctionDecl::print( std::ostream &os, Indenter indent ) const { | 
|---|
| 68 | using std::endl; | 
|---|
| 69 | using std::string; | 
|---|
| 70 |  | 
|---|
| 71 | if ( name != "" ) { | 
|---|
| 72 | os << name << ": "; | 
|---|
| 73 | } // if | 
|---|
| 74 | if ( linkage != LinkageSpec::Cforall ) { | 
|---|
| 75 | os << LinkageSpec::name( linkage ) << " "; | 
|---|
| 76 | } // if | 
|---|
| 77 |  | 
|---|
| 78 | printAll( attributes, os, indent ); | 
|---|
| 79 |  | 
|---|
| 80 | get_storageClasses().print( os ); | 
|---|
| 81 | get_funcSpec().print( os ); | 
|---|
| 82 |  | 
|---|
| 83 | if ( type ) { | 
|---|
| 84 | type->print( os, indent ); | 
|---|
| 85 | } else { | 
|---|
| 86 | os << "untyped entity "; | 
|---|
| 87 | } // if | 
|---|
| 88 |  | 
|---|
| 89 | if ( statements ) { | 
|---|
| 90 | os << indent << "... with body" << endl << indent+1; | 
|---|
| 91 | statements->print( os, indent+1 ); | 
|---|
| 92 | } // if | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | void FunctionDecl::printShort( std::ostream &os, Indenter indent ) const { | 
|---|
| 96 | using std::endl; | 
|---|
| 97 | using std::string; | 
|---|
| 98 |  | 
|---|
| 99 | if ( name != "" ) { | 
|---|
| 100 | os << name << ": "; | 
|---|
| 101 | } // if | 
|---|
| 102 |  | 
|---|
| 103 | get_storageClasses().print( os ); | 
|---|
| 104 | get_funcSpec().print( os ); | 
|---|
| 105 |  | 
|---|
| 106 | if ( type ) { | 
|---|
| 107 | type->print( os, indent ); | 
|---|
| 108 | } else { | 
|---|
| 109 | os << "untyped entity "; | 
|---|
| 110 | } // if | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | // Local Variables: // | 
|---|
| 114 | // tab-width: 4 // | 
|---|
| 115 | // mode: c++ // | 
|---|
| 116 | // compile-command: "make install" // | 
|---|
| 117 | // End: // | 
|---|