Ejemplo n.º 1
0
        /// <summary>
        /// Retrieves the referenced element from the specified namescope.
        /// </summary>
        /// <param name="namescope">The namescope from which to retrieve the referenced element.</param>
        /// <returns></returns>
        public UIElement GetReferencedElement(Namescope namescope)
        {
            Contract.Require(namescope, nameof(namescope));

            var element = namescope.GetElementByName(Name);
            if (element == null)
                throw new UvmlException(PresentationStrings.NamedElementDoesNotExist.Format(Name));

            return element;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UvmlInstantiationContext"/> class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="templatedParent">The templated parent for the instantiated object.</param>
        /// <param name="dataSource">The data source for the instantiated object.</param>
        /// <param name="dataSourceType">The data source type for the instantiated object.</param>
        /// <param name="namescope">The context's namescope, or <see langword="null"/> to create a new namescope.</param>
        internal UvmlInstantiationContext(UltravioletContext uv, Object templatedParent, Object dataSource, Type dataSourceType, Namescope namescope = null)
        {
            this.Ultraviolet = uv;
            this.Namescope = namescope ?? new Namescope();
            this.TemplatedParent = templatedParent;
            this.DataSource = dataSource;
            this.DataSourceType = dataSourceType;

            FindCompiledBindingExpressions();
        }
Ejemplo n.º 3
0
 public string GetMemberName(Member m, Function f, Namescope parent = null)
 {
     return(IsConstrained(f) ? f.Name + "_ex" : GetMemberName(m) + GetTemplateSuffix(f, parent));
 }
Ejemplo n.º 4
0
 public string GetTemplateSuffix(DataType dt, Namescope parent)
 {
     return(dt.IsStruct ? GetTemplateString(GetTemplateArguments(dt), parent) : null);
 }
Ejemplo n.º 5
0
        public static Constant CompileConstant(this Compiler compiler, AstExpression expression, Namescope scope, DataType expectedType = null)
        {
            var value  = CompileExpression(compiler, expression, scope, expectedType);
            var result = compiler.ConstantFolder.TryMakeConstant(value);

            if (result == null)
            {
                if (value != Expression.Invalid)
                {
                    compiler.Log.Error(expression.Source, ErrorCode.E0059, "Expression must be constant");
                }

                return(new Constant(expression.Source, DataType.Invalid, 0));
            }

            return(result);
        }
Ejemplo n.º 6
0
 public string GetFunctionPointer(Function f, Namescope parent = null, string suffix = "_fn")
 {
     return(GetStaticName(f.DeclaringType, parent) + "__" + f.Name + suffix);
 }
Ejemplo n.º 7
0
 public string GetTypeOfPointer(DataType dt, Namescope parent = null)
 {
     return("(uType*(*)())&" + GetStaticName(dt, parent) + "_typeof");
 }
Ejemplo n.º 8
0
 public FunctionCompiler(Compiler compiler, DataType dt, Expression obj)
     : this(compiler)
 {
     Function  = new Method(dt.Source, dt, null, obj == null ? Modifiers.Static : 0, ".member_init", DataType.Void, new Parameter[0]);
     Namescope = dt;
 }
Ejemplo n.º 9
0
        public void CreateDelegate(AstDelegate ast, Namescope parent)
        {
            var src    = ast.Name.Source;
            var result = new DelegateType(src, parent, ast.DocComment, GetTypeModifiers(parent, ast.Modifiers), ast.Name.Symbol);

            if (ast.OptionalGenericSignature != null)
            {
                CreateGenericSignature(result, ast.OptionalGenericSignature, false);
            }

            if (parent is DataType)
            {
                (parent as DataType).NestedTypes.Add(result);
            }
            else if (parent is Namespace)
            {
                (parent as Namespace).Types.Add(result);
            }
            else
            {
                Log.Error(result.Source, ErrorCode.I3047, "'delegate' is not allowed in this context");
            }

            if (ast.Members != null)
            {
                Log.Error(src, ErrorCode.I3019, "'delegate' cannot contain members");
            }

            if (ast.Attributes.Count > 0)
            {
                EnqueueAttributes(result,
                                  x => result.SetAttributes(_compiler.CompileAttributes(result.Parent, ast.Attributes)));
            }

            EnqueueType(result,
                        assignBaseType: x =>
            {
                var deferredActions   = new List <Action>();
                var parameterizedType = Parameterize(result);

                result.SetBase(_ilf.Essentials.Delegate);
                result.SetReturnType(_resolver.GetType(result, ast.ReturnType));
                result.SetParameters(_compiler.CompileParameterList(result, ast.Parameters, deferredActions));

                if (parameterizedType != result)
                {
                    var parameterizedDelegate = (DelegateType)parameterizedType;
                    parameterizedDelegate.SetBase(result.Base);
                    parameterizedDelegate.SetReturnType(result.ReturnType);
                    parameterizedDelegate.SetParameters(result.Parameters);
                }

                foreach (var action in deferredActions)
                {
                    action();
                }
            },
                        populate: x =>
            {
                var parameterizedType = Parameterize(x);

                x.Operators.Add(
                    CreateOperator(parameterizedType, OperatorType.Addition, parameterizedType,
                                   CreateParameter(ast.Name, parameterizedType, "left"),
                                   CreateParameter(ast.Name, parameterizedType, "right")));
                x.Operators.Add(
                    CreateOperator(parameterizedType, OperatorType.Subtraction, parameterizedType,
                                   CreateParameter(ast.Name, parameterizedType, "left"),
                                   CreateParameter(ast.Name, parameterizedType, "right")));
            });
        }
Ejemplo n.º 10
0
 public string GetTypeOf(Parameter p, Namescope parent, string type, TypeCache?cache = null)
 {
     return(GetTypeOf(p.Type, parent, type, null, cache) + (p.IsReference ? "->ByRef()" : null));
 }
Ejemplo n.º 11
0
 protected GenericType(Source src, Namescope parent, string comment, Modifiers modifiers, string name)
     : base(src, parent, comment, modifiers, name)
 {
 }
Ejemplo n.º 12
0
 public Element CreateElement(Source src, string value, Namescope scope)
 {
     return(new Element(src, value, 0, Compiler.NameResolver.GetUsings(scope, src)));
 }
Ejemplo n.º 13
0
        public void CreateClass(AstClass astClass, Namescope parent, IEnumerable <AstBlockMember> parentItems)
        {
            var sources = new List <string>();

            if (astClass.Modifiers.HasFlag(Modifiers.Partial))
            {
                astClass = FlattenClass(astClass, sources, parentItems);
            }

            DataType result;

            if (_cachedClasses.TryGetValue(astClass, out result))
            {
                return;
            }

            var src       = astClass.Name.Source;
            var modifiers = GetTypeModifiers(parent, astClass.Modifiers);

            switch (astClass.Type)
            {
            case AstClassType.Struct:
                result = new StructType(src, parent, astClass.DocComment, modifiers, astClass.Name.Symbol);
                break;

            case AstClassType.Class:
                result = new ClassType(src, parent, astClass.DocComment, modifiers, astClass.Name.Symbol);
                break;

            case AstClassType.Interface:
                if (modifiers.HasFlag(Modifiers.Abstract))
                {
                    Log.Error(src, ErrorCode.E0000, "'abstract' is not valid for interface");
                }

                result = new InterfaceType(src, parent, astClass.DocComment, modifiers | Modifiers.Abstract, astClass.Name.Symbol);
                break;

            default:
                Log.Error(src, ErrorCode.I3045, "<" + astClass.Type + "> is not a class, struct or interface type");
                return;
            }

            if (parent is DataType)
            {
                (parent as DataType).NestedTypes.Add(result);
            }
            else if (parent is Namespace)
            {
                (parent as Namespace).Types.Add(result);
            }
            else
            {
                Log.Error(result.Source, ErrorCode.I3046, "<" + astClass.Type + "> is not allowed in this context");
            }

            _cachedClasses.Add(astClass, result);

            if (astClass.OptionalGeneric != null)
            {
                CreateGenericSignature(result, astClass.OptionalGeneric, false);
            }

            result.SetBlock(new Block(src, result, null, result.Modifiers & Modifiers.ProtectionModifiers, ".block"));

            foreach (var s in sources)
            {
                result.SourceFiles.Add(s);
            }

            foreach (var b in astClass.Members)
            {
                if (b is AstBlockBase)
                {
                    _compiler.AstProcessor.CreateBlock(b as AstBlockBase, result, astClass.Members);
                }
            }

            if (astClass.Attributes.Count > 0)
            {
                EnqueueAttributes(result, x =>
                {
                    result.SetAttributes(_compiler.CompileAttributes(result.Parent, astClass.Attributes));

                    // Remove default constructor if TargetSpecificType
                    if (result.HasAttribute(_ilf.Essentials.TargetSpecificTypeAttribute) &&
                        result.Constructors.Count == 1 && result.Constructors[0].IsGenerated)
                    {
                        result.Constructors.Clear();
                    }
                });
            }

            EnqueueType(result,
                        x => CompileBaseTypes(x, astClass.Bases),
                        x => PopulateClass(astClass, x));
        }
Ejemplo n.º 14
0
        void Suggest(Namescope ns, string ignoredClassName = null, SuggestTypesMode mode = SuggestTypesMode.Everything)
        {
            if (ns is Namespace)
            {
                Suggest(SuggestItemType.Namespace, ns, ns.Name);
            }
            if (ns is ClassType)
            {
                if (ignoredClassName == null || ignoredClassName != ns.Name)
                {
                    if (_exceptionTypesOnly)
                    {
                        var ct = ns as ClassType;
                        if (!ct.Equals(_context.Compiler.ILFactory.Essentials.Exception) && !ct.IsSubclassOf(_context.Compiler.ILFactory.Essentials.Exception))
                        {
                            return;
                        }
                    }

                    string n = ns.Name;

                    var sit = SuggestItemType.Class;

                    if (mode == SuggestTypesMode.Blocks)
                    {
                        if (n.EndsWith("BlockFactory"))
                        {
                            n   = n.Substring(0, n.Length - "BlockFactory".Length);
                            sit = SuggestItemType.BlockFactory;
                        }
                        else
                        {
                            return;
                        }
                    }
                    else if (mode == SuggestTypesMode.Importers)
                    {
                        if (n.EndsWith("Importer"))
                        {
                            n   = n.Substring(0, n.Length - "Importer".Length);
                            sit = SuggestItemType.Importer;
                        }
                        else
                        {
                            return;
                        }
                    }
                    else if (mode == SuggestTypesMode.Attributes)
                    {
                        if (n.EndsWith("Attribute"))
                        {
                            n   = n.Substring(0, n.Length - "Attribute".Length);
                            sit = SuggestItemType.Class;
                        }
                        else
                        {
                            return;
                        }
                    }

                    Suggest(sit, ns, n);
                }
            }


            if (mode == SuggestTypesMode.Everything)
            {
                if (!_exceptionTypesOnly && ns is InterfaceType)
                {
                    Suggest(SuggestItemType.Interface, ns, ns.Name);
                }

                if (!_exceptionTypesOnly && ns is StructType)
                {
                    Suggest(SuggestItemType.Struct, ns, ns.Name);
                }

                if (!_exceptionTypesOnly && ns is DelegateType)
                {
                    Suggest(SuggestItemType.Delegate, ns, ns.Name);
                }

                if (!_exceptionTypesOnly && ns is EnumType)
                {
                    Suggest(SuggestItemType.Enum, ns, ns.Name);
                }

                if (ns is GenericParameterType)
                {
                    Suggest(SuggestItemType.GenericParameterType, ns, ns.Name);
                }
            }
        }
Ejemplo n.º 15
0
 public FunctionCompiler(Compiler compiler, Namescope namescope)
     : this(compiler)
 {
     Function  = new Method(Source.Unknown, namescope as DataType ?? Essentials.Object, null, Modifiers.Static, ".member_init", DataType.Void, new Parameter[0]);
     Namescope = namescope;
 }
Ejemplo n.º 16
0
 public string GetTemplateSuffix(Function f, Namescope parent)
 {
     return(GetTemplateString(GetTemplateArguments(f), parent));
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="UvmlInstantiationContext"/> class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="templatedParent">The templated parent for the instantiated object.</param>
        /// <param name="dataSource">The data source for the instantiated object.</param>
        /// <param name="dataSourceType">The data source type for the instantiated object.</param>
        /// <param name="namescope">The context's namescope, or <see langword="null"/> to create a new namescope.</param>
        internal UvmlInstantiationContext(UltravioletContext uv, Object templatedParent, Object dataSource, Type dataSourceType, Namescope namescope = null)
        {
            this.Ultraviolet     = uv;
            this.Namescope       = namescope ?? new Namescope();
            this.TemplatedParent = templatedParent;
            this.DataSource      = dataSource;
            this.DataSourceType  = dataSourceType;

            FindCompiledBindingExpressions();
        }
Ejemplo n.º 18
0
 public string GetTemplateString(DataType dt, Namescope parent)
 {
     return(GetTemplateString(GetTypeName(dt, parent, true)));
 }
Ejemplo n.º 19
0
        public static Expression TransformNullOpToConditionalOp(this NullOp s, Essentials types, Namescope scope)
        {
            // a ?? b -> (temp = a, temp != null ? temp : b)
            //   OR   -> a != null ? a : b

            var left  = s.Left;
            var right = s.Right;
            var ind   = TryCreateIndirection(scope, ref left);

            var cond   = new ReferenceOp(s.Source, types.Bool, EqualityType.NotEqual, left, new Constant(s.Source, left.ReturnType, null));
            var result = new ConditionalOp(s.Source, cond, left, right);

            return(ind != null
                ? (Expression) new SequenceOp(ind, result)
                : result);
        }
Ejemplo n.º 20
0
        string GetTypeOf_internal(DataType dt, Namescope parent, string type = null, Function func = null, TypeCache?cache = null)
        {
            if (cache != null && !Compare(dt, parent))
            {
                int index;
                if (cache.Value.Scope != null && cache.Value.Scope.TryGetValue(dt, out index))
                {
                    return("__types[" + index + "]");
                }
                if (cache.Value.Global != null && cache.Value.Global.TryGetValue(dt, out index))
                {
                    return("::TYPES[" + index + "/*" + dt + "*/]");
                }
                if (type != null && cache.Value.Precalc != null && cache.Value.Precalc.TryGetValue(dt, out index))
                {
                    return(type + (HasVirtualType(func)
                                ? "->GetBase(" + GetTypeOf((DataType)parent, parent, null, func, cache) + ")"
                                : null
                                   ) + "->Precalced(" + index + "/*" + dt + "*/)");
                }
            }

            var isBuildFunction = type == "type" ||
                                  type != null && type.StartsWith("type->");

            switch (dt.TypeType)
            {
            case TypeType.Void:
                return("uVoid_typeof()");

            case TypeType.RefArray:
                return(GetTypeOf(dt.ElementType, parent, type, func, cache) + "->Array()");

            case TypeType.Enum:
                return(GetStaticName(dt, parent) + "_typeof()");

            case TypeType.GenericParameter:
            {
                if (!dt.ParentType.IsGenericMethodType && isBuildFunction)
                {
                    type = "type";
                }

                var index = dt.GenericIndex;
                if (index != -1 && type != null)
                {
                    return(type + (
                               dt.ParentType.IsGenericMethodType
                                    ? "->U("
                                    : (!isBuildFunction && HasVirtualType(func)
                                            ? "->GetBase(" + GetTypeOf((DataType)parent, parent, null, func, cache) + ")"
                                            : null
                                       ) + "->T("
                               ) + index + ")");
                }

                Log.Error("C++: Failed to get typeof(" + dt.VerboseName + ") in " + ((object)func ?? parent).Quote());
                return("NULL");
            }

            default:
            {
                if (type != null &&
                    (dt.IsFlattenedDefinition || dt.IsMasterDefinition) && (
                        dt == parent && (
                            isBuildFunction ||
                            !HasVirtualType(func)
                            ) || (
                            dt.IsGenericMethodType &&
                            Compare(dt.Parent, parent)
                            )))
                {
                    return(type);
                }

                if (dt.IsFlattenedParameterization)
                {
                    if (dt.IsGenericMethodType)
                    {
                        var index = GetType(dt).MethodIndex;
                        if (index == -1)
                        {
                            Log.Error("C++: Failed to get index of " + dt.Quote() + " (generic method)");
                        }

                        var baseType = GetTypeOf(dt.ParentType, parent, type, func, cache);
                        if (baseType.StartsWith("type->MethodTypes["))
                        {
                            baseType = "type";
                        }

                        var sb = new StringBuilder(baseType +
                                                   "->MakeMethod(" + index + "/*" + dt.UnoName + dt.GenericSuffix + "*/");

                        foreach (var a in dt.GenericArguments)
                        {
                            sb.Append(", " + GetTypeOf(a, parent, type, func, cache));
                        }

                        sb.Append(", NULL)");
                        return(sb.ToString());
                    }
                    else
                    {
                        if (type != null &&
                            Compare(dt, parent))
                        {
                            return(type + (!isBuildFunction && HasVirtualType(func)
                                            ? "->GetBase(" + GetTypeOf((DataType)parent, parent, null, func, cache) + ")"
                                            : null));
                        }

                        var comma = false;
                        var sb    = new StringBuilder(
                            GetTypeOf(dt.MasterDefinition, parent, isBuildFunction ? "type" : type, func, cache) +
                            "->MakeType(");

                        foreach (var a in dt.FlattenedArguments)
                        {
                            sb.CommaWhen(comma);
                            sb.Append(GetTypeOf(a, parent, type, func, cache));
                            comma = true;
                        }

                        sb.Append(", NULL)");
                        return(sb.ToString());
                    }
                }

                SourceValue result;
                return(Environment.TryGetValue(dt, "TypeOfFunction", out result)
                        ? result.String + "()"
                        : GetStaticName(dt, parent) + "_typeof()");
            }
            }
        }
Ejemplo n.º 21
0
        public static Expression CompileExpression(this Compiler compiler, AstExpression expression, Namescope scope, DataType expectedType = null)
        {
            var fc     = new FunctionCompiler(compiler, scope);
            var result = fc.CompileExpression(expression);

            if (expectedType != null)
            {
                result = fc.CompileImplicitCast(expression.Source, expectedType, result);
            }

            return(result);
        }
Ejemplo n.º 22
0
 public void VerifyConstUsage(Source src, Member member, Namescope scope)
 {
     VerifyConstUsage(src, member, scope, IsObsolete(scope));
 }
Ejemplo n.º 23
0
        // TODO: Get rid of parent parameter
        public static bool TryTransformSetPropertyChainToSequence(this SetProperty s, Namescope scope, Statement parent, ref Expression result)
        {
            if (parent == null ||
                parent is SequenceOp && (parent as SequenceOp).Left == s)
            {
                return(false);
            }

            var ind = TryCreateIndirection(scope, ref s.Value);

            result = new SequenceOp(s, s.Value);

            if (ind != null)
            {
                result = new SequenceOp(ind, result);
            }

            return(true);
        }