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.
24238558Spfg * 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>
31277300Ssmh#ifdef illumos
32178479Sjb#include <alloca.h>
33178566Sjb#endif
34238558Spfg#include <fcntl.h>
35178479Sjb#include <stdlib.h>
36178479Sjb#include <stdio.h>
37178479Sjb
38238558Spfg#include <sys/types.h>
39270358Sdelphij#include <sys/sysctl.h>
40238558Spfg#include <sys/stat.h>
41238558Spfg
42178479Sjb#include <dt_parser.h>
43178479Sjb#include <dt_impl.h>
44178479Sjb#include <dt_provider.h>
45178479Sjb#include <dt_module.h>
46178479Sjb
47178479Sjb/*
48178479Sjb * This callback function is installed in a given identifier hash to search for
49178479Sjb * and apply deferred pragmas that are pending for a given new identifier name.
50178479Sjb * Multiple pragmas may be pending for a given name; we processs all of them.
51178479Sjb */
52178479Sjb/*ARGSUSED*/
53178479Sjbstatic void
54178479Sjbdt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp)
55178479Sjb{
56178479Sjb	dt_idhash_t *php;
57178479Sjb	dt_ident_t *pdp;
58178479Sjb
59178479Sjb	if ((php = yypcb->pcb_pragmas) == NULL)
60178479Sjb		return; /* no pragmas pending for current compilation pass */
61178479Sjb
62178479Sjb	while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) {
63178479Sjb		switch (pdp->di_kind) {
64178479Sjb		case DT_IDENT_PRAGAT:
65178479Sjb			idp->di_attr = pdp->di_attr;
66178479Sjb			break;
67178479Sjb		case DT_IDENT_PRAGBN:
68178479Sjb			idp->di_vers = pdp->di_vers;
69178479Sjb			break;
70178479Sjb		}
71178479Sjb		dt_idhash_delete(php, pdp);
72178479Sjb	}
73178479Sjb}
74178479Sjb
75178479Sjb/*
76178479Sjb * The #pragma attributes directive can be used to reset stability attributes
77178479Sjb * on a global identifier or inline definition.  If the identifier is already
78178479Sjb * defined, we can just change di_attr.  If not, we insert the pragma into a
79178479Sjb * hash table of the current pcb's deferred pragmas for later processing.
80178479Sjb */
81178479Sjbstatic void
82178479Sjbdt_pragma_attributes(const char *prname, dt_node_t *dnp)
83178479Sjb{
84178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
85178479Sjb	dtrace_attribute_t attr, *a;
86178479Sjb	dt_provider_t *pvp;
87178479Sjb	const char *name, *part;
88178479Sjb	dt_ident_t *idp;
89178479Sjb
90178479Sjb	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT ||
91178479Sjb	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
92178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
93178479Sjb		    "<attributes> <ident>\n", prname);
94178479Sjb	}
95178479Sjb
96178479Sjb	if (dtrace_str2attr(dnp->dn_string, &attr) == -1) {
97178479Sjb		xyerror(D_PRAGMA_INVAL, "invalid attributes "
98178479Sjb		    "specified by #pragma %s\n", prname);
99178479Sjb	}
100178479Sjb
101178479Sjb	dnp = dnp->dn_list;
102178479Sjb	name = dnp->dn_string;
103178479Sjb
104178479Sjb	if (strcmp(name, "provider") == 0) {
105178479Sjb		dnp = dnp->dn_list;
106178479Sjb		name = dnp->dn_string;
107178479Sjb
108178479Sjb		dnp = dnp->dn_list;
109178479Sjb		part = dnp->dn_string;
110178479Sjb
111178479Sjb		if ((pvp = dt_provider_lookup(dtp, name)) != NULL) {
112178479Sjb			if (strcmp(part, "provider") == 0) {
113178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_provider;
114178479Sjb			} else if (strcmp(part, "module") == 0) {
115178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_mod;
116178479Sjb			} else if (strcmp(part, "function") == 0) {
117178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_func;
118178479Sjb			} else if (strcmp(part, "name") == 0) {
119178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_name;
120178479Sjb			} else if (strcmp(part, "args") == 0) {
121178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_args;
122178479Sjb			} else {
123178479Sjb				xyerror(D_PRAGMA_INVAL, "invalid component "
124178479Sjb				    "\"%s\" in attribute #pragma "
125178479Sjb				    "for provider %s\n", name, part);
126178479Sjb			}
127178479Sjb
128178479Sjb			*a = attr;
129178479Sjb			return;
130178479Sjb		}
131178479Sjb
132178479Sjb	} else if ((idp = dt_idstack_lookup(
133178479Sjb	    &yypcb->pcb_globals, name)) != NULL) {
134178479Sjb
135178479Sjb		if (idp->di_gen != dtp->dt_gen) {
136178479Sjb			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
137178479Sjb			    "entity defined outside program scope\n", prname);
138178479Sjb		}
139178479Sjb
140178479Sjb		idp->di_attr = attr;
141178479Sjb		return;
142178479Sjb	}
143178479Sjb
144178479Sjb	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
145178479Sjb	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
146178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
147178479Sjb
148178479Sjb	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0,
149178479Sjb	    attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
150178479Sjb
151178479Sjb	if (idp == NULL)
152178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
153178479Sjb
154178479Sjb	if (dtp->dt_globals->dh_defer == NULL)
155178479Sjb		dtp->dt_globals->dh_defer = &dt_pragma_apply;
156178479Sjb}
157178479Sjb
158178479Sjb/*
159178479Sjb * The #pragma binding directive can be used to reset the version binding
160178479Sjb * on a global identifier or inline definition.  If the identifier is already
161178479Sjb * defined, we can just change di_vers.  If not, we insert the pragma into a
162178479Sjb * hash table of the current pcb's deferred pragmas for later processing.
163178479Sjb */
164178479Sjbstatic void
165178479Sjbdt_pragma_binding(const char *prname, dt_node_t *dnp)
166178479Sjb{
167178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
168178479Sjb	dt_version_t vers;
169178479Sjb	const char *name;
170178479Sjb	dt_ident_t *idp;
171178479Sjb
172178479Sjb	if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING ||
173178479Sjb	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
174178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
175178479Sjb		    "\"version\" <ident>\n", prname);
176178479Sjb	}
177178479Sjb
178178479Sjb	if (dt_version_str2num(dnp->dn_string, &vers) == -1) {
179178479Sjb		xyerror(D_PRAGMA_INVAL, "invalid version string "
180178479Sjb		    "specified by #pragma %s\n", prname);
181178479Sjb	}
182178479Sjb
183178479Sjb	name = dnp->dn_list->dn_string;
184178479Sjb	idp = dt_idstack_lookup(&yypcb->pcb_globals, name);
185178479Sjb
186178479Sjb	if (idp != NULL) {
187178479Sjb		if (idp->di_gen != dtp->dt_gen) {
188178479Sjb			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
189178479Sjb			    "entity defined outside program scope\n", prname);
190178479Sjb		}
191178479Sjb		idp->di_vers = vers;
192178479Sjb		return;
193178479Sjb	}
194178479Sjb
195178479Sjb	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
196178479Sjb	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
197178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
198178479Sjb
199178479Sjb	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0,
200178479Sjb	    _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
201178479Sjb
202178479Sjb	if (idp == NULL)
203178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
204178479Sjb
205178479Sjb	if (dtp->dt_globals->dh_defer == NULL)
206178479Sjb		dtp->dt_globals->dh_defer = &dt_pragma_apply;
207178479Sjb}
208178479Sjb
209238558Spfgstatic void
210238558Spfgdt_pragma_depends_finddep(dtrace_hdl_t *dtp, const char *lname, char *lib,
211238558Spfg    size_t len)
212238558Spfg{
213238558Spfg	dt_dirpath_t *dirp;
214238558Spfg	struct stat sbuf;
215238558Spfg	int found = 0;
216238558Spfg
217238558Spfg	for (dirp = dt_list_next(&dtp->dt_lib_path); dirp != NULL;
218238558Spfg	    dirp = dt_list_next(dirp)) {
219238558Spfg		(void) snprintf(lib, len, "%s/%s", dirp->dir_path, lname);
220238558Spfg
221238558Spfg		if (stat(lib, &sbuf) == 0) {
222238558Spfg			found = 1;
223238558Spfg			break;
224238558Spfg		}
225238558Spfg	}
226238558Spfg
227238558Spfg	if (!found)
228238558Spfg		xyerror(D_PRAGMA_DEPEND,
229238558Spfg		    "failed to find dependency in libpath: %s", lname);
230238558Spfg}
231238558Spfg
232178479Sjb/*
233178479Sjb * The #pragma depends_on directive can be used to express a dependency on a
234178479Sjb * module, provider or library which if not present will cause processing to
235178479Sjb * abort.
236178479Sjb */
237178479Sjbstatic void
238178479Sjbdt_pragma_depends(const char *prname, dt_node_t *cnp)
239178479Sjb{
240178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
241178479Sjb	dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
242178479Sjb	int found;
243178479Sjb	dt_lib_depend_t *dld;
244178566Sjb	char lib[MAXPATHLEN];
245248848Sgnn	size_t plen;
246248848Sgnn	char *provs, *cpy, *tok;
247178479Sjb
248178479Sjb	if (cnp == NULL || nnp == NULL ||
249178479Sjb	    cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
250178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
251178479Sjb		    "<class> <name>\n", prname);
252178479Sjb	}
253178479Sjb
254248848Sgnn	if (strcmp(cnp->dn_string, "provider") == 0) {
255248848Sgnn		/*
256248848Sgnn		 * First try to get the provider list using the
257248848Sgnn		 * debug.dtrace.providers sysctl, since that'll work even if
258248848Sgnn		 * we're not running as root.
259248848Sgnn		 */
260248848Sgnn		provs = NULL;
261248848Sgnn		if (sysctlbyname("debug.dtrace.providers", NULL, &plen, NULL, 0) ||
262248848Sgnn		    ((provs = dt_alloc(dtp, plen)) == NULL) ||
263248848Sgnn		    sysctlbyname("debug.dtrace.providers", provs, &plen, NULL, 0))
264248848Sgnn			found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
265248848Sgnn		else {
266248848Sgnn			found = B_FALSE;
267248848Sgnn			for (cpy = provs; (tok = strsep(&cpy, " ")) != NULL; )
268248848Sgnn				if (strcmp(tok, nnp->dn_string) == 0) {
269248848Sgnn					found = B_TRUE;
270248848Sgnn					break;
271248848Sgnn				}
272248848Sgnn			if (found == B_FALSE)
273248848Sgnn				found = dt_provider_lookup(dtp,
274248848Sgnn				    nnp->dn_string) != NULL;
275248848Sgnn		}
276248848Sgnn		if (provs != NULL)
277248848Sgnn			dt_free(dtp, provs);
278248848Sgnn	} else if (strcmp(cnp->dn_string, "module") == 0) {
279178479Sjb		dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
280178479Sjb		found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
281284085Smarkj#ifdef __FreeBSD__
282284085Smarkj		if (!found) {
283284085Smarkj			dt_kmodule_t *dkmp = dt_kmodule_lookup(dtp,
284284085Smarkj			    nnp->dn_string);
285284085Smarkj			found = dkmp != NULL &&
286284085Smarkj			    dt_module_getctf(dtp, dkmp->dkm_module) != NULL;
287284085Smarkj		}
288284085Smarkj#endif
289178479Sjb	} else if (strcmp(cnp->dn_string, "library") == 0) {
290178479Sjb		if (yypcb->pcb_cflags & DTRACE_C_CTL) {
291178566Sjb			assert(dtp->dt_filetag != NULL);
292178479Sjb
293238558Spfg			dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
294238558Spfg			    sizeof (lib));
295238558Spfg
296178479Sjb			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
297178479Sjb			    dtp->dt_filetag);
298178479Sjb			assert(dld != NULL);
299178479Sjb
300178479Sjb			if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies,
301178479Sjb			    lib)) != 0) {
302178479Sjb				xyerror(D_PRAGMA_DEPEND,
303178566Sjb				    "failed to add dependency %s:%s\n", lib,
304178479Sjb				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
305178479Sjb			}
306178566Sjb		} else {
307178566Sjb			/*
308178566Sjb			 * By this point we have already performed a topological
309178566Sjb			 * sort of the dependencies; we process this directive
310178566Sjb			 * as satisfied as long as the dependency was properly
311178566Sjb			 * loaded.
312178566Sjb			 */
313178566Sjb			if (dtp->dt_filetag == NULL)
314178566Sjb				xyerror(D_PRAGMA_DEPEND, "main program may "
315178566Sjb				    "not explicitly depend on a library");
316178566Sjb
317178566Sjb			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
318178566Sjb			    dtp->dt_filetag);
319178566Sjb			assert(dld != NULL);
320178566Sjb
321238558Spfg			dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
322238558Spfg			    sizeof (lib));
323178566Sjb			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
324178566Sjb			    lib);
325178566Sjb			assert(dld != NULL);
326178566Sjb
327178566Sjb			if (!dld->dtld_loaded)
328178566Sjb				xyerror(D_PRAGMA_DEPEND, "program requires "
329178566Sjb				    "library \"%s\" which failed to load",
330178566Sjb				    lib);
331178479Sjb		}
332178566Sjb
333178566Sjb		found = B_TRUE;
334178479Sjb	} else {
335178479Sjb		xyerror(D_PRAGMA_INVAL, "invalid class %s "
336178479Sjb		    "specified by #pragma %s\n", cnp->dn_string, prname);
337178479Sjb	}
338178479Sjb
339178479Sjb	if (!found) {
340178479Sjb		xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
341178479Sjb		    cnp->dn_string, nnp->dn_string);
342178479Sjb	}
343178479Sjb}
344178479Sjb
345178479Sjb/*
346178479Sjb * The #pragma error directive can be followed by any list of tokens, which we
347178479Sjb * just concatenate and print as part of our error message.
348178479Sjb */
349178479Sjbstatic void
350178479Sjbdt_pragma_error(const char *prname, dt_node_t *dnp)
351178479Sjb{
352178479Sjb	dt_node_t *enp;
353178479Sjb	size_t n = 0;
354178479Sjb	char *s;
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			n += strlen(enp->dn_string) + 1;
360178479Sjb	}
361178479Sjb
362178479Sjb	s = alloca(n + 1);
363178479Sjb	s[0] = '\0';
364178479Sjb
365178479Sjb	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
366178479Sjb		if (enp->dn_kind == DT_NODE_IDENT ||
367178479Sjb		    enp->dn_kind == DT_NODE_STRING) {
368178479Sjb			(void) strcat(s, enp->dn_string);
369178479Sjb			(void) strcat(s, " ");
370178479Sjb		}
371178479Sjb	}
372178479Sjb
373178479Sjb	xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
374178479Sjb}
375178479Sjb
376178479Sjb/*ARGSUSED*/
377178479Sjbstatic void
378178479Sjbdt_pragma_ident(const char *prname, dt_node_t *dnp)
379178479Sjb{
380178479Sjb	/* ignore any #ident or #pragma ident lines */
381178479Sjb}
382178479Sjb
383178479Sjbstatic void
384178479Sjbdt_pragma_option(const char *prname, dt_node_t *dnp)
385178479Sjb{
386178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
387178479Sjb	char *opt, *val;
388178479Sjb
389178479Sjb	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
390178479Sjb		xyerror(D_PRAGMA_MALFORM,
391178479Sjb		    "malformed #pragma %s <option>=<val>\n", prname);
392178479Sjb	}
393178479Sjb
394178479Sjb	if (dnp->dn_list != NULL) {
395178479Sjb		xyerror(D_PRAGMA_MALFORM,
396178479Sjb		    "superfluous arguments specified for #pragma %s\n", prname);
397178479Sjb	}
398178479Sjb
399178479Sjb	opt = alloca(strlen(dnp->dn_string) + 1);
400178479Sjb	(void) strcpy(opt, dnp->dn_string);
401178479Sjb
402178479Sjb	if ((val = strchr(opt, '=')) != NULL)
403178479Sjb		*val++ = '\0';
404178479Sjb
405178479Sjb	if (dtrace_setopt(dtp, opt, val) == -1) {
406178479Sjb		if (val == NULL) {
407178479Sjb			xyerror(D_PRAGMA_OPTSET,
408178479Sjb			    "failed to set option '%s': %s\n", opt,
409178479Sjb			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
410178479Sjb		} else {
411178479Sjb			xyerror(D_PRAGMA_OPTSET,
412178479Sjb			    "failed to set option '%s' to '%s': %s\n",
413178479Sjb			    opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
414178479Sjb		}
415178479Sjb	}
416178479Sjb}
417178479Sjb
418178479Sjb/*
419178479Sjb * The #line directive is used to reset the input line number and to optionally
420178479Sjb * note the file name for use in error messages.  Sun cpp(1) also produces a
421178479Sjb * third integer token after the filename which is one of the following:
422178479Sjb *
423178479Sjb * 0 - line change has nothing to do with an #include file
424178479Sjb * 1 - line change because we just entered a #include file
425178479Sjb * 2 - line change because we just exited a #include file
426178479Sjb *
427178479Sjb * We use these state tokens to adjust pcb_idepth, which in turn controls
428178479Sjb * whether type lookups access the global type space or not.
429178479Sjb */
430178479Sjbstatic void
431178479Sjbdt_pragma_line(const char *prname, dt_node_t *dnp)
432178479Sjb{
433178479Sjb	dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
434178479Sjb	dt_node_t *inp = fnp ? fnp->dn_list : NULL;
435178479Sjb
436178479Sjb	if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
437178479Sjb	    (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
438178479Sjb	    (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
439178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #%s "
440178479Sjb		    "<line> [ [\"file\"] state ]\n", prname);
441178479Sjb	}
442178479Sjb
443178479Sjb	/*
444178479Sjb	 * If a file is specified, free any old pcb_filetag and swap fnp's
445178479Sjb	 * dn_string into pcb_filetag as the new filename for error messages.
446178479Sjb	 */
447178479Sjb	if (fnp != NULL) {
448178479Sjb		if (yypcb->pcb_filetag != NULL)
449178479Sjb			free(yypcb->pcb_filetag);
450178479Sjb
451178479Sjb		/*
452178479Sjb		 * This is not pretty, but is a necessary evil until we either
453178479Sjb		 * write "dpp" or get a useful standalone cpp from DevPro.  If
454178479Sjb		 * the filename begins with /dev/fd, we know it's the master
455178479Sjb		 * input file (see dt_preproc() in dt_cc.c), so just clear the
456178479Sjb		 * dt_filetag pointer so error messages refer to the main file.
457178479Sjb		 */
458178479Sjb		if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
459178479Sjb			yypcb->pcb_filetag = fnp->dn_string;
460178479Sjb			fnp->dn_string = NULL;
461178479Sjb		} else
462178479Sjb			yypcb->pcb_filetag = NULL;
463178479Sjb	}
464178479Sjb
465178479Sjb	if (inp != NULL) {
466178479Sjb		if (inp->dn_value == 1)
467178479Sjb			yypcb->pcb_idepth++;
468178479Sjb		else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
469178479Sjb			yypcb->pcb_idepth--;
470178479Sjb	}
471178479Sjb
472178479Sjb	yylineno = dnp->dn_value;
473178479Sjb}
474178479Sjb
475178479Sjb/*
476178479Sjb * D compiler pragma types range from control directives to common pragmas to
477178479Sjb * D custom pragmas, in order of specificity.  Similar to gcc, we use #pragma D
478178479Sjb * as a special prefix for our pragmas so they can be used in mixed headers.
479178479Sjb */
480178479Sjb#define	DT_PRAGMA_DIR	0	/* pragma directive may be used after naked # */
481178479Sjb#define	DT_PRAGMA_SUB	1	/* pragma directive may be used after #pragma */
482178479Sjb#define	DT_PRAGMA_DCP	2	/* pragma may only be used after #pragma D */
483178479Sjb
484178479Sjbstatic const struct dt_pragmadesc {
485178479Sjb	const char *dpd_name;
486178479Sjb	void (*dpd_func)(const char *, dt_node_t *);
487178479Sjb	int dpd_kind;
488178479Sjb} dt_pragmas[] = {
489178479Sjb	{ "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
490178479Sjb	{ "binding", dt_pragma_binding, DT_PRAGMA_DCP },
491178479Sjb	{ "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
492178479Sjb	{ "error", dt_pragma_error, DT_PRAGMA_DIR },
493178479Sjb	{ "ident", dt_pragma_ident, DT_PRAGMA_DIR },
494178479Sjb	{ "line", dt_pragma_line, DT_PRAGMA_DIR },
495178479Sjb	{ "option", dt_pragma_option, DT_PRAGMA_DCP },
496178479Sjb	{ NULL, NULL }
497178479Sjb};
498178479Sjb
499178479Sjb/*
500178479Sjb * Process a control line #directive by looking up the directive name in our
501178479Sjb * lookup table and invoking the corresponding function with the token list.
502178479Sjb * According to K&R[A12.9], we silently ignore null directive lines.
503178479Sjb */
504178479Sjbvoid
505178479Sjbdt_pragma(dt_node_t *pnp)
506178479Sjb{
507178479Sjb	const struct dt_pragmadesc *dpd;
508178479Sjb	dt_node_t *dnp;
509178479Sjb	int kind = DT_PRAGMA_DIR;
510178479Sjb
511178479Sjb	for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
512178479Sjb		if (dnp->dn_kind == DT_NODE_INT) {
513178479Sjb			dt_pragma_line("line", dnp);
514178479Sjb			break;
515178479Sjb		}
516178479Sjb
517178479Sjb		if (dnp->dn_kind != DT_NODE_IDENT)
518178479Sjb			xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
519178479Sjb
520178479Sjb		if (kind == DT_PRAGMA_DIR &&
521178479Sjb		    strcmp(dnp->dn_string, "pragma") == 0) {
522178479Sjb			kind = DT_PRAGMA_SUB;
523178479Sjb			continue;
524178479Sjb		}
525178479Sjb
526178479Sjb		if (kind == DT_PRAGMA_SUB &&
527178479Sjb		    strcmp(dnp->dn_string, "D") == 0) {
528178479Sjb			kind = DT_PRAGMA_DCP;
529178479Sjb			continue;
530178479Sjb		}
531178479Sjb
532178479Sjb		for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
533178479Sjb			if (dpd->dpd_kind <= kind &&
534178479Sjb			    strcmp(dpd->dpd_name, dnp->dn_string) == 0)
535178479Sjb				break;
536178479Sjb		}
537178479Sjb
538178479Sjb		yylineno--; /* since we've already seen \n */
539178479Sjb
540178479Sjb		if (dpd->dpd_name != NULL) {
541178479Sjb			dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
542178479Sjb			yylineno++;
543178479Sjb			break;
544178479Sjb		}
545178479Sjb
546178479Sjb		switch (kind) {
547178479Sjb		case DT_PRAGMA_DIR:
548178479Sjb			xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
549178479Sjb			    "#%s\n", dnp->dn_string);
550178479Sjb			/*NOTREACHED*/
551178479Sjb		case DT_PRAGMA_SUB:
552178479Sjb			break; /* K&R[A12.8] says to ignore unknown pragmas */
553178479Sjb		case DT_PRAGMA_DCP:
554178479Sjb		default:
555178479Sjb			xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
556178479Sjb			    dnp->dn_string);
557178479Sjb		}
558178479Sjb
559178479Sjb		yylineno++;
560178479Sjb		break;
561178479Sjb	}
562178479Sjb
563178479Sjb	dt_node_list_free(&pnp);
564178479Sjb}
565