Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
language:namespaces [2020/05/01 16:44]
rajit [Importing namespaces]
language:namespaces [2024/07/27 12:52] (current)
rajit [Renaming namespaces]
Line 12: Line 12:
 A namespace is created by using the ''namespace'' construct. A namespace is created by using the ''namespace'' construct.
  
-<code>+<code act>
 namespace lib { namespace lib {
 ... ...
Line 38: Line 38:
 namespaces is shown below. namespaces is shown below.
  
-<code>+<code act>
 namespace datapath { namespace datapath {
     export defproc bus_interface(...) { ... }     export defproc bus_interface(...) { ... }
Line 64: Line 64:
 the entire namespace ''adder'' can be exported as follows: the entire namespace ''adder'' can be exported as follows:
  
-<code>+<code act>
 namespace datapath { namespace datapath {
     export defproc bus_interface(...) { ... }     export defproc bus_interface(...) { ... }
Line 93: Line 93:
 The basic form of import statement is shown below: The basic form of import statement is shown below:
  
-<code>+<code act>
 import "channel.act"; import "channel.act";
 ... ...
Line 107: Line 107:
  
  
-There are a few things that might create issues in such a situation. +A second version of import uses namespaces directlybut requires that 
-First, duplicate namespaces might existespecially when re-using old +ACT files be placed in locations that match the namespace 
-files. For instance, suppose we have two files: ''lib1.act'' and +hierarchyThe import statement
-''lib2.act'' both containing namespace ''lib'', but having +
-definitions that are useful. Importing both would result in the union of +
-the namespaces, and could create naming conflicts (e.g. multiple +
-definition of types having the same name---an error). To solve this +
-problem, one can do the following:+
  
-<code+<code act> 
-import "lib1.act"; +import processor::lib;
-open lib -lib1; +
-import "lib2.act"; +
-open lib -> lib2;+
 </code> </code>
  
-The ''open'' construct enables one to rename a namespace. Once this +is equivalent to the following: 
-has occuredthere cannot be any naming conflicts. This version of + 
-''open'' is a renaming construct. The old name for the namespace is +<code act> 
-eliminated.+import "processor/lib/_all_.act"; 
 +</code> 
 + 
 +It assumes that the file ''_all_.act'' in the directory 
 +''processor/lib'' contains all the definitions corresponding to the 
 +''processor::lib'' namespace. More preciselyan import statement 
 + 
 +<code act> 
 +import foo; 
 +</code> 
 + 
 +would do the following: 
 +  * Look for ''foo/_all_.act'' using the search paths specified earlier; 
 +  * If unsuccessful, then look for ''foo.act'' 
 +  * If unsuccessful, report an error 
 + 
 +After the import, the specified namespace is checked to see if it existsIf not, an error will be reported. 
 + 
 + 
 +===== Opening namespaces =====
  
-A second issue is one that is more about convenience. Consider a +If  a project has many different people working on it, it can be convenient 
-project that has many different people working on it, each in their+to have each component/module in their
 own namespace to avoid naming conflicts. This situation can result in own namespace to avoid naming conflicts. This situation can result in
 very long type names. Plus it would be more bookkeeping to have to very long type names. Plus it would be more bookkeeping to have to
 create a test environment for the types within, say, create a test environment for the types within, say,
 ''processor::lib''---not just because of the long type names, but ''processor::lib''---not just because of the long type names, but
-because not all types might be exported! In this case we can say:+because not all types might be exported! 
  
-<code>+As a syntactic convenience, we can say: 
 + 
 +<code act>
 import "lib.act"; import "lib.act";
 open processor::lib; open processor::lib;
Line 148: Line 161:
 namespace, not just the ones that are exported (including types within namespace, not just the ones that are exported (including types within
 nested namespaces). Note that this ''open'' statement will fail if nested namespaces). Note that this ''open'' statement will fail if
-all types cannot be uniquely resolved. +all types cannot be uniquely resolved. Note that the sequence of ''open'' and ''import'' statements can only be
- +
-The sequence of ''open'' and ''import'' statements can only be+
 at the beginning of an ACT file. at the beginning of an ACT file.
  
-A second version of import uses namespaces directly, but requires that +===== Renaming namespaces =====
-ACT files be placed in locations that match the namespace +
-hierarchy. The import statement+
  
-<code> 
-import processor::lib; 
-</code> 
  
-is equivalent to the following:+If there are two different files that define the same namespace (say defined in multiple projects), importing both the files may result in type conflicts. Consider a scenario where we have two ''lib'' namespaces defined for two different projects, but  both have useful circuits that we would like to re-use in a third project. If ''lib1.act'' and ''lib2.act'' both contain namespace ''lib'', importing both would take the union of the definitions in the two files, potentially resulting in errors.
  
-<code> +To resolve this issue, ACT provides a way to rename a namespace that has been imported. 
-import "processor/lib/_all_.act";+ 
 +<code act
 +import "lib1.act"; 
 +open lib -> lib1; 
 +import "lib2.act"
 +open lib -> lib2;
 </code> </code>
  
-It assumes that the file ''_all_.act'' in the directory +In this example, the ''open'' construct enables one to rename a namespace. Once this 
-''processor/lib'' contains all the definitions corresponding to the +has occured, there cannot be any naming conflicts. This version of ''open'' is a renaming construct; after the open statement, the old name for the namespace is eliminated.
-''processor::lib'' namespace. +
- +
-===== Opening namespaces ===== +
- +
- +
  
 +Another renaming scenario that can be useful is to move a namespace into another one.
 +<code act>
 +import lib;
 +import lib => priv;
 +</code>
 +The first import statement above loads in the ''lib'' namespace. The second moves the namespace //into// ''priv''. If ''priv'' doesn't exist as a namespace, it is created. With this sequence, the original ''lib'' is now accessed as ''priv::lib''.