Skip to content

Commit

Permalink
Merge pull request #264 from soutaro/update-rbs
Browse files Browse the repository at this point in the history
RBS 0.20.0
  • Loading branch information
soutaro committed Dec 6, 2020
2 parents 70e7e96 + 5836351 commit 3ac72ec
Show file tree
Hide file tree
Showing 21 changed files with 1,382 additions and 1,106 deletions.
2 changes: 2 additions & 0 deletions lib/steep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
require "steep/ast/builtin"
require "steep/ast/types/factory"

require "steep/interface/function"
require "steep/interface/block"
require "steep/interface/method_type"
require "steep/interface/substitution"
require "steep/interface/interface"
Expand Down
165 changes: 112 additions & 53 deletions lib/steep/ast/types/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,23 @@ def type(type)
end
Record.new(elements: elements, location: nil)
when RBS::Types::Proc
params = params(type.type)
return_type = type(type.type.return_type)
Proc.new(params: params, return_type: return_type, location: nil)
func = Interface::Function.new(
params: params(type.type),
return_type: type(type.type.return_type),
location: type.location
)
block = if type.block
Interface::Block.new(
type: Interface::Function.new(
params: params(type.block.type),
return_type: type(type.block.type.return_type),
location: type.location
),
optional: !type.block.required
)
end

Proc.new(type: func, block: block)
else
raise "Unexpected type given: #{type}"
end
Expand Down Expand Up @@ -145,8 +159,15 @@ def type_1(type)
end
RBS::Types::Record.new(fields: fields, location: nil)
when Proc
block = if type.block
RBS::Types::Block.new(
type: function_1(type.block.type),
required: !type.block.optional?
)
end
RBS::Types::Proc.new(
type: function_1(type.params, type.return_type),
type: function_1(type.type),
block: block,
location: nil
)
when Logic::Base
Expand All @@ -156,7 +177,10 @@ def type_1(type)
end
end

def function_1(params, return_type)
def function_1(func)
params = func.params
return_type = func.return_type

RBS::Types::Function.new(
required_positionals: params.required.map {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) },
optional_positionals: params.optional.map {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) },
Expand All @@ -170,7 +194,7 @@ def function_1(params, return_type)
end

