Compiler Directives

From The Oxygene Language Wiki

Jump to:navigation, search

This is a Language topic
Feel free to add your notes to this topic below.



Overflow Checking

The $OVERFLOW or $Q directive controls how Overflow Checking will be applied to all following code.

Use of the DEFAULT setting will revert to the default overflow option configured by the project (usually OFF). This is useful for wrapping a section of code in a specific Overflow model, without affecting the rest of the source file, by wrapping it in {$OVERFLOW OFF}...{$OFERFLOW DEFAULT}


Controlling Compiler Messages

The $SHOW and $HIDE directives can be used to explicitly suppress or unsuppress a given compiler Hint or Warning. For example {$HIDE PW12, commonly found in auto-generated code from CodeDom, will stop the "Variable X might hide existing variable X" warning.

For obvious reasons, only Hints and Warnings can be hidden, not Errors. If the Treat Warnings as Errors option is enabled, warnings that are hidden by use of $HIDE will not be handled as errors.

The long version, $SHOWMESSAGE and $HIDEMESSAGE, are provided for backward compatibility and work identically to $SHOW and $HIDE.

Emiting Your Own Compiler Messages

In addition to controlling what compiler messages are emitted, Delphi Prism also enables you to emit custom error and warning messages from your code, using the following four directives. This is useful, for example, to mark conditionally compiled code that has not been implemented for all conditional cases, such as {$IFDEF COMPACTFRAMEWORK}{$ERROR Not Implemented for the CF yet.}{$ENDIF}.

No quotes are required around the message text, but if a set of matching quotes is found, it will be stripped from the emitted message, for backward compatibility.

The first three directives are provided to emit messages corresponding to compiler Errors, Warnings and Hints. Similar to compiler-generated messages, Errors (and, if the Treat Warnings as Errors option is enabled, warnings) will fail the compile process, while the others are purely informational messages.

When using these directives, it is recommended to pick the appropriate type that matches the severity of the message, with Errors representing an unrecoverable problem, Warnings indicating a likely unexpected misbehavior and Hints merely providing optimization-type info that does not affect the code's execution.


Conditional Compilation

Defines a new symbol for the pre-processor; the defines are position dependent, so it will only be defined for everything after the {$DEFINE symbol} directive.

Undefines a symbol for the pre-processor; the defines are position dependent, so it will only be defined for everything after the {$DEFINE symbol} directive. Undefining can remove any preprocessor symbol, even ones defined by the compiler itself or the project options. When a symbol doesn't exist, the undefine will be ignored.

Checks if a define is set. Accepts only 1 parameter, which is the symbol to check. Everything up till the matching ELSE or ENDIF will be ignored, or processed (depending on the presence of the define).

Introduced for backward compatibility; everything after this, until the matching ELSE or ENDIF, is ignored.

Checks if a define is NOT set. Accepts 1 parameter like IFDEF, but reverses the condition.

Ends a matching IFDEF/IFOPT/IFNDEF or ELSE.

Reverses the processing of the the matching IFDEF/IFOPT/IFNDEF.

Within an ignored block all defines, except IFDEF, IFOPT, IFNDEF, ENDIF and ELSE are ignored (previous ones have to be processed to count the number of nested IFDEF blocks to ignore).

begin
  {$IFDEF TRIAL}
  Console.WriteLine('This a trial version!');
  {$ELSE}
  Console.WriteLine('This is the full version');
  {$ENDIF}

Depending on the presence of the TRIAL define, the first or second writeline will be emitted into code. Defines are case insensitive.

Conditional Defines


Other Directives

The $INCLUDE (or $I) directive includes the content of the specified file, as if it were written inline at the position of the directive. The file specified in the $INCLUDE directive needs to have a .inc extension, and be included in the project (the compiler will not compile .inc files in the project directly).


The $LINE directive is provided for use by auto-generated code, such as from ASP.NET, and should not normally be used in hand-written code. The directive can be used to map compiler messages such as warnings and errors to different files and line numbers. For example, in ASP.NET it is used to have errors that occur in Delphi Prism source files auto-generated behind the scenes reflected in the appropriate lines of the .ASPX file instead.


The $PARAMETER directive can be used to inject strings and code elements into source code, from the command line build. This is helpful, for example, to insert specific assembly version numbers into strings used in design-time attributes.

For example, you could call the command line compiler using oxygene.exe -params:Version=1.0.3.389 and use the following in your code [SomeDesignTimeAttribute('DesignerType, Designer.Assembly, Version='+{$PARAMETER Version}+'...')].

$DELPHICOMPATIBILITY

see DELPHICOMPATIBILITY flag.

Ignored Directives

While unknown directives found in code will generate compiler errors, the following directives will be actively ignored by the compiler:

#EXTERNALSOURCE

The {#external source} directive is used by the IDE code generation to read back from where a line originally came from. It is generally paired with the {$LINE} compiler directive. The comments are created by the IDE form designer and ASP.NET code and should never be written manually.


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox