From aa39f9f264c9e81db8dab2643a596775744a0be2 Mon Sep 17 00:00:00 2001 From: Chris Dodd Date: Sun, 25 Feb 2024 14:00:20 +1300 Subject: [PATCH 1/2] Fix hvec_map insert/emplace (#4458) * Fix hvec_map insert/emplace - should not replace the value if the key is already present * add hvec_map::count method --------- Co-authored-by: Chris Dodd --- lib/hvec_map.h | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/hvec_map.h b/lib/hvec_map.h index 9300a38cbf1..19117ccf78b 100644 --- a/lib/hvec_map.h +++ b/lib/hvec_map.h @@ -169,6 +169,11 @@ class hvec_map : hash_vector_base { size_t idx = hash_vector_base::find(&k, &cache); return idx ? const_iterator(*this, idx - 1) : end(); } + size_t count(const KEY &k) const { + hash_vector_base::lookup_cache cache; + size_t idx = hash_vector_base::find(&k, &cache); + return idx > 0; + } // FIXME -- how to do this without duplicating the code for lvalue/rvalue? VAL &operator[](const KEY &k) { @@ -230,14 +235,10 @@ class hvec_map : hash_vector_base { data.emplace_back(std::piecewise_construct_t(), std::forward_as_tuple(std::move(k)), std::forward_as_tuple(std::forward(v)...)); new_key = true; - } else { - if ((new_key = erased[idx])) { - erased[idx] = 0; - const_cast(data[idx].first) = std::move(k); - data[idx].second = VAL(std::forward(v)...); - } else { - data[idx].second = VAL(std::forward(v)...); - } + } else if ((new_key = erased[idx])) { + erased[idx] = 0; + const_cast(data[idx].first) = std::move(k); + data[idx].second = VAL(std::forward(v)...); } return std::make_pair(iterator(*this, idx), new_key); } @@ -248,14 +249,10 @@ class hvec_map : hash_vector_base { idx = data.size(); data.push_back(v); new_key = true; - } else { - if ((new_key = erased[idx])) { - erased[idx] = 0; - const_cast(data[idx].first) = v.first; - data[idx].second = v.second; - } else { - data[idx].second = v.second; - } + } else if ((new_key = erased[idx])) { + erased[idx] = 0; + const_cast(data[idx].first) = v.first; + data[idx].second = v.second; } return std::make_pair(iterator(*this, idx), new_key); } From fb4857973bc08134bb1211c20e6c679592e7652d Mon Sep 17 00:00:00 2001 From: Chris Dodd Date: Fri, 23 Feb 2024 12:06:50 +1300 Subject: [PATCH 2/2] Allow annotations on functions --- frontends/parsers/p4/p4parser.ypp | 6 +++++- ir/ir.def | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/frontends/parsers/p4/p4parser.ypp b/frontends/parsers/p4/p4parser.ypp index de71e0f0a43..0f11c202cd8 100644 --- a/frontends/parsers/p4/p4parser.ypp +++ b/frontends/parsers/p4/p4parser.ypp @@ -1413,7 +1413,11 @@ initializer /**************** Expressions ****************/ functionDeclaration - : functionPrototype blockStatement { + : annotations functionPrototype blockStatement { + auto ann = new IR::Annotations(@1, *$1); + driver.structure->pop(); + $$ = new IR::Function($2->srcInfo, $2->name, ann, $2->type, $3); } + | functionPrototype blockStatement { driver.structure->pop(); $$ = new IR::Function($1->srcInfo, $1->name, $1->type, $2); } ; diff --git a/ir/ir.def b/ir/ir.def index 2d2c1ed0aa9..6bef519624c 100644 --- a/ir/ir.def +++ b/ir/ir.def @@ -500,7 +500,8 @@ class SwitchStatement : Statement { split.run_visit(); } } -class Function : Declaration, IFunctional, ISimpleNamespace, INestedNamespace { +class Function : Declaration, IFunctional, IAnnotated, ISimpleNamespace, INestedNamespace { + optional Annotations annotations = Annotations::empty; Type_Method type; BlockStatement body; ParameterList getParameters() const override { @@ -512,6 +513,7 @@ class Function : Declaration, IFunctional, ISimpleNamespace, INestedNamespace { return type->parameters->getDeclByName(name); } std::vector getNestedNamespaces() const override { return { type->typeParameters }; } + Annotations getAnnotations() const override { return annotations; } } /////////////////////////////////////////////////////////////