Class: RBS::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/environment.rb

Defined Under Namespace

Modules: ContextUtil Classes: ClassEntry, ModuleEntry, MultiEntry, SingleEntry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



126
127
128
129
130
131
132
133
134
135
# File 'lib/rbs/environment.rb', line 126

def initialize
  @buffers = []
  @declarations = []

  @class_decls = {}
  @interface_decls = {}
  @alias_decls = {}
  @constant_decls = {}
  @global_decls = {}
end

Instance Attribute Details

#alias_declsObject (readonly)

Returns the value of attribute alias_decls.



9
10
11
# File 'lib/rbs/environment.rb', line 9

def alias_decls
  @alias_decls
end

#class_declsObject (readonly)

Returns the value of attribute class_decls.



7
8
9
# File 'lib/rbs/environment.rb', line 7

def class_decls
  @class_decls
end

#constant_declsObject (readonly)

Returns the value of attribute constant_decls.



10
11
12
# File 'lib/rbs/environment.rb', line 10

def constant_decls
  @constant_decls
end

#declarationsObject (readonly)

Returns the value of attribute declarations.



5
6
7
# File 'lib/rbs/environment.rb', line 5

def declarations
  @declarations
end

#global_declsObject (readonly)

Returns the value of attribute global_decls.



11
12
13
# File 'lib/rbs/environment.rb', line 11

def global_decls
  @global_decls
end

#interface_declsObject (readonly)

Returns the value of attribute interface_decls.



8
9
10
# File 'lib/rbs/environment.rb', line 8

def interface_decls
  @interface_decls
end

Class Method Details

.from_loader(loader) ⇒ Object



148
149
150
151
152
# File 'lib/rbs/environment.rb', line 148

def self.from_loader(loader)
  self.new.tap do |env|
    loader.load(env: env)
  end
end

Instance Method Details

#<<(decl) ⇒ Object



221
222
223
224
225
# File 'lib/rbs/environment.rb', line 221

def <<(decl)
  declarations << decl
  insert_decl(decl, outer: [], namespace: Namespace.root)
  self
end

#absolute_type(resolver, type, context:) ⇒ Object



478
479
480
481
482
# File 'lib/rbs/environment.rb', line 478

def absolute_type(resolver, type, context:)
  type.map_type_name do |name, _, _|
    absolute_type_name(resolver, name, context: context)
  end
end

#absolute_type_name(resolver, type_name, context:) ⇒ Object



474
475
476
# File 'lib/rbs/environment.rb', line 474

def absolute_type_name(resolver, type_name, context:)
  resolver.resolve(type_name, context: context) || type_name
end

#buffersObject



489
490
491
# File 'lib/rbs/environment.rb', line 489

def buffers
  buffers_decls.keys.compact
end

#buffers_declsObject



493
494
495
496
497
498
499
500
501
502
503
# File 'lib/rbs/environment.rb', line 493

def buffers_decls
  # @type var hash: Hash[Buffer, Array[AST::Declarations::t]]
  hash = {}

  declarations.each do |decl|
    location = decl.location or next
    (hash[location.buffer] ||= []) << decl
  end

  hash
end

#cache_name(cache, name:, decl:, outer:) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/rbs/environment.rb', line 154

def cache_name(cache, name:, decl:, outer:)
  if cache.key?(name)
    raise DuplicatedDeclarationError.new(_ = name, _ = decl, _ = cache[name].decl)
  end

  cache[name] = SingleEntry.new(name: name, decl: decl, outer: outer)
end

#fooObject



93
94
95
96
97
# File 'lib/rbs/environment.rb', line 93

def foo
  a = [1].sample()
  return unless a
  a + 1
end

#initialize_copy(other) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/rbs/environment.rb', line 137

def initialize_copy(other)
  @buffers = other.buffers.dup
  @declarations = other.declarations.dup

  @class_decls = other.class_decls.dup
  @interface_decls = other.interface_decls.dup
  @alias_decls = other.alias_decls.dup
  @constant_decls = other.constant_decls.dup
  @global_decls = other.global_decls.dup
end

#insert_decl(decl, outer:, namespace:) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rbs/environment.rb', line 162

