Difference between revisions of "Compiler Mode"

From Free Pascal wiki
Jump to navigationJump to search
(Add a list format and summary.)
(→‎modes: versions)
 
(17 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
{{Compiler Mode}}
 
{{Compiler Mode}}
  
The Free Pascal compiler will compile in a specific mode. Each mode dictates what syntax the compiler accepts as valid, and what it considers as invalid. The compiler mode can be set in the source code with the {$mode } compiler directive, or on the command line with the -M option.  
+
The [[FPC]] intends to be (in part) a free and open source alternative to commercial [[Pascal]] compilers.
 +
In order to achieve this a compiler switch determining the ''compiler compatibility mode'' has been introduced.
  
*Free Pascal: {$MODE '''FPC'''}<br/>This is the original Free Pascal compiler mode. It is the default mode of the compiler, so it is not necessary to explicitly add this directive.
+
Every mode implicitly enables or disables certain syntax requirements or other language constructs.
 +
Some of these can be enabled or disabled on an individual basis by using so called ''mode switches'', see below.
  
*Extended Free Pascal: {$MODE '''OBJFPC'''} <br/>This mode adds extra functionality to the FPC mode, including classes, interfaces and exceptions.
+
== modes ==
 +
A compiler compatibility mode can be specified in [[Source code|source code]] via the [[global compiler directives|global compiler directive]] <syntaxhighlight lang="pascal" inline>{$mode}</syntaxhighlight> or via the command line or <syntaxhighlight lang="text" inline>fpc.cfg(5)</syntaxhighlight> parameter <syntaxhighlight lang="bash" inline>-M</syntaxhighlight>.
  
*Turbo Pascal: {$MODE '''TP'''} <br/>This is the Turbo Pascal compatibility mode.
+
The following nine compiler compatibility modes are recognized:
 +
; Free Pascal ([[Mode FPC|<syntaxhighlight lang="pascal" inline>{$mode FPC}</syntaxhighlight>]], <syntaxhighlight lang="bash" inline>-MFPC</syntaxhighlight>)
 +
: This is the original FPC mode. As of FPC 3.x this is the ''default mode'' if neither the source code or the command line specifies a compiler compatibility mode explicitly.
 +
; Object Pascal ([[Mode ObjFPC|<syntaxhighlight lang="pascal" inline>{$mode objFPC}</syntaxhighlight>]], <syntaxhighlight lang="bash" inline>-MobjFPC</syntaxhighlight> or <syntaxhighlight lang="bash" inline>-S2</syntaxhighlight>)
 +
: This mode adds extra functionality to the <syntaxhighlight lang="text" inline>FPC</syntaxhighlight> mode, including but not limited to [[Class|classes]], [[Interface|interfaces]] and [[Exceptions|exceptions]].
 +
; [[Turbo Pascal]] ([[Mode TP|<syntaxhighlight lang="pascal" inline>{$mode TP}</syntaxhighlight>]], <syntaxhighlight lang="bash" inline>-MTP</syntaxhighlight> or <syntaxhighlight lang="bash" inline>-So</syntaxhighlight>)
 +
: This is the Turbo Pascal compatibility mode. It tries to be compatible to Borland TP 7.0, e. g. by disable function overloading.
 +
; [[Delphi]] ([[Mode Delphi|<syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight>]], <syntaxhighlight lang="bash" inline>-Mdelphi</syntaxhighlight> or <syntaxhighlight lang="bash" inline>-Sd</syntaxhighlight>)
 +
: This is the Delphi compatibility mode.
 +
; Delphi with Unicode (<syntaxhighlight lang="pascal" inline>{$mode DelphiUnicode}</syntaxhighlight>, <syntaxhighlight lang="bash" inline>-MdelphiUnicode</syntaxhighlight>) [<nowiki/>[[FPC New Features 3.0#New delphiunicode syntax mode|since FPC 3.0.0]]]
 +
: Like <syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight> but with <syntaxhighlight lang="delphi" inline>unicodeString</syntaxhighlight> as default <syntaxhighlight lang="delphi" inline>string</syntaxhighlight> type.
 +
; [[Mac Pascal]] ([[Mode MacPas|<syntaxhighlight lang="pascal" inline>{$mode MacPas}</syntaxhighlight>]], <syntaxhighlight lang="bash" inline>-MmacPas</syntaxhighlight>) [since FPC 1.9.0]
 +
: The Mac Pascal compatibility mode.
 +
; [[GNU Pascal]] ([[Mode GPC|<syntaxhighlight lang="pascal" inline>{$mode GPC}</syntaxhighlight>]], <syntaxhighlight lang="bash" inline>-MGPC</syntaxhighlight> or <syntaxhighlight lang="bash" inline>-Sp</syntaxhighlight>) [removed since FPC 2.2.0]
 +
: The GNU Pascal compatibility mode.
 +
; ISO 7185 [[Standard Pascal]] ([[Mode iso|<syntaxhighlight lang="pascal" inline>{$mode ISO}</syntaxhighlight>]], <syntaxhighlight lang="bash" inline>-MISO</syntaxhighlight>) [<nowiki/>[[FPC New Features 2.6.0#Basic ISO Standard Pascal support|since FPC 2.6.0]]]
 +
: The ISO 7185 compliant compatibility mode.
 +
; [[Extended Pascal]] ([[Mode extendedpascal|<syntaxhighlight lang="pascal" inline>{$mode extendedPascal}</syntaxhighlight>]]) [since FPC 3.2]
 +
: This is the extended Pascal mode. It tries to be as ISO 10206 compliant as possible.
  
*Delphi:  {$MODE '''DELPHI'''} <br/>This is the Delphi compatibility mode.  
+
Furthermore the special mode <syntaxhighlight lang="text" inline>default</syntaxhighlight> reverts any specifications of a compiler compatibility mode.
  
*Mac Pascal: {$ MODE '''MacPAS'''}<br/>The Mac Pascal compatibility mode.
+
Since the specifications of compiler compatibility mode implicitly imposes rigorous changes and possibly implies inclusion of other modules, it is imperative to specify the directives prior any other.
 +
 
 +
== mode switch ==
 +
Since FPC 2.3.1 the global compiler directive [[modeswitches|<syntaxhighlight lang="pascal" inline>{$modeSwitch}</syntaxhighlight>]] allows a selective selection of ''some'' features, despite the chosen mode.
 +
 
 +
A mode switch has to appear after any mode selections, otherwise the mode switches will be overwritten.
 +
<syntaxhighlight lang="pascal">// omit @-address-operator while assigning to procedural variables, despite FPC mode
 +
{$mode FPC}
 +
{$modeSwitch classicProcVars+}</syntaxhighlight>
 +
 
 +
== see also ==
 +
* [https://www.freepascal.org/docs-html/user/userse33.html § “compiler modes” in the ''Free Pascal User’s manual'']

Latest revision as of 13:13, 21 October 2019

Deutsch (de) English (en) español (es) suomi (fi) français (fr)

The FPC intends to be (in part) a free and open source alternative to commercial Pascal compilers. In order to achieve this a compiler switch determining the compiler compatibility mode has been introduced.

Every mode implicitly enables or disables certain syntax requirements or other language constructs. Some of these can be enabled or disabled on an individual basis by using so called mode switches, see below.

modes

A compiler compatibility mode can be specified in source code via the global compiler directive {$mode} or via the command line or fpc.cfg(5) parameter -M.

The following nine compiler compatibility modes are recognized:

Free Pascal ({$mode FPC}, -MFPC)
This is the original FPC mode. As of FPC 3.x this is the default mode if neither the source code or the command line specifies a compiler compatibility mode explicitly.
Object Pascal ({$mode objFPC}, -MobjFPC or -S2)
This mode adds extra functionality to the FPC mode, including but not limited to classes, interfaces and exceptions.
Turbo Pascal ({$mode TP}, -MTP or -So)
This is the Turbo Pascal compatibility mode. It tries to be compatible to Borland TP 7.0, e. g. by disable function overloading.
Delphi ({$mode Delphi}, -Mdelphi or -Sd)
This is the Delphi compatibility mode.
Delphi with Unicode ({$mode DelphiUnicode}, -MdelphiUnicode) [since FPC 3.0.0]
Like {$mode Delphi} but with unicodeString as default string type.
Mac Pascal ({$mode MacPas}, -MmacPas) [since FPC 1.9.0]
The Mac Pascal compatibility mode.
GNU Pascal ({$mode GPC}, -MGPC or -Sp) [removed since FPC 2.2.0]
The GNU Pascal compatibility mode.
ISO 7185 Standard Pascal ({$mode ISO}, -MISO) [since FPC 2.6.0]
The ISO 7185 compliant compatibility mode.
Extended Pascal ({$mode extendedPascal}) [since FPC 3.2]
This is the extended Pascal mode. It tries to be as ISO 10206 compliant as possible.

Furthermore the special mode default reverts any specifications of a compiler compatibility mode.

Since the specifications of compiler compatibility mode implicitly imposes rigorous changes and possibly implies inclusion of other modules, it is imperative to specify the directives prior any other.

mode switch

Since FPC 2.3.1 the global compiler directive {$modeSwitch} allows a selective selection of some features, despite the chosen mode.

A mode switch has to appear after any mode selections, otherwise the mode switches will be overwritten.

// omit @-address-operator while assigning to procedural variables, despite FPC mode
{$mode FPC}
{$modeSwitch classicProcVars+}

see also