gcc config
[prop.git] / prop-src / basics.ph
blob833f49c978d8ee588a82c682f310218fe2416636
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 //  This file contains the basic definitions used in the 
4 //  Prop -> C++ translator, including various flags and memory
5 //  pools definitions.
6 //
7 ///////////////////////////////////////////////////////////////////////////////
8 #ifndef basic_definitions_h
9 #define basic_definitions_h
11 #include <new>
12 #include <stdarg.h>
13 #include <AD/generic/generic.h>    // generic definitions from the library
14 #include <AD/memory/mempool.h>     // memory pool
15 #include <AD/memory/strpool.h>     // string pool
16 #include <iostream>
18 ///////////////////////////////////////////////////////////////////////////////
20 //  The following is used to deal with the new ANSI conformance 
21 //  of the "for" statement involving scoping of variables defined in
22 //  an "if".   Variables defined inside a for statement are not visible
23 //  only with the scope of the construct.
25 ///////////////////////////////////////////////////////////////////////////////
26 #define for if (0) ; else for
28 ///////////////////////////////////////////////////////////////////////////////
30 //  Two memory pools are kept for fast memory allocation. 
31 //  Memory allocated from the global pool is persistent during the run
32 //  of the translator, while memory allocated from the local pool can
33 //  be deallocated in a stack-like fashion.  Also, there is a pool for
34 //  string objects.  These are persistent like the global pool.
36 ///////////////////////////////////////////////////////////////////////////////
37 extern MemPool    mem_pool;           // memory pool for local objects
38 extern MemPool    global_pool;        // memory pool for global objects
39 extern StringPool str_pool;           // string pool for strings
41 ///////////////////////////////////////////////////////////////////////////////
43 //  Class 'MEM' is a memory management class.  Objects deriving from 
44 //  this class are allocates from the memory pools.  By default,
45 //  the local memory pool is used.  The methods 'use_global_pools'
46 //  and 'use_local_pools' can be used to switch between the global
47 //  and local pools.
49 ///////////////////////////////////////////////////////////////////////////////
50 class MEM {
51 public:
52           void * operator new (size_t);     // allocation member
53           // void   operator delete(void *);   // disable deletion
54    static void   use_global_pools();        // switch to the global pool
55    static void   use_local_pools();         // switch to the local pol
56                                             // allocate a vector from the pool
59 ///////////////////////////////////////////////////////////////////////////////
61 //  The input/output/logging streams and the output file name(if applicable).
63 ///////////////////////////////////////////////////////////////////////////////
64 extern istream * input_stream;          // The current input stream
65 extern ostream * output_stream;         // The current output stream
66 extern ostream * log_stream;            // The current logging stream
67 extern ostream&  open_logfile();        // Open the logfile
68 extern ostream*  open_output_file(const char []); // open a file for output
69 extern void encode_string(char *, const char *); // Encode a string.
71 ///////////////////////////////////////////////////////////////////////////////
73 //  Errors and messages routines.
75 ///////////////////////////////////////////////////////////////////////////////
76 extern int  errors;                        // number of errors found
77 ostream& pr_msg   (const char *, va_list); // generic print message routine
78 ostream& error    (const char *, ...);     // error message
79 ostream& msg      (const char *, ...);     // generic message
80 ostream& debug_msg(const char *, ...);     // debugging message
81 void     bug      (const char *, ...);     // abort and generate a bug report
83 ///////////////////////////////////////////////////////////////////////////////
85 //  Polymorphic lists, pairs and identifiers.  These are used throughout the
86 //  rest of the translator.
88 ///////////////////////////////////////////////////////////////////////////////
89 datatype List<T> : public MEM :: inline = #[] | #[ T ... List<T> ]
90 and      Pair<A,B> : public MEM :: inline = pair { fst : A, snd : B }
91 where type Id  = const char *  // Identifier
92 and        Ids = List<Id>      // Identifier list
95 ///////////////////////////////////////////////////////////////////////////////
97 //  Macro to iterate over a list.
99 ///////////////////////////////////////////////////////////////////////////////
100 #define APPEND_(a,b)  a ## b
101 #define NEWVAR2_(a,b) APPEND_(a,b)
102 #define NEWVAR(a)    NEWVAR2_(a,__LINE__)
103 #define for_each(T,i,l)                          \
104    T i;                                          \
105    for (List(T) NEWVAR(_i_) = l;                 \
106         NEWVAR(_i_) && (i = NEWVAR(_i_)->_1, 1); \
107         NEWVAR(_i_) = NEWVAR(_i_)->_2)
109 ///////////////////////////////////////////////////////////////////////////////
111 //  We keep track of the current source location in the following global
112 //  variables.  These are updated continuously by the parser, lexer and the
113 //  semantic processing routines.
115 ///////////////////////////////////////////////////////////////////////////////
116 extern int line;        // the current line number
117 extern int first_line;  // the first line of the current construct
118 extern Id  file;        // the current input file.
120 ///////////////////////////////////////////////////////////////////////////////
122 //  The following class, Loc, is used to keep track of the source location.
123 //  Objects derived from this class will contain the source location in
124 //  which it is constructed; useful for AST nodes.
126 ///////////////////////////////////////////////////////////////////////////////
127 struct Loc : public MEM {
128 public:
129    Id file_name;
130    int begin_line, end_line;
131    Loc();
132    Loc(int, int);
133    inline const Loc * loc() const { return this; }
134    void set_loc() const;
137 #endif