def insert_decl(decl, outer:, namespace:)
  case decl
  when AST::Declarations::Class, AST::Declarations::Module
    name = decl.name.with_prefix(namespace)

    if constant_decls.key?(name)
      raise DuplicatedDeclarationError.new(name, decl, constant_decls[name].decl)
    end

    unless class_decls.key?(name)
      case decl
      when AST::Declarations::Class
        class_decls[name] ||= ClassEntry.new(name: name)
      when AST::Declarations::Module
        class_decls[name] ||= ModuleEntry.new(name: name)
      end
    end

    existing_entry = class_decls[name]

    case
    when decl.is_a?(AST::Declarations::Module) && existing_entry.is_a?(ModuleEntry)
      # @type var existing_entry: ModuleEntry
      # @type var decl: AST::Declarations::Module
      existing_entry.insert(decl: decl, outer: outer)
    when decl.is_a?(AST::Declarations::Class) && existing_entry.is_a?(ClassEntry)
      # @type var existing_entry: ClassEntry
      # @type var decl: AST::Declarations::Class
      existing_entry.insert(decl: decl, outer: outer)
    else
      raise DuplicatedDeclarationError.new(name, decl, existing_entry.decls[0].decl)
    end

    prefix = outer + [decl]
    ns = name.to_namespace
    decl.each_decl do |d|
      insert_decl(d, outer: prefix, namespace: ns)
    end

  when AST::Declarations::Interface
    cache_name interface_decls, name: decl.name.with_prefix(namespace), decl: decl, outer: outer

  when AST::Declarations::Alias
    cache_name alias_decls, name: decl.name.with_prefix(namespace), decl: decl, outer: outer

  when AST::Declarations::Constant
    name = decl.name.with_prefix(namespace)

    if class_decls.key?(name)
      raise DuplicatedDeclarationError.new(name, decl, class_decls[name].decls[0].decl)
    end

    cache_name constant_decls, name: name, decl: decl, outer: outer

  when AST::Declarations::Global
    cache_name global_decls, name: decl.name, decl: decl, outer: outer
  end
end

#inspectObject



484
485
486
487
# File 'lib/rbs/environment.rb', line 484

def inspect
  ivars = %i[@declarations @class_decls @interface_decls @alias_decls @constant_decls @global_decls]
  "\#<RBS::Environment #{ivars.map { |iv| "#{iv}=(#{instance_variable_get(iv).size} items)"}.join(' ')}>"
end

#rejectObject



505
506
507
508
509
510
511
512
513
514
515
# File 'lib/rbs/environment.rb', line 505

def reject
  env = Environment.new

  declarations.each do |decl|
    unless yield(decl)
      env << decl
    end
  end

  env
end

#resolve_declaration(resolver, decl, outer:, prefix:) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/rbs/environment.rb', line 248

def resolve_declaration(resolver, decl, outer:, prefix:)
  if decl.is_a?(AST::Declarations::Global)
    # @type var decl: AST::Declarations::Global
    return AST::Declarations::Global.new(
      name: decl.name,
      type: absolute_type(resolver, decl.type, context: [Namespace.root]),
      location: decl.location,
      comment: decl.comment
    )
  end

  context = (outer + [decl]).each.with_object([Namespace.root]) do |decl, array|
    head = array.first or raise
    array.unshift(head + decl.name.to_namespace)
  end

  outer_context = context.drop(1)

  case decl
  when AST::Declarations::Class
    outer_ = outer + [decl]
    prefix_ = prefix + decl.name.to_namespace
    AST::Declarations::Class.new(
      name: decl.name.with_prefix(prefix),
      type_params: resolve_type_params(resolver, decl.type_params, context: context),
      super_class: decl.super_class&.yield_self do |super_class|
        AST::Declarations::Class::Super.new(
          name: absolute_type_name(resolver, super_class.name, context: outer_context),
          args: super_class.args.map {|type| absolute_type(resolver, type, context: outer_context) },
          location: super_class.location
        )
      end,
      members: decl.members.map do |member|
        case member
        when AST::Members::Base
          resolve_member(resolver, member, context: context)
        when AST::Declarations::Base
          resolve_declaration(
            resolver,
            member,
            outer: outer_,
            prefix: prefix_
          )
        else
          raise
        end
      end,
      location: decl.location,
      annotations: decl.annotations,
      comment: decl.comment
    )
  when AST::Declarations::Module
    outer_ = outer + [decl]
    prefix_ = prefix + decl.name.to_namespace
    AST::Declarations::Module.new(
      name: decl.name.with_prefix(prefix),
      type_params: resolve_type_params(resolver, decl.type_params, context: context),
      self_types: decl.self_types.map do |module_self|
        AST::Declarations::Module::Self.new(
          name: absolute_type_name(resolver, module_self.name, context: context),
          args: module_self.args.map {|type| absolute_type(resolver, type, context: context) },
          location: module_self.location
        )
      end,
      members: decl.members.map do |member|
        case member
        when AST::Members::Base
          resolve_member(resolver, member, context: context)
        when AST::Declarations::Base
          resolve_declaration(
            resolver,
            member,
            outer: outer_,
            prefix: prefix_
          )
        else
          raise
        end
      end,
      location: decl.location,
      annotations: decl.annotations,
      comment: decl.comment
    )
  when AST::Declarations::Interface
    AST::Declarations::Interface.new(
      name: decl.name.with_prefix(prefix),
      type_params: resolve_type_params(resolver, decl.type_params, context: context),
      members: decl.members.map do |member|
        resolve_member(resolver, member, context: context)
      end,
      comment: decl.comment,
      location: decl.location,
      annotations: decl.annotations
    )
  when AST::Declarations::Alias
    AST::Declarations::Alias.new(
      name: decl.name.with_prefix(prefix),
      type_params: resolve_type_params(resolver, decl.type_params, context: context),
      type: absolute_type(resolver, decl.type, context: context),
      location: decl.location,
      annotations: decl.annotations,
      comment: decl.comment
    )

  when AST::Declarations::Constant
    AST::Declarations::Constant.new(
      name: decl.name.with_prefix(prefix),
      type: absolute_type(resolver, decl.type, context: context),
      location: decl.location,
      comment: decl.comment
    )
  end
