Class: RBS::Environment
- Inherits:
-
Object
- Object
- RBS::Environment
- Defined in:
- lib/rbs/environment.rb
Defined Under Namespace
Modules: ContextUtil Classes: ClassEntry, ModuleEntry, MultiEntry, SingleEntry
Instance Attribute Summary collapse
-
#alias_decls ⇒ Object
readonly
Returns the value of attribute alias_decls.
-
#class_decls ⇒ Object
readonly
Returns the value of attribute class_decls.
-
#constant_decls ⇒ Object
readonly
Returns the value of attribute constant_decls.
-
#declarations ⇒ Object
readonly
Returns the value of attribute declarations.
-
#global_decls ⇒ Object
readonly
Returns the value of attribute global_decls.
-
#interface_decls ⇒ Object
readonly
Returns the value of attribute interface_decls.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(decl) ⇒ Object
- #absolute_type(resolver, type, context:) ⇒ Object
- #absolute_type_name(resolver, type_name, context:) ⇒ Object
- #buffers ⇒ Object
- #buffers_decls ⇒ Object
- #cache_name(cache, name:, decl:, outer:) ⇒ Object
- #foo ⇒ Object
-
#initialize ⇒ Environment
constructor
A new instance of Environment.
- #initialize_copy(other) ⇒ Object
- #insert_decl(decl, outer:, namespace:) ⇒ Object
- #inspect ⇒ Object
- #reject ⇒ Object
- #resolve_declaration(resolver, decl, outer:, prefix:) ⇒ Object
- #resolve_member(resolver, member, context:) ⇒ Object
- #resolve_method_type(resolver, type, context:) ⇒ Object
- #resolve_type_names(only: nil) ⇒ Object
- #resolve_type_params(resolver, params, context:) ⇒ Object
- #validate_type_params ⇒ Object
Constructor Details
#initialize ⇒ Environment
Returns a new instance of Environment.
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rbs/environment.rb', line 124 def initialize @buffers = [] @declarations = [] @class_decls = {} @interface_decls = {} @alias_decls = {} @constant_decls = {} @global_decls = {} end |
Instance Attribute Details
#alias_decls ⇒ Object (readonly)
Returns the value of attribute alias_decls.
7 8 9 |
# File 'lib/rbs/environment.rb', line 7 def alias_decls @alias_decls end |
#class_decls ⇒ Object (readonly)
Returns the value of attribute class_decls.
5 6 7 |
# File 'lib/rbs/environment.rb', line 5 def class_decls @class_decls end |
#constant_decls ⇒ Object (readonly)
Returns the value of attribute constant_decls.
8 9 10 |
# File 'lib/rbs/environment.rb', line 8 def constant_decls @constant_decls end |
#declarations ⇒ Object (readonly)
Returns the value of attribute declarations.
3 4 5 |
# File 'lib/rbs/environment.rb', line 3 def declarations @declarations end |
#global_decls ⇒ Object (readonly)
Returns the value of attribute global_decls.
9 10 11 |
# File 'lib/rbs/environment.rb', line 9 def global_decls @global_decls end |
#interface_decls ⇒ Object (readonly)
Returns the value of attribute interface_decls.
6 7 8 |
# File 'lib/rbs/environment.rb', line 6 def interface_decls @interface_decls end |
Class Method Details
.from_loader(loader) ⇒ Object
146 147 148 149 150 |
# File 'lib/rbs/environment.rb', line 146 def self.from_loader(loader) self.new.tap do |env| loader.load(env: env) end end |
Instance Method Details
#<<(decl) ⇒ Object
219 220 221 222 223 |
# File 'lib/rbs/environment.rb', line 219 def <<(decl) declarations << decl insert_decl(decl, outer: [], namespace: Namespace.root) self end |
#absolute_type(resolver, type, context:) ⇒ Object
476 477 478 479 480 |
# File 'lib/rbs/environment.rb', line 476 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
472 473 474 |
# File 'lib/rbs/environment.rb', line 472 def absolute_type_name(resolver, type_name, context:) resolver.resolve(type_name, context: context) || type_name end |
#buffers ⇒ Object
487 488 489 |
# File 'lib/rbs/environment.rb', line 487 def buffers buffers_decls.keys.compact end |
#buffers_decls ⇒ Object
491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/rbs/environment.rb', line 491 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
152 153 154 155 156 157 158 |
# File 'lib/rbs/environment.rb', line 152 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 |
#foo ⇒ Object
91 92 93 94 95 |
# File 'lib/rbs/environment.rb', line 91 def foo a = [1].sample() return unless a a + 1 end |
#initialize_copy(other) ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rbs/environment.rb', line 135 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
160 161 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 |
# File 'lib/rbs/environment.rb', line 160 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 |
#inspect ⇒ Object
482 483 484 485 |
# File 'lib/rbs/environment.rb', line 482 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 |
#reject ⇒ Object
503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/rbs/environment.rb', line 503 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
246 247 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 |
# File 'lib/rbs/environment.rb', line 246 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
360 361 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 |
# File 'lib/rbs/environment.rb', line 360 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
458 459 460 461 462 463 464 |
# File 'lib/rbs/environment.rb', line 458 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
231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/rbs/environment.rb', line 231 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
466 467 468 469 470 |
# File 'lib/rbs/environment.rb', line 466 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_params ⇒ Object
225 226 227 228 229 |
# File 'lib/rbs/environment.rb', line 225 def validate_type_params class_decls.each_value do |decl| decl.primary end end |