public AstStructRef(AstStructTypeExpr @struct, AstExpression original, ILocation Location = null)
     : base(Location)
 {
     Declaration   = @struct;
     Type          = @struct.Type;
     this.Original = original;
     Value         = @struct;
 }
Exemple #2
0
 public override object VisitStructTypeExpr(AstStructTypeExpr type, List <SymbolInformation> list)
 {
     list.Add(new SymbolInformation
     {
         containerName = type.Scope.Name,
         kind          = SymbolKind.Class,
         location      = CastLocation(type.Location),
         name          = type.Name
     });
     return(default);
Exemple #3
0
        public override string VisitStructTypeExpr(AstStructTypeExpr expr, int data = default(int))
        {
            var body = string.Join("\n", expr.Declarations.Select(m => m.Accept(this)));
            var head = $"struct";

            if (expr.Parameters != null && expr.Parameters.Count > 0)
            {
                head += $"({string.Join(", ", expr.Parameters.Select(p => p.Accept(this)))})";
            }
            // return $"{head} {{\n{body.Indent(4)}\n}}";
            return(head);
        }
        public override string VisitStructTypeExpr(AstStructTypeExpr str, int data = 0)
        {
            if (str.IsPolymorphic)
            {
                var body = string.Join("\n", str.Declarations.Select(m => m.Accept(this)));
                var head = $"struct";

                head += "(";
                head += string.Join(", ", str.Parameters.Select(p => $"{p.Name.Accept(this)}: {p.Type}"));
                head += ")";

                if (str.TraitExpr != null)
                {
                    head += " " + str.TraitExpr.Type;
                }

                var sb = new StringBuilder();
                sb.Append($"{head} {{\n{body.Indent(4)}\n}}");

                // polies
                if (str.PolymorphicInstances?.Count > 0)
                {
                    sb.AppendLine();
                    sb.AppendLine($"// Polymorphic instances for {head}");
                    foreach (var pi in str.PolymorphicInstances)
                    {
                        var args = string.Join(", ", pi.Parameters.Select(p => $"{p.Name.Accept(this)} = {p.Value}"));
                        sb.AppendLine($"// {args}".Indent(4));
                        sb.AppendLine(pi.Accept(this).Indent(4));
                    }
                }

                return(sb.ToString());
            }
            else
            {
                var body = string.Join("\n", str.Members.Select(m => m.Decl.Accept(this)));
                //var body = string.Join("\n", str.Declarations.Select(m => m.Accept(this)));
                var head = $"struct";

                if (str.TraitExpr != null)
                {
                    head += " " + str.TraitExpr.Type;
                }

                var sb = new StringBuilder();
                sb.Append($"{head} {{ // size: {str.StructType?.GetSize()}, alignment: {str.StructType?.GetAlignment()}\n{body.Indent(4)}\n}}");

                return(sb.ToString());
            }
        }
Exemple #5
0
        private void ComputeStructMemberSizes(AstStructTypeExpr expr)
        {
            if (expr.TypesComputed)
            {
                return;
            }
            SetupStructMembers(expr);

            foreach (var m in expr.Members)
            {
                ComputeStructMember(m, false);
                if (expr.StructType.IsCopy && !m.Decl.Type.IsCopy)
                {
                    ReportError(m.Decl, "Member is not copyable");
                }
            }

            expr.TypesComputed = true;
        }
Exemple #6
0
 private void AddStruct(AstStructTypeExpr str)
 {
     mAllStructs.Add(str);
     mUnresolvedStructs.Enqueue(str);
 }
Exemple #7
0
        private void SetupStructMembers(AstStructTypeExpr expr)
        {
            if (expr.Members != null)
            {
                return;
            }
            expr.Members = new List <AstStructMemberNew>();

            if (expr.BaseTrait != null)
            {
                // add all members of the trait
                ComputeTypeMembers(expr.BaseTrait);
                foreach (var m in expr.BaseTrait.Declaration.Members)
                {
                    var clone = m.Decl.Clone() as AstVariableDecl;
                    expr.Members.Add(new AstStructMemberNew(clone, true, false, expr.Members.Count));
                }

                // no functions, add impl automatically
                if (expr.BaseTrait.Declaration.Functions.Count == 0)
                {
                    expr.Traits.Add(expr.BaseTrait);
                    //AddTraitForType(expr.StructType,
                    //    new AstImplBlock(
                    //        new List<AstParameter>(),
                    //        expr,
                    //        expr.TraitExpr,
                    //        new List<ImplCondition>(),
                    //        new List<AstDecl>(),
                    //        expr.TraitExpr));

                    var impl = new AstImplBlock(null, expr, expr.TraitExpr, null, null, expr.TraitExpr);
                    impl.TargetType = expr.StructType;
                    impl.Trait      = expr.BaseTrait;
                    AddTraitForType(expr.StructType, impl);
                    expr.BaseTrait.Declaration.Implementations[expr.StructType] = impl;
                }
            }

            if (expr.TryGetDirective("extend", out var dir))
            {
                if (dir.Arguments.Count != 1)
                {
                    ReportError(dir, $"Must have one type argument");
                }
                else
                {
                    var arg = dir.Arguments[0];
                    arg.AttachTo(expr);
                    arg = ResolveTypeNow(arg, out var type);

                    if (type is StructType str)
                    {
                        if (str.Declaration.Extendable)
                        {
                            expr.Extends = str;
                        }
                        else
                        {
                            ReportError(arg, $"Type '{str}' is not extendable");
                        }
                    }
                    else if (!type.IsErrorType)
                    {
                        ReportError(arg, $"Argument must be a struct type");
                    }
                }
            }
            if (expr.Extendable || expr.Extends != null)
            {
                var mem = mCompiler.ParseStatement(
                    $"__type_ptr__ : &TypeInfo = @type_info(§self)",
                    new Dictionary <string, AstExpression>
                {
                    { "self", new AstTypeRef(expr.StructType, expr) }
                }) as AstVariableDecl;
                mem.Parent = expr;
                mem.Scope  = expr.SubScope;
                expr.Members.Add(new AstStructMemberNew(mem, false, true, expr.Members.Count));
            }

            if (expr.Extends != null)
            {
                // skip type_info of parent
                ComputeTypeMembers(expr.Extends);
                foreach (var m in expr.Extends.Declaration.Members.Skip(1))
                {
                    var clone = m.Decl.Clone() as AstVariableDecl;
                    expr.Members.Add(new AstStructMemberNew(clone, true, false, expr.Members.Count));
                }
            }

            foreach (var decl in expr.Declarations)
            {
                if (decl is AstVariableDecl mem)
                {
                    expr.Members.Add(new AstStructMemberNew(mem, true, false, expr.Members.Count));
                }
            }
        }
Exemple #8
0
 private AstStructTypeExpr InstantiatePolyStruct(AstStructTypeExpr decl, List <(CheezType type, object value)> args, ILocation location = null)
Exemple #9
0
        private AstExpression InferTypeStructTypeExpr(AstStructTypeExpr expr)
        {
            if (expr.IsPolyInstance)
            {
            }
            else
            {
                expr.SubScope = new Scope("struct", expr.Scope);
                if (expr.Parent is AstConstantDeclaration c)
                {
                    expr.Name = c.Name.Name;
                }
            }

            bool isCopy = expr.HasDirective("copy");

            if (expr.IsPolymorphic)
            {
                // @todo
                foreach (var p in expr.Parameters)
                {
                    p.Scope          = expr.Scope;
                    p.TypeExpr.Scope = expr.Scope;
                    p.TypeExpr       = ResolveTypeNow(p.TypeExpr, out var t);
                    p.Type           = t;

                    ValidatePolymorphicParameterType(p, p.Type);

                    expr.SubScope.DefineTypeSymbol(p.Name.Name, new PolyType(p.Name.Name, true));
                }

                expr.Type  = CheezType.Type;
                expr.Value = new GenericStructType(expr, isCopy, expr.Name);
                return(expr);
            }

            // not polymorphic
            if (expr.TraitExpr != null)
            {
                expr.TraitExpr.AttachTo(expr, expr.SubScope);
                expr.TraitExpr = InferType(expr.TraitExpr, CheezType.Type);
                if (!expr.TraitExpr.Type.IsErrorType)
                {
                    var type = expr.BaseTrait;
                    if (type == null)
                    {
                        ReportError(expr.TraitExpr, $"Expected trait type, got '{expr.TraitExpr.Value}'");
                    }
                }
            }
            expr.Extendable = expr.HasDirective("extendable");
            if (expr.HasDirective("extendable") || expr.HasDirective("extend"))
            {
                ReportError(expr, "#extendable/#extend no longer supported");
            }

            foreach (var decl in expr.Declarations)
            {
                decl.Scope = expr.SubScope;

                if (decl is AstConstantDeclaration con)
                {
                    AnalyseConstantDeclaration(con);
                }
            }

            expr.Type  = CheezType.Type;
            expr.Value = new StructType(expr, isCopy, expr.Name);
            AddStruct(expr);
            // mTypesRequiredAtRuntimeQueue.Enqueue(expr.StructType);
            return(expr);
        }
Exemple #10
0
 public virtual ReturnType VisitStructTypeExpr(AstStructTypeExpr expr, DataType data       = default) => default;