Difference between revisions of "Compiler Mode"

From Lazarus wiki
Jump to navigationJump to search
(refactor)
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|source code]] with the {$mode }
+
The [[FPC]] intends to be (in part) a free and open source alternative to commercial [[Pascal]] compilers.
[[Compiler directive|compiler directive]], or on the [[Command-line interface|command line]] with the -M [http://www.freepascal.org/docs-html/user/userap1.html command line option].  
+
In order to achieve this a compiler switch determining the ''compiler compatibility mode'' has been introduced.
  
== Modes ==
+
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.
  
*Free Pascal: [[Mode FPC|<syntaxhighlight lang="pascal" enclose="none"> {$mode FPC} </syntaxhighlight>]] <br/>This is the original Free Pascal compiler mode. As of version 3, It is the ''default mode'' of the compiler, so it is not necessary to explicitly add this directive.
+
== 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>.
  
*Extended Free Pascal: [[Mode ObjFPC|<syntaxhighlight lang="pascal" enclose="none"> {$mode OBJFPC} </syntaxhighlight>]]<br/>This mode adds extra functionality to the '''FPC''' mode, including [[Class | classes]], [[Interface|interfaces]] and [[Exceptions|exceptions]].
+
The following eight 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.
 +
; [[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>) [suspended since FPC 2.4.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>]]) [trunk only]
 +
: This will be the extended Pascal mode in a future release.
  
*Turbo Pascal: [[Mode TP|<syntaxhighlight lang="pascal" enclose="none"> {$mode TP} </syntaxhighlight>]] <br/>This is the [[Turbo Pascal]] compatibility mode.
+
Furthermore the special mode <syntaxhighlight lang="text" inline>default</syntaxhighlight> reverts any specifications of a compiler compatibility mode.
  
*Delphi:  [[Mode Delphi|<syntaxhighlight lang="pascal" enclose="none"> {$mode DELPHI} </syntaxhighlight>]] <br/>This is the [[Delphi]] 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.
  
*Mac Pascal: [[Mode MacPas|<syntaxhighlight lang="pascal" enclose="none"> {$mode MacPAS} </syntaxhighlight>]]<br/>The [[Mac Pascal]] compatibility mode.
+
== 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.
  
*ISO 7185 Standard Pascal: [[Mode iso|<syntaxhighlight lang="pascal" enclose="none"> {$mode ISO} </syntaxhighlight>]]<br/>The ISO 7185 standard compatibility mode. The ISO 7185 standard is also known as [[Standard Pascal]].
+
A mode switch has to appear after any mode selections, otherwise the mode switches will be overwritten.
 
+
<syntaxhighlight lang="pascal">// allow exceptions, despite the normal FPC mode:
== ModeSwitch ==
 
As of version 2.3.1 of Free Pascal, the {$ModeSwitch}  compiler directive has been added to allow ''features'' of a compiler mode to be selectively added to the current mode, effectively creating a ''custom mode''.
 
For example:
 
 
 
<syntaxhighlight lang="pascal">
 
 
{$mode FPC}
 
{$mode FPC}
{$ModeSwitch EXCEPTIONS}
+
{$modeSwitch exceptions+}</syntaxhighlight>
</syntaxhighlight>
 
 
 
Will add exception handling to the FPC compiler mode.
 
  
==See also ==
+
== see also ==
* [http://www.freepascal.org/docs-html/user/userse33.html Compiler modes]
+
* [https://www.freepascal.org/docs-html/user/userse33.html § “compiler modes” in the ''Free Pascal User’s manual'']

Revision as of 15:12, 20 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 eight 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.
Mac Pascal ({$mode MacPas}, -MmacPas) [since FPC 1.9.0]
The Mac Pascal compatibility mode.
GNU Pascal ({$mode GPC}, -MGPC or -Sp) [suspended since FPC 2.4.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}) [trunk only]
This will be the extended Pascal mode in a future release.

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.

// allow exceptions, despite the normal FPC mode:
{$mode FPC}
{$modeSwitch exceptions+}

see also