8000 Add typed version of `isInContext()` and make use of it instead of `findContext()` by asl · Pull Request #5048 · p4lang/p4c · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add typed version of isInContext() and make use of it instead of findContext() #5048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions frontends/common/resolveReferences/resolveReferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void ResolveReferences::checkShadowing(const IR::INamespace *ns) const {
const IR::Node *node = decl->getNode();
if (node->is<IR::StructField>()) continue;

if (node->is<IR::Parameter>() && findContext<IR::Method>() != nullptr)
if (node->is<IR::Parameter>() && isInContext<IR::Method>())
// do not give shadowing warnings for parameters of extern methods
continue;

Expand Down Expand Up @@ -397,7 +397,7 @@ void ResolveReferences::postorder(const IR::P4Program *) { LOG2("Reference map "

bool ResolveReferences::preorder(const IR::This *pointer) {
auto decl = findContext<IR::Declaration_Instance>();
if (findContext<IR::Function>() == nullptr || decl == nullptr) {
if (!isInContext<IR::Function>() || decl == nullptr) {
::P4::error(ErrorType::ERR_INVALID,
"'%1%' can only be used in the definition of an abstract method", pointer);
return false;
Expand Down
5 changes: 2 additions & 3 deletions frontends/p4/actionsInlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ void DiscoverActionsInlining::postorder(const IR::MethodCallStatement *mcs) {
if (ac == nullptr) return;
auto caller = findContext<IR::P4Action>();
if (caller == nullptr) {
if (findContext<IR::P4Parser>() != nullptr) {
if (isInContext<IR::P4Parser>()) {
::P4::error(ErrorType::ERR_UNSUPPORTED, "%1%: action invocation in parser not allowed",
mcs);
} else if (findContext<IR::P4Control>() == nullptr) {
BUG("%1%: unexpected action invocation", mcs);
}
BUG_CHECK(isInContext<IR::P4Control>(), "%1%: unexpected action invocation", mcs);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/checkCoreMethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void DoCheckCoreMethods::postorder(const IR::MethodCallExpression *expression) {

// Check that verify is only invoked from parsers.
if (auto ef = mi->to<ExternFunction>()) {
if (ef->method->name == IR::ParserState::verify && !findContext<IR::P4Parser>()) {
if (ef->method->name == IR::ParserState::verify && !isInContext<IR::P4Parser>()) {
typeError("%1%: may only be invoked in parsers", ef->expr);
}
}
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/def_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ bool ComputeWriteSet::setDefinitions(Definitions *defs, const IR::Node *node, bo
// Since parsers allow revisiting states multiple times, we allow
// overwriting always in parser states. In this case we actually expect
// that the definitions are monotonically increasing.
if (findContext<IR::ParserState>()) overwrite = true;
if (isInContext<IR::ParserState>()) overwrite = true;
// Loop bodies get visited repeatedly until a fixed point, so we likewise
// expect monotonically increasing write sets.
if (continueDefinitions != nullptr) overwrite = true; // in a loop
Expand Down
7 changes: 3 additions & 4 deletions frontends/p4/localizeActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ParamCloner : public CloneExpressions {
} // namespace

const IR::Node *TagGlobalActions::preorder(IR::P4Action *action) {
if (findContext<IR::P4Control>() == nullptr) {
if (!isInContext<IR::P4Control>()) {
cstring name = absl::StrCat(".", action->name);
action->addAnnotationIfNew(IR::Annotation::nameAnnotation, new IR::StringLiteral(name),
false);
Expand All @@ -58,7 +58,7 @@ Visitor::profile_t FindGlobalActionUses::init_apply(const IR::Node *node) {
}

bool FindGlobalActionUses::preorder(const IR::P4Action *action) {
if (findContext<IR::P4Control>() == nullptr) globalActions.emplace(action);
if (!isInContext<IR::P4Control>()) globalActions.emplace(action);
return false;
}

Expand Down Expand Up @@ -126,8 +126,7 @@ bool FindRepeatedActionUses::preorder(const IR::PathExpression *expression) {
auto decl = getDeclaration(expression->path, true);
if (!decl->is<IR::P4Action>()) return false;
auto action = decl->to<IR::P4Action>();
auto control = findContext<IR::P4Control>();
if (control == nullptr)
if (!isInContext<IR::P4Control>())
// not within a control; ignore.
return false;

Expand Down
8 changes: 4 additions & 4 deletions frontends/p4/moveDeclarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ limitations under the License.
namespace P4 {

const IR::Node *MoveDeclarations::postorder(IR::P4Action *action) {
if (findContext<IR::P4Control>() == nullptr) {
if (!isInContext<IR::P4Control>()) {
// Else let the parent control get these
IR::IndexedVector<IR::StatOrDecl> body;
const auto &m = getMoves();
Expand Down Expand Up @@ -87,8 +87,8 @@ const IR::Node *MoveDeclarations::postorder(IR::Declaration_Variable *decl) {
}

const IR::Node *MoveDeclarations::postorder(IR::Declaration_Constant *decl) {
if (findContext<IR::P4Control>() == nullptr && findContext<IR::P4Action>() == nullptr &&
findContext<IR::P4Parser>() == nullptr)
if (!isInContext<IR::P4Control>() && !isInContext<IR::P4Action>() &&
!isInContext<IR::P4Parser>())
// This is a global declaration
return decl;
addMove(decl);
Expand Down Expand Up @@ -180,7 +180,7 @@ const IR::Node *MoveInitializers::postorder(IR::ParserState *state) {
}

const IR::Node *MoveInitializers::postorder(IR::Path *path) {
if (!findContext<IR::ParserState>()) return path;
if (!isInContext<IR::ParserState>()) return path;

if (!oldStart || !loopsBackToStart || path->name != IR::ParserState::start) return path;

Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/moveDeclarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MoveDeclarations : public Transform {
prune();
return action;
}
if (findContext<IR::P4Control>() == nullptr)
if (!isInContext<IR::P4Control>())
// If we are not in a control, move to the beginning of the action.
// Otherwise move to the control's beginning.
push();
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/parserControlFlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class IfInParser : public Inspector {
setName("IfInParser");
}
void postorder(const IR::IfStatement *) override {
if (findContext<IR::P4Parser>()) *found = true;
if (isInContext<IR::P4Parser>()) *found = true;
}
};

Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/redundantParsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const IR::Node *EliminateSubparserCalls::postorder(IR::MethodCallStatement *mcs)
// Only remove redundant parser applications in parser states. Parser applications
// made in illegal locations (such as control blocks) will be detected by and error
// will be reported in later passes (i.e. DiscoverInlining).
if (!findContext<IR::ParserState>()) return mcs;
if (!isInContext<IR::ParserState>()) return mcs;

auto mi = MethodInstance::resolve(mcs->methodCall, this, typeMap, true);
if (!mi->isApply()) return mcs;
Expand Down
5 changes: 2 additions & 3 deletions frontends/p4/removeParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ void FindActionParameters::postorder(const IR::MethodCallExpression *expression)
if (!mi->is<P4::ActionCall>()) return;
auto ac = mi->to<P4::ActionCall>();

auto table = findContext<IR::P4Table>();
if (table != nullptr) {
if (findContext<IR::ActionListElement>() != nullptr)
if (isInContext<IR::P4Table>()) {
if (isInContext<IR::ActionListElement>())
// These are processed elsewhere
return;
// This is probably the default_action; we must remove some parameters
Expand Down
6 changes: 3 additions & 3 deletions frontends/p4/removeReturns.cpp
10000
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const IR::Node *DoRemoveReturns::preorder(IR::P4Action *action) {

const IR::Node *DoRemoveReturns::preorder(IR::Function *function) {
// We leave returns in abstract functions alone
if (findContext<IR::Declaration_Instance>()) {
if (isInContext<IR::Declaration_Instance>()) {
prune();
return function;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ const IR::Node *DoRemoveReturns::preorder(IR::ReturnStatement *statement) {
left = new IR::PathExpression(statement->expression->type, returnedValue);
vec.push_back(new IR::AssignmentStatement(statement->srcInfo, left, statement->expression));
}
if (findContext<IR::LoopStatement>()) vec.push_back(new IR::BreakStatement);
if (isInContext<IR::LoopStatement>()) vec.push_back(new IR::BreakStatement);
return new IR::BlockStatement(std::move(vec));
}

Expand Down Expand Up @@ -287,7 +287,7 @@ const IR::Node *DoRemoveReturns::postorder(IR::LoopStatement *loop) {
if (hasReturned() == TernaryBool::Yes) set(TernaryBool::Maybe);

// only need to add an extra check for nested loops
if (!findContext<IR::LoopStatement>()) return loop;
if (!isInContext<IR::LoopStatement>()) return loop;
// only if the inner loop may have returned
if (hasReturned() == TernaryBool::No) return loop;

Expand Down
4 changes: 2 additions & 2 deletions frontends/p4/sideEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const IR::Node *DoSimplifyExpressions::preorder(IR::ArrayIndex *expression) {
auto type = typeMap->getType(getOriginal(), true);
if (SideEffects::check(getOriginal<IR::Expression>(), this, this, typeMap) ||
// if the expression appears as part of an argument also use a temporary for the index
findContext<IR::Argument>() != nullptr) {
isInContext<IR::Argument>()) {
visit(expression->left);
CHECK_NULL(expression->left);
visit(expression->right);
Expand Down Expand Up @@ -106,7 +106,7 @@ const IR::Node *DoSimplifyExpressions::preorder(IR::Member *expression) {
const IR::Expression *rv = expression;
if (SideEffects::check(getOriginal<IR::Expression>(), this, this, typeMap) ||
// This may be part of a left-value that is passed as an out argument
findContext<IR::Argument>() != nullptr) {
isInContext<IR::Argument>()) {
visit(expression->expr);
CHECK_NULL(expression->expr);

Expand Down
4 changes: 2 additions & 2 deletions frontends/p4/sideEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class TablesInKeys : public Inspector {
return Inspector::init_apply(node);
}
void postorder(const IR::MethodCallExpression *mce) override {
if (!findContext<IR::Key>()) return;
if (!isInContext<IR::Key>()) return;
HasTableApply hta(typeMap);
hta.setCalledBy(this);
(void)mce->apply(hta, getContext());
Expand All @@ -358,7 +358,7 @@ class TablesInActions : public Inspector {
public:
explicit TablesInActions(TypeMap *typeMap) : typeMap(typeMap) {}
void postorder(const IR::MethodCallExpression *expression) override {
if (findContext<IR::ActionList>()) {
if (isInContext<IR::ActionList>()) {
HasTableApply hta(typeMap);
hta.setCalledBy(this);
(void)expression->apply(hta, getContext());
Expand Down
4 changes: 2 additions & 2 deletions frontends/p4/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const IR::Node *DoSimplifyControlFlow::postorder(IR::BlockStatement *statement)
// Cannot remove these blocks
return statement;
}
bool inBlock = findContext<IR::Statement>() != nullptr;
bool inState = findContext<IR::ParserState>() != nullptr;
bool inBlock = isInContext<IR::Statement>();
bool inState = isInContext<IR::ParserState>();
if (!(inBlock || inState)) return statement;

if (parent->is<IR::BlockStatement>() || parent->is<IR::ParserState>()) {
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/simplifyDefUse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ class FindUninitialized : public Inspector {
}

bool preorder(const IR::P4Action *action) override {
BUG_CHECK(findContext<IR::P4Program>() == nullptr, "Unexpected action");
BUG_CHECK(!isInContext<IR::P4Program>(), "Unexpected action");
LOG3("FU Visiting action " << action);
unreachable = false;
currentPoint.assign(context, action);
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/simplifyDefUse.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DoSimplifyDefUse : public Transform {
}

const IR::Node *postorder(IR::Function *function) override {
if (findContext<IR::Declaration_Instance>() == nullptr)
if (!isInContext<IR::Declaration_Instance>())
// not an abstract function implementation: these
// are processed as part of the control body
return process(function);
Expand Down
3 changes: 1 addition & 2 deletions frontends/p4/typeChecking/typeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ TypeInferenceBase::PreorderResult TypeInferenceBase::preorderDeclarationInstance
typeError("%1%: initializers only allowed for extern instances", decl->initializer);
return {decl, true};
}
if (!simpleType->template is<IR::Type_Package>() &&
(findContext<IR::IContainer>() == nullptr)) {
if (!simpleType->template is<IR::Type_Package>() && !isInContext<IR::IContainer>()) {
P4::error(ErrorType::ERR_INVALID, "%1%: cannot instantiate at top-level", decl);
return {decl, false};
}
Expand Down
16 changes: 8 additions & 8 deletions frontends/p4/typeChecking/typeCheckExpr.cpp
10000
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const IR::Node *TypeInferenceBase::postorder(const IR::Parameter *param) {
typeError("%1%: parameters with type %2% must be directionless", param, paramType);
return param;
}
if (findContext<IR::P4Action>()) {
if (isInContext<IR::P4Action>()) {
typeError("%1%: actions cannot have parameters with type %2%", param, paramType);
return param;
}
Expand Down Expand Up @@ -1791,7 +1791,7 @@ const IR::Expression *TypeInferenceBase::actionCall(bool inActionList,
// action, with signature _(arg3)
LOG2("Processing action " << dbp(actionCall));

if (findContext<IR::P4Parser>()) {
if (isInContext<IR::P4Parser>()) {
typeError("%1%: Action calls are not allowed within parsers", actionCall);
return actionCall;
}
Expand All @@ -1817,7 +1817,7 @@ const IR::Expression *TypeInferenceBase::actionCall(bool inActionList,
return actionCall;
}

bool inTable = findContext<IR::P4Table>() != nullptr;
bool inTable = isInContext<IR::P4Table>();

TypeConstraints constraints(typeMap->getSubstitutions(), typeMap);
auto params = new IR::ParameterList;
Expand Down Expand Up @@ -1955,7 +1955,7 @@ const IR::Node *TypeInferenceBase::postorder(const IR::MethodCallExpression *exp
// Handle differently methods and actions: action invocations return actions
// with different signatures
if (methodType->is<IR::Type_Action>()) {
if (findContext<IR::Function>()) {
if (isInContext<IR::Function>()) {
typeError("%1%: Functions cannot call actions", expression);
return expression;
}
Expand Down Expand Up @@ -2147,7 +2147,7 @@ const IR::Node *TypeInferenceBase::postorder(const IR::MethodCallExpression *exp
setType(result, returnType);

auto mi = MethodInstance::resolve(result, this, typeMap, getChildContext(), true);
if (mi->isApply() && findContext<IR::P4Action>()) {
if (mi->isApply() && isInContext<IR::P4Action>()) {
typeError("%1%: apply cannot be called from actions", expression);
return expression;
}
Expand All @@ -2170,8 +2170,8 @@ const IR::Node *TypeInferenceBase::postorder(const IR::MethodCallExpression *exp
}

auto bi = mi->to<BuiltInMethod>();
if ((findContext<IR::SelectCase>()) && (!bi || (bi->name == IR::Type_Stack::pop_front ||
bi->name == IR::Type_Stack::push_front))) {
if (isInContext<IR::SelectCase>() && (!bi || (bi->name == IR::Type_Stack::pop_front ||
bi->name == IR::Type_Stack::push_front))) {
typeError("%1%: no function calls allowed in this context", expression);
return expression;
}
Expand Down Expand Up @@ -2280,7 +2280,7 @@ const IR::SelectCase *TypeInferenceBase::matchCase(const IR::SelectExpression *s
const IR::Node *TypeInferenceBase::postorder(const IR::This *expression) {
if (done()) return expression;
auto decl = findContext<IR::Declaration_Instance>();
if (findContext<IR::Function>() == nullptr || decl == nullptr)
if (!isInContext<IR::Function>() || decl == nullptr)
typeError("%1%: can only be used in the definition of an abstract method", expression);
auto type = getType(decl);
setType(expression, type);
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/uniqueNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class FindSymbols : public Inspector {

public:
bool isTopLevel() const {
return findContext<IR::P4Parser>() == nullptr && findContext<IR::P4Control>() == nullptr;
return !isInContext<IR::P4Parser>() && !isInContext<IR::P4Control>();
}
explicit FindSymbols(RenameMap *renameMap) : renameMap(renameMap) {
CHECK_NULL(renameMap);
Expand Down
8 changes: 4 additions & 4 deletions frontends/p4/unusedDeclarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ const IR::Node *RemoveUnusedDeclarations::process(const IR::IDeclaration *decl)

const IR::Node *RemoveUnusedDeclarations::preorder(IR::Parameter *param) {
// Skip all things that just declare "prototypes"
if (findContext<IR::Type_Parser>() && !findContext<IR::P4Parser>()) return param;
if (findContext<IR::Type_Control>() && !findContext<IR::P4Control>()) return param;
if (findContext<IR::Type_Package>()) return param;
if (findContext<IR::Type_Method>() && !findContext<IR::Function>()) return param;
if (isInContext<IR::Type_Parser>() && !isInContext<IR::P4Parser>()) return param;
if (isInContext<IR::Type_Control>() && !isInContext<IR::P4Control>()) return param;
if (isInContext<IR::Type_Package>()) return param;
if (isInContext<IR::Type_Method>() && !isInContext<IR::Function>()) return param;
return warnIfUnused(param);
}

Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/validateMatchAnnotations.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ValidateMatchAnnotations final : public Inspector {
}
void postorder(const IR::Annotation *annotation) override {
if (annotation->name != IR::Annotation::matchAnnotation) return;
if (!findContext<IR::StructField>()) return;
if (!isInContext<IR::StructField>()) return;
// FIXME: Check annotation kind
const auto &expr = annotation->getExpr();
if (expr.size() != 1)
Expand Down
Loading
Loading
0