1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License (the "License").
6178479Sjb * You may not use this file except in compliance with the License.
7178479Sjb *
8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178479Sjb * or http://www.opensolaris.org/os/licensing.
10178479Sjb * See the License for the specific language governing permissions
11178479Sjb * and limitations under the License.
12178479Sjb *
13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178479Sjb * If applicable, add the following below this CDDL HEADER, with the
16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178479Sjb *
19178479Sjb * CDDL HEADER END
20178479Sjb */
21178566Sjb
22178479Sjb/*
23178566Sjb * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24239536Spfg * Copyright (c) 2011, Joyent Inc. All rights reserved.
25178479Sjb */
26178479Sjb
27178479Sjb#pragma ident	"%Z%%M%	%I%	%E% SMI"
28178479Sjb
29178479Sjb#include <assert.h>
30178479Sjb#include <strings.h>
31178566Sjb#if defined(sun)
32178479Sjb#include <alloca.h>
33178566Sjb#endif
34239536Spfg#include <fcntl.h>
35178479Sjb#include <stdlib.h>
36178479Sjb#include <stdio.h>
37178479Sjb
38239536Spfg#include <sys/types.h>
39239536Spfg#include <sys/stat.h>
40239536Spfg
41178479Sjb#include <dt_parser.h>
42178479Sjb#include <dt_impl.h>
43178479Sjb#include <dt_provider.h>
44178479Sjb#include <dt_module.h>
45178479Sjb
46178479Sjb/*
47178479Sjb * This callback function is installed in a given identifier hash to search for
48178479Sjb * and apply deferred pragmas that are pending for a given new identifier name.
49178479Sjb * Multiple pragmas may be pending for a given name; we processs all of them.
50178479Sjb */
51178479Sjb/*ARGSUSED*/
52178479Sjbstatic void
53178479Sjbdt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp)
54178479Sjb{
55178479Sjb	dt_idhash_t *php;
56178479Sjb	dt_ident_t *pdp;
57178479Sjb
58178479Sjb	if ((php = yypcb->pcb_pragmas) == NULL)
59178479Sjb		return; /* no pragmas pending for current compilation pass */
60178479Sjb
61178479Sjb	while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) {
62178479Sjb		switch (pdp->di_kind) {
63178479Sjb		case DT_IDENT_PRAGAT:
64178479Sjb			idp->di_attr = pdp->di_attr;
65178479Sjb			break;
66178479Sjb		case DT_IDENT_PRAGBN:
67178479Sjb			idp->di_vers = pdp->di_vers;
68178479Sjb			break;
69178479Sjb		}
70178479Sjb		dt_idhash_delete(php, pdp);
71178479Sjb	}
72178479Sjb}
73178479Sjb
74178479Sjb/*
75178479Sjb * The #pragma attributes directive can be used to reset stability attributes
76178479Sjb * on a global identifier or inline definition.  If the identifier is already
77178479Sjb * defined, we can just change di_attr.  If not, we insert the pragma into a
78178479Sjb * hash table of the current pcb's deferred pragmas for later processing.
79178479Sjb */
80178479Sjbstatic void
81178479Sjbdt_pragma_attributes(const char *prname, dt_node_t *dnp)
82178479Sjb{
83178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
84178479Sjb	dtrace_attribute_t attr, *a;
85178479Sjb	dt_provider_t *pvp;
86178479Sjb	const char *name, *part;
87178479Sjb	dt_ident_t *idp;
88178479Sjb
89178479Sjb	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT ||
90178479Sjb	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
91178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
92178479Sjb		    "<attributes> <ident>\n", prname);
93178479Sjb	}
94178479Sjb
95178479Sjb	if (dtrace_str2attr(dnp->dn_string, &attr) == -1) {
96178479Sjb		xyerror(D_PRAGMA_INVAL, "invalid attributes "
97178479Sjb		    "specified by #pragma %s\n", prname);
98178479Sjb	}
99178479Sjb
100178479Sjb	dnp = dnp->dn_list;
101178479Sjb	name = dnp->dn_string;
102178479Sjb
103178479Sjb	if (strcmp(name, "provider") == 0) {
104178479Sjb		dnp = dnp->dn_list;
105178479Sjb		name = dnp->dn_string;
106178479Sjb
107178479Sjb		dnp = dnp->dn_list;
108178479Sjb		part = dnp->dn_string;
109178479Sjb
110178479Sjb		if ((pvp = dt_provider_lookup(dtp, name)) != NULL) {
111178479Sjb			if (strcmp(part, "provider") == 0) {
112178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_provider;
113178479Sjb			} else if (strcmp(part, "module") == 0) {
114178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_mod;
115178479Sjb			} else if (strcmp(part, "function") == 0) {
116178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_func;
117178479Sjb			} else if (strcmp(part, "name") == 0) {
118178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_name;
119178479Sjb			} else if (strcmp(part, "args") == 0) {
120178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_args;
121178479Sjb			} else {
122178479Sjb				xyerror(D_PRAGMA_INVAL, "invalid component "
123178479Sjb				    "\"%s\" in attribute #pragma "
124178479Sjb				    "for provider %s\n", name, part);
125178479Sjb			}
126178479Sjb
127178479Sjb			*a = attr;
128178479Sjb			return;
129178479Sjb		}
130178479Sjb
131178479Sjb	} else if ((idp = dt_idstack_lookup(
132178479Sjb	    &yypcb->pcb_globals, name)) != NULL) {
133178479Sjb
134178479Sjb		if (idp->di_gen != dtp->dt_gen) {
135178479Sjb			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
136178479Sjb			    "entity defined outside program scope\n", prname);
137178479Sjb		}
138178479Sjb
139178479Sjb		idp->di_attr = attr;
140178479Sjb		return;
141178479Sjb	}
142178479Sjb
143178479Sjb	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
144178479Sjb	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
145178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
146178479Sjb
147178479Sjb	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0,
148178479Sjb	    attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
149178479Sjb
150178479Sjb	if (idp == NULL)
151178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
152178479Sjb
153178479Sjb	if (dtp->dt_globals->dh_defer == NULL)
154178479Sjb		dtp->dt_globals->dh_defer = &dt_pragma_apply;
155178479Sjb}
156178479Sjb
157178479Sjb/*
158178479Sjb * The #pragma binding directive can be used to reset the version binding
159178479Sjb * on a global identifier or inline definition.  If the identifier is already
160178479Sjb * defined, we can just change di_vers.  If not, we insert the pragma into a
161178479Sjb * hash table of the current pcb's deferred pragmas for later processing.
162178479Sjb */
163178479Sjbstatic void
164178479Sjbdt_pragma_binding(const char *prname, dt_node_t *dnp)
165178479Sjb{
166178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
167178479Sjb	dt_version_t vers;
168178479Sjb	const char *name;
169178479Sjb	dt_ident_t *idp;
170178479Sjb
171178479Sjb	if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING ||
172178479Sjb	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
173178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
174178479Sjb		    "\"version\" <ident>\n", prname);
175178479Sjb	}
176178479Sjb
177178479Sjb	if (dt_version_str2num(dnp->dn_string, &vers) == -1) {
178178479Sjb		xyerror(D_PRAGMA_INVAL, "invalid version string "
179178479Sjb		    "specified by #pragma %s\n", prname);
180178479Sjb	}
181178479Sjb
182178479Sjb	name = dnp->dn_list->dn_string;
183178479Sjb	idp = dt_idstack_lookup(&yypcb->pcb_globals, name);
184178479Sjb
185178479Sjb	if (idp != NULL) {
186178479Sjb		if (idp->di_gen != dtp->dt_gen) {
187178479Sjb			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
188178479Sjb			    "entity defined outside program scope\n", prname);
189178479Sjb		}
190178479Sjb		idp->di_vers = vers;
191178479Sjb		return;
192178479Sjb	}
193178479Sjb
194178479Sjb	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
195178479Sjb	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
196178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
197178479Sjb
198178479Sjb	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0,
199178479Sjb	    _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
200178479Sjb
201178479Sjb	if (idp == NULL)
202178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
203178479Sjb
204178479Sjb	if (dtp->dt_globals->dh_defer == NULL)
205178479Sjb		dtp->dt_globals->dh_defer = &dt_pragma_apply;
206178479Sjb}
207178479Sjb
208239536Spfgstatic void
209239536Spfgdt_pragma_depends_finddep(dtrace_hdl_t *dtp, const char *lname, char *lib,
210239536Spfg    size_t len)
211239536Spfg{
212239536Spfg	dt_dirpath_t *dirp;
213239536Spfg	struct stat sbuf;
214239536Spfg	int found = 0;
215239536Spfg
216239536Spfg	for (dirp = dt_list_next(&dtp->dt_lib_path); dirp != NULL;
217239536Spfg	    dirp = dt_list_next(dirp)) {
218239536Spfg		(void) snprintf(lib, len, "%s/%s", dirp->dir_path, lname);
219239536Spfg
220239536Spfg		if (stat(lib, &sbuf) == 0) {
221239536Spfg			found = 1;
222239536Spfg			break;
223239536Spfg		}
224239536Spfg	}
225239536Spfg
226239536Spfg	if (!found)
227239536Spfg		xyerror(D_PRAGMA_DEPEND,
228239536Spfg		    "failed to find dependency in libpath: %s", lname);
229239536Spfg}
230239536Spfg
231178479Sjb/*
232178479Sjb * The #pragma depends_on directive can be used to express a dependency on a
233178479Sjb * module, provider or library which if not present will cause processing to
234178479Sjb * abort.
235178479Sjb */
236178479Sjbstatic void
237178479Sjbdt_pragma_depends(const char *prname, dt_node_t *cnp)
238178479Sjb{
239178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
240178479Sjb	dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
241178479Sjb	int found;
242178479Sjb	dt_lib_depend_t *dld;
243178566Sjb	char lib[MAXPATHLEN];
244251857Sgnn	size_t plen;
245251857Sgnn	char *provs, *cpy, *tok;
246178479Sjb
247178479Sjb	if (cnp == NULL || nnp == NULL ||
248178479Sjb	    cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
249178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
250178479Sjb		    "<class> <name>\n", prname);
251178479Sjb	}
252178479Sjb
253251857Sgnn	if (strcmp(cnp->dn_string, "provider") == 0) {
254251857Sgnn		/*
255251857Sgnn		 * First try to get the provider list using the
256251857Sgnn		 * debug.dtrace.providers sysctl, since that'll work even if
257251857Sgnn		 * we're not running as root.
258251857Sgnn		 */
259251857Sgnn		provs = NULL;
260251857Sgnn		if (sysctlbyname("debug.dtrace.providers", NULL, &plen, NULL, 0) ||
261251857Sgnn		    ((provs = dt_alloc(dtp, plen)) == NULL) ||
262251857Sgnn		    sysctlbyname("debug.dtrace.providers", provs, &plen, NULL, 0))
263251857Sgnn			found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
264251857Sgnn		else {
265251857Sgnn			found = B_FALSE;
266251857Sgnn			for (cpy = provs; (tok = strsep(&cpy, " ")) != NULL; )
267251857Sgnn				if (strcmp(tok, nnp->dn_string) == 0) {
268251857Sgnn					found = B_TRUE;
269251857Sgnn					break;
270251857Sgnn				}
271251857Sgnn			if (found == B_FALSE)
272251857Sgnn				found = dt_provider_lookup(dtp,
273251857Sgnn				    nnp->dn_string) != NULL;
274251857Sgnn		}
275251857Sgnn		if (provs != NULL)
276251857Sgnn			dt_free(dtp, provs);
277251857Sgnn	} else if (strcmp(cnp->dn_string, "module") == 0) {
278178479Sjb		dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
279178479Sjb		found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
280178479Sjb	} else if (strcmp(cnp->dn_string, "library") == 0) {
281178479Sjb		if (yypcb->pcb_cflags & DTRACE_C_CTL) {
282178566Sjb			assert(dtp->dt_filetag != NULL);
283178479Sjb
284239536Spfg			dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
285239536Spfg			    sizeof (lib));
286239536Spfg
287178479Sjb			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
288178479Sjb			    dtp->dt_filetag);
289178479Sjb			assert(dld != NULL);
290178479Sjb
291178479Sjb			if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies,
292178479Sjb			    lib)) != 0) {
293178479Sjb				xyerror(D_PRAGMA_DEPEND,
294178566Sjb				    "failed to add dependency %s:%s\n", lib,
295178479Sjb				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
296178479Sjb			}
297178566Sjb		} else {
298178566Sjb			/*
299178566Sjb			 * By this point we have already performed a topological
300178566Sjb			 * sort of the dependencies; we process this directive
301178566Sjb			 * as satisfied as long as the dependency was properly
302178566Sjb			 * loaded.
303178566Sjb			 */
304178566Sjb			if (dtp->dt_filetag == NULL)
305178566Sjb				xyerror(D_PRAGMA_DEPEND, "main program may "
306178566Sjb				    "not explicitly depend on a library");
307178566Sjb
308178566Sjb			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
309178566Sjb			    dtp->dt_filetag);
310178566Sjb			assert(dld != NULL);
311178566Sjb
312239536Spfg			dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
313239536Spfg			    sizeof (lib));
314178566Sjb			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
315178566Sjb			    lib);
316178566Sjb			assert(dld != NULL);
317178566Sjb
318178566Sjb			if (!dld->dtld_loaded)
319178566Sjb				xyerror(D_PRAGMA_DEPEND, "program requires "
320178566Sjb				    "library \"%s\" which failed to load",
321178566Sjb				    lib);
322178479Sjb		}
323178566Sjb
324178566Sjb		found = B_TRUE;
325178479Sjb	} else {
326178479Sjb		xyerror(D_PRAGMA_INVAL, "invalid class %s "
327178479Sjb		    "specified by #pragma %s\n", cnp->dn_string, prname);
328178479Sjb	}
329178479Sjb
330178479Sjb	if (!found) {
331178479Sjb		xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
332178479Sjb		    cnp->dn_string, nnp->dn_string);
333178479Sjb	}
334178479Sjb}
335178479Sjb
336178479Sjb/*
337178479Sjb * The #pragma error directive can be followed by any list of tokens, which we
338178479Sjb * just concatenate and print as part of our error message.
339178479Sjb */
340178479Sjbstatic void
341178479Sjbdt_pragma_error(const char *prname, dt_node_t *dnp)
342178479Sjb{
343178479Sjb	dt_node_t *enp;
344178479Sjb	size_t n = 0;
345178479Sjb	char *s;
346178479Sjb
347178479Sjb	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
348178479Sjb		if (enp->dn_kind == DT_NODE_IDENT ||
349178479Sjb		    enp->dn_kind == DT_NODE_STRING)
350178479Sjb			n += strlen(enp->dn_string) + 1;
351178479Sjb	}
352178479Sjb
353178479Sjb	s = alloca(n + 1);
354178479Sjb	s[0] = '\0';
355178479Sjb
356178479Sjb	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
357178479Sjb		if (enp->dn_kind == DT_NODE_IDENT ||
358178479Sjb		    enp->dn_kind == DT_NODE_STRING) {
359178479Sjb			(void) strcat(s, enp->dn_string);
360178479Sjb			(void) strcat(s, " ");
361178479Sjb		}
362178479Sjb	}
363178479Sjb
364178479Sjb	xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
365178479Sjb}
366178479Sjb
367178479Sjb/*ARGSUSED*/
368178479Sjbstatic void
369178479Sjbdt_pragma_ident(const char *prname, dt_node_t *dnp)
370178479Sjb{
371178479Sjb	/* ignore any #ident or #pragma ident lines */
372178479Sjb}
373178479Sjb
374178479Sjbstatic void
375178479Sjbdt_pragma_option(const char *prname, dt_node_t *dnp)
376178479Sjb{
377178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
378178479Sjb	char *opt, *val;
379178479Sjb
380178479Sjb	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
381178479Sjb		xyerror(D_PRAGMA_MALFORM,
382178479Sjb		    "malformed #pragma %s <option>=<val>\n", prname);
383178479Sjb	}
384178479Sjb
385178479Sjb	if (dnp->dn_list != NULL) {
386178479Sjb		xyerror(D_PRAGMA_MALFORM,
387178479Sjb		    "superfluous arguments specified for #pragma %s\n", prname);
388178479Sjb	}
389178479Sjb
390178479Sjb	opt = alloca(strlen(dnp->dn_string) + 1);
391178479Sjb	(void) strcpy(opt, dnp->dn_string);
392178479Sjb
393178479Sjb	if ((val = strchr(opt, '=')) != NULL)
394178479Sjb		*val++ = '\0';
395178479Sjb
396178479Sjb	if (dtrace_setopt(dtp, opt, val) == -1) {
397178479Sjb		if (val == NULL) {
398178479Sjb			xyerror(D_PRAGMA_OPTSET,
399178479Sjb			    "failed to set option '%s': %s\n", opt,
400178479Sjb			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
401178479Sjb		} else {
402178479Sjb			xyerror(D_PRAGMA_OPTSET,
403178479Sjb			    "failed to set option '%s' to '%s': %s\n",
404178479Sjb			    opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
405178479Sjb		}
406178479Sjb	}
407178479Sjb}
408178479Sjb
409178479Sjb/*
410178479Sjb * The #line directive is used to reset the input line number and to optionally
411178479Sjb * note the file name for use in error messages.  Sun cpp(1) also produces a
412178479Sjb * third integer token after the filename which is one of the following:
413178479Sjb *
414178479Sjb * 0 - line change has nothing to do with an #include file
415178479Sjb * 1 - line change because we just entered a #include file
416178479Sjb * 2 - line change because we just exited a #include file
417178479Sjb *
418178479Sjb * We use these state tokens to adjust pcb_idepth, which in turn controls
419178479Sjb * whether type lookups access the global type space or not.
420178479Sjb */
421178479Sjbstatic void
422178479Sjbdt_pragma_line(const char *prname, dt_node_t *dnp)
423178479Sjb{
424178479Sjb	dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
425178479Sjb	dt_node_t *inp = fnp ? fnp->dn_list : NULL;
426178479Sjb
427178479Sjb	if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
428178479Sjb	    (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
429178479Sjb	    (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
430178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #%s "
431178479Sjb		    "<line> [ [\"file\"] state ]\n", prname);
432178479Sjb	}
433178479Sjb
434178479Sjb	/*
435178479Sjb	 * If a file is specified, free any old pcb_filetag and swap fnp's
436178479Sjb	 * dn_string into pcb_filetag as the new filename for error messages.
437178479Sjb	 */
438178479Sjb	if (fnp != NULL) {
439178479Sjb		if (yypcb->pcb_filetag != NULL)
440178479Sjb			free(yypcb->pcb_filetag);
441178479Sjb
442178479Sjb		/*
443178479Sjb		 * This is not pretty, but is a necessary evil until we either
444178479Sjb		 * write "dpp" or get a useful standalone cpp from DevPro.  If
445178479Sjb		 * the filename begins with /dev/fd, we know it's the master
446178479Sjb		 * input file (see dt_preproc() in dt_cc.c), so just clear the
447178479Sjb		 * dt_filetag pointer so error messages refer to the main file.
448178479Sjb		 */
449178479Sjb		if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
450178479Sjb			yypcb->pcb_filetag = fnp->dn_string;
451178479Sjb			fnp->dn_string = NULL;
452178479Sjb		} else
453178479Sjb			yypcb->pcb_filetag = NULL;
454178479Sjb	}
455178479Sjb
456178479Sjb	if (inp != NULL) {
457178479Sjb		if (inp->dn_value == 1)
458178479Sjb			yypcb->pcb_idepth++;
459178479Sjb		else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
460178479Sjb			yypcb->pcb_idepth--;
461178479Sjb	}
462178479Sjb
463178479Sjb	yylineno = dnp->dn_value;
464178479Sjb}
465178479Sjb
466178479Sjb/*
467178479Sjb * D compiler pragma types range from control directives to common pragmas to
468178479Sjb * D custom pragmas, in order of specificity.  Similar to gcc, we use #pragma D
469178479Sjb * as a special prefix for our pragmas so they can be used in mixed headers.
470178479Sjb */
471178479Sjb#define	DT_PRAGMA_DIR	0	/* pragma directive may be used after naked # */
472178479Sjb#define	DT_PRAGMA_SUB	1	/* pragma directive may be used after #pragma */
473178479Sjb#define	DT_PRAGMA_DCP	2	/* pragma may only be used after #pragma D */
474178479Sjb
475178479Sjbstatic const struct dt_pragmadesc {
476178479Sjb	const char *dpd_name;
477178479Sjb	void (*dpd_func)(const char *, dt_node_t *);
478178479Sjb	int dpd_kind;
479178479Sjb} dt_pragmas[] = {
480178479Sjb	{ "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
481178479Sjb	{ "binding", dt_pragma_binding, DT_PRAGMA_DCP },
482178479Sjb	{ "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
483178479Sjb	{ "error", dt_pragma_error, DT_PRAGMA_DIR },
484178479Sjb	{ "ident", dt_pragma_ident, DT_PRAGMA_DIR },
485178479Sjb	{ "line", dt_pragma_line, DT_PRAGMA_DIR },
486178479Sjb	{ "option", dt_pragma_option, DT_PRAGMA_DCP },
487178479Sjb	{ NULL, NULL }
488178479Sjb};
489178479Sjb
490178479Sjb/*
491178479Sjb * Process a control line #directive by looking up the directive name in our
492178479Sjb * lookup table and invoking the corresponding function with the token list.
493178479Sjb * According to K&R[A12.9], we silently ignore null directive lines.
494178479Sjb */
495178479Sjbvoid
496178479Sjbdt_pragma(dt_node_t *pnp)
497178479Sjb{
498178479Sjb	const struct dt_pragmadesc *dpd;
499178479Sjb	dt_node_t *dnp;
500178479Sjb	int kind = DT_PRAGMA_DIR;
501178479Sjb
502178479Sjb	for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
503178479Sjb		if (dnp->dn_kind == DT_NODE_INT) {
504178479Sjb			dt_pragma_line("line", dnp);
505178479Sjb			break;
506178479Sjb		}
507178479Sjb
508178479Sjb		if (dnp->dn_kind != DT_NODE_IDENT)
509178479Sjb			xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
510178479Sjb
511178479Sjb		if (kind == DT_PRAGMA_DIR &&
512178479Sjb		    strcmp(dnp->dn_string, "pragma") == 0) {
513178479Sjb			kind = DT_PRAGMA_SUB;
514178479Sjb			continue;
515178479Sjb		}
516178479Sjb
517178479Sjb		if (kind == DT_PRAGMA_SUB &&
518178479Sjb		    strcmp(dnp->dn_string, "D") == 0) {
519178479Sjb			kind = DT_PRAGMA_DCP;
520178479Sjb			continue;
521178479Sjb		}
522178479Sjb
523178479Sjb		for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
524178479Sjb			if (dpd->dpd_kind <= kind &&
525178479Sjb			    strcmp(dpd->dpd_name, dnp->dn_string) == 0)
526178479Sjb				break;
527178479Sjb		}
528178479Sjb
529178479Sjb		yylineno--; /* since we've already seen \n */
530178479Sjb
531178479Sjb		if (dpd->dpd_name != NULL) {
532178479Sjb			dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
533178479Sjb			yylineno++;
534178479Sjb			break;
535178479Sjb		}
536178479Sjb
537178479Sjb		switch (kind) {
538178479Sjb		case DT_PRAGMA_DIR:
539178479Sjb			xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
540178479Sjb			    "#%s\n", dnp->dn_string);
541178479Sjb			/*NOTREACHED*/
542178479Sjb		case DT_PRAGMA_SUB:
543178479Sjb			break; /* K&R[A12.8] says to ignore unknown pragmas */
544178479Sjb		case DT_PRAGMA_DCP:
545178479Sjb		default:
546178479Sjb			xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
547178479Sjb			    dnp->dn_string);
548178479Sjb		}
549178479Sjb
550178479Sjb		yylineno++;
551178479Sjb		break;
552178479Sjb	}
553178479Sjb
554178479Sjb	dt_node_list_free(&pnp);
555178479Sjb}
556