tagged release 0.6.4
[parrot.git] / src / pmc / lexinfo.pmc
blob8f53fa42e6f1310a3ae9027d628011496deea15a
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"
23 /* included manually to prevent breaking C++ builds -- see RT #56534*/
24 #include "pmc_hash.h"
27  * LexInfo contains a constant Hash with constant string
28  * keys and integer indices - no marking and no pmc_ext
29  * needed (except for freeze/thaw
30  */
32 pmclass LexInfo need_ext provides hash no_ro {
36 =item C<void class_init()>
38 Manipulate vtable->flags so that constant PMCs are created.
39 If your inherited LexInfo is not so constant, then don't
40 do that and provide a mark() method and set the custom_mark flag.
42 =item C<init_pmc(PMC *sub)>
44 Initialize the LexInfo PMC and remember the associate
45 subroutine.
47 =cut
51     void class_init() {
53         /* there is no pmclass const_pmc flag yet */
54         if (pass == 1)
55             interp->vtables[entry]->flags |= VTABLE_IS_CONST_PMC_FLAG;
57     }
59     VTABLE void init() {
60         real_exception(INTERP, NULL, INVALID_OPERATION,
61                 "Cannot create a LexInfo PMC without an initializer");
62     }
64     VTABLE void init_pmc(PMC *sub) {
65         PARROT_ASSERT(PObj_constant_TEST(SELF));
66         PMC_pmc_val(SELF) = sub;
68         parrot_new_pmc_hash_x(SELF,
69             (PARROT_DATA_TYPE)enum_hash_int,
70             Hash_key_type_STRING,
71             (hash_comp_fn)string_equal,     /* STRING compare */
72             (hash_hash_key_fn)string_hash); /*        hash    */
74         PObj_active_destroy_SET(SELF);
75     }
79 =item C<void destroy()>
81 Frees any malloced memory held by this PMC.
83 =cut
87     void destroy() {
88         if (PMC_struct_val(SELF))
89             parrot_hash_destroy(INTERP, (Hash *)PMC_struct_val(SELF));
90     }
94 =item C<void declare_lex_preg(STRING *name, INTVAL preg)>
96 Declare a lexical variable that is an alias for a PMC register.  The PIR
97 compiler calls this method in response to a ".lex STRING, PREG" directive.
99 =item C<INTVAL elements()>
101 Returns the number of elements in the LexInfo hash.
103 =cut
107     METHOD declare_lex_preg(STRING *name, INTVAL preg) {
108         parrot_hash_put(INTERP, (Hash*)PMC_struct_val(SELF), name, (void*)preg);
109     }
111     VTABLE INTVAL elements() {
112         return parrot_hash_size(INTERP, (Hash *)PMC_struct_val(SELF));
113     }
117 =item C<void visit(visit_info *info)>
119 =item C<void freeze(visit_info *info)>
121 =item C<void thaw(visit_info *info)>
123 Freeze/thaw interface used during freeze/thaw of the Sub PMC.
124 The implementation of the Hash PMC is called.
126 =cut
130     VTABLE void visit(visit_info *info) {
131         Parrot_Hash_visit(INTERP, SELF, info);
132     }
134     VTABLE void freeze(visit_info *info) {
135         Parrot_Hash_freeze(INTERP, SELF, info);
136     }
138     VTABLE void thaw(visit_info *info) {
139         IMAGE_IO * const io = info->image_io;
141         if (info->extra_flags == EXTRA_IS_NULL) {
142             const INTVAL elems  = VTABLE_shift_integer(INTERP, io);
143             const INTVAL k_type = VTABLE_shift_integer(INTERP, io);
144             const INTVAL v_type = VTABLE_shift_integer(INTERP, io);
145             Hash        *hash;
147             UNUSED(k_type);
148             UNUSED(v_type);
150             PARROT_ASSERT(v_type == enum_hash_int);
151             /* TODO make a better interface for hash creation
152              * TODO create hash with needed types in the first place
153              */
155             SELF.init_pmc(NULL);
156             hash          = (Hash *)PMC_struct_val(SELF);
157             hash->entries = elems;
158         }
159         else {
160             SUPER(info);
161         }
162     }
168 =back
170 =head1 SEE ALSO
172 F<docs/pdds/pdd20_lexical_vars.pod>, F<src/classes/lexpad.pmc>.
174 =cut
179  * Local variables:
180  *   c-file-style: "parrot"
181  * End:
182  * vim: expandtab shiftwidth=4:
183  */