Source:
public Object visitContextDefinition(@NotNull cqlParser.ContextDefinitionContext ctx) { String modelIdentifier = parseString(ctx.modelIdentifier()); String unqualifiedIdentifier = parseString(ctx.identifier()); currentContext = modelIdentifier != null ? modelIdentifier + "." + unqualifiedIdentifier : unqualifiedIdentifier; if (!unqualifiedIdentifier.equals("Unspecified")) { ModelContext modelContext = libraryBuilder.resolveContextName(modelIdentifier, unqualifiedIdentifier); if (!implicitContextCreated) { if (libraryBuilder.hasUsings()) { ModelInfo modelInfo = modelIdentifier == null ? libraryBuilder.getModel(libraryInfo.getDefaultModelName()).getModelInfo() : libraryBuilder.getModel(modelIdentifier).getModelInfo(); String contextTypeName = modelContext.getName(); DataType contextType = libraryBuilder.resolveTypeName(modelInfo.getName(), contextTypeName); Retrieve contextRetrieve = of.createRetrieve().withDataType(libraryBuilder.dataTypeToQName(contextType)); track(contextRetrieve, ctx); contextRetrieve.setResultType(new ListType(contextType)); String contextClassIdentifier = ((ClassType) contextType).getIdentifier(); if (contextClassIdentifier != null) { contextRetrieve.setTemplateId(contextClassIdentifier); } implicitContextExpressionDef = of.createExpressionDef() .withName(unqualifiedIdentifier) .withContext(currentContext) .withExpression(of.createSingletonFrom().withOperand(contextRetrieve)); track(implicitContextExpressionDef, ctx); implicitContextExpressionDef.getExpression().setResultType(contextType); implicitContextExpressionDef.setResultType(contextType); libraryBuilder.addExpression(implicitContextExpressionDef); } else { implicitContextExpressionDef = of.createExpressionDef() .withName(unqualifiedIdentifier) .withContext(currentContext) .withExpression(of.createNull()); track(implicitContextExpressionDef, ctx); implicitContextExpressionDef.getExpression().setResultType(libraryBuilder.resolveTypeName("System", "Any")); implicitContextExpressionDef.setResultType(implicitContextExpressionDef.getExpression().getResultType()); libraryBuilder.addExpression(implicitContextExpressionDef); } implicitContextCreated = true; return currentContext; } } return currentContext; }

Target:
public Object visitContextDefinition(@NotNull cqlParser.ContextDefinitionContext ctx) { String modelIdentifier = parseString(ctx.modelIdentifier()); String unqualifiedIdentifier = parseString(ctx.identifier()); currentContext = modelIdentifier != null ? modelIdentifier + "." + unqualifiedIdentifier : unqualifiedIdentifier; if (!unqualifiedIdentifier.equals("Unfiltered")) { ModelContext modelContext = libraryBuilder.resolveContextName(modelIdentifier, unqualifiedIdentifier); if (!implicitContextCreated) { if (libraryBuilder.hasUsings()) { ModelInfo modelInfo = modelIdentifier == null ? libraryBuilder.getModel(libraryInfo.getDefaultModelName()).getModelInfo() : libraryBuilder.getModel(modelIdentifier).getModelInfo(); String contextTypeName = modelContext.getName(); DataType contextType = libraryBuilder.resolveTypeName(modelInfo.getName(), contextTypeName); Retrieve contextRetrieve = of.createRetrieve().withDataType(libraryBuilder.dataTypeToQName(contextType)); track(contextRetrieve, ctx); contextRetrieve.setResultType(new ListType(contextType)); String contextClassIdentifier = ((ClassType) contextType).getIdentifier(); if (contextClassIdentifier != null) { contextRetrieve.setTemplateId(contextClassIdentifier); } implicitContextExpressionDef = of.createExpressionDef() .withName(unqualifiedIdentifier) .withContext(currentContext) .withExpression(of.createSingletonFrom().withOperand(contextRetrieve)); track(implicitContextExpressionDef, ctx); implicitContextExpressionDef.getExpression().setResultType(contextType); implicitContextExpressionDef.setResultType(contextType); libraryBuilder.addExpression(implicitContextExpressionDef); } else { implicitContextExpressionDef = of.createExpressionDef() .withName(unqualifiedIdentifier) .withContext(currentContext) .withExpression(of.createNull()); track(implicitContextExpressionDef, ctx); implicitContextExpressionDef.getExpression().setResultType(libraryBuilder.resolveTypeName("System", "Any")); implicitContextExpressionDef.setResultType(implicitContextExpressionDef.getExpression().getResultType()); libraryBuilder.addExpression(implicitContextExpressionDef); } implicitContextCreated = true; return currentContext; } } return currentContext; }