Compiler Directives

Compiler Directives are annotations in code that are not part of the actual code flow, but influence the behavior of the compiler, for example to control options or perform Conditional Compilation.

In Oxygene, compiler directives are enclosed in curly braces (like comments) and start with a dollar symbol, for example "{$HIDE ...}". They can occur anywhere in code. In C# and Swift, directives start with the hash symbol (for example "#hide ...") and must be in a fresh line, preceded only by whitespace. Case sensitivity for directives follows that of the language (case sensitive for C# and Swift, not case sensitive – but uppercase by convention – for Oxygene).

Defines and Conditional Compilation

The following directives allow you to set, unset and check for Defines for the purpose of Conditional Compilation.

  • {$DEFINE x} – sets a Conditional Define
  • {$UNDFINE x} or {$UNDEF x} – un-sets a Conditional Define
  • {$IF expr} or {$IFDEF expr} – checks for a single define or a boolean expression combining multiple defines with AND, OR and NOT. See Conditional Compilation for details.
  • {$IFNDEF x} – reverse of "{$IF ...}"
  • {$IFOPT x} – provided for Delphi compatibility, always acts like an undefined "{$IF FALSE}"
  • {$ENDIF} – closes off an "{$IF ...}" section
  • {$ELSE} – closes off an "{$IF ...}" section and starts a new section that will compile only if the "{$IF ...}" section was false
  • {$ELSEIF x} – combines "{$ELSE}" with a new "{$IF ...}" section
  • #define – sets a Conditional Define
  • #undef – un-sets a Conditional Define
  • #if or #ifdef – checks for a single define or a boolean expression combining multiple defines with &&, || and !. See Conditional Compilation for details.
  • #ifndef – reverse of "#if..."
  • #endif – closes off an "#if ..." section
  • #else – closes off an "#if ..." section and starts a new section that will compile only if the "#if ..." section was false
  • #elif – combines "#else" with a new "#if ...}" section
  • #define – sets a Conditional Define
  • #undef – un-sets a Conditional Define
  • #if or ifdef – checks for a single define or a boolean expression combining multiple defines with &&, || and !. See Conditional Compilation for details. Also supports the special os() and arch() macro defines.
  • #ifndef – reverse of "#if..."
  • #endif – closes off an "#if ..." section
  • #else – closes off an "#if ..." section and starts a new section that will compile only if the "#if ..." section was * #elseif – combines "#else" with a new "#if ...}" section

Messages

The directives below allow you to control or emit custom error and warning messages:

  • {$HINT msg} – emits a custom hint message
  • {$WARNING msg} – emits a custom warning message
  • {$ERROR msg} – emits a custom error message and fails the build
  • {$MESSAGE msg} – emits a custom message
  • {$HIDE N123} – disables the given warning or hit message for subsequent code
  • {$SHOW N123} – re-enables the given warning or hit message for subsequent code
  • {$HIDEMESSAGE N123} – same as above
  • {$SHOWMESSAGE N123} – same as above
  • #hint msg – emits a custom hint message
  • #warning msg – emits a custom warning message
  • #error msg – emits a custom error message and fails the build
  • #message msg – emits a custom message
  • #hint msg – emits a custom hint message
  • #warning msg – emits a custom warning message
  • #error msg – emits a custom error message and fails the build
  • #message msg – emits a custom message

ASP.NET support

The "Line" directive can be used to influence the line number information the compiler emits for error and warning messages. You will generally not use this in your own code, but ASP.NET (and other code-generating frameworks) may use this to map errors originating from auto-generated code back to source files the developer is aware of.

  • {$LINE n} – all subsequent messages will be reported relative to the line number provided
  • {$LINE HIDDEN} – all subsequent messages will be hidden
  • {$LINE DEFAULT} – all subsequent messages will be reported with their actual location again
  • #line n – all subsequent messages will be reported relative to the line number provided
  • #line hidden – all subsequent messages will be hidden
  • #line default – all subsequent messages will be reported with their actual location again
  • #line n – all subsequent messages will be reported relative to the line number provided
  • #line hidden – all subsequent messages will be hidden
  • #line default – all subsequent messages will be reported with their actual location again

Ignored Directives

The following directives are ignored by the compiler, either for compatibility reasons or (in case of the "Region" directives) because they are interpreted by the IDE only.

  • {$R+/-} or {$RANGE ON/OFF/DEFAULt} – (controls range checking in Delphi)
  • {$REGION} and ** {$ENDREGION}** – used by the IDE for code folding
  • {$HPPEMIT}
  • {$EXTERNALSYM}
  • #region and #endregion – used by the IDE for code folding
  • #hppemit
  • #externalsym
  • #region and #endregion – used by the IDE for code folding
  • #hppemit
  • #externalsym

Cross-Platform Compatibility Mode

Cross-Platform warnings, hints and compiler behavior can be toggled via the {$CROSSPLATFORM} (Oxygene) and #pragma crossplatform (C# and Swift) directive. C# and Swift will ignore any other/unknown #pragma directives.

  • {$CROSSPLATFORM ON} – turn on Cross-Platform Compatibility for subsequent code
  • {$CROSSPLATFORM OFF} – turn off Cross-Platform Compatibility for subsequent code
  • {$CROSSPLATFORM DEFAULT} – use the project default for Cross-Platform Compatibility for subsequent code
  • #pragma crossplatform on – turn on Cross-Platform Compatibility for subsequent code
  • #pragma crossplatform off – turn off Cross-Platform Compatibility for subsequent code
  • #pragma crossplatform default – use the project default for Cross-Platform Compatibility for subsequent code
  • #pragma crossplatform on – turn on Cross-Platform Compatibility for subsequent code
  • #pragma crossplatform off – turn off Cross-Platform Compatibility for subsequent code
  • #pragma crossplatform default – use the project default for Cross-Platform Compatibility for subsequent code

Additional Directives (Oxygene Only)

These directives are supported for Oxygene only:

  • {$Q+/-} or {$OVERFLOW ON/OFF/DEFAULT} – turn overflow checking on or off
  • {$G+/-} or {$GLOBALS ON/OFF/DEFAULT} – turn support for Globals on or off
  • {$TAILCALLS ON/OFF/DEFAULT} – enables or disables compiler optimizations for Tail Calls
  • {$INCLUDE file.inc} or {$I file.inc} – includes code from the specified .inc file in the current location. Deprecated, provided for compatibility only.
  • {$PARAMETER X} – allows parameters passed to the command line compiler to be used as a string literal. Deprecated.
  • {$DELPHI ON/OFF/DEFAULT} – enables or disables Delphi Compatibility Mode
  • {$DELPHICOMPATIBILITY ON/OFF/DEFAULT} – same as above