Example #1
0
        //添加小类型
        public bool AddSmaillType(int bid, string sname, string explain)
        {
            SmallType st = new SmallType
            {
                Id          = BackIdService <BigType> .Instance.NewId(),
                Name        = sname,
                EditionUser = "******",
                BigType     = bid,
                Explain     = explain
            };
            var r = dbm.AddSmaillType(st);

            return(r);
        }
Example #2
0
        private static MethodDefinition GetDefinition(MethodSyntax pMethod, int pCounter, string pNamespace, string pName)
        {
            List <SmallType> arguments = new List <SmallType>(pMethod.Parameters.Count);

            for (int i = 0; i < pMethod.Parameters.Count; i++)
            {
                var parmType = pMethod.Parameters[i].Type;
                arguments.Add(parmType);
            }

            SmallType ret         = pMethod.Type;
            string    mangledName = pNamespace + "__" + pName + "_" + pCounter;

            return(new MethodDefinition(pMethod.Scope, pMethod.Name, mangledName, pMethod.External, arguments, ret));
        }
Example #3
0
        public MethodDefinition AddMethod(SmallType pType, string pNamespace, MethodSyntax pNode)
        {
            var name = GetMethodName(pType, pNode.Name);

            if (!_methods.ContainsKey(name))
            {
                _methods.Add(name, new List <MethodDefinition>());
                _counter.Add(name, 0);
            }
            _counter[name]++;
            var md = GetDefinition(pNode, _counter[name], pNamespace, name);

            _methods[name].Add(md);
            return(md);
        }
Example #4
0
        public FindResult FromString(string pName, out SmallType pType)
        {
            //Look through primitive types
            if (SmallTypeCache.TryGetPrimitive(pName, out pType))
            {
                return(FindResult.Found);
            }

            //Look for types defined in this compilation
            pType = _types.FindType(pName);
            if (pType == SmallTypeCache.Undefined)
            {
                return(FindResult.NotFound);
            }
            return(FindResult.Found);
        }
Example #5
0
 public FindResult FromString(string pNamespace, string pName, out SmallType pType)
 {
     if (pNamespace == null)
     {
         return(FromString(pName, out pType));
     }
     else
     {
         var result = _references[pNamespace].Cache.FromString(pName, out pType);
         if (result == FindResult.NotFound)
         {
             return(FromString(pName, out pType));
         }
         else if (pType.Scope == FileScope.Private)
         {
             return(FindResult.IncorrectScope);
         }
         return(result);
     }
 }
Example #6
0
        private SmallType GetReturnType()
        {
            var ts = new SmallType[] { SmallType.I16, SmallType.I32, SmallType.I64, SmallType.Float, SmallType.Double };
            var t  = new SmallType[, ] {
                { SmallType.I16, SmallType.I32, SmallType.I64, SmallType.Float, SmallType.Double },
                { SmallType.I32, SmallType.I32, SmallType.I64, SmallType.Float, SmallType.Double },
                { SmallType.I64, SmallType.I64, SmallType.I64, SmallType.Float, SmallType.Double },
                { SmallType.Float, SmallType.Float, SmallType.Float, SmallType.Float, SmallType.Double },
                { SmallType.Double, SmallType.Double, SmallType.Double, SmallType.Double, SmallType.Double }
            };

            int i  = Array.IndexOf(ts, Left.Type);
            int i2 = Array.IndexOf(ts, Right.Type);

            if (i == -1 || i2 == -1)
            {
                return(SmallType.Undefined);
            }
            return(t[i, i2]);
        }
        protected override void VisitForSyntax(ForSyntax pNode)
        {
            _locals.AddScope();

            if (pNode.Iterator != null)
            {
                Visit(pNode.Iterator);

                //Array vs Enumerable<T>
                if (pNode.Iterator.Type.IsArray)
                {
                    _itType = pNode.Iterator.Type.GetElementType();
                }
                else if (SmallTypeCache.TryGetEnumerable(_unit, out SmallType enumerable) && pNode.Iterator.Type.IsAssignableFrom(enumerable))
                {
                    _itType = pNode.Iterator.Type.GenericArguments[0];
                }
                else
                {
                    _itType = pNode.Iterator.Type;
                }
            }
            else
            {
                foreach (var d in pNode.Initializer)
                {
                    Visit(d);
                }
                Visit(pNode.Condition);

                foreach (var f in pNode.Finalizer)
                {
                    Visit(f);
                }
            }

            Visit(pNode.Body);

            _locals.RemoveScope();
        }
