Esempio n. 1
0
        public override void Visit(FunctionCallAST functionCall)
        {
            var functionIdent = _symTableManager.LookupFunctionInfo(_currentFileName, (functionCall.Name as VariableNameAST).Name, _currentScopeId, functionCall.argTypes);

            if (functionIdent.valueRef.Pointer == IntPtr.Zero)
            {
                var fnType = IRTypesConverter.GetFunctionType(functionIdent.typeAST as FunctionTypeAST);

                functionIdent.valueRef = LLVM.AddFunction(_module, functionIdent.name, fnType);
            }

            var expressions = new LLVMValueRef[Math.Max(functionCall.ExpressionList.Count, 1)];

            for (var i = 0; i < functionCall.ExpressionList.Count; i++)
            {
                functionCall.ExpressionList[i].Accept(this);

                expressions[i] = _currentValue;

                if (LLVM.IsConstant(_currentValue))
                {
                    expressions[i] = LLVM.BuildAlloca(_builder, _currentValue.TypeOf(), "val");
                    LLVM.BuildStore(_builder, _currentValue, expressions[i]);
                    expressions[i].Dump();
                }
            }
            LLVM.BuildCall(_builder, functionIdent.valueRef, out expressions[0], (uint)functionCall.ExpressionList.Count, (functionIdent.typeAST as FunctionTypeAST).ReturnType.ToString() == "void" ? "" : functionIdent.name);
        }
Esempio n. 2
0
        public override void Visit(FunctionCallAST functionCall)
        {
            if (_stateInfo.isConst)
            {
                throw new Exception("Expression must be constant");
            }

            _stateInfo.currentType = GetReturnType(functionCall);
        }
Esempio n. 3
0
        public TypeAST GetReturnType(FunctionCallAST functionCallAst)
        {
            List <TypeAST> argsType = null;

            if (functionCallAst.ExpressionList.Count != 0)
            {
                if (functionCallAst.argTypes == null)
                {
                    argsType = new List <TypeAST>();

                    foreach (var expression in functionCallAst.ExpressionList)
                    {
                        expression.Accept(this);

                        argsType.Add(_stateInfo.currentType);
                    }

                    functionCallAst.argTypes = argsType;
                }
                else
                {
                    argsType = functionCallAst.argTypes;
                }
            }

            FunctionTypeAST functionType;

            var arrayFunctionCall = functionCallAst.Name as ArrayAccessAST;

            if (arrayFunctionCall != null)
            {
                var arrayType = GetArrayType(arrayFunctionCall);

                var accessDimensions = arrayFunctionCall.AccessExprList.Count;

                functionType = GetArrayContainedType(arrayType, accessDimensions) as FunctionTypeAST;

                string invalidArgsMessage = string.Format("Invalid function arguments expected '{0}' got '{1}'",
                                                          string.Join(",", functionType.ArgumentTypes), argsType == null ? "no argument" : string.Join(",", argsType));

                if (argsType != null &&
                    ((!functionType.IsVarArgsFn && functionType.ArgumentTypes.Count != argsType.Count) ||
                     (functionType.IsVarArgsFn && functionType.ArgumentTypes.Count > argsType.Count)))
                {
                    throw new Exception(invalidArgsMessage);
                }

                if (argsType == null && functionType.ArgumentTypes.Count != 0)
                {
                    throw new Exception(invalidArgsMessage);
                }

                for (var i = 0; i < functionType.ArgumentTypes.Count; i++)
                {
                    if (functionType.ArgumentTypes[i].ToString() != argsType[i].ToString() && !functionType.ArgumentTypes[i].GetType().IsAssignableFrom(argsType[i].GetType()))
                    {
                        throw new Exception(invalidArgsMessage);
                    }
                }
            }
            else
            {
                var functionCallInfo = _symTableManager.
                                       LookupFunctionInfo(_stateInfo.currentFile, functionCallAst.Name.ToString(),
                                                          _stateInfo.scopeId, argsType);

                if (functionCallInfo == null)
                {
                    throw new Exception(string.Format("Undefined function {0}({1})",
                                                      functionCallAst.Name,
                                                      argsType != null ? string.Join(",", argsType) : ""));
                }

                functionType = functionCallInfo.typeAST as FunctionTypeAST;
            }

            return(functionType.ReturnType);
        }
Esempio n. 4
0
 public virtual void Visit(FunctionCallAST functionCall)
 {
 }
Esempio n. 5
0
 public override void Visit(FunctionCallAST functionCall)
 {
     _exprTypeVisitor.GetAstNodeType(_currentFileName,
                                     _currentScopeId, _currentNodePosition,
                                     functionCall);
 }