Eclipse C: C Identifier Naming Convention



PURPOSE

The naming convention in Eclipse addresses the problem that names in Lisp have certain features that are not available in C, including:

SUMMARY

Symbol names in Lisp are translated in a predictable and readable manner to identifiers in C, using the following techniques:

Package prefixing:

C identifiers which name constructs with limited scope usually use some form of the Lisp symbol name alone. When necessary, the Lisp package name is used as a prefix.

C identifiers which name constructs who's scope includes the entire C file (or files) always use the Lisp package name as a prefix.

Case conversion:

The names of Lisp variable bindings appear in lower case. Dashes are converted to underscores.

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.

Renaming:

If two Lisp symbols correspond to distinct constructs with distinct scope in Lisp, but are translated to C identifiers with overlapping or shadowing scope, the inner one is renamed using a suffix with a distinguished case.

Character Translation:

Non-aphanumeric characters in symbol names are translated into a short identifying string. The character name appears in a distinguished case from the other characters in the identifier.

Special provision is made to provide hexidecimal "names" for characters greater than 255.

Symbols in the C package:

None of this is true for Lisp symbols in a package named "C". The names of symbols in this this package translate "directly" into C identifiers.

Identifier length:

No attempt is made to shorten identifiers, and almost all will be longer than 8 characters -- many will be longer than 32. Eclipse will simply not work with compilers which impose draconian limits on the length of identifiers.

BASIC EXAMPLES

The Lisp symbol PI names a global constant. The symbol has a name, "PI", and a package named "COMMON-LISP", or "CL" for short.

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

PACKAGE QUALIFICATION

An abbreviation for the package name appears at the beginning of package qualified identifiers.

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 package qualifiers are normally translated to lower case when appearing in C identifiers, thus holding to a minimum the number of character prefixes "reserved" by Eclipse.

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 variable xy-foo would then be indistinguishable from the local variable xy:foo -- both would appear as xy_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.

All 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.

Other identifiers with scope limited to a single function or compound statement block are only package qualified if:

When locally scoped identifiers do need a package qualifier, package qualifier prefix is separated from the symbol name with an underscore. This distinguishes such identifiers from global identifiers of the same name corresponding to the same type of construct (ex. functions and local variables corresponding to local function bindings).

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:


CASE CONVERSION

The default behavior for Lisp symbols, and all Lisp symbols defined by Lisp itself, uses all upper case letters. Case conversion describes how upper case alphabetic characters in Lisp symbol names are mapped into C identifiers. Characters in Lisp symbol names which are not upper case are said to be "escaped", and they are normally printed in Lisp using escape characters to indicate their non-default case. The same is done in translating to C. Thus:
	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  usrFooBBACKSLASHar 
Note that the escape characters (| or \) print in a case distinguishable from unescaped normal characters.

[A bug is that the function FOO-|bar| is indistinguishable from the function FOO|bar|.]

When 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.

CHARACTER SUBSTITUTION

It is possible for Lisp names to contain non-alpha numeric characters and even spaces. Each such character is translated into a name which appears in a distinguishing case so as not to be confused with a name which actually contains the characters of the name. For example:

symbol	local	 	local   	global 		global
name	variable 	label   	symbol 		function
------ 	-------- 	------- 	---------- 	----------
FOO!	fooBANG  	FOObang 	usrFOObang 	usrFooBANG
FOOBANG foobang  	FOOBANG 	usrFOOBANG	usrFoobang 
FOObang fooPIPEbangPIPE FOOpipebangpipe	usrFOOpipebangpipe 
						     usrFooPIPEbangPIPE

The 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.


FUNCTION NAMES

There are several aspects of Eclipse that require basic function names to not have any underscores: On the other hand, we still need to distinguish between the function 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 that FOO-1BAR and FOO1BAR both translate as usrFoo1bar. Also, -FOO and FOO both translate as usrFoo. Note, though, that 1- translates properly as cl1MINUS_FUNC.]

RENAMING

Within a Lisp function, there may be several constructs such as tagbodies which introduce local labels which have the same name, but different (perhaps nested) scope. In C, the corresponding labels must be uniquely named within each function. The compiler keeps track of these names for each function. When the same name is used within a function, the compiler renames one of them, appending __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.

CONSTANTS

Constant or literal values which are C primitives such as 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_clFOO
N.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:

  1. It is possible to have distinct uninterned symbols with the same name in Lisp, so we must use the distinguishing integer convention for uninterned symbols. As there is no package from which to obtain a package prefix, "SYM" is used as the prefix.

  2. Tokens named by symbols in the C package are uniformly exempt from case conversion, etc. However, this might cause clashes in C between functions named by symbols in the C package and literal symbols in the C package. Thus literal symbols in the C package also use the distinguishing integer convention, and a prefix of "CC".

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.)


OTHER INTERNALLY GENERATED IDENTIFIERS

The C compiler generates some internal identifiers: In both cases, the names used are interned in a system package with a package prefix of "T".

EXPLICIT C IDENTIFIERS

Most of the naming convention is designed to avoid conflicts between C identifiers representing different Lisp constructs.

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:

A notable exception is that symbols appearing as literal data in a compiled file use a similar convention as for uninterned symbols, though the symbol-name itself does appear unaltered. See Constants, above.

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:

Just as it is up to C programmers to avoid conflicts with variables, functions, types, and tags identified with the same token, it is similarly up to the Lisp programmer to avoid such conflicts with symbols in the C package.

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.


OTHER C IDENTIFIERS IN HEADERS

There are some other uses of identifiers which do not appear directly in generated code, but do appear in header (i.e. #include) files :

OUTSTANDING ISSUES

C:FUNCTIONS VERSUS ORDINARY LISP FUNCTIONS:

Maybe we should have a slightly different naming convention for functions corresponding to Lisp c:functions (maybe cl_FunctionName?):

NON FUNCTION-LIKE MACROS:

Some of the C community is used to seeing macros with arguments which do not have function-like semantics in upper case. We currently do not do this.
[Previous] [Next] [Up] [Documentation Contents] [Eclipse Home Page]