Class: RBS::Environment::ModuleEntry
- Inherits:
-
Object
- Object
- RBS::Environment::ModuleEntry
- Defined in:
- sig/environment/module_entry.rbs,
lib/rbs/environment/module_entry.rb
Overview
Represents a class entry in the environment
entry = ModuleEntry.new(TypeName.parse("::Kernel"))
entry << [nil, declaration]
entry << [[nil, TypeName.parse("::Object")], declaration]
Instance Attribute Summary collapse
-
#context_decls ⇒ Array[context_decl]
readonly
Returns the value of attribute context_decls.
-
#name ⇒ TypeName
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #<<(context_decl) ⇒ self
-
#align_params(decl) ⇒ Substitution?
Returns a substitution that renames the type parameters of the declaration to the entry's type parameters (
#type_params). - #each_decl {|arg0| ... } ⇒ Object
-
#empty? ⇒ Boolean
Returns true if the entry doesn't have any declaration.
-
#initialize(name) ⇒ ModuleEntry
constructor
A new instance of ModuleEntry.
-
#primary_decl ⇒ declaration
Returns the first declaration.
-
#self_types ⇒ Array[AST::Declarations::Module::Self]
Returns the self types of the declarations.
-
#type_params ⇒ Array[AST::TypeParam]
Returns the generics declaration.
-
#validate_type_params ⇒ void
Confirms if the type parameters in the declaration are compatible.
Constructor Details
#initialize(name) ⇒ ModuleEntry
Returns a new instance of ModuleEntry.
10 11 12 13 |
# File 'lib/rbs/environment/module_entry.rb', line 10 def initialize(name) @name = name @context_decls = [] end |
Instance Attribute Details
#context_decls ⇒ Array[context_decl] (readonly)
Returns the value of attribute context_decls.
8 9 10 |
# File 'lib/rbs/environment/module_entry.rb', line 8 def context_decls @context_decls end |
#name ⇒ TypeName (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/rbs/environment/module_entry.rb', line 6 def name @name end |
Instance Method Details
#<<(context_decl) ⇒ self
15 16 17 18 |
# File 'lib/rbs/environment/module_entry.rb', line 15 def <<(context_decl) context_decls << context_decl self end |
#align_params(decl) ⇒ Substitution?
Returns a substitution that renames the type parameters of the declaration to the entry's type parameters (#type_params)
Returns nil if the declaration uses the same type parameter names as #type_params.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'sig/environment/module_entry.rbs', line 62 def align_params(decl) entry_params = type_params decl_param_names = decl.type_params.map(&:name) return nil if decl_param_names == entry_params.map(&:name) Substitution.build( decl_param_names, entry_params.map {|param| Types::Variable.new(name: param.name, location: param.location) } ) end |
#each_decl ⇒ void #each_decl ⇒ Enumerator[declaration]
20 21 22 23 24 25 26 27 28 |
# File 'lib/rbs/environment/module_entry.rb', line 20 def each_decl(&block) if block context_decls.each do |_, decl| yield decl end else enum_for(__method__ || raise) end end |
#empty? ⇒ Boolean
Returns true if the entry doesn't have any declaration
29 30 31 |
# File 'sig/environment/module_entry.rbs', line 29 def empty? context_decls.empty? end |
#primary_decl ⇒ declaration
Returns the first declaration
This method helps using the class with ClassEntry objects.
35 36 37 |
# File 'sig/environment/module_entry.rbs', line 35 def primary_decl each_decl.first or raise end |
#self_types ⇒ Array[AST::Declarations::Module::Self]
Returns the self types of the declarations
The type variables in the self types are aligned to #type_params,
so that the self types from declarations with different type parameter
names can be compared and used with #type_params.
Note that the returned objects may be different from the ones in the
declarations, but #location points to the original declaration.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'sig/environment/module_entry.rbs', line 56 def self_types each_decl.flat_map do |decl| self_types = decl.self_types subst = align_params(decl) if self_types.empty? || subst.nil? self_types else self_types.map do |self_type| AST::Declarations::Module::Self.new( name: self_type.name, args: self_type.args.map {|type| type.sub(subst) }, location: self_type.location ) end end end.uniq end |
#type_params ⇒ Array[AST::TypeParam]
Returns the generics declaration
39 40 41 42 |
# File 'sig/environment/module_entry.rbs', line 39 def type_params validate_type_params primary_decl.type_params end |
#validate_type_params ⇒ void
This method returns an undefined value.
Confirms if the type parameters in the declaration are compatible
- Raises
GenericParameterMismatchErrorif incompatible declaration is detected.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'sig/environment/module_entry.rbs', line 45 def validate_type_params unless context_decls.empty? first_decl, *rest_decls = each_decl.to_a first_decl or raise first_params = first_decl.type_params first_names = first_params.map(&:name) rest_decls.each do |other_decl| other_params = other_decl.type_params unless first_names.size == other_params.size && first_params == AST::TypeParam.rename(other_params, new_names: first_names) raise GenericParameterMismatchError.new(name: name, decl: other_decl) end end end end |