Esempio n. 1
0
        private void ResolveDefault(
            ParserRuleContext expression,
            bool requiresLetCoercion = false,
            StatementResolutionContext statementContext = StatementResolutionContext.Undefined,
            bool isAssignmentTarget      = false,
            bool hasExplicitLetStatement = false,
            bool isSetAssignment         = false)
        {
            var withExpression  = GetInnerMostWithExpression();
            var boundExpression = _bindingService.ResolveDefault(
                _moduleDeclaration,
                _currentParent,
                expression,
                withExpression,
                statementContext,
                requiresLetCoercion,
                isAssignmentTarget);

            _failedResolutionVisitor.CollectUnresolved(boundExpression, _currentParent, withExpression);

            _boundExpressionVisitor.AddIdentifierReferences(
                boundExpression,
                _qualifiedModuleName,
                _currentScope,
                _currentParent,
                isAssignmentTarget,
                hasExplicitLetStatement,
                isSetAssignment);
        }
        private void ResolveDefault(
            ParserRuleContext expression,
            StatementResolutionContext statementContext = StatementResolutionContext.Undefined,
            bool isAssignmentTarget      = false,
            bool hasExplicitLetStatement = false,
            bool isSetAssignment         = false)
        {
            var withExpression  = GetInnerMostWithExpression();
            var boundExpression = _bindingService.ResolveDefault(
                _moduleDeclaration,
                _currentParent,
                expression,
                withExpression,
                statementContext);

            if (boundExpression.Classification == ExpressionClassification.ResolutionFailed)
            {
                var lexpression = expression as VBAParser.LExpressionContext
                                  ?? expression.GetChild <VBAParser.LExpressionContext>(0)
                                  ?? (expression as VBAParser.LExprContext
                                      ?? expression.GetChild <VBAParser.LExprContext>(0))?.lExpression();

                if (lexpression != null)
                {
                    _declarationFinder.AddUnboundContext(_currentParent, lexpression, withExpression);
                }
                else
                {
                    Logger.Warn(
                        $"Default Context: Failed to resolve {expression.GetText()}. Binding as much as we can.");
                }
            }

            IParameterizedDeclaration defaultMember = null;

            if (boundExpression.ReferencedDeclaration != null &&
                boundExpression.ReferencedDeclaration.DeclarationType != DeclarationType.Project &&
                boundExpression.ReferencedDeclaration.AsTypeDeclaration != null)
            {
                var module  = boundExpression.ReferencedDeclaration.AsTypeDeclaration;
                var members = _declarationFinder.Members(module);
                defaultMember = (IParameterizedDeclaration)members.FirstOrDefault(member =>
                                                                                  member is IParameterizedDeclaration && member.Attributes.HasDefaultMemberAttribute() &&
                                                                                  (isAssignmentTarget
                            ? member.DeclarationType.HasFlag(DeclarationType.Procedure)
                            : member.DeclarationType.HasFlag(DeclarationType.Function)));
            }

            _boundExpressionVisitor.AddIdentifierReferences(
                boundExpression,
                _qualifiedModuleName,
                _currentScope,
                _currentParent,
                isAssignmentTarget && (defaultMember == null || isSetAssignment || defaultMember.Parameters.All(param => param.IsOptional)),
                hasExplicitLetStatement,
                isSetAssignment);
        }
        public static DeclarationType GetSearchDeclarationType(StatementResolutionContext statementContext)
        {
            switch (statementContext)
            {
            case StatementResolutionContext.LetStatement:
                return(DeclarationType.PropertyLet);

            case StatementResolutionContext.SetStatement:
                return(DeclarationType.PropertySet);

            default:
                return(DeclarationType.PropertyGet);
            }
        }