Example #8
0
        internal static SmallType GetResultType(SmallType pLeft, BinaryExpressionOperator pOp, SmallType pRight)
        {
            switch (pOp)
            {
            case BinaryExpressionOperator.Addition:
            case BinaryExpressionOperator.Subtraction:
            case BinaryExpressionOperator.Multiplication:
            case BinaryExpressionOperator.Division:
            case BinaryExpressionOperator.Mod:
                if (pLeft.IsAssignableFrom(pRight))
                {
                    return(pLeft);
                }
                if (TypeHelper.IsFloat(pLeft) && TypeHelper.IsNumber(pRight))
                {
                    return(pLeft);
                }
                if (TypeHelper.IsFloat(pRight) && TypeHelper.IsNumber(pLeft))
                {
                    return(pRight);
                }
                return(SmallTypeCache.Undefined);

            case BinaryExpressionOperator.Equals:
            case BinaryExpressionOperator.GreaterThan:
            case BinaryExpressionOperator.GreaterThanOrEqual:
            case BinaryExpressionOperator.LessThan:
            case BinaryExpressionOperator.LessThanOrEqual:
            case BinaryExpressionOperator.NotEquals:
                if (pLeft.IsAssignableFrom(pRight))
                {
                    return(pLeft);
                }
                return(SmallTypeCache.Undefined);

            default:
                throw new NotSupportedException("Unknown binary expression operator " + pOp.ToString());
            }
        }
 private void ChangeNumber(NumericLiteralSyntax pNode, SmallType pType)
 {
     if (pType == SmallType.Double)
     {
         pNode.NumberType = NumberType.Double;
     }
     if (pType == SmallType.Float)
     {
         pNode.NumberType = NumberType.Float;
     }
     if (pType == SmallType.I16)
     {
         pNode.NumberType = NumberType.I16;
     }
     if (pType == SmallType.I32)
     {
         pNode.NumberType = NumberType.I32;
     }
     if (pType == SmallType.I64)
     {
         pNode.NumberType = NumberType.I64;
     }
 }
Example #10
0
        public override void Visit(WorkspaceSyntax pNode)
        {
            foreach (var i in pNode.Imports)
            {
                i.Accept(this);
            }

            //We need to register all types since structs could have fields of other types
            foreach (var s in pNode.Structs)
            {
                var st = SmallType.RegisterType("", s.Prefix, true, s.TypeArgs);
                if (s.Initializer != null)
                {
                    st.SetInitializer(new InitializerInfo(s.Initializer));
                }
            }
            foreach (var s in pNode.Structs)
            {
                s.Accept(this);
            }

            foreach (var m in pNode.Methods)
            {
                m.Accept(this);
            }

            foreach (var c in pNode.Casts)
            {
                c.Accept(this);
            }
            if (!_mainFound && _mainRequired)
            {
                Compiler.ReportError(CompilerErrorType.NoRun, new TextSpan());
            }
            MetadataCache.AddImplicitCasts();
        }
 public SmallRequestEventArgs(SmallType small) {
     Small = small;
 }
