// [2] Declaration = DeclarationSpecifier [Declarator] ';'. private bool ParseDeclaration(out DeclarationSyntax declaration) { declaration = new DeclarationSyntax(); if (!ParseDeclarationSpecifier(out var declarationSpecifier)) { return(false); } declaration.DeclarationSpecifier = declarationSpecifier; if (IsDeclarator(out var declarator)) { declaration.Declarator = declarator; } if (!CheckToken(SyntaxKind.SemiColonToken)) { return(false); } declaration.AddChild(declarationSpecifier); declaration.AddChild(declarator); return(true); }
protected virtual void VisitDeclarationSyntax(DeclarationSyntax pNode) { foreach (var v in pNode.Variables) { Visit(v); } Visit(pNode.Value); }
protected override void VisitDeclarationSyntax(DeclarationSyntax pNode) { if (pNode.IsConst && !IsConstant(null, pNode.Value)) { CompilerErrors.ConstantNotConstantValue(pNode.Value.Span); } base.VisitDeclarationSyntax(pNode); }
private static string NameInScope(int depth, DeclarationSyntax syntax) { return(syntax.Match().Returning <SyntaxToken>() .With <NamespaceSyntax>(ns => ns.Names[depth]) .With <EntitySyntax>(e => e.Name) .Exhaustive() .ValueText); }
public override void VisitDeclaration(DeclarationSyntax declaration, Void args) { // Skip poisoned declarations if (declaration?.Poisoned ?? true) { return; } base.VisitDeclaration(declaration, args); }
protected virtual SyntaxNode VisitDeclarationSyntax(DeclarationSyntax pNode) { List <IdentifierSyntax> variables = new List <IdentifierSyntax>(pNode.Variables.Count); foreach (var v in pNode.Variables) { variables.Add((IdentifierSyntax)Visit(v)); } return(SyntaxFactory.Declaration(pNode.IsConst, variables, Visit(pNode.Value))); }
public void Build(DeclarationSyntax declaration, ContainerBinder containingScope) { declaration.Match() .With <NamespaceSyntax>(@namespace => { var imports = @namespace.UsingDirectives.SelectMany(u => GatherImportedSymbols(u, containingScope)); var namesCount = @namespace.Names.Count; for (var i = 0; i < namesCount; i++) { var name = @namespace.Names[i]; var last = i == namesCount - 1; var reference = containingScope.GetMembers(name.ValueText).OfType <NamespaceReference>().Single(); containingScope = new NamespaceBinder(containingScope, reference, last ? imports : Enumerable.Empty <ImportedSymbol>()); // The innermost binder is the one that has the imports and should be associated with the syntax node if (last) { binders.Add(@namespace, containingScope); } } foreach (var member in @namespace.Members) { Build(member, containingScope); } }) .With <ClassSyntax>(@class => { var scope = new ClassBinder(containingScope, @class); binders.Add(@class, scope); foreach (var member in @class.Members) { Build(member, scope); } }) .With <FunctionSyntax>(function => { foreach (var parameter in function.Parameters) { Build(parameter.Type.Type, containingScope); } // TODO deal with return type Binder scope = new FunctionBinder(containingScope); binders.Add(function, scope); foreach (var statement in function.Body) { scope = Build(statement, scope); } }) // .With<VariableDeclaration>(global => // { // global.Type.BindNames(scope); // global.InitExpression?.BindNames(scope); // }) .Exhaustive(); }
protected override SyntaxNode VisitDeclarationSyntax(DeclarationSyntax pNode) { if (pNode.IsConst) { foreach (var v in pNode.Variables) { _locals.DefineVariableInScope(v.Value, pNode.Value); } } return(base.VisitDeclarationSyntax(pNode)); }
protected override void VisitDeclarationSyntax(DeclarationSyntax pNode) { if (!pNode.IsConst) { foreach (var v in pNode.Variables) { Visit(v); _locals.DefineVariableInScope(v.Value, new LocalReference(v.Span, v.Value, false)); } Visit(pNode.Value); } }
public virtual void VisitDeclaration(DeclarationSyntax node) { this.BeginProperty(node, name: "Declarations"); try { this.Visit(node.EnumDeclaration); this.Visit(node.ClassDeclaration); this.Visit(node.AssociationDeclaration); this.Visit(node.ConstDeclaration); } finally { this.EndProperty(); } }
public void Render(DeclarationSyntax declaration) { if (declaration.TypeDefinition != null) { Render(declaration.TypeDefinition); } else if (declaration.GlobalsBlock != null) { Render(declaration.GlobalsBlock); } else { Render(declaration.NativeFunctionDeclaration); } }
protected override void VisitDeclarationSyntax(DeclarationSyntax pNode) { Visit(pNode.Value); var isTuple = pNode.Value.Type.IsTuple; for (int i = 0; i < pNode.Variables.Count; i++) { if (!SyntaxHelper.IsDiscard(pNode.Variables[i])) { if (_locals.IsVariableDefinedInScope(pNode.Variables[i].Value)) { CompilerErrors.IdentifierAlreadyDeclared(pNode.Variables[i], pNode.Span); } else { //We do not allow variables to have the same names as types //This makes it easier to check for "static" method/fields if (SmallTypeCache.IsTypeDefined(pNode.Variables[i].Value)) { CompilerErrors.ValueDefinedAsType(pNode.Variables[i], pNode.Variables[i].Span); } else { //For tuple types we set the individual variables to the tuple field type... not the tuple itself var t = isTuple ? pNode.Value.Type.GetFieldType(i) : pNode.Value.Type; //Report expression errors and change the type to Undefined so we don't get further no expression errors if (pNode.Value.Type == SmallTypeCache.NoValue) { CompilerErrors.ExpressionNoValue(pNode.Value.Span); t = SmallTypeCache.Undefined; } pNode.Variables[i].SetType(t); _locals.DefineVariableInScope(pNode.Variables[i].Value, LocalDefinition.Create(pNode.IsConst, pNode.Variables[i].Type)); } } } } //Check that we are declaring the proper number of variables if (isTuple && pNode.Value.Type.GetFieldCount() != pNode.Variables.Count) { CompilerErrors.DeclarationCountMismatch(pNode.Value.Type.GetFieldCount(), pNode.Variables.Count, pNode.Span); } }
private void ResolveSignatureTypesInDeclaration(DeclarationSyntax declaration) { switch (declaration) { case FunctionDeclarationSyntax f: ResolveSignatureTypesInFunction(f); break; case TypeDeclarationSyntax t: ResolveSignatureTypesInTypeDeclaration(t); break; case FieldDeclarationSyntax f: ResolveSignatureTypesInField(f); break; default: throw NonExhaustiveMatchException.For(declaration); } }
public void Build(DeclarationSyntax declaration, ContainerBinder containingScope) { declaration.Match() .With<NamespaceSyntax>(@namespace => { var imports = @namespace.UsingDirectives.SelectMany(u => GatherImportedSymbols(u, containingScope)); var namesCount = @namespace.Names.Count; for(var i = 0; i < namesCount; i++) { var name = @namespace.Names[i]; var last = i == namesCount - 1; var reference = containingScope.GetMembers(name.ValueText).OfType<NamespaceReference>().Single(); containingScope = new NamespaceBinder(containingScope, reference, last ? imports : Enumerable.Empty<ImportedSymbol>()); // The innermost binder is the one that has the imports and should be associated with the syntax node if(last) binders.Add(@namespace, containingScope); } foreach(var member in @namespace.Members) Build(member, containingScope); }) .With<ClassSyntax>(@class => { var scope = new ClassBinder(containingScope, @class); binders.Add(@class, scope); foreach(var member in @class.Members) Build(member, scope); }) .With<FunctionSyntax>(function => { foreach(var parameter in function.Parameters) Build(parameter.Type.Type, containingScope); // TODO deal with return type Binder scope = new FunctionBinder(containingScope); binders.Add(function, scope); foreach(var statement in function.Body) scope = Build(statement, scope); }) // .With<VariableDeclaration>(global => // { // global.Type.BindNames(scope); // global.InitExpression?.BindNames(scope); // }) .Exhaustive(); }
public virtual void VisitDeclaration(DeclarationSyntax declaration, A args) { switch (declaration) { case MemberDeclarationSyntax memberDeclaration: VisitMemberDeclaration(memberDeclaration, args); break; case NamespaceDeclarationSyntax namespaceDeclaration: VisitNamespaceDeclaration(namespaceDeclaration, args); break; case null: // Ignore break; default: throw NonExhaustiveMatchException.For(declaration); } }
public virtual void VisitDeclaration(DeclarationSyntax node) { this.BeginProperty("Declarations"); try { this.Visit(node.EnumDeclaration); this.Visit(node.StructDeclaration); this.Visit(node.DatabaseDeclaration); this.Visit(node.InterfaceDeclaration); this.Visit(node.ComponentDeclaration); this.Visit(node.CompositeDeclaration); this.Visit(node.AssemblyDeclaration); this.Visit(node.BindingDeclaration); this.Visit(node.EndpointDeclaration); this.Visit(node.DeploymentDeclaration); } finally { this.EndProperty(); } }
private void BuildScopesInDeclaration( LexicalScope containingScope, DeclarationSyntax declaration) { var binder = new ExpressionLexicalScopesBuilder(); var diagnosticCount = diagnostics.Count; switch (declaration) { case NamespaceDeclarationSyntax ns: { if (ns.InGlobalNamespace) { containingScope = globalScope; } containingScope = BuildNamespaceScopes(containingScope, ns.Name); containingScope = BuildUsingDirectivesScope(containingScope, ns.UsingDirectives); foreach (var nestedDeclaration in ns.Declarations) { BuildScopesInDeclaration(containingScope, nestedDeclaration); } } break; case NamedFunctionDeclarationSyntax function: BuildScopesInFunctionParameters(containingScope, function, binder); binder.VisitExpression(function.ReturnTypeExpression, containingScope); BuildScopesInFunctionBody(containingScope, function, binder); break; case OperatorDeclarationSyntax operatorDeclaration: BuildScopesInFunctionParameters(containingScope, operatorDeclaration, binder); binder.VisitExpression(operatorDeclaration.ReturnTypeExpression, containingScope); BuildScopesInFunctionBody(containingScope, operatorDeclaration, binder); break; case ConstructorDeclarationSyntax constructor: BuildScopesInFunctionParameters(containingScope, constructor, binder); BuildScopesInFunctionBody(containingScope, constructor, binder); break; case InitializerDeclarationSyntax initializer: BuildScopesInFunctionParameters(containingScope, initializer, binder); BuildScopesInFunctionBody(containingScope, initializer, binder); break; case TypeDeclarationSyntax typeDeclaration: // TODO name scope for type declaration foreach (var nestedDeclaration in typeDeclaration.Members) { BuildScopesInDeclaration(containingScope, nestedDeclaration); } break; case FieldDeclarationSyntax fieldDeclaration: binder.VisitExpression(fieldDeclaration.TypeExpression, containingScope); binder.VisitExpression(fieldDeclaration.Initializer, containingScope); break; default: throw NonExhaustiveMatchException.For(declaration); } if (diagnosticCount != diagnostics.Count) { declaration.Poison(); } }
protected Declaration(DeclarationSyntax syntax, Package containingPackage, Namespace containingNamespace, Accessibility declaredAccessibility, string name) : this(syntax.Yield(), containingPackage, containingNamespace, declaredAccessibility, name) { }
protected override SyntaxNode VisitForSyntax(ForSyntax pNode) { //Rewrite for statements with iterator arrays to normal for statements if (pNode.Iterator != null) { var i = SyntaxFactory.Identifier("!i"); i.SetType(SmallTypeCache.Int); var postOp = pNode.Reverse ? UnaryExpressionOperator.PostDecrement : UnaryExpressionOperator.PostIncrement; UnaryExpressionSyntax finalizer = SyntaxFactory.UnaryExpression(i, postOp); SyntaxNode end = null; DeclarationSyntax decl = null; //Save itvar in case we are looping in a case body var rw = _rewrite; var it = _itVar; //Declare our iterator outside the for loop //This will help if our iterator is complex like a function call var iterVar = SyntaxFactory.Identifier("!iter"); iterVar.SetType(pNode.Iterator.Type); var iterDecl = SyntaxFactory.SingleDeclaration(iterVar, pNode.Iterator); if (pNode.Iterator.Type.IsArray) { //We are iterating over an array //Reverse loops will start at Array.Length and decrement to 0 //Normal loops will start at 0 and increment to Array.Length if (pNode.Reverse) { var length = SyntaxFactory.UnaryExpression(iterVar, UnaryExpressionOperator.Length); length.SetType(SmallTypeCache.Int); decl = SyntaxFactory.SingleDeclaration(i, SyntaxFactory.BinaryExpression(length, BinaryExpressionOperator.Subtraction, SyntaxFactory.NumericLiteral(1))); end = SyntaxFactory.NumericLiteral(0); } else { decl = SyntaxFactory.SingleDeclaration(i, SyntaxFactory.NumericLiteral(0)); end = SyntaxFactory.UnaryExpression(iterVar, UnaryExpressionOperator.Length); ((UnaryExpressionSyntax)end).SetType(SmallTypeCache.Int); } _itVar = SyntaxFactory.ArrayAccess(iterVar, i); ((ArrayAccessSyntax)_itVar).SetType(iterVar.Type); } else if (pNode.Iterator.Type.IsAssignableFrom(_enumerable)) { //We are iterating over an enumerable //Reverse loops will start at Count and decrement to 0 //Normal loops will start at 0 and increment to Count if (pNode.Reverse) { var count = SyntaxFactory.MemberAccess(iterVar, SyntaxFactory.Identifier("Count")); decl = SyntaxFactory.SingleDeclaration(i, SyntaxFactory.BinaryExpression(count, BinaryExpressionOperator.Subtraction, SyntaxFactory.NumericLiteral(1))); end = SyntaxFactory.NumericLiteral(0); } else { decl = SyntaxFactory.SingleDeclaration(i, SyntaxFactory.NumericLiteral(0)); end = SyntaxFactory.MemberAccess(iterVar, SyntaxFactory.Identifier("Count")); } _itVar = SyntaxFactory.MemberAccess(iterVar, SyntaxFactory.MethodCall("ItemAt", new List <SyntaxNode>() { SyntaxFactory.Identifier("!i") })); } else { //Some bad type. We can't rewrite if it isn't array or enumerable return(base.VisitForSyntax(pNode)); } var op = pNode.Reverse ? BinaryExpressionOperator.GreaterThanOrEqual : BinaryExpressionOperator.LessThan; var condition = SyntaxFactory.BinaryExpression(i, op, end); condition.SetType(SmallTypeCache.Boolean); var body = (BlockSyntax)Visit(pNode.Body); var forStatement = SyntaxFactory.For(new List <DeclarationSyntax>() { decl }, condition, new List <SyntaxNode>() { finalizer }, body); //Restore our it for any other nested loops _itVar = it; _rewrite = rw; //Return our iterator declaration and for rewrite return(SyntaxFactory.Block(new List <SyntaxNode>() { iterDecl, forStatement })); } return(base.VisitForSyntax(pNode)); }
public static void VisitDeclaration(this DeclarationVisitor <Void> visitor, DeclarationSyntax declaration) { visitor.VisitDeclaration(declaration, default); }