Esempio n. 4
0
        private IExpressionBinding VisitTypeOf(
            Declaration module,
            Declaration parent,
            VBAParser.RelationalOpContext typeOfIsExpression,
            VBAParser.TypeofexprContext typeOfLeftPartExpression,
            ParserRuleContext typeExpression,
            IBoundExpression withBlockVariable,
            StatementResolutionContext statementContext)
        {
            dynamic booleanExpression        = typeOfLeftPartExpression.expression();
            var     booleanExpressionBinding = Visit(module, parent, booleanExpression, withBlockVariable, StatementResolutionContext.Undefined);
            var     typeExpressionBinding    = VisitType(module, parent, (dynamic)typeExpression, withBlockVariable, StatementResolutionContext.Undefined);

            return(new TypeOfIsDefaultBinding(typeOfIsExpression, booleanExpressionBinding, typeExpressionBinding));
        }
 public SimpleNameDefaultBinding(
     DeclarationFinder declarationFinder,
     Declaration project,
     Declaration module,
     Declaration parent,
     ParserRuleContext context,
     string name,
     StatementResolutionContext statementContext)
 {
     _declarationFinder = declarationFinder;
     _project           = project;
     _module            = module;
     _parent            = parent;
     _context           = context;
     _name = name;
     _propertySearchType = StatementContext.GetSearchDeclarationType(statementContext);
 }
        private void ResolveDefault(
            ParserRuleContext expression,
            StatementResolutionContext statementContext = StatementResolutionContext.Undefined,
            bool isAssignmentTarget      = false,
            bool hasExplicitLetStatement = false,
            bool isSetAssignment         = false)
        {
            var withExpression  = GetInnerMostWithExpression();
            var boundExpression = _bindingService.ResolveDefault(
                _moduleDeclaration,
                _currentParent,
                expression,
                withExpression,
                statementContext);

            if (boundExpression.Classification == ExpressionClassification.ResolutionFailed)
            {
                var lexpression = expression as VBAParser.LExpressionContext
                                  ?? expression.GetChild <VBAParser.LExpressionContext>(0)
                                  ?? (expression as VBAParser.LExprContext
                                      ?? expression.GetChild <VBAParser.LExprContext>(0))?.lExpression();

                if (lexpression != null)
                {
                    _declarationFinder.AddUnboundContext(_currentParent, lexpression, withExpression);
                }
                else
                {
                    Logger.Warn(
                        $"Default Context: Failed to resolve {expression.GetText()}. Binding as much as we can.");
                }
            }

            var hasDefaultMember = false;

            if (boundExpression.ReferencedDeclaration != null &&
                boundExpression.ReferencedDeclaration.DeclarationType != DeclarationType.Project &&
                boundExpression.ReferencedDeclaration.AsTypeDeclaration != null)
            {
                var module  = boundExpression.ReferencedDeclaration.AsTypeDeclaration;
                var members = _declarationFinder.Members(module);
                hasDefaultMember = members.Any(m => m.Attributes.Any(a => a.Name == $"{m.IdentifierName}.VB_UserMemId" && a.Values.SingleOrDefault() == "0"));
            }
            _boundExpressionVisitor.AddIdentifierReferences(boundExpression, _qualifiedModuleName, _currentScope, _currentParent, (!hasDefaultMember || isSetAssignment) && isAssignmentTarget, hasExplicitLetStatement);
        }
Esempio n. 7
0
 public SimpleNameDefaultBinding(
     DeclarationFinder declarationFinder,
     Declaration project,
     Declaration module,
     Declaration parent,
     ParserRuleContext context,
     string name,
     StatementResolutionContext statementContext)
 {
     _declarationFinder = declarationFinder;
     _project           = project;
     _module            = module;
     _parent            = parent;
     _context           = context;
     // hack; SimpleNameContext.Identifier() excludes the square brackets
     _name = context.Start.Text == "[" && context.Stop.Text == "]" ? "[" + name + "]" : name;
     _propertySearchType = StatementContext.GetSearchDeclarationType(statementContext);
 }
Esempio n. 8
0
 public IExpressionBinding BuildTree(
     Declaration module,
     Declaration parent,
     IParseTree expression,
     IBoundExpression withBlockVariable,
     StatementResolutionContext statementContext,
     bool requiresLetCoercion = false,
     bool isLetAssignment     = false)
 {
     return(Visit(
                module,
                parent,
                expression,
                withBlockVariable,
                statementContext,
                requiresLetCoercion,
                isLetAssignment));
 }
Esempio n. 9
0
 public MemberAccessDefaultBinding(
     DeclarationFinder declarationFinder,
     Declaration project,
     Declaration module,
     Declaration parent,
     ParserRuleContext expression,
     IBoundExpression lExpression,
     string name,
     StatementResolutionContext statementContext,
     ParserRuleContext unrestrictedNameContext)
 {
     _declarationFinder = declarationFinder;
     _project           = project;
     _module            = module;
     _parent            = parent;
     _context           = expression;
     _lExpression       = lExpression;
     _name = name;
     _propertySearchType      = StatementContext.GetSearchDeclarationType(statementContext);
     _unrestrictedNameContext = unrestrictedNameContext;
 }
Esempio n. 10
0
 public MemberAccessDefaultBinding(
     DeclarationFinder declarationFinder,
     Declaration project,
     Declaration module,
     Declaration parent,
     VBAParser.MemberAccessExprContext expression,
     IExpressionBinding lExpressionBinding,
     StatementResolutionContext statementContext,
     ParserRuleContext unrestrictedNameContext)
     : this(
         declarationFinder,
         project,
         module,
         parent,
         expression,
         null,
         Identifier.GetName(expression.unrestrictedIdentifier()),
         statementContext,
         unrestrictedNameContext)
 {
     _lExpressionBinding = lExpressionBinding;
 }
