[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 | // |
---|
| 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 |
---|
[843054c2] | 12 | // Last Modified On : Thu May 21 21:31:16 2015 |
---|
| 13 | // Update Count : 11 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <cassert> |
---|
| 17 | |
---|
| 18 | #include "Declaration.h" |
---|
| 19 | #include "Statement.h" |
---|
| 20 | #include "Type.h" |
---|
| 21 | #include "utility.h" |
---|
| 22 | |
---|
[c11e31c] | 23 | FunctionDecl::FunctionDecl( const std::string &name, StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline ) |
---|
[843054c2] | 24 | : Parent( name, sc, linkage ), type( type ), statements( statements ), isInline( isInline ) { |
---|
[0dd3a2f] | 25 | // this is a brazen hack to force the function "main" to have C linkage |
---|
| 26 | if ( name == "main" ) { |
---|
| 27 | set_linkage( LinkageSpec::C ); |
---|
| 28 | } // if |
---|
[51b7345] | 29 | } |
---|
| 30 | |
---|
| 31 | FunctionDecl::FunctionDecl( const FunctionDecl &other ) |
---|
[843054c2] | 32 | : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ), isInline( other.isInline ) { |
---|
[51b7345] | 33 | } |
---|
| 34 | |
---|
[c11e31c] | 35 | FunctionDecl::~FunctionDecl() { |
---|
[0dd3a2f] | 36 | delete type; |
---|
| 37 | delete statements; |
---|
[51b7345] | 38 | } |
---|
| 39 | |
---|
[c11e31c] | 40 | Type * FunctionDecl::get_type() const { |
---|
[0dd3a2f] | 41 | return type; |
---|
[51b7345] | 42 | } |
---|
| 43 | |
---|
[c11e31c] | 44 | void FunctionDecl::set_type( Type *t ) { |
---|
[0dd3a2f] | 45 | type = dynamic_cast< FunctionType* >( t ); |
---|
| 46 | assert( type ); |
---|
[51b7345] | 47 | } |
---|
| 48 | |
---|
[c11e31c] | 49 | void FunctionDecl::print( std::ostream &os, int indent ) const { |
---|
[0dd3a2f] | 50 | using std::endl; |
---|
| 51 | using std::string; |
---|
| 52 | |
---|
| 53 | if ( get_name() != "" ) { |
---|
| 54 | os << get_name() << ": a "; |
---|
| 55 | } // if |
---|
| 56 | if ( get_linkage() != LinkageSpec::Cforall ) { |
---|
| 57 | os << LinkageSpec::toString( get_linkage() ) << " "; |
---|
| 58 | } // if |
---|
| 59 | if ( isInline ) { |
---|
| 60 | os << "inline "; |
---|
| 61 | } // if |
---|
| 62 | if ( get_storageClass() != NoStorageClass ) { |
---|
| 63 | os << storageClassName[ get_storageClass() ] << ' '; |
---|
| 64 | } // if |
---|
| 65 | if ( get_type() ) { |
---|
| 66 | get_type()->print( os, indent ); |
---|
| 67 | } else { |
---|
| 68 | os << "untyped entity "; |
---|
| 69 | } // if |
---|
[843054c2] | 70 | |
---|
[0dd3a2f] | 71 | if ( ! oldIdents.empty() ) { |
---|
| 72 | os << string( indent+2, ' ' ) << "with parameter names" << endl; |
---|
| 73 | for ( std::list< std::string >::const_iterator i = oldIdents.begin(); i != oldIdents.end(); ++i ) { |
---|
| 74 | os << string( indent+4, ' ' ) << *i << endl; |
---|
| 75 | } // for |
---|
| 76 | } // if |
---|
[843054c2] | 77 | |
---|
[0dd3a2f] | 78 | if ( ! oldDecls.empty() ) { |
---|
| 79 | os << string( indent+2, ' ' ) << "with parameter declarations" << endl; |
---|
| 80 | printAll( oldDecls, os, indent+4 ); |
---|
| 81 | } // if |
---|
| 82 | if ( statements ) { |
---|
| 83 | os << string( indent+2, ' ' ) << "with body " << endl; |
---|
| 84 | statements->print( os, indent+4 ); |
---|
| 85 | } // if |
---|
[51b7345] | 86 | } |
---|
| 87 | |
---|
[c11e31c] | 88 | void FunctionDecl::printShort( std::ostream &os, int indent ) const { |
---|
[0dd3a2f] | 89 | using std::endl; |
---|
| 90 | using std::string; |
---|
| 91 | |
---|
| 92 | if ( get_name() != "" ) { |
---|
| 93 | os << get_name() << ": a "; |
---|
| 94 | } // if |
---|
| 95 | if ( isInline ) { |
---|
| 96 | os << "inline "; |
---|
| 97 | } // if |
---|
| 98 | if ( get_storageClass() != NoStorageClass ) { |
---|
| 99 | os << storageClassName[ get_storageClass() ] << ' '; |
---|
| 100 | } // if |
---|
| 101 | if ( get_type() ) { |
---|
| 102 | get_type()->print( os, indent ); |
---|
| 103 | } else { |
---|
| 104 | os << "untyped entity "; |
---|
| 105 | } // if |
---|
[51b7345] | 106 | } |
---|
[0dd3a2f] | 107 | |
---|
| 108 | // Local Variables: // |
---|
| 109 | // tab-width: 4 // |
---|
| 110 | // mode: c++ // |
---|
| 111 | // compile-command: "make install" // |
---|
| 112 | // End: // |
---|