C/C++ Producer Implementation

  1. i. Introduction
  2. 1. Program overview
  3. 2. Structural Organisation
    1. 2.1. Source code modules
    2. 2.2. Type system
  4. 3. Scalar types
    1. 3.1. Arithmetic types
    2. 3.2. Integer literal types
    3. 3.3. Bitfield types
  5. 4. Pointers
    1. 4.1. Generic pointers
    2. 4.2. Pointers to data members
    3. 4.3. Pointers to function members
  6. 5. Calling conventions
    1. 5.1. Member functions
    2. 5.2. Ellipsis functions
  7. 6. Classes
    1. 6.1. Class layout
    2. 6.2. Derived class layout
    3. 6.3. Constructors and destructors
    4. 6.4. Virtual function tables
  8. 7. Exceptions
    1. 7.1. Try blocks
    2. 7.2. Local variables
    3. 7.3. Throwing an exception
    4. 7.4. Handling an exception
    5. 7.5. Exception specifications
  9. 8. Run-time type information
    1. 8.1. Defining run-time type information structures
    2. 8.2. Accessing run-time type information
  10. 9. Mangled identifier names
    1. 9.1. Mangling identifier names
    2. 9.2. Mangling namespace names
    3. 9.3. Mangling types
    4. 9.4. Other mangled names
    5. 9.5. Mangled name examples
  11. 10. Other implementation details
    1. 10.1. Parsing C++
    2. 10.2. Undefined conversions
    3. 10.3. Integer division
    4. 10.4. Dynamic initialisation
    5. 10.5. The std namespace
    6. 10.6. Error catalogue

First published .

Revision History

kate

Restructuring, in order to get rid of the style guide.

kate

Split up implementation details (which form the bulk of this document) into separate chapters.

kate

Moved out the DRA producers as a standalone tool.

kate

Moved out the C/C++ Producer Implementation into a seperate document.

DERA

tcpplus 1.8.2; TenDRA 4.1.2 release.

i. Introduction

This document describes various of the implementation details of the C++ producer TDF output. In particular it describes the standard TDF tokens used to represent the target dependent aspects of the language and to provide links into the run-time system. Many of these tokens are common to the C and C++ producers. Those which are unique to the C++ producer have names of the form ~cpp.*. Note that the description is in terms of TDF tokens, not the internal tokens introduced by the The Pragma Token Syntax.

There are two levels of implementation in the run-time system. The actual interface between the producer and the run-time system is given by the standard tokens. The provided implementation defines these tokens in a way appropriate to itself. An alternative implementation would have to define the tokens differently. It is intended that the standard tokens are sufficiently generic to allow a variety of implementations to hook into the producer output in the manner they require.

1. Program overview

The C++ producer is a large program (over 200000 lines, including automatically generated code) written in C. A description of the program structure and the basic organisation of the source code are described in the first section.

One of the design methods used in the C++ producer is the extensive use of automatic code generation tools. The type system is based around the calculus tool, which allows complex type systems to be described in a simple format. The interface generated by calculus allows for rigorous static type checking, generic type constructors for lists, stacks etc., encapsulation of the operations on the types within the system, and optional run-time checking for null pointers and discriminated union tags. An overview is given of the type system used as the basis of the C++ producer design. Also see the Calculus Users' Guide.

The other general purpose code generation tool used in the C++ producer is the parser generator, sid. A brief description of the problems in writing a C++ parser is given. Also see the SID Users' Guide.

The other code generation tools used were written specifically for the C++ producer. The error reporting routines within the producer are based on an error catalogue, from which code for constructing and printing errors is generated. The TDF output routines are based on primitives automatically generated from a standard database describing the TDF specification.

The program itself is well commented, so no lower level program documentation has been provided. When performing development work the producer should be compiled without the NDEBUG macro defined. This enables the calculus run-time checks, along with other assertions, and makes available the debugging routines, DEBUG_type, which can be used to print an object from the internal type system.

2. Structural Organisation

  1. 2.1. Source code modules
  2. 2.2. Type system
    1. 2.2.1. Primitive types
    2. 2.2.2. CV_SPEC
    3. 2.2.3. BUILTIN_TYPE
    4. 2.2.4. BASE_TYPE
    5. 2.2.5. INT_TYPE
    6. 2.2.6. FLOAT_TYPE
    7. 2.2.7. CLASS_INFO
    8. 2.2.8. CLASS_USAGE
    9. 2.2.9. CLASS_TYPE
    10. 2.2.10. GRAPH
    11. 2.2.11. VIRTUAL
    12. 2.2.12. ENUM_TYPE
    13. 2.2.13. TYPE
    14. 2.2.14. DECL_SPEC
    15. 2.2.15. HASHID
    16. 2.2.16. QUALIFIER
    17. 2.2.17. IDENTIFIER
    18. 2.2.18. MEMBER
    19. 2.2.19. NAMESPACE
    20. 2.2.20. NAT
    21. 2.2.21. FLOAT
    22. 2.2.22. STRING
    23. 2.2.23. NTEST
    24. 2.2.24. RMODE
    25. 2.2.25. EXP
    26. 2.2.26. OFFSET
    27. 2.2.27. TOKEN
    28. 2.2.28. INSTANCE
    29. 2.2.29. ERROR
    30. 2.2.30. VARIABLE
    31. 2.2.31. LOCATION
    32. 2.2.32. POSITION
    33. 2.2.33. BITSTREAM
    34. 2.2.34. BUFFER
    35. 2.2.35. OPTIONS
    36. 2.2.36. PPTOKEN

This section describes the basic organisation of the source code for the C++ producer. This includes the division of the code into separate modules and the type system conventions.

2.1. Source code modules

For convenience, the source code is divided between a number of directories:

base/

The base directory contains only the module containing the main function, the basic type descriptions and the Makefile.

obj_c/
obj_tok/
obj_templ/

The directories obj_c and obj_tok contain respectively the C and #pragma token headers generated from the type algebra by calculus. The directory obj_templ contains certain calculus template files.

utility/

Routines for such utility operations as memory allocation and error reporting, including the error catalogue.

parse/

Routines concerned with parsing and preprocessing the input, including the sid grammar.

construct/

Routines for building up and analysing the internal representation of the parsed code.

output/

Routines for outputting the internal representation in various formats including as a TDF capsule (see TDF Specification), a C++ spec file (see C/C++ Producer Implementation), or a tdfc2dump symbol table dump file.

Each module consists of a C source file, file.c say, containing function definitions, and a corresponding header file file.h containing the declarations of these functions. The header is included within its corresponding source file to check these declarations; it is protected against multiple inclusions by a macro of the form file_INCLUDED. The header contains a brief comment describing the purpose of the module; each function in the source file contains a comment describing its purpose, its inputs and its output.

The following table lists all the source modules in the C++ producer with a brief description of the purpose of each:

ModuleDirectoryPurpose
accessconstructmember access control
allocateconstructnew and delete expressions
assignconstructassignment expressions
basetypeconstructbasic type operations
bufferutilitybuffer reading and writing routines
c_classobj_ccalculus support routines
capsuleoutputtop-level TDF encoding routines
castconstructcast expressions
catalogutilityerror catalogue definition
charparsecharacter sets
checkconstructexpression checking
chktypeconstructtype checking
classconstructclass and enumeration definitions
compileoutputTDF tag definition encoding routines
constantparseinteger constant evaluation
constructconstructconstructors and destructors
convertconstructstandard type conversions
copyconstructexpression copying
debugutilitydevelopment aids
declareconstructvariable and function declarations
decodeoutputbitstream reading routines
deriveconstructbase class graphs; inherited members
destroyconstructgarbage collection routines
diagoutputTDF diagnostic output routines
dumpoutputsymbol table dump routines
encodeoutputbitstream writing routines
errorutilityerror output routines
exceptionconstructexception handling
expoutputTDF expression encoding routines
expressionconstructexpression processing
fileparselow-level I/O routines
functionconstructfunction definitions and calls
hashparsehash table and identifier name routines
identifierconstructidentifier expressions
initoutputTDF initialiser expression encoding routines
initialiseconstructvariable initialisers
instanceconstructtemplate instances and specialisations
inttypeconstructinteger and floating point type routines
labelconstructlabels and jumps
lexparselexical analysis
literalparseinteger and string literals
loadoutputC++ spec reading routines
macroparsemacro expansion
mainmain routine; command-line arguments
mangleoutputidentifier name mangling
memberconstructmember selector expressions
mergeconstructintermodule merge routines
namespaceconstructnamespaces; name look-up
operatorconstructoverloaded operators
optionutilitycompiler options
overloadconstructoverload resolution
parseparselow-level parser routines
pragmaparse#pragma directives
predictparseparser look-ahead routines
preprocparsepreprocessing directives
printutilityerror argument printing routines
qualityconstructextra expression checks
redeclareconstructvariable and function redeclarations
rewriteconstructinline member function definitions
saveoutputC++ spec writing routines
shapeoutputTDF shape encoding routines
statementconstructstatement processing
stmtoutputTDF statement encoding routines
structoutputTDF structure encoding routines
syntax[0-9]*parsesid parser output
systemutilitysystem dependent routines
tableparseportability table reading
templateconstructtemplate declarations and checks
throwoutputTDF exception handling encoding routines
tokoutputTDF standard tokens encoding
tokdefconstructtoken definitions
tokenconstructtoken declarations and expansion
typeidconstructrun-time type information
unmangleoutputidentifier name unmangling
variableconstructvariable analysis
virtualconstructvirtual functions
xallocutilitymemory allocation routines

2.2. Type system

This section describes the type system used in the C++ producer. Unless otherwise stated the types are declared using the calculus tool as part of the algebra, c_class.alg. The design of this type algebra was clearly largely based on the concepts underlying the C++ language; however TDF provided an important influence, not merely as the intended target language, but also because of its clear presentation of essential language features.

2.2.1. Primitive types

The primitive types used within the algebra c_class are defined as follows:

int = "int" ;
unsigned = "unsigned" ;
string = "character *" ;
ulong_type (ulong) = "unsigned long" ;
BITSTREAM_P (bits) = "BITSTREAM *" ;
PPTOKEN_P (pptok) = "PPTOKEN *" ;

The integral types are self-explanatory. All string literals used in the C++ producer are based on the character type:

typedef unsigned char character ;

hence the definition of string. The remaining primitive give links to those portions of the type system which are defined outside of the algebra. The types BITSTREAM and PPTOKEN are described below.

2.2.2. CV_SPEC

The enumeration type CV_SPEC (short name cv) is used to represent a C++ type qualifier. It takes the form of a bitfield, the elements of which can be or-ed together to represent combinations of type qualifiers. The cv-qualifiers are represented by cv_const and cv_volatile in the obvious manner. The value cv_lvalue is used as a qualifier to indicate whether a type is an lvalue or an rvalue. Other values are used in function types to represent the function language linkage.

