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