Esempio n. 1
0
        public override void VisitInvoke(Invoke n)
        {
            if (InternalMethodManager.IsSystemMethod(n.Method))
            {
                n.Actuals.Visit(this);
                var method = InternalMethodManager.Lookup(n.Method);

                if (method.FuncInfo.Formals.Count == 1 &&
                    method.FuncInfo.Formals.First().Value.IsSupertype(new TypeObject()) &&
                    (_lastWalkedType.IsPrimitive || _lastWalkedType.IsSubclassOf(typeof(ValueType))))
                {
                    _gen.Emit(OpCodes.Box, _lastWalkedType);
                }

                method.Emit(_gen);

                _lastWalkedType = _typeManager.LookupCilType(method.FuncInfo.ReturnType);
            }
            else
            {
                //not allowing user-defined functions
                throw new TrancheCompilerException(string.Format("Unknown method {0} at {1}", n.Method, n.Location));
            }
        }
Esempio n. 2
0
        public void AddMethod(string typeName, DeclarationMethod n)
        {
            var info = TypeBuilderMap[typeName];
            //simulation is the entry point, must be static
            var attributes = n.Name.Equals("Simulation", StringComparison.OrdinalIgnoreCase)
                                ? MethodAttributes.Public | MethodAttributes.Static
                                : MethodAttributes.Public;

            if (InternalMethodManager.IsSystemMethod(n.Name))
            {
                var method   = InternalMethodManager.Lookup(n.Name);
                var funcInfo = method.FuncInfo;
                var formals  = funcInfo.Formals.Values.Select(LookupCilType);
                var m        = info.Builder.DefineMethod(n.Name,
                                                         attributes,
                                                         LookupCilType(funcInfo.ReturnType),
                                                         formals.ToArray());

                //store this MethodBuilder, keyed off its name
                info.MethodMap.Add(n.Name, new MethodBuilderInfo(m, BuildFormalMap(n.Descriptor.Formals)));
                return;
            }


            //we need to know the CIL type for the return type and arguments
            var returnType = LookupCilType(n.ReturnType);
            var function   = (TypeFunction)n.Type;

            var methodBuilder = info.Builder.DefineMethod(n.Name,
                                                          attributes,
                                                          returnType,
                                                          function.Formals.Values.Select(LookupCilType).ToArray());

            //store this MethodBuilder, keyed off its name
            info.MethodMap.Add(n.Name, new MethodBuilderInfo(methodBuilder, BuildFormalMap(n.Descriptor.Formals)));
        }