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(BLangImportPackage importPkgNode) {
Name pkgAlias = names.fromIdNode(importPkgNode.alias);
if (symResolver.lookupSymbol(env, pkgAlias, SymTag.IMPORT) != symTable.notFoundSymbol) {
dlog.error(importPkgNode.pos, DiagnosticCode.REDECLARED_SYMBOL, pkgAlias);
return;
}
// TODO Clean this code up. Can we move the this to BLangPackageBuilder class
// Create import package symbol
Name orgName;
if (importPkgNode.orgName.value == null || importPkgNode.orgName.value.isEmpty()) {
// means it's in 'import <pkg-name>' style
orgName = Names.ANON_ORG;
} else {
// means it's in 'import <org-name>/<pkg-name>' style
orgName = names.fromIdNode(importPkgNode.orgName);
}
List<Name> nameComps = importPkgNode.pkgNameComps.stream().map(identifier -> names.fromIdNode(identifier)).collect(Collectors.toList());
String version = names.fromIdNode(importPkgNode.version).getValue().replaceAll("[^\\d.]", "");
PackageID pkgId = new PackageID(orgName, nameComps, new Name(version));
if (pkgId.name.getValue().startsWith(Names.BUILTIN_PACKAGE.value)) {
dlog.error(importPkgNode.pos, DiagnosticCode.PACKAGE_NOT_FOUND, importPkgNode.getQualifiedPackageName());
return;
}
// Attempt to load the imported package.
BLangPackage pkgNode = pkgLoader.loadPackage(pkgId, env.enclPkg.packageRepository);
if (pkgNode == null) {
dlog.error(importPkgNode.pos, DiagnosticCode.PACKAGE_NOT_FOUND, importPkgNode.getQualifiedPackageName());
return;
}
// This import package is not defined.
if (pkgNode.symbol == null) {
// Define import package now.
definePackage(pkgNode);
populateInitFunctionInvocation(importPkgNode, pkgNode.symbol);
}
// define the import package symbol in the current package scope
importPkgNode.symbol = pkgNode.symbol;
this.env.scope.define(pkgAlias, pkgNode.symbol);
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class SemanticAnalyzer method visit.
public void visit(BLangVariable varNode) {
int ownerSymTag = env.scope.owner.tag;
if ((ownerSymTag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
// If the variable is parameter then the variable symbol is already defined
if (varNode.symbol == null) {
symbolEnter.defineNode(varNode, env);
}
}
BType lhsType = varNode.symbol.type;
varNode.type = lhsType;
// Here we validate annotation attachments for package level variables.
varNode.annAttachments.forEach(a -> {
a.attachmentPoint = new BLangAnnotationAttachmentPoint(BLangAnnotationAttachmentPoint.AttachmentPoint.CONST);
a.accept(this);
});
// Here we validate document attachments for package level variables.
varNode.docAttachments.forEach(doc -> {
doc.accept(this);
});
// Analyze the init expression
BLangExpression rhsExpr = varNode.expr;
if (rhsExpr == null) {
return;
}
// Here we create a new symbol environment to catch self references by keep the current
// variable symbol in the symbol environment
// e.g. int a = x + a;
SymbolEnv varInitEnv = SymbolEnv.createVarInitEnv(varNode, env, varNode.symbol);
// It will we done during the init-function of the respective construct is visited.
if ((ownerSymTag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymTag & SymTag.SERVICE) == SymTag.SERVICE || (ownerSymTag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
return;
}
// Return if this not a safe assignment
if (!varNode.safeAssignment) {
typeChecker.checkExpr(rhsExpr, varInitEnv, Lists.of(lhsType));
return;
}
handleSafeAssignment(varNode.pos, lhsType, rhsExpr, varInitEnv);
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class Types method checkStructEquivalency.
public boolean checkStructEquivalency(BType rhsType, BType lhsType) {
if (rhsType.tag != TypeTags.STRUCT || lhsType.tag != TypeTags.STRUCT) {
return false;
}
// The public bit is on means, one is public, and the other one is private.
if (Symbols.isFlagOn(lhsType.tsymbol.flags ^ rhsType.tsymbol.flags, Flags.PUBLIC)) {
return false;
}
// If both structs are private, they should be in the same package.
if (Symbols.isPrivate(lhsType.tsymbol) && rhsType.tsymbol.pkgID != lhsType.tsymbol.pkgID) {
return false;
}
// RHS type should have at least all the fields as well attached functions of LHS type.
BStructType lhsStructType = (BStructType) lhsType;
BStructType rhsStructType = (BStructType) rhsType;
if (lhsStructType.fields.size() > rhsStructType.fields.size()) {
return false;
}
return Symbols.isPrivate(lhsType.tsymbol) && rhsType.tsymbol.pkgID == lhsType.tsymbol.pkgID ? checkEquivalencyOfTwoPrivateStructs(lhsStructType, rhsStructType) : checkEquivalencyOfPublicStructs(lhsStructType, rhsStructType);
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class CodeGenerator method getRegIndexInternal.
private RegIndex getRegIndexInternal(int typeTag, VariableIndex.Kind varIndexKind) {
int index;
switch(varIndexKind) {
case REG:
return new RegIndex(getNextIndex(typeTag, regIndexes), typeTag);
case PACKAGE:
index = getNextIndex(typeTag, pvIndexes);
break;
case FIELD:
index = getNextIndex(typeTag, fieldIndexes);
break;
default:
index = getNextIndex(typeTag, lvIndexes);
break;
}
RegIndex regIndex = new RegIndex(index, typeTag);
regIndex.isVarIndex = true;
return regIndex;
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class AnnotationDesugar method rewritePackageAnnotations.
protected void rewritePackageAnnotations(BLangPackage pkgNode) {
BLangFunction initFunction = pkgNode.initFunction;
// Remove last return statement. we will add it later. TODO : Fix this properly.
initFunction.body.stmts.remove(initFunction.body.stmts.size() - 1);
// This is the variable which store all package level annotations.
BLangVariable annotationMap = createGlobalAnnotationMapVar(pkgNode);
// handle Service Annotations.
for (BLangService service : pkgNode.services) {
generateAnnotations(service, service.name.value, initFunction, annotationMap);
for (BLangResource resource : service.resources) {
String key = service.name.value + DOT + resource.name.value;
generateAnnotations(resource, key, initFunction, annotationMap);
}
}
// Handle Function Annotations.
for (BLangFunction function : pkgNode.functions) {
generateAnnotations(function, function.symbol.name.value, initFunction, annotationMap);
}
// Handle Connector Annotations.
for (BLangConnector connector : pkgNode.connectors) {
generateAnnotations(connector, connector.name.value, initFunction, annotationMap);
for (BLangAction action : connector.actions) {
String key = connector.name.value + DOT + action.name.value;
generateAnnotations(connector, key, initFunction, annotationMap);
}
}
// Handle Struct Annotations.
for (BLangStruct struct : pkgNode.structs) {
generateAnnotations(struct, struct.name.value, initFunction, annotationMap);
for (BLangVariable field : struct.fields) {
String key = struct.name.value + DOT + field.name.value;
generateAnnotations(field, key, initFunction, annotationMap);
}
}
for (BLangEndpoint globalEndpoint : pkgNode.globalEndpoints) {
generateAnnotations(globalEndpoint, globalEndpoint.name.value, initFunction, annotationMap);
}
ASTBuilderUtil.createReturnStmt(pkgNode.pos, initFunction.body);
}
Aggregations