P4C
The P4 Compiler
Loading...
Searching...
No Matches
P4::TypeInferenceBase Class Reference
Inheritance diagram for P4::TypeInferenceBase:
[legend]

Classes

struct  Comparison
 

Public Types

using PreorderResult = std::pair<const IR::Node *, bool>
 
- Public Types inherited from P4::Visitor
typedef Visitor_Context Context
 

Public Member Functions

 TypeInferenceBase (TypeMap *typeMap, bool readOnly=false, bool checkArrays=true, bool errorOnNullDecls=false)
 
bool compare (const IR::Node *errorPosition, const IR::Type *ltype, const IR::Type *rtype, Comparison *compare)
 
void finish (const IR::Node *Node)
 
bool learn (const IR::Node *node, Visitor *caller, const Visitor::Context *ctxt)
 
const IR::Nodepostorder (const IR::ActionList *al)
 
const IR::Nodepostorder (const IR::ActionListElement *elem)
 
const IR::Nodepostorder (const IR::Add *expression)
 
const IR::Nodepostorder (const IR::AddSat *expression)
 
const IR::Nodepostorder (const IR::Annotation *annotation)
 
const IR::Nodepostorder (const IR::Argument *arg)
 
const IR::Nodepostorder (const IR::ArrayIndex *expression)
 
const IR::Nodepostorder (const IR::AssignmentStatement *stat)
 
const IR::Nodepostorder (const IR::AttribLocal *local)
 
const IR::Nodepostorder (const IR::BAnd *expression)
 
const IR::Nodepostorder (const IR::BoolLiteral *expression)
 
const IR::Nodepostorder (const IR::BOr *expression)
 
const IR::Nodepostorder (const IR::BXor *expression)
 
const IR::Nodepostorder (const IR::Cast *expression)
 
const IR::Nodepostorder (const IR::Cmpl *expression)
 
const IR::Nodepostorder (const IR::Concat *expression)
 
const IR::Nodepostorder (const IR::Constant *expression)
 
const IR::Nodepostorder (const IR::ConstructorCallExpression *expression)
 
const IR::Nodepostorder (const IR::Declaration_Constant *constant)
 
const IR::Nodepostorder (const IR::Declaration_MatchKind *decl)
 
const IR::Nodepostorder (const IR::Declaration_Variable *decl)
 
const IR::Nodepostorder (const IR::DefaultExpression *expression)
 
const IR::Nodepostorder (const IR::Div *expression)
 
const IR::Nodepostorder (const IR::Dots *expression)
 
const IR::Nodepostorder (const IR::Entry *e)
 
const IR::Nodepostorder (const IR::ForInStatement *stat)
 
const IR::Nodepostorder (const IR::HeaderStackExpression *expression)
 
const IR::Nodepostorder (const IR::IfStatement *stat)
 
const IR::Nodepostorder (const IR::Invalid *expression)
 
const IR::Nodepostorder (const IR::InvalidHeader *expression)
 
const IR::Nodepostorder (const IR::InvalidHeaderUnion *expression)
 
const IR::Nodepostorder (const IR::Key *key)
 
const IR::Nodepostorder (const IR::KeyElement *elem)
 
const IR::Nodepostorder (const IR::LAnd *expression)
 
const IR::Nodepostorder (const IR::ListExpression *expression)
 
const IR::Nodepostorder (const IR::LNot *expression)
 
const IR::Nodepostorder (const IR::LOr *expression)
 
const IR::Nodepostorder (const IR::Mask *expression)
 
const IR::Nodepostorder (const IR::Member *expression)
 
const IR::Nodepostorder (const IR::Method *method)
 
const IR::Nodepostorder (const IR::MethodCallExpression *expression)
 
const IR::Nodepostorder (const IR::MethodCallStatement *mcs)
 
const IR::Nodepostorder (const IR::Mod *expression)
 
const IR::Nodepostorder (const IR::Mul *expression)
 
const IR::Nodepostorder (const IR::Mux *expression)
 
const IR::Nodepostorder (const IR::Neg *expression)
 
const IR::Nodepostorder (const IR::Operation_Relation *expression)
 
const IR::Nodepostorder (const IR::P4Action *type)
 
const IR::Nodepostorder (const IR::P4Control *cont)
 
const IR::Nodepostorder (const IR::P4ListExpression *expression)
 
const IR::Nodepostorder (const IR::P4Parser *cont)
 
const IR::Nodepostorder (const IR::P4Table *type)
 
