Пример #1
0
 public CodeGenAnalyser(ISyntaxTreeNode astRoot, bool optimize)
 {
     _astRoot = astRoot;
     _currentLocalIdx = 0;
     _previousVarKind = VariableDeclaration.Kind.Class;
     _optimize = optimize;
 }
Пример #2
0
 public void VisitUnknown(ISyntaxTreeNode node)
 {
     if (UnknownVisitor != null)
     {
         UnknownVisitor.Visit(node);
     }
 }
Пример #3
0
        public void AddDebugSymbol(int address, ISyntaxTreeNode associatedNode)
        {
            Symbol range;

            _nodeToTokenMap.TryGetValue(associatedNode, out range);
            _addressToTokenMap[address] = range;
        }
Пример #4
0
 private void VisitPostOrder(ISyntaxTreeNode node)
 {
     if (_postOrderVisitor != null)
     {
         node.AcceptSyntaxTreeVisitor(_postOrderVisitor);
     }
 }
Пример #5
0
 public void ReplaceNode(ISyntaxTreeNode oldNode, ISyntaxTreeNode newNode)
 {
     if (_scopes.TryGetValue(oldNode, out NodeScopeInfo info))
     {
         _scopes[newNode] = info;
     }
 }
Пример #6
0
        public void SetScope(ISyntaxTreeNode node, Scope scope)
        {
            NodeScopeInfo info = new NodeScopeInfo {
                Scope = scope, DeclarationIndex = scope.Variables.Count
            };

            _scopes[node] = info;
        }
Пример #7
0
        private void AddAssigmentTypeMismatchError(IType lhsType, IType rhsType, ISyntaxTreeNode node)
        {
            string message = "Type mismatch between assignment operator.\n" +
                             $"  LHS: {lhsType.Represent()}\n" +
                             $"  RHS: {rhsType.Represent()}";

            _errors.AddError(message, node);
        }
Пример #8
0
 public NodeScopeInfo GetScope(ISyntaxTreeNode node)
 {
     if (!_scopes.TryGetValue(node, out NodeScopeInfo scope))
     {
         throw new ArgumentException("No scope associated with this node", nameof(node));
     }
     return(scope);
 }
Пример #9
0
        /// <summary>
        /// Deep equals.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as ArrayExpression;

            if (other == null)
            {
                return(false);
            }
            return(this.Elements.StructurallyEquals(other.Elements));
        }
Пример #10
0
        /// <summary>
        /// Deep equals.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as Query;

            if (other == null)
            {
                return(false);
            }
            return(this.Expression.StructurallyEquals(other.Expression));
        }
Пример #11
0
        /// <summary>
        /// Deep equals.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as BooleanLiteralExpression;

            if (other == null)
            {
                return(false);
            }
            return(this.Value == other.Value);
        }
Пример #12
0
        /// <summary>
        /// Deep equals.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as ParenthesisExpression;

            if (other == null)
            {
                return(false);
            }
            return(Expression.StructurallyEquals(other.Expression));
        }
Пример #13
0
        /// <summary>
        /// Deep equals.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as ArrayAccessExpression;

            if (other == null)
            {
                return(false);
            }

            return(this.Indices.StructurallyEquals(other.Indices));
        }
Пример #14
0
        /// <summary>
        /// Deep equals.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as UnaryOperationExpression;

            if (other == null)
            {
                return(false);
            }
            return(this.Operator == other.Operator &&
                   this.Expression.StructurallyEquals(other.Expression));
        }
Пример #15
0
        /// <summary>
        /// Deep equals for this syntax node.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as FunctionCallExpression;

            if (other == null)
            {
                return(false);
            }
            return(this.ThisExpression.StructurallyEquals(other.ThisExpression) &&
                   this.Parameters.StructurallyEquals(other.Parameters));
        }
Пример #16
0
        /// <summary>
        /// Deep equals.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as MemberExpression;

            if (other == null)
            {
                return(false);
            }
            return(this.ThisExpression.StructurallyEquals(other.ThisExpression) &&
                   this.MemberName == other.MemberName);
        }
Пример #17
0
        /// <summary>
        /// Deep equals.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as CastExpression;

            if (other == null)
            {
                return(false);
            }
            return(this.CastTypeName == other.CastTypeName &&
                   this.Expression.StructurallyEquals(other.Expression));
        }