Example #12
0
        public Compiler.FindResult FindMethod(out MethodDefinition pMethod, bool pAllowPrivate, SmallType pType, string pName, params SmallType[] pArguments)
        {
            var name = GetMethodName(pType, pName);

            if (!_methods.ContainsKey(name))
            {
                pMethod = default;
                return(Compiler.FindResult.NotFound);
            }

            List <MethodDefinition> candidates = _methods[name];
            MethodDefinition        retval     = candidates[0];

            foreach (var c in candidates)
            {
                //Parameter count match
                if (c.ArgumentTypes.Count == pArguments.Length)
                {
                    retval = c;
                    //Types match
                    bool found = true;
                    for (int i = 0; i < c.ArgumentTypes.Count && found; i++)
                    {
                        if (c.ArgumentTypes[i].IsGenericParameter && pType != null)
                        {
                            found = pArguments[i].IsAssignableFrom(pType.GenericArguments[i]);
                        }
                        else
                        {
                            found = pArguments[i].IsAssignableFrom(c.ArgumentTypes[i]);
                        }
                    }

                    //Scope match
                    if (found)
                    {
                        pMethod = c;
                        if (pMethod.Scope == FileScope.Public || pAllowPrivate)
                        {
                            return(Compiler.FindResult.Found);
                        }
                        return(Compiler.FindResult.IncorrectScope);
                    }
                }
            }

            pMethod = retval;
            return(Compiler.FindResult.NotFound);
        }
Example #13
0
 public IdentifierSyntax(string pValue) : base(0)
 {
     Value          = pValue;
     _type          = SmallType.FromString("", pValue);
     TypeParameters = new List <string>();
 }
Example #14
0
 public LocalBuilder CreateLocal(string pValue, SmallType pType)
 {
     return(CurrentMethod.CreateLocal(pValue, pType));
 }
Example #15
0
 public bool UpdateSmallType(SmallType h)
 {
     st.SetCtx(db);
     return(st.Update(h));
 }
Example #16
0
 public bool AddSmaillType(SmallType h)
 {
     st.SetCtx(db);
     return(st.Add(h));
 }
Example #17
0
        /// <summary>
        /// 小类表类型添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int PostSmallType(SmallType model)
        {
            string sql = $"insert into SmallType values('{model.Stname}')";

            return(DBHelper.ExecuteNonQuery(sql));
        }
Example #18
0
 public void SetType(SmallType pType)
 {
     _type = pType;
 }
Example #19
0
 public FieldDefinition(SmallType pType, string pName, object pValue) : this(pType, pName, pValue, FieldVisibility.Public)
 {
 }
Example #20
0
 private void CreateParameter(short pPosition, string pName, SmallType pType, bool pIsRef)
 {
     CurrentMethod.CreateParameter(pName, pPosition, pType, pIsRef);
 }
Example #21
0
        public Type EmitStruct(SmallType pDefinition)
        {
            Type t  = null;
            var  ta = TypeAttributes.Class | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.SequentialLayout;

            if (pDefinition.Exported)
            {
                ta |= TypeAttributes.Public;
            }

            var tb = _builder.DefineType(pDefinition.Name, ta, typeof(ValueType));

            GenericTypeParameterBuilder[] typeParms = null;
            if (pDefinition.IsGenericDefinition)
            {
                typeParms = tb.DefineGenericParameters(pDefinition.GenericTypeParameters.Select((pSt) => pSt.Name).ToArray()); //eww
            }

            if (pDefinition.Exported)
            {
                var c  = typeof(TypePrefixAttribute).GetConstructor(new Type[] { typeof(string) });
                var cb = new CustomAttributeBuilder(c, new object[] { pDefinition.Name });
                tb.SetCustomAttribute(cb);
            }

            foreach (var f in pDefinition.GetFields())
            {
                var found = false;
                if (typeParms != null)
                {
                    foreach (var tp in typeParms)
                    {
                        foreach (var a in f.Type.GenericTypeParameters)
                        {
                            if (tp.Name == a.Name)
                            {
                                Type tt = tp;
                                if (f.Type.IsArray)
                                {
                                    tt = tt.MakeArrayType();
                                }
                                f.Info = tb.DefineField(f.Name, tt, FieldAttributes.Public);
                                found  = true;
                                break;
                            }
                        }
                    }
                }

                if (!found)
                {
                    f.Info = tb.DefineField(f.Name, f.Type.ToSystemType(), FieldAttributes.Public);
                }
            }

            if (pDefinition.HasInitializer)
            {
                var mb = tb.DefineMethod("Initialize", MethodAttributes.HideBySig | MethodAttributes.Public, null, null);
                pDefinition.Initializer.SetBuilder(mb);
            }

            //t = tb.CreateType();

            pDefinition.SetSystemType(tb);
            return(t);
        }
