private void Visit(
            SimpleNameExpression expression,
            QualifiedModuleName module,
            Declaration scope,
            Declaration parent,
            bool isAssignmentTarget,
            bool hasExplicitLetStatement,
            bool isSetAssignment)
        {
            var callSiteContext = expression.Context;
            var callee          = expression.ReferencedDeclaration;
            var identifier      = WithEnclosingBracketsRemoved(callSiteContext.GetText());
            var selection       = callSiteContext.GetSelection();

            expression.ReferencedDeclaration.AddReference(
                module,
                scope,
                parent,
                callSiteContext,
                identifier,
                callee,
                selection,
                FindIdentifierAnnotations(module, selection.StartLine),
                isAssignmentTarget,
                hasExplicitLetStatement,
                isSetAssignment);
        }
        private void Visit(
            SimpleNameExpression expression,
            QualifiedModuleName module,
            Declaration scope,
            Declaration parent,
            bool isAssignmentTarget,
            bool hasExplicitLetStatement,
            bool isSetAssignment)
        {
            if (isAssignmentTarget && expression.Context.Parent is VBAParser.IndexExprContext && !expression.ReferencedDeclaration.IsArray)
            {
                // 'SomeDictionary' is not the assignment target in 'SomeDictionary("key") = 42'
                // ..but we want to treat array index assignment as assignment to the array itself.
                isAssignmentTarget = false;
                isSetAssignment    = false;
            }

            var callSiteContext = expression.Context;
            var identifier      = expression.Context.GetText();
            var callee          = expression.ReferencedDeclaration;

            expression.ReferencedDeclaration.AddReference(
                module,
                scope,
                parent,
                callSiteContext,
                identifier,
                callee,
                callSiteContext.GetSelection(),
                _annotationService.FindAnnotations(module, callSiteContext.GetSelection().StartLine),
                isAssignmentTarget,
                hasExplicitLetStatement,
                isSetAssignment);
        }
예제 #3
0
 public override INode VisitSimpleNameExpression(SimpleNameExpression simpleNameExpression)
 {
     return(new SimpleNameExpression(simpleNameExpression.Context, simpleNameExpression.Name)
     {
         Type = simpleNameExpression.Type,
         Declarations = simpleNameExpression.Declarations
     });
 }
예제 #4
0
        void CheckGenericInvoke(InvocationExpression expr)
        {
            Assert.AreEqual(1, expr.Arguments.Count);
            Assert.IsTrue(expr.TargetObject is SimpleNameExpression);
            SimpleNameExpression ident = (SimpleNameExpression)expr.TargetObject;

            Assert.AreEqual("myMethod", ident.Identifier);
            Assert.AreEqual(1, ident.TypeArguments.Count);
            Assert.AreEqual("System.Char", ident.TypeArguments[0].Type);
        }
예제 #5
0
		public void IdentifierOnlyInvocation()
		{
			// InitializeComponents();
			SimpleNameExpression identifier = new SimpleNameExpression("InitializeComponents");
			InvocationExpression invocation = new InvocationExpression(identifier, new List<Expression>());
			object output = invocation.AcceptVisitor(new CodeDomVisitor(), null);
			Assert.IsTrue(output is CodeMethodInvokeExpression);
			CodeMethodInvokeExpression mie = (CodeMethodInvokeExpression)output;
			Assert.AreEqual("InitializeComponents", mie.Method.MethodName);
			Assert.IsTrue(mie.Method.TargetObject is CodeThisReferenceExpression);
		}
예제 #6
0
        public void VBInvocationOnGenericType()
        {
            InvocationExpression      expr = ParseUtil.ParseExpression <InvocationExpression>("A(Of T).Foo()");
            MemberReferenceExpression mre  = (MemberReferenceExpression)expr.TargetObject;

            Assert.AreEqual("Foo", mre.MemberName);
            SimpleNameExpression tre = (SimpleNameExpression)mre.TargetObject;

            Assert.AreEqual("A", tre.Identifier);
            Assert.AreEqual("T", tre.TypeArguments[0].Type);
        }
