Skip to content
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
3 changes: 3 additions & 0 deletions common/ast/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ cc_library(
deps = [
"//common:constant",
"//common:expr",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:no_destructor",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:overload",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:variant",
],
Expand Down
117 changes: 117 additions & 0 deletions common/ast/metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@

#include "common/ast/metadata.h"

#include <cstddef>
#include <memory>
#include <string>
#include <utility>
#include <variant>
#include <vector>

#include "absl/base/no_destructor.h"
#include "absl/base/nullability.h"
#include "absl/functional/overload.h"
#include "absl/log/absl_check.h"
#include "absl/strings/str_cat.h"
#include "absl/types/variant.h"

namespace cel {
Expand All @@ -30,6 +37,96 @@ const TypeSpec& DefaultTypeSpec() {
return *type;
}

std::string FormatPrimitive(PrimitiveType t) {
switch (t) {
case PrimitiveType::kBool:
return "bool";
case PrimitiveType::kInt64:
return "int";
case PrimitiveType::kUint64:
return "uint";
case PrimitiveType::kDouble:
return "double";
case PrimitiveType::kString:
return "string";
case PrimitiveType::kBytes:
return "bytes";
default:
return "*unspecified primitive*";
}
}

std::string FormatWellKnown(WellKnownTypeSpec t) {
switch (t) {
case WellKnownTypeSpec::kAny:
return "google.protobuf.Any";
case WellKnownTypeSpec::kDuration:
return "google.protobuf.Duration";
case WellKnownTypeSpec::kTimestamp:
return "google.protobuf.Timestamp";
default:
return "*unspecified well known*";
}
}

using FormatIns = std::variant<const TypeSpec* absl_nonnull, std::string>;
using FormatStack = std::vector<FormatIns>;

void HandleFormatTypeSpec(const TypeSpec& t, FormatStack& stack,
std::string* out) {
if (t.has_dyn()) {
absl::StrAppend(out, "dyn");
} else if (t.has_null()) {
absl::StrAppend(out, "null");
} else if (t.has_primitive()) {
absl::StrAppend(out, FormatPrimitive(t.primitive()));
} else if (t.has_wrapper()) {
absl::StrAppend(out, "wrapper(", FormatPrimitive(t.wrapper()), ")");
} else if (t.has_well_known()) {
absl::StrAppend(out, FormatWellKnown(t.well_known()));
return;
} else if (t.has_abstract_type()) {
const auto& abs_type = t.abstract_type();
if (abs_type.parameter_types().empty()) {
absl::StrAppend(out, abs_type.name());
return;
}
absl::StrAppend(out, abs_type.name(), "(");
stack.push_back(")");
for (size_t i = abs_type.parameter_types().size(); i > 0; --i) {
stack.push_back(&abs_type.parameter_types()[i - 1]);
if (i > 1) {
stack.push_back(", ");
}
}

} else if (t.has_type()) {
if (t.type() == TypeSpec()) {
absl::StrAppend(out, "type");
return;
}
absl::StrAppend(out, "type(");
stack.push_back(")");
stack.push_back(&t.type());
} else if (t.has_message_type()) {
absl::StrAppend(out, t.message_type().type());
} else if (t.has_type_param()) {
absl::StrAppend(out, t.type_param().type());
} else if (t.has_list_type()) {
absl::StrAppend(out, "list(");
stack.push_back(")");
stack.push_back(&t.list_type().elem_type());
} else if (t.has_map_type()) {
absl::StrAppend(out, "map(");
stack.push_back(")");
stack.push_back(&t.map_type().value_type());
stack.push_back(", ");
stack.push_back(&t.map_type().key_type());
} else {
absl::StrAppend(out, "*error*");
}
}

TypeSpecKind CopyImpl(const TypeSpecKind& other) {
return absl::visit(
absl::Overload(
Expand Down Expand Up @@ -142,4 +239,24 @@ FunctionTypeSpec& FunctionTypeSpec::operator=(const FunctionTypeSpec& other) {
return *this;
}

std::string FormatTypeSpec(const TypeSpec& t) {
// Use a stack to avoid recursion.
// Probably overly defensive, but fuzzers will often notice the recursion
// and try to trigger it.
std::string out;
FormatStack seq;
seq.push_back(&t);
while (!seq.empty()) {
FormatIns ins = std::move(seq.back());
seq.pop_back();
if (std::holds_alternative<std::string>(ins)) {
absl::StrAppend(&out, std::get<std::string>(ins));
continue;
}
ABSL_DCHECK(std::holds_alternative<const TypeSpec*>(ins));
HandleFormatTypeSpec(*std::get<const TypeSpec*>(ins), seq, &out);
}
return out;
}

} // namespace cel
3 changes: 3 additions & 0 deletions common/ast/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,9 @@ class TypeSpec {
TypeSpecKind type_kind_;
};

// Returns a string representation of the given TypeSpec.
std::string FormatTypeSpec(const TypeSpec& t);

// Describes a resolved reference to a declaration.
class Reference {
public:
Expand Down
71 changes: 1 addition & 70 deletions testutil/baseline_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,75 +28,6 @@
namespace cel::test {
namespace {

std::string FormatPrimitive(PrimitiveType t) {
switch (t) {
case PrimitiveType::kBool:
return "bool";
case PrimitiveType::kInt64:
return "int";
case PrimitiveType::kUint64:
return "uint";
case PrimitiveType::kDouble:
return "double";
case PrimitiveType::kString:
return "string";
case PrimitiveType::kBytes:
return "bytes";
default:
return "<unspecified primitive>";
}
}

std::string FormatType(const TypeSpec& t) {
if (t.has_dyn()) {
return "dyn";
} else if (t.has_null()) {
return "null";
} else if (t.has_primitive()) {
return FormatPrimitive(t.primitive());
} else if (t.has_wrapper()) {
return absl::StrCat("wrapper(", FormatPrimitive(t.wrapper()), ")");
} else if (t.has_well_known()) {
switch (t.well_known()) {
case WellKnownTypeSpec::kAny:
return "google.protobuf.Any";
case WellKnownTypeSpec::kDuration:
return "google.protobuf.Duration";
case WellKnownTypeSpec::kTimestamp:
return "google.protobuf.Timestamp";
default:
return "<unspecified wellknown>";
}
} else if (t.has_abstract_type()) {
const auto& abs_type = t.abstract_type();
std::string s = abs_type.name();
if (!abs_type.parameter_types().empty()) {
absl::StrAppend(&s, "(",
absl::StrJoin(abs_type.parameter_types(), ",",
[](std::string* out, const auto& t) {
absl::StrAppend(out, FormatType(t));
}),
")");
}
return s;
} else if (t.has_type()) {
if (t.type() == TypeSpec()) {
return "type";
}
return absl::StrCat("type(", FormatType(t.type()), ")");
} else if (t.has_message_type()) {
return t.message_type().type();
} else if (t.has_type_param()) {
return t.type_param().type();
} else if (t.has_list_type()) {
return absl::StrCat("list(", FormatType(t.list_type().elem_type()), ")");
} else if (t.has_map_type()) {
return absl::StrCat("map(", FormatType(t.map_type().key_type()), ", ",
FormatType(t.map_type().value_type()), ")");
}
return "<error>";
}

std::string FormatReference(const cel::Reference& r) {
if (r.overload_id().empty()) {
return r.name();
Expand All @@ -113,7 +44,7 @@ class TypeAdorner : public ExpressionAdorner {

auto t = ast_.type_map().find(e.id());
if (t != ast_.type_map().end()) {
absl::StrAppend(&s, "~", FormatType(t->second));
absl::StrAppend(&s, "~", FormatTypeSpec(t->second));
}
if (const auto r = ast_.reference_map().find(e.id());
r != ast_.reference_map().end()) {
Expand Down
2 changes: 1 addition & 1 deletion testutil/baseline_tests_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ INSTANTIATE_TEST_SUITE_P(
"x~google.protobuf.Timestamp"},
TestCase{TypeSpec(DynTypeSpec()), "x~dyn"},
TestCase{TypeSpec(NullTypeSpec()), "x~null"},
TestCase{TypeSpec(UnsetTypeSpec()), "x~<error>"},
TestCase{TypeSpec(UnsetTypeSpec()), "x~*error*"},
TestCase{TypeSpec(MessageTypeSpec("com.example.Type")),
"x~com.example.Type"},
TestCase{TypeSpec(AbstractType("optional_type",
Expand Down