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