Esempio n. 11
0
        private void ResolveDefault(
            ParserRuleContext expression,
            StatementResolutionContext statementContext = StatementResolutionContext.Undefined,
            bool isAssignmentTarget      = false,
            bool hasExplicitLetStatement = false)
        {
            var boundExpression = _bindingService.ResolveDefault(
                _moduleDeclaration,
                _currentParent,
                expression,
                GetInnerMostWithExpression(),
                statementContext);

            if (boundExpression.Classification == ExpressionClassification.ResolutionFailed)
            {
                Logger.Warn(
                    string.Format(
                        "Default Context: Failed to resolve {0}. Binding as much as we can.",
                        expression.GetText()));
            }
            _boundExpressionVisitor.AddIdentifierReferences(boundExpression, _qualifiedModuleName, _currentScope, _currentParent, isAssignmentTarget, hasExplicitLetStatement);
        }
 public MemberAccessDefaultBinding(
     DeclarationFinder declarationFinder,
     Declaration project,
     Declaration module,
     Declaration parent,
     VBAParser.ObjectPrintExprContext expression,
     IExpressionBinding lExpressionBinding,
     StatementResolutionContext statementContext,
     ParserRuleContext unrestrictedNameContext)
     : this(
         declarationFinder,
         project,
         module,
         parent,
         expression,
         null,
         Tokens.Print,
         statementContext,
         unrestrictedNameContext)
 {
     _lExpressionBinding = lExpressionBinding;
 }
Esempio n. 13
0
        private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.ParenthesizedExprContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
        {
            dynamic expressionParens  = expression.expression();
            var     expressionBinding = Visit(module, parent, expressionParens, withBlockVariable, StatementResolutionContext.Undefined);

            return(new ParenthesizedDefaultBinding(expression, expressionBinding));
        }
Esempio n. 14
0
        private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.NewExprContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
        {
            var typeExpressionBinding = VisitType(module, parent, expression.expression(), withBlockVariable, StatementResolutionContext.Undefined);

            if (typeExpressionBinding == null)
            {
                return(null);
            }
            return(new NewTypeBinding(_declarationFinder, module, parent, expression, typeExpressionBinding));
        }
Esempio n. 15
0
 private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.AddressOfExpressionContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
 {
     return(_procedurePointerBindingContext.BuildTree(module, parent, expression, withBlockVariable, statementContext));
 }
