D Programming Language 2.0

Last update Sun Dec 30 20:21:54 2012


    pragma ( Identifier )
    pragma ( Identifier , ArgumentList )

Pragmas are a way to pass special information to the compiler and to add vendor specific extensions to D. Pragmas can be used by themselves terminated with a ‘;’, they can influence a statement, a block of statements, a declaration, or a block of declarations.

Pragmas can appear as either declarations, Pragma DeclarationBlock, or as statements, PragmaStatement.

pragma(ident);        // just by itself

pragma(ident) declaration; // influence one declaration

pragma(ident): // influence subsequent declarations

pragma(ident) { // influence block of declarations

pragma(ident) statement; // influence one statement

pragma(ident) { // influence block of statements

The kind of pragma it is is determined by the Identifier. ExpressionList is a comma-separated list of AssignExpressions. The AssignExpressions must be parsable as expressions, but what they mean semantically is up to the individual pragma semantics.

Predefined Pragmas

All implementations must support these, even if by just ignoring them:

Constructs a message from the arguments and prints to the standard error stream while compiling:
pragma(msg, "compiling...", 1, 1.0);
Inserts a directive in the object file to link in the library specified by the AssignExpression. The AssignExpressions must be a string literal:
pragma(lib, "foo.lib");
Puts a directive into the object file saying that the function specified in the first argument will be the start address for the program:
void foo() { ... }
pragma(startaddress, foo);
This is not normally used for application level programming, but is for specialized systems work. For applications code, the start address is taken care of by the runtime library.

Vendor Specific Pragmas

Vendor specific pragma Identifiers can be defined if they are prefixed by the vendor's trademarked name, in a similar manner to version identifiers:

pragma(DigitalMars_funky_extension) { ... }

Compilers must diagnose an error for unrecognized Pragmas, even if they are vendor specific ones. This implies that vendor specific pragmas should be wrapped in version statements:

version (DigitalMars)
    { ... }

Forums | Comments |  D  | Search | Downloads | Home