Directive (programming)
| This article does not cite any references or sources. (December 2009) |
In computer programming, a directive or pragma (from "pragmatic") is a language construct that specifies how a compiler (or assembler or interpreter) should process its input. Directives are not part of the language proper – they are not part of the grammar, and may vary from compiler to compiler – but instead function either as an in-band form of a command-line option, specifying compiler behavior, or are processed by a preprocessor. In some cases directives specify global behavior, while in other cases they only affect a local section, such as a block of programming code. In some cases, such as some C pragmas, directives are optional compiler hints, and may be ignored, but normally they are prescriptive, and must be followed. However, a directive does not perform any action in the language itself, only change the behavior of the compiler.
This term could be used to refer to proprietary third party tags and commands (or markup) embedded in code that result in additional executable processing that extend the existing compiler, assembler and language constructs present in the development environment. The term "directive" is also applied in a variety of ways that are similar to the term command.
Contents |
Assembly language [edit]
In assembly language, directives generally specify such information as the target platform, mark the separations between sections, and so on. For instance, the commonly seen directive "ALIGN", which inserts in the current section as many bytes as needed to preserve word-alignment, is generally referred to as a "directive", despite the fact that it does correspond to a particular construct in the generated code.
The C preprocessor [edit]
In C and C++, the language supports a simple macro preprocessor. Source lines that should be handled by the preprocessor, such as #define and #include are referred to as preprocessor directives.
Another C construct, the #pragma directive, is used to instruct the compiler to use pragmatic or implementation-dependent features. Two notable users of this directive are OpenMP and OpenACC.
Syntactic constructs similar to C's preprocessor directives, such as C#'s #if, are also typically called "directives", although in these cases there may not be any real preprocessing phase involved.
History [edit]
Directives date to ALGOL 68, where they are know as pragmats (from "pragmatic"), and denoted pragmat or pr; in newer languages, notably C, this has been abbreviated to "pragma" (no 't').
A common use of pragmats in ALGOL 68 is in specifying a stropping regime, meaning "how keywords are indicated". Various such directives follow, specifying the POINT, UPPER, RES (reserved), or quote regimes. Note the use of stropping for the pragmat keyword itself (abbreviated pr), either in the POINT or quote regimes:
.PR POINT .PR .PR UPPER .PR .PR RES .PR 'pr' quote 'pr'
Today directives are best-known in the C language, of early 1970s vintage, and continued through the current C99 standard, where they are either instructions to the C preprocessor, or, in the form of #pragma, directives to the compiler itself. They are also used to some degree in more modern languages; see below.
Other languages [edit]
In Ada, compiler directives are called pragmas (short for "pragmatic information").
In Turbo Pascal, directives are called significant comments, because in the language grammar they follow the same syntax as comments. In Turbo Pascal, a significant comment is a comment whose first character is a dollar sign and whose second character is a letter; for example, the equivalent of C's #include "file" directive is the significant comment {$I "file"}.
In Perl, the keyword "use", which imports modules, can also be used to specify directives, such as use strict; or use utf8;.
Python has two directives – from __future__ import feature (defined in PEP 236 -- Back to the __future__), which changes language features (and uses the existing module import syntax, as in Perl), and the coding directive (in a comment) to specify the encoding of a source code file (defined in PEP 263 -- Defining Python Source Code Encodings). A more general directive statement was proposed an rejected in PEP 244 -- The `directive' statement; these all date to 2001.
ECMAScript also uses "use" for directives.
In Visual Basic, the keyword "Option" is used for directives. Most common is "Option Explicit On", which instructs the VB compiler to require all variable declarations before use. Other directives include:
Option Strict On- Requires that implicit type casts are only to wider types.Option Compare Binary- Tells the compiler to compare text using a binary algorithm.Option Compare Text- Tells the compiler to compare text using a textual algorithm.