2.2.3. BUILTIN_TYPE

The enumeration type BUILTIN_TYPE (ntype) is used to represent the built-in C++ types (char, float, void etc.). It is used chiefly as an index into tables of type information.

2.2.4. BASE_TYPE

The enumeration type BASE_TYPE (btype) is used to represent a C++ simple type specifier such as signed, short or int. It takes the form of a bitfield, the elements of which can be or-ed together to represent combinations of type specifiers. Its chief use is when reading a type from the input file; the various simple type specifiers are combined to give a value of this type, which is then mapped to an actual C++ type.

2.2.5. INT_TYPE

The union type INT_TYPE (itype) is used to represent an integral or bitfield C++ type. The basic integral types are given by the basic field. Bitfield types are represented by the bitfield field. There are also fields representing target dependent integral promotion, arithmetic and integer literal types, plus VARIETY tokens. Only one INT_TYPE object is created for each integral type.

2.2.6. FLOAT_TYPE

The union type FLOAT_TYPE (ftype) is used to represent a floating point C++ type. The basic floating point types are given by the basic field. There are also fields representing target dependent argument promotion and arithmetic types, plus FLOAT tokens. Only one FLOAT_TYPE object is created for each floating point type.

2.2.7. CLASS_INFO

The enumeration type CLASS_INFO (cinfo) is used to represent information relating to a class or enumeration definition. It takes the form of a bitfield, the elements of which can be or-ed together to represent various combinations of properties.

2.2.8. CLASS_USAGE

The enumeration type CLASS_USAGE (cusage) is used to represent information relating to the way a class is used. It takes the form of a bitfield, the elements of which can be or-ed together to represent various combinations of properties.

2.2.9. CLASS_TYPE

The union type CLASS_TYPE (ctype) is used to represent a C++ class or union. The main components are an identifier giving the class name, class information and class usage fields, a namespace giving the class members, a graph representing the base class structure, and a virtual function table. Only one CLASS_TYPE object is created for each class or union.

Each class maintains a list, pals, of class and function identifiers which are declared as friends of that class. It also maintains a list, chums, of those class types which declare it to be a friend (this is what is actually used in the access checks). Similarly each function identifier maintains a list, chums, of those class types which declare it to be a friend.

Each class maintains a list of its constructors, destructors and conversion functions (included inherited conversion functions). It also maintains a list of its virtual base classes. This information can be obtained by other means but it is more convenient to record it within the class type itself.

2.2.10. GRAPH

The union type GRAPH (graph) is used to represent a directed acyclic graph arising from the base classes of a class. Each node of the graph has a head which is a class type, and several tails which give the base class graphs for that class. Each node has pointers, top, to the top of the graph (i.e. the most derived class), and up, to the node of which the current node is a direct base. Each node also has an access field which gives information on the base access, whether it is virtual or not, and so on, in the form of a DECL_SPEC. Virtual bases are handled by the equal field which defines an equivalence relation on the graph which identifies equivalent virtual bases.

2.2.11. VIRTUAL

The union type VIRTUAL (virt) is used to represent the virtual functions declared in a class. The table field is used to represent a virtual function table, and consists primarily of a list of VIRTUAL objects giving the virtual functions for the associated class. These virtual functions are of four kinds, each represented by a union field. A virtual function first declared in a class is represented by the simple field; a virtual function in a class which overrides an inherited virtual function is represented by the override field; an inherited, non-overridden virtual function which is not overridden in a base class is represented by the inherit field; a inherited, non-overridden virtual function which is overridden in some base class is represented by the complex field.

2.2.12. ENUM_TYPE

The union type ENUM_TYPE (etype) is used to represent a C++ enumeration type. This consists primarily of an identifier giving the enumeration name, a class information field, a type giving the underlying representation of the enumeration type, and a list of identifiers giving the enumerators comprising the enumeration.

2.2.13. TYPE

The union type TYPE (type) is used to represent a C++ type. Every type has an associated type qualifier, qual, which determines whether the type is const, volatile or an lvalue. A type may also have an associated identifier, name, giving the corresponding type name (the null identifier being used for unnamed types). The other type components are determined by the union tag. Each of the type constructs above has a corresponding field in the TYPE union: integer for integral types, floating for floating point types, bitfield for bitfield types, compound for class or union types, and enumerate for enumeration types. There are also fields top and bottom corresponding to void and bottom (the type used to represent values which never return).

Other fields of the TYPE union represent composite types; for example, the array field, representing array types, comprises a base type, sub, and an integer constant giving the array bound, size. These are generally simple, apart from func, representing a function type. This has the obvious components: a return type, ret, a list of parameter types, ptypes, and a flag indicating ellipsis functions, ellipsis. It also has an associated namespace, pars, in which the function parameters are declared. The parameter identifiers are extracted from this as a list, pids. Member function qualifiers and language linkage information are represented by a CV_QUAL, mqual. The implicit extra parameter for member functions is recorded in the list mtypes, which adds this extra type to the start of ptypes. Finally except gives any exception specifiers; the case where the exception specifier is absent being represented by the special value, univ_type_set.

2.2.14. DECL_SPEC

The enumeration type DECL_SPEC (dspec) is used to represent information on the declaration and usage of an identifier. It takes the form of a bitfield, the elements of which can be or-ed together to represent various combinations of properties. The 32 bits in this bitfield (the maximum which can be represented portably) are a significant restriction. This means that the same member of DECL_SPEC is often used to mean different things in different contexts. This can prove confusing on occasions.

2.2.15. HASHID

The union type HASHID (hashid) is used to represent a C++ identifier name. The simplest form of identifier name, name, consists of just a string of characters, such as foo. Extended identifier names, ename, are similar, but may contain Unicode characters. There are however other forms of identifier name in C++: conversion function names (conv ) such as operator int, overloaded operator names (op) such as operator+, constructor names (constr), and destructor names (destr). There are also names which are used for anonymous identifiers (anon).

Note the distinction between an identifier name and an actual identifier. The latter is a meaning associated with a name in a particular context. Every identifier name has an associated underlying meaning, id. This is used to handle keywords and macros, but for most identifier names this will be a dummy identifier. Nested underlying meanings (such as a macro hiding a keyword) are handled by linking the alias fields of the corresponding identifiers. Every identifier name also has a cache field which is used to record the look-up of this name as an unqualified identifier. This may be set to the null identifier to indicate that the look-up needs to be re-evaluated.

Identifier names are stored in one of a small number of hash tables, linked using their next field. Each name has only one entry in these tables, allowing equality of names to be implemented as EQ_hashid.

2.2.16. QUALIFIER

The enumeration type QUALIFIER (qual) is used to represent the various ways in which an identifier name can be qualified. For example, ::A::a is represented by qual_full. The value qual_mark is used in the representation of function identifier expressions to indicate that overload resolution has been performed.

2.2.17. IDENTIFIER

The union type IDENTIFIER (id) is used to represent the various kinds of C++ identifiers. Every identifier has an associated identifier name, a parent namespace, a declaration information field, and a location for its declaration or definition. Each identifier also has an alias field which is normally used to represent the aliasing which can occur in inheritance or using declarations.

The various fields of the IDENTIFIER union correspond to the various kinds of identifier which can arise in C++ - class names, functions, variables, class members, macros, keywords etc. Each field has appropriate components giving its type, its definition or whatever other information is required. For example, the variable field has a type and two expressions, giving the constructor and destructor values for the object.

Most of these identifier components are self-explanatory, however the treatment of overloaded functions bears discussion. The various fields representing functions have an over component which is used to link overloaded functions together. A set of overloaded functions is treated as if it were a single IDENTIFIER - the first in the list - for the purposes of storing in a namespace member; the other overloaded meanings are accessed by chasing down the over components. In other situations, whether a function identifier represents a single function or a set of overloaded functions can be worked out from the context. For example, in identifier expressions the identifier qualifier is used to mark whether overload resolution has taken place.

2.2.18. MEMBER

The union type MEMBER (member) is used to represent a member of a namespace. Each member contains two identifiers, id and alt. The id field gives the meaning associated with a particular name in this namespace; the alt field is used to represent a type name which may be hidden by a non-type name.

There are two kinds of member, small and large, corresponding to whether the namespace holds its members in a simple linked list or in a hash table.

2.2.19. NAMESPACE

The union type NAMESPACE (nspace) is used to represent the set of identifiers declared in a particular scope. For example, the members declared in a C++ class or namespace, the parameters declared in a function declarator and the local variables declared in a block all form scopes. The various kinds of scope are distinguished as different fields of the union, but there are basically two categories. The first, such as function blocks, which have relatively small numbers of elements, store their members as a simple linked lists. The second, such as classes, which have larger numbers of elements, store their members in hash tables. In both cases the elements are stored using the MEMBER type.

The key operation on a namespace is to look up a particular identifier name in its linked list or hash table of members to find the meaning, if any, associated with that name in the namespace. This can be a complex operation because of the need to take base classes and using directives (as stored in the use component) into account.

2.2.20. NAT

The union type NAT (nat) is used to represent an integer constant expression. Values are represented as lists of 16 bit 'digits'. Values which fit into a single digit are represented by the small field; larger values by the large field. Negated values can be represented by the neg field. Folding of integer constant expressions is performed in the producer, however the result can only be represented as described above if its value is target independent. Target dependent values are represented by the calc field which contains an expression describing how to calculate the value. The token field is used to represent NAT tokens.

Objects representing small integer constants are created at the start of the program and stored in a table for ease of access. Larger constants are created as and when they are required.

2.2.21. FLOAT

The union type FLOAT (flt) is used to represent a floating point constant expression. There is only one field, simple , which corresponds to a floating point literal. No folding of floating point constant expressions is attempted in the producer (it is virtually impossible to do so in a target independent manner).

Objects representing useful floating point constants (0.0, 1.0 etc.) are created for each floating point type and stored as part of the corresponding FLOAT_TYPE. Other values are created as and when they are required.

2.2.22. STRING

The union type STRING (str) is used to represent a string constant expression. There is only one field, simple, which corresponds to a character string literal, however the kind field can be used to modify the interpretation put on the characters appearing in the text field. By default, each character in text corresponds to a single character in the literal; however an alternative representation, in which text consists of a sequence of multibyte characters - one control character plus four value characters - is used in more complex cases.

All strings are stored in a hash table intended to ensure that the same STRING object is used for equal string literals. This not only saves space during the processing of the input file, but also facilitates the output of shared string literals in the TDF capsule.