def params(type)
Interface::Params.new(
Interface::Function::Params.new(
required: type.required_positionals.map {|param| type(param.type) },
optional: type.optional_positionals.map {|param| type(param.type) },
rest: type.rest_positionals&.yield_self {|param| type(param.type) },
Expand Down Expand Up @@ -202,13 +226,19 @@ def method_type(method_type, self_type:, subst2: nil, method_decls:)

type = Interface::MethodType.new(
type_params: type_params,
return_type: type(method_type.type.return_type).subst(subst),
params: params(method_type.type).subst(subst),
type: Interface::Function.new(
params: params(method_type.type).subst(subst),
return_type: type(method_type.type.return_type).subst(subst),
location: method_type.location
),
block: method_type.block&.yield_self do |block|
Interface::Block.new(
optional: !block.required,
type: Proc.new(params: params(block.type).subst(subst),
return_type: type(block.type.return_type).subst(subst), location: nil)
type: Interface::Function.new(
params: params(block.type).subst(subst),
return_type: type(block.type.return_type).subst(subst),
location: nil
)
)
end,
method_decls: method_decls
Expand Down Expand Up @@ -242,12 +272,12 @@ def method_type_1(method_type, self_type:)

type = RBS::MethodType.new(
type_params: type_params,
type: function_1(method_type.params.subst(subst), method_type.return_type.subst(subst)),
type: function_1(method_type.type.subst(subst)),
block: method_type.block&.yield_self do |block|
block_type = block.type.subst(subst)

RBS::MethodType::Block.new(
type: function_1(block_type.params, block_type.return_type),
RBS::Types::Block.new(
type: function_1(block_type),
required: !block.optional
)
end,
Expand Down Expand Up @@ -354,7 +384,9 @@ def setup_primitives(method_name, method_def, method_type)
when :is_a?, :kind_of?, :instance_of?
if defined_in == RBS::BuiltinNames::Object.name && member.instance?
return method_type.with(
return_type: AST::Types::Logic::ReceiverIsArg.new(location: method_type.return_type.location)
type: method_type.type.with(
return_type: AST::Types::Logic::ReceiverIsArg.new(location: method_type.type.return_type.location)
)
)
end

Expand All @@ -363,7 +395,9 @@ def setup_primitives(method_name, method_def, method_type)
when RBS::BuiltinNames::Object.name,
NilClassName
return method_type.with(
return_type: AST::Types::Logic::ReceiverIsNil.new(location: method_type.return_type.location)
type: method_type.type.with(
return_type: AST::Types::Logic::ReceiverIsNil.new(location: method_type.type.return_type.location)
)
)
end

Expand All @@ -373,15 +407,19 @@ def setup_primitives(method_name, method_def, method_type)
RBS::BuiltinNames::TrueClass.name,
RBS::BuiltinNames::FalseClass.name
return method_type.with(
return_type: AST::Types::Logic::Not.new(location: method_type.return_type.location)
type: method_type.type.with(
return_type: AST::Types::Logic::Not.new(location: method_type.type.return_type.location)
)
)
end

when :===
case defined_in
when RBS::BuiltinNames::Module.name
return method_type.with(
return_type: AST::Types::Logic::ArgIsReceiver.new(location: method_type.return_type.location)
type: method_type.type.with(
return_type: AST::Types::Logic::ArgIsReceiver.new(location: method_type.type.return_type.location)
)
)
end
end
Expand Down Expand Up @@ -572,14 +610,17 @@ def interface(type, private:, self_type: type)
method_types: type.types.map.with_index {|elem_type, index|
Interface::MethodType.new(
type_params: [],
params: Interface::Params.new(required: [AST::Types::Literal.new(value: index)],
optional: [],
rest: nil,
required_keywords: {},
optional_keywords: {},
rest_keywords: nil),
type: Interface::Function.new(
params: Interface::Function::Params.new(required: [AST::Types::Literal.new(value: index)],
optional: [],
rest: nil,
required_keywords: {},
optional_keywords: {},
rest_keywords: nil),
return_type: elem_type,
location: nil
),
block: nil,
return_type: elem_type,
method_decls: Set[]
)
} + aref.method_types
Expand All @@ -591,14 +632,17 @@ def interface(type, private:, self_type: type)
method_types: type.types.map.with_index {|elem_type, index|
Interface::MethodType.new(
type_params: [],
params: Interface::Params.new(required: [AST::Types::Literal.new(value: index), elem_type],
optional: [],
rest: nil,
required_keywords: {},
optional_keywords: {},
rest_keywords: nil),
type: Interface::Function.new(
params: Interface::Function::Params.new(required: [AST::Types::Literal.new(value: index), elem_type],
optional: [],
rest: nil,
required_keywords: {},
optional_keywords: {},
rest_keywords: nil),
return_type: elem_type,
location: nil
),
block: nil,
return_type: elem_type,
method_decls: Set[]
)
} + update.method_types
Expand All @@ -610,9 +654,12 @@ def interface(type, private:, self_type: type)
method_types: [
Interface::MethodType.new(
type_params: [],
params: Interface::Params.empty,
type: Interface::Function.new(
params: Interface::Function::Params.empty,
return_type: type.types[0] || AST::Builtin.nil_type,
location: nil
),
block: nil,
return_type: type.types[0] || AST::Builtin.nil_type,
method_decls: Set[]
)
]
Expand All @@ -624,9 +671,12 @@ def interface(type, private:, self_type: type)
method_types: [
Interface::MethodType.new(
type_params: [],
params: Interface::Params.empty,
type: Interface::Function.new(
params: Interface::Function::Params.empty,
return_type: type.types.last || AST::Builtin.nil_type,
location: nil
),
block: nil,
return_type: type.types.last || AST::Builtin.nil_type,
method_decls: Set[]
)
]
Expand All @@ -651,14 +701,17 @@ def interface(type, private:, self_type: type)

Interface::MethodType.new(
type_params: [],
params: Interface::Params.new(required: [key_type],
optional: [],
rest: nil,
required_keywords: {},
optional_keywords: {},
rest_keywords: nil),
type: Interface::Function.new(
params: Interface::Function::Params.new(required: [key_type],
optional: [],
rest: nil,
required_keywords: {},
optional_keywords: {},
rest_keywords: nil),
return_type: value_type,
location: nil
),
block: nil,
return_type: value_type,
method_decls: Set[]
)
} + ref.method_types
Expand All @@ -671,14 +724,16 @@ def interface(type, private:, self_type: type)
key_type = Literal.new(value: key_value, location: nil)
Interface::MethodType.new(
type_params: [],
params: Interface::Params.new(required: [key_type, value_type],
optional: [],
rest: nil,
required_keywords: {},
optional_keywords: {},
rest_keywords: nil),
type: Interface::Function.new(
params: Interface::Function::Params.new(required: [key_type, value_type],
optional: [],
rest: nil,
required_keywords: {},
optional_keywords: {},
rest_keywords: nil),
return_type: value_type,
location: nil),
block: nil,
return_type: value_type,
method_decls: Set[]
)
} + update.method_types
Expand All @@ -691,14 +746,18 @@ def interface(type, private:, self_type: type)
interface(Builtin::Proc.instance_type, private: private, self_type: self_type).tap do |interface|
method_type = Interface::MethodType.new(
type_params: [],
params: type.params,
return_type: type.return_type,
block: nil,
type: type.type,
block: type.block,
method_decls: Set[]
)

interface.methods[:[]] = Interface::Interface::Entry.new(method_types: [method_type])
interface.methods[:call] = Interface::Interface::Entry.new(method_types: [method_type])

if type.block_required?
interface.methods.delete(:[])
else
interface.methods[:[]] = Interface::Interface::Entry.new(method_types: [method_type.with(block: nil)])
end
end

when Logic::Base
Expand Down
Loading

0 comments on commit 3ac72ec

Please sign in to comment.