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