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;

                var isByRef      = argContext.BYREF() != null || argContext.BYVAL() == 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 IParameterizedDeclaration)
                {
                    ((IParameterizedDeclaration)_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.Event)
                {
                    result = new EventDeclaration(
                        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);
        }
 public SubroutineDeclaration(ComMember member, Declaration parent, QualifiedModuleName module, Attributes attributes, bool eventHandler)
     : base(
         module.QualifyMemberName(member.Name),
         parent,
         parent,
         string.Empty,
         null,
         string.Empty,
         Accessibility.Global,
         eventHandler ? DeclarationType.Event : DeclarationType.Procedure,
         null,
         null,
         Selection.Home,
         false,
         false,
         null,
         attributes)
 {
     AddParameters(member.Parameters.Select(decl => new ParameterDeclaration(decl, this, module)));
 }