1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #ifndef _PARSEPROTO_H 28 #define _PARSEPROTO_H 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 /* 35 * DECL - parse C type declarations. 36 * 37 * 1) Does not understand struct, union or enum definitions. 38 * 2) Does not understand auto, static, extern or typedef storage class 39 * specifiers. 40 * 3) Does not support initialization. 41 * 4) Does not support type definition. 42 * 5) Only understands array dimension specified as constant decimal 43 * integer or identifier. 44 * 45 * Supported Operations 46 * 47 * decl_Parse convert string to a decl_t. 48 * decl_Destroy Free space associated with a (previously returned) 49 * decl_t. The function follows the argument list. 50 * decl_SetName set identifier. 51 * decl_GetName return identifier. 52 * decl_ToString convert a (previously returned) decl_t into a 53 * printable representation. 54 * decl_GetArgLength return length of argument list. 55 * decl_GetNext return the next decl_t associated with the given 56 * decl_t. 57 * decl_GetDeclSpec return the declaration specifier. 58 * decl_GetDSName return identifier associated with a 59 * declaration specifier. 60 * decl_GetType return the type_t associated with a decl_t. 61 * decl_IsVarargs return true if the given decl_t is a varargs function. 62 * decl_IsFunction return true if the given decl_t is a function. 63 * 64 * type_GetNext return the next type_t associated with a given type_t. 65 * type_IsArray return true if the given type_t is an array. 66 * type_GetArraySize return size of array. 67 * 68 * type_IsPtrTo return true if the given type_t is a pointer to ... . 69 * type_GetPtrToTypeQual return type qualifiers for a pointer to ... . 70 * 71 * type_IsFunction return true if the given type_t is a function. 72 * type_GetArgLength return length of argument list. 73 * type_IsVarargs return true if function signature includes ... . 74 * type_GetArg return the decl_t associated with a given type_t. 75 * type_IsPtrFun return true if the given type_t is a pointer* to a 76 * function. 77 */ 78 79 /* Include Files */ 80 81 #include <stdio.h> 82 #include <ctype.h> 83 #include <string.h> 84 85 /* Macros and Constants */ 86 87 #define loop for (;;) 88 89 #define STT_isvoid(s) ((s) & (TS_VOID)) 90 #define STT_isfloat(s) ((s) & (TS_FLOAT | TS_DOUBLE)) 91 #define STT_ischar(s) ((s) & (TS_CHAR)) 92 #define STT_isint(s) ((s) & \ 93 (TS_SHORT | TS_INT | TS_LONG | TS_LONGLONG | TS_CHAR)) 94 #define STT_isarith(s) (STT_isfloat(s) || STT_isint(s)) 95 #define STT_isbasic(s) (STT_isarith(s) || STT_isvoid(s)) 96 #define STT_isderived(s) ((s) & (TS_STRUCT | TS_UNION | TS_ENUM | TS_TYPEDEF)) 97 #define STT_has_explicit_sign(s) ((s) & (TS_SIGNED | TS_UNSIGNED)) 98 99 /* Data Declarations */ 100 101 /* 102 * The overall type encoding is thus: 103 * 104 * decl_t encodes a declaration which consists of: 105 * identifier 106 * declaration specifier (storage class specifier, type specifier, 107 * type qualifier) 108 * type modifiers (array, function, pointer to) 109 * ancillary (varargs?, argument list) 110 * 111 * The argument list is an ordered, NULL terminated, linked list. 112 * 113 * An empty argument list (== NULL) indicates an unknown argument 114 * list, i.e. "()". 115 * 116 * declaration specifiers are encoded as bits in an enum (stt_t). 117 * 118 * type modifiers are encoded as a linked list of variant records, 119 * i.e. "array of ..." 120 * "function returning ..." and "pointer to ...". 121 * 122 * An empty list of type modifiers (== NULL) indicates a "plain" type. 123 * 124 * 125 * OK, here goes some ASCII art... 126 * 127 * 128 * base object 129 * | 130 * | 131 * V 132 * 133 * ---------- ---------- ---------- ---------- 134 * | | | | | | | | 135 * | decl_t | --> | type_t | --> | type_t | ... | type_t | --> NULL 136 * | | | | |(DD_FUN)| | | 137 * ---------- ---------- ---------- ---------- 138 * | | 139 * | | 140 * V V 141 * A ---------- ---------- 142 * NULL r | | | | 143 * g | decl_t | --> | type_t | ... --> NULL 144 * u | | | | 145 * m ---------- ---------- 146 * e | 147 * n | 148 * t V 149 * ---------- 150 * L | | 151 * i | decl_t | ... --> NULL 152 * s | | 153 * t ---------- 154 * 155 * ... 156 * 157 * | 158 * | 159 * V 160 * 161 * NULL 162 */ 163 164 /* 165 * The encoding of a declaration specifier is done primarily with an 166 * stt_t type. 167 * This type must support bit-wise operations. 168 */ 169 170 typedef enum { 171 SCS_MASK = 0x000000ff, /* storage class specifiers */ 172 SCS_NONE = 0x00000000, 173 SCS_REGISTER = 0x00000001, 174 SCS_TYPEDEF = 0x00000002, 175 SCS_EXTERN = 0x00000004, 176 SCS_AUTO = 0x00000008, 177 SCS_STATIC = 0x00000010, 178 179 TS_MASK = 0x00ffff00, /* type specifiers */ 180 TS_NO_TS = 0x00000000, 181 TS_CHAR = 0x00000100, 182 TS_SHORT = 0x00000200, 183 TS_INT = 0x00000400, 184 TS_LONG = 0x00000800, 185 TS_SIGNED = 0x00001000, 186 TS_UNSIGNED = 0x00002000, 187 TS_ENUM = 0x00004000, 188 TS_FLOAT = 0x00010000, 189 TS_DOUBLE = 0x00020000, 190 TS_STRUCT = 0x00040000, 191 TS_UNION = 0x00080000, 192 TS_TYPEDEF = 0x00100000, 193 TS_VOID = 0x00200000, 194 TS_LONGLONG = 0x00400000, /* non-ANSI type: long long */ 195 196 TQ_MASK = 0x0f000000, /* type qualifiers */ 197 TQ_NONE = 0x00000000, 198 TQ_CONST = 0x01000000, 199 TQ_VOLATILE = 0x02000000, 200 TQ_RESTRICT = 0x04000000, 201 TQ_RESTRICT_KYWD = 0x08000000 202 } stt_t; 203 204 typedef enum { /* declarator options */ 205 DD_NONE = 0, 206 DD_ARY = 1, /* array of [size] ... */ 207 DD_FUN = 2, /* function [taking and] returning ... */ 208 DD_PTR = 3 /* [tq] pointer to ... */ 209 } decl_type_t; 210 211 typedef enum { 212 DTS_DECL = 0, 213 DTS_CAST = 1, 214 DTS_RET = 3 215 } decl_dts_t; 216 217 typedef struct { 218 stt_t ds_stt; /* scs|ts|tq */ 219 char *ds_id; /* id for struct|union|enum|typedef */ 220 } decl_spec_t; 221 222 typedef struct _declarator decl_t; 223 224 typedef struct _type { 225 struct _type *t_next; /* next type_t or NULL */ 226 decl_type_t t_dt; /* oneof DD_* */ 227 /* DD_FUN */ 228 int t_nargs; /* number of arguments */ 229 int t_ellipsis; /* a varargs? */ 230 decl_t *t_args; /* list of arguments */ 231 /* DD_PTR */ 232 stt_t t_stt; /* type qualifier, TQ_* */ 233 /* DD_ARY */ 234 char *t_sizestr; /* size as a string */ 235 } type_t; 236 237 struct _declarator { 238 char *d_name; /* name of declarator */ 239 decl_spec_t *d_ds; /* ts|scs|tq */ 240 type_t *d_type; /* list of attributes or NULL */ 241 int d_ellipsis; /* a varargs? */ 242 decl_t *d_next; /* next link in chain (arglist) */ 243 }; 244 245 /* External Declarations */ 246 247 extern char *declspec_ToString(char *, decl_spec_t *); 248 249 extern void decl_Destroy(decl_t *); 250 extern int decl_GetArgLength(decl_t *); 251 extern decl_t *decl_SetName(decl_t *, char *); 252 extern char *decl_GetName(decl_t *); 253 extern type_t *decl_GetType(decl_t *); 254 extern int decl_IsVarargs(decl_t *dp); 255 extern char *decl_ToString(char *, decl_dts_t, decl_t *, 256 const char *); 257 extern const char *decl_Parse(char *, decl_t **); 258 extern void decl_GetTraceInfo(decl_t *, char *, char *, decl_t **); 259 extern char *decl_ToFormal(decl_t *); 260 extern int type_IsArray(type_t *); 261 extern int type_IsPtrTo(type_t *); 262 extern int type_IsFunction(type_t *); 263 extern int type_IsVarargs(type_t *); 264 extern int type_IsPtrFun(type_t *); 265 extern decl_t *decl_AddArgNames(decl_t *); 266 267 #ifdef __cplusplus 268 } 269 #endif 270 271 #endif /* _PARSEPROTO_H */ 272