use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class SymbolEnter method visit.
// Visitor methods
@Override
public void visit(BLangPackage pkgNode) {
if (pkgNode.completedPhases.contains(CompilerPhase.DEFINE)) {
return;
}
// Create PackageSymbol.
BPackageSymbol pSymbol = createPackageSymbol(pkgNode);
SymbolEnv builtinEnv = this.symTable.pkgEnvMap.get(symTable.builtInPackageSymbol);
SymbolEnv pkgEnv = SymbolEnv.createPkgEnv(pkgNode, pSymbol.scope, builtinEnv);
this.symTable.pkgEnvMap.put(pSymbol, pkgEnv);
createPackageInitFunctions(pkgNode);
// visit the package node recursively and define all package level symbols.
// And maintain a list of created package symbols.
pkgNode.imports.forEach(importNode -> defineNode(importNode, pkgEnv));
// Define struct nodes.
pkgNode.enums.forEach(enumNode -> defineNode(enumNode, pkgEnv));
// Define struct nodes.
pkgNode.structs.forEach(struct -> defineNode(struct, pkgEnv));
// Define object nodes
pkgNode.objects.forEach(object -> defineNode(object, pkgEnv));
// Define connector nodes.
pkgNode.connectors.forEach(con -> defineNode(con, pkgEnv));
// Define connector params and type.
defineConnectorParams(pkgNode.connectors, pkgEnv);
// Define transformer nodes.
pkgNode.transformers.forEach(tansformer -> defineNode(tansformer, pkgEnv));
// Define service and resource nodes.
pkgNode.services.forEach(service -> defineNode(service, pkgEnv));
// Define struct field nodes.
defineStructFields(pkgNode.structs, pkgEnv);
// Define object field nodes.
defineObjectFields(pkgNode.objects, pkgEnv);
// Define connector action nodes.
defineConnectorMembers(pkgNode.connectors, pkgEnv);
// Define object functions
defineObjectMembers(pkgNode.objects, pkgEnv);
// Define function nodes.
pkgNode.functions.forEach(func -> defineNode(func, pkgEnv));
// Define transformer params
defineTransformerMembers(pkgNode.transformers, pkgEnv);
// Define service resource nodes.
defineServiceMembers(pkgNode.services, pkgEnv);
// Define annotation nodes.
pkgNode.annotations.forEach(annot -> defineNode(annot, pkgEnv));
resolveAnnotationAttributeTypes(pkgNode.annotations, pkgEnv);
pkgNode.globalVars.forEach(var -> defineNode(var, pkgEnv));
pkgNode.globalEndpoints.forEach(ep -> defineNode(ep, pkgEnv));
definePackageInitFunctions(pkgNode, pkgEnv);
pkgNode.completedPhases.add(CompilerPhase.DEFINE);
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class SymbolEnter method visit.
@Override
public void visit(BLangXMLNS xmlnsNode) {
String nsURI = (String) ((BLangLiteral) xmlnsNode.namespaceURI).value;
if (xmlnsNode.prefix.value != null && nsURI.isEmpty()) {
dlog.error(xmlnsNode.pos, DiagnosticCode.INVALID_NAMESPACE_DECLARATION, xmlnsNode.prefix);
}
// set the prefix of the default namespace
if (xmlnsNode.prefix.value == null) {
xmlnsNode.prefix.value = XMLConstants.DEFAULT_NS_PREFIX;
}
BXMLNSSymbol xmlnsSymbol = Symbols.createXMLNSSymbol(names.fromIdNode(xmlnsNode.prefix), nsURI, env.enclPkg.symbol.pkgID, env.scope.owner);
xmlnsNode.symbol = xmlnsSymbol;
// First check for package-imports with the same alias.
// Here we do not check for owner equality, since package import is always at the package
// level, but the namespace declaration can be at any level.
BSymbol foundSym = symResolver.lookupSymbol(env, xmlnsSymbol.name, SymTag.PACKAGE);
if (foundSym != symTable.notFoundSymbol) {
dlog.error(xmlnsNode.pos, DiagnosticCode.REDECLARED_SYMBOL, xmlnsSymbol.name);
return;
}
// Define it in the enclosing scope. Here we check for the owner equality,
// to support overriding of namespace declarations defined at package level.
defineSymbol(xmlnsNode.pos, xmlnsSymbol);
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class SymbolResolver method visit.
public void visit(BLangUserDefinedType userDefinedTypeNode) {
// 1) Resolve the package scope using the package alias.
// If the package alias is not empty or null, then find the package scope,
// if not use the current package scope.
// 2) lookup the typename in the package scope returned from step 1.
// 3) If the symbol is not found, then lookup in the root scope. e.g. for types such as 'error'
BSymbol pkgSymbol = resolvePkgSymbol(userDefinedTypeNode.pos, this.env, names.fromIdNode(userDefinedTypeNode.pkgAlias));
if (pkgSymbol == symTable.notFoundSymbol) {
resultType = symTable.errType;
return;
}
Name typeName = names.fromIdNode(userDefinedTypeNode.typeName);
BSymbol symbol = symTable.notFoundSymbol;
// Only valued types and ANNOTATION type allowed.
if (env.scope.owner.tag == SymTag.ANNOTATION) {
symbol = lookupMemberSymbol(userDefinedTypeNode.pos, pkgSymbol.scope, this.env, typeName, SymTag.ANNOTATION);
}
// 3) Lookup the current package scope.
if (symbol == symTable.notFoundSymbol) {
symbol = lookupMemberSymbol(userDefinedTypeNode.pos, pkgSymbol.scope, this.env, typeName, SymTag.VARIABLE_NAME);
}
if (symbol == symTable.notFoundSymbol) {
// 4) Lookup the root scope for types such as 'error'
symbol = lookupMemberSymbol(userDefinedTypeNode.pos, symTable.rootScope, this.env, typeName, SymTag.VARIABLE_NAME);
}
if (symbol == symTable.notFoundSymbol) {
dlog.error(userDefinedTypeNode.pos, diagCode, typeName);
resultType = symTable.errType;
return;
}
if (symbol.kind == SymbolKind.CONNECTOR) {
userDefinedTypeNode.flagSet = EnumSet.of(Flag.CONNECTOR);
}
resultType = symbol.type;
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class Parser method generateCompilationUnit.
private CompilationUnitNode generateCompilationUnit(PackageSourceEntry sourceEntry) {
try {
int prevErrCount = dlog.errorCount;
BDiagnosticSource diagnosticSrc = getDiagnosticSource(sourceEntry);
String entryName = sourceEntry.getEntryName();
BLangCompilationUnit compUnit = (BLangCompilationUnit) TreeBuilder.createCompilationUnit();
compUnit.setName(sourceEntry.getEntryName());
compUnit.pos = new DiagnosticPos(diagnosticSrc, 1, 1, 1, 1);
ANTLRInputStream ais = new ANTLRInputStream(new ByteArrayInputStream(sourceEntry.getCode()));
ais.name = entryName;
BallerinaLexer lexer = new BallerinaLexer(ais);
lexer.removeErrorListeners();
lexer.addErrorListener(new BallerinaParserErrorListener(context, diagnosticSrc));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
BallerinaParser parser = new BallerinaParser(tokenStream);
parser.setErrorHandler(getErrorStrategy(diagnosticSrc));
parser.addParseListener(newListener(tokenStream, compUnit, diagnosticSrc));
parser.compilationUnit();
return compUnit;
} catch (IOException e) {
throw new RuntimeException("Error in populating package model: " + e.getMessage(), e);
}
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangPackage pkgNode) {
if (pkgNode.completedPhases.contains(CompilerPhase.CODE_GEN)) {
if (!buildCompiledPackage) {
programFile.packageInfoMap.put(pkgNode.symbol.pkgID.bvmAlias(), pkgNode.symbol.packageInfo);
}
return;
}
// TODO Improve this design without if/else
PackageInfo packageInfo = new PackageInfo();
pkgNode.symbol.packageInfo = packageInfo;
if (buildCompiledPackage) {
// Generating the BALO
pkgNode.imports.forEach(impPkgNode -> {
int impPkgNameCPIndex = addUTF8CPEntry(packageInfo, impPkgNode.symbol.name.value);
// TODO Improve the import package version once it is available
int impPkgVersionCPIndex = addUTF8CPEntry(packageInfo, PackageID.DEFAULT.version.value);
ImportPackageInfo importPkgInfo = new ImportPackageInfo(impPkgNameCPIndex, impPkgVersionCPIndex);
packageInfo.importPkgInfoSet.add(importPkgInfo);
packageFile.packageInfo = packageInfo;
});
} else {
// Generating a BALX
// first visit all the imports
pkgNode.imports.forEach(impPkgNode -> genNode(impPkgNode, this.env));
// TODO We need to create identifier for both name and the version
programFile.packageInfoMap.put(pkgNode.symbol.pkgID.bvmAlias(), packageInfo);
}
// Add the current package to the program file
BPackageSymbol pkgSymbol = pkgNode.symbol;
currentPkgID = pkgSymbol.pkgID;
currentPkgInfo = packageInfo;
currentPkgInfo.nameCPIndex = addUTF8CPEntry(currentPkgInfo, currentPkgID.bvmAlias());
currentPkgInfo.versionCPIndex = addUTF8CPEntry(currentPkgInfo, currentPkgID.version.value);
// Insert the package reference to the constant pool of the current package
currentPackageRefCPIndex = addPackageRefCPEntry(currentPkgInfo, currentPkgID);
// This attribute keep track of line numbers
int lineNoAttrNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.LINE_NUMBER_TABLE_ATTRIBUTE.value());
lineNoAttrInfo = new LineNumberTableAttributeInfo(lineNoAttrNameIndex);
// This attribute keep package-level variable information
int pkgVarAttrNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE.value());
currentPkgInfo.addAttributeInfo(AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE, new LocalVariableAttributeInfo(pkgVarAttrNameIndex));
pkgNode.globalVars.forEach(this::createPackageVarInfo);
pkgNode.structs.forEach(this::createStructInfoEntry);
pkgNode.enums.forEach(this::createEnumInfoEntry);
pkgNode.connectors.forEach(this::createConnectorInfoEntry);
pkgNode.functions.forEach(this::createFunctionInfoEntry);
pkgNode.services.forEach(this::createServiceInfoEntry);
pkgNode.functions.forEach(this::createFunctionInfoEntry);
pkgNode.transformers.forEach(this::createTransformerInfoEntry);
// Visit package builtin function
visitBuiltinFunctions(pkgNode.initFunction);
visitBuiltinFunctions(pkgNode.startFunction);
visitBuiltinFunctions(pkgNode.stopFunction);
pkgNode.topLevelNodes.stream().filter(pkgLevelNode -> pkgLevelNode.getKind() != NodeKind.VARIABLE && pkgLevelNode.getKind() != NodeKind.XMLNS).forEach(pkgLevelNode -> genNode((BLangNode) pkgLevelNode, this.env));
currentPkgInfo.addAttributeInfo(AttributeInfo.Kind.LINE_NUMBER_TABLE_ATTRIBUTE, lineNoAttrInfo);
currentPackageRefCPIndex = -1;
currentPkgID = null;
pkgNode.completedPhases.add(CompilerPhase.CODE_GEN);
}
Aggregations