Пример #18
0
        /// <summary>
        /// Deep equals.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as VariableExpression;

            if (other == null)
            {
                return(false);
            }

            return(other.Identifier == this.Identifier);
        }
Пример #19
0
        /// <summary>
        /// Deep equals.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool StructurallyEquals(ISyntaxTreeNode node)
        {
            var other = node as ConditionalExpression;

            if (other == null)
            {
                return(false);
            }
            return(this.Condition.StructurallyEquals(other.Condition) &&
                   this.Then.StructurallyEquals(other.Then) &&
                   this.Else.StructurallyEquals(other.Else));
        }
Пример #20
0
        public void VisitTypeSpecifier(TypeSpecifierParseNode node)
        {
            IType?type = _typeManager.GetType(node.Name, node.PointerMode);

            if (type == null)
            {
                _errors.AddError($"Undefined type name \"{node.Name}\"", node);
                return;
            }

            NewNode       = new TypeSpecifierNode(type);
            ShouldReplace = true;
        }
Пример #21
0
        internal Metadata SetCurrentDebugLocation(ISyntaxTreeNode node, bool forceDbgInfo = false)
        {
            if (_genContext.DebugInfo || forceDbgInfo)
            {
                _genContext.TryGetNodeSymbol(node, out Symbol range);
                Metadata location = _genContext.Context.CreateDebugLocation(range.LLVMLine,
                                                                            _genContext.ColumnInfo ? range.LLVMColumn : 0, _lexicalScope, Metadata.Null);
                _builder.SetCurrentDebugLocation(location);
                return(location);
            }

            return(Metadata.Null);
        }
Пример #22
0
        public TreeNodeViewModel(ISyntaxTreeNode node)
        {
            var moduleNode = node as CodeModule;

            _node = node;
            Icon = SetIcon();

            AttributeVisibility = Visibility.Collapsed;

            if (node is AttributeNode)
            {
                AttributeValue = (node as AttributeNode).Value;
                AttributeVisibility = Visibility.Visible;
            }
        }
Пример #23
0
 /// <summary>
 /// A private helper method to help check the declaration state of a property.
 /// </summary>
 /// <param name="node">Node.</param>
 /// <param name="property">Property.</param>
 /// <param name="declarationExpected">If set to <c>true</c> declaration expected.</param>
 private void checkPropertyDeclared(ISyntaxTreeNode node, IProperty property, bool declarationExpected)
 {
     if (declarationExpected)
     {
         if (!property.Declared)
         {
             // if the declaration was expected, report an error about an uninitialized variable
             analyzer.notifyError(new UninitializedVariableError(node));
         }
     }
     else
     {
         if (property.Declared)
         {
             // if the declaration was not expected, report an error about a repeated declaration of a variable
             analyzer.notifyError(new DeclarationError(node));
         }
     }
 }
Пример #24
0
        public void Visit(AssignmentParseNode node)
        {
            IAddressableNode?assignableNode = node.LHS as IAddressableNode;

            if (assignableNode == null || !assignableNode.IsAddressable())
            {
                _errors.AddError("Left hand side of assignment is not assignable.", node);
                return;
            }

            _shouldReplace = true;
            _newNode       = new AssignmentNode(assignableNode, node.RHS);

            // TODO: Need more automated symbol association for new nodes.
            Symbol originalSymbol;

            _semanticModule.SymbolMap.TryGetValue(node, out originalSymbol);
            _semanticModule.SymbolMap[_newNode] = originalSymbol;
        }
Пример #25
0
        public override string ToString()
        {
            if (Value is null)
            {
                return($"Entry point:\r\n{string.Join("\r\n", Nodes)}");
            }

            var             parents  = 0;
            ISyntaxTreeNode currNode = this;

            while (currNode.Parent != null)
            {
                parents++;
                currNode = currNode.Parent;
            }

            var tab = new string('\t', parents);

            return($"{tab}{Value.Type}: {Value.Value}\r\n{string.Join("\r\n", Nodes)}");
        }
