C identifiers which name constructs who's scope includes the entire C file (or files) always use the Lisp package name as a prefix.
The names of Lisp symbols and labels appear in upper case. Dashes are converted to underscores.
The names of Lisp functions appear with only the first character in each word capitalized. Single dashes separating words are removed.
Escaped (i.e. non-standard cased) characters in Lisp names are escaped in the C identifier as well.
When there are not enough unescaped letters in a word to make the case apparent, a suffix in a distinguished case is used which identifies the token as a symbol, function, etc.
Special provision is made to provide hexidecimal "names" for characters greater than 255.
In C, the identifier
clPI refers to this symbol. The cl part is
the package qualifier, while the symbol's name, "PI", appears in all
upper case letters as PI.
The following table gives some examples of the package qualification
and case conversion, assuming the current package is
USER, and that USER
does not inherit any symbols from package XY.
Lisp construct Lisp symbol C identifier -------------- ---------- ------------ Examples of locally scoped constructs which only use package qualification when necessary. If present, the package qualifier is separated from the symbol name by an underscore. lower case local variable FOO-BAR foo_bar F-B f_b XY:FOO-BAR XY_foo_bar XY:F-B XY_f_b upper case local label FOO-BAR FOO_BAR (go or block label) F-B F_B XY:FOO-BAR xy_FOO_BAR XY:F-B xy_F_B first letter capitalized local variable FOO-BAR FooBar representing a F-B FBfunc local function XY:FOO-BAR xy_FooBar binding XY:F-B xy_FBfunc Constructs who's C scope includes the entire file. A package qualifier is always used, and no underscore separates it from the symbol name. first letter capitalized function FOO-BAR usrFooBar F-B usrFBfunc XY:FOO-BAR xyFooBar XY:F-B xyFBfunc lexical function FOO-BAR usrFooBar_Baz BAZ defined inside F-B usrFBfunc_Baz the given function XY:FOO-BAR xyFooBar_Baz XY:F-B xyFBfunc_Baz lexical function FOO-BAR usrFooBar_xyBaz XY:BAZ defined inside F-B usrFBfunc_xyBaz the given function XY:FOO-BAR xyFooBar_xyBaz XY:F-B xyFBfunc_xyBaz upper case a symbol FOO-BAR usrFOO_BAR F-B usrF_B XY:FOO-BAR xyFOO_BAR XY:F-B xyF_B
The package qualifier used can be set by the user, and defaults to the shortest name or nickname for the package.
The package qualifiers for various system defined packages are:
The Lisp function (package-prefix package)
returns the current package-prefix used by compile-file. The Lisp function
(setf (package-prefix package)
string) assigns string as the new package prefix
for package.
It would be nice if all package qualification consistently lower cased the package qualifier prefix name. However, the local variableAll C identifiers who's scope includes the entire C file are package qualified. The package qualifier immediately precedes the symbol name, without an intervening underscore. The start of the symbol name is distinguishable because the package prefix is always lower cased, and global identifiers are always either upper case or capitalized.xy-foowould then be indistinguishable from the local variablexy:foo-- both would appear asxy_foo.Thus the only time upper case qualifiers are used is in local variable names in which the programmer explicitly used a name for the local variable which is represented by a symbol not accessible in the package the programmer is working in. This is very unusual. Even then, the local variable will shadow any conflicting external identifier (i.e. with the same name, including the upper case prefix), which is probably what is desired. If not, only the local variable name need be changed.
Note, though, that it is customary to define macros which use uninterned symbols to name local variables. These will be package qualified using upper case
SYM.
Other identifiers with scope limited to a single function or compound statement block are only package qualified if:
int, char,
switch, etc.
Non-alphanumeric characters in package names are mapped into character name strings. However, it is up to the user to make sure that the package qualifier does not:
normal symbol Lisp name ref. as local var. as global symbol as function ------ ----- --------------- ------------------- ------------------ FOO-BAR foo-bar foo_bar usrFOO_BAR usrFooBar FOO-bar foo-|bar| foo_PIPEbarPIPE usrFOO_pipebarpipe usrFooPIPEbarPIPE FOO-BaR foo-b\ar foo_bBACKSLASHar usrFOO_BbackslashaR usrFooBBACKSLASHarNote that the escape characters (| or \) print in a case distinguishable from unescaped normal characters.
[A bug is that the functionWhen performing case conversion, it sometimes happens that there aren't enough unescaped alphabetic characters to form a distinguished identifier. For example, with the rules given so far, a function and a global symbol named X would both translate as usrX. When this occurs, a distinguishing suffix of either _SYMBOL or _FUNC is used, which itself appears in a distinguishing case. This is most common for numeric functions and other symbols who's names contain no alphabetic characters at all.FOO-|bar|is indistinguishable from the functionFOO|bar|.]
symbol local local global global name variable label symbol function ------ -------- ------- ---------- ---------- FOO! fooBANG FOObang usrFOObang usrFooBANG FOOBANG foobang FOOBANG usrFOOBANG usrFoobang FOObang fooPIPEbangPIPE FOOpipebangpipe usrFOOpipebangpipe usrFooPIPEbangPIPEThe mapping of characters to names varies with usage. For example, for most purposes, the mapping is:
! " # $ % & '
BANG QUOTATION HASH DOLLAR PERCENT AMPERSAND QUOTE
( ) * + , - <dash> . /
LPAREN RPAREN STAR PLUS COMMA _ <under> DOT SLASH
: ; < = > ? space @
COLON SEMICOLON LESS EQUAL GREATER QUESTION SPACE AT
[ \ ] ^ _ <under> ` {
LBRACKET BACKSLASH RBRACKET CARET __<twice> BACKQUOTE LBRACE
| } ~ newline return backspace page tab
PIPE RBRACE TILDE NEWLINE RETURN BACK PAGE TAB
However for functions, the mapping is modified so that / => DIV and - =>
MINUS. In addition, underscores remaining after the
underscore-stripping/capitalization process are represented as UNDER.
The "name" of a character who's code is greater than 255 is
"_X****_", where **** is as many hexidecimal
digits as necessary to represent the code.
usrFoo_Bar is a lexical function BAR
defined within the function FOO.
usrFooBar is the function FOO-BAR.
FOO-BAR and FOO--BAR. The methodology used is that any
non-alphanumeric character serves as a word separator. The first
character after such a separation will be capitalized if and only if
it is alphabetic. If the separating character happens to be a hyphen
(the usual case) and there is at least one character after the hyphen,
the separating hyphen is removed. Any remaining hyphens appear as
MINUS. Any underscores appear as "UNDER".
[A bug is thatFOO-1BARandFOO1BARboth translate asusrFoo1bar. Also,-FOOandFOOboth translate asusrFoo. Note, though, that1-translates properly ascl1MINUS_FUNC.]
__r1 to the C
identifier. Note the distinguishing case.
Here's a silly example:
(tagbody foo (go foo)
(tagbody foo (go foo)))
=> { { FOO: ; goto FOO; { FOO__r1: ; goto FOO__r1; } } }
Lisp variable bindings may refer to outer bindings with the same name
in their initialization forms. Again, renaming is used, using a
distinguishing case.
(let ((a 1))
(let ((a a))
(print a))
(print a))
=> { clObject a;
clSetq(a, I_1);
{ clObject a__R1; clSetq(a__R1, a); clPrint(a__R1, clEOA); }
clPrint(a, clEOA); }
Renaming of nested scope variables also avoids the problem of lint
warnings about inner definitions hiding outer ones.
A Lisp function (or file) may also introduce different lexical functions with the same name but different scope. Ex.
(progn (flet ((foo ...))
...)
(flet ((foo ...))
...))
Again, renaming with a distinguishing case is used.
int, float, and char * may
appear directly where they are used in C code. The C compiler deals
with them in an efficient manner. However, Lisp objects are not C
primitives. Each generated C file has a number of "variables" which
correspond to each literal used in the original Lisp file. These
"variables" are initialized once when the system starts up. The
constants are coalesced. For example, if several functions in a file
used the literal value 2.0d0, one variable is created to
hold the Lisp double-float. The variable is initialized
once at system start up time. Each function in the file which uses
the value gets the value from this variable.
The naming convention for these variables breaks them down into two classes: those with unique names and those without.
Characters, integers, single and double floats, ratios, complex numbers, packages and
interned symbols all have unique names that include the "value" of the
constant. We have already seen the naming convention for interned
symbols. The others use the prefix strings: C_,
I_, F_ or D_, R_,
X_ and PKG_, respectively. The value of the constant then
appears, with suitable character mapping of non-alphanumeric
characters. Examples are:
C_a, C_bang, C_space (for #\a #\! and #\Space) I_1, I_minus1, I_123456789102345 (for 1, -1, and 123456789102345) F_1_2, F_1_3eplus10, D_1_3d0, D_1_5dminus10 (for 1.2 1.3e10 1.3d0 and 1.5d-10) R_2div3 (for 2/3) X_1plus3I, X_1_5plus3_5I, X_1_5d0plus3_5d0I (for #c(1 3) #c(1.5f0 3.5f0)...) PKG_COMMON_LISP, PKG_COMMON_LISP_USER (for #<Package COMMON-LISP> ...)The remaining constants have names which include a prefix and a small distinguishing integer:
Uninterned symbols: SYM_0_FOO, SYM_1_FOO Symbols interned in the C package: CC_0_foo Cons cells: CONS_0, CONS_1 Strings: STR_0, STR_1 Bit vectors: BV_0, BV_1 Arrays: A_0, A_1 Hash tables: H_0, H_1 Pathnames: P_0, P_1 Random state: RAND_0, RAND_1 Structures: STRUCT_0_usrFOO, STRUCT_1_usrFOO Standard-object instances: INST_0_clFOO, INST_1_clFOON.B.: The package qualified type/class of structures appear in the name. However, the identifying integer does not necessarily indicate the number of objects of that type/class which have been made.
N.B.: When symbols appear as literals, a "constant" variable is created that has a name that more or less matches both the name as it appears in Lisp, and the C naming convention that ENUMs and other names "flags" appear in upper case. Package qualification is used. There are two exceptions:
The prefixes used for constants are upper case while package qualifiers are
generally lower case. The exception occurs when a programmer uses a
symbol not accessible in the current package to name a local variable.
Thus users must still avoid using the constant prefixes for package
prefixes. The most likely clash would appear to be from a package
named, X, but fortunately, it is not possible to make a local variable
which translates, for example, as X_1plus3I. (The substring "plus"
would be either uppercase or escaped.)
However, Lisp code compiled to C by Eclipse may refer directly to C functions and variables defined in other object files and libraries. We must, therefore, arrange to translate these names in such a way that they will NOT be unique from the identifiers for the desired C constructs.
The C package in Eclipse does not inherit symbols from any other package. Any Lisp construct identified by a symbol in the C package -- local variable, function, "go" label, type, etc -- is translated to C with minimal alteration:
The case inversion is intended as a convenience, since the default reader behavior is to intern symbols in all uppercase, and most C identifiers are all lower case.
The compiler does not map any non-alphanumeric characters into their names for symbols in the C package. This allows these symbols to appear as arbitrarily complex C syntax. It is up to the user to choose symbols which translate as legal C syntax.
The translation of symbols in the C package is essentially an escape mechanism in which users can access desired C tokens such as:
(type c:int x) => int x;
c:errno => errno
(c:isalpha x) => isalpha(x)
and (c::|XCloseDisplay| x) =>
XCloseDisplay(x)
Just as it is up to C programmers to not name variables, etc. with identifiers that begin with a number, it is similarly up to the Lisp programmer to not compile constructs named with C symbols who's name begins with a number.
#include) files :
CL_. The names are chosen not to be symbols in
the Lisp package, so no user code will be able to accidentally
define a local variable which would clash.
#defines) use all uppercase letters and
are package qualified with cl_
cl_FunctionName?):
USER:FOO and a binding for local function named with the same
name when FOO is not accessible in the current package. Both would
appear in C as usr_Foo.
cl_. This is the convention for global constant
parameters (see Other C Identifiers, above). However to avoid a
clash with local variables, we would need to avoid the use of Lisp
symbol names, and these are often the clearest choice for some
constructs.
cl. This is the convention for lisp symbol literals.
Again, we would have to avoid the use of Lisp symbol names.