Exemplo n.º 1
0
        public bool checkUnique(DiagnosticPosition pos, FuncSymbol sym, Scope scope)
        {
            bool contains = scope.getSymbolsByName(sym.name, LookupKind.NON_RECURSIVE).Any();

            if (contains)
            {
                log.error(pos, messages.duplicateFunctionName, sym.name);
            }
            return(!contains);
        }
Exemplo n.º 2
0
 public bool checkMainFunction(DiagnosticPosition pos, FuncSymbol main)
 {
     // Mimic C main function sig: int main(int,char**)
     // with long substituting pointer (implying 64bit arch)
     if (main.type.ReturnType != symtab.intType ||
         main.type.ParameterTypes.Count != 2 ||
         main.type.ParameterTypes[0] != symtab.intType ||
         main.type.ParameterTypes[1] != symtab.longType)
     {
         log.error(pos, messages.mainFunctionSig);
         return(false);
     }
     return(true);
 }
Exemplo n.º 3
0
            private void enterParameter(VariableDeclaration varDef, WritableScope scope)
            {
                Type       varType = (Type)scan(varDef.type, scope);
                FuncSymbol func    = (FuncSymbol)scope.owner;
                VarSymbol  varSym  = new VarSymbol(Kind.PARAM, varDef.name, varType, func);

                varDef.symbol = varSym;

                func.parameters.Add(varSym);

                if (check.checkUniqueParam(varDef.Pos, varSym, scope))
                {
                    scope.enter(varSym);
                }
            }
Exemplo n.º 4
0
        public override Type visitFuncInvoke(FuncInvocation invocation, Environment env)
        {
            // resolve func
            string     name = invocation.funcName;
            FuncSymbol fsym = (FuncSymbol)env.scope.findFirst(name, s => s.kind == Kind.FUNC);

            if (fsym == null)
            {
                log.error(invocation.Pos, messages.functionNotDefined, name);
                // we dont need any validation
                invocation.type = symtab.errorType;
                return(symtab.errorType);
            }

            return(analyzeInvocation(invocation, fsym, env));
        }
Exemplo n.º 5
0
            public override object visitFuncDef(FuncDef func, WritableScope enclScope)
            {
                FuncSymbol fsym = makeFuncSymbol(func, enclScope);

                if (enclScope == symtab.topLevelSymbol.topScope && func.name == "main")
                {
                    mainFuncFound = true;
                    check.checkMainFunction(func.Pos, fsym);
                }

                if (check.checkUnique(func.Pos, fsym, enclScope))
                {
                    enclScope.enter(fsym);
                }
                return(null);
            }
Exemplo n.º 6
0
            private FuncSymbol makeFuncSymbol(FuncDef func, WritableScope enclScope)
            {
                FuncSymbol fsym = new FuncSymbol(func.name, enclScope.owner, null);

                func.symbol = fsym;

                fsym.owner = enclScope.owner;

                // create scope for func parameters and local variables
                WritableScope funcScope = enclScope.subScope(fsym);

                fsym.scope = funcScope;

                fsym.parameters = new List <VarSymbol>(func.parameters.Count);
                FuncType ftype = signature(func.returnType, func.parameters, funcScope);

                ftype.symbol = fsym;
                fsym.type    = ftype;
                return(fsym);
            }
Exemplo n.º 7
0
        private Type analyzeInvocation(FuncInvocation invocation, FuncSymbol fsym, Environment env)
        {
            IList <Expression> args = invocation.args;

            // check argument count
            IList <VarSymbol> paramSyms = fsym.parameters;

            if (fsym.isVararg ? args.Count < paramSyms.Count : args.Count != paramSyms.Count)
            {
                log.error(invocation.Pos, messages.wrongNumberOfArgs, fsym.name, paramSyms.Count, args.Count);
            }

            // check argument types
            int count = Math.Min(args.Count, paramSyms.Count);

            for (int i = 0; i < count; i++)
            {
                VarSymbol paramSym = paramSyms[i];
                Type      argType  = analyzeExpr(args[i], env);

                // we don't consider implicit numeric conversions for now
                if (!typings.isAssignableFrom(paramSym.type, argType))
                {
                    log.error(invocation.Pos, messages.paramTypeMismatch, fsym.name);
                }
            }

            for (int i = count; i < args.Count; ++i)
            {
                analyzeExpr(args[i], env);
            }

            fsym.isInvoked     = true;
            invocation.funcSym = fsym;
            invocation.type    = fsym.type.ReturnType;
            return(fsym.type.ReturnType);
        }
Exemplo n.º 8
0
        /// <summary>
        /// function ::= func identifier {func_params} {define_op type} colon func_body
        /// </summary>
        private IFuncNode <TwisterPrimitive> Function()
        {
            var funcToken = _matcher.MatchAndGet <IValueToken <Keyword> >(t => t.Value == Keyword.Func);
            var funcName  = _matcher.MatchAndGet <IValueToken <string> >().Value;

            if (funcName == "main")
            {
                _hasMain = true; // TODO : Pattern check full main func signature
            }
            var scope = _scopeManager.NewScope();

            if (_matcher.IsNext <RightBrackToken>())
            {
                var funcParams = FuncParams();
                scope.AddSymbols(funcParams);
            }
            _scopeManager.RemoveBottomScope();

            var type = TwisterType.Void;

            if (_matcher.IsNext <DefineToken>())
            {
                _matcher.Match();
                var typeToken = _matcher.MatchAndGet <IValueToken <Keyword> >(t => t.Value.IsTypeKeyword());
                type = typeToken.Value.ToTwisterType();
            }

            _matcher.Match <ColonToken>();

            var body = FuncBody();

            var funcSymbol = new FuncSymbol(funcName, type, SymbolAttribute.None, null /* TODO */);

            _scopeManager.ActiveScope.AddSymbol(funcSymbol);

            return(new FuncNode <TwisterPrimitive>(funcName, type, scope, body));
        }