Note that the terminal zero character does not form part of the STRING object. Instead information on this is stored as part of the type of a string literal expression. The text of the string literal is either truncated or padded with zeros until its length matches the size of the array bound in the type of the corresponding literal expression.

2.2.23. NTEST

The enumeration type NTEST (ntest) is used to represent the various C++ relational operators (==, !=, > etc.). The values correspond to the encoding of the TDF NTEST sort, which facilitates code generation. The values also have the property that the values for complementary operators (such as < and >=) always add up to the same value, ntest_negate, allowing operators to be complemented in a straightforward manner.

2.2.24. RMODE

The enumeration type RMODE (rmode) is used to represent the various C++ rounding modes (towards zero, towards smaller etc.). The values correspond to the encoding of the TDF RMODE sort, which facilitates code generation.

2.2.25. EXP

The union type EXP (exp) is used to represent a C++ expression or statement. Each expression has an associated type, type, but most of the information about an expression is stored in one of the large number of fields of the EXP union. Most of these fields are fairly simple. For example, there are fields corresponding to integer literals, floating point literals, string literals and identifiers. Composite expressions are formed in the normal way; for example, there are various binary operators comprising two argument expressions. The EXP fields corresponding to statements are slightly more complex. They each have a parent field which points to the enclosing statement. A couple of cases bear additional discussion.

The sequence field represents a compound statement or block. This contains a namespace, in which any local variables are declared, and a list of expressions, giving the statements comprising the block. The null namespace is used if the block does not constitute a scope. The first statement in the list is always a dummy to enable first and last pointers to be maintained to the start and end of the list without having to worry about null lists.

The solve_stmt field corresponds to the TDF labelled construct (in early versions of TDF this construct was called solve, hence the terminology). The problem is that C and C++ labels and gotos are totally unstructured, whereas the TDF label constructs are structured. Any statement which contains unstructured labels is enclosed in a solve_stmt construct, enclosing both the labelled statement and all jumps to it (in general this cannot be done until the end of the function). Any labels or variables which are bypassed by such unstructured jumps also need to be pulled out to the solve_stmt construct. It is not just explicit labels which can cause such problems; complex switch statements have the same effect.

2.2.26. OFFSET

The union type OFFSET (off) is used to represent an offset expression. This is used as an adjunct to the normal expression representation. The OFFSET union has fields corresponding to a type offset (used in pointer arithmetic), the offset of a member of a class and the offset of a base class. There are also simple operations on offsets, such as multiplication by an expression.

2.2.27. TOKEN

The union type TOKEN (tok) is used to represent one of a number of different categories within the C++ language. It corresponds to the sort of a token declared using the tdfc2pragma #pragma token syntax. Thus there are fields corresponding to expression, statement, integer constant, type, function, member and procedure tokens. The similarities between PROC tokens and templates have been remarked above; for example, the parameters of the template:

template < class T, int n > class A {
    T a [n] ;
    // ....
} ;

are essentially equivalent to those in the procedure token:

PROC ( TYPE T, EXP const : int : n ) ....

(recall that non-type template arguments are always constant expressions). Thus a field, templ, of the TOKEN union is used to represent lists of template parameters. Note that a further field, class, is also required to represent template template parameters. A template type is represented by a field, templ, of the union TYPE, which comprises a template sort and a sub-type expressed in terms of the template parameters.

In addition to representing token and template sorts in this way, the TOKEN union is used to represent token and template arguments. Each of the parameter sorts listed above has an appropriate value component which can store a value of that sort. Many of the union types in the algebra, including types and expressions, have a field of the form:

token -> {
    IDENTIFIER tok ;
    LIST TOKEN args ;
}

representing the given token identifier applied to the given list of arguments.

Template instances are represented slightly differently from token applications. Each instance of a template class or a template function gives rise to a new class or function identifier. This identifier has an underlying form giving the template identifier and the template arguments. This is expressed as a token member of the TYPE union (although it is not technically a type, this happens to be the most convenient representation). Each such form has an associated INSTANCE component which gives further information about the template instance. The form for a template function instance is stored in the form component of the corresponding identifier. The form for a template class instance is stored in the form component of the corresponding class type.

Members of instances of template classes also have a form type, but in this case the form is an instance type. This gives a link back to the corresponding member of the template class.

2.2.28. INSTANCE

The union type INSTANCE (inst) is used to represent a particular instance of a template or token. Each template sort has an associated list of all the instances of that template, which is used to ensure that the same template applied with the same arguments always has the same value. Information on partial or explicit specialisations and usage information are stored as part of the corresponding INSTANCE. Each template instance identifier has a link back to its corresponding INSTANCE via its form component.

2.2.29. ERROR

The union type ERROR (err) is used to represent an error arising during the compilation of a C++ program. Errors are first class objects within the producer and can be passed to and from procedures. Each error has an associated severity (serious, warning, none etc.). Simple errors are represented by the simple field, which consists of an index, number, into the error catalogue, plus a variable length list of error arguments. Errors can be combined into composite errors using the compound field, which represents the join of two errors - head followed by tail.

The chief operation on an error after it has been built up is to report it. Each error report consists of an error object and a file location indicating where the error occurred.

2.2.30. VARIABLE

The structure type VARIABLE (var) is used to represent a variable state and is used in the variable analysis checks.

2.2.31. LOCATION

The structure type LOCATION (loc) is used to represent a location in an input file. It comprises a pointer to an input file position, posn, modified by a line number, taking #line directives into account, line. Note that character positions within the line are not currently recorded.

2.2.32. POSITION

The structure type POSITION (posn) is used to represent a position in an input file. It consists of two file names, file taking #line directives into account, and input giving the actual file name, plus a line number offset, offset, which gives the difference between the line number taking #line directives into account and the actual line number. Other information stored includes the datestamp on the input file, datestamp, and a pointer to a file location which, for files included using #include, gives the location the file was included from.

2.2.33. BITSTREAM

The structure BITSTREAM is not part of the calculus type system. It is used to represent a sequence of bits such as is used, for example, in the encoding of TDF.

2.2.34. BUFFER

The structure BUFFER is not part of the calculus type system. It is used to represent a sequence of characters.

2.2.35. OPTIONS

The structure OPTIONS is not part of the calculus type system. It is used to represent the state of the tdfc2pragma compiler options at a particular point in the input file.

2.2.36. PPTOKEN

The structure PPTOKEN is not part of the calculus type system. It is used to represent a linked list of preprocessing tokens. Each token has an associated sid lexical token number, tok, plus additional data dependent on the token type. Each token also records a pointer to the current OPTIONS value.

3. Scalar types

  1. 3.1. Arithmetic types
  2. 3.2. Integer literal types
  3. 3.3. Bitfield types

3.1. Arithmetic types

The representations of the basic arithmetic types are target dependent, so, for example, an int may contain 16, 32, 64 or some other number of bits. Thus it is necessary to introduce a token to stand for each of the built-in arithmetic types (including the long long types). Each integral type is represented by a VARIETY token as follows:

TypeTokenEncoding
char~char0
signed char~signed_char0 | 4=4
unsigned char~unsigned_char0 | 8=8
signed short~signed_short1 | 4=5
unsigned short~unsigned_short1 | 8=9
signed int~signed_int2 | 4=6
unsigned int~unsigned_int2 | 8=10
signed long~signed_long3 | 4=7
unsigned long~unsigned_long3 | 8=11
signed long long~signed_longlong3 | 4 | 16=23
unsigned long long~unsigned_longlong3 | 8 | 16=27

Similarly each floating point type is represent by a FLOATING_VARIETY token:

TypeToken
float~float
double~double
long double~long_double

Each integral type also has an encoding as a SIGNED_NAT as shown above. This number is a bit pattern built up from the following values:

TypeEncoding
char0
short1
int2
long3
signed4
unsigned8
long long16

Any target dependent integral type can be represented by a SIGNED_NAT token using this encoding. This representation, rather than one based on VARIETYs, is used for ease of manipulation. The token:

~convert : ( SIGNED_NAT ) -> VARIETY

gives the mapping from the integral encoding to the representing variety. For example, it will map 6 to ~signed_int.

The token:

~promote : ( SIGNED_NAT ) -> SIGNED_NAT

describes how to form the promotion of an integral type according to the ISO C/C++ value preserving rules, and is used by the producer to represent target dependent promotion types. For example, the promotion of unsigned short may be int or unsigned int depending on the representation of these types; that is to say, ~promote ( 9 ) will be 6 on some machines and 10 on others. Although ~promote is used by default, a program may specify another token with the same sort signature to be used in its place by means of the directive:

#pragma TenDRA compute promote identifier

For example, a standard token ~sign_promote is defined which gives the older C sign preserving promotion rules. In addition, the promotion of an individual type can be specified using:

#pragma TenDRA promoted type-id : promoted-type-id

The token:

~arith_type : ( SIGNED_NAT, SIGNED_NAT ) -> SIGNED_NAT

similarly describes how to form the usual arithmetic result type from two promoted integral operand types. For example, the arithmetic type of long and unsigned int may be long or unsigned long depending on the representation of these types; that is to say, ~arith_type ( 7, 10 ) will be 7 on some machines and 11 on others.

Any tokenised type declared using:

#pragma token VARIETY v # tv

will be represented by a SIGNED_NAT token with external name tv corresponding to the encoding of v. Special cases of this are the implementation dependent integral types which arise naturally within the language. The external token names for these types are given below:

TypeToken
bool~cpp.bool
ptrdiff_tptrdiff_t
size_tsize_t
wchar_twchar_t

So, for example, a sizeof expression has shape ~convert ( size_t ). The token ~cpp.bool is defined in the default implementation, but the other tokens are defined according to their definitions on the target machine in the normal API library building mechanism.

3.2. Integer literal types

The type of an integer literal is defined in terms of the first in a list of possible integral types. The first type in which the literal value can be represented gives the type of the literal. For small literals it is possible to work out the type exactly, however for larger literals the result is target dependent. For example, the literal 50000 will have type int on machines in which 50000 fits into an int, and long otherwise. This target dependent mapping is given by a series of tokens of the form:

~lit_* : ( SIGNED_NAT ) -> SIGNED_NAT

which map a literal value to the representation of an integral type. The token used depends on the list of possible types, which in turn depends on the base used to represent the literal and the integer suffix used, as given in the following table:

BaseSuffixTokenTypes
decimal(none)~lit_intint, long, unsigned long
octal(none)~lit_hexint, unsigned int, long, unsigned long
hexadecimal(none)~lit_hexint, unsigned int, long, unsigned long
anyU~lit_unsignedunsigned int, unsigned long
anyL~lit_longlong, unsigned long
anyUL~lit_ulongunsigned long
anyLL~lit_longlonglong long, unsigned long long
anyULL~lit_ulonglongunsigned long long

Thus, for example, the shape of the integer literal 50000 is:

