Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for cpp:identifier Metadata, and the Groundwork for xxx:identifier Metadata in General #3292

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions cpp/src/Slice/MetadataValidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ Slice::validateMetadata(const UnitPtr& p, string_view prefix, map<string, Metada
MetadataInfo deprecatedInfo = {
.validOn =
{typeid(InterfaceDecl),
typeid(ClassDecl),
typeid(Operation),
typeid(Exception),
typeid(ClassDecl),
typeid(Slice::Exception),
typeid(Struct),
typeid(Sequence),
typeid(Dictionary),
Expand Down
49 changes: 24 additions & 25 deletions cpp/src/Slice/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ compareTag(const T& lhs, const T& rhs)
return lhs->tag() < rhs->tag();
}

// NOTE: It is important that this list is kept in alphabetical order!
constexpr string_view languages[] = {"cpp", "cs", "java", "js", "matlab", "php", "python", "ruby", "swift"};

// Forward declare things from Bison and Flex the parser can use.
extern int slice_parse();
extern int slice_lineno;
Expand Down Expand Up @@ -104,8 +107,6 @@ Slice::Metadata::Metadata(string rawMetadata, string file, int line) : GrammarBa
if (firstColonPos != string::npos)
{
// Check if the metadata starts with a language prefix.
// NOTE: It is important that this list is kept in alphabetical order!
constexpr string_view languages[] = {"cpp", "cs", "java", "js", "matlab", "php", "python", "rb", "swift"};
string prefix = rawMetadata.substr(0, firstColonPos);
bool hasLangPrefix = binary_search(&languages[0], &languages[sizeof(languages) / sizeof(*languages)], prefix);
if (hasLangPrefix)
Expand Down Expand Up @@ -554,35 +555,39 @@ Slice::Contained::container() const
}

string
Slice::Contained::name() const
Slice::Contained::name(string_view langPrefix) const
{
return _name;
}
// Check if any 'xxx:identifier' metadata has been applied to this element which matches `langPrefix.
// If so, we return that instead of the element's Slice identifier.
if (!langPrefix.empty())
{
// Safety-net to alert us in case we ever pass an invalid language prefix to this function.
assert(binary_search(&languages[0], &languages[sizeof(languages) / sizeof(*languages)], langPrefix));

string
Slice::Contained::scoped() const
{
return _scoped;
if (auto customName = getMetadataArgs(string(langPrefix) + ":identifier"))
{
return *customName;
}
}

return _name;
}

string
Slice::Contained::scope() const
Slice::Contained::scoped(string_view langPrefix) const
{
string::size_type idx = _scoped.rfind("::");
assert(idx != string::npos);
return string(_scoped, 0, idx + 2);
return scope(langPrefix) + name(langPrefix);
}

string
Slice::Contained::flattenedScope() const
Slice::Contained::scope(string_view langPrefix) const
{
string s = scope();
string::size_type pos = 0;
while ((pos = s.find("::", pos)) != string::npos)
string scoped;
if (auto container = dynamic_pointer_cast<Contained>(_container))
{
s.replace(pos, 2, "_");
scoped = container->scoped(langPrefix);
}
return s;
return scoped + "::";
}

string
Expand Down Expand Up @@ -1050,12 +1055,6 @@ Slice::Contained::Contained(const ContainerPtr& container, const string& name)
_container(container),
_name(name)
{
ContainedPtr cont = dynamic_pointer_cast<Contained>(_container);
if (cont)
{
_scoped = cont->scoped();
}
_scoped += "::" + _name;
assert(_unit);
_file = _unit->currentFile();
_line = _unit->currentLine();
Expand Down
13 changes: 7 additions & 6 deletions cpp/src/Slice/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,11 @@ namespace Slice
{
public:
[[nodiscard]] ContainerPtr container() const;
[[nodiscard]] std::string name() const;
[[nodiscard]] std::string scoped() const;
[[nodiscard]] std::string scope() const;
[[nodiscard]] std::string flattenedScope() const;

[[nodiscard]] std::string name(std::string_view langPrefix = "") const;
[[nodiscard]] std::string scoped(std::string_view langPrefix = "") const;
[[nodiscard]] std::string scope(std::string_view langPrefix = "") const;

[[nodiscard]] std::string file() const;
[[nodiscard]] int line() const;

Expand Down Expand Up @@ -401,7 +402,6 @@ namespace Slice

ContainerPtr _container;
std::string _name;
std::string _scoped;
std::string _file;
int _line;
std::string _docComment;
Expand Down Expand Up @@ -453,7 +453,6 @@ namespace Slice
EnumeratorList enumerators() const;
EnumeratorList enumerators(const std::string& identifier) const;
ContainedList contents() const;
std::string thisScope() const;
void visit(ParserVisitor* visitor) override;

bool checkIntroduced(const std::string& scopedName, ContainedPtr namedThing = nullptr);
Expand Down Expand Up @@ -483,6 +482,8 @@ namespace Slice
}

protected:
std::string thisScope() const;

bool validateConstant(
const std::string& name,
const TypePtr& type,
Expand Down
Loading
Loading