* t/pmc/complex.t:
[parrot.git] / src / pmc / lexinfo.pmc
blob8c60031e0f4c793b36dd6559c3499e015e90cec0
1 /*
2 Copyright (C) 2008, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/lexinfo.pmc - LexInfo PMC
9 =head1 DESCRIPTION
11 These are the vtable functions for the lexinfo PMC.
13 =head2 Functions
15 =over 4
17 =cut
21 #include "parrot/parrot.h"
22 #include "pmc_hash.h"
25  * LexInfo contains a constant Hash with constant string
26  * keys and integer indices - no marking and no pmc_ext
27  * needed (except for freeze/thaw
28  */
30 pmclass LexInfo need_ext provides hash no_ro {
34 =item C<void class_init()>
36 Manipulate vtable->flags so that constant PMCs are created.
37 If your inherited LexInfo is not so constant, then don't
38 do that and provide a mark() method and set the custom_mark flag.
40 =item C<init_pmc(PMC *sub)>
42 Initialize the LexInfo PMC and remember the associate
43 subroutine.
45 =cut
49     void class_init() {
51         /* there is no pmclass const_pmc flag yet */
52         if (pass == 1)
53             interp->vtables[entry]->flags |= VTABLE_IS_CONST_PMC_FLAG;
55     }
57     VTABLE void init() {
58         real_exception(INTERP, NULL, INVALID_OPERATION,
59                 "Cannot create a LexInfo PMC without an initializer");
60     }
62     VTABLE void init_pmc(PMC *sub) {
63         PARROT_ASSERT(PObj_constant_TEST(SELF));
64         PMC_pmc_val(SELF) = sub;
66         parrot_new_pmc_hash_x(SELF,
67             (PARROT_DATA_TYPE)enum_hash_int,
68             Hash_key_type_STRING,
69             (hash_comp_fn)string_equal,     /* STRING compare */
70             (hash_hash_key_fn)string_hash); /*        hash    */
72         PObj_active_destroy_SET(SELF);
73     }
77 =item C<void destroy()>
79 Frees any malloced memory held by this PMC.
81 =cut
85     void destroy() {
86         if (PMC_struct_val(SELF))
87             parrot_hash_destroy(INTERP, (Hash *)PMC_struct_val(SELF));
88     }
92 =item C<void declare_lex_preg(STRING *name, INTVAL preg)>
94 Declare a lexical variable that is an alias for a PMC register.  The PIR
95 compiler calls this method in response to a ".lex STRING, PREG" directive.
97 =item C<INTVAL elements()>
99 Returns the number of elements in the LexInfo hash.
101 =cut
105     METHOD declare_lex_preg(STRING *name, INTVAL preg) {
106         parrot_hash_put(INTERP, (Hash*)PMC_struct_val(SELF), name, (void*)preg);
107     }
109     VTABLE INTVAL elements() {
110         return parrot_hash_size(INTERP, (Hash *)PMC_struct_val(SELF));
111     }
115 =item C<void visit(visit_info *info)>
117 =item C<void freeze(visit_info *info)>
119 =item C<void thaw(visit_info *info)>
121 Freeze/thaw interface used during freeze/thaw of the Sub PMC.
122 The implementation of the Hash PMC is called.
124 =cut
128     VTABLE void visit(visit_info *info) {
129         Parrot_Hash_visit(INTERP, SELF, info);
130     }
132     VTABLE void freeze(visit_info *info) {
133         Parrot_Hash_freeze(INTERP, SELF, info);
134     }
136     VTABLE void thaw(visit_info *info) {
137         IMAGE_IO * const io = info->image_io;
139         if (info->extra_flags == EXTRA_IS_NULL) {
140             const INTVAL elems  = VTABLE_shift_integer(INTERP, io);
141             const INTVAL k_type = VTABLE_shift_integer(INTERP, io);
142             const INTVAL v_type = VTABLE_shift_integer(INTERP, io);
143             Hash        *hash;
145             UNUSED(k_type);
146             UNUSED(v_type);
148             PARROT_ASSERT(v_type == enum_hash_int);
149             /* TODO make a better interface for hash creation
150              * TODO create hash with needed types in the first place
151              */
153             SELF.init_pmc(NULL);
154             hash          = (Hash *)PMC_struct_val(SELF);
155             hash->entries = elems;
156         }
157         else {
158             SUPER(info);
159         }
160     }
166 =back
168 =head1 SEE ALSO
170 F<docs/pdds/pdd20_lexical_vars.pod>, F<src/classes/lexpad.pmc>.
172 =cut
177  * Local variables:
178  *   c-file-style: "parrot"
179  * End:
180  * vim: expandtab shiftwidth=4:
181  */