/// <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; }
/// <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(); }
public string GetMemberName(Member m, Function f, Namescope parent = null) { return(IsConstrained(f) ? f.Name + "_ex" : GetMemberName(m) + GetTemplateSuffix(f, parent)); }
public string GetTemplateSuffix(DataType dt, Namescope parent) { return(dt.IsStruct ? GetTemplateString(GetTemplateArguments(dt), parent) : null); }
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); }
public string GetFunctionPointer(Function f, Namescope parent = null, string suffix = "_fn") { return(GetStaticName(f.DeclaringType, parent) + "__" + f.Name + suffix); }
public string GetTypeOfPointer(DataType dt, Namescope parent = null) { return("(uType*(*)())&" + GetStaticName(dt, parent) + "_typeof"); }
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; }
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"))); }); }
public string GetTypeOf(Parameter p, Namescope parent, string type, TypeCache?cache = null) { return(GetTypeOf(p.Type, parent, type, null, cache) + (p.IsReference ? "->ByRef()" : null)); }
protected GenericType(Source src, Namescope parent, string comment, Modifiers modifiers, string name) : base(src, parent, comment, modifiers, name) { }
public Element CreateElement(Source src, string value, Namescope scope) { return(new Element(src, value, 0, Compiler.NameResolver.GetUsings(scope, src))); }
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)); }
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); } } }
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; }
public string GetTemplateSuffix(Function f, Namescope parent) { return(GetTemplateString(GetTemplateArguments(f), parent)); }
public string GetTemplateString(DataType dt, Namescope parent) { return(GetTemplateString(GetTypeName(dt, parent, true))); }
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); }
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()"); } } }
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); }
public void VerifyConstUsage(Source src, Member member, Namescope scope) { VerifyConstUsage(src, member, scope, IsObsolete(scope)); }
// 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); }