Example #1
0
        private IBoundExpression ResolveEnclosingModuleNamespace()
        {
            /*  Namespace tier 2:
             *  Enclosing Module namespace: A variable, constant, Enum type, Enum member, property,
             *  function or subroutine defined at the module-level in the enclosing module.
             */
            var moduleVariable = _declarationFinder.FindMemberEnclosingModule(_module, _parent, _name, DeclarationType.Variable);

            if (IsValidMatch(moduleVariable, _name))
            {
                return(new SimpleNameExpression(moduleVariable, ExpressionClassification.Variable, _context));
            }
            var moduleConstant = _declarationFinder.FindMemberEnclosingModule(_module, _parent, _name, DeclarationType.Constant);

            if (IsValidMatch(moduleConstant, _name))
            {
                return(new SimpleNameExpression(moduleConstant, ExpressionClassification.Variable, _context));
            }
            var enumType = _declarationFinder.FindMemberEnclosingModule(_module, _parent, _name, DeclarationType.Enumeration);

            if (IsValidMatch(enumType, _name))
            {
                return(new SimpleNameExpression(enumType, ExpressionClassification.Type, _context));
            }
            var enumMember = _declarationFinder.FindMemberEnclosingModule(_module, _parent, _name, DeclarationType.EnumerationMember);

            if (IsValidMatch(enumMember, _name))
            {
                return(new SimpleNameExpression(enumMember, ExpressionClassification.Value, _context));
            }
            // Prioritize return value assignments over any other let/set property references.
            if (_parent.DeclarationType == DeclarationType.PropertyGet && _declarationFinder.IsMatch(_parent.IdentifierName, _name))
            {
                return(new SimpleNameExpression(_parent, ExpressionClassification.Property, _context));
            }
            var property = _declarationFinder.FindMemberEnclosingModule(_module, _parent, _name, _propertySearchType);

            if (IsValidMatch(property, _name))
            {
                return(new SimpleNameExpression(property, ExpressionClassification.Property, _context));
            }
            var function = _declarationFinder.FindMemberEnclosingModule(_module, _parent, _name, DeclarationType.Function);

            if (IsValidMatch(function, _name))
            {
                return(new SimpleNameExpression(function, ExpressionClassification.Function, _context));
            }
            var subroutine = _declarationFinder.FindMemberEnclosingModule(_module, _parent, _name, DeclarationType.Procedure);

            if (IsValidMatch(subroutine, _name))
            {
                return(new SimpleNameExpression(subroutine, ExpressionClassification.Subroutine, _context));
            }
            return(null);
        }
        private IBoundExpression ResolveEnclosingProject(string name)
        {
            /*  Namespace tier 2:
             *  Enclosing Project namespace: The enclosing project itself, a referenced project, or a
             *  procedural module or class module contained in the enclosing project.
             */
            if (_declarationFinder.IsMatch(_project.ProjectName, name))
            {
                // Ensure there is no ambiguous matches
                var ambiguousClassModule = _declarationFinder.FindModuleEnclosingProjectWithoutEnclosingModule(_project, _module, name, DeclarationType.ClassModule);
                if (ambiguousClassModule != null)
                {
                    // We have an indeterminate result; let's check if this is a part of member access first...
                    if (_expression.Parent.GetType().Name != nameof(VBAParser.MemberAccessExprContext))
                    {
                        // There is no qualifier -- this is normally a VBA compile error
                        throw new InvalidOperationException($"There cannot be a unqualified reference where the name '{name}' is ambiguous.");
                    }
                }

                return(new SimpleNameExpression(_project, ExpressionClassification.Project, _expression));
            }
            var referencedProject = _declarationFinder.FindReferencedProject(_project, name);

            if (referencedProject != null)
            {
                return(new SimpleNameExpression(referencedProject, ExpressionClassification.Project, _expression));
            }
            if (_module.DeclarationType == DeclarationType.ProceduralModule && _declarationFinder.IsMatch(_module.IdentifierName, name))
            {
                return(new SimpleNameExpression(_module, ExpressionClassification.ProceduralModule, _expression));
            }
            var proceduralModuleEnclosingProject = _declarationFinder.FindModuleEnclosingProjectWithoutEnclosingModule(_project, _module, name, DeclarationType.ProceduralModule);

            if (proceduralModuleEnclosingProject != null)
            {
                return(new SimpleNameExpression(proceduralModuleEnclosingProject, ExpressionClassification.ProceduralModule, _expression));
            }
            if (_module.DeclarationType == DeclarationType.ClassModule && _declarationFinder.IsMatch(_module.IdentifierName, name))
            {
                return(new SimpleNameExpression(_module, ExpressionClassification.Type, _expression));
            }
            var classEnclosingProject = _declarationFinder.FindModuleEnclosingProjectWithoutEnclosingModule(_project, _module, name, DeclarationType.ClassModule);

            if (classEnclosingProject != null)
            {
                return(new SimpleNameExpression(classEnclosingProject, ExpressionClassification.Type, _expression));
            }
            return(null);
        }
Example #3
0
        private IBoundExpression ResolveProject()
        {
            if (_declarationFinder.IsMatch(_project.ProjectName, _name))
            {
                return(new MemberAccessExpression(_project, ExpressionClassification.Project, _context, _unrestrictedNameContext, _lExpression));
            }
            var referencedProjectRightOfDot = _declarationFinder.FindReferencedProject(_project, _name);

            if (referencedProjectRightOfDot != null)
            {
                return(new MemberAccessExpression(referencedProjectRightOfDot, ExpressionClassification.Project, _context, _unrestrictedNameContext, _lExpression));
            }
            return(null);
        }
        private IBoundExpression ResolveProject(IBoundExpression lExpression, string name)
        {
            /*
             * <l-expression> refers to the enclosing project and <unrestricted-name> is either the name of
             *  the enclosing project or a referenced project. In this case, the member access expression is
             *  classified as a project and refers to the specified project.
             */
            if (_declarationFinder.IsMatch(_project.ProjectName, name))
            {
                return(new MemberAccessExpression(_project, ExpressionClassification.Project, _expression, _unrestrictedNameContext, lExpression));
            }
            var referencedProjectRightOfDot = _declarationFinder.FindReferencedProject(_project, name);

            if (referencedProjectRightOfDot != null)
            {
                return(new MemberAccessExpression(referencedProjectRightOfDot, ExpressionClassification.Project, _expression, _unrestrictedNameContext, lExpression));
            }
            return(null);
        }
        private IBoundExpression ResolveEnclosingProject(string name)
        {
            /*
             *  Enclosing Project namespace: The enclosing project itself or a procedural module contained in
             *  the enclosing project.
             */
            if (_declarationFinder.IsMatch(_project.ProjectName, name))
            {
                return(new SimpleNameExpression(_project, ExpressionClassification.Project, _expression));
            }
            if (_module.DeclarationType == DeclarationType.ProceduralModule && _declarationFinder.IsMatch(_module.IdentifierName, name))
            {
                return(new SimpleNameExpression(_module, ExpressionClassification.ProceduralModule, _expression));
            }
            var proceduralModuleEnclosingProject = _declarationFinder.FindModuleEnclosingProjectWithoutEnclosingModule(_project, _module, name, DeclarationType.ProceduralModule);

            if (proceduralModuleEnclosingProject != null)
            {
                return(new SimpleNameExpression(proceduralModuleEnclosingProject, ExpressionClassification.ProceduralModule, _expression));
            }
            return(null);
        }
        private IBoundExpression ResolveEnclosingProject(string name)
        {
            /*  Namespace tier 2:
             *  Enclosing Project namespace: The enclosing project itself, a referenced project, or a
             *  procedural module or class module contained in the enclosing project.
             */
            if (_declarationFinder.IsMatch(_project.Project.Name, name))
            {
                return(new SimpleNameExpression(_project, ExpressionClassification.Project, _expression));
            }
            var referencedProject = _declarationFinder.FindReferencedProject(_project, name);

            if (referencedProject != null)
            {
                return(new SimpleNameExpression(referencedProject, ExpressionClassification.Project, _expression));
            }
            if (_module.DeclarationType == DeclarationType.ProceduralModule && _declarationFinder.IsMatch(_module.IdentifierName, name))
            {
                return(new SimpleNameExpression(_module, ExpressionClassification.ProceduralModule, _expression));
            }
            var proceduralModuleEnclosingProject = _declarationFinder.FindModuleEnclosingProjectWithoutEnclosingModule(_project, _module, name, DeclarationType.ProceduralModule);

            if (proceduralModuleEnclosingProject != null)
            {
                return(new SimpleNameExpression(proceduralModuleEnclosingProject, ExpressionClassification.ProceduralModule, _expression));
            }
            if (_module.DeclarationType == DeclarationType.ClassModule && _declarationFinder.IsMatch(_module.IdentifierName, name))
            {
                return(new SimpleNameExpression(_module, ExpressionClassification.Type, _expression));
            }
            var classEnclosingProject = _declarationFinder.FindModuleEnclosingProjectWithoutEnclosingModule(_project, _module, name, DeclarationType.ClassModule);

            if (classEnclosingProject != null)
            {
                return(new SimpleNameExpression(classEnclosingProject, ExpressionClassification.Type, _expression));
            }
            return(null);
        }