const IR::Nodepostorder (const IR::P4ValueSet *type)
 
const IR::Nodepostorder (const IR::Parameter *param)
 
const IR::Nodepostorder (const IR::PathExpression *expression)
 
const IR::Nodepostorder (const IR::Property *elem)
 
const IR::Nodepostorder (const IR::Range *expression)
 
const IR::Nodepostorder (const IR::ReturnStatement *stat)
 
const IR::Nodepostorder (const IR::SelectCase *elem)
 
const IR::Nodepostorder (const IR::SelectExpression *expression)
 
const IR::Nodepostorder (const IR::SerEnumMember *member)
 
const IR::Nodepostorder (const IR::Shl *expression)
 
const IR::Nodepostorder (const IR::Shr *expression)
 
const IR::Nodepostorder (const IR::Slice *expression)
 
const IR::Nodepostorder (const IR::StringLiteral *expression)
 
const IR::Nodepostorder (const IR::StructExpression *expression)
 
const IR::Nodepostorder (const IR::StructField *field)
 
const IR::Nodepostorder (const IR::Sub *expression)
 
const IR::Nodepostorder (const IR::SubSat *expression)
 
const IR::Nodepostorder (const IR::SwitchStatement *stat)
 
const IR::Nodepostorder (const IR::This *expression)
 
const IR::Nodepostorder (const IR::Type_Action *type)
 
const IR::Nodepostorder (const IR::Type_ActionEnum *type)
 
const IR::Nodepostorder (const IR::Type_ArchBlock *type)
 
const IR::Nodepostorder (const IR::Type_Base *type)
 
const IR::Nodepostorder (const IR::Type_Enum *type)
 
const IR::Nodepostorder (const IR::Type_Error *decl)
 
const IR::Nodepostorder (const IR::Type_Extern *type)
 
const IR::Nodepostorder (const IR::Type_Header *type)
 
const IR::Nodepostorder (const IR::Type_HeaderUnion *type)
 
const IR::Nodepostorder (const IR::Type_InfInt *type)
 
const IR::Nodepostorder (const IR::Type_List *type)
 
const IR::Nodepostorder (const IR::Type_Method *type)
 
const IR::Nodepostorder (const IR::Type_Name *type)
 
const IR::Nodepostorder (const IR::Type_Newtype *type)
 
const IR::Nodepostorder (const IR::Type_P4List *type)
 
const IR::Nodepostorder (const IR::Type_Package *type)
 
const IR::Nodepostorder (const IR::Type_Set *type)
 
const IR::Nodepostorder (const IR::Type_Specialized *type)
 
const IR::Nodepostorder (const IR::Type_SpecializedCanonical *type)
 
const IR::Nodepostorder (const IR::Type_Stack *type)
 
const IR::Nodepostorder (const IR::Type_Struct *type)
 
const IR::Nodepostorder (const IR::Type_Table *type)
 
const IR::Nodepostorder (const IR::Type_Tuple *type)
 
const IR::Nodepostorder (const IR::Type_Type *type)
 
const IR::Nodepostorder (const IR::Type_Typedef *type)
 
const IR::Nodepostorder (const IR::Type_Var *type)
 
const IR::Nodepostorder (const IR::TypeNameExpression *expression)
 
const IR::Nodepostorder (const IR::UPlus *expression)
 
PreorderResult preorder (const IR::Declaration_Instance *decl)
 
PreorderResult preorder (const IR::EntriesList *el)
 
PreorderResult preorder (const IR::Function *function)
 
PreorderResult preorder (const IR::P4Program *program)
 
PreorderResult preorder (const IR::Type_SerEnum *type)
 
PreorderResult preorder (IR::Declaration_Instance *decl)
 
PreorderResult preorder (IR::Function *function)
 
ReadOnlyTypeInferencereadOnlyClone () const
 
void start (const IR::Node *node)
 
- Public Member Functions inherited from P4::Visitor
virtual const IR::Nodeapply_visitor (const IR::Node *n, const char *name=0)=0
 
virtual bool check_clone (const Visitor *a)
 
virtual bool check_global (cstring)
 
virtual void clear_globals ()
 
virtual Visitorclone () const
 
virtual ControlFlowVisitorcontrolFlowVisitor ()
 
virtual void end_apply ()
 
virtual void end_apply (const IR::Node *root)
 
virtual void erase_global (cstring)
 
template<class T >
const T * findContext () const
 
