P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
P4::TypeInference Class Reference
Inheritance diagram for P4::TypeInference:

Classes

struct  Comparison
 

Public Member Functions

 TypeInference (ReferenceMap *refMap, TypeMap *typeMap, bool readOnly=false, bool checkArrays=true)
 
const IR::Node * apply_visitor (const IR::Node *, const char *name=0) override
 
TypeInferenceclone () const override
 
bool compare (const IR::Node *errorPosition, const IR::Type *ltype, const IR::Type *rtype, Comparison *compare)
 
void end_apply (const IR::Node *Node) override
 
Visitor::profile_t init_apply (const IR::Node *node) override
 
bool learn (const IR::Node *node, Visitor *caller)
 
const IR::Node * postorder (IR::ActionList *al) override
 
const IR::Node * postorder (IR::ActionListElement *elem) override
 
const IR::Node * postorder (IR::Add *expression) override
 
const IR::Node * postorder (IR::AddSat *expression) override
 
const IR::Node * postorder (IR::Annotation *annotation) override
 
const IR::Node * postorder (IR::Argument *arg) override
 
const IR::Node * postorder (IR::ArrayIndex *expression) override
 
const IR::Node * postorder (IR::AssignmentStatement *stat) override
 
const IR::Node * postorder (IR::AttribLocal *local) override
 
const IR::Node * postorder (IR::BAnd *expression) override
 
const IR::Node * postorder (IR::BoolLiteral *expression) override
 
const IR::Node * postorder (IR::BOr *expression) override
 
const IR::Node * postorder (IR::BXor *expression) override
 
const IR::Node * postorder (IR::Cast *expression) override
 
const IR::Node * postorder (IR::Cmpl *expression) override
 
const IR::Node * postorder (IR::Concat *expression) override
 
const IR::Node * postorder (IR::Constant *expression) override
 
const IR::Node * postorder (IR::ConstructorCallExpression *expression) override
 
const IR::Node * postorder (IR::Declaration_Constant *constant) override
 
const IR::Node * postorder (IR::Declaration_MatchKind *decl) override
 
const IR::Node * postorder (IR::Declaration_Variable *decl) override
 
const IR::Node * postorder (IR::DefaultExpression *expression) override
 
const IR::Node * postorder (IR::Div *expression) override
 
const IR::Node * postorder (IR::Dots *expression) override
 
const IR::Node * postorder (IR::Entry *e) override
 
const IR::Node * postorder (IR::HeaderStackExpression *expression) override
 
const IR::Node * postorder (IR::IfStatement *stat) override
 
const IR::Node * postorder (IR::Invalid *expression) override
 
const IR::Node * postorder (IR::InvalidHeader *expression) override
 
const IR::Node * postorder (IR::InvalidHeaderUnion *expression) override
 
const IR::Node * postorder (IR::Key *key) override
 
const IR::Node * postorder (IR::KeyElement *elem) override
 
const IR::Node * postorder (IR::LAnd *expression) override
 
const IR::Node * postorder (IR::ListExpression *expression) override
 
const IR::Node * postorder (IR::LNot *expression) override
 
const IR::Node * postorder (IR::LOr *expression) override
 
const IR::Node * postorder (IR::Mask *expression) override
 
const IR::Node * postorder (IR::Member *expression) override
 
const IR::Node * postorder (IR::Method *method) override
 
const IR::Node * postorder (IR::MethodCallExpression *expression) override
 
const IR::Node * postorder (IR::Mod *expression) override
 
const IR::Node * postorder (IR::Mul *expression) override
 
const IR::Node * postorder (IR::Mux *expression) override
 
const IR::Node * postorder (IR::Neg *expression) override
 
const IR::Node * postorder (IR::Operation_Relation *expression) override
 
const IR::Node * postorder (IR::P4Action *type) override
 
const IR::Node * postorder (IR::P4Control *cont) override
 
const IR::Node * postorder (IR::P4ListExpression *expression) override
 
const IR::Node * postorder (IR::P4Parser *cont) override
 
const IR::Node * postorder (IR::P4Table *type) override
 
const IR::Node * postorder (IR::P4ValueSet *type) override
 
const IR::Node * postorder (IR::Parameter *param) override
 
const IR::Node * postorder (IR::PathExpression *expression) override
 
const IR::Node * postorder (IR::Property *elem) override
 
const IR::Node * postorder (IR::Range *expression) override
 
const IR::Node * postorder (IR::ReturnStatement *stat) override
 
const IR::Node * postorder (IR::SelectCase *elem) override
 
const IR::Node * postorder (IR::SelectExpression *expression) override
 
const IR::Node * postorder (IR::SerEnumMember *member) override
 
const IR::Node * postorder (IR::Shl *expression) override
 
const IR::Node * postorder (IR::Shr *expression) override
 
const IR::Node * postorder (IR::Slice *expression) override
 
const IR::Node * postorder (IR::StringLiteral *expression) override
 
const IR::Node * postorder (IR::StructExpression *expression) override
 
const IR::Node * postorder (IR::StructField *field) override
 
const IR::Node * postorder (IR::Sub *expression) override
 
const IR::Node * postorder (IR::SubSat *expression) override
 
const IR::Node * postorder (IR::SwitchStatement *stat) override
 
const IR::Node * postorder (IR::This *expression) override
 
const IR::Node * postorder (IR::Type_Action *type) override
 
const IR::Node * postorder (IR::Type_ActionEnum *type) override
 
const IR::Node * postorder (IR::Type_ArchBlock *type) override
 
const IR::Node * postorder (IR::Type_Base *type) override
 
const IR::Node * postorder (IR::Type_Enum *type) override
 
const IR::Node * postorder (IR::Type_Error *decl) override
 
const IR::Node * postorder (IR::Type_Extern *type) override
 
const IR::Node * postorder (IR::Type_Header *type) override
 
const IR::Node * postorder (IR::Type_HeaderUnion *type) override
 
const IR::Node * postorder (IR::Type_InfInt *type) override
 
const IR::Node * postorder (IR::Type_List *type) override
 
const IR::Node * postorder (IR::Type_Method *type) override
 
const IR::Node * postorder (IR::Type_Name *type) override
 
const IR::Node * postorder (IR::Type_Newtype *type) override
 
const IR::Node * postorder (IR::Type_P4List *type) override
 
const IR::Node * postorder (IR::Type_Package *type) override
 
const IR::Node * postorder (IR::Type_Set *type) override
 
const IR::Node * postorder (IR::Type_Specialized *type) override
 
const IR::Node * postorder (IR::Type_SpecializedCanonical *type) override
 
const IR::Node * postorder (IR::Type_Stack *type) override
 
const IR::Node * postorder (IR::Type_Struct *type) override
 
const IR::Node * postorder (IR::Type_Table *type) override
 
const IR::Node * postorder (IR::Type_Tuple *type) override
 
const IR::Node * postorder (IR::Type_Type *type) override
 
const IR::Node * postorder (IR::Type_Typedef *type) override
 
const IR::Node * postorder (IR::Type_Var *type) override
 
const IR::Node * postorder (IR::TypeNameExpression *expression) override
 
const IR::Node * postorder (IR::UPlus *expression) override
 
const IR::Node * preorder (IR::Declaration_Instance *decl) override
 
const IR::Node * preorder (IR::EntriesList *el) override
 
const IR::Node * preorder (IR::Expression *expression) override
 
const IR::Node * preorder (IR::Function *function) override
 
const IR::Node * preorder (IR::P4Program *program) override
 
const IR::Node * preorder (IR::Type *type) override
 
const IR::Node * preorder (IR::Type_SerEnum *type) override
 
const IR::Node * pruneIfDone (const IR::Node *node)
 

Static Public Member Functions

static const IR::Type * specialize (const IR::IMayBeGenericType *type, const IR::Vector< IR::Type > *arguments)
 

Protected Member Functions

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::Node * binaryArith (const IR::Operation_Binary *op)
 
const IR::Node * binaryBool (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)
 
const IR::Type * canonicalizeFields (const IR::Type_StructLike *type, std::function< const IR::Type *(const IR::IndexedVector< IR::StructField > *)> 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::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
 
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::Node * shift (const IR::Operation_Binary *op)
 
const IR::Node * typeSet (const IR::Operation_Binary *op)
 
TypeVariableSubstitutionunify (const IR::Node *errorPosition, const IR::Type *destType, const IR::Type *srcType, cstring errorFormat=nullptr, 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, cstring errorFormat, std::initializer_list< const IR::Node * > errorArgs)
 
TypeVariableSubstitutionunifyCast (const IR::Node *errorPosition, const IR::Type *destType, const IR::Type *srcType, cstring errorFormat=nullptr, std::initializer_list< const IR::Node * > errorArgs={})
 
const IR::Node * unsBinaryArith (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 Attributes

bool checkArrays = true
 
const IR::ActionList * currentActionList
 Action list of the current table.
 
bool readOnly
 

Static Protected Attributes

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

Class Documentation

◆ P4::TypeInference::Comparison

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

Member Function Documentation

◆ assignment()

const IR::Expression * P4::TypeInference::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::TypeInference::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::TypeInference::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::TypeInference::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::TypeInference::postorder ( IR::Entry * entry)
override

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::TypeInference::postorder ( IR::Key * key)
override

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

◆ preorder()

const IR::Node * P4::TypeInference::preorder ( IR::EntriesList * el)
override

typecheck a table initializer entry list

◆ specialize()

const IR::Type * P4::TypeInference::specialize ( const IR::IMayBeGenericType * type,
const IR::Vector< IR::Type > * arguments )
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::TypeInference::unifyCast ( const IR::Node * errorPosition,
const IR::Type * destType,
const IR::Type * srcType,
cstring errorFormat = nullptr,
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::TypeInference::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::TypeInference::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