~convert ( ~lit_int ( 50000 ) )

3.3. Bitfield types

The sign of a plain bitfield type, declared without using signed or unsigned, is left unspecified in C and C++. The token:

~cpp.bitf_sign : ( SIGNED_NAT ) -> BOOL

is used to give a mapping from integral types to the sign of a plain bitfield of that type, in a form suitable for use in the TDF bfvar_bits construct. (Note that ~cpp.bitf_sign should have been a standard C token but was omitted.)

4. Pointers

  1. 4.1. Generic pointers
  2. 4.2. Pointers to data members
  3. 4.3. Pointers to function members

4.1. Generic pointers

TDF has no concept of a generic pointer type, so tokens are used to defer the representation of void * and the basic operations on it to the target machine. The fundamental token is:

~ptr_void : () -> SHAPE

which gives the representation of void *. This shape will be denoted by pv in the description of the following tokens. It is not guaranteed that pv is a TDF pointer shape, although normally it will be implemented as a pointer to a suitable alignment.

The token:

~null_pv : () -> EXP pv

gives the value of a null pointer of type void *. Generic pointers can also be converted to and from other pointers. These conversions are represented by the tokens:

~to_ptr_void : ( ALIGNMENT a, EXP POINTER a ) -> EXP pv
~from_ptr_void : ( ALIGNMENT a, EXP pv ) -> EXP POINTER a

where the given alignment describes the destination or source pointer type. Finally a generic pointer may be tested against the null pointer or two generic pointers may be compared. These operations are represented by the tokens:

~cpp.pv_compare : ( EXP pv, EXP pv, LABEL, NTEST ) -> EXP TOP

where the given NTEST gives the comparison to be applied and the given label gives the destination to jump to if the test fails. (Note that ~cpp.pv_compare should have been a standard C token but was omitted.)

4.2. Pointers to data members

The representation of, and operations on, pointers to data members are represented by tokens to allow for a variety of implementations. It is assumed that all pointers to data members (as opposed to pointers to function members) are represented by the same shape:

~cpp.pm.type : () -> SHAPE

This shape will be denoted by pm in the description of the following tokens.

There are two basic methods of constructing a pointer to a data member. The first is to take the address of a data member of a class. A data member is represented in TDF by an expression which gives the offset of the member from the start of its enclosing compound shape (note that it is not possible to take the address of a member of a virtual base). The mapping from this offset to a pointer to a data member is given by:

~cpp.pm.make : ( EXP OFFSET ) -> EXP pm

The second way of constructing a pointer to a data member is to use a null pointer to member:

~cpp.pm.null : () -> EXP pm

The other fundamental operation on a pointer to data member is to turn it back into an offset expression which can be added to a pointer to a class to access a member of that class in a .* or ->* operation. This is done by the token:

~cpp.pm.offset : ( EXP pm, ALIGNMENT a ) -> EXP OFFSET ( a, a )

Note that it is necessary to specify an alignment in order to describe the shape of the result. The value of this token is undefined if the given expression is a null pointer to data member.

A pointer to a data member of a non-virtual base class can be converted to a pointer to a data member of a derived class. The reverse conversion is also possible using static_cast. If the base is a primary base class then these conversions are trivial and have no effect. Otherwise null pointers to data members are converted to null pointers to data members, and the non-null cases are handled by the tokens:

~cpp.pm.cast : ( EXP pm, EXP OFFSET ) -> EXP pm
~cpp.pm.uncast : ( EXP pm, EXP OFFSET ) -> EXP pm

where the given offset is the offset of the base class within the derived class. It is also possible to convert between any two pointers to data members using reinterpret_cast. This conversion is implied by the equality of representation between any two pointers to data members and has no effect.

The only remaining operations on pointer to data members are to test one against the null pointer to data member and to compare two pointer to data members. These are represented by the tokens:

~cpp.pm.test : ( EXP pm, LABEL, NTEST ) -> EXP TOP
~cpp.pm.compare : ( EXP pm, EXP pm, LABEL, NTEST ) -> EXP TOP

where the given NTEST gives the comparison to be applied and the given label gives the destination to jump to if the test fails.

In the default implementation, pointers to data members are implemented as int. The null pointer to member is represented by 0 and the address of a class member is represented by 1 plus the offset of the member (in bytes). Casting to and from a derived class then correspond to adding or subtracting the base class offset (in bytes), and pointer to member comparisons correspond to integer comparisons.

4.3. Pointers to function members

As with pointers to data members, pointers to function members and the operations on them are represented by tokens to allow for a range of implementations. All pointers to function members are represented by the same shape:

~cpp.pmf.type : () -> SHAPE

This shape will be denoted by pmf in the description of the following tokens. Many of the tokens take an expression which has a shape which is a pointer to the alignment of pmf. This will be denoted by ppmf.

There are two basic methods for constructing a pointer to a function member. The first is to take the address of a non-static member function of a class. There are two cases, depending on whether or not the member function is virtual. The non-virtual case is given by the token:

~cpp.pmf.make : ( EXP PROC, EXP OFFSET, EXP OFFSET ) -> EXP pmf

where the first argument is the address of the corresponding function, the second argument gives any base class offset which is to be added when calling this function (to deal with inherited member functions), and the third argument is a zero offset.

For virtual functions, a pointer to function member of the form above is entered in the virtual function table for the corresponding class. The actual pointer to the virtual function member then gives a reference into the virtual function table as follows:

~cpp.pmf.vmake : ( SIGNED_NAT, EXP OFFSET, EXP, EXP ) -> EXP pmf

where the first argument gives the index of the function within the virtual function table, the second argument gives the offset of the vptr field within the class, and the third and fourth arguments are zero offsets.

The second way of constructing a pointer to a function member is to use a null pointer to function member:

~cpp.pmf.null : () -> EXP pmf
~cpp.pmf.null2 : () -> EXP pmf

For technical reasons there are two versions of this token, although they have the same value. The first token is used in static initialisers; the second token is used in other expressions.

The cast operations on pointers to function members are more complex than those on pointers to data members. The value to be cast is copied into a temporary and one of the tokens:

~cpp.pmf.cast : ( EXP ppmf, EXP OFFSET, EXP, EXP OFFSET ) -> EXP TOP
~cpp.pmf.uncast : ( EXP ppmf, EXP OFFSET, EXP, EXP OFFSET ) -> EXP TOP

is applied to modify the value of the temporary according to the given cast. The first argument gives the address of the temporary, the second gives the base class offset to be added or subtracted, the third gives the number to be added or subtracted to convert virtual function indexes for the base class into virtual function indexes for the derived class, and the fourth gives the offset of the vptr field within the class. Again, the ability to use reinterpret_cast to convert between any two pointer to function member types arises because of the uniform representation of these types.

As with pointers to data members, there are tokens implementing comparisons on pointers to function members:

~cpp.pmf.test : ( EXP ppmf, LABEL, NTEST ) -> EXP TOP
~cpp.pmf.compare : ( EXP ppmf, EXP ppmf, LABEL, NTEST ) -> EXP TOP

Note however that the arguments are passed by reference.

The most important, and most complex, operation is calling a function through a pointer to function member. The first step is to copy the pointer to function member into a temporary. The token:

~cpp.pmf.virt : ( EXP ppmf, EXP, ALIGNMENT ) -> EXP TOP

is then applied to the temporary to convert a pointer to a virtual function member to a normal pointer to function member by looking it up in the corresponding virtual function table. The first argument gives the address of the temporary, the second gives the object to which the function is to be applied, and the third gives the alignment of the corresponding class. Now the base class conversion to be applied to the object can be determined by applying the token:

~cpp.pmf.delta : ( ALIGNMENT a, EXP ppmf ) -> EXP OFFSET ( a, a )

to the temporary to find the offset to be added. Finally the function to be called can be extracted from the temporary using the token:

~cpp.pmf.func : ( EXP ppmf ) -> EXP PROC

The function call then procedes as normal.

The default implementation is that described in the ARM, where each pointer to function member is represented in the form:

struct PTR_MEM_FUNC {
    short delta ;
    short index ;
    union {
	void ( *func ) () ;
	short off ;
    } u ;
} ;

The delta field gives the base class offset (in bytes) to be added before applying the function. The index field is 0 for null pointers, -1 for non-virtual function pointers and the index into the virtual function table for virtual function pointers (as described below these indexes start from 1). For non-virtual function pointers the function itself is given by the u.func field. For virtual function pointers the offset of the vptr field within the class is given by the u.off field.

5. Calling conventions

  1. 5.1. Member functions
  2. 5.2. Ellipsis functions

The function calling conventions used by the C++ producer are essentially the same as those used by the C producer with one exception. That is to say, all types except arrays are passed by value (note that individual installers may modify these conventions to conform to their own ABIs).

The exception concerns classes with a non-trivial constructor, destructor or assignment operator. These classes are passed as function arguments by taking a reference to a copy of the object (although it is often possible to eliminate the copy and pass a reference to the object directly). They are passed as function return values by adding an extra parameter to the start of the function parameters giving a reference to a location into which the return value should be copied.

5.1. Member functions

Non-static member functions are implemented in the obvious fashion, by passing a pointer to the object the method is being applied to as the first argument (or the second argument if the method has an extra argument for its return value).

5.2. Ellipsis functions

Calls to functions declared with ellipses are via the apply_proc TDF construct, with all the arguments being treated as non-variable. However the definition of such a function uses the make_proc construct with a variable parameter. This parameter can be referred to within the program using the ... expression. The type of this expression is given by the built-in token:

~__va_t : () -> SHAPE

The va_start macro declared in the <stdarg.h> header then describes how the variable parameter (expressed as ...) can be converted to an expression of type va_list suitable for use in the va_arg macro.

Note that the variable parameter is in effect only being used to determine where the first optional parameter is defined. The assumption is that all such parameters are located contiguously on the stack, however the fact that calls to such functions do not use the variable parameter mechanism means that this is not automatically the case. Strictly speaking this means that the implementation of ellipsis functions uses undefined behaviour in TDF, however given the non-type-safe function calling rules in C this is unavoidable and installers need to make provision for such calls (by dumping any parameters from registers to the stack if necessary). Given the theoretically type-safe nature of C++ it would be possible to avoid such undefined behaviour, but the need for C-compatible calling conventions prevents this.

6. Classes

  1. 6.1. Class layout
  2. 6.2. Derived class layout
    1. 6.2.1. Single inheritance
    2. 6.2.2. Multiple inheritance
    3. 6.2.3. Virtual inheritance
  3. 6.3. Constructors and destructors
  4. 6.4. Virtual function tables

6.1. Class layout

Consider a class with no base classes:

class A {
    // A's members
} ;

