Class: RBS::ConstantTable
- Inherits:
-
Object
- Object
- RBS::ConstantTable
- Defined in:
- lib/rbs/constant_table.rb
Instance Attribute Summary collapse
-
#constant_scopes_cache ⇒ Object
readonly
Returns the value of attribute constant_scopes_cache.
-
#definition_builder ⇒ Object
readonly
Returns the value of attribute definition_builder.
Instance Method Summary collapse
- #absolute_type_name(name, namespace:) ⇒ Object
- #constant_scopes(name) ⇒ Object
- #constant_scopes0(name, scopes: []) ⇒ Object
- #constant_scopes_module(name, scopes:) ⇒ Object
- #env ⇒ Object
-
#initialize(builder:) ⇒ ConstantTable
constructor
A new instance of ConstantTable.
- #name_to_constant(name) ⇒ Object
- #resolve_constant_reference(name, context:) ⇒ Object
- #resolve_constant_reference_context(name, context:) ⇒ Object
- #resolve_constant_reference_inherit(name, scopes:, no_object: false) ⇒ Object
- #split_name(name) ⇒ Object
Constructor Details
#initialize(builder:) ⇒ ConstantTable
Returns a new instance of ConstantTable.
10 11 12 13 |
# File 'lib/rbs/constant_table.rb', line 10 def initialize(builder:) @definition_builder = builder @constant_scopes_cache = {} end |
Instance Attribute Details
#constant_scopes_cache ⇒ Object (readonly)
Returns the value of attribute constant_scopes_cache.
4 5 6 |
# File 'lib/rbs/constant_table.rb', line 4 def constant_scopes_cache @constant_scopes_cache end |
#definition_builder ⇒ Object (readonly)
Returns the value of attribute definition_builder.
3 4 5 |
# File 'lib/rbs/constant_table.rb', line 3 def definition_builder @definition_builder end |
Instance Method Details
#absolute_type_name(name, namespace:) ⇒ Object
144 145 146 147 148 |
# File 'lib/rbs/constant_table.rb', line 144 def absolute_type_name(name, namespace:) env.absolute_type_name(name, namespace: namespace) do raise end end |
#constant_scopes(name) ⇒ Object
81 82 83 |
# File 'lib/rbs/constant_table.rb', line 81 def constant_scopes(name) constant_scopes_cache[name] ||= constant_scopes0(name, scopes: []) end |
#constant_scopes0(name, scopes: []) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rbs/constant_table.rb', line 100 def constant_scopes0(name, scopes: []) decl = env.find_class(name) namespace = name.to_namespace case decl when AST::Declarations::Module constant_scopes0 BuiltinNames::Module.name, scopes: scopes constant_scopes_module name, scopes: scopes when AST::Declarations::Class unless name == BuiltinNames::BasicObject.name super_name = decl.super_class&.yield_self {|super_class| absolute_type_name(super_class.name, namespace: namespace) } || BuiltinNames::Object.name constant_scopes0 super_name, scopes: scopes end decl.members.each do |member| case member when AST::Members::Include constant_scopes_module absolute_type_name(member.name, namespace: namespace), scopes: scopes end end scopes.unshift namespace else raise "Unexpected declaration: #{name}" end env.each_extension(name).sort_by {|e| e.extension_name.to_s }.each do |extension| extension.members.each do |member| case member when AST::Members::Include constant_scopes_module absolute_type_name(member.name, namespace: namespace), scopes: [] end end end scopes end |
#constant_scopes_module(name, scopes:) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rbs/constant_table.rb', line 85 def constant_scopes_module(name, scopes:) decl = env.find_class(name) namespace = name.to_namespace decl.members.each do |member| case member when AST::Members::Include constant_scopes_module absolute_type_name(member.name, namespace: namespace), scopes: scopes end end scopes.unshift namespace end |
#env ⇒ Object
6 7 8 |
# File 'lib/rbs/constant_table.rb', line 6 def env definition_builder.env end |
#name_to_constant(name) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rbs/constant_table.rb', line 15 def name_to_constant(name) case when env.name_to_constant.key?(name) decl = env.name_to_constant[name] type = env.absolute_type(decl.type, namespace: name.namespace) {|type| type.name.absolute! } Constant.new(name: name, type: type, declaration: decl) when env.class?(name) decl = env.name_to_decl[name] type = Types::ClassSingleton.new(name: name, location: nil) Constant.new(name: name, type: type, declaration: decl) end end |
#resolve_constant_reference(name, context:) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rbs/constant_table.rb', line 32 def resolve_constant_reference(name, context:) head, *tail = split_name(name) head_constant = case when name.absolute? name_to_constant(TypeName.new(name: head, namespace: Namespace.root)) when !context || context.empty? name_to_constant(TypeName.new(name: head, namespace: Namespace.root)) else resolve_constant_reference_context(head, context: context) || resolve_constant_reference_inherit(head, scopes: constant_scopes(context.to_type_name)) end if head_constant tail.inject(head_constant) do |constant, name| resolve_constant_reference_inherit name, scopes: constant_scopes(constant.name), no_object: constant.name != BuiltinNames::Object.name end end end |
#resolve_constant_reference_context(name, context:) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/rbs/constant_table.rb', line 55 def resolve_constant_reference_context(name, context:) if context.empty? nil else name_to_constant(TypeName.new(name: name, namespace: context)) || resolve_constant_reference_context(name, context: context.parent) end end |
#resolve_constant_reference_inherit(name, scopes:, no_object: false) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rbs/constant_table.rb', line 64 def resolve_constant_reference_inherit(name, scopes:, no_object: false) scopes.each do |context| if context.path == [:Object] unless no_object constant = name_to_constant(TypeName.new(name: name, namespace: context)) || name_to_constant(TypeName.new(name: name, namespace: Namespace.root)) end else constant = name_to_constant(TypeName.new(name: name, namespace: context)) end return constant if constant end nil end |
#split_name(name) ⇒ Object
28 29 30 |
# File 'lib/rbs/constant_table.rb', line 28 def split_name(name) name.namespace.path + [name.name] end |