source: src/GenPoly/FindFunction.cc @ c6b4432

Last change on this file since c6b4432 was c6b4432, checked in by Andrew Beach <ajbeach@…>, 8 months ago

Remove BaseSyntaxNode? and clean-up.

  • Property mode set to 100644
File size: 3.4 KB
Line 
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// FindFunction.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Fri Oct  7 17:05:20 2022
13// Update Count     : 7
14//
15
16#include "FindFunction.h"
17
18#include <utility>                      // for pair
19
20#include "AST/Pass.hpp"                 // for Pass
21#include "AST/Type.hpp"
22#include "GenPoly/ErasableScopedMap.h"  // for ErasableScopedMap<>::iterator
23#include "GenPoly/GenPoly.h"            // for TyVarMap
24#include "ScrubTyVars.h"                // for ScrubTyVars
25
26namespace GenPoly {
27
28namespace {
29
30struct FindFunctionCore :
31                public ast::WithGuards,
32                public ast::WithShortCircuiting,
33                public ast::WithVisitorRef<FindFunctionCore> {
34        FindFunctionCore(
35                std::vector<ast::ptr<ast::FunctionType>> & functions,
36                const TypeVarMap & typeVars, FindFunctionPred predicate,
37                bool replaceMode );
38
39        void previsit( ast::FunctionType const * type );
40        ast::Type const * postvisit( ast::FunctionType const * type );
41        void previsit( ast::PointerType const * type );
42private:
43        void handleForall( const ast::FunctionType::ForallList & forall );
44
45        std::vector<ast::ptr<ast::FunctionType>> &functions;
46        TypeVarMap typeVars;
47        FindFunctionPred predicate;
48        bool replaceMode;
49};
50
51FindFunctionCore::FindFunctionCore(
52                std::vector<ast::ptr<ast::FunctionType>> & functions,
53                const TypeVarMap &typeVars, FindFunctionPred predicate,
54                bool replaceMode ) :
55        functions( functions ), typeVars( typeVars ),
56        predicate( predicate ), replaceMode( replaceMode ) {}
57
58void FindFunctionCore::handleForall( const ast::FunctionType::ForallList & forall ) {
59        for ( const ast::ptr<ast::TypeInstType> & td : forall ) {
60                TypeVarMap::iterator var = typeVars.find( *td );
61                if ( var != typeVars.end() ) {
62                        typeVars.erase( var->first );
63                } // if
64        } // for
65}
66
67void FindFunctionCore::previsit( ast::FunctionType const * type ) {
68        visit_children = false;
69        GuardScope( typeVars );
70        handleForall( type->forall );
71        //ast::accept_all( type->returns, *visitor );
72        // This might have to become ast::mutate_each with return.
73        ast::accept_each( type->returns, *visitor );
74}
75
76ast::Type const * FindFunctionCore::postvisit( ast::FunctionType const * type ) {
77        ast::Type const * ret = type;
78        if ( predicate( type, typeVars ) ) {
79                functions.push_back( type );
80                if ( replaceMode ) {
81                        // replace type parameters in function type with void*
82                        ret = scrubTypeVars( ast::deepCopy( type ), typeVars );
83                } // if
84        } // if
85        return ret;
86}
87
88void FindFunctionCore::previsit( ast::PointerType const * /*type*/ ) {
89        GuardScope( typeVars );
90}
91
92} // namespace
93
94void findFunction( const ast::Type * type,
95                std::vector<ast::ptr<ast::FunctionType>> & functions,
96                const TypeVarMap & typeVars, FindFunctionPred predicate ) {
97        ast::Pass<FindFunctionCore> pass( functions, typeVars, predicate, false );
98        type->accept( pass );
99}
100
101const ast::Type * findAndReplaceFunction( const ast::Type * type,
102                std::vector<ast::ptr<ast::FunctionType>> & functions,
103                const TypeVarMap & typeVars, FindFunctionPred predicate ) {
104        ast::Pass<FindFunctionCore> pass( functions, typeVars, predicate, true );
105        return type->accept( pass );
106}
107
108} // namespace GenPoly
109
110// Local Variables: //
111// tab-width: 4 //
112// mode: c++ //
113// compile-command: "make install" //
114// End: //
Note: See TracBrowser for help on using the repository browser.