Class: RBS::Resolver::ConstantResolver
- Inherits:
-
Object
- Object
- RBS::Resolver::ConstantResolver
- Defined in:
- lib/rbs/resolver/constant_resolver.rb
Defined Under Namespace
Classes: Table
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#child_constants_cache ⇒ Object
readonly
Returns the value of attribute child_constants_cache.
-
#context_constants_cache ⇒ Object
readonly
Returns the value of attribute context_constants_cache.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #children(module_name) ⇒ Object
- #constants(context) ⇒ Object
- #constants_from_ancestors(module_name, constants:) ⇒ Object
- #constants_from_context(context, constants:) ⇒ Object
- #constants_itself(context, constants:) ⇒ Object
-
#initialize(builder:) ⇒ ConstantResolver
constructor
A new instance of ConstantResolver.
- #load_child_constants(name) ⇒ Object
- #load_context_constants(context) ⇒ Object
- #resolve(name, context:) ⇒ Object
- #resolve_child(module_name, name) ⇒ Object
Constructor Details
#initialize(builder:) ⇒ ConstantResolver
Returns a new instance of ConstantResolver.
73 74 75 76 77 78 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 73 def initialize(builder:) @builder = builder @table = Table.new(builder.env) @context_constants_cache = {} @child_constants_cache = {} end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
70 71 72 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 70 def builder @builder end |
#child_constants_cache ⇒ Object (readonly)
Returns the value of attribute child_constants_cache.
71 72 73 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 71 def child_constants_cache @child_constants_cache end |
#context_constants_cache ⇒ Object (readonly)
Returns the value of attribute context_constants_cache.
71 72 73 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 71 def context_constants_cache @context_constants_cache end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
70 71 72 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 70 def table @table end |
Instance Method Details
#children(module_name) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 97 def children(module_name) unless child_constants_cache.key?(module_name) load_child_constants(module_name) end child_constants_cache[module_name] or raise end |
#constants(context) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 85 def constants(context) unless context_constants_cache.key?(context) load_context_constants(context) end context_constants_cache[context] end |
#constants_from_ancestors(module_name, constants:) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 162 def constants_from_ancestors(module_name, constants:) builder.ancestor_builder.instance_ancestors(module_name).ancestors.each do |ancestor| if ancestor.is_a?(Definition::Ancestor::Instance) case ancestor.source when AST::Members::Include, :super, nil consts = table.children(ancestor.name) or raise constants.merge!(consts) end end end end |
#constants_from_context(context, constants:) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 145 def constants_from_context(context, constants:) if context parent, last = context constants_from_context(parent, constants: constants) or return false if last consts = table.children(last) or return false constants.merge!(consts) end else constants.merge!(table.toplevel) end true end |
#constants_itself(context, constants:) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 174 def constants_itself(context, constants:) if context _, typename = context if typename if (ns = typename.namespace).empty? constant = table.toplevel[typename.name] or raise else hash = table.children(ns.to_type_name) or raise constant = hash[typename.name] end constants[typename.name] = constant end end end |
#load_child_constants(name) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 120 def load_child_constants(name) # @type var constants: Hash[Symbol, Constant] constants = {} if table.children(name) builder.ancestor_builder.instance_ancestors(name).ancestors.each do |ancestor| if ancestor.is_a?(Definition::Ancestor::Instance) if ancestor.name == BuiltinNames::Object.name if name != BuiltinNames::Object.name next end end case ancestor.source when AST::Members::Include, :super, nil consts = table.children(ancestor.name) or raise constants.merge!(consts) end end end end child_constants_cache[name] = constants end |
#load_context_constants(context) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 105 def load_context_constants(context) # @type var consts: Hash[Symbol, Constant] consts = {} if context if last = context[1] constants_from_ancestors(last, constants: consts) end end constants_from_context(context, constants: consts) or return constants_itself(context, constants: consts) context_constants_cache[context] = consts end |
#resolve(name, context:) ⇒ Object
80 81 82 83 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 80 def resolve(name, context:) cs = constants(context) or raise "Broken context is given" cs[name] end |
#resolve_child(module_name, name) ⇒ Object
93 94 95 |
# File 'lib/rbs/resolver/constant_resolver.rb', line 93 def resolve_child(module_name, name) children(module_name)[name] end |