template<class T >
const T * findContext (const Context *&c) const
 
template<class T >
const T * findOrigCtxt () const
 
template<class T >
const T * findOrigCtxt (const Context *&c) const
 
virtual Visitorflow_clone ()
 
virtual void flow_merge (Visitor &)
 
virtual bool flow_merge_closure (Visitor &)
 
virtual void flow_merge_global_from (cstring)
 
virtual void flow_merge_global_to (cstring)
 
const ContextgetChildContext () const
 
int getChildrenVisited () const
 
const ContextgetContext () const
 
int getContextDepth () const
 
const IR::NodegetCurrentNode () const
 
template<class T >
const T * getCurrentNode () const
 
const IR::NodegetOriginal () const
 
template<class T >
const T * getOriginal () const
 
template<class T >
const T * getParent () const
 
virtual bool has_flow_joins () const
 
virtual profile_t init_apply (const IR::Node *root)
 
profile_t init_apply (const IR::Node *root, const Context *parent_context)
 
bool isInContext (const IR::Node *n) const
 
virtual const char * name () const
 
template<class T >
void parallel_visit (const IR::Vector< T > &v, const char *name, int cidx)
 
template<class T >
void parallel_visit (const IR::Vector< T > &v, const char *name=0)
 
template<class T >
void parallel_visit (IR::Vector< T > &v, const char *name, int cidx)
 
template<class T >
void parallel_visit (IR::Vector< T > &v, const char *name=0)
 
void print_context () const
 
const VisitorsetCalledBy (const Visitor *visitor)
 
void setName (const char *name)
 
void visit (const IR::Node &n, const char *name, int cidx)
 
void visit (const IR::Node &n, const char *name=0)
 
void visit (const IR::Node *&n, const char *name, int cidx)
 
void visit (const IR::Node *&n, const char *name=0)
 
void visit (const IR::Node *const &n, const char *name, int cidx)
 
void visit (const IR::Node *const &n, const char *name=0)
 
void visit (IR::Node &n, const char *name, int cidx)
 
void visit (IR::Node &n, const char *name=0)
 
void visit (IR::Node *&, const char *=0, int=0)
 
template<class T , typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>, class... Args>
void warn (const int kind, const char *format, const T &node, Args &&...args)
 The const ref variant of the above.
 
template<class T , typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
void warn (const int kind, const char *format, const T *node, Args &&...args)
 
bool warning_enabled (int warning_kind) const
 
- Public Member Functions inherited from P4::ResolutionContext
const IR::IDeclarationgetDeclaration (const IR::Path *path, bool notNull=false) const
 
const IR::IDeclarationgetDeclaration (const IR::This *, bool notNull=false) const
 
auto getDeclarations (const IR::INamespace *ns) const
 Returns the set of decls that exist in the given namespace.
 
auto getDeclsByName (const IR::INamespace *ns, cstring name) const
 Returns the set of decls with the given name that exist in the given namespace.
 
std::vector< const IR::IDeclaration * > resolve (const IR::ID &name, ResolutionType type) const
 Resolve references for name, restricted to type declarations.
 
virtual const IR::IDeclarationresolvePath (const IR::Path *path, bool isType) const
 
const IR::Type * resolveType (const IR::Type *type) const
 Resolve a refrence to a type type.
 
const IR::IDeclarationresolveUnique (const IR::ID &name, ResolutionType type, const IR::INamespace *=nullptr) const
 Resolve reference for name, restricted to type declarations, and expect one result.
 
- Public Member Functions inherited from P4::DeclarationLookup

Static Public Member Functions

static const IR::Type * specialize (const IR::IMayBeGenericType *type, const IR::Vector< IR::Type > *arguments, const Visitor::Context *ctxt)
 
- Static Public Member Functions inherited from P4::Visitor
static cstring demangle (const char *)
 
static bool warning_enabled (const Visitor *visitor, int warning_kind)
 

Protected Member Functions

 TypeInferenceBase (TypeMap *typeMap, std::shared_ptr< MinimalNameGenerator > nameGen)
 
const IR::Expression * actionCall (bool inActionList, const IR::MethodCallExpression *actionCall)
 
void addSubstitutions (const TypeVariableSubstitution *tvs)
 
const IR::Expression * assignment (const IR::Node *errorPosition, const IR::Type *destType, const IR::Expression *sourceExpression)
 
const IR::NodebinaryArith (const IR::Operation_Binary *op)
 
const IR::NodebinaryBool (const IR::Operation_Binary *op)
 
bool canCastBetween (const IR::Type *dest, const IR::Type *src) const
 
virtual const IR::Type * canonicalize (const IR::Type *type)
 
template<class Ctor >
const IR::Type * canonicalizeFields (const IR::Type_StructLike *type, Ctor constructor)
 
virtual const IR::ParameterList * canonicalizeParameters (const IR::ParameterList *params)
 
bool checkAbstractMethods (const IR::Declaration_Instance *inst, const IR::Type_Extern *type)
 
std::pair< const IR::Type *, const IR::Vector< IR::Argument > * > checkExternConstructor (const IR::Node *errorPosition, const IR::Type_Extern *ext, const IR::Vector< IR::Argument > *arguments)
 
bool checkParameters (const IR::ParameterList *paramList, bool forbidModules=false, bool forbidPackage=false) const
 
const IR::Type_Bits * checkUnderlyingEnumType (const IR::Type *enumType)
 
const IR::Type * cloneWithFreshTypeVariables (const IR::IMayBeGenericType *type)
 
const IR::Expression * constantFold (const IR::Expression *expression)
 
std::pair< const IR::Type *, const IR::Vector< IR::Argument > * > containerInstantiation (const IR::Node *node, const IR::Vector< IR::Argument > *args, const IR::IContainer *container)
 
bool containsActionEnum (const IR::Type *type) const
 
bool containsHeader (const IR::Type *canonType)
 
bool done () const
 
const IR::NodegetInitialNode () const
 
const IR::Type * getType (const IR::Node *element) const
 
const IR::Type * getTypeType (const IR::Node *element) const
 
bool isCompileTimeConstant (const IR::Expression *expression) const
 
bool isLeftValue (const IR::Expression *expression) const
 
const IR::SelectCase * matchCase (const IR::SelectExpression *select, const IR::Type_BaseList *selectType, const IR::SelectCase *selectCase, const IR::Type *caseType)
 
bool onlyBitsOrBitStructs (const IR::Type *type) const
 
template<class Node >
PreorderResult preorderDeclarationInstanceImpl (Node *decl)
 
template<class Node >
PreorderResult preorderFunctionImpl (Node *function)
 
void setCompileTimeConstant (const IR::Expression *expression)
 
void setLeftValue (const IR::Expression *expression)
 
void setType (const IR::Node *element, const IR::Type *type)
 
virtual const IR::Type * setTypeType (const IR::Type *type, bool learn=true)
 
const IR::Nodeshift (const IR::Operation_Binary *op)
 
const IR::NodetypeSet (const IR::Operation_Binary *op)
 
TypeVariableSubstitutionunify (const IR::Node *errorPosition, const IR::Type *destType, const IR::Type *srcType, std::string_view errorFormat={}, std::initializer_list< const IR::Node * > errorArgs={})
 Same as above, not allowing casts.
 
TypeVariableSubstitutionunifyBase (bool allowCasts, const IR::Node *errorPosition, const IR::Type *destType, const IR::Type *srcType, std::string_view errorFormat, std::initializer_list< const IR::Node * > errorArgs)
 
TypeVariableSubstitutionunifyCast (const IR::Node *errorPosition, const IR::Type *destType, const IR::Type *srcType, std::string_view errorFormat={}, std::initializer_list< const IR::Node * > errorArgs={})
 
const IR::NodeunsBinaryArith (const IR::Operation_Binary *op)
 
const IR::ActionListElement * validateActionInitializer (const IR::Expression *actionCall)
 
bool validateFields (const IR::Type *type, std::function< bool(const IR::Type *)> checker) const
 
- Protected Member Functions inherited from P4::Visitor
virtual void init_join_flows (const IR::Node *)
 
virtual bool join_flows (const IR::Node *)
 
virtual void post_join_flows (const IR::Node *, const IR::Node *)
 
void visit_children (const IR::Node *, std::function< void()> fn)
 
virtual void visitAgain () const
 
virtual void visitOnce () const
 
- Protected Member Functions inherited from P4::ResolutionContext
 ResolutionContext (bool ao)
 
std::vector< const IR::IDeclaration * > lookup (const IR::INamespace *ns, const IR::ID &name, ResolutionType type) const
 
std::vector< const IR::IDeclaration * > lookupMatchKind (const IR::ID &name) const
 
const IR::Vector< IR::Argument > * methodArguments (cstring name) const
 We are resolving a method call. Find the arguments from the context.
 