Each object of class A needs its own copy of the non-static data members of A and, for polymorphic types, a means of referencing the virtual function table and run-time type information for A. This is accomplished using a layout of the form:

A vptr A typeid A ptr typeid A voff A vfunc1 vfunc2 vfuncn ... voff A
Figure 1. Class A

where the A component consists of the non-static data members and vptr A is a pointer to the virtual function table for A. For non-polymorphic classes the vptr A field is omitted; otherwise space for vptr A needs to be allocated within the class and the pointer needs to be initialised in each constructor for A. The precise layout of the virtual function table and the run-time type information is given below.

Two alternative ways of laying out the non-static data members within the class are implemented. The first, which is default, gives them in the order in which they are declared in the class definition. The second lays out the public, the protected, and the private members in three distinct sections, the members within each section being given in the order in which they are declared. The latter can be enabled using the -jo command-line option.

The offset of each member within the class (including vptr A) can be calculated in terms of the offset of the previous member. The first member has offset zero. The offset of any other member is given by the offset of the previous member plus the size of the previous member, rounded up to the alignment of the current member. The overall size of the class is given by the offset of the last member plus the size of the last member, rounded up using the token:

~comp_off : ( EXP OFFSET ) -> EXP OFFSET

which allows for any target dependent padding at the end of the class. The shape of the class is then a compound shape with this offset.

Classes with no members need to be treated slightly differently. The shape of such a class is given by the token:

~cpp.empty.shape : () -> SHAPE

(recall that an empty class still has a nonzero size). The token:

~cpp.empty.offset : () -> EXP OFFSET

is used to represent the offset required for an empty class when it is used as a base class. This may be a zero offset.

Bitfield members provide a slight complication to the picture above. The offset of a bitfield is additionally padded using the token:

~pad : ( EXP OFFSET, SHAPE, SHAPE ) -> EXP OFFSET

where the two shapes give the type underlying the bitfield and the bitfield itself.

The layout of unions is similar to that of classes except that all members have zero offset, and the size of the union is the maximum of the sizes of its members, suitably padded. Of course unions cannot be polymorphic and cannot have base classes.

Pointers to incomplete classes are represented by means of the alignment:

~cpp.empty.align : () -> ALIGNMENT

This token is also used for the alignment of a complete class if that class is never used in the generated TDF in a manner which requires it to be complete. This can lead to savings on the size of the generated code by preventing the need to define all the member offset tokens in order to find the shape of the class.

6.2. Derived class layout

The description of the implementation of derived classes will be given in terms of the example class hierarchy given by:

class A {
    // A's members
} ;

class B : public A {
    // B's members
} ;

class C : public A {
    // C's members
} ;

class D : public B, public C {
    // D's members
} ;

or, as a directed acyclic graph:

D B A C A
Figure 2. Class D

6.2.1. Single inheritance

The layout of class A is given by:

A vptr A vtbl A typeid A
Figure 3. Class A

as above. Class B inherits all the members of class A plus those members explicitly declared within class B. In addition, class B inherits all the virtual member functions of A, some of which may be overridden in B, extended by any additional virtual functions declared in B. This may be represented as follows:

A B vptr B vptr B::A vtbl B::A vtbl B typeid B
Figure 4. Class B

where A denotes those members inherited from the base class and B denotes those members added in the derived class. Note that an object of class B contains a sub-object of class A. The fact that this sub-object is located at the start of B means that the base class conversion from B to A is trivial. Any base class with this property is called a primary base class.

Note that in theory two virtual function tables are required, the normal virtual function table for B, denoted by vtbl B, and a modified virtual function table for A, denoted by vtbl B::A, taking into account any overriding virtual functions within B, and pointing to B's run-time type information. This latter means that the dynamic type information for the A sub-object relates to B rather than A. However these two tables can usually be combined - if the virtual functions added in B are listed in the virtual function table after those inherited from A and the form of the overriding is suitably well behaved (in the sense defined below) then vptr B::A is an initial segment of vptr B. It is also possible to remove the vptr B field and use vptr B::A in its place in this case (it has to be this way round to preserve the A sub-object). Thus the items shaded in the diagram can be removed.

The class C is similarly given by:

A C vptr C vptr C::A vtbl C::A vtbl C typeid C
Figure 5. Class C

6.2.2. Multiple inheritance

Class D is more complex because of the presence of multiple inheritance. D inherits all the members of B, including those which B inherits from A, plus all the members of C, including those which C inherits from A. It also inherits all of the virtual member functions from B and C, some of which may be overridden in D, extended by any additional virtual functions declared in D. This may be represented as follows:

A B C D vptr D::B::A vptr D::B vptr D::C::A vptr D::C vptr D vtbl D::B::A vtbl D::B vtbl D::C::A vtbl D::C vtbl D typeid D delta D::C
Figure 6. Class D

Note that there are two copies of A in D because virtual inheritance has not been used.

The B base class of D is essentially similar to the single inheritance case already discussed; the C base class is different however. Note firstly that the C sub-object of D is located at a non-zero offset, delta D::C, from the start of the object. This means that the base class conversion from D to C consists of adding this offset (for pointer conversions things are further complicated by the need to allow for null pointers). Also vtbl D::C is not an initial segment of vtbl D because this contains the virtual functions inherited from B first, followed by those inherited from C, followed by those first declared in D (there are other reasons as well). Thus vtbl D::C cannot be eliminated.

6.2.3. Virtual inheritance

Virtual inheritance introduces a further complication. Now consider the class hierarchy given by:

class A {
// A's members
} ;

class B : virtual public A {
// B's members
} ;

class C : virtual public A {
// C's members
} ;

class D : public B, public C {
// D's members
} ;

or, as a directed acyclic graph:

D B C A
Figure 7. Class D

As before A is given by:

A vptr A vtbl A typeid A
Figure 8. Class A

but now B is given by:

ptr A A B vptr B::A vtbl B::A vptr B vtbl B typeid B
Figure 9. Class B

Rather than having the sub-object of class A directly as part of B, the class now contains a pointer, ptr A, to this sub-object. The virtual sub-objects are always located at the end of a class layout; their offset may therefore vary for different objects, however the offset for ptr A is always fixed. The ptr A field is initialised in each constructor for B. In order to perform the base class conversion from B to A, the contents of ptr A are taken (again provision needs to be made for null pointers in pointer conversions). In cases when the dynamic type of the B object can be determined statically it is possible to access the A sub-object directly by adding a suitable offset. Because this conversion is non-trivial (see below) the virtual function table vtbl B::A is not an initial segment of vtbl B and cannot be eliminated.

The class C is similarly given by:

ptr A A C vptr C::A vtbl C::A vptr C vtbl C typeid C
Figure 10. Class C

Now the class D is given by:

ptr A A B C D vptr D::A vptr D::B vptr C::A vtbl D::A vtbl D::B vtbl D::C vptr D::C vptr D vtbl C vtbl D typeid D
Figure 11. Class D

Note that there is a single A sub-object of D referenced by the ptr A fields in both the B and C sub-objects. The elimination of vtbl D::B is as above.

6.3. Constructors and destructors

The implementation of constructors and destructors, whether explicitly or implicitly defined, is slightly more complex than that of other member functions. For example, the constructors need to set up the internal vptr and ptr fields mentioned above.

The order of initialisation in a constructor is as follows:

  • The internal ptr fields giving the locations of the virtual base classes are initialised.

  • The constructors for the virtual base classes are called.

  • The constructors for the non-virtual direct base classes are called.

  • The internal vptr fields giving the locations of the virtual function tables are initialised.

  • The constructors for the members of the class are called.

  • The main constructor body is executed.

To ensure that each virtual base is only initialised once, if a class has a virtual base class then all its constructors have an implicit extra parameter of type int. The first two steps above are then only applied if this flag is nonzero. In normal applications of the constructor this argument will be 1, however in base class initialisations such as those in the third and fourth steps above, it will be 0.

Note that similar steps to protect virtual base classes are not taken in an implicitly declared operator= function. The order of assignment in this case is as follows:

  • The assignment operators for the direct base classes (both virtual and non-virtual) are called.

  • The assignment operators for the members of the class are called.

  • A reference to the object assigned to (i.e. *this) is returned.

The order of destruction in a destructor is essentially the reverse of the order of construction:

  • The main destructor body is executed.

  • The destructor for the members of the class are called.

  • The internal vptr fields giving the locations of the virtual function tables are re-initialised.

  • The destructors for the non-virtual direct base classes are called.

  • The destructors for the virtual base classes are called.

  • If necessary the space occupied by the object is deallocated.

All destructors have an extra parameter of type int. The virtual base classes are only destroyed if this flag is nonzero when and-ed with 2. The space occupied by the object is only deallocated if this flag is nonzero when and-ed with 1. This deallocation is equivalent to inserting:

delete this ;

in the destructor. The operator delete function is called via the destructor in this way in order to implement the pseudo-virtual nature of these deallocation functions. Thus for normal destructor calls the extra argument is 2, for base class destructor calls it is 0, and for calls arising from a delete expression it is 3.

The point at which the virtual function tables are initialised in the constructor, and the fact that they are re-initialised in the destructor, is to ensure that virtual functions called from base class initialisers are handled correctly (see ISO C++ 12.7).

A further complication arises from the need to destroy partially constructed objects if an exception is thrown in a constructor. A count is maintained of the number of base classes and members constructed within a constructor. If an exception is thrown then it is caught in the constructor, the constructed base classes and members are destroyed, and the exception is re-thrown. The count variable is used to determine which bases and members need to be destroyed.

These partial destructors currently do not interact correctly with any exception specification on the constructor. Exceptions thrown within destructors are not correctly handled either.

6.4. Virtual function tables

The virtual functions in a polymorphic class are given in its virtual function table in the following order: firstly those virtual functions inherited from its direct base classes (which may be overridden in the derived class) followed by those first declared in the derived class in the order in which they are declared. Note that this can result in virtual functions inherited from virtual base classes appearing more than once. The virtual functions are numbered from 1 (this is slightly more convenient than numbering from 0 in the default implementation).

The virtual function table for this class has shape:

~cpp.vtab.type : ( NAT ) -> SHAPE

the argument being n + 1 where n is the number of virtual functions in the class (there is also a token:

~cpp.vtab.diag : () -> SHAPE

which is used in the diagnostic output for a generic virtual function table). The table is created using the token:

~cpp.vtab.make : ( EXP pti, EXP OFFSET, NAT, EXP NOF ) -> EXP vt

where the first expression gives the address of the run-time type information structure for the class, the second expression gives the offset of the vptr field within the class (i.e. voff), the integer constant is n + 1, and the final expression is a make_nof construct giving information on each of the n virtual functions.

The information given on each virtual function in this table has the form of a pointer to function member formed using the token:

~cpp.pmf.make : ( EXP PROC, EXP OFFSET, EXP OFFSET ) -> EXP pmf

as above, except that the third argument gives the offset of the base class in virtual function tables such as vtbl B::A. For pure virtual functions the function pointer in this token is given by:

~cpp.vtab.pure : () -> EXP PROC

In the default implementation this gives a function __TCPPLUS_pure which just calls abort.

To avoid duplicate copies of virtual function tables and run-time type information structures being created, the ARM algorithm is used. The virtual function table and run-time type information structure for a class are defined in the module containing the definition of the first non-inline, non-pure virtual function declared in that class. If such a function does not exist then duplicate copies are created in every module which requires them. In the former case the virtual function table will have an external tag name; in the latter case it will be an internal tag. This scheme can be overridden using the -jv command-line option, which causes local virtual function tables to be output for all classes.

Note that the discussion above applies to both simple virtual function tables, such as vtbl B above, and to those arising from base classes, such as vtbl B::A. We are now in a position to precisely determine when vtbl B::A is an initial segment of vtbl B and hence can be eliminated. Firstly, A must be the first direct base class of B and cannot be virtual. This is to ensure both that there are no virtual functions in vtbl B before those inherited from A, and that the corresponding base class conversion is trivial so that the pointers to function members of B comprising the virtual function table can be equally regarded as pointers to function members of A. The second requirement is that if a virtual function for A, f, is overridden in B then the return type for B::f cannot differ from the return type for A::f by a non-trivial conversion (recall that ISO C++ allows the return types to differ by a base class conversion). In the non-trivial conversion case the function entered in vtbl B::A needs to be, not B::f as in vtbl B, but a stub function which calls B::f and converts its return value to the return type of A::f.

The virtual function call mechanism is implemented using the token:

~cpp.vtab.func : ( EXP ppvt, SIGNED_NAT ) -> EXP ppmf

which has as its arguments a reference to the vptr field of the object the function is to be called for, and the number of the virtual function to be called. It returns a reference to the corresponding pointer to function member within the object's virtual function table. The function is then called by extracting the base class offset to be added, and the function to be called, from this reference using the tokens:

~cpp.pmf.delta : ( ALIGNMENT a, EXP ppmf ) -> EXP OFFSET ( a, a )
~cpp.pmf.func : ( EXP ppmf ) -> EXP PROC

described as part of the pointer to function member call mechanism above.

7. Exceptions

  1. 7.1. Try blocks
  2. 7.2. Local variables
  3. 7.3. Throwing an exception
  4. 7.4. Handling an exception
  5. 7.5. Exception specifications

Conceptually, exception handling can be described in terms of the following diagram:

current end terminate object destroy try catch
Figure 12. Try Stack

At any point in the execution of the program there is a stack of currently active try blocks and currently active local variables. A try block is pushed onto the stack as it is entered and popped from the stack when it is left (whether directly or via a jump). A local variable with a non-trivial destructor is pushed onto the stack just after its constructor has been called at the start of its scope, and popped from the stack just before its destructor is called at the end of its scope (including before jumps out of its scope). Each element of an array is considered a separate object. Each try block has an associated list of handlers. Each local variable has an associated destructor.

Provided no exception is thrown this stack grows and shrinks in a well-behaved manner as execution proceeds. When an exception is thrown an exception manager is invoked to find a matching exception handler. The exception manager proceeds to execute a loop to unwind the stack as follows. If the stack is empty then the exception cannot be caught and std::terminate is called. Otherwise the top element is popped from the stack. If this is a local variable then the associated destructor is called for the variable. If the top element is a try block then the current exception is compared in turn to each of the associated handlers. If a match is found then execution jumps to the handler body, otherwise the exception manager continues to the next element of the stack.

Note that this description is purely conceptual. There is no need for exception handling to be implemented by a stack in this way (although the default implementation uses a similar technique). It does however serve to illustrate the various stages which must exist in any implementation.

7.1. Try blocks

At the start of a try block a variable of shape:

~cpp.try.type : () -> SHAPE

is declared corresponding to the stack element for this block. This is then initialised using the token:

~cpp.try.begin : ( EXP ptb, EXP POINTER fa, EXP POINTER ca ) -> EXP TOP

where the first argument is a pointer to this variable, the second argument is the TDF current_env construct, and the third argument is the result of the TDF make_local_lv construct on the label which is used to mark the first handler associated with the block. Note that the last two arguments enable a TDF long_jump construct to be applied to transfer control to the first handler.

When control exits from a try block, whether by reaching the end of the block or jumping out of it, the block is removed from the stack using the token:

~cpp.try.end : ( EXP ptb ) -> EXP TOP

where the argument is a pointer to the try block variable.

7.2. Local variables

The technique used to add a local variable with a non-trivial destructor to the stack is similar to that used in the dynamic initialisation of global variables. A local variable of shape ~cpp.destr.type is declared at the start of the variable scope. This is initialised just after the constructor for the variable is called using the token:

~cpp.destr.local : ( EXP pd, EXP POINTER c, EXP PROC ) -> EXP TOP

where the first argument is a pointer to the variable being initialised, the second is a pointer to the local variable to be destroyed, and the third is the destructor to be called. At the end of the variable scope, just before its destructor is called, the token:

~cpp.destr.end : ( EXP pd ) -> EXP TOP

where the argument is a pointer to destructor variable, is called to remove the local variable destructor from the stack. Note that partially constructed objects are destroyed within their constructors (see §6.3) so that only completely constructed objects need to be considered.

In cases where the local variable may be conditionally initialised (for example a temporary variable in the second operand of a || operation) the local variable of shape ~cpp.destr.type is initialised to the value given by the token:

~cpp.destr.null : () -> EXP d

(normally it is left uninitialised). Before the destructor for this variable is called the value of the token:

~cpp.destr.ptr : ( EXP pd ) -> EXP POINTER c

is tested. If ~cpp.destr.local has been called for this variable then this token returns a pointer to the variable, otherwise it returns a null pointer. The token ~cpp.destr.end and the destructor are only called if this token indicates that the variable has been initialised.

7.3. Throwing an exception

When a throw expression with an argument is encountered a number of steps performed. Firstly, space is allocated to hold the exception value using the token:

~cpp.except.alloc : ( EXP VARIETY size_t ) -> EXP pv

the argument of which gives the size of the value. The space allocated is returned as an expression of type void *. Secondly, the exception value is copied into the space allocated, using a copy constructor if appropriate. Finally the exception is raised using the token:

~cpp.except.throw : ( EXP pv, EXP pti, EXP PROC ) -> EXP BOTTOM

The first argument gives the pointer to the exception value, returned by ~cpp.except.alloc, the second argument gives a pointer to the run-time type information for the exception type, and the third argument gives the destructor to be called to destroy the exception value (if any). This token sets the current exception to the given values and invokes the exception manager as above.

A throw expression without an argument results in a call to the token:

~cpp.except.rethrow : () -> EXP BOTTOM

which re-invokes the exception manager with the current exception. If there is no current exception then the implementation should call std::terminate.

7.4. Handling an exception

The exception manager proceeds to find an exception in the manner described above, unwinding the stack and calling destructors for local variables. When a try block is popped from the stack a TDF long_jump is applied to transfer control to its list of handlers. For each handler in turn it is checked whether the handler can catch the current exception. For ... handlers this is always true; for other handlers it is checked using the token:

~cpp.except.catch : ( EXP pti ) -> EXP VARIETY int

where the argument is a pointer to the run-time type information for the handler type. This token gives 1 if the exception is caught by this handler, and 0 otherwise. If the exception is not caught by the handler then the next handler is checked, until there are no more handlers associated with the try block. In this case control is passed back to the exception manager by re-throwing the current exception using ~cpp.except.rethrow.

If an exception is caught by a handler then a number of steps are performed. Firstly, if appropriate, the handler variable is initialised by copying the current exception value. A pointer to the current exception value can be obtained using the token:

~cpp.except.value : () -> EXP pv

Once this initialisation is complete the token:

~cpp.except.caught : () -> EXP TOP

is called to indicate that the exception has been caught. The handler body is then executed. When control exits from the handler, whether by reaching the end of the handler or by jumping out of it, the token:

~cpp.except.end : () -> EXP TOP

is called to indicate that the exception has been completed. Note that the implementation should call the destructor for the current exception and free the space allocated by ~cpp.except.alloc at this point. Execution then continues with the statement following the handler.

To conclude, the TDF generated for a try block and its associated list of handlers has the form:

variable (
    long_jump_access,
    stack_tag,
    make_value ( ~cpp.try.type ),
    conditional (
	handler_label,
	sequence (
	    ~cpp.try.begin (
		obtain_tag ( stack_tag ),
		current_env,
		make_local_lv ( handler_label ) ),
		try-block-body,
		~cpp.try.end ),
	    conditional (
		catch_label_1,
		sequence (
		    integer_test (
			not_equal,
			catch_label_1,
			~cpp.except.catch (
			    handler-1-typeid ) )
		    variable (
			handler_tag_1,
			handler-1-init (
			    ~cpp.except.value ),
			sequence (
			    ~cpp.except.caught,
			    handler-1-body ) )
		    ~cpp.except.end )
		conditional (
		    catch_label_2,
		    further-handlers,
		    ~cpp.except.rethrow ) ) ) )

Note that for a local variable to maintain its previous value when an exception is caught in this way it is necessary to declare it using the TDF long_jump_access construct. Any local variable which contains a try block in its scope is declared in this way.

To aid implementations in the writing of exception managers the following standard tokens are provided:

~cpp.ptr.code : () -> SHAPE POINTER ca
~cpp.ptr.frame : () -> SHAPE POINTER fa
~cpp.except.jump : ( EXP POINTER fa, EXP POINTER ca ) -> EXP BOTTOM

These give the shape of the TDF make_local_lv construct, the shape of the TDF current_env construct, and direct access to the TDF long_jump access. The exception manager in the default implementation is a function called __TCPPLUS_throw.

7.5. Exception specifications

If a function is declared with an exception specification then extra code needs to be generated in the function definition to catch any unexpected exceptions thrown by the function and to call std::unexpected . Since this is a potentially high overhead for small functions, this extra code is not generated if it can be proved that such unexpected exceptions can never be thrown (the analysis is essentially the same as that in the exception analysis check; see tdfc2pragma).

The implementation of exception specification is to enclose the entire function definition in a try block. The handler for this block uses ~cpp.except.catch to check whether the current exception can be caught by any of the types listed in the exception specification. If so the current exception is re-thrown. If none of these types catch the current exception then the token:

~cpp.except.bad : ( SIGNED_NAT ) -> EXP TOP