Пример #26
0
        public void Visit(AccessParseNode node)
        {
            IType type = _expressionTypeManager.GetExpressionType(node.Lhs);

            if (!(type is StructType structType))
            {
                _errors.AddError($"Type '{type.Represent()}' has no accessible members.", node);
                return;
            }

            DeclarationNode?declaration = structType.Struct.Members.Find(decl => decl.Name == node.Rhs.Name);

            if (declaration == null)
            {
                _errors.AddError($"No such member {node.Rhs.Name} in struct {structType.Struct.Name}.", node.Rhs);
                return;
            }

            NewNode       = new AccessNode(node.Lhs, declaration);
            ShouldReplace = true;
        }
Пример #27
0
        public void Visit(IdentifierParseNode node)
        {
            ShouldReplace = true;

            NodeScopeInfo   nodeScopeInfo = _scopeManager.GetScope(node);
            DeclarationNode?decl          = nodeScopeInfo.Scope.FindNearestDeclaration(node.Name, nodeScopeInfo.DeclarationIndex);

            if (decl != null)
            {
                NewNode = UpdateSymbolMap(new VariableNode(decl), node);
                return;
            }

            if (_semanticModule.EnumInfo.TryGetValue(node.Name, out EnumDeclarationInfo identifierInfo))
            {
                NewNode = UpdateSymbolMap(new EnumValueNode(identifierInfo.Declaration), node);
                return;
            }

            ShouldReplace = false;
            _errors.AddError($"Undeclared identifier {node.Name}", node);
        }
        private void AssertLifetime(IType?lhsType, Scope?lhs, Scope?rhs, ISyntaxTreeNode context)
        {
            if (lhsType is IPointerType rhsPointerType)
            {
                if (rhsPointerType.Mode != PointerMode.Borrowed)
                {
                    return;
                }
            }
            else
            {
                return;
            }

            if (lhs == null || rhs == null)
            {
                return;
            }

            if (lhs.Outlives(rhs))
            {
                _errors.AddError("Incompatible lifetime.", context);
            }
        }
Пример #29
0
        public void Visit(FunctionCallParseNode node)
        {
            IdentifierParseNode?identifier = node.LHS as IdentifierParseNode;

            if (identifier == null)
            {
                _errors.AddError("LHS of function call operator is not an identifier.", node);
                return;
            }

            ShouldReplace = true;

            FunctionCallNode?resultNode = null;

            if (!_semanticModule.Functions.TryGetValue(identifier.Name, out FunctionDefinitionNode function))
            {
                _errors.AddError("Undefined function " + identifier.Name, node);
            }
            else if (function.Parameters.Length != node.Arguments.Count)
            {
                _errors.AddError($"Expected {function.Parameters.Length} argument(s), got {node.Arguments.Count}", node);
                return;
            }
            else
            {
                resultNode = new FunctionCallNode(function, node.Arguments);
            }

            if (resultNode == null)
            {
                resultNode = MakeFakeFunctionCall(identifier);
            }

            UpdateSymbolMap(resultNode, node);
            NewNode = resultNode;
        }
Пример #30
0
 /// <summary>
 /// Deep equals
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 public bool StructurallyEquals(ISyntaxTreeNode node)
 {
     return(node as NullExpression != null);
 }
Пример #31
0
 public UninitializedVariableError(ISyntaxTreeNode node)
     : base(ErrorConstants.UNINITIALIZED_VARIABLE_ERROR_MESSAGE, node)
 {
 }
Пример #32
0
 private void HandleExpressionOrStatementNode(ISyntaxTreeNode node)
 {
     node.Scope = CurrentScope;
 }
Пример #33
0
 public void VisitUnknown(ISyntaxTreeNode node)
 {
     Print("(Unknown Syntax Tree Node)");
 }
Пример #34
0
 public ModuleNode(ISyntaxTreeNode header, IEnumerable<ISyntaxTreeNode> declarations, IEnumerable<ISyntaxTreeNode> members)
     : base(SyntaxTreeNodeType.ModuleNode, header.Nodes.OfType<AttributeNode>().FirstOrDefault(node => node.NodeName == "VB_Name").Value)
 {
     var nodes = header.Nodes.Concat(declarations).Concat(members);
     foreach (var node in nodes)
     {
         Nodes.Add(node);
     }
 }