Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[enum] defer creation of type object until we know the base type
now setting compilation blows up in term:sym<name> with Can only use nqp_get_sc_for_object with a SixModelObject
  • Loading branch information
moritz committed May 11, 2012
1 parent 417d760 commit 24a3016
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
51 changes: 40 additions & 11 deletions src/Perl6/Actions.pm
Expand Up @@ -2252,21 +2252,33 @@ class Perl6::Actions is HLL::Actions {
# Get, or find, enumeration base type and create type object with
# correct base type.
my $longname := $<longname> ?? $*W.disect_longname($<longname>) !! 0;
my $base_type := $*OFTYPE ?? $*OFTYPE.ast !! $*W.find_symbol(['Int']);
my $name := $<longname> ?? $longname.name() !! $<variable><desigilname>;
my $type_obj := $*W.pkg_create_mo($/, %*HOW<enum>, :name($name), :base_type($base_type));

# Add roles (which will provide the enum-related methods).
$*W.apply_trait('&trait_mod:<does>', $type_obj, $*W.find_symbol(['Enumeration']));
if pir::type_check__IPP($type_obj, $*W.find_symbol(['Numeric'])) {
$*W.apply_trait('&trait_mod:<does>', $type_obj, $*W.find_symbol(['NumericEnumeration']));
my $type_obj;
my sub make_type_obj($base_type) {
$type_obj := $*W.pkg_create_mo($/, %*HOW<enum>, :$name, :$base_type);
# Add roles (which will provide the enum-related methods).
$*W.apply_trait('&trait_mod:<does>', $type_obj, $*W.find_symbol(['Enumeration']));
if pir::type_check__IPP($type_obj, $*W.find_symbol(['Numeric'])) {
$*W.apply_trait('&trait_mod:<does>', $type_obj, $*W.find_symbol(['NumericEnumeration']));
}
if pir::type_check__IPP($type_obj, $*W.find_symbol(['Stringy'])) {
$*W.apply_trait('&trait_mod:<does>', $type_obj, $*W.find_symbol(['StringyEnumeration']));
}
# Apply traits, compose and install package.
for $<trait> {
($_.ast)($type_obj) if $_.ast;
}
$*W.pkg_compose($type_obj);
}

# Apply traits, compose and install package.
for $<trait> {
($_.ast)($type_obj) if $_.ast;
my $base_type;
my $has_base_type;
if $*OFTYPE {
$base_type := $*OFTYPE.ast;
$has_base_type := 1;
make_type_obj($base_type);
}
$*W.pkg_compose($type_obj);

if $<variable> {
$*W.throw($/, 'X::Comp::NYI',
feature => "Variable case of enums",
Expand Down Expand Up @@ -2330,6 +2342,21 @@ class Perl6::Actions is HLL::Actions {
$*W.throw($/, ['X', 'Value', 'Dynamic'], what => 'Enumeration');
}
}
if $has_base_type {
unless pir::type_check__IPP($cur_value, $base_type) {
$/.CURSOR.panic("Type error in enum. Got '"
~ $cur_value.HOW.name($cur_value)
~ "' Expected: '"
~ $base_type.HOW.name($base_type)
~ "'"
);
}
}
else {
$base_type := $cur_value.WHAT;
$has_base_type := 1;
make_type_obj($base_type);
}
}
else {
$cur_key := $_<compile_time_value>;
Expand All @@ -2346,6 +2373,8 @@ class Perl6::Actions is HLL::Actions {
$*W.install_package_symbol($*PACKAGE, nqp::unbox_s($cur_key), $val_obj);
}
}
# create a type object even for empty enums
make_type_obj unless $has_base_type;

# We evaluate to the enum type object.
make $*W.get_ref($type_obj);
Expand Down
5 changes: 1 addition & 4 deletions src/Perl6/World.pm
Expand Up @@ -1214,11 +1214,8 @@ class Perl6::World is HLL::World {
# Adds a value to an enumeration.
method create_enum_value($enum_type_obj, $key, $value) {
# Create directly.
my $val := pir::repr_box_int__PiP($value, $enum_type_obj);
my $base_type := ($enum_type_obj.HOW.parents($enum_type_obj, :local(1)))[0];
my $val := pir::repr_change_type__0PP($value, $enum_type_obj);
nqp::bindattr($val, $enum_type_obj, '$!key', $key);
nqp::bindattr($val, $enum_type_obj, '$!value',
pir::repr_box_int__PiP($value, $base_type));
self.add_object($val);

# Add to meta-object.
Expand Down
5 changes: 5 additions & 0 deletions src/core/Enumeration.pm
Expand Up @@ -46,6 +46,11 @@ my role NumericEnumeration {
self.key
}
}
my role StringyEnumeration {
multi method Str(::?CLASS:D:) {
self.value
}
}

sub ANON_ENUM(*@args) {
my Mu $prev = -1;
Expand Down

0 comments on commit 24a3016

Please sign in to comment.