Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Directive (programming)

From Wikipedia, the free encyclopedia
Language construct that specifies how a compiler should process its input
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Directive" programming – news ·newspapers ·books ·scholar ·JSTOR
(December 2013) (Learn how and when to remove this message)

Incomputer programming, adirective orpragma (from "pragmatic") is alanguage construct that specifies how acompiler (or othertranslator) should process its input. Depending on theprogramming language, directives may or may not be part of thegrammar of the language and may vary from compiler to compiler. They can be processed by apreprocessor to specify compiler behavior, or function as a form of in-band parameterization.

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 programs, 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, but rather only a change in 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 termcommand.

The C preprocessor

[edit]
Main article:C preprocessor

InC andC++, the language supports a simplemacropreprocessor. Source lines that should be handled by the preprocessor, such as#define and#include are referred to aspreprocessor directives.

Syntactic constructs similar to C's preprocessor directives, such asC#'s#if, are also typically called "directives", although in these cases there may not be any real preprocessing phase involved.

All preprocessor commands, except fordefined (when following a conditional directive), begin with a hash symbol (#). UntilC++26, the keywordsexport,import andmodule were partially handled by the preprocessor.[1]

History

[edit]

Directives date toJOVIAL.[2]

COBOL has a COPY directive.

InALGOL 68, directives are known aspragmats (from "pragmatic"), and denotedpragmat orpr; in newer languages, notably C, this has been abbreviated to "pragma" (no 't').

A common use of pragmats in ALGOL 68 is in specifying astropping 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 thepragmat keyword itself (abbreviatedpr), either in the POINT or quote regimes:

.PRPOINT.PR.PRUPPER.PR.PRRES.PR'pr' quote 'pr'

Today directives are best known in the C language, of early 1970s vintage, and continued through the currentC99 standard, where they are either instructions to theC 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]
  • InAda, compiler directives are calledpragmas (short for "pragmatic information").
  • InCommon Lisp, directives are calleddeclarations, and are specified using thedeclare construct (alsoproclaim ordeclaim).[3] With one exception, declarations are optional, and do not affect the semantics of the program. The one exception isspecial, which must be specified where appropriate.
  • InTurbo Pascal, directives are calledsignificant comments, because in the languagegrammar they follow the same syntax ascomments. In Turbo Pascal, a significant comment is a comment whose first character is adollar sign and whose second character is a letter; for example, the equivalent of C's#include "file" directive is the significant comment{$I "file"}.
  • InPerl, thekeyword "use", which imports modules, can also be used to specify directives, such asuse strict; oruse utf8;.
  • Haskell pragmas are specified using a specialized comment syntax, e.g.{-# INLINE foo #-}.[4] It is also possible to use the C preprocessor in Haskell, by writing{-# LANGUAGE CPP #-}.
  • PHP uses the directivedeclare(strict_types=1).
  • InPL/I, directives begin with aPercent sign (%) and end with a semicolon (;), e.g.,%INCLUDEfoo;,%NOPRINT;,%PAGE;,%POP;,%SKIP;, the same as with preprocessor statements.
  • Python has two directives –from __future__ import feature (defined inPEP 236 -- Back to the __future__), which changes language features (and uses the existing module import syntax, as in Perl), and thecoding directive (in a comment) to specify the encoding of asource code file (defined inPEP 263 -- Defining Python Source Code Encodings). A more general directive statement was proposed and rejected inPEP 244 -- The `directive' statement; these all date to 2001.
  • ECMAScript also adopts theuse syntax for directives, with the difference that pragmas are declared as string literals (e.g."use strict";, or"use asm";), rather than a function call.
  • InVisual Basic, the keyword "Option" is used for directives:
    • Option Explicit On|Off - When on disallows implicit declaration of variables at first use requiring explicit declaration beforehand.
    • Option Compare Binary - Results in string comparisons based on a sort order derived from the internal binary representations of the characters - e.g. for the English/European code page (ANSI 1252) A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø. Affects intrinsic operators (e.g. =, <>, <, >), the Select Case block, and VBruntime library string functions (e.g. InStr).
    • Option Compare Text - Results in string comparisons based on a case-insensitive text sort order determined by your system's locale - e.g. for the English/European code page (ANSI 1252) (A=a) < (À = à) < (B=b) < (E=e) < (Ê = ê) < (Z=z) < (Ø = ø). Affects intrinsic operators (e.g. =, <>, <, >), the Select Case block, and VB runtime library string functions (e.g. InStr).
    • Option Strict On|Off - When on disallows:
      • typeless programming - where declarations which lack an explicit type are implicitly typed as Object.
      • late-binding (i.e.dynamic dispatch to CLR, DLR, and COM objects) on values statically typed as Object.
      • implicit narrowing conversions - requiring all conversions to narrower types (e.g. from Long to Integer, Object to String, Control to TextBox) be explicit in code using conversion operators (e.g. CInt, DirectCast, CType).
    • Option Infer On|Off - When on enables the compiler to infer the type of local variables from their initializers.
  • InRuby, interpreter directives are referred to aspragmas and are specified by top-of-file comments that follow akey: value notation. For example,coding: UTF-8 indicates that the file is encoded via theUTF-8character encoding.
  • InC#, compiler directives are called pre-processing directives. C# does not technically handle these using a preprocessor, but rather directly in the code. There are a number of different compiler directives, which mostly align with those from C and C++, including#pragma, which is specifically used to control compiler warnings and debugger checksums.[5][6] C# also features some directives not used in C or C++, including#nullable and#region. C# also does not allow function-like macros, but does allow regular macros, for purposes such as conditional compilation.
  • TheSQLiteDBMS includes a PRAGMA directive that is used to introduce commands that are not compatible with other DBMS.[7]
  • InSolidity, compiler directives are called pragmas, and are specified using the `pragma` keyword.[8]

Assembly language

[edit]
  • Inassembly language, directives, also referred to as pseudo-operations or "pseudo-ops", generally specify such information as the target machine, mark separations between code sections, define and change assembly-time variables, define macros, designate conditional and repeated code, define reserved memory areas, and so on. Some, but not all, assemblers use a specific syntax to differentiate pseudo-ops from instruction mnemonics, such as prefacing the pseudo-op with a period, such as the pseudo-op.END, which might direct the assembler to stop assembling code.

PL/SQL

[edit]

See also

[edit]

Footnotes

[edit]
  1. ^"P1857R1 - Modules Dependency Discovery".
  2. ^"Chapter 17 - Directives"(PDF).Computer Programming Manual for JOVIAL (J73) Language(PDF) (Technical report). June 1981. pp. 243–263. RADC-TR-81-143. RetrievedMay 28, 2023.
  3. ^Steele 1990,Chapter 9: Declarations, p. 215–237.
  4. ^"7.20. Pragmas".GHC 7.8.3 Documentation. Retrieved18 July 2014.
  5. ^dotnet-bot."Lexical structure - C# language specification".docs.microsoft.com. Retrieved2019-11-01.
  6. ^BillWagner."#pragma - C# Reference".docs.microsoft.com. Retrieved2019-11-01.
  7. ^"Pragma statements supported by SQLite".www.sqlite.org.
  8. ^"Layout of a Solidity Source File — Solidity 0.8.27 documentation".docs.soliditylang.org. Retrieved2024-06-03.
  9. ^Feuerstein, Steven; Pribyl, Bill (23 January 2014).Oracle PL/SQL Programming (6 ed.). O'Reilly Media, Inc. (published 2014).ISBN 9781449324414. Retrieved2016-06-16.PL/SQL has a PRAGMA keyword with the following syntax: PRAGMAinstruction_to_compiler; [...] PL/SQL offers several pragmas [...]

References

[edit]

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Directive_(programming)&oldid=1312718954"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp