source: src/GenPoly/GenPoly.cc@ cf16f94

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since cf16f94 was cf16f94, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

create temporary return variable for return expressions, remove unnecessary code after temporary-return-variable change, fix missing lvalue qualifiers, change stream operator to |

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[51587aa]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//
[01aeade]7// GenPoly.cc --
[51587aa]8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
[cf16f94]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Dec 1 15:18:54 2015
13// Update Count : 12
[51587aa]14//
[51b73452]15
16#include "GenPoly.h"
17#include "SynTree/Type.h"
18
[b1a6d6b]19#include <iostream>
20using namespace std;
[51b73452]21
22namespace GenPoly {
[cf16f94]23 // A function needs an adapter if it returns a polymorphic value or if any of its parameters have polymorphic type
[e56cfdb0]24 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
25 if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
26 return true;
27 } // if
28 for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
29 if ( isPolyVal( (*innerArg)->get_type(), tyVars ) ) {
30 return true;
31 } // if
32 } // for
33 return false;
34 }
35
36 bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
37 bool doTransform = false;
38 if ( ! function->get_returnVals().empty() ) {
39 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) {
40
41 // figure out if the return type is specified by a type parameter
42 for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) {
43 if ( (*tyVar)->get_name() == typeInst->get_name() ) {
44 doTransform = true;
45 name = typeInst->get_name();
46 break;
47 } // if
48 } // for
49 if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) {
50 doTransform = true;
51 } // if
52 } // if
53 } // if
54 return doTransform;
55 }
56
57 bool isPolyRet( FunctionType *function, std::string &name ) {
58 TyVarMap dummyTyVars;
59 return isPolyRet( function, name, dummyTyVars );
[01aeade]60 }
[51b73452]61
[e56cfdb0]62 bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
63 std::string dummyString;
64 return isPolyRet( function, dummyString, otherTyVars );
[01aeade]65 }
[b1a6d6b]66
[e56cfdb0]67 bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
68 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
[01aeade]69 if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
70 return true;
71 } // if
72 } // if
73 return false;
74 }
[b1a6d6b]75
[bdf1954]76 bool isPolyObj( Type *type, const TyVarMap &tyVars ) {
77 if ( isPolyVal( type, tyVars ) ) {
78 return true;
79 } else if ( PointerType *pt = dynamic_cast<PointerType*>( type ) ) {
80 return isPolyObj( pt->get_base(), tyVars );
81 } else {
82 return false;
83 }
84 }
85
[01aeade]86 void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
87 for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
88 os << i->first << " (" << i->second << ") ";
89 } // for
90 os << std::endl;
91 }
[51b73452]92} // namespace GenPoly
[01aeade]93
[51587aa]94// Local Variables: //
95// tab-width: 4 //
96// mode: c++ //
97// compile-command: "make install" //
98// End: //
Note: See TracBrowser for help on using the repository browser.