public override LLVMRegister VisitMemberAssignmentFunctionDeclarationStatement([NotNull] ClepsParser.MemberAssignmentFunctionDeclarationStatementContext context) { bool isStatic = context.STATIC() != null; ClepsParser.FunctionParametersListContext parametersContext = context.assignmentFunctionDeclarationStatement().functionParametersList(); string functionName = context.assignmentFunctionDeclarationStatement().FunctionName.Text; return(VisitFunctionDeclarationBody(context, ClepsType.GetVoidType(), parametersContext, functionName, isStatic)); }
public override int VisitMemberAssignmentFunctionDeclarationStatement([NotNull] ClepsParser.MemberAssignmentFunctionDeclarationStatementContext context) { bool isStatic = context.STATIC() != null; var assignmentFunctionDeclarationStatement = context.assignmentFunctionDeclarationStatement(); string functionName = assignmentFunctionDeclarationStatement.FunctionName.Text; ClepsParser.FunctionParametersListContext parameterContext = assignmentFunctionDeclarationStatement.functionParametersList(); var ret = GenerateMemberFunction(context, isStatic, functionName, ClepsType.GetVoidType(), parameterContext); return(ret); }
private LLVMRegister VisitFunctionDeclarationBody ( Antlr4.Runtime.ParserRuleContext context, ClepsType clepsReturnType, ClepsParser.FunctionParametersListContext parametersContext, string functionName, bool isStatic ) { string className = String.Join(".", CurrentNamespaceAndClass); if (!ClassManager.DoesClassContainMember(className, functionName, isStatic)) { //if the member was not found in the loaded member stage, then this is probably due to an earlier parsing error, just stop processing this member return(null); } FunctionHierarchy.Add(functionName); string fullyQualifiedName = String.Join(".", CurrentNamespaceAndClass.Union(FunctionHierarchy).ToList()); LLVMValueRef currFunc = LLVM.GetNamedFunction(Module, fullyQualifiedName); LLVMBasicBlockRef basicBlock = LLVM.GetFirstBasicBlock(currFunc); LLVM.PositionBuilderAtEnd(Builder, basicBlock); VariableManager.AddBlock(); List <string> paramNames = parametersContext._FunctionParameterNames.Select(p => p.VariableName.Text).ToList(); List <ClepsType> clepsParameterTypes = parametersContext._FunctionParameterTypes.Select(t => ClepsType.GetBasicType(t)).ToList(); if (!isStatic) { ClepsType thisClassType = ClepsType.GetBasicType(className, 1); paramNames.Insert(0, "this"); clepsParameterTypes.Insert(0, thisClassType); } List <LLVMValueRef> paramValueRegisters = currFunc.GetParams().ToList(); paramNames.Zip(clepsParameterTypes, (ParamName, ParamType) => new { ParamName, ParamType }) .Zip(paramValueRegisters, (ParamNameAndType, ParamRegister) => new { ParamNameAndType.ParamName, ParamNameAndType.ParamType, ParamRegister }) .ToList() .ForEach(p => { LLVMValueRef functionParamPtr = LLVM.BuildAlloca(Builder, LLVM.TypeOf(p.ParamRegister), p.ParamName); LLVM.BuildStore(Builder, p.ParamRegister, functionParamPtr); VariableManager.AddLocalVariable(p.ParamName, p.ParamType, functionParamPtr); }); var ret = VisitChildren(context); VariableManager.RemoveBlock(); FunctionHierarchy.RemoveAt(FunctionHierarchy.Count - 1); return(ret); }
public override LLVMRegister VisitMemberFunctionDeclarationStatement([NotNull] ClepsParser.MemberFunctionDeclarationStatementContext context) { bool isStatic = context.STATIC() != null; ClepsParser.TypenameAndVoidContext returnTypeContext = context.functionDeclarationStatement().FunctionReturnType; ClepsType clepsReturnType = ClepsType.GetBasicOrVoidType(returnTypeContext); ClepsParser.FunctionParametersListContext parametersContext = context.functionDeclarationStatement().functionParametersList(); string functionName = context.functionDeclarationStatement().FunctionName.GetText(); return(VisitFunctionDeclarationBody(context, clepsReturnType, parametersContext, functionName, isStatic)); }
public override int VisitMemberOperatorFunctionDeclarationStatement([NotNull] ClepsParser.MemberOperatorFunctionDeclarationStatementContext context) { bool isStatic = context.STATIC() != null; var assignmentFunctionDeclarationStatement = context.operatorFunctionDeclarationStatment(); string functionName = assignmentFunctionDeclarationStatement.FunctionName.GetText(); ClepsParser.TypenameContext functionReturnContext = assignmentFunctionDeclarationStatement.FunctionReturnType; ClepsType clepsReturnType = ClepsType.GetBasicType(functionReturnContext); ClepsParser.FunctionParametersListContext parameterContext = assignmentFunctionDeclarationStatement.functionParametersList(); var ret = GenerateMemberFunction(context, isStatic, functionName, clepsReturnType, parameterContext); return(ret); }
private int GenerateMemberFunction(ParserRuleContext context, bool isStatic, string functionName, ClepsType clepsReturnType, ClepsParser.FunctionParametersListContext parameterContext) { string className = String.Join(".", CurrentNamespaceAndClass); FunctionHierarchy.Add(functionName); string fullFunctionName = String.Join(".", FunctionHierarchy); string fullyQualifiedName = String.Format("{0}.{1}", className, fullFunctionName); if (ClassManager.DoesClassContainMember(className, fullFunctionName)) { string errorMessage = String.Format("Class {0} has multiple definitions of member {1}", className, fullFunctionName); Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage)); //Don't process this member return(-1); } List <ClepsType> clepsParameterTypes = parameterContext._FunctionParameterTypes.Select(t => ClepsType.GetBasicType(t)).ToList(); if (!isStatic) { ClepsType currentClassPtrType = ClepsType.GetBasicType(className, new List <uint>() /* array dims */, 1 /* */); clepsParameterTypes.Insert(0, currentClassPtrType); } LLVMTypeRef?llvmReturnType = ClepsLLVMTypeConvertorInst.GetLLVMTypeOrNull(clepsReturnType); if (llvmReturnType == null) { string errorMessage = String.Format("Type {0} was not found", clepsReturnType.GetTypeName()); Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage)); //If the return type is not found, try to continue by assuming a void return. Compiler will still show an error clepsReturnType = ClepsType.GetVoidType(); llvmReturnType = ClepsLLVMTypeConvertorInst.GetLLVMTypeOrNull(clepsReturnType).Value; } LLVMTypeRef[] llvmParameterTypes = GetLLVMTypesArrFromValidClepsTypesList(context, clepsParameterTypes); LLVMTypeRef funcType = LLVM.FunctionType(llvmReturnType.Value, llvmParameterTypes, false); LLVMValueRef newFunc = LLVM.AddFunction(Module, fullyQualifiedName, funcType); LLVMBasicBlockRef blockRef = LLVM.AppendBasicBlockInContext(Context, newFunc, "entry"); LLVM.PositionBuilderAtEnd(Builder, blockRef); ClepsType clepsFunctionType = ClepsType.GetFunctionType(clepsReturnType, clepsParameterTypes); ClassManager.AddNewMember(className, fullFunctionName, isStatic, clepsFunctionType); FunctionHierarchy.RemoveAt(FunctionHierarchy.Count - 1); return(0); }