예제 #7
0
        public void IdentifierOnlyInvocation()
        {
            // InitializeComponents();
            SimpleNameExpression identifier = new SimpleNameExpression("InitializeComponents");
            InvocationExpression invocation = new InvocationExpression(identifier, new List <Expression>());
            object output = invocation.AcceptVisitor(new CodeDomVisitor(), null);

            Assert.IsTrue(output is CodeMethodInvokeExpression);
            CodeMethodInvokeExpression mie = (CodeMethodInvokeExpression)output;

            Assert.AreEqual("InitializeComponents", mie.Method.MethodName);
            Assert.IsTrue(mie.Method.TargetObject is CodeThisReferenceExpression);
        }
예제 #8
0
        void CheckGenericInvoke2(InvocationExpression expr)
        {
            Assert.AreEqual(0, expr.Arguments.Count);
            Assert.IsTrue(expr.TargetObject is SimpleNameExpression);
            SimpleNameExpression ident = (SimpleNameExpression)expr.TargetObject;

            Assert.AreEqual("myMethod", ident.Identifier);
            Assert.AreEqual(2, ident.TypeArguments.Count);
            Assert.AreEqual("T", ident.TypeArguments[0].Type);
            Assert.IsFalse(ident.TypeArguments[0].IsKeyword);
            Assert.AreEqual("System.Boolean", ident.TypeArguments[1].Type);
            Assert.IsTrue(ident.TypeArguments[1].IsKeyword);
        }
예제 #9
0
        public override void ExitMethodInvocationExpression(MethodInvocationExpression methodInvocationExpression)
        {
            var targetMethod = methodInvocationExpression.Target;

            if (targetMethod.Modifiers.Contains(MemberModifier.Static))
            {
                return;
            }

            SimpleNameExpression methodTarget   = null;
            IExpression          thisExpression = null;

            switch (methodInvocationExpression.Base)
            {
            case MemberExpression memberExpression:
                methodTarget = new SimpleNameExpression(memberExpression.Context, memberExpression.Name)
                {
                    Declaration = targetMethod,
                    Type        = new MethodReferenceType(targetMethod)
                };
                thisExpression = memberExpression.Base;
                break;

            case SimpleNameExpression simpleNameExpression:
                //Will only happen if called from another class member
                methodTarget = simpleNameExpression;
                var containingMethod = simpleNameExpression.NearestAncestorOfType <MethodDeclaration>();
                var thisParameter    = containingMethod.Variables.First();
                thisExpression = new SimpleNameExpression(simpleNameExpression.Context, thisParameter.Name)
                {
                    Declaration = thisParameter,
                    Type        = thisParameter.Type
                };
                break;
            }

            methodInvocationExpression.Base.ReplaceWith(methodTarget);
            var addBeforeNode = methodInvocationExpression.Arguments.FirstOrDefault();

            if (addBeforeNode != null)
            {
                methodInvocationExpression.AddChildBefore(addBeforeNode, thisExpression);
            }
            else
            {
                methodInvocationExpression.AddChild(thisExpression);
            }
        }
예제 #10
0
        //Update references to non-static methods and getters
        public override void ExitSimpleNameExpression(SimpleNameExpression simpleNameExpression)
        {
            var targetMethod = simpleNameExpression.Declarations.Collect(
                (GetterDeclaration x) => replacedGetters[x],
                (SetterDeclaration x) => replacedSetters[x])
                               .SingleOrDefault();

            if (targetMethod == null)
            {
                return;
            }
            var @base = new SimpleNameExpression(simpleNameExpression.Context, targetMethod.Name)
            {
                Declaration = targetMethod,
                Type        = new MethodReferenceType(targetMethod)
            };

            ReplaceGetterOrSetter(simpleNameExpression, simpleNameExpression.Declaration, @base);
        }
