use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangSimpleVarRef varRefExpr) {
BLangSimpleVarRef genVarRefExpr = varRefExpr;
// XML qualified name reference. e.g: ns0:foo
if (varRefExpr.pkgSymbol != null && varRefExpr.pkgSymbol.tag == SymTag.XMLNS) {
BLangXMLQName qnameExpr = new BLangXMLQName(varRefExpr.variableName);
qnameExpr.nsSymbol = (BXMLNSSymbol) varRefExpr.pkgSymbol;
qnameExpr.localname = varRefExpr.variableName;
qnameExpr.prefix = varRefExpr.pkgAlias;
qnameExpr.namespaceURI = qnameExpr.nsSymbol.namespaceURI;
qnameExpr.isUsedInXML = false;
qnameExpr.pos = varRefExpr.pos;
qnameExpr.type = symTable.stringType;
result = qnameExpr;
return;
}
BSymbol ownerSymbol = varRefExpr.symbol.owner;
if ((varRefExpr.symbol.tag & SymTag.FUNCTION) == SymTag.FUNCTION && varRefExpr.symbol.type.tag == TypeTags.INVOKABLE) {
genVarRefExpr = new BLangFunctionVarRef(varRefExpr.symbol);
} else if ((ownerSymbol.tag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
// Local variable in a function/resource/action/worker
genVarRefExpr = new BLangLocalVarRef(varRefExpr.symbol);
} else if ((ownerSymbol.tag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
// Field variable in a receiver
genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
} else if ((ownerSymbol.tag & SymTag.STRUCT) == SymTag.STRUCT) {
genVarRefExpr = new BLangFieldVarRef(varRefExpr.symbol);
} else if ((ownerSymbol.tag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymbol.tag & SymTag.SERVICE) == SymTag.SERVICE) {
// Package variable | service variable
// We consider both of them as package level variables
genVarRefExpr = new BLangPackageVarRef(varRefExpr.symbol);
// Only locking service level and package level variables
if (!enclLocks.isEmpty()) {
enclLocks.peek().addLockVariable(varRefExpr.symbol);
}
}
genVarRefExpr.type = varRefExpr.type;
result = genVarRefExpr;
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class Desugar method visit.
// visitors
@Override
public void visit(BLangPackage pkgNode) {
if (pkgNode.completedPhases.contains(CompilerPhase.DESUGAR)) {
result = pkgNode;
return;
}
SymbolEnv env = this.symTable.pkgEnvMap.get(pkgNode.symbol);
// Adding object functions to package level.
pkgNode.objects.forEach(o -> {
o.functions.forEach(f -> {
pkgNode.functions.add(f);
pkgNode.topLevelNodes.add(f);
});
});
// Rewriting Object to struct
pkgNode.objects.forEach(o -> pkgNode.structs.add(rewriteObjectToStruct(o, env)));
pkgNode.structs = rewrite(pkgNode.structs, env);
// Adding struct init functions to package level.
pkgNode.structs.forEach(struct -> {
pkgNode.functions.add(struct.initFunction);
pkgNode.topLevelNodes.add(struct.initFunction);
});
pkgNode.imports = rewrite(pkgNode.imports, env);
pkgNode.xmlnsList = rewrite(pkgNode.xmlnsList, env);
pkgNode.globalVars = rewrite(pkgNode.globalVars, env);
pkgNode.globalEndpoints = rewrite(pkgNode.globalEndpoints, env);
pkgNode.globalEndpoints.forEach(endpoint -> endpointDesugar.defineGlobalEndpoint(endpoint, env));
annotationDesugar.rewritePackageAnnotations(pkgNode);
endpointDesugar.rewriteAllEndpointsInPkg(pkgNode, env);
endpointDesugar.rewriteServiceBoundToEndpointInPkg(pkgNode, env);
pkgNode.transformers = rewrite(pkgNode.transformers, env);
pkgNode.functions = rewrite(pkgNode.functions, env);
pkgNode.connectors = rewrite(pkgNode.connectors, env);
pkgNode.services = rewrite(pkgNode.services, env);
pkgNode.initFunction = rewrite(pkgNode.initFunction, env);
pkgNode.startFunction = rewrite(pkgNode.startFunction, env);
pkgNode.stopFunction = rewrite(pkgNode.stopFunction, env);
pkgNode.completedPhases.add(CompilerPhase.DESUGAR);
result = pkgNode;
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class BLangPackageBuilder method addAnonStructType.
public void addAnonStructType(DiagnosticPos pos, Set<Whitespace> ws) {
// Generate a name for the anonymous struct
String genName = anonymousModelHelper.getNextAnonymousStructKey(pos.src.pkgID);
IdentifierNode anonStructGenName = createIdentifier(genName);
// Create an anonymous struct and add it to the list of structs in the current package.
BLangStruct structNode = populateStructNode(pos, ws, anonStructGenName, true);
this.compUnit.addTopLevelNode(structNode);
addType(createUserDefinedType(pos, ws, (BLangIdentifier) TreeBuilder.createIdentifierNode(), structNode.name));
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class BinaryFileWriter method writeLibraryPackage.
public void writeLibraryPackage(BLangPackage packageNode) {
String fileName = getOutputFileName(packageNode, BLANG_COMPILED_PACKAGE_FILE_SUFFIX);
CompiledBinaryFile.PackageFile packageFile = this.codeGenerator.generateBALO(packageNode);
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
try {
PackageFileWriter.writePackage(packageFile, byteArrayOS);
} catch (IOException e) {
throw new BLangCompilerException("error writing package file '" + fileName + "'", e);
}
this.sourceDirectory.saveCompiledPackage(new ByteArrayInputStream(byteArrayOS.toByteArray()), fileName);
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class CompilerDriver method compile.
// Private methods
private BLangPackage compile(BLangPackage bLangPackage) {
BLangPackage pkgNode = bLangPackage;
// "ballerina/built-in" packages is only the pre-known package by the Ballerina compiler. So load it first.
BLangPackage builtInPackage = loadBuiltInPackage();
if (this.stopCompilation(pkgNode, CompilerPhase.DEFINE)) {
return pkgNode;
}
pkgNode = define(pkgNode);
if (this.stopCompilation(pkgNode, CompilerPhase.TYPE_CHECK)) {
return pkgNode;
}
pkgNode = typeCheck(pkgNode);
if (this.stopCompilation(pkgNode, CompilerPhase.CODE_ANALYZE)) {
return pkgNode;
}
pkgNode = codeAnalyze(pkgNode);
if (this.stopCompilation(pkgNode, CompilerPhase.TAINT_ANALYZE)) {
return pkgNode;
}
pkgNode = taintAnalyze(pkgNode);
if (this.stopCompilation(pkgNode, CompilerPhase.COMPILER_PLUGIN)) {
return pkgNode;
}
pkgNode = annotationProcess(pkgNode);
if (this.stopCompilation(pkgNode, CompilerPhase.DESUGAR)) {
return pkgNode;
}
// TODO : Improve this.
desugar(builtInPackage);
return desugar(pkgNode);
}
Aggregations