Skip to content

Commit b227eee

Browse files
committed
Document symbol table and namespace
1 parent dbd6988 commit b227eee

File tree

1 file changed

+97
-13
lines changed

1 file changed

+97
-13
lines changed

src/util/README.md

Lines changed: 97 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,103 @@ of a while loop is a statement.
101101

102102
\subsection symbolt_section symbolt, symbol_tablet, and namespacet
103103

104-
To be documented.
105-
106-
\subsubsection symbol_lifetimes_section Symbol lifetimes, symbol modes, name, base-name, pretty-name; semantic of lifetimes for symex?
107-
108-
To be documented.
109-
110-
\subsubsection storing_symbols_section Storing symbols and hiding symbols (namespacet)
111-
112-
To be documented.
113-
114-
\subsubsection ns_follow_section ns.follow
115-
116-
To be documented.
104+
A symbol table is a mapping from symbol names to \ref symbolt objects, which
105+
store a symbol's name, attributes, type and perhaps value. They are used to
106+
describe local and global variables, type definitions and function prototypes
107+
and definitions.
108+
109+
All symbols store a type (an instance of \ref typet). For function or method
110+
symbols these are \ref code_typet instances.
111+
112+
Global variable symbols may have a value (an \ref exprt), in which case it is
113+
used to initialise the global.
114+
115+
Method or function symbols may also have a value, in which case it is a
116+
\ref codet and gives the function definition. A method or function symbol
117+
without a value is a prototype (for example, it might be an `extern` declaration
118+
in C). A function symbol that has been converted to a GOTO function *may* be
119+
replaced with a special "compiled" value, but this varies from driver program to
120+
program -- at the time of writing, only \ref goto-cc does this.
121+
122+
Local variables' symbol values are always ignored;
123+
any initialiser must be explicitly assigned after they are instantiated by a
124+
declaration (\ref code_declt).
125+
126+
Symbol expressions (\ref symbol_exprt) and types (\ref symbol_typet) refer to
127+
symbols stored in a symbol table. Symbol expressions can be thought of as
128+
referring to the table for more detail about a symbol (for example, is it a
129+
local or a global variable, or perhaps a function?), and have a type which must
130+
match the type given in the symbol table. Symbol types can be thought of as
131+
shorthands or aliases for a type given in more detail in the symbol table, for
132+
example permitting a shorthand for a large structure type, as well as permitting
133+
the construction of expressions referring to that type before its full
134+
definition is known.
135+
136+
Note the implementation of \ref symbol_tablet is split into a base interface
137+
(\ref symbol_table_baset) and an implementation (\ref symbol_tablet). There is
138+
one alternate implementation (\ref journalling_symbol_tablet) which additionally
139+
maintains a log or journal of symbol creation, modification and deletions.
140+
141+
Namespaces (\ref namespacet) provide a read-only view on one or more symbol
142+
tables, and provide helper functions that aid accessing the table. A namespace
143+
may layer one or more symbol tables, in which case any lookup operation checks
144+
the 'top' symbol table before moving down the layers towards the 'bottom' symbol
145+
table, looking up the target symbol name in each successive table until one is
146+
found. Note class \ref multi_namespacet can layer arbitrary numbers of symbol
147+
tables, while for historical reasons \ref namespacet can layer up to two.
148+
149+
The namespace wrapper class also provides the \ref namespacet::follow
150+
operation, which dereferences a `symbol_typet` to retrieve the type it refers
151+
to, including following a symbol_typet which refers to another symbol which
152+
eventually refers to a 'real' type.
153+
154+
\subsubsection symbolt_lifetime Symbol lifetimes
155+
156+
Symbols with \ref symbolt::is_static_lifetime set are initialised before a
157+
program's entry-point is called and live until it ends. Such long-lived
158+
variables are used to implement globals, but also C's procedure-local static
159+
variables, which have restricted visiblity but the lifetime of a global.
160+
They may be marked dead using a \ref code_deadt instruction, but this does not
161+
render the variable inaccessible, it only indicates that the variable's current
162+
value will not be read without an intervening write.
163+
164+
Non-type, non-global symbols (those with \ref symbolt::is_static_lifetime and
165+
\ref symbolt::is_type not set) are local variables, and their lifespan
166+
description varies depending on context.
167+
168+
In symbol table function definitions (the values of function-typed symbols),
169+
local variables are created using a \ref code_declt instruction, and live until
170+
their enclosing \ref code_blockt ends (similar to the C idiom
171+
`{ int x; ... } // x lifespan ends`). By contrast in GOTO programs locals are
172+
declared using a DECL instruction and live until a DEAD instruction referring
173+
to the same symbol. Explicit DEAD instructions are always used rather than
174+
killing implicitly by exiting a function.
175+
176+
Multiple instances of the same local may be live at the same time by virtue of
177+
recursive function calls; a dead instruction or scope end always targets the
178+
most recently allocated instance.
179+
180+
Scoping rules are particular to source languages and are not enforced by
181+
CPROVER. For example, in standard C it is not possible to refer to a local
182+
variable across functions without using a pointer, but in some possible source
183+
languages this is permitted.
184+
185+
\subsubsection symbolt_details Symbol details
186+
187+
Symbols have:
188+
* A mode, which indicates the source language frontend responsible for creating
189+
them. This is mainly used in pretty-printing the symbol table, to indicate
190+
the appropriate language frontend to use rendering the symbol's value and/or
191+
type. For example, mode == ID_C == "C" indicates that \ref ansi_ct, the C
192+
front-end, should be used to pretty-print, which in turn delegates to
193+
\ref expr2ct.
194+
* A base-name and pretty-name, which are a short and user-friendly version of
195+
the symbol's definitive name respectively.
196+
* Several flags (see \ref symbolt for full details), including
197+
\ref symbolt::is_static_lifetime (is this a global variable symbol?),
198+
\ref symbolt::is_type (is this a type definition),
199+
\ref symbolt::is_thread_local (of a variable, are there implicitly instances
200+
of this variable per-thread?).
117201

118202
\subsection cmdlinet
119203

0 commit comments

Comments
 (0)