[0dd3a2f] | 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 | //
|
---|
[9243a501] | 7 | // FunctionDecl.cc --
|
---|
[0dd3a2f] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[8b7ee09] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[ddfd945] | 12 | // Last Modified On : Thu Mar 16 08:33:41 2017
|
---|
| 13 | // Update Count : 74
|
---|
[0dd3a2f] | 14 | //
|
---|
| 15 |
|
---|
[ea6332d] | 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...
|
---|
[51b73452] | 20 |
|
---|
[ea6332d] | 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 "Parser/LinkageSpec.h" // for Spec, linkageName, Cforall
|
---|
| 26 | #include "Statement.h" // for CompoundStmt
|
---|
| 27 | #include "Type.h" // for Type, FunctionType, Type::FuncSpecif...
|
---|
[97dbc09] | 28 | #include "VarExprReplacer.h"
|
---|
[51b73452] | 29 |
|
---|
[3fe34ae] | 30 | extern bool translation_unit_nomain;
|
---|
| 31 |
|
---|
[ddfd945] | 32 | FunctionDecl::FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements, std::list< Attribute * > attributes, Type::FuncSpecifiers fs )
|
---|
[a7c90d4] | 33 | : Parent( name, scs, linkage, attributes, fs ), type( type ), statements( statements ) {
|
---|
[dd020c0] | 34 | // hack forcing the function "main" to have Cforall linkage to replace main even if it is inside an extern
|
---|
[0dd3a2f] | 35 | if ( name == "main" ) {
|
---|
[13de47bc] | 36 | set_linkage( CodeGen::FixMain::mainLinkage() );
|
---|
[0dd3a2f] | 37 | } // if
|
---|
[51b73452] | 38 | }
|
---|
| 39 |
|
---|
| 40 | FunctionDecl::FunctionDecl( const FunctionDecl &other )
|
---|
[a7c90d4] | 41 | : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ) {
|
---|
[97dbc09] | 42 |
|
---|
| 43 | VarExprReplacer::DeclMap declMap;
|
---|
| 44 | for ( auto p : group_iterate( other.type->parameters, type->parameters ) ) {
|
---|
| 45 | declMap[ std::get<0>(p) ] = std::get<1>(p);
|
---|
| 46 | }
|
---|
| 47 | for ( auto p : group_iterate( other.type->returnVals, type->returnVals ) ) {
|
---|
| 48 | declMap[ std::get<0>(p) ] = std::get<1>(p);
|
---|
| 49 | }
|
---|
| 50 | if ( ! declMap.empty() ) {
|
---|
[c0b9f5d] | 51 | VarExprReplacer::replace( this, declMap );
|
---|
[97dbc09] | 52 | }
|
---|
[51b73452] | 53 | }
|
---|
| 54 |
|
---|
[c11e31c] | 55 | FunctionDecl::~FunctionDecl() {
|
---|
[0dd3a2f] | 56 | delete type;
|
---|
| 57 | delete statements;
|
---|
[51b73452] | 58 | }
|
---|
| 59 |
|
---|
[75626a1] | 60 | FunctionDecl * FunctionDecl::newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements ) {
|
---|
| 61 | return new FunctionDecl( name, Type::StorageClasses(), LinkageSpec::C, type, statements );
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[50377a4] | 64 | void FunctionDecl::print( std::ostream &os, Indenter indent ) const {
|
---|
[0dd3a2f] | 65 | using std::endl;
|
---|
| 66 | using std::string;
|
---|
[9243a501] | 67 |
|
---|
[50377a4] | 68 | if ( name != "" ) {
|
---|
| 69 | os << name << ": ";
|
---|
[0dd3a2f] | 70 | } // if
|
---|
[50377a4] | 71 | if ( linkage != LinkageSpec::Cforall ) {
|
---|
| 72 | os << LinkageSpec::linkageName( linkage ) << " ";
|
---|
[0dd3a2f] | 73 | } // if
|
---|
[7baed7d] | 74 |
|
---|
[50377a4] | 75 | printAll( attributes, os, indent );
|
---|
[7baed7d] | 76 |
|
---|
[6e8bd43] | 77 | get_storageClasses().print( os );
|
---|
| 78 | get_funcSpec().print( os );
|
---|
[dd020c0] | 79 |
|
---|
[50377a4] | 80 | if ( type ) {
|
---|
| 81 | type->print( os, indent );
|
---|
[0dd3a2f] | 82 | } else {
|
---|
| 83 | os << "untyped entity ";
|
---|
| 84 | } // if
|
---|
[843054c2] | 85 |
|
---|
[0dd3a2f] | 86 | if ( statements ) {
|
---|
[50377a4] | 87 | os << indent << "... with body " << endl << indent+1;
|
---|
| 88 | statements->print( os, indent+1 );
|
---|
[0dd3a2f] | 89 | } // if
|
---|
[51b73452] | 90 | }
|
---|
| 91 |
|
---|
[50377a4] | 92 | void FunctionDecl::printShort( std::ostream &os, Indenter indent ) const {
|
---|
[0dd3a2f] | 93 | using std::endl;
|
---|
| 94 | using std::string;
|
---|
[9243a501] | 95 |
|
---|
[50377a4] | 96 | if ( name != "" ) {
|
---|
| 97 | os << name << ": ";
|
---|
[0dd3a2f] | 98 | } // if
|
---|
[7baed7d] | 99 |
|
---|
[6e8bd43] | 100 | get_storageClasses().print( os );
|
---|
| 101 | get_funcSpec().print( os );
|
---|
[dd020c0] | 102 |
|
---|
[50377a4] | 103 | if ( type ) {
|
---|
| 104 | type->print( os, indent );
|
---|
[0dd3a2f] | 105 | } else {
|
---|
| 106 | os << "untyped entity ";
|
---|
| 107 | } // if
|
---|
[51b73452] | 108 | }
|
---|
[0dd3a2f] | 109 |
|
---|
| 110 | // Local Variables: //
|
---|
| 111 | // tab-width: 4 //
|
---|
| 112 | // mode: c++ //
|
---|
| 113 | // compile-command: "make install" //
|
---|
| 114 | // End: //
|
---|