public void Resolve(VBAParser.AsTypeClauseContext context)
        {
            var asType = context.type();

            if (asType == null)
            {
                return;
            }

            Declaration         type      = null;
            IdentifierReference reference = null;

            var baseType = asType.baseType();

            if (baseType != null)
            {
                var collection = baseType.COLLECTION();
                if (collection != null)
                {
                    type      = _declarations[collection.GetText()].SingleOrDefault(item => item.IsBuiltIn && item.DeclarationType == DeclarationType.Class);
                    reference = CreateReference(baseType, type);
                }
            }
            else
            {
                type      = ResolveType(asType.complexType());
                reference = CreateReference(asType.complexType(), type);
            }

            if (type != null)
            {
                type.AddReference(reference);
                _alreadyResolved.Add(reference.Context);
            }
        }
예제 #2
0
        public void Resolve(VBAParser.AsTypeClauseContext context)
        {
            // All "As Type" expressions are resolved here, statements don't have to resolve their "As Types" themselves.
            var asType = context.type();

            if (asType == null)
            {
                return;
            }
            var baseType = asType.baseType();

            if (baseType != null)
            {
                // Fixed-Length strings can have a constant-name as length that is a simple-name-expression that also has to be resolved.
                var length = context.fieldLength();
                if (context.fieldLength() != null && context.fieldLength().identifierValue() != null)
                {
                    ResolveDefault(context.fieldLength().identifierValue());
                }
                return;
            }
            ResolveType(asType.complexType());
        }
        public override void EnterUdtMember(VBAParser.UdtMemberContext context)
        {
            VBAParser.AsTypeClauseContext asTypeClause = null;
            bool      isArray  = false;
            string    typeHint = null;
            string    identifier;
            Selection identifierSelection;

            if (context.reservedNameMemberDeclaration() != null)
            {
                identifier          = Identifier.GetName(context.reservedNameMemberDeclaration().unrestrictedIdentifier());
                identifierSelection = Identifier.GetNameSelection(context.reservedNameMemberDeclaration().unrestrictedIdentifier());
                asTypeClause        = context.reservedNameMemberDeclaration().asTypeClause();
            }
            else
            {
                identifier          = Identifier.GetName(context.untypedNameMemberDeclaration().untypedIdentifier());
                identifierSelection = Identifier.GetNameSelection(context.untypedNameMemberDeclaration().untypedIdentifier());
                asTypeClause        = context.untypedNameMemberDeclaration().optionalArrayClause().asTypeClause();
                isArray             = context.untypedNameMemberDeclaration().optionalArrayClause().arrayDim() != null;
            }
            var asTypeName = asTypeClause == null
                ? Tokens.Variant
                : asTypeClause.type().GetText();

            AddDeclaration(
                CreateDeclaration(
                    identifier,
                    asTypeName,
                    Accessibility.Implicit,
                    DeclarationType.UserDefinedTypeMember,
                    context,
                    identifierSelection,
                    isArray,
                    asTypeClause,
                    typeHint));
        }
        private Declaration CreateDeclaration(
            string identifierName,
            string asTypeName,
            Accessibility accessibility,
            DeclarationType declarationType,
            ParserRuleContext context,
            Selection selection,
            bool isArray,
            VBAParser.AsTypeClauseContext asTypeContext,
            string typeHint,
            bool selfAssigned = false,
            bool withEvents   = false)
        {
            Declaration result;

            if (declarationType == DeclarationType.Parameter)
            {
                var argContext = (VBAParser.ArgContext)context;
                var isOptional = argContext.OPTIONAL() != null;
                // TODO: "As Type" could be missing. Temp solution until default values are parsed correctly.
                if (isOptional && asTypeContext != null)
                {
                    // if parameter is optional, asTypeName may contain the default value
                    var complexType = asTypeContext.type().complexType();
                    if (complexType != null && complexType.expression() is VBAParser.RelationalOpContext)
                    {
                        asTypeName = complexType.expression().GetChild(0).GetText();
                    }
                }

                var isByRef      = argContext.BYREF() != null;
                var isParamArray = argContext.PARAMARRAY() != null;
                result = new ParameterDeclaration(
                    new QualifiedMemberName(_qualifiedName, identifierName),
                    _parentDeclaration,
                    context,
                    selection,
                    asTypeName,
                    asTypeContext,
                    typeHint,
                    isOptional,
                    isByRef,
                    isArray,
                    isParamArray);
                if (_parentDeclaration is IDeclarationWithParameter)
                {
                    ((IDeclarationWithParameter)_parentDeclaration).AddParameter(result);
                }
            }
            else
            {
                var        key        = Tuple.Create(identifierName, declarationType);
                Attributes attributes = null;
                if (_attributes.ContainsKey(key))
                {
                    attributes = _attributes[key];
                }

                var annotations = FindAnnotations(selection.StartLine);
                if (declarationType == DeclarationType.Procedure)
                {
                    result = new SubroutineDeclaration(new QualifiedMemberName(_qualifiedName, identifierName), _parentDeclaration, _currentScopeDeclaration, asTypeName, accessibility, context, selection, false, annotations, attributes);
                }
                else if (declarationType == DeclarationType.Function)
                {
                    result = new FunctionDeclaration(
                        new QualifiedMemberName(_qualifiedName, identifierName),
                        _parentDeclaration,
                        _currentScopeDeclaration,
                        asTypeName,
                        asTypeContext,
                        typeHint,
                        accessibility,
                        context,
                        selection,
                        isArray,
                        false,
                        annotations,
                        attributes);
                }
                else if (declarationType == DeclarationType.LibraryProcedure || declarationType == DeclarationType.LibraryFunction)
                {
                    result = new ExternalProcedureDeclaration(new QualifiedMemberName(_qualifiedName, identifierName), _parentDeclaration, _currentScopeDeclaration, declarationType, asTypeName, asTypeContext, accessibility, context, selection, false, annotations);
                }
                else if (declarationType == DeclarationType.PropertyGet)
                {
                    result = new PropertyGetDeclaration(
                        new QualifiedMemberName(_qualifiedName, identifierName),
                        _parentDeclaration,
                        _currentScopeDeclaration,
                        asTypeName,
                        asTypeContext,
                        typeHint,
                        accessibility,
                        context,
                        selection,
                        isArray,
                        false,
                        annotations,
                        attributes);
                }
                else if (declarationType == DeclarationType.PropertySet)
                {
                    result = new PropertySetDeclaration(new QualifiedMemberName(_qualifiedName, identifierName), _parentDeclaration, _currentScopeDeclaration, asTypeName, accessibility, context, selection, false, annotations, attributes);
                }
                else if (declarationType == DeclarationType.PropertyLet)
                {
                    result = new PropertyLetDeclaration(new QualifiedMemberName(_qualifiedName, identifierName), _parentDeclaration, _currentScopeDeclaration, asTypeName, accessibility, context, selection, false, annotations, attributes);
                }
                else
                {
                    result = new Declaration(
                        new QualifiedMemberName(_qualifiedName, identifierName),
                        _parentDeclaration,
                        _currentScopeDeclaration,
                        asTypeName,
                        typeHint,
                        selfAssigned,
                        withEvents,
                        accessibility,
                        declarationType,
                        context,
                        selection,
                        isArray,
                        asTypeContext,
                        false,
                        annotations,
                        attributes);
                }
                if (_parentDeclaration.DeclarationType == DeclarationType.ClassModule && result is ICanBeDefaultMember && ((ICanBeDefaultMember)result).IsDefaultMember)
                {
                    ((ClassModuleDeclaration)_parentDeclaration).DefaultMember = result;
                }
            }
            return(result);
        }