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
-
#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_type_names(only: nil) ⇒ Object
- #validate_type_params ⇒ Object
Constructor Details
#initialize ⇒ Environment
Returns a new instance of Environment.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rbs/environment.rb', line 105 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
127 128 129 130 131 |
# File 'lib/rbs/environment.rb', line 127 def self.from_loader(loader) self.new.tap do |env| loader.load(env: env) end end |
Instance Method Details
#<<(decl) ⇒ Object
200 201 202 203 204 |
# File 'lib/rbs/environment.rb', line 200 def <<(decl) declarations << decl insert_decl(decl, outer: [], namespace: Namespace.root) self end |
#absolute_type(resolver, type, context:) ⇒ Object
436 437 438 439 440 |
# File 'lib/rbs/environment.rb', line 436 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
432 433 434 |
# File 'lib/rbs/environment.rb', line 432 def absolute_type_name(resolver, type_name, context:) resolver.resolve(type_name, context: context) || type_name end |
#buffers ⇒ Object
447 448 449 |
# File 'lib/rbs/environment.rb', line 447 def buffers buffers_decls.keys.compact end |
#buffers_decls ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/rbs/environment.rb', line 451 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
133 134 135 136 137 138 139 |
# File 'lib/rbs/environment.rb', line 133 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 |
#initialize_copy(other) ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rbs/environment.rb', line 116 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 |
# File 'lib/rbs/environment.rb', line 141 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
442 443 444 445 |
# File 'lib/rbs/environment.rb', line 442 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
463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/rbs/environment.rb', line 463 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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 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 |
# File 'lib/rbs/environment.rb', line 227 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 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: decl.type_params, super_class: decl.super_class&.yield_self do |super_class| AST::Declarations::Class::Super.new( name: absolute_type_name(resolver, super_class.name, context: context), args: super_class.args.map {|type| absolute_type(resolver, type, context: 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: decl.type_params, 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: decl.type_params, 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: 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
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 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 |
# File 'lib/rbs/environment.rb', line 338 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| type.map_type {|ty| absolute_type(resolver, ty, context: context) } end, comment: member.comment, overload: member.overload?, annotations: member.annotations, location: member.location ) 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 ) 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 ) 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 ) 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_type_names(only: nil) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/rbs/environment.rb', line 212 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 |
#validate_type_params ⇒ Object
206 207 208 209 210 |
# File 'lib/rbs/environment.rb', line 206 def validate_type_params class_decls.each_value do |decl| decl.primary end end |