use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.
the class TypeChecker method checkFunctionInvocationExpr.
private void checkFunctionInvocationExpr(BLangInvocation iExpr, BStructType structType) {
String funcName = iExpr.name.value;
Name uniqueFuncName = names.fromString(Symbols.getAttachedFuncSymbolName(structType.tsymbol.name.value, funcName));
BPackageSymbol packageSymbol = (BPackageSymbol) structType.tsymbol.owner;
BSymbol funcSymbol = symResolver.lookupMemberSymbol(iExpr.pos, packageSymbol.scope, this.env, uniqueFuncName, SymTag.FUNCTION);
if (funcSymbol == symTable.notFoundSymbol) {
// Check functions defined within the struct.
Name functionName = names.fromString(Symbols.getAttachedFuncSymbolName(iExpr.expr.symbol.type.tsymbol.name.value, iExpr.name.value));
funcSymbol = symResolver.resolveStructField(iExpr.pos, env, functionName, iExpr.expr.symbol.type.tsymbol);
if (funcSymbol == symTable.notFoundSymbol) {
// Check, any function pointer in struct field with given name.
funcSymbol = symResolver.resolveStructField(iExpr.pos, env, names.fromIdNode(iExpr.name), iExpr.expr.symbol.type.tsymbol);
if (funcSymbol == symTable.notFoundSymbol || funcSymbol.type.tag != TypeTags.INVOKABLE) {
dlog.error(iExpr.pos, DiagnosticCode.UNDEFINED_FUNCTION_IN_STRUCT, funcName, structType);
resultTypes = getListWithErrorTypes(expTypes.size());
return;
}
if ((funcSymbol.flags & Flags.ATTACHED) != Flags.ATTACHED) {
iExpr.functionPointerInvocation = true;
}
}
} else {
// Attached function found
// Check for the explicit initializer function invocation
BStructSymbol.BAttachedFunction initializerFunc = ((BStructSymbol) structType.tsymbol).initializerFunc;
if (initializerFunc != null && initializerFunc.funcName.value.equals(funcName)) {
dlog.error(iExpr.pos, DiagnosticCode.STRUCT_INITIALIZER_INVOKED, structType.tsymbol.toString());
}
}
iExpr.symbol = funcSymbol;
checkInvocationParamAndReturnType(iExpr);
}
use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.
the class Types method checkEquivalencyOfPublicStructs.
private boolean checkEquivalencyOfPublicStructs(BStructType lhsType, BStructType rhsType) {
int fieldCounter = 0;
for (; fieldCounter < lhsType.fields.size(); fieldCounter++) {
BStructField lhsField = lhsType.fields.get(fieldCounter);
BStructField rhsField = rhsType.fields.get(fieldCounter);
if (Symbols.isPrivate(lhsField.symbol) || Symbols.isPrivate(rhsField.symbol)) {
return false;
}
if (lhsField.name.equals(rhsField.name) && isSameType(rhsField.type, lhsField.type)) {
continue;
}
return false;
}
// Check the rest of the fields in RHS type
for (; fieldCounter < rhsType.fields.size(); fieldCounter++) {
if (Symbols.isPrivate(rhsType.fields.get(fieldCounter).symbol)) {
return false;
}
}
BStructSymbol lhsStructSymbol = (BStructSymbol) lhsType.tsymbol;
List<BAttachedFunction> lhsFuncs = lhsStructSymbol.attachedFuncs;
List<BAttachedFunction> rhsFuncs = ((BStructSymbol) rhsType.tsymbol).attachedFuncs;
int lhsAttachedFuncCount = lhsStructSymbol.initializerFunc != null ? lhsFuncs.size() - 1 : lhsFuncs.size();
if (lhsAttachedFuncCount > rhsFuncs.size()) {
return false;
}
for (BAttachedFunction lhsFunc : lhsFuncs) {
if (lhsFunc == lhsStructSymbol.initializerFunc || lhsFunc == lhsStructSymbol.defaultsValuesInitFunc) {
continue;
}
if (Symbols.isPrivate(lhsFunc.symbol)) {
return false;
}
BAttachedFunction rhsFunc = getMatchingInvokableType(rhsFuncs, lhsFunc);
if (rhsFunc == null || Symbols.isPrivate(rhsFunc.symbol)) {
return false;
}
}
// Check for private attached function of the RHS type
for (BAttachedFunction rhsFunc : rhsFuncs) {
if (Symbols.isPrivate(rhsFunc.symbol)) {
return false;
}
}
return true;
}
use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.
the class CodeGenerator method createServiceInfoEntry.
private void createServiceInfoEntry(BLangService serviceNode) {
// Add service name as an UTFCPEntry to the constant pool
int serviceNameCPIndex = addUTF8CPEntry(currentPkgInfo, serviceNode.name.value);
// Create service info
if (serviceNode.endpointType != null) {
String endPointQName = serviceNode.endpointType.tsymbol.toString();
// TODO: bvmAlias needed?
int epNameCPIndex = addUTF8CPEntry(currentPkgInfo, endPointQName);
ServiceInfo serviceInfo = new ServiceInfo(currentPackageRefCPIndex, serviceNameCPIndex, serviceNode.symbol.flags, epNameCPIndex);
// Add service level variables
int localVarAttNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE.value());
LocalVariableAttributeInfo localVarAttributeInfo = new LocalVariableAttributeInfo(localVarAttNameIndex);
serviceNode.vars.forEach(var -> visitVarSymbol(var.var.symbol, pvIndexes, localVarAttributeInfo));
serviceInfo.addAttributeInfo(AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE, localVarAttributeInfo);
// Create the init function info
BLangFunction serviceInitFunction = (BLangFunction) serviceNode.getInitFunction();
createFunctionInfoEntry(serviceInitFunction);
serviceInfo.initFuncInfo = currentPkgInfo.functionInfoMap.get(serviceInitFunction.name.toString());
currentPkgInfo.addServiceInfo(serviceNode.name.value, serviceInfo);
// Create resource info entries for all resources
serviceNode.resources.forEach(res -> createResourceInfoEntry(res, serviceInfo));
}
}
use of org.wso2.carbon.apimgt.core.models.Function 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);
}
use of org.wso2.carbon.apimgt.core.models.Function in project ballerina by ballerina-lang.
the class CodeGenerator method createFunctionInfoEntry.
/**
* Creates a {@code FunctionInfo} from the given function node in AST.
*
* @param funcNode function node in AST
*/
private void createFunctionInfoEntry(BLangFunction funcNode) {
BInvokableSymbol funcSymbol = funcNode.symbol;
BInvokableType funcType = (BInvokableType) funcSymbol.type;
// Add function name as an UTFCPEntry to the constant pool
int funcNameCPIndex = this.addUTF8CPEntry(currentPkgInfo, funcNode.name.value);
FunctionInfo funcInfo = new FunctionInfo(currentPackageRefCPIndex, funcNameCPIndex);
funcInfo.paramTypes = funcType.paramTypes.toArray(new BType[0]);
funcInfo.retParamTypes = funcType.retTypes.toArray(new BType[0]);
funcInfo.signatureCPIndex = addUTF8CPEntry(this.currentPkgInfo, generateFunctionSig(funcInfo.paramTypes, funcInfo.retParamTypes));
funcInfo.flags = funcSymbol.flags;
if (funcNode.receiver != null) {
funcInfo.attachedToTypeCPIndex = getTypeCPIndex(funcNode.receiver.type).value;
}
this.addWorkerInfoEntries(funcInfo, funcNode.getWorkers());
// Add parameter default value info
addParameterDefaultValues(funcNode, funcInfo);
this.currentPkgInfo.functionInfoMap.put(funcSymbol.name.value, funcInfo);
}
Aggregations