2.8 Pragmas
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
A pragma is a compiler directive. There are language-defined pragmas that give instructions for optimization, listing control, etc. An implementation may support additional (implementation-defined) pragmas.
Language Design Principles
pragma
s are treated as unrecognized pragma
s, the program should remain both syntactically and semantically legal. There are a few exceptions to this general principle (for example, pragma
Import can eliminate the need for a completion), but the principle remains, and is strictly true at the syntactic level. Certainly any implementation-defined pragma
s should obey this principle both syntactically and semantically, so that if the pragma
s are not recognized by some other implementation, the program will remain legal. Syntax
2pragma
::=
pragma identifier
[(pragma_argument_association
{, pragma_argument_association
})];
3/3pragma_argument_association
::=
[pragma_argument_identifier
=>] name
| [pragma_argument_identifier
=>] expression
| pragma_argument_aspect_mark
=> name
| pragma_argument_aspect_mark
=> expression
4/3In a pragma
, any pragma_argument_association
s without a pragma_argument_identifier
or pragma_argument_aspect_mark
shall precede any associations with a pragma_argument_identifier
or pragma_argument_aspect_mark
.
Pragma
s are only allowed at the following places in a program:
- After a semicolon delimiter, but not within a
formal_part
,discriminant_part
, ordeclare_expression
. 7/3 - At any place where the syntax rules allow a construct defined by a syntactic category whose name ends with “
declaration
”, “item
”, “statement
”, “clause
”, or “alternative
”, or one of the syntactic categoriesvariant
orexception_handler
; but not in place of such a construct if the construct is required, or is part of a list that is required to have at least one such construct. 7.1/3 - In place of a
statement
in asequence_of_statements
. 7.2/3 - At any place where a
compilation_unit
is allowed.
Additional syntax rules and placement restrictions exist for specific pragmas.
pragma
is not used in any BNF syntax rule. The name of a pragma
is the identifier following the reserved word pragma. The name
or expression
of a pragma_argument_association
is a pragma argument.
An identifier specific to a pragma is an identifier or reserved word that is used in a pragma argument with special meaning for that pragma.
identifier
" as an argument of the pragma
, that identifier
is an identifier specific to that pragma.identifier
(the reserved word(s) are not shown). For example, the restriction No_Use_Of_Attribute (see 13.12.1) allows the reserved words which can be attribute designators, but the syntax for a restriction does not include these reserved words. Static Semantics
11If an implementation does not recognize the name of a pragma
, then it has no effect on the semantics of the program. Inside such a pragma
, the only rules that apply are the Syntax Rules.
pragma
does not cause freezing, even though the rules in 13.14, “Freezing Rules” say it does; the above rule overrules those other rules. On the other hand, an expression in a recognized pragma
causes freezing, even if this makes something illegal.pragma
.pragma
altogether. The interpretation of constructs appearing inside implementation-defined pragma
s is implementation defined. For example: “pragma Mumble(X);”. If the current implementation has never heard of Mumble, then it doesn't know whether X is a name, an expression, or an identifier specific to the pragma Mumble. Dynamic Semantics
12Any pragma
that appears at the place of an executable construct is executed. Unless otherwise specified for a particular pragma, this execution consists of the evaluation of each evaluable pragma argument in an arbitrary order.
pragma
that appears at the place of an elaborable construct, execution is elaboration.name
nor an expression
— such identifiers are not evaluated (unless an implementation defines them to be evaluated in the case of an implementation-defined pragma
).pragma
can contain an expression that is not evaluated. Note that pragma
s in type_definition
s may contain expressions that depend on discriminants.Implementation Requirements
13The implementation shall give a warning message for an unrecognized pragma name.
pragma
is a compile-time error. Implementation Permissions
14An implementation may provide implementation-defined pragmas; the name of an implementation-defined pragma shall differ from those of the language-defined pragmas.
An implementation may ignore an unrecognized pragma even if it violates some of the Syntax Rules, if detecting the syntax error is too complex.
Implementation Advice
16/3Normally, implementation-defined pragmas should have no semantic effect for error-free programs; that is, if the implementation-defined pragmas in a working program are replaced with unrecognized pragmas, the program should still be legal, and should still have the same semantics.
pragma
to a program might cause an error (either at compile time or at run time). On the other hand, if the language-specified semantics for a feature are in part implementation defined, it makes sense to support pragmas that control the feature, and that have real semantics; thus, this paragraph is merely a recommendation. Normally, an implementation should not define pragmas that can make an illegal program legal, except as follows:
- A
pragma
used to complete a declaration;
- A
pragma
used to configure the environment by adding, removing, or replacinglibrary_item
s.
library_item
s in an environment.pragma
s for upward compatibility reasons, even though all of these pragma
s can make an illegal program legal. (The latter three can affect legality in a rather subtle way: They affect the value of named numbers in System, and can therefore affect the legality in cases where static expressions are required.)Incompatibilities With Ada 83
Extensions to Ada 83
Wording Changes from Ada 83
Extensions to Ada 2005
pragma
s in place of a statement
, even if there are no other statement
s in a sequence_of_statements
.aspect_mark
s; this allows identifier
'Class in this context. As usual, this is only allowed if specifically allowed by a particular pragma. Wording Changes from Ada 2005
Wording Changes from Ada 2012
pragma
s are not allowed in declare_expression
s. We don't allow this as the definition of most pragmas assume that they're given between entities. We also don't want to answer questions about conformance of pragma
s, especially unrecognized pragma
s and pragma
s whose state can change between the specification and body. Syntax
20The forms of List, Page, and Optimize pragma
s are as follows:
pragma List(identifier
);
pragma Page;
pragma Optimize(identifier
);
[Other pragmas are defined throughout this Reference Manual, and are summarized in Annex L.]
Static Semantics
25A pragma
List takes one of the identifier
s On or Off as the single argument. This pragma is allowed anywhere a pragma
is allowed. It specifies that listing of the compilation is to be continued or suspended until a List pragma
with the opposite argument is given within the same compilation. The pragma
itself is always listed if the compiler is producing a listing.
A pragma
Page is allowed anywhere a pragma
is allowed. It specifies that the program text which follows the pragma
should start on a new page (if the compiler is currently producing a listing).
A pragma
Optimize takes one of the identifier
s Time, Space, or Off as the single argument. This pragma
is allowed anywhere a pragma
is allowed, and it applies until the end of the immediately enclosing declarative region, or for a pragma
at the place of a compilation_unit
, to the end of the compilation
. It gives advice to the implementation as to whether time or space is the primary optimization criterion, or that optional optimizations should be turned off. [It is implementation defined how this advice is followed.]
Examples
28Examples of pragmas:
pragma List(Off); -- turn off listing generation
pragma Optimize(Off); -- turn off optional optimizations
pragma Assertion_Policy(Check); -- check assertions
pragma Assert(Exists(File_Name),
Message => "Nonexistent file"); -- assert file exists
Extensions to Ada 83
pragma
now allows the identifier Off to request that normal optimization be turned off.Wording Changes from Ada 83
Wording Changes from Ada 95
pragma
Suppress is obsolescent.