package jetbrains.mps.baseLanguage.textGen;
/*Generated by MPS */
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import java.util.List;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.text.rt.TextGenContext;
import jetbrains.mps.text.impl.TextGenSupport;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.core.behavior.IDeprecatable__BehaviorDescriptor;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.util.NameUtil;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.baseLanguage.behavior.Classifier__BehaviorDescriptor;
import org.jetbrains.mps.openapi.model.SReference;
import jetbrains.mps.smodel.SNodeUtil;
import jetbrains.mps.baseLanguage.tuples.runtime.Tuples;
import jetbrains.mps.smodel.DynamicReference;
import jetbrains.mps.baseLanguage.tuples.runtime.MultiTuple;
import org.jetbrains.mps.openapi.model.SModelReference;
import org.apache.log4j.Level;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations;
import org.jetbrains.mps.openapi.language.SReferenceLink;
public abstract class BaseLanguageTextGen {
private static final Logger LOG = LogManager.getLogger(BaseLanguageTextGen.class);
public static void typeParameters(List<SNode> types, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
if (ListSequence.fromList(types).isNotEmpty()) {
tgs.append("<");
{
Iterable<SNode> collection = types;
final SNode lastItem = Sequence.fromIterable(collection).last();
for (SNode item : collection) {
tgs.appendNode(item);
if (item != lastItem) {
tgs.append(", ");
}
}
}
tgs.append(">");
}
}
public static void arguments(SNode methodCall, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
tgs.append("(");
{
Iterable<SNode> collection = SLinkOperations.getChildren(methodCall, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11857355952L, 0xf8c78301aeL, "actualArgument"));
final SNode lastItem = Sequence.fromIterable(collection).last();
for (SNode item : collection) {
tgs.appendNode(item);
if (item != lastItem) {
tgs.append(", ");
}
}
}
tgs.append(")");
}
public static void newLine(boolean need, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
if (need) {
tgs.newLine();
}
}
public static void annotations(SNode annotable, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
for (SNode item : SLinkOperations.getChildren(annotable, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a6be947aL, 0x114a6beb0bdL, "annotation"))) {
tgs.appendNode(item);
}
if (SNodeOperations.isInstanceOf(annotable, MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x11d205fe38dL, "jetbrains.mps.lang.core.structure.IDeprecatable")) && (boolean) IDeprecatable__BehaviorDescriptor.isDeprecated_idhOwoPtR.invoke(SNodeOperations.cast(annotable, MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x11d205fe38dL, "jetbrains.mps.lang.core.structure.IDeprecatable")))) {
boolean containsDeprecated = false;
for (SNode annotationInstance : SLinkOperations.getChildren(annotable, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a6be947aL, 0x114a6beb0bdL, "annotation"))) {
if (SLinkOperations.getTarget(annotationInstance, MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a6b4ccabL, 0x114a6b85d40L, "annotation")) == SNodeOperations.getNode("6354ebe7-c22a-4a0f-ac54-50b52ab9b065/java:java.lang(JDK/)", "~Deprecated")) {
containsDeprecated = true;
break;
}
}
if (!(containsDeprecated)) {
SNode deprecated = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a6b4ccabL, "jetbrains.mps.baseLanguage.structure.AnnotationInstance"));
SLinkOperations.setTarget(deprecated, MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a6b4ccabL, 0x114a6b85d40L, "annotation"), SNodeOperations.getNode("6354ebe7-c22a-4a0f-ac54-50b52ab9b065/java:java.lang(JDK/)", "~Deprecated"));
tgs.appendNode(deprecated);
}
}
}
public static void visibility(SNode v, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
if ((v == null)) {
tgs.append("/*package*/ ");
} else {
tgs.appendNode(v);
}
}
public static void visibilityWithIndent(SNode v, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
tgs.indent();
BaseLanguageTextGen.visibility(v, ctx);
}
public static void internalClassifierName(SNode node, SNode contextNode, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
if ((node == null)) {
tgs.append("???");
tgs.reportError("classifier is null");
return;
}
BaseLanguageTextGen.appendClassName(BaseLanguageTextGen.getPackageName(node, ctx), NameUtil.longNameFromNamespaceAndShortName(BaseLanguageTextGen.getPackageName(node, ctx), SPropertyOperations.getString(node, MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, 0x11a134c900dL, "nestedName"))), contextNode, ctx);
}
public static void internalClassName(String packageName, String className, SNode contextNode, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
BaseLanguageTextGen.appendClassName(packageName, NameUtil.longNameFromNamespaceAndShortName(packageName, className), contextNode, ctx);
}
public static void variableDeclaration(SNode node, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
if (SPropertyOperations.getBoolean(node, MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0x111f9e9f00cL, "isFinal"))) {
tgs.append("final ");
}
tgs.appendNode(SLinkOperations.getTarget(node, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x450368d90ce15bc3L, 0x4ed4d318133c80ceL, "type")));
tgs.append(" ");
tgs.append(SPropertyOperations.getString(node, MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name")));
if ((SLinkOperations.getTarget(node, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0xf8c37f506eL, "initializer")) != null)) {
tgs.append(" = ");
tgs.appendNode(SLinkOperations.getTarget(node, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0xf8c37f506eL, "initializer")));
}
}
public static void fileHeader(SNode cls, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
tgs.getContextObject("ctx", ClassifierUnitContext.class).registerDependenciesOf(cls);
boolean topClassifier = !((boolean) Classifier__BehaviorDescriptor.isInner_idsWroEc0xXl.invoke(cls));
if (topClassifier) {
tgs.pushTextArea("HEADER");
tgs.append("package " + BaseLanguageTextGen.getPackageName(cls, ctx) + ";");
tgs.newLine();
tgs.newLine();
tgs.append("/*Generated by MPS */");
tgs.newLine();
tgs.newLine();
tgs.popTextArea();
tgs.pushTextArea("SEPARATOR");
tgs.newLine();
tgs.popTextArea();
}
}
public static void methodCall(SNode methodCall, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
BaseLanguageTextGen.methodTypeArguments(methodCall, ctx);
BaseLanguageTextGen.referenceToShortName(SNodeOperations.getReference(methodCall, MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11857355952L, 0xf8c78301adL, "baseMethodDeclaration")), ctx);
BaseLanguageTextGen.arguments(methodCall, ctx);
}
public static void methodTypeArguments(SNode methodCall, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
if (ListSequence.fromList(SLinkOperations.getChildren(methodCall, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11857355952L, 0x4500f31eb02a7788L, "typeArgument"))).isNotEmpty()) {
tgs.append("<");
{
Iterable<SNode> collection = SLinkOperations.getChildren(methodCall, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11857355952L, 0x4500f31eb02a7788L, "typeArgument"));
final SNode lastItem = Sequence.fromIterable(collection).last();
for (SNode item : collection) {
tgs.appendNode(item);
if (item != lastItem) {
tgs.append(",");
}
}
}
tgs.append(">");
}
}
public static void referenceToShortName(SReference reference, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
if (reference == null) {
tgs.reportError("null reference");
tgs.append("???");
return;
}
SNode targetNode = reference.getTargetNode();
if (targetNode == null) {
tgs.reportError(String.format("Unknown target for role %s", reference.getLink()));
tgs.append("???");
return;
}
String name = SNodeUtil.getResolveInfo(targetNode);
if (name == null) {
tgs.reportError(String.format("No resolve info for node %s", targetNode));
name = targetNode.getName();
}
if (name == null) {
tgs.append("???");
} else {
tgs.append(name);
}
}
public static void blClassifierRef(SReference classifierRef, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
if (classifierRef == null) {
tgs.reportError("null classifier ref");
return;
}
Tuples._2<String, String> packageAndShortName = BaseLanguageTextGen.getPackageAndShortName(classifierRef, ctx);
if (packageAndShortName == null) {
return;
}
String longName = NameUtil.longNameFromNamespaceAndShortName(packageAndShortName._0(), packageAndShortName._1());
BaseLanguageTextGen.appendClassName(packageAndShortName._0(), longName, classifierRef.getSourceNode(), ctx);
}
protected static Tuples._2<String, String> getPackageAndShortName(SReference classifierRef, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
SReference reference = classifierRef;
if (reference == null) {
tgs.reportError("null reference");
return null;
}
String shortName = "";
String packageName = "";
if (reference instanceof DynamicReference) {
shortName = ((jetbrains.mps.smodel.SReference) reference).getResolveInfo();
// hack, todo: remove!
if (shortName.startsWith("[")) {
return MultiTuple.<String,String>from(shortName.substring(1, shortName.lastIndexOf("]")).trim(), shortName.substring(shortName.lastIndexOf("]") + 1).trim());
} else {
// todo: remove!
final SModelReference modelReference = reference.getTargetSModelReference();
if (modelReference != null) {
packageName = modelReference.getName().getLongName();
if (LOG.isEnabledFor(Level.WARN)) {
LOG.warn("generating classifier reference with target model reference " + modelReference + " @ " + classifierRef);
}
} else {
int lastDot = shortName.lastIndexOf('.');
if (lastDot >= 0) {
packageName = shortName.substring(0, lastDot);
shortName = shortName.substring(lastDot + 1).replace('$', '.');
} else {
SModel sModel = classifierRef.getSourceNode().getModel();
packageName = (sModel != null ? sModel.getName().getLongName() : "");
}
}
return MultiTuple.<String,String>from(packageName, shortName);
}
} else {
SNode targetNode = jetbrains.mps.util.SNodeOperations.getTargetNodeSilently(reference);
if (targetNode == null) {
tgs.reportError("Target node is null for reference to classifier with role " + check_v9xqql_a0a0a0a0b0a4a0(SLinkOperations.getRefLink(classifierRef)) + "; resolve info " + SLinkOperations.getResolveInfo(classifierRef) + "; " + jetbrains.mps.util.SNodeOperations.getDebugText(classifierRef.getSourceNode()));
return null;
}
return MultiTuple.<String,String>from(SModelOperations.getModelName(SNodeOperations.getModel(targetNode)), (SNodeOperations.isInstanceOf(targetNode, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, "jetbrains.mps.baseLanguage.structure.Classifier")) ? SPropertyOperations.getString(SNodeOperations.cast(targetNode, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, "jetbrains.mps.baseLanguage.structure.Classifier")), MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, 0x11a134c900dL, "nestedName")) : jetbrains.mps.util.SNodeOperations.getResolveInfo(targetNode)));
}
}
protected static String getPackageName(SNode cls, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
return SModelOperations.getModelName(SNodeOperations.getModel(cls));
}
protected static String getClassName(String packageName, String fqName, SNode contextNode, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
if (fqName == null) {
tgs.reportError("class name is NULL");
return "???";
}
ImportEntry importEntry = tgs.getContextObject("ctx", ClassifierUnitContext.class).getClassifierRefText(packageName, fqName, contextNode);
if (importEntry.needsImport()) {
tgs.pushTextArea("IMPORTS");
tgs.append("import ");
tgs.append(importEntry.getImport());
tgs.append(";");
tgs.newLine();
tgs.popTextArea();
}
return importEntry.getName();
}
protected static void appendClassName(String packageName, String fqName, SNode contextNode, final TextGenContext ctx) {
final TextGenSupport tgs = new TextGenSupport(ctx);
tgs.append(BaseLanguageTextGen.getClassName(packageName, fqName, contextNode, ctx));
}
public static ClassifierUnitContext contextObjectInstance_ctx(SNode primaryInputNode) {
return new ClassifierUnitContext(primaryInputNode);
}
private static String check_v9xqql_a0a0a0a0b0a4a0(SReferenceLink checkedDotOperand) {
if (null != checkedDotOperand) {
return checkedDotOperand.getName();
}
return null;
}
}