Example #22
0
        public MethodInfo EmitFunction(MethodDefinition pDefinition)
        {
            if (pDefinition.ExternMethod != null)
            {
                pDefinition.SetExternMethod(pDefinition.ExternMethod);
                if (pDefinition.ExternMethod.ContainsGenericParameters)
                {
                    for (int i = 0; i < pDefinition.TypeHints.Count; i++)
                    {
                        var args = pDefinition.ExternMethod.GetGenericArguments();
                        for (int j = 0; j < args.Length; j++)
                        {
                            if (pDefinition.TypeHints[i].Name == args[j].Name)
                            {
                                pDefinition.TypeHints[i].SetSystemType(args[j]);
                            }
                        }
                    }
                }
                return(pDefinition.CallSite);
            }

            var ma = MethodAttributes.HideBySig | MethodAttributes.Static;

            switch (pDefinition.Scope)
            {
            case Scope.Private:
                ma |= MethodAttributes.Private;
                break;

            case Scope.Public:
                ma |= MethodAttributes.Public;
                break;
            }

            CurrentMethod = pDefinition;
            var mb = _currentType.DefineMethod(pDefinition.Name, ma, null, null);

            CurrentMethod.SetBuilder(mb);

            Type[] types = new Type[pDefinition.Parameters.Length];

            Type[] typeParms = null;
            if (pDefinition.TypeHints.Count > 0)
            {
                string[] typeNames = new string[pDefinition.TypeHints.Count];
                for (int i = 0; i < typeNames.Length; i++)
                {
                    typeNames[i] = pDefinition.TypeHints[i].Name;
                }
                typeParms = mb.DefineGenericParameters(typeNames);
            }

            for (int i = 0; i < pDefinition.Parameters.Length; i++)
            {
                var  st = pDefinition.Parameters[i].Type;
                Type t  = null;
                if (typeParms != null)
                {
                    for (int j = 0; j < typeParms.Length; j++)
                    {
                        for (int k = 0; k < st.GenericTypeArgs.Length; k++)
                        {
                            var stt = st.GenericTypeArgs[k];
                            if (stt.Name == typeParms[j].Name)
                            {
                                t = typeParms[j];
                                stt.SetSystemType(t);
                            }
                        }
                    }

                    if (st.IsGenericTypeParameter)
                    {
                        t = typeParms[0];
                        st.SetSystemType(t);
                    }
                }

                types[i] = st.ToSystemType();
                if (pDefinition.Parameters[i].IsRef)
                {
                    types[i] = types[i].MakeByRefType();
                }
            }

            SmallType srt = pDefinition.GetReturnType();

            if (typeParms != null)
            {
                for (int j = 0; j < typeParms.Length; j++)
                {
                    if (srt.Name == typeParms[j].Name)
                    {
                        srt.SetSystemType(typeParms[j]);
                    }
                }
            }

            mb.SetParameters(types);
            mb.SetReturnType(srt.ToSystemType());

            if (pDefinition.IsMain)
            {
                _assembly.SetEntryPoint(CurrentMethod.CallSite, PEFileKinds.ConsoleApplication);
                var cb = new CustomAttributeBuilder(typeof(STAThreadAttribute).GetConstructor(new Type[] { }), new object[] { });
                mb.SetCustomAttribute(cb);
            }

            for (short i = 0; i < pDefinition.Parameters.Length; i++)
            {
                CreateParameter(i, pDefinition.Parameters[i].Name, pDefinition.Parameters[i].Type, pDefinition.Parameters[i].IsRef);
            }
            return(CurrentMethod.CallSite);
        }
Example #23
0
 public static GenericTypeSyntax GenericType(SmallType pType)
 {
     return(new GenericTypeSyntax(pType));
 }
Example #24
0
        public LLVMValueRef EmitMethodHeader(string pName, Syntax.MethodSyntax pMethod, out string pNewName)
        {
            //Get method return type
            LLVMTypeRef ret;

            if (pMethod.ReturnValues.Count == 0)
            {
                ret = LLVMTypeRef.VoidType();
            }
            else if (pMethod.ReturnValues.Count == 1)
            {
                ret = SmallTypeCache.GetLLVMType(pMethod.Type, this);
            }
            else
            {
                LLVMTypeRef[] types = new LLVMTypeRef[pMethod.ReturnValues.Count];
                for (int i = 0; i < types.Length; i++)
                {
                    types[i] = SmallTypeCache.GetLLVMType(pMethod.ReturnValues[i].Type, this);
                }
                ret = LLVM.StructType(types, false);
                Cache.SetLLVMType(pMethod.Type.Name, ret);
            }

            //If we are emitting a struct method we need to add "self" as a parameter
            SmallType[]   originalTypes = new SmallType[pMethod.Parameters.Count];
            LLVMTypeRef[] parmTypes     = null;
            int           start         = 0;

            if (CurrentStruct != null)
            {
                parmTypes    = new LLVMTypeRef[pMethod.Parameters.Count + 1];
                parmTypes[0] = LLVMTypeRef.PointerType(SmallTypeCache.GetLLVMType(CurrentStruct, this), 0);
                start        = 1;
            }
            else
            {
                parmTypes = new LLVMTypeRef[pMethod.Parameters.Count];
            }

            //Get parameter types
            for (int i = 0; i < pMethod.Parameters.Count; i++)
            {
                var parmType = pMethod.Parameters[i].Type;
                if (parmType.IsGenericParameter)
                {
                    originalTypes[i] = TypeMappings[parmType.Name];
                }
                else
                {
                    originalTypes[i] = parmType;
                }

                //For calling external methods with strings, we only want to pass the character array
                if (pMethod.External && parmType == SmallTypeCache.String)
                {
                    parmType = parmType.GetElementType();
                }

                parmTypes[start + i] = SmallTypeCache.GetLLVMType(parmType, this);
                if (pMethod.Parameters[i].Type.IsStruct || pMethod.Parameters[i].Type.IsArray)
                {
                    parmTypes[start + i] = LLVMTypeRef.PointerType(parmTypes[start + i], 0);
                }
            }

            var result = Cache.FindMethod(out MethodDefinition pDefinition, null, CurrentStruct, pName, originalTypes);

            Debug.Assert(result == Compiler.FindResult.Found);
            pNewName = pDefinition.MangledName;

            //Method header
            var func = LLVM.GetNamedFunction(CurrentModule, pNewName);

            if (func.Pointer == IntPtr.Zero)
            {
                func = LLVM.AddFunction(CurrentModule, pNewName, LLVM.FunctionType(ret, parmTypes, false));
                LLVM.SetLinkage(func, LLVMLinkage.LLVMExternalLinkage);
            }

            if (pMethod.External)
            {
                //Create attribute so we can find it later when executing
                var attribute = LLVM.CreateStringAttribute(_context, "external", 8, pMethod.Annotation.Value, (uint)pMethod.Annotation.Value.Length);
                LLVM.AddAttributeAtIndex(func, LLVMAttributeIndex.LLVMAttributeFunctionIndex, attribute);
            }

            return(func);
        }
Example #25
0
 internal GenericTypeSyntax(SmallType pType) : base(null, pType.Name, new List <TypeSyntax>())
 {
     _type = pType;
 }
Example #26
0
 public void Request(SmallType smallType, long uval) {
     HandleSend(new IS_SMALL { ReqI = RequestId, SubT = smallType, UVal = (uint)uval });
 }
Example #27
0
 public bool DeleteSmaillType(SmallType h)
 {
     st.SetCtx(db);
     return(st.Delete(h));
 }
Example #28
0
 public MemberAccess(LLVMValueRef pValue, SmallType pType)
 {
     Value = pValue;
     Type  = pType;
 }
Example #29
0
        public override void Visit(ImportSyntax pNode)
        {
            if (!_importPath.Contains(pNode.Path))
            {
                _importPath.Add(pNode.Path);
                var path = System.IO.Path.GetFullPath(System.IO.Path.Combine(_path, pNode.Path + ".dll"));

                Assembly asm = null;
                try
                {
                    asm = Assembly.LoadFile(path);
                }
                catch (Exception)
                {
                    Compiler.ReportError(CompilerErrorType.InvalidImport, pNode, pNode.Path);
                    return;
                }

                try
                {
                    System.IO.File.Copy(path, System.IO.Path.Combine(_path, System.IO.Path.GetFileName(path)), true);
                }
                catch (Exception)
                {
                    Compiler.ReportError("Unable to copy import to local directory");
                }

                //Import all types from the import
                //TODO move this to struct definition thing?
                foreach (var t in asm.GetTypes())
                {
                    if (t.IsValueType && t.IsPublic)
                    {
                        string prefix = "";
                        var    attr   = t.GetCustomAttributes <Emitting.TypePrefixAttribute>().SingleOrDefault();

                        //All exported types must have a TypePrefixAttribute in order to be imported
                        if (attr != null)
                        {
                            //
                            //Create type
                            //
                            prefix = attr.Prefix;

                            //Get any generic type parameters of the type
                            List <string> typeParameters = new List <string>();
                            if (t.IsGenericType)
                            {
                                var ga = t.GetGenericArguments();
                                for (int i = 0; i < ga.Length; i++)
                                {
                                    typeParameters.Add(ga[i].Name);
                                }
                            }
                            var st = SmallType.RegisterType(pNode.Alias, prefix, true, typeParameters);
                            st.SetSystemType(t);

                            foreach (var f in t.GetFields())
                            {
                                SmallType fieldType = null;
                                if (f.FieldType.IsGenericParameter)
                                {
                                    //If it's a generic type parameter, use the generic type
                                    foreach (var s in st.GenericTypeParameters)
                                    {
                                        if (s.Name.Equals(f.FieldType.Name, StringComparison.OrdinalIgnoreCase))
                                        {
                                            fieldType = s;
                                        }
                                    }
                                }
                                else
                                {
                                    fieldType = SmallType.FromSystemType(f.FieldType);
                                }

                                typeParameters.Clear();

                                //Get field type parameters
                                if (f.FieldType.ContainsGenericParameters)
                                {
                                    var typeArguments = t.GetGenericArguments();
                                    foreach (var tp in typeArguments)
                                    {
                                        if (tp.IsGenericParameter)
                                        {
                                            typeParameters.Add(tp.Name);
                                        }
                                    }
                                }

                                st.AddField(f.Name, typeParameters, fieldType);
                            }

                            //Check if we have an initializer function
                            var m = t.GetMethod("Initialize");
                            if (m != null)
                            {
                                var ii = new InitializerInfo(null);
                                ii.SetMethod(m);
                                st.SetInitializer(ii);
                            }
                        }
                    }
                }

                foreach (var t in asm.GetTypes())
                {
                    foreach (var m in t.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static))
                    {
                        var p = m.GetParameters();
                        MethodDefinition.Parameter[] parameters = new MethodDefinition.Parameter[p.Length];
                        for (int i = 0; i < p.Length; i++)
                        {
                            parameters[i] = new MethodDefinition.Parameter(SmallType.FromSystemType(p[i].ParameterType), p[i].Name, p[i].ParameterType.IsByRef);
                        }

                        SmallType[] returnType;
                        if (m.ReturnType.IsConstructedGenericType)
                        {
                            var types = m.ReturnType.GetGenericArguments();
                            returnType = new SmallType[types.Length];
                            for (int i = 0; i < types.Length; i++)
                            {
                                returnType[i] = SmallType.FromSystemType(types[i]);
                            }
                        }
                        else if (m.ReturnType.IsGenericParameter)
                        {
                            returnType = new SmallType[] { SmallType.CreateGenericParameter(m.ReturnType.Name) };
                        }
                        else if (m.ReturnType != typeof(void))
                        {
                            returnType = new SmallType[] { SmallType.FromSystemType(m.ReturnType) }
                        }
                        ;
                        else
                        {
                            returnType = new SmallType[0];
                        }

                        List <SmallType> typeHints = new List <SmallType>();
                        if (m.IsGenericMethodDefinition)
                        {
                            var typeParameters = m.GetGenericArguments();
                            foreach (var tp in typeParameters)
                            {
                                if (tp.IsGenericParameter)
                                {
                                    typeHints.Add(SmallType.CreateGenericParameter(tp.Name));
                                }
                            }
                        }

                        MethodDefinition md = null;
                        if (returnType.Length == 1 && parameters.Length == 1 &&
                            m.Name == MetadataCache.CastFunction(parameters[0].Type, returnType[0]))
                        {
                            md = MetadataCache.AddImportedCast(pNode.Alias, parameters[0].Type, parameters[0].Name, returnType[0]);
                        }
                        else
                        {
                            md = MetadataCache.AddImportedMethod(pNode.Alias, m.Name, parameters, returnType, typeHints);
                        }
                        md.SetExternMethod(m);
                    }
                }
            }

            if (MetadataCache.ImportedNamespaces().Contains(pNode.Alias))
            {
                Compiler.ReportError(CompilerErrorType.DuplicateImportAlias, pNode, pNode.Alias);
            }
            else
            {
                MetadataCache.AddNamespace(pNode.Alias);
            }
        }
        protected override void VisitMethodCallSyntax(MethodCallSyntax pNode)
        {
            base.VisitMethodCallSyntax(pNode);

            SmallType[] types = new SmallType[pNode.Arguments.Count];
            for (int i = 0; i < types.Length; i++)
            {
                types[i] = pNode.Arguments[i].Type;
                if (types[i] == SmallTypeCache.NoValue)
                {
                    CompilerErrors.ExpressionNoValue(pNode.Arguments[i].Span);
                }
            }

            if (SyntaxHelper.HasUndefinedCastAsArg(pNode))
            {
                IList <MethodDefinition> matches = _unit.GetAllMatches(Namespace, pNode.Value, pNode.Arguments.Count);
                if (matches.Count > 1)
                {
                    //If multiple matches are found the implicit cast could map to either method, so we can't tell
                    CompilerErrors.InferImplicitCast(pNode.Span);
                    return;
                }
                else if (matches.Count == 1)
                {
                    //Check if we can determine implicit cast type yet
                    for (int j = 0; j < Math.Min(matches[0].ArgumentTypes.Count, pNode.Arguments.Count); j++)
                    {
                        if (SyntaxHelper.IsUndefinedCast(pNode.Arguments[j]))
                        {
                            TrySetImplicitCastType(pNode.Arguments[j], matches[0].ArgumentTypes[j]);
                            types[j] = pNode.Arguments[j].Type;
                        }
                    }
                }
            }



            //Check to ensure this method exists
            var result = SyntaxHelper.FindMethodOnType(out MethodDefinition m, _unit, Namespace, pNode.Value, CurrentType, types);

            switch (result)
            {
            case Compiler.FindResult.NotFound:
                CompilerErrors.MethodNotFound(m, Struct, pNode.Value, pNode.Arguments, pNode.Span);
                return;

            case Compiler.FindResult.IncorrectScope:
                CompilerErrors.MethodNotInScope(m, Struct, pNode.Value, pNode.Arguments, pNode.Span);
                return;
            }

            for (int i = 0; i < m.ArgumentTypes.Count; i++)
            {
                ForceCastLiteral(m.ArgumentTypes[i], pNode.Arguments[i]);
            }

            //Poly our method definition to match any generic types
            m = m.MakeConcreteDefinition(CurrentType);
            pNode.SetType(m.ReturnType);
        }