예제 #11
0
        /// <summary>
        ///     <c>
        ///         type_list:
        ///             [simple_name '='] type {',' [simple_name '='] type};
        ///     </c>
        ///     for class, enum, enum item.
        /// </summary>
        internal static List <(TypeName type, SimpleNameExpression label)> ParseNamedTypeArgs(
            AstNode parent
            )
        {
            var   typeArgs = new List <(TypeName, SimpleNameExpression)>();
            Token start    = parent.Peek;

            if (!parent.Peek.Is(CloseParenthesis))
            {
                do
                {
                    SimpleNameExpression name = null;
                    int startIdx = parent.Ast.Index;
                    if (parent.Peek.Is(Identifier))
                    {
                        name = new SimpleNameExpression(parent);
                        if (!parent.MaybeEat(OpAssign))
                        {
                            parent.MoveTo(startIdx);
                        }
                    }

                    typeArgs.Add((ParseTypeName(parent), name));
                } while (parent.MaybeEat(Comma));
            }

            if (typeArgs.Count == 0)
            {
                // redundant parens
                parent.Unit.Blame(
                    BlameType.RedundantEmptyListOfTypeArguments,
                    start.Span.Start,
                    parent.Token.Span.End
                    );
            }

            return(typeArgs);
        }
예제 #12
0
        private void Visit(
            SimpleNameExpression expression,
            QualifiedModuleName module,
            Declaration scope,
            Declaration parent,
            bool isAssignmentTarget,
            bool hasExplicitLetStatement)
        {
            var callSiteContext = expression.Context;
            var identifier      = expression.Context.GetText();
            var callee          = expression.ReferencedDeclaration;

            expression.ReferencedDeclaration.AddReference(
                module,
                scope,
                parent,
                callSiteContext,
                identifier,
                callee,
                callSiteContext.GetSelection(),
                _annotationService.FindAnnotations(module, callSiteContext.GetSelection().StartLine),
                isAssignmentTarget,
                hasExplicitLetStatement);
        }
예제 #13
0
 private void Visit(SimpleNameExpression expression, Func <Declaration, IdentifierReference> referenceCreator)
 {
     expression.ReferencedDeclaration.AddReference(referenceCreator(expression.ReferencedDeclaration));
 }
예제 #14
0
 public object VisitSimpleNameExpression(SimpleNameExpression identifierExpression, object data)
 {
     throw new NotImplementedException();
 }
예제 #15
0
 public virtual void ExitSimpleNameExpression(SimpleNameExpression simpleNameExpression)
 {
 }
예제 #16
0
 public virtual void EnterSimpleNameExpression(SimpleNameExpression simpleNameExpression)
 {
 }
예제 #17
0
 public virtual T VisitSimpleNameExpression(SimpleNameExpression simpleNameExpression)
 {
     return(VisitChildren(simpleNameExpression));
 }
예제 #18
0
        public override void ExitSimpleNameExpression(SimpleNameExpression simpleNameExpression)
        {
            if (simpleNameExpression.Declarations.Any())
            {
                return;
            }
            var ancestors = simpleNameExpression.AllAncestorsAndSelf()
                            .OfAnyType(typeof(IHasVariables),
                                       typeof(ClassDeclaration),
                                       typeof(ModuleDeclaration),
                                       typeof(SourceFile),
                                       typeof(Root));

            foreach (var ancestor in ancestors)
            {
                var declarations = GetMatchingDeclarations(ancestor, simpleNameExpression, simpleNameExpression.Name, true);
                if (!declarations.Any())
                {
                    continue;
                }
                if (declarations.Count > 1 && declarations.NotOfType(typeof(ITypeDeclaration)).Any())
                {
                    Errors.Add(new CompilationError(simpleNameExpression.Context, $"Found multiple declarations matching '{simpleNameExpression.Name}'"));
                }

                simpleNameExpression.Declarations = declarations.ToList();
                simpleNameExpression.Type         = GetType(simpleNameExpression.Declarations);

                if (declarations.Count == 1 && declarations[0] is GetterSetterDeclaration getterSetterDeclaration)
                {
                    if (simpleNameExpression.Parent is AssignmentExpression assignmentExpression && simpleNameExpression == assignmentExpression.Left)
                    {
                        if (getterSetterDeclaration.Setter == null)
                        {
                            Errors.Add(new CompilationError(simpleNameExpression.Context, $"{getterSetterDeclaration.Name} has no setter."));
                        }
                        else
                        {
                            simpleNameExpression.Declarations = new List <INamedDeclaration> {
                                getterSetterDeclaration.Setter
                            }
                        };
                        if (assignmentExpression.Operator.Value != "=" && getterSetterDeclaration.Getter == null)
                        {
                            Errors.Add(new CompilationError(simpleNameExpression.Context, $"{getterSetterDeclaration.Name} has no getter."));
                        }
                    }
                    else
                    {
                        if (getterSetterDeclaration.Getter == null)
                        {
                            Errors.Add(new CompilationError(simpleNameExpression.Context, $"{getterSetterDeclaration.Name} has no getter."));
                        }
                        else
                        {
                            simpleNameExpression.Declarations = new List <INamedDeclaration> {
                                getterSetterDeclaration.Getter
                            }
                        };
                    }
                }
                return;
            }
            throw new CompilationError(simpleNameExpression.Context, $"Found no declarations matching '{simpleNameExpression.Name}'");
        }