Protected Attributes

bool checkArrays = true
 
const IR::ActionList * currentActionList
 Action list of the current table.
 
bool errorOnNullDecls = false
 
bool readOnly = false
 
- Protected Attributes inherited from P4::Visitor
bool dontForwardChildrenBeforePreorder = false
 
bool joinFlows = false
 
bool visitDagOnce = true
 
- Protected Attributes inherited from P4::ResolutionContext
bool anyOrder = false
 

Static Protected Attributes

static constexpr bool forbidModules = true
 
static constexpr bool forbidPackages = true
 

Additional Inherited Members

- Public Attributes inherited from P4::Visitor
const Visitorcalled_by = nullptr
 
cstring internalName
 
SplitFlowVisit_base *& split_link
 
SplitFlowVisit_basesplit_link_mem = nullptr
 

Class Documentation

◆ P4::TypeInferenceBase::Comparison

struct P4::TypeInferenceBase::Comparison
Class Members
const Expression * left
const Expression * right

Member Function Documentation

◆ assignment()

const IR::Expression * P4::TypeInferenceBase::assignment ( const IR::Node * errorPosition,
const IR::Type * destType,
const IR::Expression * sourceExpression )
protected

Tries to assign sourceExpression to a destination with type destType. This may rewrite the sourceExpression, in particular converting InfInt values to values with concrete types.

Returns
new sourceExpression.

◆ canonicalize()

const IR::Type * P4::TypeInferenceBase::canonicalize ( const IR::Type * type)
protectedvirtual

Converts each type to a canonical representation. Made virtual to enable private midend passes to extend standard IR with custom IR classes.

◆ checkUnderlyingEnumType()

const IR::Type_Bits * P4::TypeInferenceBase::checkUnderlyingEnumType ( const IR::Type * enumType)
protected

Check if the underlying type for enum is bit<> or int<> and emit error if it is not.

Returns
the resolved type, or nullptr if the type is invalid

◆ containerInstantiation()

std::pair< const IR::Type *, const IR::Vector< IR::Argument > * > P4::TypeInferenceBase::containerInstantiation ( const IR::Node * node,
const IR::Vector< IR::Argument > * constructorArguments,
const IR::IContainer * container )
protected
Returns
: A pair containing the type returned by the constructor and the new arguments (which may change due to insertion of casts).

◆ postorder() [1/2]

const IR::Node * P4::TypeInferenceBase::postorder ( const IR::Entry * entry)

typecheck a table initializer entry

The invariants are:

  • table keys and entry keys must have the same length
  • entry key elements must be compile time constants
  • actionRefs in entries must be in the action list
  • table keys must have been type checked before entries

Moreover, the EntriesList visitor should have checked for the table invariants.

◆ postorder() [2/2]

const IR::Node * P4::TypeInferenceBase::postorder ( const IR::Key * key)

compute the type of table keys. Used to typecheck pre-defined entries.

◆ preorder()

TypeInferenceBase::PreorderResult P4::TypeInferenceBase::preorder ( const IR::EntriesList * el)

typecheck a table initializer entry list

◆ specialize()

const IR::Type * P4::TypeInferenceBase::specialize ( const IR::IMayBeGenericType * type,
const IR::Vector< IR::Type > * arguments,
const Visitor::Context * ctxt )
static

Bind the parameters with the specified arguments. For example, given a type void _<T>(T data) it can be specialized to void _<int<32>>(int<32> data);

◆ unifyCast()

TypeVariableSubstitution * P4::TypeInferenceBase::unifyCast ( const IR::Node * errorPosition,
const IR::Type * destType,
const IR::Type * srcType,
std::string_view errorFormat = {},
std::initializer_list< const IR::Node * > errorArgs = {} )
inlineprotected

Unifies two types. Returns nullptr if unification fails. Populates the typeMap with values for the type variables. This allows an implicit cast from the right type to the left type.

◆ validateActionInitializer()

const IR::ActionListElement * P4::TypeInferenceBase::validateActionInitializer ( const IR::Expression * actionCall)
protected

This is used to validate the initializer for the default_action or for actions in the entries list. Returns the action list element on success.

◆ validateFields()

bool P4::TypeInferenceBase::validateFields ( const IR::Type * type,
std::function< bool(const IR::Type *)> checker ) const
protected

Validate the fields of a struct type using the supplied checker. The checker returns "false" when a field is invalid. Return true on success