Example #31
0
 public override T FromNode <T>(T pNode)
 {
     _type = pNode.Type;
     return(base.FromNode(pNode));
 }
Example #32
0
        public override void Visit(MethodSyntax pNode)
        {
            //Create any generic type parameters to the function
            Dictionary <string, SmallType> typeArgs = new Dictionary <string, SmallType>();

            foreach (var t in pNode.TypeHints)
            {
                typeArgs.Add(t, SmallType.CreateGenericParameter(t));
            }

            //
            //Create types for method
            //

            //Infer parameter types now that structs have been defined
            foreach (var p in pNode.Parameters)
            {
                var st = SmallType.FromString(p.Namespace, p.Value);
                if (p.TypeParameters.Count > 0)
                {
                    SmallType[] types = new SmallType[p.TypeParameters.Count];
                    for (int i = 0; i < types.Length; i++)
                    {
                        //If the type parameter is one on the method definition, use that type
                        if (typeArgs.ContainsKey(p.TypeParameters[i]))
                        {
                            types[i] = typeArgs[p.TypeParameters[i]];
                        }
                        else
                        {
                            types[i] = SmallType.FromString("", p.TypeParameters[i]);
                        }
                    }

                    if (!p.Type.IsVariant)
                    {
                        st = st.MakeGenericType(types);
                    }
                    else
                    {
                        //vnt types are transformed to the actual type parameter
                        if (p.TypeParameters.Count > 1)
                        {
                            SmallType[] typeParameters = new SmallType[p.TypeParameters.Count];
                            for (int i = 0; i < typeParameters.Length; i++)
                            {
                                typeParameters[i] = SmallType.CreateGenericParameter(p.TypeParameters[i]);
                            }
                            st = SmallType.CreateTupleOf(typeParameters);
                        }
                        else
                        {
                            st = SmallType.CreateGenericParameter(p.TypeParameters[0]);
                        }
                    }
                }
                p.SetType(st);
            }

            foreach (var r in pNode.ReturnValues)
            {
                //If the type parameter is one on the method definition, use that type
                if (typeArgs.ContainsKey(r.Value))
                {
                    r.SetType(SmallType.CreateGenericParameter(r.Value));
                }
                else
                {
                    r.SetType(SmallType.FromString(r.Namespace, r.Value));
                }
            }

            //
            //Create method definition
            //

            //Create parameters
            MethodDefinition.Parameter[] parameters = new MethodDefinition.Parameter[pNode.Parameters.Count];
            for (int i = 0; i < pNode.Parameters.Count; i++)
            {
                parameters[i] = new MethodDefinition.Parameter(pNode.Parameters[i]);
            }

            //Create return types
            SmallType[] returnTypes = new SmallType[pNode.ReturnValues.Count];
            for (int i = 0; i < pNode.ReturnValues.Count; i++)
            {
                returnTypes[i] = pNode.ReturnValues[i].Type;
            }

            var d = MetadataCache.AddMethod(pNode.Name, parameters, returnTypes, typeArgs.Values.ToList());

            if (pNode.Annotations.Count > 0)
            {
                //Process any standard annotations
                //run
                //export
                //external info
                foreach (var a in pNode.Annotations)
                {
                    if (a.Value.Equals("run", StringComparison.OrdinalIgnoreCase))
                    {
                        if (_mainFound)
                        {
                            Compiler.ReportError(CompilerErrorType.DuplicateRun, pNode);
                        }
                        else
                        {
                            d.IsMain = true;
                        }

                        if (pNode.Parameters.Count > 0 || pNode.ReturnValues.Count > 0)
                        {
                            Compiler.ReportError(CompilerErrorType.InvalidRun, pNode);
                        }
                        _mainFound = true;
                    }
                    else if (a.Value.Equals("export", StringComparison.OrdinalIgnoreCase))
                    {
                        d.SetScope(Scope.Public);
                    }
                    else if (pNode.External)
                    {
                        var s = a.Value.Split(';');
                        if (s.Length != 3)
                        {
                            Compiler.ReportError(CompilerErrorType.InvalidExternalAnnotation, pNode);
                        }

                        d.SetExternInfo(s[0], s[1], s[2]);
                    }
                }

                if (d.ExternMethod != null && d.Scope == Scope.Public)
                {
                    Compiler.ReportError(CompilerErrorType.ExportExternal, pNode, pNode.Name);
                }
            }
            pNode.SetDefinition(d);
        }