source: src/GenPoly/GenPoly.h@ 93c10de

ADT ast-experimental
Last change on this file since 93c10de was 93c10de, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Minimal changes to pull out nested types, TypeInstType::TypeEnvKey and TypeDecl::Data (now TypeData) from there parent types. Although they do connect to the parent types they were nested in they are used on their own most of the time.

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