xref: /titanic_50/usr/src/tools/ndrgen/ndr_print.c (revision a0b6e447978c306e15941d158bf6939a42ed2726)
1d0e51869Samw /*
2d0e51869Samw  * CDDL HEADER START
3d0e51869Samw  *
4d0e51869Samw  * The contents of this file are subject to the terms of the
5d0e51869Samw  * Common Development and Distribution License (the "License").
6d0e51869Samw  * You may not use this file except in compliance with the License.
7d0e51869Samw  *
8d0e51869Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9d0e51869Samw  * or http://www.opensolaris.org/os/licensing.
10d0e51869Samw  * See the License for the specific language governing permissions
11d0e51869Samw  * and limitations under the License.
12d0e51869Samw  *
13d0e51869Samw  * When distributing Covered Code, include this CDDL HEADER in each
14d0e51869Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15d0e51869Samw  * If applicable, add the following below this CDDL HEADER, with the
16d0e51869Samw  * fields enclosed by brackets "[]" replaced with your own identifying
17d0e51869Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
18d0e51869Samw  *
19d0e51869Samw  * CDDL HEADER END
20d0e51869Samw  */
21d0e51869Samw 
22d0e51869Samw /*
23*a0b6e447SAlan Wright  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24d0e51869Samw  * Use is subject to license terms.
25d0e51869Samw  */
26d0e51869Samw 
27d0e51869Samw #include "ndrgen.h"
28d0e51869Samw #include "y.tab.h"
29d0e51869Samw 
30d0e51869Samw 
31d0e51869Samw static void print_declaration(ndr_node_t *);
32d0e51869Samw static void print_advice_list(ndr_node_t *);
33d0e51869Samw static void print_node_list(ndr_node_t *);
34d0e51869Samw 
35d0e51869Samw 
36d0e51869Samw void
tdata_dump(void)37d0e51869Samw tdata_dump(void)
38d0e51869Samw {
39d0e51869Samw 	print_node_list(construct_list);
40d0e51869Samw }
41d0e51869Samw 
42d0e51869Samw void
print_node(ndr_node_t * np)43d0e51869Samw print_node(ndr_node_t *np)
44d0e51869Samw {
45d0e51869Samw 	char		*nm;
46d0e51869Samw 
47d0e51869Samw 	if (!np) {
48d0e51869Samw 		(void) printf("<null>");
49d0e51869Samw 		return;
50d0e51869Samw 	}
51d0e51869Samw 
52d0e51869Samw 	switch (np->label) {
53d0e51869Samw 	case ALIGN_KW:		nm = "align";		break;
54d0e51869Samw 	case STRUCT_KW:		nm = "struct";		break;
55d0e51869Samw 	case UNION_KW:		nm = "union";		break;
56d0e51869Samw 	case TYPEDEF_KW:	nm = "typedef";		break;
57d0e51869Samw 	case INTERFACE_KW:	nm = "interface";	break;
58d0e51869Samw 	case IN_KW:		nm = "in";		break;
59d0e51869Samw 	case OUT_KW:		nm = "out";		break;
60d0e51869Samw 	case SIZE_IS_KW:	nm = "size_is";		break;
61d0e51869Samw 	case LENGTH_IS_KW:	nm = "length_is";	break;
62d0e51869Samw 	case STRING_KW:		nm = "string";		break;
63d0e51869Samw 	case TRANSMIT_AS_KW:	nm = "transmit_as";	break;
64d0e51869Samw 	case OPERATION_KW:	nm = "operation";	break;
65d0e51869Samw 	case UUID_KW:		nm = "uuid";		break;
66d0e51869Samw 	case _NO_REORDER_KW:	nm = "_no_reorder";	break;
67d0e51869Samw 	case EXTERN_KW:		nm = "extern";		break;
68d0e51869Samw 	case ARG_IS_KW:		nm = "arg_is";		break;
69d0e51869Samw 	case CASE_KW:		nm = "case";		break;
70d0e51869Samw 	case DEFAULT_KW:	nm = "default";		break;
71d0e51869Samw 	case BASIC_TYPE:	nm = "<btype>";		break;
72d0e51869Samw 	case TYPENAME:		nm = "<tname>";		break;
73d0e51869Samw 	case IDENTIFIER:	nm = "<ident>";		break;
74d0e51869Samw 	case INTEGER:		nm = "<intg>";		break;
75d0e51869Samw 	case STRING:		nm = "<string>";	break;
76d0e51869Samw 	case STAR:		nm = "<*>";		break;
77d0e51869Samw 	case LB:		nm = "<[>";		break;
78d0e51869Samw 	case LP:		nm = "<(>";		break;
79d0e51869Samw 	case L_MEMBER:		nm = "<member>";	break;
80d0e51869Samw 	default:
81d0e51869Samw 		(void) printf("<<lab=%d>>", np->label);
82d0e51869Samw 		return;
83d0e51869Samw 	}
84d0e51869Samw 
85d0e51869Samw 	switch (np->label) {
86d0e51869Samw 	case STRUCT_KW:
87d0e51869Samw 	case UNION_KW:
88d0e51869Samw 	case TYPEDEF_KW:
89d0e51869Samw 		(void) printf("\n");
90d0e51869Samw 		if (np->n_c_advice) {
91d0e51869Samw 			print_advice_list(np->n_c_advice);
92d0e51869Samw 			(void) printf("\n");
93d0e51869Samw 		}
94d0e51869Samw 		(void) printf("%s ", nm);
95d0e51869Samw 		print_node(np->n_c_typename);
96d0e51869Samw 		(void) printf(" {\n");
97d0e51869Samw 		print_node_list(np->n_c_members);
98d0e51869Samw 		(void) printf("};\n");
99d0e51869Samw 		break;
100d0e51869Samw 
101d0e51869Samw 	case IN_KW:
102d0e51869Samw 	case OUT_KW:
103d0e51869Samw 	case STRING_KW:
104d0e51869Samw 	case DEFAULT_KW:
105d0e51869Samw 	case _NO_REORDER_KW:
106d0e51869Samw 	case EXTERN_KW:
107d0e51869Samw 		(void) printf("%s", nm);
108d0e51869Samw 		break;
109d0e51869Samw 
110d0e51869Samw 	case ALIGN_KW:
111d0e51869Samw 		/*
112d0e51869Samw 		 * Don't output anything for default alignment.
113d0e51869Samw 		 */
114d0e51869Samw 		if ((np->n_a_arg == NULL) || (np->n_a_arg->n_int == 0))
115d0e51869Samw 			break;
116d0e51869Samw 		(void) printf("%s(", nm);
117d0e51869Samw 		print_node(np->n_a_arg);
118d0e51869Samw 		(void) printf(")");
119d0e51869Samw 		break;
120d0e51869Samw 
121d0e51869Samw 	case SIZE_IS_KW:
122d0e51869Samw 	case LENGTH_IS_KW:
123*a0b6e447SAlan Wright 		(void) printf("%s(", nm);
124*a0b6e447SAlan Wright 		print_field_attr(np);
125*a0b6e447SAlan Wright 		(void) printf(")");
126*a0b6e447SAlan Wright 		break;
127*a0b6e447SAlan Wright 
128*a0b6e447SAlan Wright 	case INTERFACE_KW:
129d0e51869Samw 	case TRANSMIT_AS_KW:
130d0e51869Samw 	case ARG_IS_KW:
131d0e51869Samw 	case CASE_KW:
132d0e51869Samw 	case OPERATION_KW:
133d0e51869Samw 	case UUID_KW:
134d0e51869Samw 		(void) printf("%s(", nm);
135d0e51869Samw 		print_node(np->n_a_arg);
136d0e51869Samw 		(void) printf(")");
137d0e51869Samw 		break;
138d0e51869Samw 
139d0e51869Samw 	case BASIC_TYPE:
140d0e51869Samw 	case TYPENAME:
141d0e51869Samw 	case IDENTIFIER:
142d0e51869Samw 		(void) printf("%s", np->n_sym->name);
143d0e51869Samw 		break;
144d0e51869Samw 
145d0e51869Samw 	case INTEGER:
146d0e51869Samw 		(void) printf("%ld", np->n_int);
147d0e51869Samw 		break;
148d0e51869Samw 
149d0e51869Samw 	case STRING:
150d0e51869Samw 		(void) printf("\"%s\"", np->n_str);
151d0e51869Samw 		break;
152d0e51869Samw 
153d0e51869Samw 	case STAR:
154d0e51869Samw 		(void) printf("*");
155d0e51869Samw 		print_node(np->n_d_descend);
156d0e51869Samw 		break;
157d0e51869Samw 
158d0e51869Samw 	case LB:
159d0e51869Samw 		print_node(np->n_d_descend);
160d0e51869Samw 		(void) printf("[");
161d0e51869Samw 		if (np->n_d_dim)
162d0e51869Samw 			print_node(np->n_d_dim);
163d0e51869Samw 		(void) printf("]");
164d0e51869Samw 		break;
165d0e51869Samw 
166d0e51869Samw 	case LP:
167d0e51869Samw 		(void) printf("(");
168d0e51869Samw 		print_node(np->n_d_descend);
169d0e51869Samw 		(void) printf(")");
170d0e51869Samw 		break;
171d0e51869Samw 
172d0e51869Samw 	case L_MEMBER:
173d0e51869Samw 		if (np->n_m_advice) {
174d0e51869Samw 			(void) printf("    ");
175d0e51869Samw 			print_advice_list(np->n_m_advice);
176d0e51869Samw 			(void) printf("\n");
177d0e51869Samw 		}
178d0e51869Samw 		(void) printf("\t");
179d0e51869Samw 		print_declaration(np);
180d0e51869Samw 		(void) printf(";\n");
181d0e51869Samw 		break;
182d0e51869Samw 
183d0e51869Samw 	default:
184d0e51869Samw 		return;
185d0e51869Samw 	}
186d0e51869Samw }
187d0e51869Samw 
188*a0b6e447SAlan Wright /*
189*a0b6e447SAlan Wright  * Field attributes are used to specify the size of an array, or the portion
190*a0b6e447SAlan Wright  * of the array, that contains valid data, which is done by associating
191*a0b6e447SAlan Wright  * another parameter with the array that contains the sizing information.
192*a0b6e447SAlan Wright  *
193*a0b6e447SAlan Wright  * Supports formats such as size_is(x) or size_is(x / 2).  The supported
194*a0b6e447SAlan Wright  * operators are:
195*a0b6e447SAlan Wright  *
196*a0b6e447SAlan Wright  * 	* / % + - & | ^
197*a0b6e447SAlan Wright  */
198*a0b6e447SAlan Wright void
print_field_attr(ndr_node_t * np)199*a0b6e447SAlan Wright print_field_attr(ndr_node_t *np)
200*a0b6e447SAlan Wright {
201*a0b6e447SAlan Wright 	static char	*valid = "*/%+-&|^";
202*a0b6e447SAlan Wright 	ndr_node_t	*arg;
203*a0b6e447SAlan Wright 	char		*name;
204*a0b6e447SAlan Wright 	char		*operator;
205*a0b6e447SAlan Wright 	long		value;
206*a0b6e447SAlan Wright 
207*a0b6e447SAlan Wright 	arg = np->n_a_arg;
208*a0b6e447SAlan Wright 	if (arg->label != IDENTIFIER)
209*a0b6e447SAlan Wright 		fatal_error("invalid label %d", arg->label);
210*a0b6e447SAlan Wright 	if ((name = arg->n_sym->name) == NULL)
211*a0b6e447SAlan Wright 		fatal_error("missing symbol name");
212*a0b6e447SAlan Wright 
213*a0b6e447SAlan Wright 	arg = np->n_a_arg1;
214*a0b6e447SAlan Wright 	operator = NULL;
215*a0b6e447SAlan Wright 	if (arg->label == IDENTIFIER) {
216*a0b6e447SAlan Wright 		operator = arg->n_sym->name;
217*a0b6e447SAlan Wright 
218*a0b6e447SAlan Wright 		if (operator != NULL) {
219*a0b6e447SAlan Wright 			/*
220*a0b6e447SAlan Wright 			 * The lexer sets the name and operator to
221*a0b6e447SAlan Wright 			 * the same value if there is no operator.
222*a0b6e447SAlan Wright 			 */
223*a0b6e447SAlan Wright 			if (strcmp(name, operator) == 0)
224*a0b6e447SAlan Wright 				operator = NULL;
225*a0b6e447SAlan Wright 			else if (strchr(valid, *operator) == NULL)
226*a0b6e447SAlan Wright 				compile_error("invalid operator: %s", operator);
227*a0b6e447SAlan Wright 		}
228*a0b6e447SAlan Wright 	}
229*a0b6e447SAlan Wright 
230*a0b6e447SAlan Wright 	arg = np->n_a_arg2;
231*a0b6e447SAlan Wright 	if (arg->label == INTEGER) {
232*a0b6e447SAlan Wright 		value = arg->n_int;
233*a0b6e447SAlan Wright 
234*a0b6e447SAlan Wright 		if ((value == 0) && strcmp(operator, "/") == 0)
235*a0b6e447SAlan Wright 			compile_error("divide by zero");
236*a0b6e447SAlan Wright 	}
237*a0b6e447SAlan Wright 
238*a0b6e447SAlan Wright 	if (operator)
239*a0b6e447SAlan Wright 		(void) printf("%s %s %ldUL", name, operator, value);
240*a0b6e447SAlan Wright 	else
241*a0b6e447SAlan Wright 		(void) printf("%s", name);
242*a0b6e447SAlan Wright }
243*a0b6e447SAlan Wright 
244d0e51869Samw static void
print_declaration(ndr_node_t * np)245d0e51869Samw print_declaration(ndr_node_t *np)
246d0e51869Samw {
247d0e51869Samw 	ndr_node_t	*dnp = np->n_m_decl;
248d0e51869Samw 	char		buf[NDLBUFSZ];
249d0e51869Samw 	char		*p = buf;
250d0e51869Samw 
251d0e51869Samw 	if (np->n_m_type &&
252d0e51869Samw 	    (np->n_m_type->label == IDENTIFIER ||
253d0e51869Samw 	    np->n_m_type->label == TYPENAME)) {
254d0e51869Samw 		(void) snprintf(buf, NDLBUFSZ, "%s", np->n_m_type->n_sym->name);
255d0e51869Samw 
256d0e51869Samw 		while (*p)
257d0e51869Samw 			p++;
258d0e51869Samw 
259d0e51869Samw 		if (dnp && dnp->label == STAR) {
260d0e51869Samw 			*p++ = ' ';
261d0e51869Samw 			while (dnp && dnp->label == STAR) {
262d0e51869Samw 				*p++ = '*';
263d0e51869Samw 				dnp = dnp->n_d_descend;
264d0e51869Samw 			}
265d0e51869Samw 		}
266d0e51869Samw 		*p = 0;
267d0e51869Samw 		(void) printf("%-23s ", buf);
268d0e51869Samw 	} else {
269d0e51869Samw 		print_node(np->n_m_type);
270d0e51869Samw 		(void) printf(" ");
271d0e51869Samw 	}
272d0e51869Samw 
273d0e51869Samw 	print_node(dnp);
274d0e51869Samw }
275d0e51869Samw 
276d0e51869Samw static void
print_advice_list(ndr_node_t * np)277d0e51869Samw print_advice_list(ndr_node_t *np)
278d0e51869Samw {
279d0e51869Samw 	if (!np)
280d0e51869Samw 		return;
281d0e51869Samw 
282d0e51869Samw 	(void) printf("[");
283d0e51869Samw 	for (; np; np = np->n_next) {
284d0e51869Samw 		print_node(np);
285d0e51869Samw 		if (np->n_next)
286d0e51869Samw 			(void) printf(" ");
287d0e51869Samw 	}
288d0e51869Samw 	(void) printf("]");
289d0e51869Samw }
290d0e51869Samw 
291d0e51869Samw static void
print_node_list(ndr_node_t * np)292d0e51869Samw print_node_list(ndr_node_t *np)
293d0e51869Samw {
294d0e51869Samw 	for (; np; np = np->n_next) {
295d0e51869Samw 		print_node(np);
296d0e51869Samw 	}
297d0e51869Samw }
298