[51b7345] | 1 | #include <cassert> |
---|
| 2 | |
---|
| 3 | #include "Declaration.h" |
---|
| 4 | #include "Statement.h" |
---|
| 5 | #include "Type.h" |
---|
| 6 | #include "utility.h" |
---|
| 7 | |
---|
| 8 | |
---|
[c11e31c] | 9 | FunctionDecl::FunctionDecl( const std::string &name, StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline ) |
---|
| 10 | : Parent( name, sc, linkage ), type( type ), statements( statements ), isInline( isInline ) { |
---|
| 11 | // this is a brazen hack to force the function "main" to have C linkage |
---|
| 12 | if ( name == "main" ) { |
---|
[51b7345] | 13 | set_linkage( LinkageSpec::C ); |
---|
| 14 | } |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | FunctionDecl::FunctionDecl( const FunctionDecl &other ) |
---|
[c11e31c] | 18 | : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ), isInline( other.isInline ) { |
---|
[51b7345] | 19 | } |
---|
| 20 | |
---|
[c11e31c] | 21 | FunctionDecl::~FunctionDecl() { |
---|
[51b7345] | 22 | delete type; |
---|
| 23 | delete statements; |
---|
| 24 | } |
---|
| 25 | |
---|
[c11e31c] | 26 | Type * FunctionDecl::get_type() const { |
---|
[51b7345] | 27 | return type; |
---|
| 28 | } |
---|
| 29 | |
---|
[c11e31c] | 30 | void FunctionDecl::set_type( Type *t ) { |
---|
[51b7345] | 31 | type = dynamic_cast< FunctionType* >( t ); |
---|
| 32 | assert( type ); |
---|
| 33 | } |
---|
| 34 | |
---|
[c11e31c] | 35 | void FunctionDecl::print( std::ostream &os, int indent ) const { |
---|
[51b7345] | 36 | using std::endl; |
---|
| 37 | using std::string; |
---|
| 38 | |
---|
[c11e31c] | 39 | if ( get_name() != "" ) { |
---|
[51b7345] | 40 | os << get_name() << ": a "; |
---|
| 41 | } |
---|
[c11e31c] | 42 | if ( get_linkage() != LinkageSpec::Cforall ) { |
---|
[51b7345] | 43 | os << LinkageSpec::toString( get_linkage() ) << " "; |
---|
| 44 | } |
---|
[c11e31c] | 45 | if ( isInline ) { |
---|
[51b7345] | 46 | os << "inline "; |
---|
| 47 | } |
---|
[c11e31c] | 48 | if ( get_storageClass() != NoStorageClass ) { |
---|
[51b7345] | 49 | os << storageClassName[ get_storageClass() ] << ' '; |
---|
| 50 | } |
---|
[c11e31c] | 51 | if ( get_type() ) { |
---|
[51b7345] | 52 | get_type()->print( os, indent ); |
---|
| 53 | } else { |
---|
| 54 | os << "untyped entity "; |
---|
| 55 | } |
---|
[c11e31c] | 56 | if ( ! oldIdents.empty() ) { |
---|
[51b7345] | 57 | os << string( indent+2, ' ' ) << "with parameter names" << endl; |
---|
| 58 | for( std::list< std::string >::const_iterator i = oldIdents.begin(); i != oldIdents.end(); ++i ) { |
---|
| 59 | os << string( indent+4, ' ' ) << *i << endl; |
---|
| 60 | } |
---|
| 61 | } |
---|
[c11e31c] | 62 | if ( ! oldDecls.empty() ) { |
---|
[51b7345] | 63 | os << string( indent+2, ' ' ) << "with parameter declarations" << endl; |
---|
| 64 | printAll( oldDecls, os, indent+4 ); |
---|
| 65 | } |
---|
[c11e31c] | 66 | if ( statements ) { |
---|
[51b7345] | 67 | os << string( indent+2, ' ' ) << "with body " << endl; |
---|
| 68 | statements->print( os, indent+4 ); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
[c11e31c] | 72 | void FunctionDecl::printShort( std::ostream &os, int indent ) const { |
---|
[51b7345] | 73 | using std::endl; |
---|
| 74 | using std::string; |
---|
| 75 | |
---|
[c11e31c] | 76 | if ( get_name() != "" ) { |
---|
[51b7345] | 77 | os << get_name() << ": a "; |
---|
| 78 | } |
---|
[c11e31c] | 79 | if ( isInline ) { |
---|
[51b7345] | 80 | os << "inline "; |
---|
| 81 | } |
---|
[c11e31c] | 82 | if ( get_storageClass() != NoStorageClass ) { |
---|
[51b7345] | 83 | os << storageClassName[ get_storageClass() ] << ' '; |
---|
| 84 | } |
---|
[c11e31c] | 85 | if ( get_type() ) { |
---|
[51b7345] | 86 | get_type()->print( os, indent ); |
---|
| 87 | } else { |
---|
| 88 | os << "untyped entity "; |
---|
| 89 | } |
---|
| 90 | } |
---|