is called. The argument is 1 if the exception specification includes the special type std::bad_exception, and 0 otherwise. The implementation should call std::unexpected, but how any exceptions thrown during this call are to be handled depends on the value of the argument.

8. Run-time type information

  1. 8.1. Defining run-time type information structures
  2. 8.2. Accessing run-time type information

Each C++ type can be associated with a run-time type information structure giving information about that type. These type information structures have shape given by the token:

~cpp.typeid.type : () -> SHAPE

which corresponds to the representation for the standard type std::type_info declared in the header <typeinfo>. Each type information structure consists of a tag number, giving information on the kind of type represented, a string literal, giving the name of the type, and a pointer to a list of base type information structures. These are combined to give a type information structure using the token:

~cpp.typeid.make : ( SIGNED_NAT, EXP, EXP ) -> EXP ti

Each base type information structure has shape given by the token:

~cpp.baseid.type : () -> SHAPE

It consists of a pointer to a type information structure, an expression used to describe the offset of a base class, a pointer to the next base type information structure in the list, and two integers giving information on type qualifiers etc. These are combined to give a base type information structure using the token:

~cpp.baseid.make : ( EXP, EXP, EXP, SIGNED_NAT, SIGNED_NAT ) -> EXP bi

The following table gives the various tag numbers used in type information structures plus a list of the base type information structures associated with each type. Macros giving these tag numbers are provided in the default implementation in a header, interface.h, which is shared by the C++ producer.

TypeFormTagBase information
integer0
floating point1
void2
class or structclass T3[base,access,virtual], ....
unionunion T4
enumerationenum T5
pointercv T *6[T,cv,0]
referencecv T &7[T,cv,0]
pointer to membercv T S::*8[S,0,0], [T,cv,0]
arraycv T [n]9[T,cv,n]
bitfieldcv T : n10[T,cv,n]
C++ functioncv T ( S1, ..., Sn )11[T,cv,0], [S1,0,0], ...., [Sn,0,0]
C functioncv T ( S1, ..., Sn )12[T,cv,0], [S1,0,0], ...., [Sn,0,0]

In the form column cv T is used to denote not only the normal cv-qualifiers but, when T is a function type, the member function cv-qualifiers. Arrays with an unspecified bound are treated as if their bound was zero. Functions with ellipsis are treated as if they had an extra parameter of a dummy type named ... (see below). Note the distinction between C++ and C function types.

Each base type information structure is described as a triple consisting of a type and two integers. One of these integers may be used to encode a type qualifier, cv, as follows:

QualifierEncoding
(none)0
const1
volatile2
const volatile3

The base type information for a class consists of information on each of its direct base classes. The includes the offset of this base within the class (for a virtual base class this is the offset of the corresponding ptr field), whether the base is virtual (1) or not (0), and the base class access, encoded as follows:

AccessEncoding
public0
protected1
private2

For example, the run-time type information structures for the classes declared in the diamond lattice above can be represented as follows:

3 0 1 "A" "B" "C" "D" B::A C::A D::B D::C typeid D = typeid B = typeid C = typeid A =
Figure 13. Type ID D

8.1. Defining run-time type information structures

For built-in types, the run-time type information structure may be referenced by the token:

~cpp.typeid.basic : ( SIGNED_NAT ) -> EXP pti

where the argument gives the encoding of the type as given in the following table:

TypeEncodingTypeEncoding
char0unsigned long11
(error)1float12
void2double13
(bottom)3long double14
signed char4wchar_t16
signed short5bool17
signed int6(ptrdiff_t)18
signed long7(size_t)19
unsigned char8(...)20
unsigned short9signed long long23
unsigned int10unsigned long long27

Note that the encoding for the basic integral types is the same as that given above. The other types are assigned to unused values. Note that the encodings for ptrdiff_t and size_t are not used, instead that for their implementation is used (using the standard tokens ptrdiff_t and size_t). The encodings for bool and wchar_t are used because they are conceptually distinct types even though they are implemented as one of the basic integral types. The type labelled ... is the dummy used in the representation of ellipsis functions. The default implementation uses an array of type information structures, __TCPPLUS_typeid, to implement ~cpp.typeid.basic.

The run-time type information structures for classes are defined in the same place as their virtual function tables. Other run-time type information structures are defined in whatever modules require them. In the former case the type information structure will have an external tag name; in the latter case it will be an internal tag.

8.2. Accessing run-time type information

The primary means of accessing the run-time type information for an object is using the typeid construct. In cases where the operand type can be determined statically, the address of the corresponding type information structure is returned. In other cases the token:

~cpp.typeid.ref : ( EXP ppvt ) -> EXP pti

is used, where the argument gives a reference to the vptr field of the object being checked. From this information it is trivial to trace the corresponding type information.

Another means of querying the run-time type information for an object is using the dynamic_cast construct. When the result cannot be determined statically, this is implemented using the token:

~cpp.dynam.cast : ( EXP ppvt, EXP pti ) -> EXP pv

where the first expression gives a reference to the vptr field of the object being cast and the second gives the run-time type information for the type being cast to. In the default implementation this token is implemented by the procedure __TCPPLUS_dynamic_cast. The key point to note is that the virtual function table contains the offset, voff, of the vptr field from the start of the most complete object. Thus it is possible to find the address of the most complete object. The run-time type information contains enough information to determine whether this object has a sub-object of the type being cast to, and if so, how to find the address of this sub-object. The result is returned as a void *, with the null pointer indicating that the conversion is not possible.

9. Mangled identifier names

  1. 9.1. Mangling identifier names
  2. 9.2. Mangling namespace names
  3. 9.3. Mangling types
  4. 9.4. Other mangled names
  5. 9.5. Mangled name examples

In a similar fashion to other C++ compilers, the C++ producer needs a method of mapping C++ identifiers to a form suitable for further processing, namely TDF tag names. This mangled name contains an encoding of the identifier name, its parent namespace or class and its type. Identifiers with C linkage are not mangled. The producer contains a built-in name unmangler which performs the reverse operation of transforming the mangled form of an identifier name back to the underlying identifier. This can be useful when analysing system linker errors.

Note that the type of an identifier forms part of its mangled name not only for functions, but also for variables. Many other compilers do not mangle variable names, however the ISO C++ rules on namespaces and variables with C linkage make it necessary (this can be suppressed using the -j-n command-line option). Declaring the language linkage of a variable inconsistently can therefore lead to linking errors with the C++ producer which are not detected by other compilers. A common example is:

extern int errno ;

which, leaving aside whether errno is actually an external variable, should be:

extern "C" int errno ;

As described above, the mangled form of an identifier has three components; the identifier name, the identifier namespace and the identifier type. Two underscores (__) are used to separate the name component from the namespace and type components. The mangling scheme used is based on that described in the ARM. The description below is not complete; the mangling and unmangling routines themselves should be consulted for a complete description.

9.1. Mangling identifier names

Simple identifier names are mapped to themselves. Unicode characters of the forms \uxxxx and \Uxxxxxxxx are mapped to __kxxxx and __Kxxxxxxxx respectively, where the hex digits are output in their canonical lower-case form. Constructors are mapped to __ct and destructors to __dt. Conversions functions are mapped to __optype where type is the mangled form of the conversion type. Overloaded operator functions, operator@, are mapped as follows:

OperatorMappingOperatorMappingOperatorMapping
&__ad&=__aad[]__vc
->__rf->*__rm=__as
,__cm~__co/__dv
/=__adv==__eq()__cl
>__gt>=__ge<__lt
<=__le&&__aa||__oo
<<__ls<<=__als-__mi
-=__ami--__mm!__nt
!=__ne|__or|=__aor
+__pl+=__apl++__pp
%__md%=__amd>>__rs
>>=__ars*__ml*=__aml
^__er^=__aerdelete__dl
delete []__vdnew__nwnew []__vn
?:__cn:__cs::__cc
.__df.*__dmabs__ab
max__mxmin__mnsizeof__sz
typeid__tdvtable__tb

Note that this table contains a number of operators which are not part of C++ or cannot be overloaded in C++. These are used in the representation of target dependent integer constants.

9.2. Mangling namespace names

The global namespace is mapped to an empty string. Simple namespace and class names are mapped as above, but are preceded by a series of decimal digits giving the length of the mangled name. Nested namespaces and classes are represented by a sequence of such namespace names, preceded by the number of elements in the sequence. This takes the form Qdigit if there are less than 10 elements, or Q_digits_ if there are more than 10. Note that members of anonymous classes or namespaces are local to their translation unit, and so do not have external tag names.

9.3. Mangling types

The mangling of types is essentially similar to that used in the tdfc2dump symbol table dump format. The type used in the mangled name for an identifier ignores the return type for a function and ignores the most significant bound for an array.

The built-in types are mapped in precisely the same way as in the symbol table dump. Class and enumeration types are mapped to their type names mangled in the same way as the namespace names above. The exception to this is that in a class member, the parent class is mapped to X.

The composite types are again mapped in a similar fashion to that in the dump file. For example, PCc represents const char *. The only difficult case concerns function parameter types where the ARM T and N encodings are used for duplicate parameter types. The function return type is included in the mangled form except for function identifier types. In the cases where the identifier is known always to represent a function (constructors, destructors etc.) the initial F indicating a function type is also omitted.

The types of template functions and classes are represented by the underlying template and the template arguments giving rise to the instance. Template classes are preceded by t; template functions are preceded by G rather than F. Type arguments are represented by Z followed by the type value; non-type arguments are represented by the argument type followed by the argument value. In the underlying type the template parameters are represented by m0, m1 etc. An alternative scheme, in which the mangled form of a template function includes the type of that instance, rather than the underlying template, can be enabled using the -j-f command-line option.

9.4. Other mangled names

The virtual function table for a class, when this is a variable with external linkage, is named __vt__type , where type is the mangled form of the class name. The virtual function table for a base class is named __vt__base where base is a sequence of mangled class names specifying the base class. The run-time type information structure for a type, when this is a variable with external linkage, is named __ti__type, where type is the mangled form of the type name.

9.5. Mangled name examples

The following gives some examples of the name mangling scheme:

class A {
    static int a ;               // a__1Ai
public :
    A () ;                       // __ct__1A
    A ( int ) ;                  // __ct__1Ai
    A ( const A & ) ;            // __ct__1ARCX
    virtual ~A () ;              // __dt__1A
    operator bool () ;           // __opb__1A
    bool operator! () ;          // __nt__1A
} ;

// virtual function table        __vt__1A
// run-time type information     __ti__1A

int f ( A *, int, A * ) ;        // f__FP1AiT1
int b = 2 ;                      // b__i
int c [3] ;                      // c__A_i

namespace N {
    int *p = 0 ;                 // p__1NPi
}

