From a34dfc0906d5e3b09b5dc333e1e1d373b850a314 Mon Sep 17 00:00:00 2001 From: Mark Spruiell Date: Thu, 1 Feb 2018 10:42:02 -0800 Subject: ICE-8544 - Updates to packaging metadata --- cpp/src/Slice/JavaUtil.cpp | 119 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 95 insertions(+), 24 deletions(-) (limited to 'cpp/src/Slice/JavaUtil.cpp') diff --git a/cpp/src/Slice/JavaUtil.cpp b/cpp/src/Slice/JavaUtil.cpp index 3cc4a2e3967..0f506c1a391 100644 --- a/cpp/src/Slice/JavaUtil.cpp +++ b/cpp/src/Slice/JavaUtil.cpp @@ -431,6 +431,11 @@ private: result.push_back(s); continue; } + else if(s.substr(prefix.size(), pos - prefix.size()) == "package") + { + result.push_back(s); + continue; + } dc->warning(InvalidMetaData, cont->file(), cont->line(), "ignoring invalid metadata `" + s + "'"); } @@ -444,7 +449,8 @@ private: return result; } - StringList validateType(const SyntaxTreeBasePtr& p, const StringList& metaData, const string& file, const string& line) + StringList validateType(const SyntaxTreeBasePtr& p, const StringList& metaData, const string& file, + const string& line) { const UnitPtr unit = p->unit(); const DefinitionContextPtr dc = unit->findDefinitionContext(file); @@ -520,6 +526,18 @@ private: dc->warning(InvalidMetaData, file, line, "ignoring invalid metadata `" + *i + "'"); } } + else if(i->find("java:package:") == 0) + { + ModulePtr m = ModulePtr::dynamicCast(p); + if(m && UnitPtr::dynamicCast(m->container())) + { + newMetaData.push_back(*i); + } + else + { + dc->warning(InvalidMetaData, file, line, "ignoring invalid metadata `" + *i + "'"); + } + } else { newMetaData.push_back(*i); @@ -528,7 +546,8 @@ private: return newMetaData; } - StringList validateGetSet(const SyntaxTreeBasePtr& p, const StringList& metaData, const string& file, const string& line) + StringList validateGetSet(const SyntaxTreeBasePtr& p, const StringList& metaData, const string& file, + const string& line) { const UnitPtr unit = p->unit(); const DefinitionContextPtr dc= unit->findDefinitionContext(file); @@ -921,25 +940,51 @@ Slice::JavaCompatGenerator::convertScopedName(const string& scoped, const string string Slice::JavaCompatGenerator::getPackagePrefix(const ContainedPtr& cont) const { - UnitPtr unit = cont->container()->unit(); - string file = cont->file(); - assert(!file.empty()); - - map::const_iterator p = _filePackagePrefix.find(file); - if(p != _filePackagePrefix.end()) + // + // Traverse to the top-level module. + // + ModulePtr m; + ContainedPtr p = cont; + while(true) { - return p->second; + if(ModulePtr::dynamicCast(p)) + { + m = ModulePtr::dynamicCast(p); + } + + ContainerPtr c = p->container(); + p = ContainedPtr::dynamicCast(c); // This cast fails for Unit. + if(!p) + { + break; + } } + assert(m); + + // + // The java:package metadata can be defined as global metadata or applied to a top-level module. + // We check for the metadata at the top-level module first and then fall back to the global scope. + // static const string prefix = "java:package:"; - DefinitionContextPtr dc = unit->findDefinitionContext(file); - assert(dc); - string q = dc->findMetaData(prefix); + + string q; + if(!m->findMetaData(prefix, q)) + { + UnitPtr unit = cont->unit(); + string file = cont->file(); + assert(!file.empty()); + + DefinitionContextPtr dc = unit->findDefinitionContext(file); + assert(dc); + q = dc->findMetaData(prefix); + } + if(!q.empty()) { q = q.substr(prefix.size()); } - _filePackagePrefix[file] = q; + return q; } @@ -3375,25 +3420,51 @@ Slice::JavaGenerator::convertScopedName(const string& scoped, const string& pref string Slice::JavaGenerator::getPackagePrefix(const ContainedPtr& cont) const { - UnitPtr unit = cont->container()->unit(); - string file = cont->file(); - assert(!file.empty()); - - map::const_iterator p = _filePackagePrefix.find(file); - if(p != _filePackagePrefix.end()) + // + // Traverse to the top-level module. + // + ModulePtr m; + ContainedPtr p = cont; + while(true) { - return p->second; + if(ModulePtr::dynamicCast(p)) + { + m = ModulePtr::dynamicCast(p); + } + + ContainerPtr c = p->container(); + p = ContainedPtr::dynamicCast(c); // This cast fails for Unit. + if(!p) + { + break; + } } + assert(m); + + // + // The java:package metadata can be defined as global metadata or applied to a top-level module. + // We check for the metadata at the top-level module first and then fall back to the global scope. + // static const string prefix = "java:package:"; - DefinitionContextPtr dc = unit->findDefinitionContext(file); - assert(dc); - string q = dc->findMetaData(prefix); + + string q; + if(!m->findMetaData(prefix, q)) + { + UnitPtr unit = cont->unit(); + string file = cont->file(); + assert(!file.empty()); + + DefinitionContextPtr dc = unit->findDefinitionContext(file); + assert(dc); + q = dc->findMetaData(prefix); + } + if(!q.empty()) { q = q.substr(prefix.size()); } - _filePackagePrefix[file] = q; + return q; } -- cgit v1.2.3