summaryrefslogtreecommitdiff
path: root/llvm/utils/TableGen/DirectiveEmitter.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2021-02-16 20:13:02 +0000
committerDimitry Andric <dim@FreeBSD.org>2021-02-16 20:13:02 +0000
commitb60736ec1405bb0a8dd40989f67ef4c93da068ab (patch)
tree5c43fbb7c9fc45f0f87e0e6795a86267dbd12f9d /llvm/utils/TableGen/DirectiveEmitter.cpp
parentcfca06d7963fa0909f90483b42a6d7d194d01e08 (diff)
Diffstat (limited to 'llvm/utils/TableGen/DirectiveEmitter.cpp')
-rw-r--r--llvm/utils/TableGen/DirectiveEmitter.cpp754
1 files changed, 503 insertions, 251 deletions
diff --git a/llvm/utils/TableGen/DirectiveEmitter.cpp b/llvm/utils/TableGen/DirectiveEmitter.cpp
index 2061ff1fdd1a..c9daa9b24155 100644
--- a/llvm/utils/TableGen/DirectiveEmitter.cpp
+++ b/llvm/utils/TableGen/DirectiveEmitter.cpp
@@ -11,9 +11,9 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/TableGen/DirectiveEmitter.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
@@ -40,23 +40,15 @@ private:
namespace llvm {
-// Get Directive or Clause name formatted by replacing whitespaces with
-// underscores.
-std::string getFormattedName(StringRef Name) {
- std::string N = Name.str();
- std::replace(N.begin(), N.end(), ' ', '_');
- return N;
-}
-
// Generate enum class
void GenerateEnumClass(const std::vector<Record *> &Records, raw_ostream &OS,
- StringRef Enum, StringRef Prefix, StringRef CppNamespace,
- bool MakeEnumAvailableInNamespace) {
+ StringRef Enum, StringRef Prefix,
+ const DirectiveLanguage &DirLang) {
OS << "\n";
OS << "enum class " << Enum << " {\n";
for (const auto &R : Records) {
- const auto Name = R->getValueAsString("name");
- OS << " " << Prefix << getFormattedName(Name) << ",\n";
+ BaseRecord Rec{R};
+ OS << " " << Prefix << Rec.getFormattedName() << ",\n";
}
OS << "};\n";
OS << "\n";
@@ -68,44 +60,130 @@ void GenerateEnumClass(const std::vector<Record *> &Records, raw_ostream &OS,
// At the same time we do not loose the strong type guarantees of the enum
// class, that is we cannot pass an unsigned as Directive without an explicit
// cast.
- if (MakeEnumAvailableInNamespace) {
+ if (DirLang.hasMakeEnumAvailableInNamespace()) {
OS << "\n";
for (const auto &R : Records) {
- const auto FormattedName = getFormattedName(R->getValueAsString("name"));
- OS << "constexpr auto " << Prefix << FormattedName << " = "
- << "llvm::" << CppNamespace << "::" << Enum << "::" << Prefix
- << FormattedName << ";\n";
+ BaseRecord Rec{R};
+ OS << "constexpr auto " << Prefix << Rec.getFormattedName() << " = "
+ << "llvm::" << DirLang.getCppNamespace() << "::" << Enum
+ << "::" << Prefix << Rec.getFormattedName() << ";\n";
}
}
}
-// Generate the declaration section for the enumeration in the directive
-// language
-void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
+// Generate enums for values that clauses can take.
+// Also generate function declarations for get<Enum>Name(StringRef Str).
+void GenerateEnumClauseVal(const std::vector<Record *> &Records,
+ raw_ostream &OS, const DirectiveLanguage &DirLang,
+ std::string &EnumHelperFuncs) {
+ for (const auto &R : Records) {
+ Clause C{R};
+ const auto &ClauseVals = C.getClauseVals();
+ if (ClauseVals.size() <= 0)
+ continue;
- const auto &DirectiveLanguages =
- Records.getAllDerivedDefinitions("DirectiveLanguage");
+ const auto &EnumName = C.getEnumName();
+ if (EnumName.size() == 0) {
+ PrintError("enumClauseValue field not set in Clause" +
+ C.getFormattedName() + ".");
+ return;
+ }
- if (DirectiveLanguages.size() != 1) {
- PrintError("A single definition of DirectiveLanguage is needed.");
- return;
+ OS << "\n";
+ OS << "enum class " << EnumName << " {\n";
+ for (const auto &CV : ClauseVals) {
+ ClauseVal CVal{CV};
+ OS << " " << CV->getName() << "=" << CVal.getValue() << ",\n";
+ }
+ OS << "};\n";
+
+ if (DirLang.hasMakeEnumAvailableInNamespace()) {
+ OS << "\n";
+ for (const auto &CV : ClauseVals) {
+ OS << "constexpr auto " << CV->getName() << " = "
+ << "llvm::" << DirLang.getCppNamespace() << "::" << EnumName
+ << "::" << CV->getName() << ";\n";
+ }
+ EnumHelperFuncs += (llvm::Twine(EnumName) + llvm::Twine(" get") +
+ llvm::Twine(EnumName) + llvm::Twine("(StringRef);\n"))
+ .str();
+
+ EnumHelperFuncs +=
+ (llvm::Twine("llvm::StringRef get") + llvm::Twine(DirLang.getName()) +
+ llvm::Twine(EnumName) + llvm::Twine("Name(") +
+ llvm::Twine(EnumName) + llvm::Twine(");\n"))
+ .str();
+ }
+ }
+}
+
+bool HasDuplicateClauses(const std::vector<Record *> &Clauses,
+ const Directive &Directive,
+ llvm::StringSet<> &CrtClauses) {
+ bool HasError = false;
+ for (const auto &C : Clauses) {
+ VersionedClause VerClause{C};
+ const auto insRes = CrtClauses.insert(VerClause.getClause().getName());
+ if (!insRes.second) {
+ PrintError("Clause " + VerClause.getClause().getRecordName() +
+ " already defined on directive " + Directive.getRecordName());
+ HasError = true;
+ }
}
+ return HasError;
+}
+
+// Check for duplicate clauses in lists. Clauses cannot appear twice in the
+// three allowed list. Also, since required implies allowed, clauses cannot
+// appear in both the allowedClauses and requiredClauses lists.
+bool HasDuplicateClausesInDirectives(const std::vector<Record *> &Directives) {
+ bool HasDuplicate = false;
+ for (const auto &D : Directives) {
+ Directive Dir{D};
+ llvm::StringSet<> Clauses;
+ // Check for duplicates in the three allowed lists.
+ if (HasDuplicateClauses(Dir.getAllowedClauses(), Dir, Clauses) ||
+ HasDuplicateClauses(Dir.getAllowedOnceClauses(), Dir, Clauses) ||
+ HasDuplicateClauses(Dir.getAllowedExclusiveClauses(), Dir, Clauses)) {
+ HasDuplicate = true;
+ }
+ // Check for duplicate between allowedClauses and required
+ Clauses.clear();
+ if (HasDuplicateClauses(Dir.getAllowedClauses(), Dir, Clauses) ||
+ HasDuplicateClauses(Dir.getRequiredClauses(), Dir, Clauses)) {
+ HasDuplicate = true;
+ }
+ if (HasDuplicate)
+ PrintFatalError("One or more clauses are defined multiple times on"
+ " directive " +
+ Dir.getRecordName());
+ }
+
+ return HasDuplicate;
+}
+
+// Check consitency of records. Return true if an error has been detected.
+// Return false if the records are valid.
+bool DirectiveLanguage::HasValidityErrors() const {
+ if (getDirectiveLanguages().size() != 1) {
+ PrintFatalError("A single definition of DirectiveLanguage is needed.");
+ return true;
+ }
+
+ return HasDuplicateClausesInDirectives(getDirectives());
+}
- const auto &DirectiveLanguage = DirectiveLanguages[0];
- StringRef LanguageName = DirectiveLanguage->getValueAsString("name");
- StringRef DirectivePrefix =
- DirectiveLanguage->getValueAsString("directivePrefix");
- StringRef ClausePrefix = DirectiveLanguage->getValueAsString("clausePrefix");
- StringRef CppNamespace = DirectiveLanguage->getValueAsString("cppNamespace");
- bool MakeEnumAvailableInNamespace =
- DirectiveLanguage->getValueAsBit("makeEnumAvailableInNamespace");
- bool EnableBitmaskEnumInNamespace =
- DirectiveLanguage->getValueAsBit("enableBitmaskEnumInNamespace");
+// Generate the declaration section for the enumeration in the directive
+// language
+void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
+ const auto DirLang = DirectiveLanguage{Records};
+ if (DirLang.HasValidityErrors())
+ return;
- OS << "#ifndef LLVM_" << LanguageName << "_INC\n";
- OS << "#define LLVM_" << LanguageName << "_INC\n";
+ OS << "#ifndef LLVM_" << DirLang.getName() << "_INC\n";
+ OS << "#define LLVM_" << DirLang.getName() << "_INC\n";
- if (EnableBitmaskEnumInNamespace)
+ if (DirLang.hasEnableBitmaskEnumInNamespace())
OS << "\n#include \"llvm/ADT/BitmaskEnum.h\"\n";
OS << "\n";
@@ -114,41 +192,48 @@ void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
// Open namespaces defined in the directive language
llvm::SmallVector<StringRef, 2> Namespaces;
- llvm::SplitString(CppNamespace, Namespaces, "::");
+ llvm::SplitString(DirLang.getCppNamespace(), Namespaces, "::");
for (auto Ns : Namespaces)
OS << "namespace " << Ns << " {\n";
- if (EnableBitmaskEnumInNamespace)
+ if (DirLang.hasEnableBitmaskEnumInNamespace())
OS << "\nLLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();\n";
// Emit Directive enumeration
- const auto &Directives = Records.getAllDerivedDefinitions("Directive");
- GenerateEnumClass(Directives, OS, "Directive", DirectivePrefix, CppNamespace,
- MakeEnumAvailableInNamespace);
+ GenerateEnumClass(DirLang.getDirectives(), OS, "Directive",
+ DirLang.getDirectivePrefix(), DirLang);
// Emit Clause enumeration
- const auto &Clauses = Records.getAllDerivedDefinitions("Clause");
- GenerateEnumClass(Clauses, OS, "Clause", ClausePrefix, CppNamespace,
- MakeEnumAvailableInNamespace);
+ GenerateEnumClass(DirLang.getClauses(), OS, "Clause",
+ DirLang.getClausePrefix(), DirLang);
+
+ // Emit ClauseVal enumeration
+ std::string EnumHelperFuncs;
+ GenerateEnumClauseVal(DirLang.getClauses(), OS, DirLang, EnumHelperFuncs);
// Generic function signatures
OS << "\n";
OS << "// Enumeration helper functions\n";
- OS << "Directive get" << LanguageName
+ OS << "Directive get" << DirLang.getName()
<< "DirectiveKind(llvm::StringRef Str);\n";
OS << "\n";
- OS << "llvm::StringRef get" << LanguageName
+ OS << "llvm::StringRef get" << DirLang.getName()
<< "DirectiveName(Directive D);\n";
OS << "\n";
- OS << "Clause get" << LanguageName << "ClauseKind(llvm::StringRef Str);\n";
+ OS << "Clause get" << DirLang.getName()
+ << "ClauseKind(llvm::StringRef Str);\n";
OS << "\n";
- OS << "llvm::StringRef get" << LanguageName << "ClauseName(Clause C);\n";
+ OS << "llvm::StringRef get" << DirLang.getName() << "ClauseName(Clause C);\n";
OS << "\n";
OS << "/// Return true if \\p C is a valid clause for \\p D in version \\p "
<< "Version.\n";
OS << "bool isAllowedClauseForDirective(Directive D, "
<< "Clause C, unsigned Version);\n";
OS << "\n";
+ if (EnumHelperFuncs.length() > 0) {
+ OS << EnumHelperFuncs;
+ OS << "\n";
+ }
// Closing namespaces
for (auto Ns : llvm::reverse(Namespaces))
@@ -156,138 +241,183 @@ void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
OS << "} // namespace llvm\n";
- OS << "#endif // LLVM_" << LanguageName << "_INC\n";
+ OS << "#endif // LLVM_" << DirLang.getName() << "_INC\n";
}
// Generate function implementation for get<Enum>Name(StringRef Str)
void GenerateGetName(const std::vector<Record *> &Records, raw_ostream &OS,
- StringRef Enum, StringRef Prefix, StringRef LanguageName,
- StringRef Namespace) {
+ StringRef Enum, const DirectiveLanguage &DirLang,
+ StringRef Prefix) {
OS << "\n";
- OS << "llvm::StringRef llvm::" << Namespace << "::get" << LanguageName << Enum
- << "Name(" << Enum << " Kind) {\n";
+ OS << "llvm::StringRef llvm::" << DirLang.getCppNamespace() << "::get"
+ << DirLang.getName() << Enum << "Name(" << Enum << " Kind) {\n";
OS << " switch (Kind) {\n";
for (const auto &R : Records) {
- const auto Name = R->getValueAsString("name");
- const auto AlternativeName = R->getValueAsString("alternativeName");
- OS << " case " << Prefix << getFormattedName(Name) << ":\n";
+ BaseRecord Rec{R};
+ OS << " case " << Prefix << Rec.getFormattedName() << ":\n";
OS << " return \"";
- if (AlternativeName.empty())
- OS << Name;
+ if (Rec.getAlternativeName().empty())
+ OS << Rec.getName();
else
- OS << AlternativeName;
+ OS << Rec.getAlternativeName();
OS << "\";\n";
}
OS << " }\n"; // switch
- OS << " llvm_unreachable(\"Invalid " << LanguageName << " " << Enum
+ OS << " llvm_unreachable(\"Invalid " << DirLang.getName() << " " << Enum
<< " kind\");\n";
OS << "}\n";
}
// Generate function implementation for get<Enum>Kind(StringRef Str)
void GenerateGetKind(const std::vector<Record *> &Records, raw_ostream &OS,
- StringRef Enum, StringRef Prefix, StringRef LanguageName,
- StringRef Namespace, bool ImplicitAsUnknown) {
+ StringRef Enum, const DirectiveLanguage &DirLang,
+ StringRef Prefix, bool ImplicitAsUnknown) {
- auto DefaultIt = std::find_if(Records.begin(), Records.end(), [](Record *R) {
- return R->getValueAsBit("isDefault") == true;
- });
+ auto DefaultIt = llvm::find_if(
+ Records, [](Record *R) { return R->getValueAsBit("isDefault") == true; });
if (DefaultIt == Records.end()) {
- PrintError("A least one " + Enum + " must be defined as default.");
+ PrintError("At least one " + Enum + " must be defined as default.");
return;
}
- const auto FormattedDefaultName =
- getFormattedName((*DefaultIt)->getValueAsString("name"));
+ BaseRecord DefaultRec{(*DefaultIt)};
OS << "\n";
- OS << Enum << " llvm::" << Namespace << "::get" << LanguageName << Enum
- << "Kind(llvm::StringRef Str) {\n";
+ OS << Enum << " llvm::" << DirLang.getCppNamespace() << "::get"
+ << DirLang.getName() << Enum << "Kind(llvm::StringRef Str) {\n";
OS << " return llvm::StringSwitch<" << Enum << ">(Str)\n";
for (const auto &R : Records) {
- const auto Name = R->getValueAsString("name");
+ BaseRecord Rec{R};
if (ImplicitAsUnknown && R->getValueAsBit("isImplicit")) {
- OS << " .Case(\"" << Name << "\"," << Prefix << FormattedDefaultName
- << ")\n";
+ OS << " .Case(\"" << Rec.getName() << "\"," << Prefix
+ << DefaultRec.getFormattedName() << ")\n";
} else {
- OS << " .Case(\"" << Name << "\"," << Prefix << getFormattedName(Name)
- << ")\n";
+ OS << " .Case(\"" << Rec.getName() << "\"," << Prefix
+ << Rec.getFormattedName() << ")\n";
}
}
- OS << " .Default(" << Prefix << FormattedDefaultName << ");\n";
+ OS << " .Default(" << Prefix << DefaultRec.getFormattedName() << ");\n";
OS << "}\n";
}
+// Generate function implementation for get<ClauseVal>Kind(StringRef Str)
+void GenerateGetKindClauseVal(const DirectiveLanguage &DirLang,
+ raw_ostream &OS) {
+ for (const auto &R : DirLang.getClauses()) {
+ Clause C{R};
+ const auto &ClauseVals = C.getClauseVals();
+ if (ClauseVals.size() <= 0)
+ continue;
+
+ auto DefaultIt = llvm::find_if(ClauseVals, [](Record *CV) {
+ return CV->getValueAsBit("isDefault") == true;
+ });
+
+ if (DefaultIt == ClauseVals.end()) {
+ PrintError("At least one val in Clause " + C.getFormattedName() +
+ " must be defined as default.");
+ return;
+ }
+ const auto DefaultName = (*DefaultIt)->getName();
+
+ const auto &EnumName = C.getEnumName();
+ if (EnumName.size() == 0) {
+ PrintError("enumClauseValue field not set in Clause" +
+ C.getFormattedName() + ".");
+ return;
+ }
+
+ OS << "\n";
+ OS << EnumName << " llvm::" << DirLang.getCppNamespace() << "::get"
+ << EnumName << "(llvm::StringRef Str) {\n";
+ OS << " return llvm::StringSwitch<" << EnumName << ">(Str)\n";
+ for (const auto &CV : ClauseVals) {
+ ClauseVal CVal{CV};
+ OS << " .Case(\"" << CVal.getFormattedName() << "\"," << CV->getName()
+ << ")\n";
+ }
+ OS << " .Default(" << DefaultName << ");\n";
+ OS << "}\n";
+
+ OS << "\n";
+ OS << "llvm::StringRef llvm::" << DirLang.getCppNamespace() << "::get"
+ << DirLang.getName() << EnumName
+ << "Name(llvm::" << DirLang.getCppNamespace() << "::" << EnumName
+ << " x) {\n";
+ OS << " switch (x) {\n";
+ for (const auto &CV : ClauseVals) {
+ ClauseVal CVal{CV};
+ OS << " case " << CV->getName() << ":\n";
+ OS << " return \"" << CVal.getFormattedName() << "\";\n";
+ }
+ OS << " }\n"; // switch
+ OS << " llvm_unreachable(\"Invalid " << DirLang.getName() << " "
+ << EnumName << " kind\");\n";
+ OS << "}\n";
+ }
+}
+
void GenerateCaseForVersionedClauses(const std::vector<Record *> &Clauses,
raw_ostream &OS, StringRef DirectiveName,
- StringRef DirectivePrefix,
- StringRef ClausePrefix,
+ const DirectiveLanguage &DirLang,
llvm::StringSet<> &Cases) {
for (const auto &C : Clauses) {
- const auto MinVersion = C->getValueAsInt("minVersion");
- const auto MaxVersion = C->getValueAsInt("maxVersion");
- const auto SpecificClause = C->getValueAsDef("clause");
- const auto ClauseName =
- getFormattedName(SpecificClause->getValueAsString("name"));
+ VersionedClause VerClause{C};
- if (Cases.find(ClauseName) == Cases.end()) {
- Cases.insert(ClauseName);
- OS << " case " << ClausePrefix << ClauseName << ":\n";
- OS << " return " << MinVersion << " <= Version && " << MaxVersion
- << " >= Version;\n";
+ const auto ClauseFormattedName = VerClause.getClause().getFormattedName();
+
+ if (Cases.find(ClauseFormattedName) == Cases.end()) {
+ Cases.insert(ClauseFormattedName);
+ OS << " case " << DirLang.getClausePrefix() << ClauseFormattedName
+ << ":\n";
+ OS << " return " << VerClause.getMinVersion()
+ << " <= Version && " << VerClause.getMaxVersion() << " >= Version;\n";
}
}
}
// Generate the isAllowedClauseForDirective function implementation.
-void GenerateIsAllowedClause(const std::vector<Record *> &Directives,
- raw_ostream &OS, StringRef LanguageName,
- StringRef DirectivePrefix, StringRef ClausePrefix,
- StringRef CppNamespace) {
+void GenerateIsAllowedClause(const DirectiveLanguage &DirLang,
+ raw_ostream &OS) {
OS << "\n";
- OS << "bool llvm::" << CppNamespace << "::isAllowedClauseForDirective("
+ OS << "bool llvm::" << DirLang.getCppNamespace()
+ << "::isAllowedClauseForDirective("
<< "Directive D, Clause C, unsigned Version) {\n";
- OS << " assert(unsigned(D) <= llvm::" << CppNamespace
+ OS << " assert(unsigned(D) <= llvm::" << DirLang.getCppNamespace()
<< "::Directive_enumSize);\n";
- OS << " assert(unsigned(C) <= llvm::" << CppNamespace
+ OS << " assert(unsigned(C) <= llvm::" << DirLang.getCppNamespace()
<< "::Clause_enumSize);\n";
OS << " switch (D) {\n";
- for (const auto &D : Directives) {
+ for (const auto &D : DirLang.getDirectives()) {
+ Directive Dir{D};
- const auto DirectiveName = D->getValueAsString("name");
- const auto &AllowedClauses = D->getValueAsListOfDefs("allowedClauses");
- const auto &AllowedOnceClauses =
- D->getValueAsListOfDefs("allowedOnceClauses");
- const auto &AllowedExclusiveClauses =
- D->getValueAsListOfDefs("allowedExclusiveClauses");
- const auto &RequiredClauses = D->getValueAsListOfDefs("requiredClauses");
-
- OS << " case " << DirectivePrefix << getFormattedName(DirectiveName)
+ OS << " case " << DirLang.getDirectivePrefix() << Dir.getFormattedName()
<< ":\n";
- if (AllowedClauses.size() == 0 && AllowedOnceClauses.size() == 0 &&
- AllowedExclusiveClauses.size() == 0 && RequiredClauses.size() == 0) {
+ if (Dir.getAllowedClauses().size() == 0 &&
+ Dir.getAllowedOnceClauses().size() == 0 &&
+ Dir.getAllowedExclusiveClauses().size() == 0 &&
+ Dir.getRequiredClauses().size() == 0) {
OS << " return false;\n";
} else {
OS << " switch (C) {\n";
llvm::StringSet<> Cases;
- GenerateCaseForVersionedClauses(AllowedClauses, OS, DirectiveName,
- DirectivePrefix, ClausePrefix, Cases);
+ GenerateCaseForVersionedClauses(Dir.getAllowedClauses(), OS,
+ Dir.getName(), DirLang, Cases);
- GenerateCaseForVersionedClauses(AllowedOnceClauses, OS, DirectiveName,
- DirectivePrefix, ClausePrefix, Cases);
+ GenerateCaseForVersionedClauses(Dir.getAllowedOnceClauses(), OS,
+ Dir.getName(), DirLang, Cases);
- GenerateCaseForVersionedClauses(AllowedExclusiveClauses, OS,
- DirectiveName, DirectivePrefix,
- ClausePrefix, Cases);
+ GenerateCaseForVersionedClauses(Dir.getAllowedExclusiveClauses(), OS,
+ Dir.getName(), DirLang, Cases);
- GenerateCaseForVersionedClauses(RequiredClauses, OS, DirectiveName,
- DirectivePrefix, ClausePrefix, Cases);
+ GenerateCaseForVersionedClauses(Dir.getRequiredClauses(), OS,
+ Dir.getName(), DirLang, Cases);
OS << " default:\n";
OS << " return false;\n";
@@ -297,37 +427,32 @@ void GenerateIsAllowedClause(const std::vector<Record *> &Directives,
}
OS << " }\n"; // End of directives switch
- OS << " llvm_unreachable(\"Invalid " << LanguageName
+ OS << " llvm_unreachable(\"Invalid " << DirLang.getName()
<< " Directive kind\");\n";
OS << "}\n"; // End of function isAllowedClauseForDirective
}
// Generate a simple enum set with the give clauses.
void GenerateClauseSet(const std::vector<Record *> &Clauses, raw_ostream &OS,
- StringRef ClauseEnumSetClass, StringRef ClauseSetPrefix,
- StringRef DirectiveName, StringRef DirectivePrefix,
- StringRef ClausePrefix, StringRef CppNamespace) {
+ StringRef ClauseSetPrefix, Directive &Dir,
+ const DirectiveLanguage &DirLang) {
OS << "\n";
- OS << " static " << ClauseEnumSetClass << " " << ClauseSetPrefix
- << DirectivePrefix << getFormattedName(DirectiveName) << " {\n";
+ OS << " static " << DirLang.getClauseEnumSetClass() << " " << ClauseSetPrefix
+ << DirLang.getDirectivePrefix() << Dir.getFormattedName() << " {\n";
for (const auto &C : Clauses) {
- const auto SpecificClause = C->getValueAsDef("clause");
- const auto ClauseName = SpecificClause->getValueAsString("name");
- OS << " llvm::" << CppNamespace << "::Clause::" << ClausePrefix
- << getFormattedName(ClauseName) << ",\n";
+ VersionedClause VerClause{C};
+ OS << " llvm::" << DirLang.getCppNamespace()
+ << "::Clause::" << DirLang.getClausePrefix()
+ << VerClause.getClause().getFormattedName() << ",\n";
}
OS << " };\n";
}
// Generate an enum set for the 4 kinds of clauses linked to a directive.
-void GenerateDirectiveClauseSets(const std::vector<Record *> &Directives,
- raw_ostream &OS, StringRef LanguageName,
- StringRef ClauseEnumSetClass,
- StringRef DirectivePrefix,
- StringRef ClausePrefix,
- StringRef CppNamespace) {
+void GenerateDirectiveClauseSets(const DirectiveLanguage &DirLang,
+ raw_ostream &OS) {
IfDefScope Scope("GEN_FLANG_DIRECTIVE_CLAUSE_SETS", OS);
@@ -336,35 +461,24 @@ void GenerateDirectiveClauseSets(const std::vector<Record *> &Directives,
// Open namespaces defined in the directive language.
llvm::SmallVector<StringRef, 2> Namespaces;
- llvm::SplitString(CppNamespace, Namespaces, "::");
+ llvm::SplitString(DirLang.getCppNamespace(), Namespaces, "::");
for (auto Ns : Namespaces)
OS << "namespace " << Ns << " {\n";
- for (const auto &D : Directives) {
- const auto DirectiveName = D->getValueAsString("name");
-
- const auto &AllowedClauses = D->getValueAsListOfDefs("allowedClauses");
- const auto &AllowedOnceClauses =
- D->getValueAsListOfDefs("allowedOnceClauses");
- const auto &AllowedExclusiveClauses =
- D->getValueAsListOfDefs("allowedExclusiveClauses");
- const auto &RequiredClauses = D->getValueAsListOfDefs("requiredClauses");
+ for (const auto &D : DirLang.getDirectives()) {
+ Directive Dir{D};
OS << "\n";
- OS << " // Sets for " << DirectiveName << "\n";
+ OS << " // Sets for " << Dir.getName() << "\n";
- GenerateClauseSet(AllowedClauses, OS, ClauseEnumSetClass, "allowedClauses_",
- DirectiveName, DirectivePrefix, ClausePrefix,
- CppNamespace);
- GenerateClauseSet(AllowedOnceClauses, OS, ClauseEnumSetClass,
- "allowedOnceClauses_", DirectiveName, DirectivePrefix,
- ClausePrefix, CppNamespace);
- GenerateClauseSet(AllowedExclusiveClauses, OS, ClauseEnumSetClass,
- "allowedExclusiveClauses_", DirectiveName,
- DirectivePrefix, ClausePrefix, CppNamespace);
- GenerateClauseSet(RequiredClauses, OS, ClauseEnumSetClass,
- "requiredClauses_", DirectiveName, DirectivePrefix,
- ClausePrefix, CppNamespace);
+ GenerateClauseSet(Dir.getAllowedClauses(), OS, "allowedClauses_", Dir,
+ DirLang);
+ GenerateClauseSet(Dir.getAllowedOnceClauses(), OS, "allowedOnceClauses_",
+ Dir, DirLang);
+ GenerateClauseSet(Dir.getAllowedExclusiveClauses(), OS,
+ "allowedExclusiveClauses_", Dir, DirLang);
+ GenerateClauseSet(Dir.getRequiredClauses(), OS, "requiredClauses_", Dir,
+ DirLang);
}
// Closing namespaces
@@ -377,118 +491,253 @@ void GenerateDirectiveClauseSets(const std::vector<Record *> &Directives,
// Generate a map of directive (key) with DirectiveClauses struct as values.
// The struct holds the 4 sets of enumeration for the 4 kinds of clauses
// allowances (allowed, allowed once, allowed exclusive and required).
-void GenerateDirectiveClauseMap(const std::vector<Record *> &Directives,
- raw_ostream &OS, StringRef LanguageName,
- StringRef ClauseEnumSetClass,
- StringRef DirectivePrefix,
- StringRef ClausePrefix,
- StringRef CppNamespace) {
+void GenerateDirectiveClauseMap(const DirectiveLanguage &DirLang,
+ raw_ostream &OS) {
IfDefScope Scope("GEN_FLANG_DIRECTIVE_CLAUSE_MAP", OS);
OS << "\n";
- OS << "struct " << LanguageName << "DirectiveClauses {\n";
- OS << " const " << ClauseEnumSetClass << " allowed;\n";
- OS << " const " << ClauseEnumSetClass << " allowedOnce;\n";
- OS << " const " << ClauseEnumSetClass << " allowedExclusive;\n";
- OS << " const " << ClauseEnumSetClass << " requiredOneOf;\n";
- OS << "};\n";
-
- OS << "\n";
-
- OS << "std::unordered_map<llvm::" << CppNamespace << "::Directive, "
- << LanguageName << "DirectiveClauses>\n";
- OS << " directiveClausesTable = {\n";
+ OS << "{\n";
- for (const auto &D : Directives) {
- const auto FormattedDirectiveName =
- getFormattedName(D->getValueAsString("name"));
- OS << " {llvm::" << CppNamespace << "::Directive::" << DirectivePrefix
- << FormattedDirectiveName << ",\n";
+ for (const auto &D : DirLang.getDirectives()) {
+ Directive Dir{D};
+ OS << " {llvm::" << DirLang.getCppNamespace()
+ << "::Directive::" << DirLang.getDirectivePrefix()
+ << Dir.getFormattedName() << ",\n";
OS << " {\n";
- OS << " llvm::" << CppNamespace << "::allowedClauses_"
- << DirectivePrefix << FormattedDirectiveName << ",\n";
- OS << " llvm::" << CppNamespace << "::allowedOnceClauses_"
- << DirectivePrefix << FormattedDirectiveName << ",\n";
- OS << " llvm::" << CppNamespace << "::allowedExclusiveClauses_"
- << DirectivePrefix << FormattedDirectiveName << ",\n";
- OS << " llvm::" << CppNamespace << "::requiredClauses_"
- << DirectivePrefix << FormattedDirectiveName << ",\n";
+ OS << " llvm::" << DirLang.getCppNamespace() << "::allowedClauses_"
+ << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n";
+ OS << " llvm::" << DirLang.getCppNamespace() << "::allowedOnceClauses_"
+ << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n";
+ OS << " llvm::" << DirLang.getCppNamespace()
+ << "::allowedExclusiveClauses_" << DirLang.getDirectivePrefix()
+ << Dir.getFormattedName() << ",\n";
+ OS << " llvm::" << DirLang.getCppNamespace() << "::requiredClauses_"
+ << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n";
OS << " }\n";
OS << " },\n";
}
- OS << "};\n";
+ OS << "}\n";
}
-// Generate the implemenation section for the enumeration in the directive
-// language
-void EmitDirectivesFlangImpl(const std::vector<Record *> &Directives,
- raw_ostream &OS, StringRef LanguageName,
- StringRef ClauseEnumSetClass,
- StringRef DirectivePrefix, StringRef ClausePrefix,
- StringRef CppNamespace) {
+// Generate classes entry for Flang clauses in the Flang parse-tree
+// If the clause as a non-generic class, no entry is generated.
+// If the clause does not hold a value, an EMPTY_CLASS is used.
+// If the clause class is generic then a WRAPPER_CLASS is used. When the value
+// is optional, the value class is wrapped into a std::optional.
+void GenerateFlangClauseParserClass(const DirectiveLanguage &DirLang,
+ raw_ostream &OS) {
- GenerateDirectiveClauseSets(Directives, OS, LanguageName, ClauseEnumSetClass,
- DirectivePrefix, ClausePrefix, CppNamespace);
+ IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES", OS);
- GenerateDirectiveClauseMap(Directives, OS, LanguageName, ClauseEnumSetClass,
- DirectivePrefix, ClausePrefix, CppNamespace);
+ OS << "\n";
+
+ for (const auto &C : DirLang.getClauses()) {
+ Clause Clause{C};
+ if (!Clause.getFlangClass().empty()) {
+ OS << "WRAPPER_CLASS(" << Clause.getFormattedParserClassName() << ", ";
+ if (Clause.isValueOptional() && Clause.isValueList()) {
+ OS << "std::optional<std::list<" << Clause.getFlangClass() << ">>";
+ } else if (Clause.isValueOptional()) {
+ OS << "std::optional<" << Clause.getFlangClass() << ">";
+ } else if (Clause.isValueList()) {
+ OS << "std::list<" << Clause.getFlangClass() << ">";
+ } else {
+ OS << Clause.getFlangClass();
+ }
+ } else {
+ OS << "EMPTY_CLASS(" << Clause.getFormattedParserClassName();
+ }
+ OS << ");\n";
+ }
}
-// Generate the implemenation section for the enumeration in the directive
-// language.
-void EmitDirectivesGen(RecordKeeper &Records, raw_ostream &OS) {
+// Generate a list of the different clause classes for Flang.
+void GenerateFlangClauseParserClassList(const DirectiveLanguage &DirLang,
+ raw_ostream &OS) {
- const auto &DirectiveLanguages =
- Records.getAllDerivedDefinitions("DirectiveLanguage");
+ IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES_LIST", OS);
- if (DirectiveLanguages.size() != 1) {
- PrintError("A single definition of DirectiveLanguage is needed.");
- return;
+ OS << "\n";
+ llvm::interleaveComma(DirLang.getClauses(), OS, [&](Record *C) {
+ Clause Clause{C};
+ OS << Clause.getFormattedParserClassName() << "\n";
+ });
+}
+
+// Generate dump node list for the clauses holding a generic class name.
+void GenerateFlangClauseDump(const DirectiveLanguage &DirLang,
+ raw_ostream &OS) {
+
+ IfDefScope Scope("GEN_FLANG_DUMP_PARSE_TREE_CLAUSES", OS);
+
+ OS << "\n";
+ for (const auto &C : DirLang.getClauses()) {
+ Clause Clause{C};
+ OS << "NODE(" << DirLang.getFlangClauseBaseClass() << ", "
+ << Clause.getFormattedParserClassName() << ")\n";
+ }
+}
+
+// Generate Unparse functions for clauses classes in the Flang parse-tree
+// If the clause is a non-generic class, no entry is generated.
+void GenerateFlangClauseUnparse(const DirectiveLanguage &DirLang,
+ raw_ostream &OS) {
+
+ IfDefScope Scope("GEN_FLANG_CLAUSE_UNPARSE", OS);
+
+ OS << "\n";
+
+ for (const auto &C : DirLang.getClauses()) {
+ Clause Clause{C};
+ if (!Clause.getFlangClass().empty()) {
+ if (Clause.isValueOptional() && Clause.getDefaultValue().empty()) {
+ OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass()
+ << "::" << Clause.getFormattedParserClassName() << " &x) {\n";
+ OS << " Word(\"" << Clause.getName().upper() << "\");\n";
+
+ OS << " Walk(\"(\", x.v, \")\");\n";
+ OS << "}\n";
+ } else if (Clause.isValueOptional()) {
+ OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass()
+ << "::" << Clause.getFormattedParserClassName() << " &x) {\n";
+ OS << " Word(\"" << Clause.getName().upper() << "\");\n";
+ OS << " Put(\"(\");\n";
+ OS << " if (x.v.has_value())\n";
+ if (Clause.isValueList())
+ OS << " Walk(x.v, \",\");\n";
+ else
+ OS << " Walk(x.v);\n";
+ OS << " else\n";
+ OS << " Put(\"" << Clause.getDefaultValue() << "\");\n";
+ OS << " Put(\")\");\n";
+ OS << "}\n";
+ } else {
+ OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass()
+ << "::" << Clause.getFormattedParserClassName() << " &x) {\n";
+ OS << " Word(\"" << Clause.getName().upper() << "\");\n";
+ OS << " Put(\"(\");\n";
+ if (Clause.isValueList())
+ OS << " Walk(x.v, \",\");\n";
+ else
+ OS << " Walk(x.v);\n";
+ OS << " Put(\")\");\n";
+ OS << "}\n";
+ }
+ } else {
+ OS << "void Before(const " << DirLang.getFlangClauseBaseClass()
+ << "::" << Clause.getFormattedParserClassName() << " &) { Word(\""
+ << Clause.getName().upper() << "\"); }\n";
+ }
}
+}
+
+// Generate the implementation section for the enumeration in the directive
+// language
+void EmitDirectivesFlangImpl(const DirectiveLanguage &DirLang,
+ raw_ostream &OS) {
+
+ GenerateDirectiveClauseSets(DirLang, OS);
+
+ GenerateDirectiveClauseMap(DirLang, OS);
- const auto &DirectiveLanguage = DirectiveLanguages[0];
- StringRef DirectivePrefix =
- DirectiveLanguage->getValueAsString("directivePrefix");
- StringRef LanguageName = DirectiveLanguage->getValueAsString("name");
- StringRef ClausePrefix = DirectiveLanguage->getValueAsString("clausePrefix");
- StringRef CppNamespace = DirectiveLanguage->getValueAsString("cppNamespace");
- StringRef ClauseEnumSetClass =
- DirectiveLanguage->getValueAsString("clauseEnumSetClass");
+ GenerateFlangClauseParserClass(DirLang, OS);
- const auto &Directives = Records.getAllDerivedDefinitions("Directive");
+ GenerateFlangClauseParserClassList(DirLang, OS);
- EmitDirectivesFlangImpl(Directives, OS, LanguageName, ClauseEnumSetClass,
- DirectivePrefix, ClausePrefix, CppNamespace);
+ GenerateFlangClauseDump(DirLang, OS);
+
+ GenerateFlangClauseUnparse(DirLang, OS);
}
-// Generate the implemenation for the enumeration in the directive
-// language. This code can be included in library.
-void EmitDirectivesImpl(RecordKeeper &Records, raw_ostream &OS) {
+void GenerateClauseClassMacro(const DirectiveLanguage &DirLang,
+ raw_ostream &OS) {
+ // Generate macros style information for legacy code in clang
+ IfDefScope Scope("GEN_CLANG_CLAUSE_CLASS", OS);
- const auto &DirectiveLanguages =
- Records.getAllDerivedDefinitions("DirectiveLanguage");
+ OS << "\n";
- if (DirectiveLanguages.size() != 1) {
- PrintError("A single definition of DirectiveLanguage is needed.");
- return;
+ OS << "#ifndef CLAUSE\n";
+ OS << "#define CLAUSE(Enum, Str, Implicit)\n";
+ OS << "#endif\n";
+ OS << "#ifndef CLAUSE_CLASS\n";
+ OS << "#define CLAUSE_CLASS(Enum, Str, Class)\n";
+ OS << "#endif\n";
+ OS << "#ifndef CLAUSE_NO_CLASS\n";
+ OS << "#define CLAUSE_NO_CLASS(Enum, Str)\n";
+ OS << "#endif\n";
+ OS << "\n";
+ OS << "#define __CLAUSE(Name, Class) \\\n";
+ OS << " CLAUSE(" << DirLang.getClausePrefix()
+ << "##Name, #Name, /* Implicit */ false) \\\n";
+ OS << " CLAUSE_CLASS(" << DirLang.getClausePrefix()
+ << "##Name, #Name, Class)\n";
+ OS << "#define __CLAUSE_NO_CLASS(Name) \\\n";
+ OS << " CLAUSE(" << DirLang.getClausePrefix()
+ << "##Name, #Name, /* Implicit */ false) \\\n";
+ OS << " CLAUSE_NO_CLASS(" << DirLang.getClausePrefix() << "##Name, #Name)\n";
+ OS << "#define __IMPLICIT_CLAUSE_CLASS(Name, Str, Class) \\\n";
+ OS << " CLAUSE(" << DirLang.getClausePrefix()
+ << "##Name, Str, /* Implicit */ true) \\\n";
+ OS << " CLAUSE_CLASS(" << DirLang.getClausePrefix()
+ << "##Name, Str, Class)\n";
+ OS << "#define __IMPLICIT_CLAUSE_NO_CLASS(Name, Str) \\\n";
+ OS << " CLAUSE(" << DirLang.getClausePrefix()
+ << "##Name, Str, /* Implicit */ true) \\\n";
+ OS << " CLAUSE_NO_CLASS(" << DirLang.getClausePrefix() << "##Name, Str)\n";
+ OS << "\n";
+
+ for (const auto &R : DirLang.getClauses()) {
+ Clause C{R};
+ if (C.getClangClass().empty()) { // NO_CLASS
+ if (C.isImplicit()) {
+ OS << "__IMPLICIT_CLAUSE_NO_CLASS(" << C.getFormattedName() << ", \""
+ << C.getFormattedName() << "\")\n";
+ } else {
+ OS << "__CLAUSE_NO_CLASS(" << C.getFormattedName() << ")\n";
+ }
+ } else { // CLASS
+ if (C.isImplicit()) {
+ OS << "__IMPLICIT_CLAUSE_CLASS(" << C.getFormattedName() << ", \""
+ << C.getFormattedName() << "\", " << C.getClangClass() << ")\n";
+ } else {
+ OS << "__CLAUSE(" << C.getFormattedName() << ", " << C.getClangClass()
+ << ")\n";
+ }
+ }
}
- const auto &DirectiveLanguage = DirectiveLanguages[0];
- StringRef DirectivePrefix =
- DirectiveLanguage->getValueAsString("directivePrefix");
- StringRef LanguageName = DirectiveLanguage->getValueAsString("name");
- StringRef ClausePrefix = DirectiveLanguage->getValueAsString("clausePrefix");
- StringRef CppNamespace = DirectiveLanguage->getValueAsString("cppNamespace");
- const auto &Directives = Records.getAllDerivedDefinitions("Directive");
- const auto &Clauses = Records.getAllDerivedDefinitions("Clause");
+ OS << "\n";
+ OS << "#undef __IMPLICIT_CLAUSE_NO_CLASS\n";
+ OS << "#undef __IMPLICIT_CLAUSE_CLASS\n";
+ OS << "#undef __CLAUSE\n";
+ OS << "#undef CLAUSE_NO_CLASS\n";
+ OS << "#undef CLAUSE_CLASS\n";
+ OS << "#undef CLAUSE\n";
+}
- StringRef IncludeHeader =
- DirectiveLanguage->getValueAsString("includeHeader");
+// Generate the implementation section for the enumeration in the directive
+// language.
+void EmitDirectivesGen(RecordKeeper &Records, raw_ostream &OS) {
+ const auto DirLang = DirectiveLanguage{Records};
+ if (DirLang.HasValidityErrors())
+ return;
- if (!IncludeHeader.empty())
- OS << "#include \"" << IncludeHeader << "\"\n\n";
+ EmitDirectivesFlangImpl(DirLang, OS);
+
+ GenerateClauseClassMacro(DirLang, OS);
+}
+
+// Generate the implementation for the enumeration in the directive
+// language. This code can be included in library.
+void EmitDirectivesImpl(RecordKeeper &Records, raw_ostream &OS) {
+ const auto DirLang = DirectiveLanguage{Records};
+ if (DirLang.HasValidityErrors())
+ return;
+
+ if (!DirLang.getIncludeHeader().empty())
+ OS << "#include \"" << DirLang.getIncludeHeader() << "\"\n\n";
OS << "#include \"llvm/ADT/StringRef.h\"\n";
OS << "#include \"llvm/ADT/StringSwitch.h\"\n";
@@ -496,29 +745,32 @@ void EmitDirectivesImpl(RecordKeeper &Records, raw_ostream &OS) {
OS << "\n";
OS << "using namespace llvm;\n";
llvm::SmallVector<StringRef, 2> Namespaces;
- llvm::SplitString(CppNamespace, Namespaces, "::");
+ llvm::SplitString(DirLang.getCppNamespace(), Namespaces, "::");
for (auto Ns : Namespaces)
OS << "using namespace " << Ns << ";\n";
// getDirectiveKind(StringRef Str)
- GenerateGetKind(Directives, OS, "Directive", DirectivePrefix, LanguageName,
- CppNamespace, /*ImplicitAsUnknown=*/false);
+ GenerateGetKind(DirLang.getDirectives(), OS, "Directive", DirLang,
+ DirLang.getDirectivePrefix(), /*ImplicitAsUnknown=*/false);
// getDirectiveName(Directive Kind)
- GenerateGetName(Directives, OS, "Directive", DirectivePrefix, LanguageName,
- CppNamespace);
+ GenerateGetName(DirLang.getDirectives(), OS, "Directive", DirLang,
+ DirLang.getDirectivePrefix());
// getClauseKind(StringRef Str)
- GenerateGetKind(Clauses, OS, "Clause", ClausePrefix, LanguageName,
- CppNamespace, /*ImplicitAsUnknown=*/true);
+ GenerateGetKind(DirLang.getClauses(), OS, "Clause", DirLang,
+ DirLang.getClausePrefix(),
+ /*ImplicitAsUnknown=*/true);
// getClauseName(Clause Kind)
- GenerateGetName(Clauses, OS, "Clause", ClausePrefix, LanguageName,
- CppNamespace);
+ GenerateGetName(DirLang.getClauses(), OS, "Clause", DirLang,
+ DirLang.getClausePrefix());
+
+ // get<ClauseVal>Kind(StringRef Str)
+ GenerateGetKindClauseVal(DirLang, OS);
// isAllowedClauseForDirective(Directive D, Clause C, unsigned Version)
- GenerateIsAllowedClause(Directives, OS, LanguageName, DirectivePrefix,
- ClausePrefix, CppNamespace);
+ GenerateIsAllowedClause(DirLang, OS);
}
} // namespace llvm