Skip to content

Commit

Permalink
Rename Module.annotations() to directives(); AddAnnotation to `…
Browse files Browse the repository at this point in the history
…AddDirective`.

PiperOrigin-RevId: 715488169
  • Loading branch information
dplassgit authored and copybara-github committed Jan 14, 2025
1 parent 8a91320 commit d863099
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 47 deletions.
8 changes: 4 additions & 4 deletions xls/codegen/vast/dslx_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -719,13 +719,13 @@ absl::StatusOr<std::string> DslxBuilder::FormatModule() {
if (std::holds_alternative<dslx::ConstantDef*>(member)) {
// Allow non-standard constant names, because we do not canonicalize the
// original SV names to SCREAMING_SNAKE_CASE in the conversion process.
module_.AddAnnotation(
dslx::ModuleAnnotation::kAllowNonstandardConstantNaming);
module_.AddDirective(
dslx::ModuleDirective::kAllowNonstandardConstantNaming);

// Similar for members, they are not in SV going to be consistency named
// in `snake_case` convention.
module_.AddAnnotation(
dslx::ModuleAnnotation::kAllowNonstandardMemberNaming);
module_.AddDirective(
dslx::ModuleDirective::kAllowNonstandardMemberNaming);
break;
}
}
Expand Down
12 changes: 6 additions & 6 deletions xls/dslx/fmt/ast_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2893,24 +2893,24 @@ static int NumHardLinesAfter(const AstNode* node, const ModuleMember& member,
absl::StatusOr<DocRef> Formatter::Format(const Module& n) {
std::vector<DocRef> pieces;

if (!n.annotations().empty()) {
for (ModuleAnnotation annotation : n.annotations()) {
if (!n.directives().empty()) {
for (ModuleDirective annotation : n.directives()) {
switch (annotation) {
case ModuleAnnotation::kAllowNonstandardConstantNaming:
case ModuleDirective::kAllowNonstandardConstantNaming:
pieces.push_back(
arena_.MakeText("#![allow(nonstandard_constant_naming)]"));
pieces.push_back(arena_.hard_line());
break;
case ModuleAnnotation::kAllowNonstandardMemberNaming:
case ModuleDirective::kAllowNonstandardMemberNaming:
pieces.push_back(
arena_.MakeText("#![allow(nonstandard_member_naming)]"));
pieces.push_back(arena_.hard_line());
break;
case ModuleAnnotation::kTypeInferenceVersion2:
case ModuleDirective::kTypeInferenceVersion2:
pieces.push_back(arena_.MakeText("#![feature(type_inference_v2)]"));
pieces.push_back(arena_.hard_line());
break;
case ModuleAnnotation::kAllowUseSyntax:
case ModuleDirective::kAllowUseSyntax:
pieces.push_back(arena_.MakeText("#![feature(use_syntax)]"));
pieces.push_back(arena_.hard_line());
break;
Expand Down
4 changes: 2 additions & 2 deletions xls/dslx/frontend/ast_cloner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1148,8 +1148,8 @@ absl::StatusOr<std::unique_ptr<Module>> CloneModule(const Module& module,
CloneReplacer replacer) {
auto new_module = std::make_unique<Module>(module.name(), module.fs_path(),
*module.file_table());
for (const ModuleAnnotation& ann : module.annotations()) {
new_module->AddAnnotation(ann);
for (const ModuleDirective& dir : module.directives()) {
new_module->AddDirective(dir);
}
AstCloner cloner(new_module.get(), std::move(replacer));
XLS_RETURN_IF_ERROR(module.Accept(&cloner));
Expand Down
18 changes: 9 additions & 9 deletions xls/dslx/frontend/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,22 @@ std::string Module::ToString() const {
absl::StrAppend(out, ToAstNode(member)->ToString());
});

if (!annotations().empty()) {
// Make an annotation block above the module contents if there are
// annotations.
if (!directives().empty()) {
// Make an directive block above the module contents if there are
// directives.
std::string header = absl::StrJoin(
annotations(), "\n", [](std::string* out, ModuleAnnotation annotation) {
switch (annotation) {
case ModuleAnnotation::kAllowNonstandardConstantNaming:
directives(), "\n", [](std::string* out, ModuleDirective directive) {
switch (directive) {
case ModuleDirective::kAllowNonstandardConstantNaming:
absl::StrAppend(out, "#![allow(nonstandard_constant_naming)]");
break;
case ModuleAnnotation::kAllowNonstandardMemberNaming:
case ModuleDirective::kAllowNonstandardMemberNaming:
absl::StrAppend(out, "#![allow(nonstandard_member_naming)]");
break;
case ModuleAnnotation::kTypeInferenceVersion2:
case ModuleDirective::kTypeInferenceVersion2:
absl::StrAppend(out, "#![feature(type_inference_v2)]");
break;
case ModuleAnnotation::kAllowUseSyntax:
case ModuleDirective::kAllowUseSyntax:
absl::StrAppend(out, "#![feature(use_syntax)]");
break;
}
Expand Down
16 changes: 8 additions & 8 deletions xls/dslx/frontend/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Pos GetPos(const ModuleMember& module_member);

std::string_view GetModuleMemberTypeName(const ModuleMember& module_member);

enum class ModuleAnnotation : uint8_t {
enum class ModuleDirective : uint8_t {
// Suppresses the "constant naming" warning.
kAllowNonstandardConstantNaming,

Expand Down Expand Up @@ -262,14 +262,14 @@ class Module : public AstNode {
// Finds all the AST nodes that fall within the given span.
std::vector<const AstNode*> FindContained(const Span& target) const;

// Tags this module as having the given module-level annotation "annotation".
void AddAnnotation(ModuleAnnotation annotation) {
annotations_.insert(annotation);
// Tags this module as having the given module-level directive "directive".
void AddDirective(ModuleDirective directive) {
directives_.insert(directive);
}

// Returns all the module-level annotation tags.
const absl::btree_set<ModuleAnnotation>& annotations() const {
return annotations_;
// Returns all the module-level directive tags.
const absl::btree_set<ModuleDirective>& directives() const {
return directives_;
}

FileTable* file_table() const { return file_table_; }
Expand Down Expand Up @@ -330,7 +330,7 @@ class Module : public AstNode {
// having many definition nodes of the same builtin thing floating around.
absl::flat_hash_map<std::string, BuiltinNameDef*> builtin_name_defs_;

absl::btree_set<ModuleAnnotation> annotations_;
absl::btree_set<ModuleDirective> directives_;

// The span of the module is only known once parsing has completed.
//
Expand Down
14 changes: 6 additions & 8 deletions xls/dslx/frontend/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ absl::Status Parser::ParseModuleDirective() {
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kCParen));
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kCBrack));
if (feature == "use_syntax") {
module_->AddAnnotation(ModuleAnnotation::kAllowUseSyntax);
module_->AddDirective(ModuleDirective::kAllowUseSyntax);
} else if (feature == "type_inference_v2") {
module_->AddAnnotation(ModuleAnnotation::kTypeInferenceVersion2);
module_->AddDirective(ModuleDirective::kTypeInferenceVersion2);
} else {
return ParseErrorStatus(
directive_span,
Expand All @@ -266,10 +266,10 @@ absl::Status Parser::ParseModuleDirective() {
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kOParen));
XLS_ASSIGN_OR_RETURN(std::string to_allow, PopIdentifierOrError());
if (to_allow == "nonstandard_constant_naming") {
module_->AddAnnotation(ModuleAnnotation::kAllowNonstandardConstantNaming);
module_->AddDirective(ModuleDirective::kAllowNonstandardConstantNaming);
}
if (to_allow == "nonstandard_member_naming") {
module_->AddAnnotation(ModuleAnnotation::kAllowNonstandardMemberNaming);
module_->AddDirective(ModuleDirective::kAllowNonstandardMemberNaming);
}
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kCParen));
XLS_RETURN_IF_ERROR(DropTokenOrError(TokenKind::kCBrack));
Expand Down Expand Up @@ -449,8 +449,7 @@ absl::StatusOr<std::unique_ptr<Module>> Parser::ParseModule(
break;
}
case Keyword::kImport: {
if (module_->annotations().contains(
ModuleAnnotation::kAllowUseSyntax)) {
if (module_->directives().contains(ModuleDirective::kAllowUseSyntax)) {
return ParseErrorStatus(
peek->span(),
"`import` syntax is disabled for this module via "
Expand All @@ -461,8 +460,7 @@ absl::StatusOr<std::unique_ptr<Module>> Parser::ParseModule(
break;
}
case Keyword::kUse: {
if (!module_->annotations().contains(
ModuleAnnotation::kAllowUseSyntax)) {
if (!module_->directives().contains(ModuleDirective::kAllowUseSyntax)) {
return ParseErrorStatus(
peek->span(),
"`use` syntax is not enabled for this module; enable with "
Expand Down
10 changes: 5 additions & 5 deletions xls/dslx/frontend/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2958,23 +2958,23 @@ TEST_F(ParserTest, ParseAllowNonstandardConstantNamingAnnotation) {
XLS_ASSERT_OK_AND_ASSIGN(std::unique_ptr<Module> module,
parser.ParseModule());
EXPECT_THAT(
module->annotations(),
testing::ElementsAre(ModuleAnnotation::kAllowNonstandardConstantNaming));
module->directives(),
testing::ElementsAre(ModuleDirective::kAllowNonstandardConstantNaming));
}

TEST_F(ParserTest, NoAttributeForTypeInferenceVersion) {
XLS_ASSERT_OK_AND_ASSIGN(std::unique_ptr<Module> module, Parse(R"(
fn f() { () }
)"));
EXPECT_THAT(module->annotations(), testing::IsEmpty());
EXPECT_THAT(module->directives(), testing::IsEmpty());
}

TEST_F(ParserTest, ParseTypeInferenceVersionAttributeWithValue2) {
XLS_ASSERT_OK_AND_ASSIGN(std::unique_ptr<Module> module, Parse(R"(
#![feature(type_inference_v2)]
)"));
EXPECT_THAT(module->annotations(),
testing::ElementsAre(ModuleAnnotation::kTypeInferenceVersion2));
EXPECT_THAT(module->directives(),
testing::ElementsAre(ModuleDirective::kTypeInferenceVersion2));
}

// Verifies that we can walk backwards through a tree. In this case, from the
Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/parse_and_typecheck.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ absl::StatusOr<TypecheckedModule> TypecheckModule(
WarningCollector warnings(import_data->enabled_warnings());
XLS_ASSIGN_OR_RETURN(
TypeInfo * type_info,
module->annotations().contains(ModuleAnnotation::kTypeInferenceVersion2)
module->directives().contains(ModuleDirective::kTypeInferenceVersion2)
? TypecheckModuleV2(module.get(), import_data, &warnings)
: TypecheckModule(module.get(), import_data, &warnings));
TypecheckedModule result{module.get(), type_info, std::move(warnings)};
Expand Down
4 changes: 2 additions & 2 deletions xls/dslx/type_system/deduce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ static void WarnOnInappropriateConstantName(std::string_view identifier,
const Module& module,
DeduceCtx* ctx) {
if (!IsScreamingSnakeCase(identifier) &&
!module.annotations().contains(
ModuleAnnotation::kAllowNonstandardConstantNaming)) {
!module.directives().contains(
ModuleDirective::kAllowNonstandardConstantNaming)) {
ctx->warnings()->Add(
span, WarningKind::kConstantNaming,
absl::StrFormat("Standard style is SCREAMING_SNAKE_CASE for constant "
Expand Down
4 changes: 2 additions & 2 deletions xls/dslx/type_system/deduce_struct_def_base_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ void WarnOnInappropriateMemberName(std::string_view member_name,
const Span& span, const Module& module,
DeduceCtx* ctx) {
if (!IsAcceptablySnakeCase(member_name) &&
!module.annotations().contains(
ModuleAnnotation::kAllowNonstandardMemberNaming)) {
!module.directives().contains(
ModuleDirective::kAllowNonstandardMemberNaming)) {
ctx->warnings()->Add(
span, WarningKind::kMemberNaming,
absl::StrFormat("Standard style is snake_case for struct member names; "
Expand Down

0 comments on commit d863099

Please sign in to comment.