Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| language:namespaces [2019/04/18 18:27] – created rajit | language:namespaces [2024/07/27 16:52] (current) – [Renaming namespaces] rajit | ||
|---|---|---|---|
| Line 10: | Line 10: | ||
| ===== Creating a namespace ===== | ===== Creating a namespace ===== | ||
| + | A namespace is created by using the '' | ||
| + | <code act> | ||
| + | namespace lib { | ||
| + | ... | ||
| + | export defproc buffer (a1of2? l; a1of2! r) { ... } | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | The process '' | ||
| + | '' | ||
| + | '':: | ||
| + | C++. | ||
| + | |||
| + | An ACT type or instance is first evaluated in the current | ||
| + | namespace; if it doesn' | ||
| + | global namespace is searched next. | ||
| + | |||
| + | A namespace typically contains user-defined types. By default, these | ||
| + | types are only visible within the current namespace. To allow a type | ||
| + | to be visible in any other namespace, it must be prefaced by the | ||
| + | '' | ||
| + | |||
| + | A namespace can also contain another namespace. However, these | ||
| + | namespaces do not have special privileges. An example of nested | ||
| + | namespaces is shown below. | ||
| + | |||
| + | <code act> | ||
| + | namespace datapath { | ||
| + | export defproc bus_interface(...) { ... } | ||
| + | namespace adder { | ||
| + | export defproc alu(...) { | ||
| + | bus_interface b; | ||
| + | } | ||
| + | } | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Nesting does not give a namespace special permissions; | ||
| + | '' | ||
| + | namespace '' | ||
| + | '' | ||
| + | '' | ||
| + | qualified name, due to the fact that the definition is in scope due to | ||
| + | nesting. | ||
| + | |||
| + | '' | ||
| + | '' | ||
| + | namespace '' | ||
| + | exports the definition one level up. To export this another level out, | ||
| + | the entire namespace '' | ||
| + | |||
| + | <code act> | ||
| + | namespace datapath { | ||
| + | export defproc bus_interface(...) { ... } | ||
| + | export namespace adder { | ||
| + | export defproc alu(...) { | ||
| + | bus_interface b; | ||
| + | } | ||
| + | } | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Now '' | ||
| + | namespace. | ||
| + | |||
| + | //Namespace globals// are instances that are defined outside any type | ||
| + | definition. While the '' | ||
| + | or other constructs used to construct circuits, other namespaces can | ||
| + | only have global data types or channels---i.e. no circuits. | ||
| + | |||
| + | ===== Importing namespaces ===== | ||
| + | |||
| + | There are two mechanisms to import other files in ACT. These | ||
| + | imports have to be at the beginning of an ACT file. If the same | ||
| + | file is imported twice within the same scope, ACT automatically | ||
| + | skips the second import. | ||
| + | |||
| + | The basic form of import statement is shown below: | ||
| + | |||
| + | <code act> | ||
| + | import " | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | This import searches for a file called '' | ||
| + | part of the ACT design. ACT has multiple mechanisms that can be used | ||
| + | to specify search paths for the files that an import statement can access: | ||
| + | |||
| + | * the current directory: if the file is located in the current working directory, then it is used. | ||
| + | * the colon-separated path specified by '' | ||
| + | * '' | ||
| + | |||
| + | |||
| + | A second version of import uses namespaces directly, but requires that | ||
| + | ACT files be placed in locations that match the namespace | ||
| + | hierarchy. The import statement | ||
| + | |||
| + | <code act> | ||
| + | import processor:: | ||
| + | </ | ||
| + | |||
| + | is equivalent to the following: | ||
| + | |||
| + | <code act> | ||
| + | import " | ||
| + | </ | ||
| + | |||
| + | It assumes that the file '' | ||
| + | '' | ||
| + | '' | ||
| + | |||
| + | <code act> | ||
| + | import foo; | ||
| + | </ | ||
| + | |||
| + | would do the following: | ||
| + | * Look for '' | ||
| + | * If unsuccessful, | ||
| + | * If unsuccessful, | ||
| + | |||
| + | After the import, the specified namespace is checked to see if it exists. If not, an error will be reported. | ||
| + | |||
| + | |||
| + | ===== Opening namespaces ===== | ||
| + | |||
| + | If a project has many different people working on it, it can be convenient | ||
| + | to have each component/ | ||
| + | own namespace to avoid naming conflicts. This situation can result in | ||
| + | very long type names. Plus it would be more bookkeeping to have to | ||
| + | create a test environment for the types within, say, | ||
| + | '' | ||
| + | because not all types might be exported! | ||
| + | |||
| + | As a syntactic convenience, | ||
| + | |||
| + | <code act> | ||
| + | import " | ||
| + | open processor:: | ||
| + | |||
| + | a1of2 d; | ||
| + | </ | ||
| + | |||
| + | This version of the '' | ||
| + | functions: (i) it adds '' | ||
| + | types, and (ii) it allows one to access all types within the | ||
| + | namespace, not just the ones that are exported (including types within | ||
| + | nested namespaces). Note that this '' | ||
| + | all types cannot be uniquely resolved. Note that the sequence of '' | ||
| + | at the beginning of an ACT file. | ||
| + | |||
| + | ===== Renaming namespaces ===== | ||
| + | |||
| + | |||
| + | 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 '' | ||
| + | |||
| + | To resolve this issue, ACT provides a way to rename a namespace that has been imported. | ||
| + | |||
| + | <code act> | ||
| + | import " | ||
| + | open lib -> lib1; | ||
| + | import " | ||
| + | open lib -> lib2; | ||
| + | </ | ||
| + | |||
| + | In this example, the '' | ||
| + | has occured, there cannot be any naming conflicts. This version of '' | ||
| + | |||
| + | Another renaming scenario that can be useful is to move a namespace into another one. | ||
| + | <code act> | ||
| + | import lib; | ||
| + | import lib => priv; | ||
| + | </ | ||
| + | The first import statement above loads in the '' | ||