source: src/SynTree/FunctionDecl.cc @ c8cbb41

Last change on this file since c8cbb41 was 056bee8, checked in by Andrew Beach <ajbeach@…>, 20 months ago

Added printing of with clauses to the printing of both new and old ast nodes.

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[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//
[9243a501]7// FunctionDecl.cc --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[8b7ee09]11// Last Modified By : Peter A. Buhr
[d912bed]12// Last Modified On : Mon Dec 16 15:11:20 2019
13// Update Count     : 77
[0dd3a2f]14//
15
[ea6332d]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...
[51b7345]20
[ea6332d]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
[312029a]25#include "Expression.h"
[07de76b]26#include "LinkageSpec.h"         // for Spec, linkageName, Cforall
[ea6332d]27#include "Statement.h"           // for CompoundStmt
28#include "Type.h"                // for Type, FunctionType, Type::FuncSpecif...
[7862059]29#include "DeclReplacer.h"
[51b7345]30
[3fe34ae]31extern bool translation_unit_nomain;
32
[ddfd945]33FunctionDecl::FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements, std::list< Attribute * > attributes, Type::FuncSpecifiers fs )
[a7c90d4]34        : Parent( name, scs, linkage, attributes, fs ), type( type ), statements( statements ) {
[dd020c0]35        // hack forcing the function "main" to have Cforall linkage to replace main even if it is inside an extern
[0dd3a2f]36        if ( name == "main" ) {
[13de47bc]37                set_linkage( CodeGen::FixMain::mainLinkage() );
[0dd3a2f]38        } // if
[51b7345]39}
40
41FunctionDecl::FunctionDecl( const FunctionDecl &other )
[a7c90d4]42                : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ) {
[97dbc09]43
[7862059]44        DeclReplacer::DeclMap declMap;
[97dbc09]45        for ( auto p : group_iterate( other.type->parameters, type->parameters ) ) {
46                declMap[ std::get<0>(p) ] = std::get<1>(p);
47        }
48        for ( auto p : group_iterate( other.type->returnVals, type->returnVals ) ) {
49                declMap[ std::get<0>(p) ] = std::get<1>(p);
50        }
51        if ( ! declMap.empty() ) {
[7862059]52                DeclReplacer::replace( this, declMap );
[97dbc09]53        }
[574894d]54        cloneAll( other.withExprs, withExprs );
[51b7345]55}
56
[c11e31c]57FunctionDecl::~FunctionDecl() {
[0dd3a2f]58        delete type;
59        delete statements;
[574894d]60        deleteAll( withExprs );
[51b7345]61}
62
[75626a1]63FunctionDecl * FunctionDecl::newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements ) {
64        return new FunctionDecl( name, Type::StorageClasses(), LinkageSpec::C, type, statements );
65}
66
[50377a4]67void FunctionDecl::print( std::ostream &os, Indenter indent ) const {
[0dd3a2f]68        using std::endl;
69        using std::string;
[9243a501]70
[50377a4]71        if ( name != "" ) {
72                os << name << ": ";
[0dd3a2f]73        } // if
[50377a4]74        if ( linkage != LinkageSpec::Cforall ) {
[d912bed]75                os << LinkageSpec::name( linkage ) << " ";
[0dd3a2f]76        } // if
[7baed7d]77
[50377a4]78        printAll( attributes, os, indent );
[7baed7d]79
[6e8bd43]80        get_storageClasses().print( os );
81        get_funcSpec().print( os );
[dd020c0]82
[50377a4]83        if ( type ) {
84                type->print( os, indent );
[0dd3a2f]85        } else {
86                os << "untyped entity ";
87        } // if
[843054c2]88
[056bee8]89        if ( !withExprs.empty() ) {
90                os << indent << "... with clause" << std::endl;
91                os << indent + 1;
92                printAll( withExprs, os, indent + 1 );
93        }
94
[0dd3a2f]95        if ( statements ) {
[08222c7]96                os << indent << "... with body" << endl << indent+1;
[50377a4]97                statements->print( os, indent+1 );
[0dd3a2f]98        } // if
[51b7345]99}
100
[50377a4]101void FunctionDecl::printShort( std::ostream &os, Indenter indent ) const {
[0dd3a2f]102        using std::endl;
103        using std::string;
[9243a501]104
[50377a4]105        if ( name != "" ) {
106                os << name << ": ";
[0dd3a2f]107        } // if
[7baed7d]108
[6e8bd43]109        get_storageClasses().print( os );
110        get_funcSpec().print( os );
[dd020c0]111
[50377a4]112        if ( type ) {
113                type->print( os, indent );
[0dd3a2f]114        } else {
115                os << "untyped entity ";
116        } // if
[51b7345]117}
[0dd3a2f]118
119// Local Variables: //
120// tab-width: 4 //
121// mode: c++ //
122// compile-command: "make install" //
123// End: //
Note: See TracBrowser for help on using the repository browser.