Add get_qualified_type() and solve several problem with missing qualifiers, which...
[cparser] / type.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #ifndef TYPE_H
21 #define TYPE_H
22
23 #include <stdio.h>
24 #include <stdbool.h>
25 #include "ast.h"
26 #include "symbol.h"
27
28 /* note that the constant values represent the rank of the types as defined
29  * in § 6.3.1 */
30 typedef enum atomic_type_kind_t {
31         ATOMIC_TYPE_INVALID = 0,
32         ATOMIC_TYPE_VOID,
33         ATOMIC_TYPE_CHAR,
34         ATOMIC_TYPE_SCHAR,
35         ATOMIC_TYPE_UCHAR,
36         ATOMIC_TYPE_SHORT,
37         ATOMIC_TYPE_USHORT,
38         ATOMIC_TYPE_INT,
39         ATOMIC_TYPE_UINT,
40         ATOMIC_TYPE_LONG,
41         ATOMIC_TYPE_ULONG,
42         ATOMIC_TYPE_LONGLONG,
43         ATOMIC_TYPE_ULONGLONG,
44         ATOMIC_TYPE_FLOAT,
45         ATOMIC_TYPE_DOUBLE,
46         ATOMIC_TYPE_LONG_DOUBLE,
47         ATOMIC_TYPE_BOOL,
48
49         ATOMIC_TYPE_LAST = ATOMIC_TYPE_BOOL
50 } atomic_type_kind_t;
51
52 typedef enum atomic_type_flag_t {
53         ATOMIC_TYPE_FLAG_NONE       = 0,
54         ATOMIC_TYPE_FLAG_SIGNED     = 1 << 0,
55         ATOMIC_TYPE_FLAG_INTEGER    = 1 << 1,
56         ATOMIC_TYPE_FLAG_FLOAT      = 1 << 2,
57         ATOMIC_TYPE_FLAG_ARITHMETIC = 1 << 3,
58         ATOMIC_TYPE_FLAG_COMPLEX    = 1 << 4,
59 } atomic_type_flag_t;
60
61 typedef enum type_qualifier_t {
62         TYPE_QUALIFIER_NONE     = 0,
63         TYPE_QUALIFIER_CONST    = 1 << 0,
64         TYPE_QUALIFIER_RESTRICT = 1 << 1,
65         TYPE_QUALIFIER_VOLATILE = 1 << 2,
66         /* microsoft extended qualifiers */
67         TYPE_QUALIFIER_W64      = 1 << 3,
68         TYPE_QUALIFIER_PTR32    = 1 << 4,
69         TYPE_QUALIFIER_PTR64    = 1 << 5,
70         TYPE_QUALIFIER_SPTR     = 1 << 6,
71         TYPE_QUALIFIER_UPTR     = 1 << 7,
72 } type_qualifier_t;
73 typedef unsigned short type_qualifiers_t;
74
75 typedef struct type_base_t           type_base_t;
76 typedef struct atomic_type_t         atomic_type_t;
77 typedef struct complex_type_t        complex_type_t;
78 typedef struct imaginary_type_t      imaginary_type_t;
79 typedef struct pointer_type_t        pointer_type_t;
80 typedef struct function_parameter_t  function_parameter_t;
81 typedef struct function_type_t       function_type_t;
82 typedef struct compound_type_t       compound_type_t;
83 typedef struct enum_type_t           enum_type_t;
84 typedef struct builtin_type_t        builtin_type_t;
85 typedef struct array_type_t          array_type_t;
86 typedef struct typedef_type_t        typedef_type_t;
87 typedef struct bitfield_type_t       bitfield_type_t;
88 typedef struct typeof_type_t         typeof_type_t;
89 typedef union  type_t                type_t;
90
91 void init_types(void);
92 void exit_types(void);
93
94 void print_type(const type_t *type);
95
96 /**
97  * prints a human readable form of @p type. prints an abstract typename
98  * if symbol is NULL
99  */
100 void print_type_ext(const type_t *type, const symbol_t *symbol,
101                     const scope_t *scope);
102
103 void print_type_qualifiers(type_qualifiers_t qualifiers);
104
105 void print_enum_definition(const declaration_t *declaration);
106 void print_compound_definition(const declaration_t *declaration);
107
108 /**
109  * set output stream for the type printer
110  */
111 void type_set_output(FILE *out);
112
113 void inc_type_visited(void);
114
115 /**
116  * returns true if type contains integer numbers
117  */
118 bool is_type_integer(const type_t *type);
119
120 /**
121  * Returns true if the given type is an enum type.
122  */
123 bool is_type_enum(const type_t *type);
124
125 /**
126  * return true if type contains signed numbers
127  */
128 bool is_type_signed(const type_t *type);
129
130 /**
131  * returns true if type contains floating point numbers
132  */
133 bool is_type_float(const type_t *type);
134
135 /**
136  * returns true if type contains complex numbers
137  */
138 bool is_type_complex(const type_t *type);
139
140 bool is_type_real(const type_t *type);
141
142 /**
143  * returns true if the type is valid. A type is valid if it contains no
144  * unresolved references anymore and is not of TYPE_INVALID.
145  */
146 bool type_valid(const type_t *type);
147
148 /**
149  * returns true if the type is an arithmetic type (§6.2.5 clause 18)
150  */
151 bool is_type_arithmetic(const type_t *type);
152
153 /**
154  * returns true if the type is a scalar type (§6.2.5 clause 21)
155  */
156 bool is_type_scalar(const type_t *type);
157
158 bool is_type_incomplete(const type_t *type);
159
160 bool is_type_object(const type_t *type);
161
162 bool types_compatible(const type_t *type1, const type_t *type2);
163
164 type_t *get_unqualified_type(type_t *type);
165 type_t *get_qualified_type(type_t*, type_qualifiers_t);
166 type_t *skip_typeref(type_t *type);
167
168 /**
169  * returns size of an atomic type kind in bytes
170  */
171 unsigned get_atomic_type_size(atomic_type_kind_t kind);
172
173 /**
174  * returns alignment of an atomic type kind in bytes
175  */
176 unsigned get_atomic_type_alignment(atomic_type_kind_t kind);
177
178 /**
179  * returns flags of an atomic type kind
180  */
181 unsigned get_atomic_type_flags(atomic_type_kind_t kind);
182
183 atomic_type_kind_t get_intptr_kind(void);
184 atomic_type_kind_t get_uintptr_kind(void);
185
186 /**
187  * Find the atomic type kind representing a given size (signed).
188  */
189 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size);
190
191 /**
192  * Find the atomic type kind representing a given size (unsigned).
193  */
194 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size);
195
196 #endif