Esempio n. 16
0
        private IExpressionBinding VisitUnaryOp(Declaration module, Declaration parent, ParserRuleContext context, ParserRuleContext expr, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
        {
            dynamic exprExpr    = expr;
            var     exprBinding = Visit(module, parent, exprExpr, withBlockVariable, StatementResolutionContext.Undefined);

            return(new UnaryOpDefaultBinding(context, exprBinding));
        }
Esempio n. 17
0
        private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.LExprContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
        {
            dynamic lexpr = expression.lExpression();

            return(Visit(module, parent, lexpr, withBlockVariable, statementContext));
        }
Esempio n. 18
0
        private IExpressionBinding VisitBinaryOp(Declaration module, Declaration parent, ParserRuleContext context, ParserRuleContext left, ParserRuleContext right, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
        {
            dynamic leftExpr     = left;
            var     leftBinding  = Visit(module, parent, leftExpr, withBlockVariable, StatementResolutionContext.Undefined);
            dynamic rightExpr    = right;
            var     rightBinding = Visit(module, parent, rightExpr, withBlockVariable, StatementResolutionContext.Undefined);

            return(new BinaryOpDefaultBinding(context, leftBinding, rightBinding));
        }
Esempio n. 19
0
 private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.CallStmtContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
 {
     // Call statements always have an argument list.
     // One of the reasons we're doing this is that an empty argument list could represent a call to a default member,
     // which requires us to use an IndexDefaultBinding.
     if (expression.CALL() == null)
     {
         dynamic lexpr        = expression.expression();
         var     lexprBinding = Visit(module, parent, lexpr, withBlockVariable, StatementResolutionContext.Undefined);
         var     argList      = VisitArgumentList(module, parent, expression.argumentList(), withBlockVariable, StatementResolutionContext.Undefined);
         SetLeftMatch(lexprBinding, argList.Arguments.Count);
         return(new IndexDefaultBinding(_declarationFinder, Declaration.GetProjectParent(parent), module, parent, expression.expression(), lexprBinding, argList));
     }
     else
     {
         var lexprBinding = Visit(module, parent, (dynamic)expression.expression(), withBlockVariable, StatementResolutionContext.Undefined);
         if (!(lexprBinding is IndexDefaultBinding))
         {
             return(new IndexDefaultBinding(_declarationFinder, Declaration.GetProjectParent(parent), module, parent, expression.expression(), lexprBinding, new ArgumentList()));
         }
         else
         {
             return(lexprBinding);
         }
     }
 }
Esempio n. 20
0
 private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.IntegerExpressionContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
 {
     return(Visit(module, parent, (dynamic)expression.expression(), withBlockVariable, StatementResolutionContext.Undefined));
 }
Esempio n. 21
0
 private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.LiteralExpressionContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
 {
     return(new LiteralDefaultBinding(expression));
 }
Esempio n. 22
0
        private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.DictionaryAccessExprContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
        {
            dynamic lExpression        = expression.lExpression();
            var     lExpressionBinding = Visit(module, parent, lExpression, withBlockVariable, StatementResolutionContext.Undefined);

            return(VisitDictionaryAccessExpression(module, parent, expression, expression.unrestrictedIdentifier(), lExpressionBinding, StatementResolutionContext.Undefined));
        }
Esempio n. 23
0
 private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.LogicalNotOpContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
 {
     return(VisitUnaryOp(module, parent, expression, expression.expression(), withBlockVariable, StatementResolutionContext.Undefined));
 }
Esempio n. 24
0
        public IExpressionBinding BuildTree(Declaration module, Declaration parent, ParserRuleContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
        {
            dynamic dynamicExpression = expression;
            var     type = expression.GetType();

            return(Visit(module, parent, dynamicExpression, withBlockVariable, statementContext));
        }
Esempio n. 25
0
        private IExpressionBinding VisitDictionaryAccessExpression(Declaration module, Declaration parent, ParserRuleContext expression, ParserRuleContext nameContext, IBoundExpression lExpression, StatementResolutionContext statementContext)
        {
            /*
             *  A dictionary access expression is syntactically translated into an index expression with the same
             *  expression for <l-expression> and an argument list with a single positional argument with a
             *  declared type of String and a value equal to the name value of <unrestricted-name>.
             */
            var fakeArgList = new ArgumentList();

            fakeArgList.AddArgument(new ArgumentListArgument(new LiteralDefaultBinding(nameContext), ArgumentListArgumentType.Positional));
            return(new IndexDefaultBinding(_declarationFinder, Declaration.GetProjectParent(parent), module, parent, expression, lExpression, fakeArgList));
        }
Esempio n. 26
0
        public IBoundExpression Resolve(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false)
        {
            IExpressionBinding bindingTree = BuildTree(module, parent, expression, withBlockVariable, statementContext);

            return(bindingTree?.Resolve());
        }
Esempio n. 27
0
 private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.WithDictionaryAccessExprContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
 {
     /*
      *  A <with-member-access-expression> or <with-dictionary-access-expression> is
      *  statically resolved as a normal member access or dictionary access expression, respectively, as if
      *  the innermost enclosing With block variable was specified for <l-expression>. If there is no
      *  enclosing With block, the <with-expression> is invalid.
      */
     return(VisitDictionaryAccessExpression(module, parent, expression, expression.unrestrictedIdentifier(), withBlockVariable, StatementResolutionContext.Undefined));
 }
Esempio n. 28
0
 private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.WithMemberAccessExprContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
 {
     return(new MemberAccessDefaultBinding(_declarationFinder, Declaration.GetProjectParent(parent), module, parent, expression, withBlockVariable, expression.unrestrictedIdentifier().GetText(), statementContext, expression.unrestrictedIdentifier()));
 }
Esempio n. 29
0
        public IExpressionBinding BuildTree(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false)
        {
            switch (expression)
            {
            case VBAParser.LExprContext lExprContext:
                return(Visit(module, parent, lExprContext.lExpression()));

            case VBAParser.CtLExprContext ctLExprContext:
                return(Visit(module, parent, ctLExprContext.lExpression()));

            case VBAParser.BuiltInTypeExprContext builtInTypeExprContext:
                return(Visit(builtInTypeExprContext.builtInType()));

            default:
                throw new NotSupportedException($"Unexpected context type {expression.GetType()}");
            }
        }
Esempio n. 30
0
 private IExpressionBinding Visit(Declaration module, Declaration parent, VBAParser.RelationalOpContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext)
 {
     // To make the grammar we treat a type-of-is expression as a construct of the form "TYPEOF expression", where expression
     // is always "expression IS expression".
     if (expression.expression()[0] is VBAParser.TypeofexprContext)
     {
         return(VisitTypeOf(module, parent, expression, (VBAParser.TypeofexprContext)expression.expression()[0], expression.expression()[1], withBlockVariable, StatementResolutionContext.Undefined));
     }
     return(VisitBinaryOp(module, parent, expression, expression.expression()[0], expression.expression()[1], withBlockVariable, StatementResolutionContext.Undefined));
 }