| [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 | 
|---|
|  | 12 | // Last Modified On : Thu Aug 18 23:50:14 2016 | 
|---|
|  | 13 | // Update Count     : 20 | 
|---|
| [0dd3a2f] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [51b73452] | 16 | #include <cassert> | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include "Declaration.h" | 
|---|
|  | 19 | #include "Statement.h" | 
|---|
|  | 20 | #include "Type.h" | 
|---|
| [7baed7d] | 21 | #include "Attribute.h" | 
|---|
| [d3b7937] | 22 | #include "Common/utility.h" | 
|---|
| [4d4882a] | 23 | #include "InitTweak/InitTweak.h" | 
|---|
| [51b73452] | 24 |  | 
|---|
| [8b7ee09] | 25 | FunctionDecl::FunctionDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements, bool isInline, bool isNoreturn, std::list< Attribute * > attributes ) | 
|---|
| [f9cebb5] | 26 | : Parent( name, sc, linkage, attributes ), type( type ), statements( statements ) { | 
|---|
| [1db21619] | 27 | set_isInline( isInline ); | 
|---|
|  | 28 | set_isNoreturn( isNoreturn ); | 
|---|
| [0dd3a2f] | 29 | // this is a brazen hack to force the function "main" to have C linkage | 
|---|
|  | 30 | if ( name == "main" ) { | 
|---|
|  | 31 | set_linkage( LinkageSpec::C ); | 
|---|
|  | 32 | } // if | 
|---|
| [51b73452] | 33 | } | 
|---|
|  | 34 |  | 
|---|
|  | 35 | FunctionDecl::FunctionDecl( const FunctionDecl &other ) | 
|---|
| [7baed7d] | 36 | : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ) { | 
|---|
| [51b73452] | 37 | } | 
|---|
|  | 38 |  | 
|---|
| [c11e31c] | 39 | FunctionDecl::~FunctionDecl() { | 
|---|
| [0dd3a2f] | 40 | delete type; | 
|---|
|  | 41 | delete statements; | 
|---|
| [2037f82] | 42 | deleteAll( oldDecls ); | 
|---|
| [51b73452] | 43 | } | 
|---|
|  | 44 |  | 
|---|
| [c11e31c] | 45 | Type * FunctionDecl::get_type() const { | 
|---|
| [0dd3a2f] | 46 | return type; | 
|---|
| [51b73452] | 47 | } | 
|---|
|  | 48 |  | 
|---|
| [c11e31c] | 49 | void FunctionDecl::set_type( Type *t ) { | 
|---|
| [0dd3a2f] | 50 | type = dynamic_cast< FunctionType* >( t ); | 
|---|
|  | 51 | assert( type ); | 
|---|
| [51b73452] | 52 | } | 
|---|
|  | 53 |  | 
|---|
| [c11e31c] | 54 | void FunctionDecl::print( std::ostream &os, int indent ) const { | 
|---|
| [0dd3a2f] | 55 | using std::endl; | 
|---|
|  | 56 | using std::string; | 
|---|
| [9243a501] | 57 |  | 
|---|
| [0dd3a2f] | 58 | if ( get_name() != "" ) { | 
|---|
| [5f2f2d7] | 59 | os << get_name() << ": "; | 
|---|
| [0dd3a2f] | 60 | } // if | 
|---|
|  | 61 | if ( get_linkage() != LinkageSpec::Cforall ) { | 
|---|
|  | 62 | os << LinkageSpec::toString( get_linkage() ) << " "; | 
|---|
|  | 63 | } // if | 
|---|
| [1db21619] | 64 | if ( get_isInline() ) { | 
|---|
| [0dd3a2f] | 65 | os << "inline "; | 
|---|
|  | 66 | } // if | 
|---|
| [1db21619] | 67 | if ( get_isNoreturn() ) { | 
|---|
| [de62360d] | 68 | os << "_Noreturn "; | 
|---|
|  | 69 | } // if | 
|---|
| [7baed7d] | 70 |  | 
|---|
| [f9cebb5] | 71 | printAll( get_attributes(), os, indent ); | 
|---|
| [7baed7d] | 72 |  | 
|---|
| [68cd1ce] | 73 | if ( get_storageClass() != DeclarationNode::NoStorageClass ) { | 
|---|
|  | 74 | os << DeclarationNode::storageName[ get_storageClass() ] << ' '; | 
|---|
| [0dd3a2f] | 75 | } // if | 
|---|
|  | 76 | if ( get_type() ) { | 
|---|
|  | 77 | get_type()->print( os, indent ); | 
|---|
|  | 78 | } else { | 
|---|
|  | 79 | os << "untyped entity "; | 
|---|
|  | 80 | } // if | 
|---|
| [843054c2] | 81 |  | 
|---|
| [0dd3a2f] | 82 | if ( ! oldIdents.empty() ) { | 
|---|
| [de62360d] | 83 | os << string( indent + 2, ' ' ) << "with parameter names" << endl; | 
|---|
| [0dd3a2f] | 84 | for ( std::list< std::string >::const_iterator i = oldIdents.begin(); i != oldIdents.end(); ++i ) { | 
|---|
| [de62360d] | 85 | os << string( indent + 4, ' ' ) << *i << endl; | 
|---|
| [0dd3a2f] | 86 | } // for | 
|---|
|  | 87 | } // if | 
|---|
| [843054c2] | 88 |  | 
|---|
| [0dd3a2f] | 89 | if ( ! oldDecls.empty() ) { | 
|---|
| [de62360d] | 90 | os << string( indent + 2, ' ' ) << "with parameter declarations" << endl; | 
|---|
|  | 91 | printAll( oldDecls, os, indent + 4 ); | 
|---|
| [0dd3a2f] | 92 | } // if | 
|---|
|  | 93 | if ( statements ) { | 
|---|
| [de62360d] | 94 | os << string( indent + 2, ' ' ) << "with body " << endl; | 
|---|
| [9243a501] | 95 | os << string( indent + 4, ' ' ); | 
|---|
| [de62360d] | 96 | statements->print( os, indent + 4 ); | 
|---|
| [0dd3a2f] | 97 | } // if | 
|---|
| [51b73452] | 98 | } | 
|---|
|  | 99 |  | 
|---|
| [c11e31c] | 100 | void FunctionDecl::printShort( std::ostream &os, int indent ) const { | 
|---|
| [0dd3a2f] | 101 | using std::endl; | 
|---|
|  | 102 | using std::string; | 
|---|
| [9243a501] | 103 |  | 
|---|
| [0dd3a2f] | 104 | if ( get_name() != "" ) { | 
|---|
| [5f2f2d7] | 105 | os << get_name() << ": "; | 
|---|
| [0dd3a2f] | 106 | } // if | 
|---|
| [1db21619] | 107 | if ( get_isInline() ) { | 
|---|
| [0dd3a2f] | 108 | os << "inline "; | 
|---|
|  | 109 | } // if | 
|---|
| [1db21619] | 110 | if ( get_isNoreturn() ) { | 
|---|
| [de62360d] | 111 | os << "_Noreturn "; | 
|---|
|  | 112 | } // if | 
|---|
| [7baed7d] | 113 |  | 
|---|
|  | 114 | // xxx - should printShort print attributes? | 
|---|
|  | 115 |  | 
|---|
| [68cd1ce] | 116 | if ( get_storageClass() != DeclarationNode::NoStorageClass ) { | 
|---|
|  | 117 | os << DeclarationNode::storageName[ get_storageClass() ] << ' '; | 
|---|
| [0dd3a2f] | 118 | } // if | 
|---|
|  | 119 | if ( get_type() ) { | 
|---|
|  | 120 | get_type()->print( os, indent ); | 
|---|
|  | 121 | } else { | 
|---|
|  | 122 | os << "untyped entity "; | 
|---|
|  | 123 | } // if | 
|---|
| [51b73452] | 124 | } | 
|---|
| [0dd3a2f] | 125 |  | 
|---|
|  | 126 | // Local Variables: // | 
|---|
|  | 127 | // tab-width: 4 // | 
|---|
|  | 128 | // mode: c++ // | 
|---|
|  | 129 | // compile-command: "make install" // | 
|---|
|  | 130 | // End: // | 
|---|