| 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 | 
|---|
| 12 | // Last Modified On : Thu May 21 21:31:16 2015 | 
|---|
| 13 | // Update Count     : 11 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <cassert> | 
|---|
| 17 |  | 
|---|
| 18 | #include "Declaration.h" | 
|---|
| 19 | #include "Statement.h" | 
|---|
| 20 | #include "Type.h" | 
|---|
| 21 | #include "utility.h" | 
|---|
| 22 |  | 
|---|
| 23 | FunctionDecl::FunctionDecl( const std::string &name, StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline ) | 
|---|
| 24 | : Parent( name, sc, linkage ), type( type ), statements( statements ), isInline( isInline ) { | 
|---|
| 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 | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 | FunctionDecl::FunctionDecl( const FunctionDecl &other ) | 
|---|
| 32 | : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ), isInline( other.isInline ) { | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | FunctionDecl::~FunctionDecl() { | 
|---|
| 36 | delete type; | 
|---|
| 37 | delete statements; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | Type * FunctionDecl::get_type() const { | 
|---|
| 41 | return type; | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | void FunctionDecl::set_type( Type *t ) { | 
|---|
| 45 | type = dynamic_cast< FunctionType* >( t ); | 
|---|
| 46 | assert( type ); | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | void FunctionDecl::print( std::ostream &os, int indent ) const { | 
|---|
| 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 | 
|---|
| 70 |  | 
|---|
| 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 | 
|---|
| 77 |  | 
|---|
| 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 | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | void FunctionDecl::printShort( std::ostream &os, int indent ) const { | 
|---|
| 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 | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | // Local Variables: // | 
|---|
| 109 | // tab-width: 4 // | 
|---|
| 110 | // mode: c++ // | 
|---|
| 111 | // compile-command: "make install" // | 
|---|
| 112 | // End: // | 
|---|