| 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 | // PolyCost.cc --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Sun May 17 09:50:12 2015
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Wed Jun 19 10:45:00 2019
 | 
|---|
| 13 | // Update Count     : 4
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "AST/SymbolTable.hpp"
 | 
|---|
| 17 | #include "AST/Pass.hpp"
 | 
|---|
| 18 | #include "AST/Type.hpp"
 | 
|---|
| 19 | #include "AST/TypeEnvironment.hpp"
 | 
|---|
| 20 | #include "Common/PassVisitor.h"
 | 
|---|
| 21 | #include "SymTab/Indexer.h"   // for Indexer
 | 
|---|
| 22 | #include "SynTree/Type.h"     // for TypeInstType, Type
 | 
|---|
| 23 | #include "TypeEnvironment.h"  // for EqvClass, TypeEnvironment
 | 
|---|
| 24 | 
 | 
|---|
| 25 | namespace ResolvExpr {
 | 
|---|
| 26 |         struct PolyCost {
 | 
|---|
| 27 |                 PolyCost( const TypeEnvironment &env, const SymTab::Indexer &indexer );
 | 
|---|
| 28 | 
 | 
|---|
| 29 |                 void previsit( TypeInstType * aggregateUseType );
 | 
|---|
| 30 |                 int result;
 | 
|---|
| 31 |                 const TypeEnvironment &tenv;
 | 
|---|
| 32 |                 const SymTab::Indexer &indexer;
 | 
|---|
| 33 |         };
 | 
|---|
| 34 | 
 | 
|---|
| 35 |         int polyCost( Type *type, const TypeEnvironment & env, const SymTab::Indexer &indexer ) {
 | 
|---|
| 36 |                 PassVisitor<PolyCost> coster( env, indexer );
 | 
|---|
| 37 |                 type->accept( coster );
 | 
|---|
| 38 |                 return (coster.pass.result > 0) ? 1 : 0;
 | 
|---|
| 39 |         }
 | 
|---|
| 40 | 
 | 
|---|
| 41 |         PolyCost::PolyCost( const TypeEnvironment & env, const SymTab::Indexer & indexer ) : result( 0 ), tenv( env ), indexer( indexer ) {
 | 
|---|
| 42 |         }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |         void PolyCost::previsit(TypeInstType * typeInst) {
 | 
|---|
| 45 |                 if ( const EqvClass *eqvClass = tenv.lookup( typeInst->name ) ) {
 | 
|---|
| 46 |                         if ( eqvClass->type ) {
 | 
|---|
| 47 |                                 if ( TypeInstType * otherTypeInst = dynamic_cast< TypeInstType* >( eqvClass->type ) ) {
 | 
|---|
| 48 |                                         if ( indexer.lookupType( otherTypeInst->name ) ) {
 | 
|---|
| 49 |                                                 // bound to opaque type
 | 
|---|
| 50 |                                                 result += 1;
 | 
|---|
| 51 |                                         } // if
 | 
|---|
| 52 |                                 } else {
 | 
|---|
| 53 |                                         // bound to concrete type
 | 
|---|
| 54 |                                         result += 1;
 | 
|---|
| 55 |                                 } // if
 | 
|---|
| 56 |                         } // if
 | 
|---|
| 57 |                 } // if
 | 
|---|
| 58 |         }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | // TODO: When the old PolyCost is torn out get rid of the _new suffix.
 | 
|---|
| 61 | class PolyCost_new {
 | 
|---|
| 62 |         const ast::SymbolTable &symtab;
 | 
|---|
| 63 | public:
 | 
|---|
| 64 |         int result;
 | 
|---|
| 65 |         const ast::TypeEnvironment &env_;
 | 
|---|
| 66 | 
 | 
|---|
| 67 |         PolyCost_new( const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ) 
 | 
|---|
| 68 |         : symtab( symtab ), result( 0 ), env_( env ) {}
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         void previsit( const ast::TypeInstType * type ) {
 | 
|---|
| 71 |                 if ( const ast::EqvClass * eqv = env_.lookup( *type ) ) /* && */ if ( eqv->bound ) {
 | 
|---|
| 72 |                         if ( const ast::TypeInstType * otherType = eqv->bound.as< ast::TypeInstType >() ) {
 | 
|---|
| 73 |                                 if ( symtab.lookupType( otherType->name ) ) {
 | 
|---|
| 74 |                                         // Bound to opaque type.
 | 
|---|
| 75 |                                         result += 1;
 | 
|---|
| 76 |                                 }
 | 
|---|
| 77 |                         } else {
 | 
|---|
| 78 |                                 // Bound to concrete type.
 | 
|---|
| 79 |                                 result += 1;
 | 
|---|
| 80 |                         }
 | 
|---|
| 81 |                 }
 | 
|---|
| 82 |         }
 | 
|---|
| 83 | };
 | 
|---|
| 84 | 
 | 
|---|
| 85 | int polyCost(
 | 
|---|
| 86 |         const ast::Type * type, const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
 | 
|---|
| 87 | ) {
 | 
|---|
| 88 |         ast::Pass<PolyCost_new> costing( symtab, env );
 | 
|---|
| 89 |         type->accept( costing );
 | 
|---|
| 90 |         return (costing.core.result > 0) ? 1 : 0;
 | 
|---|
| 91 | }
 | 
|---|
| 92 | 
 | 
|---|
| 93 | } // namespace ResolvExpr
 | 
|---|
| 94 | 
 | 
|---|
| 95 | // Local Variables: //
 | 
|---|
| 96 | // tab-width: 4 //
 | 
|---|
| 97 | // mode: c++ //
 | 
|---|
| 98 | // compile-command: "make install" //
 | 
|---|
| 99 | // End: //
 | 
|---|