Before the Pike code is sent to the compiler it is fed through the
preprocessor. The preprocessor converts the source code from its
character encoding into the Pike internal representation, performs
some simple normalizations and consistency checks and executes the
"preprocessor directives" that the programmer may have put into the
file. The preprocessor directives are like a very simple programming
language that allows for simple code generation and manipulation.
The code preprocessor can be called from within Pike with the
cpp
call.
Pike code is Unicode enabled, so the first thing the preprocessor has to do is to try to determine the character encoding of the file. It will first look at the two first bytes of the file and interpret them according to this chart.
Byte 0 | Byte 1 | Interpretation |
0 | 0 | 32bit wide string. |
0 | >0 | 16bit Unicode string. |
>0 | 0 | 16bit Unicode string in reverse byte order. |
0xfe | 0xff | 16bit Unicode string. |
0xff | 0xfe | 16bit Unicode string in reverse byte order. |
0x7b | 0x83 | EBCDIC-US ("#c"). |
0x7b | 0x40 | EBCDIC-US ("# "). |
0x7b | 0x09 | EBCDIC-US ("#\t"). |
The preprocessor collapses all consecutive white space characters outside of strings, except for newlines, to single space characters. All // and /**/ comments are removed, as are #! lines. Pike considers ANSI/DEC escape sequences as white space. Supported formats are <ESC>[\040-\077]+[\100-\177] and <CSI>[\040-\077]*[\100-\177]. Note that this means that it is possible to do color markup in the actual source file.
The preprocessor will treat seven consecutive < characters outside of a string as an version control conflict error and will return "Merge conflict detected."
Defining macros or constants is one of the most used preprocessor features. It enables you to make abstractions on a code generation level as well as altering constants cross-application. The simplest use of the #define directive however is to declare a "define" as present.
#define DO_OVERSAMPLING
The existence of this definition can now be used by e.g. #ifdef and #ifndef to activate or deactivate blocks of program code.
#ifdef DO_OVERSAMPLING // This code is not always run. img->render(size*4)->shrink(4); #endif
Note that defines can be given to pike at execution time. In order to set DO_OVERSAMPLING from a command line, the option -DDO_OVERSAMPLING is added before the name of the pike program. E.g. pike -DDO_OVERSAMPLING my_program.pike.
A define can also be given a specific value, which will be inserted everywhere the define is placed in the source code.
#define CYCLES 20 void do_stuff() { for(int i; i<CYCLES; i++) do_other_stuff(); }
Defines can be given specific values on the command line too, just be sure to quote them as required by your shell.
~% pike '-DTEXT="Hello world!"' -e 'write("%s\n", TEXT);' Hello world!
Finally #define can also be used to define macros. Macros are just text expansion with arguments, but it is often very useful to make a cleaner looking code and to write less.
#define VAR(X) id->misc->variable[X] #define ROL(X,Y) (((X)<<(Y))&7+((X)>>(8-(Y)))) #define PLACEHOLDER(X) void X(mixed ... args) { \ error("Method " #X " is not implemented yet.\n"); } #define ERROR(X,Y ...) werror("MyClass" X "\n", Y) #define NEW_CONSTANTS(X) do{ int i=sizeof(all_constants()); \ X \ werror("Constant diff is %d\n", sizeof(all_constants())-i); \ }while(0) #define MY_FUNC(X,Y) void my##X##Y()
All of the preprocessor directives except the string-related (#string and #"") should have the hash character (#) as the first character of the line. Even though it is currently allowed to be indented, it is likely that this will generate warnings or errors in the future. Note that it is however allowed to put white-space between the hash character and the rest of the directive to create indentation in code.
#!
All lines beginning with #!
will be regarded as comments,
to enable shell integration. It is recommended that Pike applications
begin with the line "#! /usr/bin/env pike" for maximum cross
platform compatibility.
#line
#<integer>
A hash character followed by a number or by the string "line" and a number will make the preprocessor line counter set this number as the line number for the next line and adjust the following lines accordingly.
All error messages from Pike will use these line numbers.
Optionally the number may be followed by a file name, e.g. #line 1 "/home/pike/program.pike.in". Then this filename will be used instead of the current file for error messages.
#""
If a string literal is opened with #" newlines in the string will end up in the string literal, instead of triggering a "newline in string" error.
Newlines will be converted to \n characters in the string even if the newlines in the file are something else.
This preprocessor directive may appear anywhere a string may appear.
#string
#(#)
#[#]
#{#}
If a string literal is opened with #( all subsequent characters until the closing #) will be treated as literals, including newlines, \, " and '.
There are three different pairs of start/end tokens for this type of literals, #( and #), #[ and #], and #{ and #}.
#["\n\'##]
is equivalent to"\"\\n\\'#"
.
#string
The preprocessor directive #string
will load the file in the
string that follows and insert its contents as a string. This
preprocessor directive may appear anywhere a string may appear.
do_something(#string "the_file.wks" );
#include
#include
#include
is used to insert the contents of another file into
the processed file at the place of the include directive.
Files can be referenced either by absolute or relative path from the source file, or searched for in the include paths.
To include a file with absolute or relative path, use double quotes, e.g. #include "constants.pike" or #include "../debug.h".
To include from the include paths, use less than and greater than, e.g. #include <profiling.h>.
It is also possible to include a file whose path is defined in a preprocessor macro, e.g. #include USER_SETTINGS.
#if
The #if
directive can evaluate simple expressions and, if
the expression is evaluates to true, "activate" the code block that
follows. The code block ends when an #endif
, #else
,
#elseif
or #elif
block is encountered at the same
nesting depth.
The #if
expressions may include defines, integer, string
and float constants, ?:, || and && operations,
~, ^, !, | and & operations,
<, >, <=, >=, == and != operations,
+, -, *, /, << and >> operations
and paranthesis.
Strings may also be indexed with the [] index operator.
Finally there are three special "functions" available in #if
expressions; defined()
, efun()
and constant()
.
#ifdef
, #ifndef
, #elif
, #else
, #endif
,
defined()
, constant()
, efun()
#ifdef
Check whether an identifier is a macro.
The directive
#ifdef <identifier>
is equivalent to
#if defined
(<identifier>)
#if
, #ifndef
, defined
#ifndef
Check whether an identifier is not a macro.
This is the inverse of #ifdef
.
The directive
#ifndef <identifier>
is equivalent to
#if !defined
(<identifier>)
#if
, #ifdef
, defined
#endif
End a block opened with #if
, #ifdef
, #ifndef
,
#else
, #elseif
or #elif
.
#ifdef DEBUG do_debug_stuff(); #endif // DEBUG
#else
This directive is used to divide the current code block into another code block with inverse activation.
#ifdef FAST_ALGORITHM do_fast_algorithm(); #elif defined(EXPERIMENTAL_ALGORITHM) do_experimental_algorithm(); #else do_default_algorithm(); #endif
#elif
#elseif
These work as a combined #else
and #if
without
adding an extra level of nesting.
The following two are equivalent:
#ifdef A // Code for A. #else #ifdef B // Code for B. #else #ifdef C // Code for C. #else // Code for D. #endif #endif #endif
And
#ifdef A // Code for A. #elif defined(B) // Code for B. #elseif defined(C) // Code for C. #else // Code for D. #endif
#if
, #ifdef
, #else
, defined()
, constant()
#elifdef
#elseifdef
These work as a combined #else
and #ifdef
without
adding an extra level of nesting.
The following two are equivalent:
#ifdef A // Code for A. #else #ifdef B // Code for B. #else #ifdef C // Code for C. #else // Code for D. #endif #endif #endif
And
#ifdef A // Code for A. #elifdef B // Code for B. #elseifdef C // Code for C. #else // Code for D. #endif
#if
, #ifdef
, #else
, defined()
, constant()
#elifndef
#elseifndef
These work as a combined #else
and #ifndef
without
adding an extra level of nesting.
The following two are equivalent:
#ifndef A // Code for not A. #else #ifndef B // Code for not B. #else #ifdef C // Code for not C. #else // Code for ABC. #endif #endif #endif
And
#ifndef A // Code for not A. #elifndef B // Code for not B. #elseifndef C // Code for not C. #else // Code for ABC. #endif
#if
, #ifdef
, #else
, defined()
, constant()
#define
This directive is used to define or redefine a cpp macro.
The simplest way to use define is to write
#define <identifier> <replacement string>
which will cause all subsequent occurances of <identifier to be replaced with the <replacement string>.
Define also has the capability to use arguments, thus a line like
#define <identifier>(arg1, arg2) <replacement string>
would cause <identifer> to be a macro. All occurances of '<identifier>(something1,something2d)' would be replaced with the <replacement string>. And in the <replacement string>, arg1 and arg2 will be replaced with something1 and something2.
#undef
#undefine
This removes the effect of a #define
, all subsequent occurances of
the undefined identifier will not be replaced by anything.
Note that when undefining a macro, you just give the identifer, not the arguments.
// Strip debug #define werror(X ...) 0 #include "/home/someone/experimental/stuff.h" #undef werror
#define
, defined()
#charset
Inform the preprocessor about which charset the file is encoded with. The Charset module is called with this string to decode the remainder of the file.
#pike
Set the Pike compiler backward compatibility level.
This tells the compiler which version of Pike it should attempt to emulate from this point on in the current compilation unit.
This is typically used to "quick-fix" old code to work with more recent versions of Pike.
// This code was written for Pike 7.2, and depends on // the old behaviour for 7.2::dirname(). #pike 7.2 // ... Code that uses dirname() ...
This directive is also needed for Pike modules that have been installed globally, and might be used by a Pike that has been started with the -V flag.
// Pike modules that are bundled with Pike are // typically written for the same version of Pike. #pike __REAL_VERSION__
#pragma
This is a generic directive for flags to the compiler.
These are some of the flags that are available:
| This is the same as adding the modifier inline to all functions that follow. |
| Instructs the compiler to mark all symbols as final. |
| Enable warnings for use of deprecated or experimental symbols
(default). Note that this does not enable warnings for
experimental symbols if those warnings have been disabled
explicitly via |
| Disable warnings for use of deprecated or experimental symbols. This is typically used in code that implements the deprecated symbols. |
| Enable warnings for use of experimental symbols (default). Note that this does not enable warnings for experimental symbols when warnings for deprecated symbols are disabled. |
| Disable warnings for use of experimental symbols. This is typically used in code that implements or tests the experimental symbols. |
| Cause nested classes to save a reference to their surrounding class even if not strictly needed. |
| Inverse of "save_parent". This is needed to override
if the global symbol |
| Enable warnings for all cases where the compiler isn't certain that the types are correct. |
| Enable disassembly output for the code being compiled.
Note that this option essentially has a function-level
scope, so enabling it for just a few lines is usually
a noop. This is similar to |
| Disable disassembly output (default). |
#require
If the directive evaluates to false, the source file will be
considered to have failed dependencies, and will not be found by
the resolver. In practical terms the cpp()
call will return
zero.
#if
#warning
Generate a warning during preprocessing.
This directive causes a cpp warning, it can be used to notify the user that certain functions are missing and similar things.
#if !constant(Yp) #warning Support for NIS not available. Some features may not work. #endif
#error
#error
Throw an error during preprocessing.
This directive causes a cpp error. It can be used to notify the user that certain functions are missing and similar things.
Note that this directive will cause cpp()
to throw
an error at the end of preprocessing, which will cause
any compilation to fail.
#if !constant(Yp) #error Support for NIS not available. #endif
#warning
constant
__VERSION__
This define contains the current Pike version as a float. If another Pike version is emulated, this define is updated accordingly.
__REAL_VERSION__
constant
__MAJOR__
This define contains the major part of the current Pike version, represented as an integer. If another Pike version is emulated, this define is updated accordingly.
__REAL_MAJOR__
constant
__MINOR__
This define contains the minor part of the current Pike version, represented as an integer. If another Pike version is emulated, this define is updated accordingly.
__REAL_MINOR__
constant
__BUILD__
This constant contains the build number of the current Pike version, represented as an integer. If another Pike version is emulated, this constant remains unaltered.
__REAL_MINOR__
constant
__REAL_VERSION__
This define always contains the version of the current Pike, represented as a float.
__VERSION__
constant
__REAL_MAJOR__
This define always contains the major part of the version of the current Pike, represented as an integer.
__MAJOR__
constant
__REAL_MINOR__
This define always contains the minor part of the version of the current Pike, represented as an integer.
__MINOR__
constant
__REAL_BUILD__
This define always contains the minor part of the version of the current Pike, represented as an integer.
__BUILD__
constant
__DATE__
This define contains the current date at the time of compilation, e.g. "Jul 28 2001".
constant
__TIME__
This define contains the current time at the time of compilation, e.g. "12:20:51".
constant
__FILE__
This define contains the file path and name of the source file.
constant
__DIR__
This define contains the directory path of the source file.
constant
__LINE__
This define contains the current line number, represented as an integer, in the source file.
int(1..)
__COUNTER__
This define contains a unique counter (unless it has been expanded Int.NATIVE_MAX times) represented as an integer.
constant
__AUTO_BIGNUM__
This define is defined when automatic bignum conversion is enabled. When enabled all integers will automatically be converted to bignums when they get bigger than what can be represented by an integer, hampering performance slightly instead of crashing the program. This define is always set since Pike 8.0.
constant
__NT__
This define is defined when the Pike is running on a Microsoft Windows OS, not just Microsoft Windows NT, as the name implies.
constant
__PIKE__
This define is always true.
constant
__amigaos__
This define is defined when the Pike is running on Amiga OS.
constant
__APPLE__
This define is defined when the Pike is running on MacOS X.
constant
__HAIKU__
This define is defined when the Pike is running on Haiku.
void
_Pragma(string
directive
)
This macro inserts the corresponding #pragma
directive
in the source.
e.g. _Pragma("strict_types")
is the same
as #pragma strict_types
.
#pragma
constant
static_assert
This define expands to the symbol _Static_assert
.
It is the preferred way to perform static (ie compile-time) assertions.
The macro can also be used to check for whether static assertions are supported.
predef::_Static_assert()
These functions are used in #if
et al
expressions to test for presence of symbols.
bool
constant(mixed
identifier
)
__deprecated__
bool
efun(mixed
identifier
)
Check whether the argument resolves to a constant or not.
#if
, defined()
bool
defined(mixed
identifier
)
Check whether an identifier is a cpp macro or not.
defined
returns true if the symbol given as argument
is defined.
#if defined(MY_DEF) is equivalent to #ifdef MY_DEF.
#if
, #ifdef
, constant()