10. Other implementation details

  1. 10.1. Parsing C++
  2. 10.2. Undefined conversions
  3. 10.3. Integer division
  4. 10.4. Dynamic initialisation
  5. 10.5. The std namespace
  6. 10.6. Error catalogue

10.1. Parsing C++

The parser used in the C++ producer is generated using the sid tool. Because of the large size of the generated code (1.3MB), sid is instructed to split its output into a number of more manageable modules.

sid is designed as a parser for grammars which can be transformed into LL(1) grammars. The distinguishing feature of these grammars is that the parser can always decide what to do next based on the current terminal. This is not the case in C++; in some circumstances a potentially unlimited look-ahead is required to distinguish, for example, declaration statements from expression statements. In the technical phrase, C++ is an LL(k) grammar. Fortunately there are relatively few such situations, and sid provides a mechanism, predicates, for bypassing the normal parsing mechanism in these cases. Thus it is possible, although difficult, to express C++ as a sid grammar.

The sid grammar file, syntax.sid, is closely based on the ISO C++ grammar. In particular, the same production names have been used. The grammar has been extended slightly to allow common syntactic errors to be detected elegantly. Other parsing errors are handled by sid's exception mechanism. At present there is only limited recovery after such errors.

The lexical analysis routines in the C++ producer are hand-crafted, based on an initial version generated by the simple lexical analyser generator, lexi. lexi has been used more directly to generate the lexical analysers for certain of the other automatic code generating tools, including calculus, used in the producer.

The sid grammar contains a number of entry points. The most important is parse_file, which is used to parse a complete C++ translation unit. The syntax for the tdfc2pragma #pragma TenDRA directives is included within the same grammar with two entry points, parse_tendra in normal use, and parse_preproc for use in preprocessing mode. There are also entry points in the grammar for each of the kinds of token argument. The parsing routines for token and template arguments are largely hand-crafted, based on these primitives.

Certain parsing operations are performed before control passes to the sid grammar. As mentioned above, these include the processing of token and template applications. The other important case concerns nested name specifiers. For example, in:

class A {
    class B {
    static int c ;
    } ;
} ;

int A::B::c = 0 ;

the qualified identifier A::B::c is split into two terminals, a nested name specifier, A::B::, and an identifier, c, which is looked up in the corresponding namespace. Note that it is at this stage that name look-up occurs. An identifier can be mapped to one of a number of terminals, including keywords, type names, namespace names and other identifiers, according to the result of this look-up. If the look-up gives a macro then this is expanded at this stage.

10.2. Undefined conversions

Several conversions in C and C++ can only be represented by undefined TDF. For example, converting a pointer to an integer can only be represented in TDF by forming a union of the pointer and integer shapes, putting the pointer into the union and pulling the integer out. Such conversions are tokenised. Undefined conversions not mentioned below may be performed by combining those given with the standard, well-defined, conversions.

The token:

~ptr_to_ptr : ( ALIGNMENT a, ALIGNMENT b, EXP POINTER a ) -> EXP POINTER b

is used to convert between two incompatible pointer types. The first alignment describes the source pointer shape while the second describes the destination pointer shape. Note that if the destination alignment is greater than the source alignment then the source pointer can be used in most TDF constructs in place of the destination pointer, so the use of ~ptr_to_ptr can be omitted (the exception is pointer_test which requires equal alignments). Base class pointer conversions are examples of these well-behaved, alignment preserving conversions.

The tokens:

~f_to_pv : ( EXP PROC ) -> EXP pv
~pv_to_f : ( EXP pv ) -> EXP PROC

are used to convert pointers to functions to and from void * (these conversions are not allowed in ISO C/C++ but are in older dialects).

The tokens:

~i_to_p : ( VARIETY v, ALIGNMENT a, EXP INTEGER v ) -> EXP POINTER a
~p_to_i : ( ALIGNMENT a, VARIETY v, EXP POINTER a ) -> EXP INTEGER v
~i_to_pv : ( VARIETY v, EXP INTEGER v ) -> EXP pv
~pv_to_i : ( VARIETY v, EXP pv ) -> EXP INTEGER v

are used to convert integers to and from void * and other pointers.

10.3. Integer division

The precise form of the integer division and remainder operations in C and C++ is left unspecified with respect to the sign of the result if either operand is negative. The tokens:

~div : ( EXP INTEGER v, EXP INTEGER v ) -> EXP INTEGER v
~rem : ( EXP INTEGER v, EXP INTEGER v ) -> EXP INTEGER v

are used to represent integer division and remainder. They will map onto one of the pairs of TDF constructs, div0 and rem0, div1 and rem1 or div2 and rem2.

10.4. Dynamic initialisation

The dynamic initialisation of variables with static storage duration in C++ is implemented by means of the TDF initial_value construct. However in order for the producer to maintain control over the order of initialisation, rather than each variable being initialised separately using initial_value, a single expression is created which initialises all the variables in a module, and this initialiser expression is used to initialise a single dummy variable using initial_value. Note that, while this enables the variables within a single module to be initialised in the order in which they are defined, the order of initialisation between different modules is unspecified.

The implementation needs to keep a list of those variables with static storage duration which have been initialised so that it can call the destructors for these objects at the end of the program. This is done by declaring a variable of shape:

~cpp.destr.type : () -> SHAPE

for each such object with a non-trivial destructor. Each element of an array is considered a distinct object. Immediately after the variable has been initialised the token:

~cpp.destr.global : ( EXP pd, EXP POINTER c, EXP PROC ) -> EXP TOP

is called to add the variable to the list of objects to be destroyed. The first argument is the address of the dummy variable just declared, the second is the address of the object to be destroyed, and the third is the destructor to be used. In this way a list giving the objects to be destroyed, and the order in which to destroy them, is built up. Note that partially constructed objects are destroyed within their constructors (see §6.3) so that only completely constructed objects need to be considered.

The implementation also needs to ensure that it calls the destructors in this list at the end of the program, including calls of exit. This is done by calling the token:

~cpp.destr.init : () -> EXP TOP

at the start of each initial_value construct. In the default implementation this uses atexit to register a function, __TCPPLUS_term, which calls the destructors. To aid alternative implementations the token:

~cpp.start : () -> EXP TOP

is called at the start of the main function, however this has no effect in the default implementation.

10.5. The std namespace

Several classes declared in the std namespace arise naturally as part of the C++ language specification. These are as follows:

TypePurpose
std::type_infotype of typeid construct
std::bad_castthrown by dynamic_cast construct
std::bad_typeidthrown by typeid construct
std::bad_allocthrown by new construct
std::bad_exceptionused in exception specifications

The definitions of these classes are found, when needed, by looking up the appropriate class name in the std namespace. Depending on the context, an error may be reported if the class is not found. It is possible to modify the namespace which is searched for these classes using the directive:

#pragma TenDRA++ set std namespace : scope-name

where scope-name can be an identifier giving a namespace name or ::, indicating the global namespace.

10.6. Error catalogue

This section describes the error catalogue which lies at the heart of the C++ producer's error reporting routines. The full error catalogue syntax is documented under make_err A typical entry in the catalogue is as follows:

class_union_deriv ( CLASS_TYPE: ct )
{
    USAGE:              serious
    PROPERTIES:         ansi
    KEY (ISO)           "9.5"
    KEY (STANDARD)      "The union '"ct"' can't have base classes"
}

This defines an error, class_union_deriv, which takes a single parameter ct of type CLASS_TYPE. The severity of this error is serious; that is to say, a constraint error. The error property ansi indicates that the error arises from the ISO C++ standard, the associated ISO key indicating section 9.5. Finally the text to be printed for this error, including a reference to ct, is given. Looking up section 9.5 in the ISO C++ standard reveals the corresponding constraint in paragraph 1:

A union shall not have base classes.

Each constraint within the ISO C++ standard has a corresponding error in this way. The errors are named in a systematic fashion using the section names used in the draft standard. For example, section 9.5 is called class.union, so all the constraint errors arising from this section have names of the form class_union_*. These error names can be used in the tdfc2pragma low level directives such as:

#pragma TenDRA++ error "class_union_deriv" allow

to modify the error severity. The effect of reducing the severity of a constraint error in this way is undefined.

In addition to the obvious error severity levels, serious, warning and none, the error catalogue specifies a list of optional severity levels along with their default values. For example, the entry:

link_incompat = serious

sets up an option named link_incompat which is a constraint error by default. Errors with this severity, such as:

dcl_stc_external ( LONG_ID: id, PTR_LOC: loc )
{
    USAGE:              link_incompat
    PROPERTIES:         ansi
    KEY (ISO)           "7.1.1"
    KEY (STANDARD)      "'"id"' previously declared with external
             linkage (at "loc")"
}

are therefore constraint errors. The severity associated with link_incompat can be modified either directly, using the directive:

#pragma TenDRA++ option "link_incompat" allow

or indirectly using the directive:

#pragma TenDRA incompatible linkage allow

the effect being to modify the severity of the associated error messages.

The error catalogue is processed by a simple tool, make_err, which generates C code which is compiled into the C++ producer. Each error in the catalogue is assigned a number (there are currently 873 errors in the catalogue) which gives an index into an automatically generated table of error information. It is this error number, together with a list of error arguments, which forms the associated ERROR object. make_err generates a macro for each error in the catalogue which takes arguments of the appropriate types (which may be statically checked) and creates an ERROR object. For example, for the entry above this macro takes the form:

ERROR ERR_class_union_deriv ( CLASS_TYPE ) ;

These macros hide the error catalogue numbers from the rest of the C++ producer.

It is also possible to join a number of simple ERROR objects to form a single composite ERROR. The severity of the composite error is the maximum of the severities of the component errors. To this purpose a dummy error severity level whatever is introduced which is less severe than any other level. This is intended for use with error messages which are only ever used to add information to existing errors, and which inherit their severity level from the main error.

The text of a simple error message can be found in the table of error information. The text contains certain escape sequences indicating where the error arguments are to be printed. For example, %1 indicates the second argument. The error argument sorts - what is referred to as the error signature - is also stored in the table of error information as an array of characters, each corresponding to an ERR_KEY_type macro. The producer defines printing routines for each of the types given by these values, and calls the appropriate routine to print the argument.

There are several command-line options which can be used to modify the form in which the error message is printed. The default format is as follows:

"file.C", line 42: Error: [ISO 9.5]: The union 'U' can't have base classes.

The ISO section number can be suppressed using -m-s. The -mc option causes the source code line giving rise to the error to be printed as part of the message, with !!!! marking the position of the error within the line. The -me option causes the error name, class_union_deriv, to be printed as part of the message. The -ml option causes the full file location, including the list of #include directives used in reaching the file, to be printed. The -mt option causes typedef names to be used when printing types, rather than expanding to the type definition.