end

#resolve_member(resolver, member, context:) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/rbs/environment.rb', line 362

def resolve_member(resolver, member, context:)
  case member
  when AST::Members::MethodDefinition
    AST::Members::MethodDefinition.new(
      name: member.name,
      kind: member.kind,
      types: member.types.map do |type|
        resolve_method_type(resolver, type, context: context)
      end,
      comment: member.comment,
      overload: member.overload?,
      annotations: member.annotations,
      location: member.location,
      visibility: member.visibility
    )
  when AST::Members::AttrAccessor
    AST::Members::AttrAccessor.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      kind: member.kind,
      annotations: member.annotations,
      comment: member.comment,
      location: member.location,
      ivar_name: member.ivar_name,
      visibility: member.visibility
    )
  when AST::Members::AttrReader
    AST::Members::AttrReader.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      kind: member.kind,
      annotations: member.annotations,
      comment: member.comment,
      location: member.location,
      ivar_name: member.ivar_name,
      visibility: member.visibility
    )
  when AST::Members::AttrWriter
    AST::Members::AttrWriter.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      kind: member.kind,
      annotations: member.annotations,
      comment: member.comment,
      location: member.location,
      ivar_name: member.ivar_name,
      visibility: member.visibility
    )
  when AST::Members::InstanceVariable
    AST::Members::InstanceVariable.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      comment: member.comment,
      location: member.location
    )
  when AST::Members::ClassInstanceVariable
    AST::Members::ClassInstanceVariable.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      comment: member.comment,
      location: member.location
    )
  when AST::Members::ClassVariable
    AST::Members::ClassVariable.new(
      name: member.name,
      type: absolute_type(resolver, member.type, context: context),
      comment: member.comment,
      location: member.location
    )
  when AST::Members::Include
    AST::Members::Include.new(
      name: absolute_type_name(resolver, member.name, context: context),
      args: member.args.map {|type| absolute_type(resolver, type, context: context) },
      comment: member.comment,
      location: member.location,
      annotations: member.annotations
    )
  when AST::Members::Extend
    AST::Members::Extend.new(
      name: absolute_type_name(resolver, member.name, context: context),
      args: member.args.map {|type| absolute_type(resolver, type, context: context) },
      comment: member.comment,
      location: member.location,
      annotations: member.annotations
    )
  when AST::Members::Prepend
    AST::Members::Prepend.new(
      name: absolute_type_name(resolver, member.name, context: context),
      args: member.args.map {|type| absolute_type(resolver, type, context: context) },
      comment: member.comment,
      location: member.location,
      annotations: member.annotations
    )
  else
    member
  end
end

#resolve_method_type(resolver, type, context:) ⇒ Object



460
461
462
463
464
465
466
# File 'lib/rbs/environment.rb', line 460

def resolve_method_type(resolver, type, context:)
  type.map_type do |ty|
    absolute_type(resolver, ty, context: context)
  end.map_type_bound do |bound|
    _ = absolute_type(resolver, bound, context: context)
  end
end

#resolve_type_names(only: nil) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/rbs/environment.rb', line 233

def resolve_type_names(only: nil)
  resolver = TypeNameResolver.from_env(self)
  env = Environment.new()

  declarations.each do |decl|
    if only && !only.member?(decl)
      env << decl
    else
      env << resolve_declaration(resolver, decl, outer: [], prefix: Namespace.root)
    end
  end

  env
end

#resolve_type_params(resolver, params, context:) ⇒ Object



468
469
470
471
472
# File 'lib/rbs/environment.rb', line 468

def resolve_type_params(resolver, params, context:)
  params.map do |param|
    param.map_type {|type| _ = absolute_type(resolver, type, context: context) }
  end
end

#validate_type_paramsObject



227
228
229
230
231
# File 'lib/rbs/environment.rb', line 227

def validate_type_params
  class_decls.each_value do |decl|
    decl.primary
  end
end