-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow concatenation of string literals at compile time (#4856)
* Allow type-checking string cocnatenation Signed-off-by: Vladimir Still <[email protected]> * Constant-fold string concatenation Signed-off-by: Vladimir Still <[email protected]> * Allow @name/@deprecated/@nowarn to contain concats Signed-off-by: Vladimir Still <[email protected]> * Prevent BindTypeVariables to descend into unstructured annotation and fail on missing type in typeMap Signed-off-by: Vladimir Still <[email protected]> * Add a test for constant-folding strings Signed-off-by: Vladimir Still <[email protected]> * Fix formatting Signed-off-by: Vladimir Still <[email protected]> * Add a hint when + on strings is used, add negative ests Signed-off-by: Vladimir Still <[email protected]> * Fix formatting Signed-off-by: Vladimir Still <[email protected]> * Fix a typo Signed-off-by: Vladimir Still <[email protected]> * Update frontends/p4/typeChecking/bindVariables.h Co-authored-by: Anton Korobeynikov <[email protected]> Signed-off-by: Vladimír Štill <[email protected]> Signed-off-by: Vladimir Still <[email protected]> * Add explicit validation of string annotations Signed-off-by: Vladimir Still <[email protected]> * Let invalid concats be resolved by type checker, not contant folder Signed-off-by: Vladimir Still <[email protected]> * Fix compilation Signed-off-by: Vladimir Still <[email protected]> * Avoid complaining about unsized integer in the string ++ int cas Signed-off-by: Vladimir Still <[email protected]> * Update test reference outputs Signed-off-by: Vladimir Still <[email protected]> --------- Signed-off-by: Vladimir Still <[email protected]> Signed-off-by: Vladimír Štill <[email protected]> Co-authored-by: Anton Korobeynikov <[email protected]>
- Loading branch information
Showing
25 changed files
with
402 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
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_VALIDATESTRINGANNOTATIONS_H_ | ||
#define FRONTENDS_P4_VALIDATESTRINGANNOTATIONS_H_ | ||
|
||
#include "frontends/p4/typeMap.h" | ||
#include "ir/ir.h" | ||
#include "lib/error.h" | ||
|
||
/// @file | ||
/// @brief Check validity of built-in string annotations. | ||
|
||
namespace P4 { | ||
|
||
/// Checks that the build-in string annotations (\@name, \@deprecated, \@noWarn) have string | ||
/// arguments. | ||
class ValidateStringAnnotations final : public Inspector { | ||
TypeMap *typeMap; | ||
|
||
public: | ||
explicit ValidateStringAnnotations() {} | ||
|
||
void postorder(const IR::Annotation *annotation) override { | ||
const auto name = annotation->name; | ||
if (name != IR::Annotation::nameAnnotation && | ||
name != IR::Annotation::deprecatedAnnotation && | ||
name != IR::Annotation::noWarnAnnotation) { | ||
return; | ||
} | ||
if (annotation->expr.size() != 1) | ||
error(ErrorType::ERR_INVALID, "%1%: annotation must have exactly 1 argument", | ||
annotation); | ||
auto e0 = annotation->expr.at(0); | ||
if (!e0->is<IR::StringLiteral>()) | ||
error(ErrorType::ERR_TYPE_ERROR, "%1%: @%2% annotation's value must be a string", e0, | ||
annotation->name.originalName); | ||
} | ||
}; | ||
|
||
} // namespace P4 | ||
|
||
#endif /* FRONTENDS_P4_VALIDATESTRINGANNOTATIONS_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
extern void log(string msg); | ||
|
||
void fn() { | ||
log("a" + "b"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
extern void log(string msg); | ||
|
||
void fn() { | ||
log("a" ++ 4); | ||
log(1w2 ++ "a"); | ||
} |
4 changes: 4 additions & 0 deletions
4
testdata/p4_16_errors/spec-issue1297-string-cat-err-2-anno.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// should not compile -- not a string | ||
@name(4) | ||
void fn() { | ||
} |
4 changes: 4 additions & 0 deletions
4
testdata/p4_16_errors/spec-issue1297-string-cat-err-3-anno.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// should not compile -- not valid string op | ||
@deprecated("a" | "b") | ||
void fn() { | ||
} |
4 changes: 4 additions & 0 deletions
4
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-0.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
extern void log(string msg); | ||
void fn() { | ||
log("a" + "b"); | ||
} |
3 changes: 3 additions & 0 deletions
3
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-0.p4-stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
spec-issue1297-string-cat-err-0.p4(4): [--Werror=type-error] error: +: cannot be applied to expression '"b"' with type 'string', did you mean to use '++'? | ||
log("a" + "b"); | ||
^ |
5 changes: 5 additions & 0 deletions
5
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-1.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
extern void log(string msg); | ||
void fn() { | ||
log("a" ++ 4); | ||
log(1w0 ++ "a"); | ||
} |
9 changes: 9 additions & 0 deletions
9
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-1.p4-stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
spec-issue1297-string-cat-err-1.p4(5): [--Wwarn=mismatch] warning: 1w2: value does not fit in 1 bits | ||
log(1w2 | ||
^^^ | ||
spec-issue1297-string-cat-err-1.p4(4): [--Werror=type-error] error: "a" ++ 4: Concatenation not defined on string and int | ||
log("a" ++ 4); | ||
^^^^^^ | ||
spec-issue1297-string-cat-err-1.p4(5): [--Werror=type-error] error: 0 ++ "a": Concatenation not defined on bit<1> and string | ||
log(1w2 ++ "a"); | ||
^^^^^^^^^^ |
2 changes: 2 additions & 0 deletions
2
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-2-anno.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@name(4) void fn() { | ||
} |
3 changes: 3 additions & 0 deletions
3
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-2-anno.p4-stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
spec-issue1297-string-cat-err-2-anno.p4(2): [--Werror=type-error] error: 4: @name annotation's value must be a string | ||
@name(4) | ||
^ |
2 changes: 2 additions & 0 deletions
2
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-3-anno.p4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@deprecated("a" | "b") void fn() { | ||
} |
3 changes: 3 additions & 0 deletions
3
testdata/p4_16_errors_outputs/spec-issue1297-string-cat-err-3-anno.p4-stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
spec-issue1297-string-cat-err-3-anno.p4(2): [--Werror=type-error] error: "a" | "b": @deprecated annotation's value must be a string | ||
@deprecated("a" | "b") | ||
^^^^^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <core.p4> | ||
|
||
#define DEPR_NAME depr | ||
#define _STR(x) #x | ||
#define STR(x) _STR(x) | ||
#define AAA "_x_" | ||
#define BBB "_y_" | ||
|
||
// test constant-folding concat in @deprecated | ||
@deprecated("function " ++ STR(DEPR_NAME) ++ " is deprecated") | ||
void DEPR_NAME (int<4> x) { } | ||
|
||
extern void log(string msg); | ||
|
||
control c() { | ||
|
||
// constant-folded (unstructured, but with a special meaning defined in P4C) | ||
@name("foo" ++ BBB ++ "bar" ++ AAA) | ||
action foo() { } | ||
|
||
// constant-folded | ||
@DummyVendor_Structured[a="X_" ++ "Y", b="Y" ++ "_" ++ "z"] | ||
action bar() { } | ||
|
||
// constant-folded | ||
@DummyVendor_Structured2["X_" ++ "Y", "Y" ++ "_" ++ "z"] | ||
action baz() { } | ||
|
||
// NOT constant-folded (unstructured with no special meaning) | ||
@DummyVendor_Unstructured(a="X_" ++ "Y", b="Y" ++ "_" ++ "z") | ||
action tmp() { } | ||
|
||
@name("table" ++ "_" ++ "a") | ||
table a { | ||
actions = { foo; bar; baz; tmp; } | ||
} | ||
|
||
apply { | ||
log("simple msg"); | ||
// constant-folded | ||
log("concat" ++ " " ++ "msg " | ||
++ "multiple lines"); | ||
a.apply(); | ||
depr(4); | ||
} | ||
} | ||
|
||
control proto(); | ||
package top(proto p); | ||
|
||
top(c()) main; |
Oops, something went wrong.