Пример #1
0
        public void AddField(AstVariableDefinition variable)
        {
            var field = new CSharp.Field(variable.Identifier.SymbolName.CanonicalName.FullName, variable.TypeReference.ToCode())
            {
                AccessModifiers = AccessModifiers.Private,
                FieldModifiers  = FieldModifiers.Static,
            };

            _moduleClass.AddField(field);
        }
Пример #2
0
        public override void VisitAssignment(AstAssignment assign)
        {
            Ast.Guard(assign.HasVariable, "AstVariable not set on assign.");
            assign.VisitChildren(this);

            if (assign.Variable is AstVariableReference varRef &&
                varRef.VariableDefinition is null)
            {
                var symbol = varRef.Symbol;

                AstTypeReference?typeRef = null;
                // Variable.TypeReference is usually null.
                if (varRef.HasTypeReference)
                {
                    typeRef = varRef.TypeReference.MakeCopy();
                }

                // It is set only when type is explicitly in source code, or has been inferred.
                var varDef = new AstVariableDefinition(typeRef);
                varDef.SetIdentifier(varRef.Identifier);
                varDef.SetSymbol(symbol);
                symbol.RemoveReference(varRef);
                AstSymbolReferenceRemover.RemoveReference(varRef);
                symbol.AddNode(varDef);
                assign.SetVariableDefinition(varDef);

                // do the children again for types
                assign.VisitChildren(this);
            }

            if (!assign.Variable !.HasTypeReference)
            {
                // typeless assign of var (x = 42)
                var expr    = assign.Expression;
                var typeRef = expr.TypeReference;
                Ast.Guard(typeRef.HasIdentifier, "AstIdentifier not set on AstTypeReference.");

                var symbol = assign.Variable.Symbol;

                var def = symbol.DefinitionAs <AstTypeDefinition>();
                if (def is not null)
                {
                    typeRef = AstTypeReferenceType.From(def);
                    assign.Variable.SetTypeReference(typeRef);
                }
                else
                {
                    assign.Variable.SetTypeReference(typeRef.MakeCopy());
                }

                Visit(assign.Variable.TypeReference);
            }
        }
Пример #3
0
        public override void VisitVariableDefinition(AstVariableDefinition variable)
        {
            if (variable.IsTopLevel())
            {
                Context.ModuleClass.AddField(variable);
            }
            else
            {
                Context.CodeBuilder.AddVariable(variable);
            }

            if (variable.ParentAs <AstAssignment>() is null)
            {
                // prevent 'use of unassigned variable' error
                Context.CodeBuilder.CsBuilder.EndLine(" = default");
            }
        }
Пример #4
0
 public void MapDefinition(AstVariableDefinition variableAst, VariableDefinition variable)
 {
     locals.Add(variableAst, variable);
 }
Пример #5
0
 public AstVariableReference(AstVariableDefinition variable)
 {
     this.Variable = variable;
 }
 public override void VisitVariableDefinition(AstVariableDefinition variable)
 {
     variable.TypeReference.Should().NotBeNull();
     variable.VisitChildren(this);
 }
Пример #7
0
 public void AddVariable(AstVariableDefinition variable)
 {
     _csBuilder.StartVariable(variable.TypeReference.ToCode(), variable.Identifier.SymbolName.CanonicalName.FullName);
 }
Пример #8
0
 protected virtual void AppendVariableDefinition(StringBuilder builder, AstVariableDefinition variableDefinition)
 {
     builder.Append(variableDefinition);
 }
Пример #9
0
 public virtual void VisitVariableDefinition(AstVariableDefinition variable)
 {
     variable.VisitChildren(this);
 }