xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_pragma.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <strings.h>
30*0Sstevel@tonic-gate #include <alloca.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <stdio.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <dt_parser.h>
35*0Sstevel@tonic-gate #include <dt_impl.h>
36*0Sstevel@tonic-gate #include <dt_provider.h>
37*0Sstevel@tonic-gate #include <dt_module.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * This callback function is installed in a given identifier hash to search for
41*0Sstevel@tonic-gate  * and apply deferred pragmas that are pending for a given new identifier name.
42*0Sstevel@tonic-gate  * Multiple pragmas may be pending for a given name; we processs all of them.
43*0Sstevel@tonic-gate  */
44*0Sstevel@tonic-gate /*ARGSUSED*/
45*0Sstevel@tonic-gate static void
46*0Sstevel@tonic-gate dt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp)
47*0Sstevel@tonic-gate {
48*0Sstevel@tonic-gate 	dt_idhash_t *php;
49*0Sstevel@tonic-gate 	dt_ident_t *pdp;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 	if ((php = yypcb->pcb_pragmas) == NULL)
52*0Sstevel@tonic-gate 		return; /* no pragmas pending for current compilation pass */
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) {
55*0Sstevel@tonic-gate 		switch (pdp->di_kind) {
56*0Sstevel@tonic-gate 		case DT_IDENT_PRAGAT:
57*0Sstevel@tonic-gate 			idp->di_attr = pdp->di_attr;
58*0Sstevel@tonic-gate 			break;
59*0Sstevel@tonic-gate 		case DT_IDENT_PRAGBN:
60*0Sstevel@tonic-gate 			idp->di_vers = pdp->di_vers;
61*0Sstevel@tonic-gate 			break;
62*0Sstevel@tonic-gate 		}
63*0Sstevel@tonic-gate 		dt_idhash_delete(php, pdp);
64*0Sstevel@tonic-gate 	}
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * The #pragma attributes directive can be used to reset stability attributes
69*0Sstevel@tonic-gate  * on a global identifier or inline definition.  If the identifier is already
70*0Sstevel@tonic-gate  * defined, we can just change di_attr.  If not, we insert the pragma into a
71*0Sstevel@tonic-gate  * hash table of the current pcb's deferred pragmas for later processing.
72*0Sstevel@tonic-gate  */
73*0Sstevel@tonic-gate static void
74*0Sstevel@tonic-gate dt_pragma_attributes(const char *prname, dt_node_t *dnp)
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
77*0Sstevel@tonic-gate 	dtrace_attribute_t attr, *a;
78*0Sstevel@tonic-gate 	dt_provider_t *pvp;
79*0Sstevel@tonic-gate 	const char *name, *part;
80*0Sstevel@tonic-gate 	dt_ident_t *idp;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT ||
83*0Sstevel@tonic-gate 	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
84*0Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
85*0Sstevel@tonic-gate 		    "<attributes> <ident>\n", prname);
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	if (dtrace_str2attr(dnp->dn_string, &attr) == -1) {
89*0Sstevel@tonic-gate 		xyerror(D_PRAGMA_INVAL, "invalid attributes "
90*0Sstevel@tonic-gate 		    "specified by #pragma %s\n", prname);
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	dnp = dnp->dn_list;
94*0Sstevel@tonic-gate 	name = dnp->dn_string;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	if (strcmp(name, "provider") == 0) {
97*0Sstevel@tonic-gate 		dnp = dnp->dn_list;
98*0Sstevel@tonic-gate 		name = dnp->dn_string;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 		dnp = dnp->dn_list;
101*0Sstevel@tonic-gate 		part = dnp->dn_string;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 		if ((pvp = dt_provider_lookup(dtp, name)) != NULL) {
104*0Sstevel@tonic-gate 			if (strcmp(part, "provider") == 0) {
105*0Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_provider;
106*0Sstevel@tonic-gate 			} else if (strcmp(part, "module") == 0) {
107*0Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_mod;
108*0Sstevel@tonic-gate 			} else if (strcmp(part, "function") == 0) {
109*0Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_func;
110*0Sstevel@tonic-gate 			} else if (strcmp(part, "name") == 0) {
111*0Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_name;
112*0Sstevel@tonic-gate 			} else if (strcmp(part, "args") == 0) {
113*0Sstevel@tonic-gate 				a = &pvp->pv_desc.dtvd_attr.dtpa_args;
114*0Sstevel@tonic-gate 			} else {
115*0Sstevel@tonic-gate 				xyerror(D_PRAGMA_INVAL, "invalid component "
116*0Sstevel@tonic-gate 				    "\"%s\" in attribute #pragma "
117*0Sstevel@tonic-gate 				    "for provider %s\n", name, part);
118*0Sstevel@tonic-gate 			}
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 			*a = attr;
121*0Sstevel@tonic-gate 			return;
122*0Sstevel@tonic-gate 		}
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	} else if ((idp = dt_idstack_lookup(
125*0Sstevel@tonic-gate 	    &yypcb->pcb_globals, name)) != NULL) {
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 		if (idp->di_gen != dtp->dt_gen) {
128*0Sstevel@tonic-gate 			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
129*0Sstevel@tonic-gate 			    "entity defined outside program scope\n", prname);
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		idp->di_attr = attr;
133*0Sstevel@tonic-gate 		return;
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
137*0Sstevel@tonic-gate 	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
138*0Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0,
141*0Sstevel@tonic-gate 	    attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	if (idp == NULL)
144*0Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if (dtp->dt_globals->dh_defer == NULL)
147*0Sstevel@tonic-gate 		dtp->dt_globals->dh_defer = &dt_pragma_apply;
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate /*
151*0Sstevel@tonic-gate  * The #pragma binding directive can be used to reset the version binding
152*0Sstevel@tonic-gate  * on a global identifier or inline definition.  If the identifier is already
153*0Sstevel@tonic-gate  * defined, we can just change di_vers.  If not, we insert the pragma into a
154*0Sstevel@tonic-gate  * hash table of the current pcb's deferred pragmas for later processing.
155*0Sstevel@tonic-gate  */
156*0Sstevel@tonic-gate static void
157*0Sstevel@tonic-gate dt_pragma_binding(const char *prname, dt_node_t *dnp)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
160*0Sstevel@tonic-gate 	dt_version_t vers;
161*0Sstevel@tonic-gate 	const char *name;
162*0Sstevel@tonic-gate 	dt_ident_t *idp;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING ||
165*0Sstevel@tonic-gate 	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
166*0Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
167*0Sstevel@tonic-gate 		    "\"version\" <ident>\n", prname);
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	if (dt_version_str2num(dnp->dn_string, &vers) == -1) {
171*0Sstevel@tonic-gate 		xyerror(D_PRAGMA_INVAL, "invalid version string "
172*0Sstevel@tonic-gate 		    "specified by #pragma %s\n", prname);
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	name = dnp->dn_list->dn_string;
176*0Sstevel@tonic-gate 	idp = dt_idstack_lookup(&yypcb->pcb_globals, name);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	if (idp != NULL) {
179*0Sstevel@tonic-gate 		if (idp->di_gen != dtp->dt_gen) {
180*0Sstevel@tonic-gate 			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
181*0Sstevel@tonic-gate 			    "entity defined outside program scope\n", prname);
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 		idp->di_vers = vers;
184*0Sstevel@tonic-gate 		return;
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
188*0Sstevel@tonic-gate 	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
189*0Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0,
192*0Sstevel@tonic-gate 	    _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	if (idp == NULL)
195*0Sstevel@tonic-gate 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	if (dtp->dt_globals->dh_defer == NULL)
198*0Sstevel@tonic-gate 		dtp->dt_globals->dh_defer = &dt_pragma_apply;
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate /*
202*0Sstevel@tonic-gate  * The #pragma depends_on directive can be used to express a dependency on a
203*0Sstevel@tonic-gate  * module or provider, which if not present will cause processing to abort.
204*0Sstevel@tonic-gate  */
205*0Sstevel@tonic-gate static void
206*0Sstevel@tonic-gate dt_pragma_depends(const char *prname, dt_node_t *cnp)
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
209*0Sstevel@tonic-gate 	dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
210*0Sstevel@tonic-gate 	int found;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	if (cnp == NULL || nnp == NULL ||
213*0Sstevel@tonic-gate 	    cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
214*0Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
215*0Sstevel@tonic-gate 		    "<class> <name>\n", prname);
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	if (strcmp(cnp->dn_string, "provider") == 0)
219*0Sstevel@tonic-gate 		found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
220*0Sstevel@tonic-gate 	else if (strcmp(cnp->dn_string, "module") == 0) {
221*0Sstevel@tonic-gate 		dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
222*0Sstevel@tonic-gate 		found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
223*0Sstevel@tonic-gate 	} else {
224*0Sstevel@tonic-gate 		xyerror(D_PRAGMA_INVAL, "invalid class %s "
225*0Sstevel@tonic-gate 		    "specified by #pragma %s\n", cnp->dn_string, prname);
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	if (!found) {
229*0Sstevel@tonic-gate 		xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
230*0Sstevel@tonic-gate 		    cnp->dn_string, nnp->dn_string);
231*0Sstevel@tonic-gate 	}
232*0Sstevel@tonic-gate }
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate /*
235*0Sstevel@tonic-gate  * The #pragma error directive can be followed by any list of tokens, which we
236*0Sstevel@tonic-gate  * just concatenate and print as part of our error message.
237*0Sstevel@tonic-gate  */
238*0Sstevel@tonic-gate static void
239*0Sstevel@tonic-gate dt_pragma_error(const char *prname, dt_node_t *dnp)
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate 	dt_node_t *enp;
242*0Sstevel@tonic-gate 	size_t n = 0;
243*0Sstevel@tonic-gate 	char *s;
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
246*0Sstevel@tonic-gate 		if (enp->dn_kind == DT_NODE_IDENT ||
247*0Sstevel@tonic-gate 		    enp->dn_kind == DT_NODE_STRING)
248*0Sstevel@tonic-gate 			n += strlen(enp->dn_string) + 1;
249*0Sstevel@tonic-gate 	}
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	s = alloca(n + 1);
252*0Sstevel@tonic-gate 	s[0] = '\0';
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
255*0Sstevel@tonic-gate 		if (enp->dn_kind == DT_NODE_IDENT ||
256*0Sstevel@tonic-gate 		    enp->dn_kind == DT_NODE_STRING) {
257*0Sstevel@tonic-gate 			(void) strcat(s, enp->dn_string);
258*0Sstevel@tonic-gate 			(void) strcat(s, " ");
259*0Sstevel@tonic-gate 		}
260*0Sstevel@tonic-gate 	}
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
263*0Sstevel@tonic-gate }
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate /*ARGSUSED*/
266*0Sstevel@tonic-gate static void
267*0Sstevel@tonic-gate dt_pragma_ident(const char *prname, dt_node_t *dnp)
268*0Sstevel@tonic-gate {
269*0Sstevel@tonic-gate 	/* ignore any #ident or #pragma ident lines */
270*0Sstevel@tonic-gate }
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate static void
273*0Sstevel@tonic-gate dt_pragma_option(const char *prname, dt_node_t *dnp)
274*0Sstevel@tonic-gate {
275*0Sstevel@tonic-gate 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
276*0Sstevel@tonic-gate 	char *opt, *val;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
279*0Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM,
280*0Sstevel@tonic-gate 		    "malformed #pragma %s <option>=<val>\n", prname);
281*0Sstevel@tonic-gate 	}
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	if (dnp->dn_list != NULL) {
284*0Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM,
285*0Sstevel@tonic-gate 		    "superfluous arguments specified for #pragma %s\n", prname);
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	opt = alloca(strlen(dnp->dn_string) + 1);
289*0Sstevel@tonic-gate 	(void) strcpy(opt, dnp->dn_string);
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	if ((val = strchr(opt, '=')) != NULL)
292*0Sstevel@tonic-gate 		*val++ = '\0';
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	if (dtrace_setopt(dtp, opt, val) == -1) {
295*0Sstevel@tonic-gate 		if (val == NULL) {
296*0Sstevel@tonic-gate 			xyerror(D_PRAGMA_OPTSET,
297*0Sstevel@tonic-gate 			    "failed to set option '%s': %s\n", opt,
298*0Sstevel@tonic-gate 			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
299*0Sstevel@tonic-gate 		} else {
300*0Sstevel@tonic-gate 			xyerror(D_PRAGMA_OPTSET,
301*0Sstevel@tonic-gate 			    "failed to set option '%s' to '%s': %s\n",
302*0Sstevel@tonic-gate 			    opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
303*0Sstevel@tonic-gate 		}
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate }
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate /*
308*0Sstevel@tonic-gate  * The #line directive is used to reset the input line number and to optionally
309*0Sstevel@tonic-gate  * note the file name for use in error messages.  Sun cpp(1) also produces a
310*0Sstevel@tonic-gate  * third integer token after the filename which is one of the following:
311*0Sstevel@tonic-gate  *
312*0Sstevel@tonic-gate  * 0 - line change has nothing to do with an #include file
313*0Sstevel@tonic-gate  * 1 - line change because we just entered a #include file
314*0Sstevel@tonic-gate  * 2 - line change because we just exited a #include file
315*0Sstevel@tonic-gate  *
316*0Sstevel@tonic-gate  * We use these state tokens to adjust pcb_idepth, which in turn controls
317*0Sstevel@tonic-gate  * whether type lookups access the global type space or not.
318*0Sstevel@tonic-gate  */
319*0Sstevel@tonic-gate static void
320*0Sstevel@tonic-gate dt_pragma_line(const char *prname, dt_node_t *dnp)
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
323*0Sstevel@tonic-gate 	dt_node_t *inp = fnp ? fnp->dn_list : NULL;
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
326*0Sstevel@tonic-gate 	    (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
327*0Sstevel@tonic-gate 	    (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
328*0Sstevel@tonic-gate 		xyerror(D_PRAGMA_MALFORM, "malformed #%s "
329*0Sstevel@tonic-gate 		    "<line> [ [\"file\"] state ]\n", prname);
330*0Sstevel@tonic-gate 	}
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	/*
333*0Sstevel@tonic-gate 	 * If a file is specified, free any old pcb_filetag and swap fnp's
334*0Sstevel@tonic-gate 	 * dn_string into pcb_filetag as the new filename for error messages.
335*0Sstevel@tonic-gate 	 */
336*0Sstevel@tonic-gate 	if (fnp != NULL) {
337*0Sstevel@tonic-gate 		if (yypcb->pcb_filetag != NULL)
338*0Sstevel@tonic-gate 			free(yypcb->pcb_filetag);
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 		/*
341*0Sstevel@tonic-gate 		 * This is not pretty, but is a necessary evil until we either
342*0Sstevel@tonic-gate 		 * write "dpp" or get a useful standalone cpp from DevPro.  If
343*0Sstevel@tonic-gate 		 * the filename begins with /dev/fd, we know it's the master
344*0Sstevel@tonic-gate 		 * input file (see dt_preproc() in dt_cc.c), so just clear the
345*0Sstevel@tonic-gate 		 * dt_filetag pointer so error messages refer to the main file.
346*0Sstevel@tonic-gate 		 */
347*0Sstevel@tonic-gate 		if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
348*0Sstevel@tonic-gate 			yypcb->pcb_filetag = fnp->dn_string;
349*0Sstevel@tonic-gate 			fnp->dn_string = NULL;
350*0Sstevel@tonic-gate 		} else
351*0Sstevel@tonic-gate 			yypcb->pcb_filetag = NULL;
352*0Sstevel@tonic-gate 	}
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	if (inp != NULL) {
355*0Sstevel@tonic-gate 		if (inp->dn_value == 1)
356*0Sstevel@tonic-gate 			yypcb->pcb_idepth++;
357*0Sstevel@tonic-gate 		else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
358*0Sstevel@tonic-gate 			yypcb->pcb_idepth--;
359*0Sstevel@tonic-gate 	}
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	yylineno = dnp->dn_value;
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate /*
365*0Sstevel@tonic-gate  * D compiler pragma types range from control directives to common pragmas to
366*0Sstevel@tonic-gate  * D custom pragmas, in order of specificity.  Similar to gcc, we use #pragma D
367*0Sstevel@tonic-gate  * as a special prefix for our pragmas so they can be used in mixed headers.
368*0Sstevel@tonic-gate  */
369*0Sstevel@tonic-gate #define	DT_PRAGMA_DIR	0	/* pragma directive may be used after naked # */
370*0Sstevel@tonic-gate #define	DT_PRAGMA_SUB	1	/* pragma directive may be used after #pragma */
371*0Sstevel@tonic-gate #define	DT_PRAGMA_DCP	2	/* pragma may only be used after #pragma D */
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate static const struct dt_pragmadesc {
374*0Sstevel@tonic-gate 	const char *dpd_name;
375*0Sstevel@tonic-gate 	void (*dpd_func)(const char *, dt_node_t *);
376*0Sstevel@tonic-gate 	int dpd_kind;
377*0Sstevel@tonic-gate } dt_pragmas[] = {
378*0Sstevel@tonic-gate 	{ "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
379*0Sstevel@tonic-gate 	{ "binding", dt_pragma_binding, DT_PRAGMA_DCP },
380*0Sstevel@tonic-gate 	{ "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
381*0Sstevel@tonic-gate 	{ "error", dt_pragma_error, DT_PRAGMA_DIR },
382*0Sstevel@tonic-gate 	{ "ident", dt_pragma_ident, DT_PRAGMA_DIR },
383*0Sstevel@tonic-gate 	{ "line", dt_pragma_line, DT_PRAGMA_DIR },
384*0Sstevel@tonic-gate 	{ "option", dt_pragma_option, DT_PRAGMA_DCP },
385*0Sstevel@tonic-gate 	{ NULL, NULL }
386*0Sstevel@tonic-gate };
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate /*
389*0Sstevel@tonic-gate  * Process a control line #directive by looking up the directive name in our
390*0Sstevel@tonic-gate  * lookup table and invoking the corresponding function with the token list.
391*0Sstevel@tonic-gate  * According to K&R[A12.9], we silently ignore null directive lines.
392*0Sstevel@tonic-gate  */
393*0Sstevel@tonic-gate void
394*0Sstevel@tonic-gate dt_pragma(dt_node_t *pnp)
395*0Sstevel@tonic-gate {
396*0Sstevel@tonic-gate 	const struct dt_pragmadesc *dpd;
397*0Sstevel@tonic-gate 	dt_node_t *dnp;
398*0Sstevel@tonic-gate 	int kind = DT_PRAGMA_DIR;
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
401*0Sstevel@tonic-gate 		if (dnp->dn_kind == DT_NODE_INT) {
402*0Sstevel@tonic-gate 			dt_pragma_line("line", dnp);
403*0Sstevel@tonic-gate 			break;
404*0Sstevel@tonic-gate 		}
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 		if (dnp->dn_kind != DT_NODE_IDENT)
407*0Sstevel@tonic-gate 			xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 		if (kind == DT_PRAGMA_DIR &&
410*0Sstevel@tonic-gate 		    strcmp(dnp->dn_string, "pragma") == 0) {
411*0Sstevel@tonic-gate 			kind = DT_PRAGMA_SUB;
412*0Sstevel@tonic-gate 			continue;
413*0Sstevel@tonic-gate 		}
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 		if (kind == DT_PRAGMA_SUB &&
416*0Sstevel@tonic-gate 		    strcmp(dnp->dn_string, "D") == 0) {
417*0Sstevel@tonic-gate 			kind = DT_PRAGMA_DCP;
418*0Sstevel@tonic-gate 			continue;
419*0Sstevel@tonic-gate 		}
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 		for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
422*0Sstevel@tonic-gate 			if (dpd->dpd_kind <= kind &&
423*0Sstevel@tonic-gate 			    strcmp(dpd->dpd_name, dnp->dn_string) == 0)
424*0Sstevel@tonic-gate 				break;
425*0Sstevel@tonic-gate 		}
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 		yylineno--; /* since we've already seen \n */
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 		if (dpd->dpd_name != NULL) {
430*0Sstevel@tonic-gate 			dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
431*0Sstevel@tonic-gate 			yylineno++;
432*0Sstevel@tonic-gate 			break;
433*0Sstevel@tonic-gate 		}
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 		switch (kind) {
436*0Sstevel@tonic-gate 		case DT_PRAGMA_DIR:
437*0Sstevel@tonic-gate 			xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
438*0Sstevel@tonic-gate 			    "#%s\n", dnp->dn_string);
439*0Sstevel@tonic-gate 			/*NOTREACHED*/
440*0Sstevel@tonic-gate 		case DT_PRAGMA_SUB:
441*0Sstevel@tonic-gate 			break; /* K&R[A12.8] says to ignore unknown pragmas */
442*0Sstevel@tonic-gate 		case DT_PRAGMA_DCP:
443*0Sstevel@tonic-gate 		default:
444*0Sstevel@tonic-gate 			xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
445*0Sstevel@tonic-gate 			    dnp->dn_string);
446*0Sstevel@tonic-gate 		}
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 		yylineno++;
449*0Sstevel@tonic-gate 		break;
450*0Sstevel@tonic-gate 	}
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	dt_node_list_free(&pnp);
453*0Sstevel@tonic-gate }
454