source: src/GenPoly/GenPoly.h @ b2ddaf3

ADTast-experimental
Last change on this file since b2ddaf3 was c8837e5, checked in by Andrew Beach <ajbeach@…>, 20 months ago

Rewrite in GenPoly? to avoid mixing new AST and TyVarMap? (which internally has old AST code). Some nearby functions got writen out even though they are not used, and hence not tested.

  • Property mode set to 100644
File size: 7.7 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// GenPoly.h --
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 15:06:00 2022
13// Update Count     : 9
14//
15
16#pragma once
17
18#include <iostream>               // for ostream
19#include <string>                 // for string, allocator, operator+, basic...
20
21#include "ErasableScopedMap.h"    // for ErasableScopedMap
22#include "AST/Decl.hpp"           // for TypeDecl::Data
23#include "AST/Fwd.hpp"            // for ApplicationExpr, BaseInstType, Func...
24#include "SymTab/Mangler.h"       // for Mangler
25#include "SynTree/Declaration.h"  // for TypeDecl::Data, AggregateDecl, Type...
26#include "SynTree/SynTree.h"      // for Visitor Nodes
27
28namespace GenPoly {
29
30        // TODO Via some tricks this works for ast::TypeDecl::Data as well.
31        typedef ErasableScopedMap< std::string, TypeDecl::Data > TyVarMap;
32        using TypeVarMap = ErasableScopedMap< std::string, ast::TypeDecl::Data >;
33
34        /// Replaces a TypeInstType by its referrent in the environment, if applicable
35        Type* replaceTypeInst( Type* type, const TypeSubstitution* env );
36
37        /// returns polymorphic type if is polymorphic type, NULL otherwise; will look up substitution in env if provided
38        Type *isPolyType( Type *type, const TypeSubstitution *env = 0 );
39        const ast::Type * isPolyType(const ast::Type * type, const ast::TypeSubstitution * env = nullptr);
40
41        /// returns polymorphic type if is polymorphic type in tyVars, NULL otherwise; will look up substitution in env if provided
42        Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 );
43        const ast::Type * isPolyType( const ast::Type * type, const TypeVarMap & typeVars, const ast::TypeSubstitution * subst = nullptr );
44
45        /// returns dynamic-layout type if is dynamic-layout type in tyVars, NULL otherwise; will look up substitution in env if provided
46        ReferenceToType *isDynType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 );
47        const ast::BaseInstType *isDynType( const ast::Type * type, const TypeVarMap & typeVars, const ast::TypeSubstitution * subst = 0 );
48
49        /// true iff function has dynamic-layout return type under the given type variable map
50        ReferenceToType *isDynRet( FunctionType *function, const TyVarMap &tyVars );
51        const ast::BaseInstType *isDynRet( const ast::FunctionType * type, const TypeVarMap & typeVars );
52
53        /// true iff function has dynamic-layout return type under the type variable map generated from its forall-parameters
54        ReferenceToType *isDynRet( FunctionType *function );
55
56        /// A function needs an adapter if it returns a dynamic-layout value or if any of its parameters have dynamic-layout type
57        bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVarr );
58        bool needsAdapter( ast::FunctionType const * adaptee, const TypeVarMap & typeVars );
59
60        /// returns polymorphic type if is pointer to polymorphic type, NULL otherwise; will look up substitution in env if provided
61        Type *isPolyPtr( Type *type, const TypeSubstitution *env = 0 );
62
63        /// returns polymorphic type if is pointer to polymorphic type in tyVars, NULL otherwise; will look up substitution in env if provided
64        Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 );
65        const ast::Type * isPolyPtr( const ast::Type * type, const TypeVarMap & typeVars, const ast::TypeSubstitution * env = 0 );
66
67        /// if the base type (after dereferencing N >= 0 pointers) is a polymorphic type, returns the base type, NULL otherwise;
68        /// N will be stored in levels, if provided, will look up substitution in env if provided
69        Type *hasPolyBase( Type *type, int *levels = 0, const TypeSubstitution *env = 0 );
70
71        /// if the base type (after dereferencing N >= 0 pointers) is a polymorphic type in tyVars, returns the base type, NULL otherwise;
72        /// N will be stored in levels, if provided, will look up substitution in env if provided
73        Type *hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels = 0, const TypeSubstitution *env = 0 );
74        const ast::Type * hasPolyBase( const ast::Type * type, const TypeVarMap & typeVars, int * levels = 0, const ast::TypeSubstitution * env = 0 );
75
76        /// true iff this type or some base of this type after dereferencing pointers is either polymorphic or a generic type with at least one
77        /// polymorphic parameter; will look up substitution in env if provided.
78        bool includesPolyType( Type *type, const TypeSubstitution *env = 0 );
79
80        /// true iff this type or some base of this type after dereferencing pointers is either polymorphic in tyVars, or a generic type with
81        /// at least one polymorphic parameter in tyVars; will look up substitution in env if provided.
82        bool includesPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 );
83
84        /// Returns a pointer to the base FunctionType if ty is the type of a function (or pointer to one), NULL otherwise
85        FunctionType *getFunctionType( Type *ty );
86        const ast::FunctionType * getFunctionType( const ast::Type * ty );
87
88        /// If expr (after dereferencing N >= 0 pointers) is a variable expression, returns the variable expression, NULL otherwise;
89        /// N will be stored in levels, if provided
90        VariableExpr *getBaseVar( Expression *expr, int *levels = 0 );
91
92        /// true iff types are structurally identical, where TypeInstType's match any type.
93        bool typesPolyCompatible( Type *aty, Type *bty );
94        bool typesPolyCompatible( ast::Type const * lhs, ast::Type const * rhs );
95
96        /// true if arg requires boxing given exprTyVars
97        bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, const TypeSubstitution * env );
98        bool needsBoxing( const ast::Type * param, const ast::Type * arg, const TypeVarMap & typeVars, const ast::TypeSubstitution * subst );
99
100        /// true if arg requires boxing in the call to appExpr
101        bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, const TypeSubstitution * env );
102        bool needsBoxing( const ast::Type * param, const ast::Type * arg, const ast::ApplicationExpr * expr, const ast::TypeSubstitution * subst );
103
104        /// Adds the type variable `tyVar` to `tyVarMap`
105        void addToTyVarMap( TypeDecl * tyVar, TyVarMap &tyVarMap );
106        void addToTypeVarMap( const ast::TypeDecl * type, TypeVarMap & typeVars );
107
108        /// Adds the declarations in the forall list of type (and its pointed-to type if it's a pointer type) to `tyVarMap`
109        void makeTyVarMap( Type *type, TyVarMap &tyVarMap );
110        void makeTypeVarMap( const ast::Type * type, TypeVarMap & typeVars );
111
112        /// Prints type variable map
113        void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap );
114        void printTypeVarMap( std::ostream &os, const TypeVarMap & typeVars );
115
116        /// Gets the mangled name of this type; alias for SymTab::Mangler::mangleType().
117        inline std::string mangleType( Type *ty ) { return SymTab::Mangler::mangleType( ty ); }
118
119        /// Gets the name of the sizeof parameter for the type, given its mangled name
120        inline std::string sizeofName( const std::string &name ) { return std::string( "_sizeof_" ) + name; }
121
122        /// Gets the name of the alignof parameter for the type, given its mangled name
123        inline std::string alignofName( const std::string &name ) { return std::string( "_alignof_" ) + name; }
124
125        /// Gets the name of the offsetof parameter for the type, given its mangled name
126        inline std::string offsetofName( const std::string &name ) { return std::string( "_offsetof_" ) + name; }
127
128        /// Gets the name of the layout function for a given aggregate type, given its declaration
129        inline std::string layoutofName( AggregateDecl *decl ) { return std::string( "_layoutof_" ) + decl->get_name(); }
130
131} // namespace GenPoly
132
133// Local Variables: //
134// tab-width: 4 //
135// mode: c++ //
136// compile-command: "make install" //
137// End: //
Note: See TracBrowser for help on using the repository browser.