Skip to content

Commit

Permalink
No longer share metadata between 'Decl' and 'Def' types.
Browse files Browse the repository at this point in the history
  • Loading branch information
InsertCreativityHere committed Dec 4, 2024
1 parent 5948bd5 commit dfb4fc4
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 48 deletions.
10 changes: 6 additions & 4 deletions cpp/src/Slice/MetadataValidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ Slice::validateMetadata(const UnitPtr& p, string_view prefix, map<string, Metada

// "amd"
MetadataInfo amdInfo = {
{typeid(InterfaceDecl), typeid(Operation)},
{typeid(InterfaceDef), typeid(Operation)},
MetadataArgumentKind::NoArguments,
};
knownMetadata.emplace("amd", std::move(amdInfo));

// "deprecated"
MetadataInfo deprecatedInfo = {
{typeid(InterfaceDecl),
typeid(InterfaceDef),
typeid(ClassDecl),
typeid(ClassDef),
typeid(Operation),
typeid(Exception),
typeid(Struct),
Expand All @@ -97,22 +99,22 @@ Slice::validateMetadata(const UnitPtr& p, string_view prefix, map<string, Metada

// "format"
MetadataInfo formatInfo = {
{typeid(InterfaceDecl), typeid(Operation)},
{typeid(InterfaceDef), typeid(Operation)},
MetadataArgumentKind::SingleArgument,
{{"compact", "sliced", "default"}},
};
knownMetadata.emplace("format", std::move(formatInfo));

// "marshaled-result"
MetadataInfo marshaledResultInfo = {
{typeid(InterfaceDecl), typeid(Operation)},
{typeid(InterfaceDef), typeid(Operation)},
MetadataArgumentKind::NoArguments,
};
knownMetadata.emplace("marshaled-result", std::move(marshaledResultInfo));

// "protected"
MetadataInfo protectedInfo = {
{typeid(ClassDecl), typeid(Slice::Exception), typeid(Struct), typeid(DataMember)},
{typeid(ClassDef), typeid(Slice::Exception), typeid(Struct), typeid(DataMember)},
MetadataArgumentKind::NoArguments,
};
knownMetadata.emplace("protected", std::move(protectedInfo));
Expand Down
28 changes: 2 additions & 26 deletions cpp/src/Slice/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ Slice::Contained::setMetadata(MetadataList metadata)
bool
Slice::Contained::hasMetadata(string_view directive) const
{
for (const auto& p : getMetadata())
for (const auto& p : _metadata)
{
if (p->directive() == directive)
{
Expand All @@ -981,7 +981,7 @@ Slice::Contained::hasMetadata(string_view directive) const
optional<string>
Slice::Contained::getMetadataArgs(string_view directive) const
{
for (const auto& p : getMetadata())
for (const auto& p : _metadata)
{
if (p->directive() == directive)
{
Expand Down Expand Up @@ -2679,18 +2679,6 @@ Slice::ClassDef::compactId() const
return _compactId;
}

MetadataList
Slice::ClassDef::getMetadata() const
{
return _declaration->getMetadata();
}

void
Slice::ClassDef::setMetadata(MetadataList metadata)
{
_declaration->setMetadata(std::move(metadata));
}

Slice::ClassDef::ClassDef(const ContainerPtr& container, const string& name, int id, const ClassDefPtr& base)
: SyntaxTreeBase(container->unit()),
Container(container->unit()),
Expand Down Expand Up @@ -3112,18 +3100,6 @@ Slice::InterfaceDef::ids() const
return ids;
}

MetadataList
Slice::InterfaceDef::getMetadata() const
{
return _declaration->getMetadata();
}

void
Slice::InterfaceDef::setMetadata(MetadataList metadata)
{
_declaration->setMetadata(std::move(metadata));
}

Slice::InterfaceDef::InterfaceDef(const ContainerPtr& container, const string& name, const InterfaceList& bases)
: SyntaxTreeBase(container->unit()),
Container(container->unit()),
Expand Down
10 changes: 2 additions & 8 deletions cpp/src/Slice/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ namespace Slice

int includeLevel() const;

virtual MetadataList getMetadata() const;
virtual void setMetadata(MetadataList metadata);
MetadataList getMetadata() const;
void setMetadata(MetadataList metadata);
bool hasMetadata(std::string_view directive) const;
std::optional<std::string> getMetadataArgs(std::string_view directive) const;

Expand Down Expand Up @@ -572,9 +572,6 @@ namespace Slice
int compactId() const;
std::string kindOf() const final;

MetadataList getMetadata() const final;
void setMetadata(MetadataList metadata) final;

private:
friend class Container;

Expand Down Expand Up @@ -708,9 +705,6 @@ namespace Slice
// Returns the type IDs of all the interfaces in the inheritance tree, in alphabetical order.
StringList ids() const;

MetadataList getMetadata() const final;
void setMetadata(MetadataList metadata) final;

private:
friend class Container;

Expand Down
4 changes: 2 additions & 2 deletions cpp/src/slice2cpp/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,8 @@ Slice::Gen::validateMetadata(const UnitPtr& u)
typeInfo.extraValidation = [](const MetadataPtr& meta, const SyntaxTreeBasePtr& p) -> optional<string>
{
// 'cpp:type' can be placed on containers, but only if it is the 'string' flavor of the metadata.
if (dynamic_pointer_cast<Module>(p) || dynamic_pointer_cast<InterfaceDecl>(p) ||
dynamic_pointer_cast<ClassDecl>(p) || dynamic_pointer_cast<Struct>(p) ||
if (dynamic_pointer_cast<Module>(p) || dynamic_pointer_cast<InterfaceDef>(p) ||
dynamic_pointer_cast<ClassDef>(p) || dynamic_pointer_cast<Struct>(p) ||
dynamic_pointer_cast<Slice::Exception>(p))
{
const string& argument = meta->arguments();
Expand Down
13 changes: 10 additions & 3 deletions cpp/src/slice2cs/CsUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,13 @@ Slice::CsGenerator::MetadataVisitor::visitClassDecl(const ClassDeclPtr& p)
validate(p);
}

bool
Slice::CsGenerator::MetadataVisitor::visitClassDefStart(const ClassDefPtr& p)
{
validate(p);
return true;
}

bool
Slice::CsGenerator::MetadataVisitor::visitExceptionStart(const ExceptionPtr& p)
{
Expand Down Expand Up @@ -2017,15 +2024,15 @@ Slice::CsGenerator::MetadataVisitor::validate(const ContainedPtr& cont)
continue;
}
}
else if (dynamic_pointer_cast<ClassDecl>(cont) || dynamic_pointer_cast<Exception>(cont))
else if (dynamic_pointer_cast<ClassDef>(cont) || dynamic_pointer_cast<Exception>(cont))
{
if (directive == "cs:property" && arguments.empty())
{
newLocalMetadata.push_back(metadata);
continue;
}
}
else if (dynamic_pointer_cast<InterfaceDecl>(cont))
else if (dynamic_pointer_cast<InterfaceDef>(cont))
{
if (directive == "cs:implements" && !arguments.empty())
{
Expand All @@ -2049,7 +2056,7 @@ Slice::CsGenerator::MetadataVisitor::validate(const ContainedPtr& cont)
DataMemberPtr dataMember = dynamic_pointer_cast<DataMember>(cont);
StructPtr st = dynamic_pointer_cast<Struct>(dataMember->container());
ExceptionPtr ex = dynamic_pointer_cast<Exception>(dataMember->container());
ClassDeclPtr cl = dynamic_pointer_cast<ClassDecl>(dataMember->container());
ClassDefPtr cl = dynamic_pointer_cast<ClassDef>(dataMember->container());
static const string csTypePrefix = "cs:type:";
// TODO Seems like this validation only got half written?...
}
Expand Down
1 change: 1 addition & 0 deletions cpp/src/slice2cs/CsUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ namespace Slice
bool visitUnitStart(const UnitPtr&) final;
bool visitModuleStart(const ModulePtr&) final;
void visitClassDecl(const ClassDeclPtr&) final;
bool visitClassDefStart(const ClassDefPtr&) final;
bool visitExceptionStart(const ExceptionPtr&) final;
bool visitStructStart(const StructPtr&) final;
void visitOperation(const OperationPtr&) final;
Expand Down
12 changes: 10 additions & 2 deletions cpp/src/slice2java/JavaUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ namespace
p->setMetadata(std::move(metadata));
}

bool visitClassDefStart(const ClassDefPtr& p) final
{
MetadataList metadata = getMetadata(p);
metadata = validateType(p, metadata);
metadata = validateGetSet(p, metadata);
p->setMetadata(std::move(metadata));
}

bool visitExceptionStart(const ExceptionPtr& p) final
{
MetadataList metadata = getMetadata(p);
Expand Down Expand Up @@ -398,7 +406,7 @@ namespace
}
else if (directive == "java:implements")
{
if (dynamic_pointer_cast<ClassDecl>(p) || dynamic_pointer_cast<Struct>(p))
if (dynamic_pointer_cast<ClassDef>(p) || dynamic_pointer_cast<Struct>(p))
{
newMetadata.push_back(m);
}
Expand Down Expand Up @@ -438,7 +446,7 @@ namespace
{
// The "getset" metadata can only be specified on a class, struct, exception or data member.
if (m->directive() == "java:getset" &&
(!dynamic_pointer_cast<ClassDecl>(p) && !dynamic_pointer_cast<Struct>(p) &&
(!dynamic_pointer_cast<ClassDef>(p) && !dynamic_pointer_cast<Struct>(p) &&
!dynamic_pointer_cast<Slice::Exception>(p) && !dynamic_pointer_cast<DataMember>(p)))
{
p->unit()->warning(m->file(), m->line(), InvalidMetadata, "invalid metadata for " + p->kindOf());
Expand Down
16 changes: 16 additions & 0 deletions cpp/src/slice2py/PythonUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ namespace Slice
bool visitUnitStart(const UnitPtr&) final;
bool visitModuleStart(const ModulePtr&) final;
void visitClassDecl(const ClassDeclPtr&) final;
bool visitClassDefStart(const ClassDefPtr&) final;
void visitInterfaceDecl(const InterfaceDeclPtr&) final;
bool visitInterfaceDefStart(const InterfaceDefPtr&) final;
bool visitExceptionStart(const ExceptionPtr&) final;
bool visitStructStart(const StructPtr&) final;
void visitOperation(const OperationPtr&) final;
Expand Down Expand Up @@ -2973,12 +2975,26 @@ Slice::Python::MetadataVisitor::visitClassDecl(const ClassDeclPtr& p)
reject(p);
}

bool
Slice::Python::MetadataVisitor::visitClassDefStart(const ClassDefPtr& p)
{
reject(p);
return true;
}

void
Slice::Python::MetadataVisitor::visitInterfaceDecl(const InterfaceDeclPtr& p)
{
reject(p);
}

bool
Slice::Python::MetadataVisitor::visitInterfaceDefStart(const InterfaceDefPtr& p)
{
reject(p);
return true;
}

bool
Slice::Python::MetadataVisitor::visitExceptionStart(const ExceptionPtr& p)
{
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/slice2swift/SwiftUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2118,12 +2118,12 @@ SwiftGenerator::MetadataVisitor::validate(const SyntaxTreeBasePtr& p, const Cont
continue;
}

if (dynamic_pointer_cast<InterfaceDecl>(p) && directive == "swift:inherits" && !arguments.empty())
if (dynamic_pointer_cast<InterfaceDef>(p) && directive == "swift:inherits" && !arguments.empty())
{
continue;
}

if ((dynamic_pointer_cast<ClassDecl>(p) || dynamic_pointer_cast<InterfaceDecl>(p) ||
if ((dynamic_pointer_cast<ClassDef>(p) || dynamic_pointer_cast<InterfaceDef>(p) ||
dynamic_pointer_cast<Enum>(p) || dynamic_pointer_cast<Exception>(p)) &&
directive == "swift:attribute" && !arguments.empty())
{
Expand Down
2 changes: 1 addition & 1 deletion cpp/test/Slice/errorDetection/WarningInvalidMetadata.err
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ WarningInvalidMetadata.ice:31: warning: the 'cpp:no-default-include' metadata do
WarningInvalidMetadata.ice:34: warning: 'amd' metadata cannot be specified as file metadata
WarningInvalidMetadata.ice:40: warning: the 'cpp:header-ext' metadata only accepts one argument but a list was provided
WarningInvalidMetadata.ice:40: warning: 'cpp:header-ext' metadata cannot be applied to sequences
WarningInvalidMetadata.ice:44: warning: ignoring duplicate metadata: 'deprecated:do not use this' does not match previously applied metadata of 'deprecated'
WarningInvalidMetadata.ice:44: warning: ignoring duplicate metadata: 'deprecated' has already been applied in this context
WarningInvalidMetadata.ice:48: warning: ignoring unknown metadata: 'unknown'
WarningInvalidMetadata.ice:48: warning: ignoring unknown metadata: 'cpp:unknown'
WarningInvalidMetadata.ice:48: warning: ignoring unknown metadata: 'bad:unknown'
Expand Down

0 comments on commit dfb4fc4

Please sign in to comment.