1 # Copyright (C) 2005-2010, Parrot Foundation.
4 =head1 PDD 21: Namespaces
8 Description and implementation of Parrot namespaces.
18 =item - Namespaces should be stored under first-level namespaces corresponding
19 to the HLL language name
21 =item - Namespaces should be hierarchical
23 =item - The C<get_namespace> opcode takes a multidimensional hash key or an
26 =item - Namespaces follow the semantics of the HLL in which they're defined
28 =item - exports follow the semantics of the library's language
30 =item - Two interfaces: typed and untyped
38 A High Level Language, such as Perl, Python, or Tcl, in contrast to PIR, which
39 is a low-level language.
41 =head3 "current namespace"
43 The I<current namespace> at runtime is the namespace associated with the
44 currently executing subroutine. PASM assigns each subroutine a namespace when
45 compilation of the subroutine begins. Don't change the associated namespace
46 of a subroutine unless you're prepared for weird consequences.
48 (PASM also has its own separate concept of current namespace which is used to
49 initialize the runtime current namespace as well as determine where to store
55 =head3 Namespace Indexing Syntax
57 Namespaces are denoted in Parrot as simple strings, multidimensional
58 hash keys, or arrays of name strings.
60 A namespace may appear in Parrot source code as the string C<"a"> or the
63 A nested namespace "b" inside the namespace "a" will appear as the key
66 There is no limit to namespace nesting.
68 =head3 Naming Conventions
70 Parrot's target languages have a wide variety of namespace models. By
71 implementing an API and standard conventions, it should be possible to
72 allow interoperability while still allowing each one to choose the best
73 internal representation.
78 =item True Root Namespace
80 The true root namespace is hidden from common usage, but it is available
81 via the C<get_root_namespace> opcode. For example:
83 $P0 = get_root_namespace
85 This root namespace stringifies to the empty string.
87 =item HLL Root Namespaces
89 Each HLL must store public items in a namespace named with the lowercased
90 name of the HLL. This is the HLL root namespace. For instance, Tcl's
91 user-created namespaces should live in the C<tcl> namespace. This
92 eliminates any accidental collisions between languages.
94 An HLL root namespace must be stored at the first level in Parrot's namespace
95 hierarchy. These top-level namespaces should also be specified in a standard
96 unicode encoding. The reasons for these restrictions is to allow compilers
97 to remain completely ignorant of each other.
99 Parrot internals are stored in the default HLL root namespace C<parrot>.
101 =item HLL Implementation Namespaces
103 Each HLL must store implementation internals (private items) in an HLL
104 root namespace named with an underscore and the lowercased name of the
105 HLL. For instance, Tcl's implementation internals should live in the
108 =item HLL User-Created Namespaces
110 Each HLL must store all user-created namespaces under the HLL root
111 namespace. It is suggested that HLLs use hierarchical namespaces to
112 practical extent. A single flat namespace can be made to work, but it
113 complicates symbol exportation.
117 =head3 Namespace PMC API
119 Most languages leave their symbols plain, which makes lookups quite
120 straightforward. Others use sigils or other mangling techniques, complicating
121 the problem of interoperability.
123 Parrot namespaces assist with interoperability by providing two interface
124 subsets: the I<untyped interface> and the I<typed interface>.
126 =head4 Untyped Interface
128 Each HLL may, when working with its own namespace objects, use the I<untyped
129 interface>, which allows direct naming in the native style of the namespace's
132 This interface consists of the standard Parrot hash interface, with all its
133 keys, values, lookups, deletions, etc. Just treat the namespace like a
134 hash. (It probably is one, really, deep down.)
136 The untyped interface also has one method:
144 $P1 = $P2.'get_name'()
148 Gets the name of the namespace $P2 as an array of strings. For example,
149 if $P2 is a Perl 5 namespace "Some::Module", within the Perl 5 HLL, then
150 get_name() on $P2 returns an array of "perl5", "Some", "Module". It
151 returns the literal namespace names as the HLL stored them, without
152 filtering for name mangling.
154 NOTE: Due to aliasing, this value may be wrong -- i.e. it may disagree with
155 the namespace name with which you found the namespace in the first place.
159 =head4 Typed Interface
161 When a given namespace's HLL is either different from the current HLL or
162 unknown, an HLL should generally use only the language-agnostic namespace
163 interface. This interface isolates HLLs from each others' naming quirks. It
164 consists of C<add_foo()>, C<find_foo()>, and C<del_foo()> methods, for
165 values of "foo" including "sub" (something executable), "namespace"
166 (something in which to find more names), and "var" (anything).
168 NOTE: The job of the typed interface is to bridge I<naming> differences, and
169 I<only> naming differences. Therefore: 1) It does not enforce, nor even
170 notice, the interface requirements of "sub" or "namespace": e.g.
171 execution of C<add_sub("foo", $P0)> does I<not> automatically guarantee
172 that $P0 is an invokable subroutine; and 2) it does not prevent
173 overwriting one type with another.
177 =item C<add_namespace>
181 $P1.'add_namespace'($S2, $P3)
185 Store $P3 as a namespace under the namespace $P1, with the name of $S2.
191 $P1.'add_sub'($S2, $P3)
195 Store $P3 as a subroutine with the name of $S2 in the namespace $P1.
201 $P1.'add_var'($S2, $P3)
205 Store $P3 as a variable with the name of $S2 in the namespace $P1.
207 IMPLEMENTATION NOTE: Perl namespace implementations may choose to implement
208 add_var() by checking which parts of the variable interface are
209 implemented by $P0 (scalar, array, and/or hash) so it can decide on an
212 =item C<del_namespace>, C<del_sub>, C<del_var>
216 $P1.'del_namespace'($S2)
222 Delete the sub, namespace, or variable named $S2 from the namespace $P1.
224 =item C<find_namespace>, C<find_sub>, C<find_var>
228 $P1 = $P2.'find_namespace'($S3)
229 $P1 = $P2.'find_sub'($S3)
230 $P1 = $P2.'find_var'($S3)
234 Find the sub, namespace, or variable named $S3 in the namespace $P2.
236 IMPLEMENTATION NOTE: Perl namespace implementations should implement
237 find_var() to check all variable sigils, but the order is not to be counted on
238 by users. If you're planning to let Python code see your module, you should
239 avoid exporting both C<our $A> and C<our @A>. (Well, you might want to
240 consider not exporting variables at all, but that's a style issue.)
246 $P1.'export_to'($P2, $P3)
250 Export items from the namespace $P1 into the namespace $P2. The items to
251 export are named in $P3, which may be an array of strings, a hash, or null.
252 If $P3 is an array of strings, interpretation of items in an array follows
253 the conventions of the source (exporting) namespace.
254 If $P3 is a hash, the keys correspond to the names in the source namespace,
255 and the values correspond to the names in the destination namespace.
256 If a hash value is null or an empty string, the name in the hash key is used.
257 A null $P3 requests the 'default' set of items.
258 Any other type passed into $P3 throws an exception.
260 The base Parrot namespace export_to() function interprets item names as
261 literals -- no wildcards or other special meaning. There is no default list
262 of items to export, so $P3 of null and $P3 of an empty array have the same
265 NOTE: Exportation may entail non-obvious, odd, or even mischievous behavior.
266 For example, Perl's pragmata are implemented as exports, and they don't
267 actually export anything.
269 IMPLEMENTATION EXAMPLES: Suppose a Perl program were to import some Tcl module
270 with an import pattern of "c*" -- something that might be expressed in Perl 6
271 as C<use tcl:Some::Module 'c*'>. This operation would import all the commands
272 that start with 'c' from the given Tcl namespace into the current Perl
273 namespace. This is so because, regardless of whether 'c*' is a Perl 6 style
274 export pattern, it I<is> a valid Tcl export pattern.
276 {XXX - The ':' for HLL is just proposed. This example will need to be
279 IMPLEMENTATION NOTE: Most namespace C<export_to> implementations will restrict
280 themselves to using the typed interface on the target namespace. However,
281 they may also decide to check the type of the target namespace and, if it
282 turns out to be of a compatible type, to use same-language shortcuts.
284 DESIGN TODO: Figure out a good convention for a default export list in the
285 base namespace PMC. Maybe a standard method "expand_export_list()"?
289 =head3 Compiler PMC API
299 $P1 = $P2.'parse_name'($S3)
303 Parse the name in $S3 using the rules specific to the compiler $P2, and
304 return an array of individual name elements.
306 For example, a Java compiler would turn 'C<a.b.c>' to C<['a','b','c']>,
307 while a Perl compiler would turn 'C<a::b::c>' into the same result.
308 Meanwhile, due to Perl's sigil rules, 'C<$a::b::c>' would become
311 =item C<get_namespace>
315 $P1 = $P2.'get_namespace'($P3)
319 Ask the compiler $P2 to find its namespace which is named by the
320 elements of the array in $P3. If $P3 is a null PMC or an empty array,
321 C<get_namespace> retrieves the base namespace for the HLL. It returns a
322 namespace PMC on success and a null PMC on failure.
324 This method allows other HLLs to know one name (the HLL) and then work with
325 that HLL's modules without having to know the name it chose for its namespace
326 tree. (If you really want to know the name, the get_name() method should work
327 on the returned namespace PMC.)
329 Note that this method is basically a convenience and/or performance hack, as
330 it does the equivalent of C<get_root_namespace> followed by
331 zero or more calls to <namespace>.get_namespace(). However, any compiler is
332 free to cheat if it doesn't get caught, e.g. to use the untyped namespace
333 interface if the language doesn't mangle namespace names.
335 =item C<load_library>
339 $P1.'load_library'($P2, $P3)
343 Ask this compiler to load a library/module named by the elements of the array
344 in $P2, with optional control information in $P3.
346 For example, Perl 5's module named "Some::Module" should be loaded using (in
347 pseudo Perl 6): C<perl5.load_library(["Some", "Module"], null)>.
349 The meaning of $P3 is compiler-specific. The only universal legal value is
350 Null, which requests a "normal" load. The meaning of "normal" varies, but
351 the ideal would be to perform only the minimal actions required.
353 On failure, an exception is thrown.
357 =head3 Subroutine PMC API
359 Some information must be available about subroutines to implement the correct
360 behavior about namespaces.
366 =item C<get_namespace>
370 $P1 = $P2.'get_namespace'()
374 Retrieve the namespace $P1 where the subroutine $P2 was defined. (As
375 opposed to the namespace(s) that it may have been exported to.)
379 =head3 Namespace Opcodes
381 The namespace opcodes all have 3 variants: one that operates from the
382 currently selected namespace (i.e. the namespace of the currently
383 executing subroutine), one that operates from the HLL root namespace
384 (identified by "hll" in the opcode name), and one that operates from the
385 true root namespace (identified by "root" in the name).
389 =item C<set_namespace>
391 =begin PIR_FRAGMENT_INVALID
393 set_namespace ['key'], $P1
394 set_hll_namespace ['key'], $P1
395 set_root_namespace ['key'], $P1
397 =end PIR_FRAGMENT_INVALID
399 Add the namespace PMC $P1 under the name denoted by a multidimensional
402 =begin PIR_FRAGMENT_INVALID
404 set_namespace $P1, $P2
405 set_hll_namespace $P1, $P2
406 set_root_namespace $P1, $P2
408 =end PIR_FRAGMENT_INVALID
410 Add the namespace PMC $P2 under the name denoted by an array of name
413 =item C<get_namespace>
418 $P1 = get_hll_namespace
419 $P1 = get_root_namespace
423 Retrieve the current namespace, the HLL root namespace, or the true
424 root namespace and store it in $P1.
426 =begin PIR_FRAGMENT_INVALID
428 $P1 = get_namespace [key]
429 $P1 = get_hll_namespace [key]
430 $P1 = get_root_namespace [key]
432 =end PIR_FRAGMENT_INVALID
434 Retrieve the namespace denoted by a multidimensional hash key and
439 $P1 = get_namespace $P2
440 $P1 = get_hll_namespace $P2
441 $P1 = get_root_namespace $P2
445 Retrieve the namespace denoted by the array of names $P2 and store it in
448 Thus, to get the "Foo::Bar" namespace from the top-level of the HLL if
449 the name was known at compile time, you could retrieve the namespace
454 $P0 = get_hll_namespace ["Foo"; "Bar"]
458 If the name was not known at compile time, you would retrieve the
459 namespace with an array instead:
463 $P1 = split "::", "Foo::Bar"
464 $P0 = get_hll_namespace $P1
468 =item C<make_namespace>
470 =begin PIR_FRAGMENT_INVALID
472 $P1 = make_namespace [key]
473 $P1 = make_hll_namespace [key]
474 $P1 = make_root_namespace [key]
476 =end PIR_FRAGMENT_INVALID
478 Create and retrieve the namespace denoted by a multidimensional hash key
479 and store it in C<$P1>. If the namespace already exists, only retrieve
482 =begin PIR_FRAGMENT_INVALID
484 $P1 = make_namespace $P2
485 $P1 = make_hll_namespace $P2
486 $P1 = make_root_namespace $P2
488 =end PIR_FRAGMENT_INVALID
490 Create and retrieve the namespace denoted by the array of names $P2 and
491 store it in C<$P1>. If the namespace already exists, only retrieve it.
498 $P1 = get_hll_global $S2
499 $P1 = get_root_global $S2
503 Retrieve the symbol named $S2 in the current namespace, HLL root
504 namespace, or true root namespace.
509 $P1 = get_global [key], $S2
510 $P1 = get_hll_global [key], $S2
511 $P1 = get_root_global [key], $S2
515 Retrieve the symbol named $S2 by a multidimensional hash key relative
516 to the current namespace, HLL root namespace, or true root namespace.
520 $P1 = get_global $P2, $S3
521 $P1 = get_hll_global $P2, $S3
522 $P1 = get_root_global $P2, $S3
526 Retrieve the symbol named $S3 by the array of names $P2 relative to the
527 current namespace, HLL root namespace, or true root namespace.
534 set_hll_global $S1, $P2
535 set_root_global $S1, $P2
539 Store $P2 as the symbol named $S1 in the current namespace, HLL root
540 namespace, or true root namespace.
545 set_global [key], $S1, $P2
546 set_hll_global [key], $S1, $P2
547 set_root_global [key], $S1, $P2
551 Store $P2 as the symbol named $S1 by a multidimensional hash key,
552 relative to the current namespace, HLL root namespace, or true root
553 namespace. If the given namespace does not exist it is created.
557 set_global $P1, $S2, $P3
558 set_hll_global $P1, $S2, $P3
559 set_root_global $P1, $S2, $P3
563 Store $P3 as the symbol named $S2 by the array of names $P1, relative to
564 the current namespace, HLL root namespace, or true root namespace. If
565 the given namespace does not exist it is created.
569 =head3 HLL Namespace Mapping
571 In order to make this work, Parrot must somehow figure out what type of
572 namespace PMC to create.
574 =head4 Default Namespace
576 The default namespace PMC will implement Parrot's current behavior.
578 =head4 Compile-time Creation
586 should map roughly to this PIR:
591 .loadlib "perl5_group"
601 In this case, the C<main> sub would be tied to Perl 5 by the C<.HLL>
602 directive, so a Perl 5 namespace would be created.
604 =head4 Run-time Creation
606 Consider the following Perl 5 program:
612 The C<Foo::> namespace is created at run-time (without any optimizations). In
613 these cases, Parrot should create the namespace based on the HLL of the PIR
614 subroutine that calls the store function.
619 .loadlib "perl5_group"
622 $P0 = new 'PerlString'
626 $P1 = new 'PerlString'
630 $P2 = split "::", $S0
635 set_global $P2, $S0, $P3
640 In this case, C<set_global> should see that it was called from "main",
641 which is in a Perl 5 namespace, so it will create the "Foo" namespace as
644 =head2 Language Notes
650 Perl 6 may wish to be able to access the namespace as a hash with sigils.
651 That is certainly possible, even with subroutines and methods. It's not
652 important that a HLL use the typed namespace API, it is only important that it
653 provides it for others to use.
655 So Perl 6 may implement C<get_keyed> and C<set_keyed> VTABLE slots that
656 allow the namespace PMC to be used as a hash. The C<find_sub> method would,
657 in this case, append a "&" sigil to the front of the sub/method name and
658 search in the internal hash.
662 =head4 Importing from Python
664 Since functions and variables overlap in Python's namespaces, when exporting
665 to another HLL's namespace, the Python namespace PMC's C<export_to> method
666 should use introspection to determine whether C<x> should be added using
667 C<add_var> or C<add_sub>. C<$I0 = does $P0, "Sub"> may be enough to decide
670 =head4 Subroutines and Namespaces
672 Since Python's subroutines and namespaces are just variables (the namespace
673 collides there), the Python PMC's C<find_var> method may return subroutines as
685 %Foo::{"&bar"} = &foo;
692 $P0 = get_global "&foo"
693 $P1 = get_namespace ["Foo"]
695 # A smart perl6 compiler would emit this,
696 # because it knows that Foo is a perl6 namespace:
699 # But a naive perl6 compiler would emit this:
700 $P1.'add_sub'("bar", $P0)
710 =head4 Cross-language Exportation
715 use tcl:Some::Module 'w*'; # XXX - ':' after HLL may change in Perl 6
716 write("this is a tcl command");
718 PIR (without error checking):
731 tcl.'load_library'(ns, $P0)
732 $P0 = tcl.'get_namespace'(ns)
734 $P0.'export_to'($P1, 'w*')
735 "write"("this is a tcl command")
750 vim: expandtab shiftwidth=4: