stevedlawrence commented on a change in pull request #326: Daffodil 2280
cleanup - removes backpointers and factory patterns no longer needed
URL: https://github.com/apache/incubator-daffodil/pull/326#discussion_r393659312
##########
File path: daffodil-core/src/main/scala/org/apache/daffodil/dsom/Nesting.scala
##########
@@ -28,117 +29,80 @@ import org.apache.daffodil.exceptions.Assert
*/
case class EnclosingComponentDef(encloser: SchemaComponent, lexicalPosition:
Int)
-trait NestingMixin {
+trait NestingLexicalMixin { self: SchemaComponent =>
/** The lexically enclosing schema component */
def optLexicalParent: Option[SchemaComponent]
- /**
- * Define this for schema components that have back-references to ref
- * objects.
- * So group def to group ref, globalelementdecl to element ref,
- * type to element, base type to derived type.
- *
- * Not for format annotations however. We don't backpoint those to
- * other format annotations that ref them.
- */
- //
- // Uncomment this to chase down these usages and revise them.
- //
- //
- //@deprecated("2019-06-03", "Use enclosingComponentDefs method, and deal
with sharing.")
- protected def enclosingComponentDef: Option[SchemaComponent]
-
- protected def enclosingComponentDefs: Seq[EnclosingComponentDef]
-
def shortSchemaComponentDesignator: String
- /**
- * The enclosing component, and follows back-references
- * from types to their elements, from globalElementDef to
- * elementRefs, from simpleType defs to derived simpletype defs,
- * from global group defs to group refs
- *
- * Note: the enclosing component of a global element or global group
- * referenced from a element ref or group ref, is NOT the ref object, but
the
- * component that contains the ref object
- */
- //
- // Uncomment this to chase down these usages and revise them.
- //
- //
- //@deprecated("2019-06-03", "Rewrite to use lexicalParent or
enclosingComponents methods, and deal with sharing.")
- final lazy val enclosingComponent: Option[SchemaComponent] =
enclosingComponentDef
-
- final lazy val enclosingComponents: Seq[EnclosingComponentDef] = {
- val res = enclosingComponentDefs
- // System.err.println("enclosingComponents: Component " +
this.shortSchemaComponentDesignator + (
- // if (res.isEmpty) " has no enclosing component."
- // else " has enclosing components " + res.map {
_.encloser.shortSchemaComponentDesignator }.mkString(", ")))
- res
- }
-}
-
-/**
- * Mixin for all schema factories and schema components with no
- * backpointers, just a lexical parent. This means all the non-global
- * schema components.
- */
-trait NestingLexicalMixin
- extends NestingMixin { self: SchemaComponent =>
+ private def ue(sc: SchemaComponent) =
+ Assert.usageError("Not to be called on " + sc)
- //@deprecated("2019-06-03", "Use enclosingComponentDefs method, and deal
with sharing.")
- override protected lazy val enclosingComponentDef = optLexicalParent
-
- final override protected lazy val enclosingComponentDefs:
Seq[EnclosingComponentDef] =
- optLexicalParent.map {
- lp =>
- val pos = self match {
- case t: Term => t.position
- case _ => 1
- }
- EnclosingComponentDef(lp, pos)
- }.toSeq
-}
-
-/**
- * Mixin for all global schema components
- */
-trait NestingTraversesToReferenceMixin
- extends NestingMixin { self: SchemaComponent =>
-
- def factory: SchemaComponentFactory
-
- //@deprecated("2019-06-03", "Use referringComponents method, and deal with
sharing.")
- def referringComponent: Option[SchemaComponent]
-
- //@deprecated("2019-06-03", "Use enclosingComponentDefs method, and deal
with sharing.")
- final override protected lazy val enclosingComponentDef:
Option[SchemaComponent] = LV('enclosingComponentDef) {
- Assert.invariant(optLexicalParent.isDefined &&
- optLexicalParent.get.isInstanceOf[SchemaDocument]) // global things have
schema documents as their parents.
- referringComponent
- }.value
-
- /**
- * Enables compilation to know all the points of use of a global
- * component.
- */
- lazy val referringComponents: Seq[(String, Seq[RefSpec])] = {
- schemaSet.root.refMap.get(this.factory) match {
- case None => Seq()
- case Some(seq) => seq
+ final protected lazy val enclosingComponents: Seq[EnclosingComponentDef] = {
+ this match {
+ case sd: SchemaDocument => ue(sd)
+ case ss: SchemaSet => ue(ss)
+ case s: Schema => ue(s)
+ case pf: ProcessorFactory => ue(pf)
+ case _ => // ok
}
+ val ecs =
+ optLexicalParent.toSeq.flatMap {
+ lp =>
+ val pos = this match {
+ case t: Term => t.position
+ case _ => 1
+ }
+ val ecs =
+ lp match {
+ case sd: SchemaDocument => {
+ // enclosing lexical component is schema document, so
+ // we need to see what references this global component
+ this match {
+ case r: Root => Nil
+ case gedecl: GlobalElementDecl if
(schemaSet.root.referencedElement eq gedecl) => {
+ // this is the global root element's declaration
+ // in this case the enclosing component is the root, which
is a
+ // special kind of element reference.
+ Seq(EnclosingComponentDef(schemaSet.root, 1))
+ }
+ case gc: GlobalComponent => {
+ val optRefList = schemaSet.root.refMap.get(gc)
+ val refList =
+ optRefList.toSeq.flatMap {
+ pairList =>
+ pairList.flatMap {
+ pair =>
+ pair match {
+ case (scid, seqRefSpec) => seqRefSpec
+ }
+ }
+ }
+ val ecs = refList.map { refSpec =>
+ val frm = refSpec.from
+ frm match {
+ case er: ElementRef => // ok
+ case gr: GroupRef => // ok
+ case ed: ElementDeclMixin if
(ed.namedTypeQName.isDefined) => // ok
+ case _ => Assert.invariantFailed("referring component
is not a ref object: " + frm)
+ }
+ EnclosingComponentDef(refSpec.from, pos)
+ }
+ ecs
+ }
+ case es: DFDLEscapeScheme => Nil
+ case f: DFDLFormat => Nil
+ case p: DFDLProperty => Nil
+ case _ => Assert.invariantFailed("non-global component has
schema document as lexical parent: " + this)
+ }
+ }
+ case ec => {
+ Seq(EnclosingComponentDef(ec, pos))
+ }
+ }
+ ecs
+ }
+ ecs
Review comment:
Could you maybe add some extra comments to this section? It's pretty deeply
nested with lots of mapping and the same variable name use within different
scopes. It's kindof hard to follow. I think some extra comments make help make
it more clear what's going on.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services