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