home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / info / gcc.i05 (.txt) < prev    next >
GNU Info File  |  1993-06-12  |  48KB  |  902 lines

  1. This is Info file gcc.info, produced by Makeinfo-1.47 from the input
  2. file gcc.tex.
  3.    This file documents the use and the internals of the GNU compiler.
  4.    Copyright (C) 1988, 1989, 1992 Free Software Foundation, Inc.
  5.    Permission is granted to make and distribute verbatim copies of this
  6. manual provided the copyright notice and this permission notice are
  7. preserved on all copies.
  8.    Permission is granted to copy and distribute modified versions of
  9. this manual under the conditions for verbatim copying, provided also
  10. that the sections entitled "GNU General Public License" and "Protect
  11. Your Freedom--Fight `Look And Feel'" are included exactly as in the
  12. original, and provided that the entire resulting derived work is
  13. distributed under the terms of a permission notice identical to this
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions, except that the sections entitled "GNU General Public
  17. License" and "Protect Your Freedom--Fight `Look And Feel'", and this
  18. permission notice, may be included in translations approved by the Free
  19. Software Foundation instead of in the original English.
  20. File: gcc.info,  Node: Unos Install,  Next: VMS Install,  Prev: 3b1 Install,  Up: Installation
  21. Installing GNU CC on Unos
  22. =========================
  23.    Use `configure unos' for building on Unos.
  24.    The Unos assembler is named `casm' instead of `as'.  For some
  25. strange reason linking `/bin/as' to `/bin/casm' changes the behavior,
  26. and does not work.  So, when installing GNU CC, you should install the
  27. following script as `as' in the subdirectory where the passes of GCC
  28. are installed:
  29.      #!/bin/sh
  30.      casm $*
  31.    The default Unos library is named `libunos.a' instead of `libc.a'. 
  32. To allow GNU CC to function, either change all references to `-lc' in
  33. `gcc.c' to `-lunos' or link `/lib/libc.a' to `/lib/libunos.a'.
  34.    When compiling GNU CC with the standard compiler, to overcome bugs in
  35. the support of `alloca', do not use `-O' when making stage 2. Then use
  36. the stage 2 compiler with `-O' to make the stage 3 compiler.  This
  37. compiler will have the same characteristics as the usual stage 2
  38. compiler on other systems.  Use it to make a stage 4 compiler and
  39. compare that with stage 3 to verify proper compilation.
  40.    (Perhaps simply defining `ALLOCA' in `x-crds' as described in the
  41. comments there will make the above paragraph superfluous.  Please
  42. inform us of whether this works.)
  43.    Unos uses memory segmentation instead of demand paging, so you will
  44. need a lot of memory.  5 Mb is barely enough if no other tasks are
  45. running. If linking `cc1' fails, try putting the object files into a
  46. library and linking from that library.
  47. File: gcc.info,  Node: VMS Install,  Next: WE32K Install,  Prev: Unos Install,  Up: Installation
  48. Installing GNU CC on VMS
  49. ========================
  50.    The VMS version of GNU CC is distributed in a backup saveset
  51. containing both source code and precompiled binaries.
  52.    To install the `gcc' command so you can use the compiler easily, in
  53. the same manner as you use the VMS C compiler, you must install the VMS
  54. CLD file for GNU CC as follows:
  55.   1. Define the VMS logical names `GNU_CC' and `GNU_CC_INCLUDE' to
  56.      point to the directories where the GNU CC executables
  57.      (`gcc-cpp.exe', `gcc-cc1.exe', etc.) and the C include files are
  58.      kept respectively.  This should be done with the commands:
  59.           $ assign /system /translation=concealed -
  60.             disk:[gcc.] gnu_cc
  61.           $ assign /system /translation=concealed -
  62.             disk:[gcc.include.] gnu_cc_include
  63.      with the appropriate disk and directory names.  These commands can
  64.      be placed in your system startup file so they will be executed
  65.      whenever the machine is rebooted.  You may, if you choose, do this
  66.      via the `GCC_INSTALL.COM' script in the `[GCC]' directory.
  67.   2. Install the `GCC' command with the command line:
  68.           $ set command /table=sys$common:[syslib]dcltables -
  69.             /output=sys$common:[syslib]dcltables gnu_cc:[000000]gcc
  70.           $ install replace sys$common:[syslib]dcltables
  71.   3. To install the help file, do the following:
  72.           $ library/help sys$library:helplib.hlb gcc.hlp
  73.      Now you can invoke the compiler with a command like `gcc /verbose
  74.      file.c', which is equivalent to the command `gcc -v -c file.c' in
  75.      Unix.
  76.    If you wish to use GNU C++ you must first install GNU CC, and then
  77. perform the following steps:
  78.   1. Define the VMS logical name `GNU_GXX_INCLUDE' to point to the
  79.      directory where the preprocessor will search for the C++ header
  80.      files. This can be done with the command:
  81.           $ assign /system /translation=concealed -
  82.             disk:[gcc.gxx_include.] gnu_gxx_include
  83.      with the appropriate disk and directory name.  If you are going to
  84.      be using libg++, this is where the libg++ install procedure will
  85.      install the libg++ header files.
  86.   2. Obtain the file `gcc-cc1plus.exe', and place this in the same
  87.      directory that `gcc-cc1.exe' is kept.
  88.      The GNU C++ compiler can be invoked with a command like `gcc /plus
  89.      /verbose file.cc', which is equivalent to the command `g++ -v -c
  90.      file.cc' in Unix.
  91.    We try to put corresponding binaries and sources on the VMS
  92. distribution tape.  But sometimes the binaries will be from an older
  93. version than the sources, because we don't always have time to update
  94. them.  (Use the `/version' option to determine the version number of
  95. the binaries and compare it with the source file `version.c' to tell
  96. whether this is so.)  In this case, you should use the binaries you get
  97. to recompile the sources.  If you must recompile, here is how:
  98.   1. Execute the command procedure `vmsconfig.com' to copy files
  99.      `vax-vms.h', `xm-vax-vms.h', `vax.c' and `vax.md' to `tm.h',
  100.      `config.h', `aux-output.c', and `md.' respectively, and to create
  101.      files `tconfig.h' and `hconfig.h'.  This procedure also creates
  102.      several linker option files used by `make-cc1.com' and a data file
  103.      used by `make-l2.com'.
  104.           $ @vmsconfig.com
  105.   2. Setup the logical names and command tables as defined above.  In
  106.      addition, define the VMS logical name `GNU_BISON' to point at the
  107.      to the directories where the Bison executable is kept.  This
  108.      should be done with the command:
  109.           $ assign /system /translation=concealed -
  110.             disk:[bison.] gnu_bison
  111.      You may, if you choose, use the `INSTALL_BISON.COM' script in the
  112.      `[BISON]' directory.
  113.   3. Install the `BISON' command with the command line:
  114.           $ set command /table=sys$common:[syslib]dcltables -
  115.             /output=sys$common:[syslib]dcltables -
  116.             gnu_bison:[000000]bison
  117.           $ install replace sys$common:[syslib]dcltables
  118.   4. Type `@make-gcc' to recompile everything (alternatively, you may
  119.      submit the file `make-gcc.com' to a batch queue).  If you wish to
  120.      build the GNU C++ compiler as well as the GNU CC compiler, you must
  121.      first edit `make-gcc.com' and follow the instructions that appear
  122.      in the comments.
  123.   5. In order to use GCC, you need a library of functions which GCC
  124.      compiled code will call to perform certain tasks, and these
  125.      functions are defined in the file `libgcc2.c'.  To compile this
  126.      you should use the command procedure `make-l2.com', which will
  127.      generate the library `libgcc2.olb'. `libgcc2.olb' should be built
  128.      using the compiler built from the same distribution that
  129.      `libgcc2.c' came from, and `make-gcc.com' will automatically do
  130.      all of this for you.
  131.      To install the library, use the following commands:
  132.           $ library gnu_cc:[000000]gcclib/delete=(new,eprintf)
  133.           $ library libgcc2/extract=*/output=libgcc2.obj
  134.           $ library gnu_cc:[000000]gcclib libgcc2.obj
  135.      The first command simply removes old modules that will be replaced
  136.      with modules from libgcc2.  If the VMS librarian complains about
  137.      those modules not being present, simply ignore the message and
  138.      continue on with the next command.
  139.      Whenever you update the compiler on your system, you should also
  140.      update the library with the above procedure.
  141.   6. You may wish to build GCC in such a way that no files are written
  142.      to the directory where the source files reside.  An example would
  143.      be the when the source files are on a read-only disk.  In these
  144.      cases, execute the following DCL commands (substituting your
  145.      actual path names):
  146.           $ assign dua0:[gcc.build_dir.]/translation=concealed, -
  147.                    dua1:[gcc.source_dir.]/translation=concealed  gcc_build
  148.           $ set default gcc_build:[000000]
  149.      where `dua1:[gcc.source_dir]' contains the source code, and
  150.      `dua0:[gcc.build_dir]' is meant to contain all of the generated
  151.      object files and executables.  Once you have done this, you can
  152.      proceed building GCC as described above.  (Keep in mind that
  153.      `gcc_build' is a rooted logical name, and thus the device names in
  154.      each element of the search list must be an actual physical device
  155.      name rather than another rooted logical name).
  156.   7. *If you are building GNU CC with a previous version of GNU CC, you
  157.      also should check to see that you have the newest version of the
  158.      assembler*.  In particular, GNU CC version 2 treats global constant
  159.      variables slightly differently from GNU CC version 1, and GAS
  160.      version 1.38.1 does not have the patches required to work with GCC
  161.      version 2. If you use GAS 1.38.1, then `extern const' variables
  162.      will not have the read-only bit set, and the linker will generate
  163.      warning messages about mismatched psect attributes for these
  164.      variables.  These warning messages are merely a nuisance, and can
  165.      safely be ignored.
  166.      If you are compiling with a version of GNU CC older than 1.33,
  167.      specify `/DEFINE=("inline=")' as an option in all the
  168.      compilations.  This requires editing all the `gcc' commands in
  169.      `make-cc1.com'. (The older versions had problems supporting
  170.      `inline'.)  Once you have a working 1.33 or newer GNU CC, you can
  171.      change this file back.
  172.   8. If you want to build GNU CC with the VAX C compiler, you will need
  173.      to make minor changes in `make-cccp.com' and `make-cc1.com' to
  174.      choose alternate definitions of `CC', `CFLAGS', and `LIBS'.  See
  175.      comments in those files.  However, you must also have a working
  176.      version of the GNU assembler (GNU as, aka GAS) as it is used as
  177.      the back-end for GNU CC to produce binary object modules and is
  178.      not included in the GNU CC sources.  GAS is also needed to compile
  179.      `libgcc2' in order to build `gcclib' (see above); `make-l2.com'
  180.      expects to be able to find it operational in
  181.      `gnu_cc:[000000]gnu-as.exe'.
  182.      To use GNU CC on VMS, you need the VMS driver programs `gcc.exe',
  183.      `gcc.com', and `gcc.cld'.  They are distributed with the VMS
  184.      binaries (`gcc-vms') rather than the GNU CC sources.  GAS is also
  185.      included in `gcc-vms', as is Bison.
  186.      Once you have successfully built GNU CC with VAX C, you should use
  187.      the resulting compiler to rebuild itself.  Before doing this, be
  188.      sure to restore the `CC', `CFLAGS', and `LIBS' definitions in
  189.      `make-cccp.com' and `make-cc1.com'.  The second generation
  190.      compiler will be able to take advantage of many optimizations that
  191.      must be suppressed when building with other compilers.
  192.    Under previous versions of GNU CC, the generated code would
  193. occasionally give strange results when linked with the sharable
  194. `VAXCRTL' library. Now this should work.
  195.    Even with this version, however, GNU CC itself should not be linked
  196. with the sharable `VAXCRTL'.  The version of `qsort' in `VAXCRTL' has a
  197. bug (known to be present in VMS versions V4.6 through V5.5) which
  198. causes the compiler to fail.
  199.    The executables that are generated by `make-cc1.com' and
  200. `make-cccp.com' use the object library version of `VAXCRTL' in order to
  201. make use of the `qsort' routine in `gcclib.olb'.  If you wish to link
  202. the compiler executables with the shareable image version of `VAXCRTL',
  203. you should edit the file `tm.h' (created by `vmsconfig.com') to define
  204. the macro `QSORT_WORKAROUND'.
  205.    `QSORT_WORKAROUND' is always defined when GNU CC is compiled with
  206. VAX C, to avoid a problem in case `gcclib.olb' is not yet available.
  207. File: gcc.info,  Node: WE32K Install,  Next: MIPS Install,  Prev: VMS Install,  Up: Installation
  208. Installing GNU CC on the WE32K
  209. ==============================
  210.    These computers are also known as the 3b2, 3b5, 3b20 and other
  211. similar names.  (However, the 3b1 is actually a 68000; see *Note 3b1
  212. Install::.)
  213.    Don't use `-g' when compiling with the system's compiler.  The
  214. system's linker seems to be unable to handle such a large program with
  215. debugging information.
  216.    The system's compiler runs out of capacity when compiling `stmt.c'
  217. in GNU CC.  You can work around this by building `cpp' in GNU CC first,
  218. then use that instead of the system's preprocessor with the system's C
  219. compiler to compile `stmt.c'.  Here is how:
  220.      mv /lib/cpp /lib/cpp.att
  221.      cp cpp /lib/cpp.gnu
  222.      echo "/lib/cpp.gnu -traditional $*" > /lib/cpp
  223.      chmod +x /lib/cpp
  224.    The system's compiler produces bad code for some of the GNU CC
  225. optimization files.  So you must build the stage 2 compiler without
  226. optimization.  Then build a stage 3 compiler with optimization. That
  227. executable should work.  Here are the necessary commands:
  228.      make LANGUAGES=c CC=stage1/xgcc CFLAGS="-Bstage1/ -g"
  229.      make stage2
  230.      make CC=stage2/xgcc CFLAGS="-Bstage2/ -g -O"
  231.    You may need to raise the ULIMIT setting to build a C++ compiler, as
  232. the file `cc1plus' is larger than one megabyte.
  233. File: gcc.info,  Node: MIPS Install,  Prev: WE32K Install,  Up: Installation
  234. Installing GNU CC on the MIPS
  235. =============================
  236.    See *Note Installation:: about whether to use `--with-stabs' or not.
  237.    The MIPS C compiler needs to be told to increase its table size for
  238. switch statements with the `-Wf,-XNg1500' option in order to compile
  239. `cp-parse.c'.  If you use the `-O2' optimization option, you also need
  240. to use `-Olimit 3000'. Both of these options are automatically
  241. generated in the `Makefile' that the shell script `configure' builds.
  242. If you override the `CC' make variable and use the MIPS compilers, you
  243. may need to add `-Wf,-XNg1500 -Olimit 3000'.
  244.    MIPS computers running RISC-OS can support four different
  245. personalities: default, BSD 4.3, System V.3, and System V.4 (older
  246. versions of RISC-OS don't support V.4).  To configure GCC for these
  247. platforms use the following configurations:
  248. `mips-mips-riscos`rev''
  249.      Default configuration for RISC-OS, revision `rev'.
  250. `mips-mips-riscos`rev'bsd'
  251.      BSD 4.3 configuration for RISC-OS, revision `rev'.
  252. `mips-mips-riscos`rev'sysv4'
  253.      System V.4 configuration for RISC-OS, revision `rev'.
  254. `mips-mips-riscos`rev'sysv'
  255.      System V.3 configuration for RISC-OS, revision `rev'.
  256.    The revision `rev' mentioned above is the revision of RISC-OS to
  257. use.  You must reconfigure GCC when going from a RISC-OS revision 4 to
  258. RISC-OS revision 5.  This has the effect of avoiding a linker bug (see
  259. *Note Installation Problems:: for more details).
  260.    DECstations can support three different personalities: Ultrix, DEC
  261. OSF/1, and OSF/rose.  To configure GCC for these platforms use the
  262. following configurations:
  263. `decstation-ultrix'
  264.      Ultrix configuration.
  265. `decstation-osf1'
  266.      Dec's version of OSF/1.
  267. `decstation-osfrose'
  268.      Open Software Foundation reference port of OSF/1 which uses the
  269.      OSF/rose object file format instead of ECOFF.  Normally, you would
  270.      not select this configuration.
  271. File: gcc.info,  Node: Extensions,  Next: Trouble,  Prev: Installation,  Up: Top
  272. GNU Extensions to the C Language
  273. ********************************
  274.    GNU C provides several language features not found in ANSI standard
  275. C. (The `-pedantic' option directs GNU CC to print a warning message if
  276. any of these features is used.)  To test for the availability of these
  277. features in conditional compilation, check for a predefined macro
  278. `__GNUC__', which is always defined under GNU CC.
  279. * Menu:
  280. * Statement Exprs::     Putting statements and declarations inside expressions.
  281. * Local Labels::        Labels local to a statement-expression.
  282. * Labels as Values::    Getting pointers to labels, and computed gotos.
  283. * Nested Functions::    As in Algol and Pascal, lexical scoping of functions.
  284. * Naming Types::        Giving a name to the type of some expression.
  285. * Typeof::              `typeof': referring to the type of an expression.
  286. * Lvalues::             Using `?:', `,' and casts in lvalues.
  287. * Conditionals::        Omitting the middle operand of a `?:' expression.
  288. * Long Long::        Double-word integers--`long long int'.
  289. * Zero Length::         Zero-length arrays.
  290. * Variable Length::     Arrays whose length is computed at run time.
  291. * Macro Varargs::    Macros with variable number of arguments.
  292. * Subscripting::        Any array can be subscripted, even if not an lvalue.
  293. * Pointer Arith::       Arithmetic on `void'-pointers and function pointers.
  294. * Initializers::        Non-constant initializers.
  295. * Constructors::        Constructor expressions give structures, unions
  296.                          or arrays as values.
  297. * Labeled Elements::    Labeling elements of initializers.
  298. * Cast to Union::       Casting to union type from any member of the union.
  299. * Case Ranges::        `case 1 ... 9' and such.
  300. * Function Attributes:: Declaring that functions have no side effects,
  301.                          or that they can never return.
  302. * Function Prototypes:: Prototype declarations and old-style definitions.
  303. * Dollar Signs::        Dollar sign is allowed in identifiers.
  304. * Character Escapes::   `\e' stands for the character ESC.
  305. * Variable Attributes::    Specifying attributes of variables.
  306. * Alignment::           Inquiring about the alignment of a type or variable.
  307. * Inline::              Defining inline functions (as fast as macros).
  308. * Extended Asm::        Assembler instructions with C expressions as operands.
  309.                          (With them you can define "built-in" functions.)
  310. * Asm Labels::          Specifying the assembler name to use for a C symbol.
  311. * Explicit Reg Vars::   Defining variables residing in specified registers.
  312. * Alternate Keywords::  `__const__', `__asm__', etc., for header files.
  313. * Incomplete Enums::    `enum foo;', with details to follow.
  314. File: gcc.info,  Node: Statement Exprs,  Next: Local Labels,  Up: Extensions
  315. Statements and Declarations within Expressions
  316. ==============================================
  317.    A compound statement enclosed in parentheses may appear as an
  318. expression in GNU C.  This allows you to use loops, switches, and local
  319. variables within an expression.
  320.    Recall that a compound statement is a sequence of statements
  321. surrounded by braces; in this construct, parentheses go around the
  322. braces.  For example:
  323.      ({ int y = foo (); int z;
  324.         if (y > 0) z = y;
  325.         else z = - y;
  326.         z; })
  327. is a valid (though slightly more complex than necessary) expression for
  328. the absolute value of `foo ()'.
  329.    The last thing in the compound statement should be an expression
  330. followed by a semicolon; the value of this subexpression serves as the
  331. value of the entire construct.  (If you use some other kind of statement
  332. last within the braces, the construct has type `void', and thus
  333. effectively no value.)
  334.    This feature is especially useful in making macro definitions "safe"
  335. (so that they evaluate each operand exactly once).  For example, the
  336. "maximum" function is commonly defined as a macro in standard C as
  337. follows:
  338.      #define max(a,b) ((a) > (b) ? (a) : (b))
  339. But this definition computes either A or B twice, with bad results if
  340. the operand has side effects.  In GNU C, if you know the type of the
  341. operands (here let's assume `int'), you can define the macro safely as
  342. follows:
  343.      #define maxint(a,b) \
  344.        ({int _a = (a), _b = (b); _a > _b ? _a : _b; })
  345.    Embedded statements are not allowed in constant expressions, such as
  346. the value of an enumeration constant, the width of a bit field, or the
  347. initial value of a static variable.
  348.    If you don't know the type of the operand, you can still do this,
  349. but you must use `typeof' (*note Typeof::.) or type naming (*note
  350. Naming Types::.).
  351. File: gcc.info,  Node: Local Labels,  Next: Labels as Values,  Prev: Statement Exprs,  Up: Extensions
  352. Locally Declared Labels
  353. =======================
  354.    Each statement expression is a scope in which "local labels" can be
  355. declared.  A local label is simply an identifier; you can jump to it
  356. with an ordinary `goto' statement, but only from within the statement
  357. expression it belongs to.
  358.    A local label declaration looks like this:
  359.      __label__ LABEL;
  360.      __label__ LABEL1, LABEL2, ...;
  361.    Local label declarations must come at the beginning of the statement
  362. expression, right after the `({', before any ordinary declarations.
  363.    The label declaration defines the label *name*, but does not define
  364. the label itself.  You must do this in the usual way, with `LABEL:',
  365. within the statements of the statement expression.
  366.    The local label feature is useful because statement expressions are
  367. often used in macros.  If the macro contains nested loops, a `goto' can
  368. be useful for breaking out of them.  However, an ordinary label whose
  369. scope is the whole function cannot be used: if the macro can be
  370. expanded several times in one function, the label will be multiply
  371. defined in that function.  A local label avoids this problem.  For
  372. example:
  373.      #define SEARCH(array, target)                     \
  374.      ({                                               \
  375.        __label__ found;                                \
  376.        typeof (target) _SEARCH_target = (target);      \
  377.        typeof (*(array)) *_SEARCH_array = (array);     \
  378.        int i, j;                                       \
  379.        int value;                                      \
  380.        for (i = 0; i < max; i++)                       \
  381.          for (j = 0; j < max; j++)                     \
  382.            if (_SEARCH_array[i][j] == _SEARCH_target)  \
  383.              { value = i; goto found; }              \
  384.        value = -1;                                     \
  385.       found:                                           \
  386.        value;                                          \
  387.      })
  388. File: gcc.info,  Node: Labels as Values,  Next: Nested Functions,  Prev: Local Labels,  Up: Extensions
  389. Labels as Values
  390. ================
  391.    You can get the address of a label defined in the current function
  392. (or a containing function) with the unary operator `&&'.  The value has
  393. type `void *'.  This value is a constant and can be used wherever a
  394. constant of that type is valid.  For example:
  395.      void *ptr;
  396.      ...
  397.      ptr = &&foo;
  398.    To use these values, you need to be able to jump to one.  This is
  399. done with the computed goto statement(1), `goto *EXP;'.  For example,
  400.      goto *ptr;
  401. Any expression of type `void *' is allowed.
  402.    One way of using these constants is in initializing a static array
  403. that will serve as a jump table:
  404.      static void *array[] = { &&foo, &&bar, &&hack };
  405.    Then you can select a label with indexing, like this:
  406.      goto *array[i];
  407. Note that this does not check whether the subscript is in bounds--array
  408. indexing in C never does that.
  409.    Such an array of label values serves a purpose much like that of the
  410. `switch' statement.  The `switch' statement is cleaner, so use that
  411. rather than an array unless the problem does not fit a `switch'
  412. statement very well.
  413.    Another use of label values is in an interpreter for threaded code.
  414. The labels within the interpreter function can be stored in the
  415. threaded code for super-fast dispatching.
  416.    You can use this mechanism to jump to code in a different function. 
  417. If you do that, totally unpredictable things will happen.  The best way
  418. to avoid this is to store the label address only in automatic variables
  419. and never pass it as an argument.
  420.    ---------- Footnotes ----------
  421.    (1)  The analogous feature in Fortran is called an assigned goto,
  422. but that name seems inappropriate in C, where one can do more than
  423. simply store label addresses in label variables.
  424. File: gcc.info,  Node: Nested Functions,  Next: Naming Types,  Prev: Labels as Values,  Up: Extensions
  425. Nested Functions
  426. ================
  427.    A "nested function" is a function defined inside another function.
  428. The nested function's name is local to the block where it is defined.
  429. For example, here we define a nested function named `square', and call
  430. it twice:
  431.      foo (double a, double b)
  432.      {
  433.        double square (double z) { return z * z; }
  434.      
  435.        return square (a) + square (b);
  436.      }
  437.    The nested function can access all the variables of the containing
  438. function that are visible at the point of its definition.  This is
  439. called "lexical scoping".  For example, here we show a nested function
  440. which uses an inherited variable named `offset':
  441.      bar (int *array, int offset, int size)
  442.      {
  443.        int access (int *array, int index)
  444.          { return array[index + offset]; }
  445.        int i;
  446.        ...
  447.        for (i = 0; i < size; i++)
  448.          ... access (array, i) ...
  449.      }
  450.    It is possible to call the nested function from outside the scope of
  451. its name by storing its address or passing the address to another
  452. function:
  453.      hack (int *array, int size)
  454.      {
  455.        void store (int index, int value)
  456.          { array[index] = value; }
  457.      
  458.        intermediate (store, size);
  459.      }
  460.    Here, the function `intermediate' receives the address of `store' as
  461. an argument.  If `intermediate' calls `store', the arguments given to
  462. `store' are used to store into `array'.  But this technique works only
  463. so long as the containing function (`hack', in this example) does not
  464. exit.  If you try to call the nested function through its address after
  465. the containing function has exited, all hell will break loose.
  466.    GNU CC implements taking the address of a nested function using a
  467. technique called "trampolines".  A paper describing them is available
  468. from `maya.idiap.ch' in the file `pub/tmb/usenix88-lexic.ps.Z'.
  469.    A nested function can jump to a label inherited from a containing
  470. function, provided the label was explicitly declared in the containing
  471. function (*note Local Labels::.).  Such a jump returns instantly to the
  472. containing function, exiting the nested function which did the `goto'
  473. and any intermediate functions as well.  Here is an example:
  474.      bar (int *array, int offset, int size)
  475.      {
  476.        __label__ failure;
  477.        int access (int *array, int index)
  478.          {
  479.            if (index > size)
  480.              goto failure;
  481.            return array[index + offset];
  482.          }
  483.        int i;
  484.        ...
  485.        for (i = 0; i < size; i++)
  486.          ... access (array, i) ...
  487.        ...
  488.        return 0;
  489.      
  490.       /* Control comes here from `access'
  491.          if it detects an error.  */
  492.       failure:
  493.        return -1;
  494.      }
  495.    A nested function always has internal linkage.  Declaring one with
  496. `extern' is erroneous.  If you need to declare the nested function
  497. before its definition, use `auto' (which is otherwise meaningless for
  498. function declarations).
  499.      bar (int *array, int offset, int size)
  500.      {
  501.        __label__ failure;
  502.        auto int access (int *, int);
  503.        ...
  504.        int access (int *array, int index)
  505.          {
  506.            if (index > size)
  507.              goto failure;
  508.            return array[index + offset];
  509.          }
  510.        ...
  511.      }
  512. File: gcc.info,  Node: Naming Types,  Next: Typeof,  Prev: Nested Functions,  Up: Extensions
  513. Naming an Expression's Type
  514. ===========================
  515.    You can give a name to the type of an expression using a `typedef'
  516. declaration with an initializer.  Here is how to define NAME as a type
  517. name for the type of EXP:
  518.      typedef NAME = EXP;
  519.    This is useful in conjunction with the statements-within-expressions
  520. feature.  Here is how the two together can be used to define a safe
  521. "maximum" macro that operates on any arithmetic type:
  522.      #define max(a,b) \
  523.        ({typedef _ta = (a), _tb = (b);  \
  524.          _ta _a = (a); _tb _b = (b);     \
  525.          _a > _b ? _a : _b; })
  526.    The reason for using names that start with underscores for the local
  527. variables is to avoid conflicts with variable names that occur within
  528. the expressions that are substituted for `a' and `b'.  Eventually we
  529. hope to design a new form of declaration syntax that allows you to
  530. declare variables whose scopes start only after their initializers;
  531. this will be a more reliable way to prevent such conflicts.
  532. File: gcc.info,  Node: Typeof,  Next: Lvalues,  Prev: Naming Types,  Up: Extensions
  533. Referring to a Type with `typeof'
  534. =================================
  535.    Another way to refer to the type of an expression is with `typeof'.
  536. The syntax of using of this keyword looks like `sizeof', but the
  537. construct acts semantically like a type name defined with `typedef'.
  538.    There are two ways of writing the argument to `typeof': with an
  539. expression or with a type.  Here is an example with an expression:
  540.      typeof (x[0](1))
  541. This assumes that `x' is an array of functions; the type described is
  542. that of the values of the functions.
  543.    Here is an example with a typename as the argument:
  544.      typeof (int *)
  545. Here the type described is that of pointers to `int'.
  546.    If you are writing a header file that must work when included in
  547. ANSI C programs, write `__typeof__' instead of `typeof'. *Note
  548. Alternate Keywords::.
  549.    A `typeof'-construct can be used anywhere a typedef name could be
  550. used.  For example, you can use it in a declaration, in a cast, or
  551. inside of `sizeof' or `typeof'.
  552.    * This declares `y' with the type of what `x' points to.
  553.           typeof (*x) y;
  554.    * This declares `y' as an array of such values.
  555.           typeof (*x) y[4];
  556.    * This declares `y' as an array of pointers to characters:
  557.           typeof (typeof (char *)[4]) y;
  558.      It is equivalent to the following traditional C declaration:
  559.           char *y[4];
  560.      To see the meaning of the declaration using `typeof', and why it
  561.      might be a useful way to write, let's rewrite it with these macros:
  562.           #define pointer(T)  typeof(T *)
  563.           #define array(T, N) typeof(T [N])
  564.      Now the declaration can be rewritten this way:
  565.           array (pointer (char), 4) y;
  566.      Thus, `array (pointer (char), 4)' is the type of arrays of 4
  567.      pointers to `char'.
  568. File: gcc.info,  Node: Lvalues,  Next: Conditionals,  Prev: Typeof,  Up: Extensions
  569. Generalized Lvalues
  570. ===================
  571.    Compound expressions, conditional expressions and casts are allowed
  572. as lvalues provided their operands are lvalues.  This means that you
  573. can take their addresses or store values into them.
  574.    For example, a compound expression can be assigned, provided the last
  575. expression in the sequence is an lvalue.  These two expressions are
  576. equivalent:
  577.      (a, b) += 5
  578.      a, (b += 5)
  579.    Similarly, the address of the compound expression can be taken. 
  580. These two expressions are equivalent:
  581.      &(a, b)
  582.      a, &b
  583.    A conditional expression is a valid lvalue if its type is not void
  584. and the true and false branches are both valid lvalues.  For example,
  585. these two expressions are equivalent:
  586.      (a ? b : c) = 5
  587.      (a ? b = 5 : (c = 5))
  588.    A cast is a valid lvalue if its operand is an lvalue.  A simple
  589. assignment whose left-hand side is a cast works by converting the
  590. right-hand side first to the specified type, then to the type of the
  591. inner left-hand side expression.  After this is stored, the value is
  592. converted back to the specified type to become the value of the
  593. assignment.  Thus, if `a' has type `char *', the following two
  594. expressions are equivalent:
  595.      (int)a = 5
  596.      (int)(a = (char *)(int)5)
  597.    An assignment-with-arithmetic operation such as `+=' applied to a
  598. cast performs the arithmetic using the type resulting from the cast,
  599. and then continues as in the previous case.  Therefore, these two
  600. expressions are equivalent:
  601.      (int)a += 5
  602.      (int)(a = (char *)(int) ((int)a + 5))
  603.    You cannot take the address of an lvalue cast, because the use of its
  604. address would not work out coherently.  Suppose that `&(int)f' were
  605. permitted, where `f' has type `float'.  Then the following statement
  606. would try to store an integer bit-pattern where a floating point number
  607. belongs:
  608.      *&(int)f = 1;
  609.    This is quite different from what `(int)f = 1' would do--that would
  610. convert 1 to floating point and store it.  Rather than cause this
  611. inconsistency, we think it is better to prohibit use of `&' on a cast.
  612.    If you really do want an `int *' pointer with the address of `f',
  613. you can simply write `(int *)&f'.
  614. File: gcc.info,  Node: Conditionals,  Next: Long Long,  Prev: Lvalues,  Up: Extensions
  615. Conditional Expressions with Omitted Operands
  616. =============================================
  617.    The middle operand in a conditional expression may be omitted.  Then
  618. if the first operand is nonzero, its value is the value of the
  619. conditional expression.
  620.    Therefore, the expression
  621.      x ? : y
  622. has the value of `x' if that is nonzero; otherwise, the value of `y'.
  623.    This example is perfectly equivalent to
  624.      x ? x : y
  625. In this simple case, the ability to omit the middle operand is not
  626. especially useful.  When it becomes useful is when the first operand
  627. does, or may (if it is a macro argument), contain a side effect.  Then
  628. repeating the operand in the middle would perform the side effect
  629. twice.  Omitting the middle operand uses the value already computed
  630. without the undesirable effects of recomputing it.
  631. File: gcc.info,  Node: Long Long,  Next: Zero Length,  Prev: Conditionals,  Up: Extensions
  632. Double-Word Integers
  633. ====================
  634.    GNU C supports data types for integers that are twice as long as
  635. `long int'.  Simply write `long long int' for a signed integer, or
  636. `unsigned long long int' for an unsigned integer.
  637.    You can use these types in arithmetic like any other integer types.
  638. Addition, subtraction, and bitwise boolean operations on these types
  639. are open-coded on all types of machines.  Multiplication is open-coded
  640. if the machine supports fullword-to-doubleword a widening multiply
  641. instruction.  Division and shifts are open-coded only on machines that
  642. provide special support.  The operations that are not open-coded use
  643. special library routines that come with GNU CC.
  644.    There may be pitfalls when you use `long long' types for function
  645. arguments, unless you declare function prototypes.  If a function
  646. expects type `int' for its argument, and you pass a value of type `long
  647. long int', confusion will result because the caller and the subroutine
  648. will disagree about the number of bytes for the argument. Likewise, if
  649. the function expects `long long int' and you pass `int'.  The best way
  650. to avoid such problems is to use prototypes.
  651. File: gcc.info,  Node: Zero Length,  Next: Variable Length,  Prev: Long Long,  Up: Extensions
  652. Arrays of Length Zero
  653. =====================
  654.    Zero-length arrays are allowed in GNU C.  They are very useful as
  655. the last element of a structure which is really a header for a
  656. variable-length object:
  657.      struct line {
  658.        int length;
  659.        char contents[0];
  660.      };
  661.      
  662.      {
  663.        struct line *thisline = (struct line *)
  664.          malloc (sizeof (struct line) + this_length);
  665.        thisline->length = this_length;
  666.      }
  667.    In standard C, you would have to give `contents' a length of 1, which
  668. means either you waste space or complicate the argument to `malloc'.
  669. File: gcc.info,  Node: Variable Length,  Next: Macro Varargs,  Prev: Zero Length,  Up: Extensions
  670. Arrays of Variable Length
  671. =========================
  672.    Variable-length automatic arrays are allowed in GNU C.  These arrays
  673. are declared like any other automatic arrays, but with a length that is
  674. not a constant expression.  The storage is allocated at the point of
  675. declaration and deallocated when the brace-level is exited.  For
  676. example:
  677.      FILE *
  678.      concat_fopen (char *s1, char *s2, char *mode)
  679.      {
  680.        char str[strlen (s1) + strlen (s2) + 1];
  681.        strcpy (str, s1);
  682.        strcat (str, s2);
  683.        return fopen (str, mode);
  684.      }
  685.    Jumping or breaking out of the scope of the array name deallocates
  686. the storage.  Jumping into the scope is not allowed; you get an error
  687. message for it.
  688.    You can use the function `alloca' to get an effect much like
  689. variable-length arrays.  The function `alloca' is available in many
  690. other C implementations (but not in all).  On the other hand,
  691. variable-length arrays are more elegant.
  692.    There are other differences between these two methods.  Space
  693. allocated with `alloca' exists until the containing *function* returns.
  694. The space for a variable-length array is deallocated as soon as the
  695. array name's scope ends.  (If you use both variable-length arrays and
  696. `alloca' in the same function, deallocation of a variable-length array
  697. will also deallocate anything more recently allocated with `alloca'.)
  698.    You can also use variable-length arrays as arguments to functions:
  699.      struct entry
  700.      tester (int len, char data[len][len])
  701.      {
  702.        ...
  703.      }
  704.    The length of an array is computed once when the storage is allocated
  705. and is remembered for the scope of the array in case you access it with
  706. `sizeof'.
  707.    If you want to pass the array first and the length afterward, you can
  708. use a forward declaration in the parameter list--another GNU extension.
  709.      struct entry
  710.      tester (int len; char data[len][len], int len)
  711.      {
  712.        ...
  713.      }
  714.    The `int len' before the semicolon is a "parameter forward
  715. declaration", and it serves the purpose of making the name `len' known
  716. when the declaration of `data' is parsed.
  717.    You can write any number of such parameter forward declarations in
  718. the parameter list.  They can be separated by commas or semicolons, but
  719. the last one must end with a semicolon, which is followed by the "real"
  720. parameter declarations.  Each forward declaration must match a "real"
  721. declaration in parameter name and data type.
  722. File: gcc.info,  Node: Macro Varargs,  Next: Subscripting,  Prev: Variable Length,  Up: Extensions
  723. Macros with Variable Numbers of Arguments
  724. =========================================
  725.    In GNU C, a macro can accept a variable number of arguments, much as
  726. a function can.  The syntax for defining the macro looks much like that
  727. used for a function.  Here is an example:
  728.      #define eprintf(format, args...)  \
  729.       fprintf (stderr, format, ## args)
  730.    Here `args' is a "rest argument": it takes in zero or more
  731. arguments, as many as the call contains.  All of them plus the commas
  732. between them form the value of `args', which is substituted into the
  733. macro body where `args' is used.  Thus, we have these expansions:
  734.      eprintf ("%s:%d: ", input_file_name, line_number)
  735.      ==>
  736.      fprintf (stderr, "%s:%d: ", input_file_name, line_number)
  737. Note that the comma after the string constant comes from the definition
  738. of `eprintf', whereas the last comma comes from the value of `args'.
  739.    The reason for using `##' is to handle the case when `args' matches
  740. no arguments at all.  In this case, `args' has an empty value.  In this
  741. case, the second comma in the definition becomes an embarrassment: if
  742. it got through to the expansion of the macro, we would get something
  743. like this:
  744.      fprintf (stderr, "success!\n", )
  745. which is invalid C syntax.  `##' gets rid of the comma, so we get the
  746. following instead:
  747.      fprintf (stderr, "success!\n")
  748.    This is a special feature of the GNU C preprocessor: `##' adjacent
  749. to a rest argument discards the token on the other side of the `##', if
  750. the rest argument value is empty.
  751. File: gcc.info,  Node: Subscripting,  Next: Pointer Arith,  Prev: Macro Varargs,  Up: Extensions
  752. Non-Lvalue Arrays May Have Subscripts
  753. =====================================
  754.    Subscripting is allowed on arrays that are not lvalues, even though
  755. the unary `&' operator is not.  For example, this is valid in GNU C
  756. though not valid in other C dialects:
  757.      struct foo {int a[4];};
  758.      
  759.      struct foo f();
  760.      
  761.      bar (int index)
  762.      {
  763.        return f().a[index];
  764.      }
  765. File: gcc.info,  Node: Pointer Arith,  Next: Initializers,  Prev: Subscripting,  Up: Extensions
  766. Arithmetic on `void'- and Function-Pointers
  767. ===========================================
  768.    In GNU C, addition and subtraction operations are supported on
  769. pointers to `void' and on pointers to functions.  This is done by
  770. treating the size of a `void' or of a function as 1.
  771.    A consequence of this is that `sizeof' is also allowed on `void' and
  772. on function types, and returns 1.
  773.    The option `-Wpointer-arith' requests a warning if these extensions
  774. are used.
  775. File: gcc.info,  Node: Initializers,  Next: Constructors,  Prev: Pointer Arith,  Up: Extensions
  776. Non-Constant Initializers
  777. =========================
  778.    The elements of an aggregate initializer for an automatic variable
  779. are not required to be constant expressions in GNU C.  Here is an
  780. example of an initializer with run-time varying elements:
  781.      foo (float f, float g)
  782.      {
  783.        float beat_freqs[2] = { f-g, f+g };
  784.        ...
  785.      }
  786. File: gcc.info,  Node: Constructors,  Next: Labeled Elements,  Prev: Initializers,  Up: Extensions
  787. Constructor Expressions
  788. =======================
  789.    GNU C supports constructor expressions.  A constructor looks like a
  790. cast containing an initializer.  Its value is an object of the type
  791. specified in the cast, containing the elements specified in the
  792. initializer.
  793.    Usually, the specified type is a structure.  Assume that `struct
  794. foo' and `structure' are declared as shown:
  795.      struct foo {int a; char b[2];} structure;
  796. Here is an example of constructing a `struct foo' with a constructor:
  797.      structure = ((struct foo) {x + y, 'a', 0});
  798. This is equivalent to writing the following:
  799.      {
  800.        struct foo temp = {x + y, 'a', 0};
  801.        structure = temp;
  802.      }
  803.    You can also construct an array.  If all the elements of the
  804. constructor are (made up of) simple constant expressions, suitable for
  805. use in initializers, then the constructor is an lvalue and can be
  806. coerced to a pointer to its first element, as shown here:
  807.      char **foo = (char *[]) { "x", "y", "z" };
  808.    Array constructors whose elements are not simple constants are not
  809. very useful, because the constructor is not an lvalue.  There are only
  810. two valid ways to use it: to subscript it, or initialize an array
  811. variable with it.  The former is probably slower than a `switch'
  812. statement, while the latter does the same thing an ordinary C
  813. initializer would do.  Here is an example of subscripting an array
  814. constructor:
  815.      output = ((int[]) { 2, x, 28 }) [input];
  816.    Constructor expressions for scalar types and union types are is also
  817. allowed, but then the constructor expression is equivalent to a cast.
  818. File: gcc.info,  Node: Labeled Elements,  Next: Cast to Union,  Prev: Constructors,  Up: Extensions
  819. Labeled Elements in Initializers
  820. ================================
  821.    Standard C requires the elements of an initializer to appear in a
  822. fixed order, the same as the order of the elements in the array or
  823. structure being initialized.
  824.    In GNU C you can give the elements in any order, specifying the array
  825. indices or structure field names they apply to.
  826.    To specify an array index, write `[INDEX]' before the element value.
  827.  For example,
  828.      int a[6] = { [4] 29, [2] 15 };
  829. is equivalent to
  830.      int a[6] = { 0, 0, 15, 0, 29, 0 };
  831. The index values must be constant expressions, even if the array being
  832. initialized is automatic.
  833.    In a structure initializer, specify the name of a field to initialize
  834. with `FIELDNAME:' before the element value.  For example, given the
  835. following structure,
  836.      struct point { int x, y; };
  837. the following initialization
  838.      struct point p = { y: yvalue, x: xvalue };
  839. is equivalent to
  840.      struct point p = { xvalue, yvalue };
  841.    You can also use an element label when initializing a union, to
  842. specify which element of the union should be used.  For example,
  843.      union foo { int i; double d; };
  844.      
  845.      union foo f = { d: 4 };
  846. will convert 4 to a `double' to store it in the union using the second
  847. element.  By contrast, casting 4 to type `union foo' would store it
  848. into the union as the integer `i', since it is an integer.  (*Note Cast
  849. to Union::.)
  850.    You can combine this technique of naming elements with ordinary C
  851. initialization of successive elements.  Each initializer element that
  852. does not have a label applies to the next consecutive element of the
  853. array or structure.  For example,
  854.      int a[6] = { [1] v1, v2, [4] v4 };
  855. is equivalent to
  856.      int a[6] = { 0, v1, v2, 0, v4, 0 };
  857.    Labeling the elements of an array initializer is especially useful
  858. when the indices are characters or belong to an `enum' type. For
  859. example:
  860.      int whitespace[256]
  861.        = { [' '] 1, ['\t'] 1, ['\h'] 1,
  862.            ['\f'] 1, ['\n'] 1, ['\r'] 1 };
  863. File: gcc.info,  Node: Case Ranges,  Next: Function Attributes,  Prev: Cast to Union,  Up: Extensions
  864. Case Ranges
  865. ===========
  866.    You can specify a range of consecutive values in a single `case'
  867. label, like this:
  868.      case LOW ... HIGH:
  869. This has the same effect as the proper number of individual `case'
  870. labels, one for each integer value from LOW to HIGH, inclusive.
  871.    This feature is especially useful for ranges of ASCII character
  872. codes:
  873.      case 'A' ... 'Z':
  874.    *Be careful:* Write spaces around the `...', for otherwise it may be
  875. parsed wrong when you use it with integer values.  For example, write
  876. this:
  877.      case 1 ... 5:
  878. rather than this:
  879.      case 1...5:
  880. File: gcc.info,  Node: Cast to Union,  Next: Case Ranges,  Prev: Labeled Elements,  Up: Extensions
  881. Cast to a Union Type
  882. ====================
  883.    A cast to union type is like any other cast, except that the type
  884. specified is a union type.  You can specify the type either with `union
  885. TAG' or with a typedef name.
  886.    The types that may be cast to the union type are those of the members
  887. of the union.  Thus, given the following union and variables:
  888.      union foo { int i; double d; };
  889.      int x;
  890.      double y;
  891. both `x' and `y' can be cast to type `union' foo.
  892.    Using the cast as the right-hand side of an assignment to a variable
  893. of union type is equivalent to storing in a member of the union:
  894.      union foo u;
  895.      ...
  896.      u = (union foo) x  ==  u.i = x
  897.      u = (union foo) y  ==  u.d = y
  898.    You can also use the union cast as a function argument:
  899.      void hack (union foo);
  900.      ...
  901.      hack ((union foo) x);
  902.