Skip to content

Commit

Permalink
[NFC] Split TypeInference (#4814)
Browse files Browse the repository at this point in the history
* Factor our type type checking into a separate file. No functionality change

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Factor out expression type checking into a separate file. No functionality change

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Factor out type checking for statements. No functionality change.

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Factor out declarations type checking. Some further cleanup here and there. No functionality change

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Get rid of std::function

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Move to proper place

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Switch to better containers

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Simplify includes

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

* Do not forget to lint headers

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>

---------

Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
  • Loading branch information
asl authored Jul 17, 2024
1 parent fcaa90d commit ad161b2
Show file tree
Hide file tree
Showing 9 changed files with 3,566 additions and 3,464 deletions.
5 changes: 5 additions & 0 deletions frontends/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ set (P4_FRONTEND_SRCS
p4/typeChecking/bindVariables.cpp
p4/typeChecking/syntacticEquivalence.cpp
p4/typeChecking/typeChecker.cpp
p4/typeChecking/typeCheckDecl.cpp
p4/typeChecking/typeCheckExpr.cpp
p4/typeChecking/typeCheckStmt.cpp
p4/typeChecking/typeCheckTypes.cpp
p4/typeChecking/typeConstraints.cpp
p4/typeChecking/typeSubstitution.cpp
p4/typeChecking/typeSubstitutionVisitor.cpp
Expand Down Expand Up @@ -144,6 +148,7 @@ set (P4_FRONTEND_HDRS
p4/ternaryBool.h
p4/toP4/toP4.h
p4/typeChecking/bindVariables.h
p4/typeChecking/constantTypeSubstitution.h
p4/typeChecking/syntacticEquivalence.h
p4/typeChecking/typeChecker.h
p4/typeChecking/typeConstraints.h
Expand Down
83 changes: 83 additions & 0 deletions frontends/p4/typeChecking/constantTypeSubstitution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef FRONTENDS_P4_TYPECHECKING_CONSTANTTYPESUBSTITUTION_H_
#define FRONTENDS_P4_TYPECHECKING_CONSTANTTYPESUBSTITUTION_H_

#include "frontends/common/resolveReferences/resolveReferences.h"
#include "ir/visitor.h"
#include "typeChecker.h"
#include "typeSubstitution.h"

namespace P4 {

// Used to set the type of Constants after type inference
class ConstantTypeSubstitution : public Transform, ResolutionContext {
TypeVariableSubstitution *subst;
TypeMap *typeMap;
TypeInference *tc;

public:
ConstantTypeSubstitution(TypeVariableSubstitution *subst, TypeMap *typeMap, TypeInference *tc)
: subst(subst), typeMap(typeMap), tc(tc) {
CHECK_NULL(subst);
CHECK_NULL(typeMap);
CHECK_NULL(tc);
LOG3("ConstantTypeSubstitution " << subst);
}

const IR::Node *postorder(IR::Constant *cst) override {
auto cstType = typeMap->getType(getOriginal(), true);
if (!cstType->is<IR::ITypeVar>()) return cst;
auto repl = cstType;
while (repl->is<IR::ITypeVar>()) {
auto next = subst->get(repl->to<IR::ITypeVar>());
BUG_CHECK(next != repl, "Cycle in substitutions: %1%", next);
if (!next) break;
repl = next;
}
if (repl != cstType) {
// We may replace a type variable with another one
LOG2("Inferred type " << repl << " for " << cst);
cst = new IR::Constant(cst->srcInfo, repl, cst->value, cst->base);
} else {
LOG2("No type inferred for " << cst << " repl is " << repl);
}
return cst;
}

const IR::Expression *convert(const IR::Expression *expr, const Visitor::Context *ctxt) {
auto result = expr->apply(*this, ctxt)->to<IR::Expression>();
if (result != expr && (::errorCount() == 0)) tc->learn(result, this, ctxt);
return result;
}
const IR::Vector<IR::Expression> *convert(const IR::Vector<IR::Expression> *vec,
const Visitor::Context *ctxt) {
auto result = vec->apply(*this, ctxt)->to<IR::Vector<IR::Expression>>();
if (result != vec) tc->learn(result, this, ctxt);
return result;
}
const IR::Vector<IR::Argument> *convert(const IR::Vector<IR::Argument> *vec,
const Visitor::Context *ctxt) {
auto result = vec->apply(*this, ctxt)->to<IR::Vector<IR::Argument>>();
if (result != vec) tc->learn(result, this, ctxt);
return result;
}
};

} // namespace P4

#endif // FRONTENDS_P4_TYPECHECKING_CONSTANTTYPESUBSTITUTION_H_
Loading

0 comments on commit ad161b2

Please sign in to comment.