|
| 1 | +using Rubberduck.Parsing.Symbols; |
| 2 | + |
| 3 | +namespace Rubberduck.Parsing.Binding |
| 4 | +{ |
| 5 | + public sealed class SimpleNameTypeBinding : IExpressionBinding |
| 6 | + { |
| 7 | + private readonly DeclarationFinder _declarationFinder; |
| 8 | + private readonly Declaration _module; |
| 9 | + private readonly VBAExpressionParser.SimpleNameExpressionContext _expression; |
| 10 | + |
| 11 | + public SimpleNameTypeBinding(DeclarationFinder declarationFinder, Declaration module, VBAExpressionParser.SimpleNameExpressionContext expression) |
| 12 | + { |
| 13 | + _declarationFinder = declarationFinder; |
| 14 | + _module = module; |
| 15 | + _expression = expression; |
| 16 | + } |
| 17 | + |
| 18 | + public IBoundExpression Resolve() |
| 19 | + { |
| 20 | + IBoundExpression boundExpression = null; |
| 21 | + string name = ExpressionName.GetName(_expression.name()); |
| 22 | + boundExpression = ResolveEnclodingModule(name); |
| 23 | + if (boundExpression != null) |
| 24 | + { |
| 25 | + return boundExpression; |
| 26 | + } |
| 27 | + boundExpression = ResolveEnclosingProject(name); |
| 28 | + if (boundExpression != null) |
| 29 | + { |
| 30 | + return boundExpression; |
| 31 | + } |
| 32 | + boundExpression = ResolveOtherModuleInEnclosingProject(name); |
| 33 | + if (boundExpression != null) |
| 34 | + { |
| 35 | + return boundExpression; |
| 36 | + } |
| 37 | + boundExpression = ResolveReferencedProject(name); |
| 38 | + if (boundExpression != null) |
| 39 | + { |
| 40 | + return boundExpression; |
| 41 | + } |
| 42 | + return ResolveModuleInReferencedProject(name); |
| 43 | + } |
| 44 | + |
| 45 | + private IBoundExpression ResolveEnclodingModule(string name) |
| 46 | + { |
| 47 | + /* Namespace tier 1: |
| 48 | + Enclosing Module namespace: A UDT or Enum type defined at the module-level in the |
| 49 | + enclosing module. |
| 50 | + */ |
| 51 | + var udt = _declarationFinder.Find(_module, name, DeclarationType.UserDefinedType); |
| 52 | + if (udt != null) |
| 53 | + { |
| 54 | + return new SimpleNameExpression(udt, ExpressionClassification.Type, _expression); |
| 55 | + } |
| 56 | + var enumType = _declarationFinder.Find(_module, name, DeclarationType.UserDefinedType); |
| 57 | + if (enumType != null) |
| 58 | + { |
| 59 | + return new SimpleNameExpression(enumType, ExpressionClassification.Type, _expression); |
| 60 | + } |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + private IBoundExpression ResolveEnclosingProject(string name) |
| 65 | + { |
| 66 | + /* Namespace tier 2: |
| 67 | + Enclosing Project namespace: The enclosing project itself, a referenced project, or a |
| 68 | + procedural module or class module contained in the enclosing project. |
| 69 | + */ |
| 70 | + var enclosingProjectDeclaration = _module.ParentDeclaration; |
| 71 | + if (enclosingProjectDeclaration.Project.Name == name) |
| 72 | + { |
| 73 | + return new SimpleNameExpression(enclosingProjectDeclaration, ExpressionClassification.Project, _expression); |
| 74 | + } |
| 75 | + var referencedProject = _declarationFinder.FindReferencedProject(enclosingProjectDeclaration, name); |
| 76 | + if (referencedProject != null) |
| 77 | + { |
| 78 | + return new SimpleNameExpression(referencedProject, ExpressionClassification.Type, _expression); |
| 79 | + } |
| 80 | + var proceduralModuleEnclosingProject = _declarationFinder.Find(enclosingProjectDeclaration, name, DeclarationType.Module); |
| 81 | + if (proceduralModuleEnclosingProject != null) |
| 82 | + { |
| 83 | + return new SimpleNameExpression(proceduralModuleEnclosingProject, ExpressionClassification.ProceduralModule, _expression); |
| 84 | + } |
| 85 | + var classEnclosingProject = _declarationFinder.Find(enclosingProjectDeclaration, name, DeclarationType.Class); |
| 86 | + if (classEnclosingProject != null) |
| 87 | + { |
| 88 | + return new SimpleNameExpression(classEnclosingProject, ExpressionClassification.Type, _expression); |
| 89 | + } |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + private IBoundExpression ResolveOtherModuleInEnclosingProject(string name) |
| 94 | + { |
| 95 | + /* Namespace tier 3: |
| 96 | + Other Module in Enclosing Project namespace: An accessible UDT or Enum type defined in a |
| 97 | + procedural module or class module within the enclosing project other than the enclosing module. |
| 98 | + */ |
| 99 | + Declaration enclosingProjectDeclaration = _module.ParentDeclaration; |
| 100 | + var accessibleUdt = _declarationFinder.FindAccessible(enclosingProjectDeclaration, _module, name, DeclarationType.UserDefinedType); |
| 101 | + if (accessibleUdt != null) |
| 102 | + { |
| 103 | + return new SimpleNameExpression(accessibleUdt, ExpressionClassification.Type, _expression); |
| 104 | + } |
| 105 | + var accessibleType = _declarationFinder.FindAccessible(enclosingProjectDeclaration, _module, name, DeclarationType.Enumeration); |
| 106 | + if (accessibleType != null) |
| 107 | + { |
| 108 | + return new SimpleNameExpression(accessibleType, ExpressionClassification.Type, _expression); |
| 109 | + } |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + private IBoundExpression ResolveReferencedProject(string name) |
| 114 | + { |
| 115 | + /* Namespace tier 4: |
| 116 | + Referenced Project namespace: An accessible procedural module or class module contained in |
| 117 | + a referenced project. |
| 118 | + */ |
| 119 | + var enclosingProjectDeclaration = _module.ParentDeclaration; |
| 120 | + var accessibleModule = _declarationFinder.FindInReferencedProjectModule(enclosingProjectDeclaration, name); |
| 121 | + if (accessibleModule != null) |
| 122 | + { |
| 123 | + return new SimpleNameExpression(accessibleModule, ExpressionClassification.ProceduralModule, _expression); |
| 124 | + } |
| 125 | + var accessibleClass = _declarationFinder.FindInReferencedProjectClass(enclosingProjectDeclaration, name); |
| 126 | + if (accessibleClass != null) |
| 127 | + { |
| 128 | + return new SimpleNameExpression(accessibleClass, ExpressionClassification.Type, _expression); |
| 129 | + } |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + private IBoundExpression ResolveModuleInReferencedProject(string name) |
| 134 | + { |
| 135 | + /* Namespace tier 5: |
| 136 | + Module in Referenced Project namespace: An accessible UDT or Enum type defined in a |
| 137 | + procedural module or class module within a referenced project. |
| 138 | + */ |
| 139 | + var enclosingProjectDeclaration = _module.ParentDeclaration; |
| 140 | + var referencedProjectUdt = _declarationFinder.FindInReferencedProject(enclosingProjectDeclaration, name, DeclarationType.UserDefinedType, DeclarationType.Module | DeclarationType.Class); |
| 141 | + if (referencedProjectUdt != null) |
| 142 | + { |
| 143 | + return new SimpleNameExpression(referencedProjectUdt, ExpressionClassification.Type, _expression); |
| 144 | + } |
| 145 | + var referencedProjectEnumType = _declarationFinder.FindInReferencedProject(enclosingProjectDeclaration, name, DeclarationType.Enumeration, DeclarationType.Module | DeclarationType.Class); |
| 146 | + if (referencedProjectEnumType != null) |
| 147 | + { |
| 148 | + return new SimpleNameExpression(referencedProjectEnumType, ExpressionClassification.Type, _expression); |
| 149 | + } |
| 150 | + return null; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments