2 Copyright (C) 2008, Parrot Foundation.
7 src/pmc/lexinfo.pmc - LexInfo PMC
11 These are the vtable functions for the lexinfo PMC.
21 /* included manually to prevent breaking C++ builds -- see RT #56534*/
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
30 pmclass LexInfo extends Hash 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
51 /* there is no pmclass const_pmc flag yet */
53 interp->vtables[entry]->flags |= VTABLE_IS_CONST_PMC_FLAG;
58 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
59 "Cannot create a LexInfo PMC without an initializer");
62 VTABLE void init_pmc(PMC *sub) {
64 Parrot_LexInfo_attributes *attrs =
65 mem_allocate_zeroed_typed(Parrot_LexInfo_attributes);
66 PARROT_ASSERT(PObj_constant_TEST(SELF));
68 hash = parrot_create_hash(INTERP,
69 (PARROT_DATA_TYPE)enum_hash_int,
71 (hash_comp_fn)Parrot_str_not_equal, /* STRING compare */
72 (hash_hash_key_fn)Parrot_str_to_hashval); /* hash */
74 PMC_data(SELF) = attrs;
75 SELF.set_pointer(hash);
76 PObj_active_destroy_SET(SELF);
81 =item C<void declare_lex_preg(STRING *name, INTVAL preg)>
83 Declare a lexical variable that is an alias for a PMC register. The PIR
84 compiler calls this method in response to a ".lex STRING, PREG" directive.
86 =item C<INTVAL elements()>
88 Returns the number of elements in the LexInfo hash.
94 METHOD declare_lex_preg(STRING *name, INTVAL preg) {
95 parrot_hash_put(INTERP, (Hash *)SELF.get_pointer(), name, (void*)preg);
98 VTABLE INTVAL elements() {
99 return parrot_hash_size(INTERP, (Hash *)SELF.get_pointer());
105 =item C<PMC *inspect_str(STRING *what)>
107 Introspects this LexInfo structure. The only valid introspection key is
108 C<symbols>, which gets an array of the names of the symbols in this lexpad.
114 VTABLE PMC *inspect_str(STRING *what) {
115 if (Parrot_str_equal(interp, what, CONST_STRING(interp, "symbols"))) {
116 PMC *result = pmc_new(interp, enum_class_ResizableStringArray);
117 Hash *hash = (Hash *)SELF.get_pointer();
118 UINTVAL entries = hash->entries;
122 for (i = hash->mask; i >= 0; --i) {
123 HashBucket *bucket = hash->bi[i];
125 if (++found > entries)
126 Parrot_ex_throw_from_c_args(interp, NULL, 1,
127 "Detected corruption at LexInfo hash %p entries %d",
130 PARROT_ASSERT(bucket->key);
131 VTABLE_push_string(interp, result, (STRING *)bucket->key);
133 bucket = bucket->next;
140 Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
141 "Unknown introspection value '%S'", what);
146 =item C<void visit(visit_info *info)>
148 =item C<void freeze(visit_info *info)>
150 =item C<void thaw(visit_info *info)>
152 Freeze/thaw interface used during freeze/thaw of the Sub PMC.
153 The implementation of the Hash PMC is called.
160 VTABLE void thaw(visit_info *info) {
161 IMAGE_IO * const io = info->image_io;
163 if (info->extra_flags == EXTRA_IS_NULL) {
164 const INTVAL elems = VTABLE_shift_integer(INTERP, io);
165 const INTVAL k_type = VTABLE_shift_integer(INTERP, io);
166 const INTVAL v_type = VTABLE_shift_integer(INTERP, io);
172 PARROT_ASSERT(v_type == enum_hash_int);
173 /* TODO make a better interface for hash creation
174 * TODO create hash with needed types in the first place
178 hash = (Hash *)SELF.get_pointer();
179 hash->entries = elems;
194 F<docs/pdds/pdd20_lexical_vars.pod>, F<src/classes/lexpad.pmc>.
202 * c-file-style: "parrot"
204 * vim: expandtab shiftwidth=4: