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.
-
#buffers ⇒ Object
readonly
Returns the value of attribute buffers.
-
#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
- #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
- #resolve_declaration(resolver, decl, outer:, prefix:) ⇒ Object
- #resolve_member(resolver, member, context:) ⇒ Object
- #resolve_type_names ⇒ Object
Constructor Details
#initialize ⇒ Environment
Returns a new instance of Environment.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rbs/environment.rb', line 99 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.
8 9 10 |
# File 'lib/rbs/environment.rb', line 8 def alias_decls @alias_decls end |
#buffers ⇒ Object (readonly)
Returns the value of attribute buffers.
3 4 5 |
# File 'lib/rbs/environment.rb', line 3 def buffers @buffers end |
#class_decls ⇒ Object (readonly)
Returns the value of attribute class_decls.
6 7 8 |
# File 'lib/rbs/environment.rb', line 6 def class_decls @class_decls end |
#constant_decls ⇒ Object (readonly)
Returns the value of attribute constant_decls.
9 10 11 |
# File 'lib/rbs/environment.rb', line 9 def constant_decls @constant_decls end |
#declarations ⇒ Object (readonly)
Returns the value of attribute declarations.
4 5 6 |
# File 'lib/rbs/environment.rb', line 4 def declarations @declarations end |
#global_decls ⇒ Object (readonly)
Returns the value of attribute global_decls.
10 11 12 |
# File 'lib/rbs/environment.rb', line 10 def global_decls @global_decls end |
#interface_decls ⇒ Object (readonly)
Returns the value of attribute interface_decls.
7 8 9 |
# File 'lib/rbs/environment.rb', line 7 def interface_decls @interface_decls end |
Class Method Details
.from_loader(loader) ⇒ Object
121 122 123 124 125 |
# File 'lib/rbs/environment.rb', line 121 def self.from_loader(loader) self.new.tap do |env| loader.load(env: env) end end |
Instance Method Details
#<<(decl) ⇒ Object
195 196 197 198 199 |
# File 'lib/rbs/environment.rb', line 195 def <<(decl) declarations << decl insert_decl(decl, outer: [], namespace: Namespace.root) self end |
#absolute_type(resolver, type, context:) ⇒ Object
412 413 414 415 416 |
# File 'lib/rbs/environment.rb', line 412 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
408 409 410 |
# File 'lib/rbs/environment.rb', line 408 def absolute_type_name(resolver, type_name, context:) resolver.resolve(type_name, context: context) || type_name end |
#cache_name(cache, name:, decl:, outer:) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/rbs/environment.rb', line 127 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
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rbs/environment.rb', line 110 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
135 136 137 138 139 140 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 |
# File 'lib/rbs/environment.rb', line 135 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) # OK when decl.is_a?(AST::Declarations::Class) && existing_entry.is_a?(ClassEntry) # OK else raise DuplicatedDeclarationError.new(name, decl, existing_entry.primary.decl) end existing_entry.insert(decl: decl, outer: outer) 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 when AST::Declarations::Extension RBS.logger.warn "#{Location.to_string decl.location} Extension construct is deprecated: use class/module syntax instead" end end |
#inspect ⇒ Object
418 419 420 421 |
# File 'lib/rbs/environment.rb', line 418 def inspect ivars = %i[@buffers @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 |
#resolve_declaration(resolver, decl, outer:, prefix:) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 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 |
# File 'lib/rbs/environment.rb', line 212 def resolve_declaration(resolver, decl, outer:, prefix:) if decl.is_a?(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| array.unshift(array.first + 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) } ) 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_ ) 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_ ) 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
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 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 |
# File 'lib/rbs/environment.rb', line 316 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, attributes: member.attributes, location: member.location ) when AST::Members::AttrAccessor AST::Members::AttrAccessor.new( name: member.name, type: absolute_type(resolver, member.type, context: context), 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), 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), 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 ⇒ Object
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rbs/environment.rb', line 201 def resolve_type_names resolver = TypeNameResolver.from_env(self) env = Environment.new() declarations.each do |decl| env << resolve_declaration(resolver, decl, outer: [], prefix: Namespace.root) end env end |