예제 #19
0
        private AstMethodDefinition ProcessInternal(ThreeAddressCode codeModel)
        {
            BlockStatement block = new BlockStatement();

            foreach (Triplet t in codeModel.RawTriplets)
            {
                switch (t.Opcode)
                {
                case TripletOpCode.Assignment:
                    SimpleNameExpression lhs = new SimpleNameExpression((t.Result as VariableDefinition).Name);
                    IntegerLiteral       rhs = new IntegerLiteral((Int64)t.Operand1);
                    var assignmentStmt       = new AssignmentStatement(lhs, AssignmentOperatorKind.Equal, rhs);
                    block.Statements.Add(assignmentStmt);
                    break;

                case TripletOpCode.Return:
                    var returnExpr = new SimpleNameExpression((t.Operand1 as VariableReference).Name);
                    var returnStmt = new ReturnStatement(returnExpr);
                    block.Statements.Add(returnStmt);
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            AstMethodDefinition result = new AstMethodDefinition(/*MethodDefinition*/ null, block);

            /*Triplet t = codeModel.Root;
             * //FunctionDeclaration decl = new FunctionDecl();
             * do {
             * switch (t.Opcode) {
             *  // Basic
             *  case TripletOpCode.Assignment: // result = op1
             *    // Create a VariableDeclaration
             *    //
             *    //t.Operand1 + t.Operand2;
             *    break;
             *
             * /*
             *  // Array deref etc
             *  //...
             *
             *  // Aritmetic
             *  Addition,       // result = op1 + op2
             *  Substraction,   // result = op1 - op2
             *  Multiplication, // result = op1 * op2
             *  Division,       // result = op1 / op2
             *  Reminder,       // result = op1 % op2
             *  Negate,         // result = - op1
             *  And,            // result = op1 & op2
             *  Or,             // result = op1 | op2
             *  Xor,            // result = op1 ^ op2
             *  Not,            // result = ! op1
             *  ShiftLeft,      // result = op1 << op2
             *  ShiftRight,     // result = op1 >> op2
             *  CheckOverflow,  // checkoverflow
             *  //...
             *
             *  // Cast
             *  Cast,           // result = (op1) op2
             *
             *  // Logic
             *  Equal,          // result = op1 == op2
             *  Less,           // result = op1 < op2
             *  Great,          // result = op1 > op2
             *  //...
             *
             *  // Control
             *  Goto,           // goto op1/label
             *  IfFalse,        // iffalse op1 goto op2/label
             *  IfTrue,         // iftrue op1 goto op2/label
             *  Switch,         // switch op1 goto op2/array/labels
             *  //...
             *
             *  // Methods
             *  Call,           // result = call op1/method/signature
             *  CallVirt,       // result = callvirt op1/method/signature
             *  PushParam,      // pushparam op1
             *  Return,         // return op1
             *  //...
             *
             *  // Object model
             *  //...
             *
             *  // Exceptions handling
             *  //...
             *
             *  // Other
             *  Nop             // nop
             *//*
             *  dafault:
             *    throw new NotImplementedException("Triplet opcode not supported yet.");
             * }
             * }
             * while ((t = t.Next) != null);
             */
            return(result);
        }