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