Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////// Function Statement Implementations ///////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        #region Function Statement Implementations

        // Note - the return of statements are not really used. It is more used in the expression returns, which is in the next section

        public override LLVMRegister VisitFunctionReturnStatement([NotNull] ClepsParser.FunctionReturnStatementContext context)
        {
            LLVMValueRef returnValueRegister;
            ClepsType    returnType;

            if (context.rightHandExpression() == null)
            {
                returnValueRegister = LLVM.BuildRetVoid(Builder);
                returnType          = ClepsType.GetVoidType();
            }
            else
            {
                var returnValuePtr = Visit(context.rightHandExpression());
                returnValueRegister = LLVM.BuildLoad(Builder, returnValuePtr.LLVMPtrValueRef, "returnValue");
                LLVM.BuildRet(Builder, returnValueRegister);
                returnType = returnValuePtr.VariableType;
            }

            var ret = new LLVMRegister(returnType, returnValueRegister);

            return(ret);
        }