| 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 Mar 16 08:33:41 2017
 | 
|---|
| 13 | // Update Count     : 74
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <cassert>               // for assert
 | 
|---|
| 17 | #include <list>                  // for list
 | 
|---|
| 18 | #include <ostream>               // for operator<<, ostream, basic_ostream
 | 
|---|
| 19 | #include <string>                // for operator<<, string, char_traits, ope...
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "Attribute.h"           // for Attribute
 | 
|---|
| 22 | #include "CodeGen/FixMain.h"     // for FixMain
 | 
|---|
| 23 | #include "Common/utility.h"      // for maybeClone, printAll
 | 
|---|
| 24 | #include "Declaration.h"         // for FunctionDecl, FunctionDecl::Parent
 | 
|---|
| 25 | #include "Parser/LinkageSpec.h"  // for Spec, linkageName, Cforall
 | 
|---|
| 26 | #include "Statement.h"           // for CompoundStmt
 | 
|---|
| 27 | #include "Type.h"                // for Type, FunctionType, Type::FuncSpecif...
 | 
|---|
| 28 | #include "DeclReplacer.h"
 | 
|---|
| 29 | 
 | 
|---|
| 30 | extern bool translation_unit_nomain;
 | 
|---|
| 31 | 
 | 
|---|
| 32 | FunctionDecl::FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements, std::list< Attribute * > attributes, Type::FuncSpecifiers fs )
 | 
|---|
| 33 |         : Parent( name, scs, linkage, attributes, fs ), type( type ), statements( statements ) {
 | 
|---|
| 34 |         // hack forcing the function "main" to have Cforall linkage to replace main even if it is inside an extern
 | 
|---|
| 35 |         if ( name == "main" ) {
 | 
|---|
| 36 |                 set_linkage( CodeGen::FixMain::mainLinkage() );
 | 
|---|
| 37 |         } // if
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | FunctionDecl::FunctionDecl( const FunctionDecl &other )
 | 
|---|
| 41 |                 : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ) {
 | 
|---|
| 42 | 
 | 
|---|
| 43 |         DeclReplacer::DeclMap declMap;
 | 
|---|
| 44 |         for ( auto p : group_iterate( other.type->parameters, type->parameters ) ) {
 | 
|---|
| 45 |                 declMap[ std::get<0>(p) ] = std::get<1>(p);
 | 
|---|
| 46 |         }
 | 
|---|
| 47 |         for ( auto p : group_iterate( other.type->returnVals, type->returnVals ) ) {
 | 
|---|
| 48 |                 declMap[ std::get<0>(p) ] = std::get<1>(p);
 | 
|---|
| 49 |         }
 | 
|---|
| 50 |         if ( ! declMap.empty() ) {
 | 
|---|
| 51 |                 DeclReplacer::replace( this, declMap );
 | 
|---|
| 52 |         }
 | 
|---|
| 53 |         cloneAll( other.withExprs, withExprs );
 | 
|---|
| 54 | }
 | 
|---|
| 55 | 
 | 
|---|
| 56 | FunctionDecl::~FunctionDecl() {
 | 
|---|
| 57 |         delete type;
 | 
|---|
| 58 |         delete statements;
 | 
|---|
| 59 |         deleteAll( withExprs );
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | FunctionDecl * FunctionDecl::newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements ) {
 | 
|---|
| 63 |         return new FunctionDecl( name, Type::StorageClasses(), LinkageSpec::C, type, statements );
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | void FunctionDecl::print( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 67 |         using std::endl;
 | 
|---|
| 68 |         using std::string;
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         if ( name != "" ) {
 | 
|---|
| 71 |                 os << name << ": ";
 | 
|---|
| 72 |         } // if
 | 
|---|
| 73 |         if ( linkage != LinkageSpec::Cforall ) {
 | 
|---|
| 74 |                 os << LinkageSpec::linkageName( linkage ) << " ";
 | 
|---|
| 75 |         } // if
 | 
|---|
| 76 | 
 | 
|---|
| 77 |         printAll( attributes, os, indent );
 | 
|---|
| 78 | 
 | 
|---|
| 79 |         get_storageClasses().print( os );
 | 
|---|
| 80 |         get_funcSpec().print( os );
 | 
|---|
| 81 | 
 | 
|---|
| 82 |         if ( type ) {
 | 
|---|
| 83 |                 type->print( os, indent );
 | 
|---|
| 84 |         } else {
 | 
|---|
| 85 |                 os << "untyped entity ";
 | 
|---|
| 86 |         } // if
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         if ( statements ) {
 | 
|---|
| 89 |                 os << indent << "... with body " << endl << indent+1;
 | 
|---|
| 90 |                 statements->print( os, indent+1 );
 | 
|---|
| 91 |         } // if
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | void FunctionDecl::printShort( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 95 |         using std::endl;
 | 
|---|
| 96 |         using std::string;
 | 
|---|
| 97 | 
 | 
|---|
| 98 |         if ( name != "" ) {
 | 
|---|
| 99 |                 os << name << ": ";
 | 
|---|
| 100 |         } // if
 | 
|---|
| 101 | 
 | 
|---|
| 102 |         get_storageClasses().print( os );
 | 
|---|
| 103 |         get_funcSpec().print( os );
 | 
|---|
| 104 | 
 | 
|---|
| 105 |         if ( type ) {
 | 
|---|
| 106 |                 type->print( os, indent );
 | 
|---|
| 107 |         } else {
 | 
|---|
| 108 |                 os << "untyped entity ";
 | 
|---|
| 109 |         } // if
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | // Local Variables: //
 | 
|---|
| 113 | // tab-width: 4 //
 | 
|---|
| 114 | // mode: c++ //
 | 
|---|
| 115 | // compile-command: "make install" //
 | 
|---|
| 116 | // End: //
 | 
|---|