Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/dt_decl.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2005 Sun Microsystems, Inc. All rights reserved.23* Copyright (c) 2013 by Delphix. All rights reserved.24* Copyright (c) 2013 Joyent, Inc. All rights reserved.25* Use is subject to license terms.26*/2728#pragma ident "%Z%%M% %I% %E% SMI"2930#include <strings.h>31#include <stdlib.h>32#include <limits.h>33#include <alloca.h>34#include <assert.h>3536#include <dt_decl.h>37#include <dt_parser.h>38#include <dt_module.h>39#include <dt_impl.h>4041static dt_decl_t *42dt_decl_check(dt_decl_t *ddp)43{44if (ddp->dd_kind == CTF_K_UNKNOWN)45return (ddp); /* nothing to check if the type is not yet set */4647if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "char") == 0 &&48(ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG))) {49xyerror(D_DECL_CHARATTR, "invalid type declaration: short and "50"long may not be used with char type\n");51}5253if (ddp->dd_name != NULL && strcmp(ddp->dd_name, "void") == 0 &&54(ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG |55(DT_DA_SIGNED | DT_DA_UNSIGNED)))) {56xyerror(D_DECL_VOIDATTR, "invalid type declaration: attributes "57"may not be used with void type\n");58}5960if (ddp->dd_kind != CTF_K_INTEGER &&61(ddp->dd_attr & (DT_DA_SIGNED | DT_DA_UNSIGNED))) {62xyerror(D_DECL_SIGNINT, "invalid type declaration: signed and "63"unsigned may only be used with integer type\n");64}6566if (ddp->dd_kind != CTF_K_INTEGER && ddp->dd_kind != CTF_K_FLOAT &&67(ddp->dd_attr & (DT_DA_LONG | DT_DA_LONGLONG))) {68xyerror(D_DECL_LONGINT, "invalid type declaration: long and "69"long long may only be used with integer or "70"floating-point type\n");71}7273return (ddp);74}7576dt_decl_t *77dt_decl_alloc(ushort_t kind, char *name)78{79dt_decl_t *ddp = malloc(sizeof (dt_decl_t));8081if (ddp == NULL)82longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);8384ddp->dd_kind = kind;85ddp->dd_attr = 0;86ddp->dd_ctfp = NULL;87ddp->dd_type = CTF_ERR;88ddp->dd_name = name;89ddp->dd_node = NULL;90ddp->dd_next = NULL;9192return (ddp);93}9495void96dt_decl_free(dt_decl_t *ddp)97{98dt_decl_t *ndp;99100for (; ddp != NULL; ddp = ndp) {101ndp = ddp->dd_next;102free(ddp->dd_name);103dt_node_list_free(&ddp->dd_node);104free(ddp);105}106}107108void109dt_decl_reset(void)110{111dt_scope_t *dsp = &yypcb->pcb_dstack;112dt_decl_t *ddp = dsp->ds_decl;113114while (ddp->dd_next != NULL) {115dsp->ds_decl = ddp->dd_next;116ddp->dd_next = NULL;117dt_decl_free(ddp);118ddp = dsp->ds_decl;119}120}121122dt_decl_t *123dt_decl_push(dt_decl_t *ddp)124{125dt_scope_t *dsp = &yypcb->pcb_dstack;126dt_decl_t *top = dsp->ds_decl;127128if (top != NULL &&129top->dd_kind == CTF_K_UNKNOWN && top->dd_name == NULL) {130top->dd_kind = CTF_K_INTEGER;131(void) dt_decl_check(top);132}133134assert(ddp->dd_next == NULL);135ddp->dd_next = top;136dsp->ds_decl = ddp;137138return (ddp);139}140141dt_decl_t *142dt_decl_pop(void)143{144dt_scope_t *dsp = &yypcb->pcb_dstack;145dt_decl_t *ddp = dt_decl_top();146147dsp->ds_decl = NULL;148free(dsp->ds_ident);149dsp->ds_ident = NULL;150dsp->ds_ctfp = NULL;151dsp->ds_type = CTF_ERR;152dsp->ds_class = DT_DC_DEFAULT;153dsp->ds_enumval = -1;154155return (ddp);156}157158dt_decl_t *159dt_decl_pop_param(char **idp)160{161dt_scope_t *dsp = &yypcb->pcb_dstack;162163if (dsp->ds_class != DT_DC_DEFAULT && dsp->ds_class != DT_DC_REGISTER) {164xyerror(D_DECL_PARMCLASS, "inappropriate storage class "165"for function or associative array parameter\n");166}167168if (idp != NULL && dt_decl_top() != NULL) {169*idp = dsp->ds_ident;170dsp->ds_ident = NULL;171}172173return (dt_decl_pop());174}175176dt_decl_t *177dt_decl_top(void)178{179dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;180181if (ddp == NULL)182longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);183184if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {185ddp->dd_kind = CTF_K_INTEGER;186(void) dt_decl_check(ddp);187}188189return (ddp);190}191192dt_decl_t *193dt_decl_ident(char *name)194{195dt_scope_t *dsp = &yypcb->pcb_dstack;196dt_decl_t *ddp = dsp->ds_decl;197198if (dsp->ds_ident != NULL) {199free(name);200xyerror(D_DECL_IDENT, "old-style declaration or "201"incorrect type specified\n");202}203204dsp->ds_ident = name;205206if (ddp == NULL)207ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));208209return (ddp);210}211212void213dt_decl_class(dt_dclass_t class)214{215dt_scope_t *dsp = &yypcb->pcb_dstack;216217if (dsp->ds_class != DT_DC_DEFAULT) {218xyerror(D_DECL_CLASS, "only one storage class allowed "219"in a declaration\n");220}221222dsp->ds_class = class;223}224225/*226* Set the kind and name of the current declaration. If none is allocated,227* make a new decl and push it on to the top of our stack. If the name or kind228* is already set for the current decl, then we need to fail this declaration.229* This can occur because too many types were given (e.g. "int int"), etc.230*/231dt_decl_t *232dt_decl_spec(ushort_t kind, char *name)233{234dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;235236if (ddp == NULL)237return (dt_decl_push(dt_decl_alloc(kind, name)));238239/*240* If we already have a type name specified and we see another type241* name, this is an error if the declaration is a typedef. If the242* declaration is not a typedef, then the user may be trying to declare243* a variable whose name has been returned by lex as a TNAME token:244* call dt_decl_ident() as if the grammar's IDENT rule was matched.245*/246if (ddp->dd_name != NULL && kind == CTF_K_TYPEDEF) {247if (yypcb->pcb_dstack.ds_class != DT_DC_TYPEDEF)248return (dt_decl_ident(name));249xyerror(D_DECL_IDRED, "identifier redeclared: %s\n", name);250}251252if (ddp->dd_name != NULL || ddp->dd_kind != CTF_K_UNKNOWN)253xyerror(D_DECL_COMBO, "invalid type combination\n");254255ddp->dd_kind = kind;256ddp->dd_name = name;257258return (dt_decl_check(ddp));259}260261dt_decl_t *262dt_decl_attr(ushort_t attr)263{264dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;265266if (ddp == NULL) {267ddp = dt_decl_push(dt_decl_alloc(CTF_K_UNKNOWN, NULL));268ddp->dd_attr = attr;269return (ddp);270}271272if (attr == DT_DA_LONG && (ddp->dd_attr & DT_DA_LONG)) {273ddp->dd_attr &= ~DT_DA_LONG;274attr = DT_DA_LONGLONG;275}276277ddp->dd_attr |= attr;278return (dt_decl_check(ddp));279}280281/*282* Examine the list of formal parameters 'flist' and determine if the formal283* name fnp->dn_string is defined in this list (B_TRUE) or not (B_FALSE).284* If 'fnp' is in 'flist', do not search beyond 'fnp' itself in 'flist'.285*/286static int287dt_decl_protoform(dt_node_t *fnp, dt_node_t *flist)288{289dt_node_t *dnp;290291for (dnp = flist; dnp != fnp && dnp != NULL; dnp = dnp->dn_list) {292if (dnp->dn_string != NULL &&293strcmp(dnp->dn_string, fnp->dn_string) == 0)294return (B_TRUE);295}296297return (B_FALSE);298}299300/*301* Common code for parsing array, function, and probe definition prototypes.302* The prototype node list is specified as 'plist'. The formal prototype303* against which to compare the prototype is specified as 'flist'. If plist304* and flist are the same, we require that named parameters are unique. If305* plist and flist are different, we require that named parameters in plist306* match a name that is present in flist.307*/308int309dt_decl_prototype(dt_node_t *plist,310dt_node_t *flist, const char *kind, uint_t flags)311{312char n[DT_TYPE_NAMELEN];313int is_void, v = 0, i = 1;314int form = plist != flist;315dt_node_t *dnp;316317for (dnp = plist; dnp != NULL; dnp = dnp->dn_list, i++) {318319if (dnp->dn_type == CTF_ERR && !(flags & DT_DP_VARARGS)) {320dnerror(dnp, D_DECL_PROTO_VARARGS, "%s prototype may "321"not use a variable-length argument list\n", kind);322}323324if (dt_node_is_dynamic(dnp) && !(flags & DT_DP_DYNAMIC)) {325dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "326"use parameter of type %s: %s, parameter #%d\n",327kind, dt_node_type_name(dnp, n, sizeof (n)),328dnp->dn_string ? dnp->dn_string : "(anonymous)", i);329}330331is_void = dt_node_is_void(dnp);332v += is_void;333334if (is_void && !(flags & DT_DP_VOID)) {335dnerror(dnp, D_DECL_PROTO_TYPE, "%s prototype may not "336"use parameter of type %s: %s, parameter #%d\n",337kind, dt_node_type_name(dnp, n, sizeof (n)),338dnp->dn_string ? dnp->dn_string : "(anonymous)", i);339}340341if (is_void && dnp->dn_string != NULL) {342dnerror(dnp, D_DECL_PROTO_NAME, "void parameter may "343"not have a name: %s\n", dnp->dn_string);344}345346if (dnp->dn_string != NULL &&347dt_decl_protoform(dnp, flist) != form) {348dnerror(dnp, D_DECL_PROTO_FORM, "parameter is "349"%s declared in %s prototype: %s, parameter #%d\n",350form ? "not" : "already", kind, dnp->dn_string, i);351}352353if (dnp->dn_string == NULL &&354!is_void && !(flags & DT_DP_ANON)) {355dnerror(dnp, D_DECL_PROTO_NAME, "parameter declaration "356"requires a name: parameter #%d\n", i);357}358}359360if (v != 0 && plist->dn_list != NULL)361xyerror(D_DECL_PROTO_VOID, "void must be sole parameter\n");362363return (v ? 0 : i - 1); /* return zero if sole parameter is 'void' */364}365366dt_decl_t *367dt_decl_array(dt_node_t *dnp)368{369dt_decl_t *ddp = dt_decl_push(dt_decl_alloc(CTF_K_ARRAY, NULL));370dt_scope_t *dsp = &yypcb->pcb_dstack;371dt_decl_t *ndp = ddp;372373/*374* After pushing the array on to the decl stack, scan ahead for multi-375* dimensional array declarations and push the current decl to the376* bottom to match the resulting CTF type tree and data layout. Refer377* to the comments in dt_decl_type() and ISO C 6.5.2.1 for more info.378*/379while (ndp->dd_next != NULL && ndp->dd_next->dd_kind == CTF_K_ARRAY)380ndp = ndp->dd_next; /* skip to bottom-most array declaration */381382if (ndp != ddp) {383if (dnp != NULL && dnp->dn_kind == DT_NODE_TYPE) {384xyerror(D_DECL_DYNOBJ,385"cannot declare array of associative arrays\n");386}387dsp->ds_decl = ddp->dd_next;388ddp->dd_next = ndp->dd_next;389ndp->dd_next = ddp;390}391392if (ddp->dd_next->dd_name != NULL &&393strcmp(ddp->dd_next->dd_name, "void") == 0)394xyerror(D_DECL_VOIDOBJ, "cannot declare array of void\n");395396if (dnp != NULL && dnp->dn_kind != DT_NODE_TYPE) {397dnp = ddp->dd_node = dt_node_cook(dnp, DT_IDFLG_REF);398399if (dt_node_is_posconst(dnp) == 0) {400xyerror(D_DECL_ARRSUB, "positive integral constant "401"expression or tuple signature expected as "402"array declaration subscript\n");403}404405if (dnp->dn_value > UINT_MAX)406xyerror(D_DECL_ARRBIG, "array dimension too big\n");407408} else if (dnp != NULL) {409ddp->dd_node = dnp;410(void) dt_decl_prototype(dnp, dnp, "array", DT_DP_ANON);411}412413return (ddp);414}415416/*417* When a function is declared, we need to fudge the decl stack a bit if the418* declaration uses the function pointer (*)() syntax. In this case, the419* dt_decl_func() call occurs *after* the dt_decl_ptr() call, even though the420* resulting type is "pointer to function". To make the pointer land on top,421* we check to see if 'pdp' is non-NULL and a pointer. If it is, we search422* backward for a decl tagged with DT_DA_PAREN, and if one is found, the func423* decl is inserted behind this node in the decl list instead of at the top.424* In all cases, the func decl's dd_next pointer is set to the decl chain425* for the function's return type and the function parameter list is discarded.426*/427dt_decl_t *428dt_decl_func(dt_decl_t *pdp, dt_node_t *dnp)429{430dt_decl_t *ddp = dt_decl_alloc(CTF_K_FUNCTION, NULL);431432ddp->dd_node = dnp;433434(void) dt_decl_prototype(dnp, dnp, "function",435DT_DP_VARARGS | DT_DP_VOID | DT_DP_ANON);436437if (pdp == NULL || pdp->dd_kind != CTF_K_POINTER)438return (dt_decl_push(ddp));439440while (pdp->dd_next != NULL && !(pdp->dd_next->dd_attr & DT_DA_PAREN))441pdp = pdp->dd_next;442443if (pdp->dd_next == NULL)444return (dt_decl_push(ddp));445446ddp->dd_next = pdp->dd_next;447pdp->dd_next = ddp;448449return (pdp);450}451452dt_decl_t *453dt_decl_ptr(void)454{455return (dt_decl_push(dt_decl_alloc(CTF_K_POINTER, NULL)));456}457458dt_decl_t *459dt_decl_sou(uint_t kind, char *name)460{461dt_decl_t *ddp = dt_decl_spec(kind, name);462char n[DT_TYPE_NAMELEN];463ctf_file_t *ctfp;464ctf_id_t type;465uint_t flag;466467if (yypcb->pcb_idepth != 0)468ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;469else470ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;471472if (yypcb->pcb_dstack.ds_next != NULL)473flag = CTF_ADD_NONROOT;474else475flag = CTF_ADD_ROOT;476477(void) snprintf(n, sizeof (n), "%s %s",478kind == CTF_K_STRUCT ? "struct" : "union",479name == NULL ? "(anon)" : name);480481if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR &&482ctf_type_kind(ctfp, type) != CTF_K_FORWARD)483xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);484485if (kind == CTF_K_STRUCT)486type = ctf_add_struct(ctfp, flag, name);487else488type = ctf_add_union(ctfp, flag, name);489490if (type == CTF_ERR || ctf_update(ctfp) == CTF_ERR) {491xyerror(D_UNKNOWN, "failed to define %s: %s\n",492n, ctf_errmsg(ctf_errno(ctfp)));493}494495ddp->dd_ctfp = ctfp;496ddp->dd_type = type;497498dt_scope_push(ctfp, type);499return (ddp);500}501502void503dt_decl_member(dt_node_t *dnp)504{505dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;506dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;507char *ident = yypcb->pcb_dstack.ds_ident;508509const char *idname = ident ? ident : "(anon)";510char n[DT_TYPE_NAMELEN];511512dtrace_typeinfo_t dtt;513ctf_encoding_t cte;514ctf_id_t base;515uint_t kind;516ssize_t size;517518if (dsp == NULL)519longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);520521if (ddp == NULL)522longjmp(yypcb->pcb_jmpbuf, EDT_NODECL);523524if (dnp == NULL && ident == NULL)525xyerror(D_DECL_MNAME, "member declaration requires a name\n");526527if (ddp->dd_kind == CTF_K_UNKNOWN && ddp->dd_name == NULL) {528ddp->dd_kind = CTF_K_INTEGER;529(void) dt_decl_check(ddp);530}531532if (dt_decl_type(ddp, &dtt) != 0)533longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);534535if (ident != NULL && strchr(ident, '`') != NULL) {536xyerror(D_DECL_SCOPE, "D scoping operator may not be used "537"in a member name (%s)\n", ident);538}539540if (dtt.dtt_ctfp == DT_DYN_CTFP(yypcb->pcb_hdl) &&541dtt.dtt_type == DT_DYN_TYPE(yypcb->pcb_hdl)) {542xyerror(D_DECL_DYNOBJ,543"cannot have dynamic member: %s\n", ident);544}545546base = ctf_type_resolve(dtt.dtt_ctfp, dtt.dtt_type);547kind = ctf_type_kind(dtt.dtt_ctfp, base);548size = ctf_type_size(dtt.dtt_ctfp, base);549550if (kind == CTF_K_FORWARD || ((kind == CTF_K_STRUCT ||551kind == CTF_K_UNION) && size == 0)) {552xyerror(D_DECL_INCOMPLETE, "incomplete struct/union/enum %s: "553"%s\n", dt_type_name(dtt.dtt_ctfp, dtt.dtt_type,554n, sizeof (n)), ident);555}556557if (size == 0)558xyerror(D_DECL_VOIDOBJ, "cannot have void member: %s\n", ident);559560/*561* If a bit-field qualifier was part of the member declaration, create562* a new integer type of the same name and attributes as the base type563* and size equal to the specified number of bits. We reset 'dtt' to564* refer to this new bit-field type and continue on to add the member.565*/566if (dnp != NULL) {567dnp = dt_node_cook(dnp, DT_IDFLG_REF);568569/*570* A bit-field member with no declarator is permitted to have571* size zero and indicates that no more fields are to be packed572* into the current storage unit. We ignore these directives573* as the underlying ctf code currently does so for all fields.574*/575if (ident == NULL && dnp->dn_kind == DT_NODE_INT &&576dnp->dn_value == 0) {577dt_node_free(dnp);578goto done;579}580581if (dt_node_is_posconst(dnp) == 0) {582xyerror(D_DECL_BFCONST, "positive integral constant "583"expression expected as bit-field size\n");584}585586if (ctf_type_kind(dtt.dtt_ctfp, base) != CTF_K_INTEGER ||587ctf_type_encoding(dtt.dtt_ctfp, base, &cte) == CTF_ERR ||588IS_VOID(cte)) {589xyerror(D_DECL_BFTYPE, "invalid type for "590"bit-field: %s\n", idname);591}592593if (dnp->dn_value > cte.cte_bits) {594xyerror(D_DECL_BFSIZE, "bit-field too big "595"for type: %s\n", idname);596}597598cte.cte_offset = 0;599cte.cte_bits = (uint_t)dnp->dn_value;600601dtt.dtt_type = ctf_add_integer(dsp->ds_ctfp,602CTF_ADD_NONROOT, ctf_type_name(dtt.dtt_ctfp,603dtt.dtt_type, n, sizeof (n)), &cte);604605if (dtt.dtt_type == CTF_ERR ||606ctf_update(dsp->ds_ctfp) == CTF_ERR) {607xyerror(D_UNKNOWN, "failed to create type for "608"member '%s': %s\n", idname,609ctf_errmsg(ctf_errno(dsp->ds_ctfp)));610}611612dtt.dtt_ctfp = dsp->ds_ctfp;613dt_node_free(dnp);614}615616/*617* If the member type is not defined in the same CTF container as the618* one associated with the current scope (i.e. the container for the619* struct or union itself) or its parent, copy the member type into620* this container and reset dtt to refer to the copied type.621*/622if (dtt.dtt_ctfp != dsp->ds_ctfp &&623dtt.dtt_ctfp != ctf_parent_file(dsp->ds_ctfp)) {624625dtt.dtt_type = ctf_add_type(dsp->ds_ctfp,626dtt.dtt_ctfp, dtt.dtt_type);627dtt.dtt_ctfp = dsp->ds_ctfp;628629if (dtt.dtt_type == CTF_ERR ||630ctf_update(dtt.dtt_ctfp) == CTF_ERR) {631xyerror(D_UNKNOWN, "failed to copy type of '%s': %s\n",632idname, ctf_errmsg(ctf_errno(dtt.dtt_ctfp)));633}634}635636if (ctf_add_member(dsp->ds_ctfp, dsp->ds_type,637ident, dtt.dtt_type) == CTF_ERR) {638xyerror(D_UNKNOWN, "failed to define member '%s': %s\n",639idname, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));640}641642done:643free(ident);644yypcb->pcb_dstack.ds_ident = NULL;645dt_decl_reset();646}647648/*ARGSUSED*/649static int650dt_decl_hasmembers(const char *name, int value, void *private)651{652return (1); /* abort search and return true if a member exists */653}654655dt_decl_t *656dt_decl_enum(char *name)657{658dt_decl_t *ddp = dt_decl_spec(CTF_K_ENUM, name);659char n[DT_TYPE_NAMELEN];660ctf_file_t *ctfp;661ctf_id_t type;662uint_t flag;663664if (yypcb->pcb_idepth != 0)665ctfp = yypcb->pcb_hdl->dt_cdefs->dm_ctfp;666else667ctfp = yypcb->pcb_hdl->dt_ddefs->dm_ctfp;668669if (yypcb->pcb_dstack.ds_next != NULL)670flag = CTF_ADD_NONROOT;671else672flag = CTF_ADD_ROOT;673674(void) snprintf(n, sizeof (n), "enum %s", name ? name : "(anon)");675676if (name != NULL && (type = ctf_lookup_by_name(ctfp, n)) != CTF_ERR) {677if (ctf_enum_iter(ctfp, type, dt_decl_hasmembers, NULL))678xyerror(D_DECL_TYPERED, "type redeclared: %s\n", n);679} else if ((type = ctf_add_enum(ctfp, flag, name)) == CTF_ERR) {680xyerror(D_UNKNOWN, "failed to define %s: %s\n",681n, ctf_errmsg(ctf_errno(ctfp)));682}683684ddp->dd_ctfp = ctfp;685ddp->dd_type = type;686687dt_scope_push(ctfp, type);688return (ddp);689}690691void692dt_decl_enumerator(char *s, dt_node_t *dnp)693{694dt_scope_t *dsp = yypcb->pcb_dstack.ds_next;695dtrace_hdl_t *dtp = yypcb->pcb_hdl;696697dt_idnode_t *inp;698dt_ident_t *idp;699char *name;700int value;701702name = alloca(strlen(s) + 1);703(void) strcpy(name, s);704free(s);705706if (dsp == NULL)707longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);708709assert(dsp->ds_decl->dd_kind == CTF_K_ENUM);710value = dsp->ds_enumval + 1; /* default is previous value plus one */711712if (strchr(name, '`') != NULL) {713xyerror(D_DECL_SCOPE, "D scoping operator may not be used in "714"an enumerator name (%s)\n", name);715}716717/*718* If the enumerator is being assigned a value, cook and check the node719* and then free it after we get the value. We also permit references720* to identifiers which are previously defined enumerators in the type.721*/722if (dnp != NULL) {723if (dnp->dn_kind != DT_NODE_IDENT || ctf_enum_value(724dsp->ds_ctfp, dsp->ds_type, dnp->dn_string, &value) != 0) {725dnp = dt_node_cook(dnp, DT_IDFLG_REF);726727if (dnp->dn_kind != DT_NODE_INT) {728xyerror(D_DECL_ENCONST, "enumerator '%s' must "729"be assigned to an integral constant "730"expression\n", name);731}732733if ((intmax_t)dnp->dn_value > INT_MAX ||734(intmax_t)dnp->dn_value < INT_MIN) {735xyerror(D_DECL_ENOFLOW, "enumerator '%s' value "736"overflows INT_MAX (%d)\n", name, INT_MAX);737}738739value = (int)dnp->dn_value;740}741dt_node_free(dnp);742}743744if (ctf_add_enumerator(dsp->ds_ctfp, dsp->ds_type,745name, value) == CTF_ERR || ctf_update(dsp->ds_ctfp) == CTF_ERR) {746xyerror(D_UNKNOWN, "failed to define enumerator '%s': %s\n",747name, ctf_errmsg(ctf_errno(dsp->ds_ctfp)));748}749750dsp->ds_enumval = value; /* save most recent value */751752/*753* If the enumerator name matches an identifier in the global scope,754* flag this as an error. We only do this for "D" enumerators to755* prevent "C" header file enumerators from conflicting with the ever-756* growing list of D built-in global variables and inlines. If a "C"757* enumerator conflicts with a global identifier, we add the enumerator758* but do not insert a corresponding inline (i.e. the D variable wins).759*/760if (dt_idstack_lookup(&yypcb->pcb_globals, name) != NULL) {761if (dsp->ds_ctfp == dtp->dt_ddefs->dm_ctfp) {762xyerror(D_DECL_IDRED,763"identifier redeclared: %s\n", name);764} else765return;766}767768dt_dprintf("add global enumerator %s = %d\n", name, value);769770idp = dt_idhash_insert(dtp->dt_globals, name, DT_IDENT_ENUM,771DT_IDFLG_INLINE | DT_IDFLG_REF, 0, _dtrace_defattr, 0,772&dt_idops_inline, NULL, dtp->dt_gen);773774if (idp == NULL)775longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);776777yyintprefix = 0;778yyintsuffix[0] = '\0';779yyintdecimal = 0;780781dnp = dt_node_int(value);782dt_node_type_assign(dnp, dsp->ds_ctfp, dsp->ds_type, B_FALSE);783784if ((inp = malloc(sizeof (dt_idnode_t))) == NULL)785longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);786787/*788* Remove the INT node from the node allocation list and store it in789* din_list and din_root so it persists with and is freed by the ident.790*/791assert(yypcb->pcb_list == dnp);792yypcb->pcb_list = dnp->dn_link;793dnp->dn_link = NULL;794795bzero(inp, sizeof (dt_idnode_t));796inp->din_list = dnp;797inp->din_root = dnp;798799idp->di_iarg = inp;800idp->di_ctfp = dsp->ds_ctfp;801idp->di_type = dsp->ds_type;802}803804/*805* Look up the type corresponding to the specified decl stack. The scoping of806* the underlying type names is handled by dt_type_lookup(). We build up the807* name from the specified string and prefixes and then lookup the type. If808* we fail, an errmsg is saved and the caller must abort with EDT_COMPILER.809*/810int811dt_decl_type(dt_decl_t *ddp, dtrace_typeinfo_t *tip)812{813dtrace_hdl_t *dtp = yypcb->pcb_hdl;814815dt_module_t *dmp;816ctf_arinfo_t r;817ctf_id_t type;818819char n[DT_TYPE_NAMELEN];820uint_t flag;821char *name;822int rv;823824tip->dtt_flags = 0;825826/*827* Based on our current #include depth and decl stack depth, determine828* which dynamic CTF module and scope to use when adding any new types.829*/830dmp = yypcb->pcb_idepth ? dtp->dt_cdefs : dtp->dt_ddefs;831flag = yypcb->pcb_dstack.ds_next ? CTF_ADD_NONROOT : CTF_ADD_ROOT;832833if (ddp->dd_attr & DT_DA_USER)834tip->dtt_flags = DTT_FL_USER;835836/*837* If we have already cached a CTF type for this decl, then we just838* return the type information for the cached type.839*/840if (ddp->dd_ctfp != NULL &&841(dmp = dt_module_lookup_by_ctf(dtp, ddp->dd_ctfp)) != NULL) {842tip->dtt_object = dmp->dm_name;843tip->dtt_ctfp = ddp->dd_ctfp;844tip->dtt_type = ddp->dd_type;845return (0);846}847848/*849* Currently CTF treats all function pointers identically. We cache a850* representative ID of kind CTF_K_FUNCTION and just return that type.851* If we want to support full function declarations, dd_next refers to852* the declaration of the function return type, and the parameter list853* should be parsed and hung off a new pointer inside of this decl.854*/855if (ddp->dd_kind == CTF_K_FUNCTION) {856tip->dtt_object = dtp->dt_ddefs->dm_name;857tip->dtt_ctfp = DT_FUNC_CTFP(dtp);858tip->dtt_type = DT_FUNC_TYPE(dtp);859return (0);860}861862/*863* If the decl is a pointer, resolve the rest of the stack by calling864* dt_decl_type() recursively and then compute a pointer to the result.865* Similar to the code above, we return a cached id for function ptrs.866*/867if (ddp->dd_kind == CTF_K_POINTER) {868if (ddp->dd_next->dd_kind == CTF_K_FUNCTION) {869tip->dtt_object = dtp->dt_ddefs->dm_name;870tip->dtt_ctfp = DT_FPTR_CTFP(dtp);871tip->dtt_type = DT_FPTR_TYPE(dtp);872return (0);873}874875if ((rv = dt_decl_type(ddp->dd_next, tip)) == 0 &&876(rv = dt_type_pointer(tip)) != 0) {877xywarn(D_UNKNOWN, "cannot find type: %s*: %s\n",878dt_type_name(tip->dtt_ctfp, tip->dtt_type,879n, sizeof (n)), ctf_errmsg(dtp->dt_ctferr));880}881882return (rv);883}884885/*886* If the decl is an array, we must find the base type and then call887* dt_decl_type() recursively and then build an array of the result.888* The C and D multi-dimensional array syntax requires that consecutive889* array declarations be processed from right-to-left (i.e. top-down890* from the perspective of the declaration stack). For example, an891* array declaration such as int x[3][5] is stored on the stack as:892*893* (bottom) NULL <- ( INT "int" ) <- ( ARR [3] ) <- ( ARR [5] ) (top)894*895* but means that x is declared to be an array of 3 objects each of896* which is an array of 5 integers, or in CTF representation:897*898* type T1:( content=int, nelems=5 ) type T2:( content=T1, nelems=3 )899*900* For more details, refer to K&R[5.7] and ISO C 6.5.2.1. Rather than901* overcomplicate the implementation of dt_decl_type(), we push array902* declarations down into the stack in dt_decl_array(), above, so that903* by the time dt_decl_type() is called, the decl stack looks like:904*905* (bottom) NULL <- ( INT "int" ) <- ( ARR [5] ) <- ( ARR [3] ) (top)906*907* which permits a straightforward recursive descent of the decl stack908* to build the corresponding CTF type tree in the appropriate order.909*/910if (ddp->dd_kind == CTF_K_ARRAY) {911/*912* If the array decl has a parameter list associated with it,913* this is an associative array declaration: return <DYN>.914*/915if (ddp->dd_node != NULL &&916ddp->dd_node->dn_kind == DT_NODE_TYPE) {917tip->dtt_object = dtp->dt_ddefs->dm_name;918tip->dtt_ctfp = DT_DYN_CTFP(dtp);919tip->dtt_type = DT_DYN_TYPE(dtp);920return (0);921}922923if ((rv = dt_decl_type(ddp->dd_next, tip)) != 0)924return (rv);925926/*927* If the array base type is not defined in the target928* container or its parent, copy the type to the target929* container and reset dtt_ctfp and dtt_type to the copy.930*/931if (tip->dtt_ctfp != dmp->dm_ctfp &&932tip->dtt_ctfp != ctf_parent_file(dmp->dm_ctfp)) {933934tip->dtt_type = ctf_add_type(dmp->dm_ctfp,935tip->dtt_ctfp, tip->dtt_type);936tip->dtt_ctfp = dmp->dm_ctfp;937938if (tip->dtt_type == CTF_ERR ||939ctf_update(tip->dtt_ctfp) == CTF_ERR) {940xywarn(D_UNKNOWN, "failed to copy type: %s\n",941ctf_errmsg(ctf_errno(tip->dtt_ctfp)));942return (-1);943}944}945946/*947* The array index type is irrelevant in C and D: just set it948* to "long" for all array types that we create on-the-fly.949*/950r.ctr_contents = tip->dtt_type;951r.ctr_index = ctf_lookup_by_name(tip->dtt_ctfp, "long");952r.ctr_nelems = ddp->dd_node ?953(uint_t)ddp->dd_node->dn_value : 0;954955tip->dtt_object = dmp->dm_name;956tip->dtt_ctfp = dmp->dm_ctfp;957tip->dtt_type = ctf_add_array(dmp->dm_ctfp, CTF_ADD_ROOT, &r);958959if (tip->dtt_type == CTF_ERR ||960ctf_update(tip->dtt_ctfp) == CTF_ERR) {961xywarn(D_UNKNOWN, "failed to create array type: %s\n",962ctf_errmsg(ctf_errno(tip->dtt_ctfp)));963return (-1);964}965966return (0);967}968969/*970* Allocate space for the type name and enough space for the maximum971* additional text ("unsigned long long \0" requires 20 more bytes).972*/973name = alloca(ddp->dd_name ? strlen(ddp->dd_name) + 20 : 20);974name[0] = '\0';975976switch (ddp->dd_kind) {977case CTF_K_INTEGER:978case CTF_K_FLOAT:979if (ddp->dd_attr & DT_DA_SIGNED)980(void) strcat(name, "signed ");981if (ddp->dd_attr & DT_DA_UNSIGNED)982(void) strcat(name, "unsigned ");983if (ddp->dd_attr & DT_DA_SHORT)984(void) strcat(name, "short ");985if (ddp->dd_attr & DT_DA_LONG)986(void) strcat(name, "long ");987if (ddp->dd_attr & DT_DA_LONGLONG)988(void) strcat(name, "long long ");989if (ddp->dd_attr == 0 && ddp->dd_name == NULL)990(void) strcat(name, "int");991break;992case CTF_K_STRUCT:993(void) strcpy(name, "struct ");994break;995case CTF_K_UNION:996(void) strcpy(name, "union ");997break;998case CTF_K_ENUM:999(void) strcpy(name, "enum ");1000break;1001case CTF_K_TYPEDEF:1002break;1003default:1004xywarn(D_UNKNOWN, "internal error -- "1005"bad decl kind %u\n", ddp->dd_kind);1006return (-1);1007}10081009/*1010* Add dd_name unless a short, long, or long long is explicitly1011* suffixed by int. We use the C/CTF canonical names for integers.1012*/1013if (ddp->dd_name != NULL && (ddp->dd_kind != CTF_K_INTEGER ||1014(ddp->dd_attr & (DT_DA_SHORT | DT_DA_LONG | DT_DA_LONGLONG)) == 0))1015(void) strcat(name, ddp->dd_name);10161017/*1018* Lookup the type. If we find it, we're done. Otherwise create a1019* forward tag for the type if it is a struct, union, or enum. If1020* we can't find it and we can't create a tag, return failure.1021*/1022if ((rv = dt_type_lookup(name, tip)) == 0)1023return (rv);10241025switch (ddp->dd_kind) {1026case CTF_K_STRUCT:1027case CTF_K_UNION:1028case CTF_K_ENUM:1029type = ctf_add_forward(dmp->dm_ctfp, flag,1030ddp->dd_name, ddp->dd_kind);1031break;1032default:1033xywarn(D_UNKNOWN, "failed to resolve type %s: %s\n", name,1034dtrace_errmsg(dtp, dtrace_errno(dtp)));1035return (rv);1036}10371038if (type == CTF_ERR || ctf_update(dmp->dm_ctfp) == CTF_ERR) {1039xywarn(D_UNKNOWN, "failed to add forward tag for %s: %s\n",1040name, ctf_errmsg(ctf_errno(dmp->dm_ctfp)));1041return (-1);1042}10431044ddp->dd_ctfp = dmp->dm_ctfp;1045ddp->dd_type = type;10461047tip->dtt_object = dmp->dm_name;1048tip->dtt_ctfp = dmp->dm_ctfp;1049tip->dtt_type = type;10501051return (0);1052}10531054void1055dt_scope_create(dt_scope_t *dsp)1056{1057dsp->ds_decl = NULL;1058dsp->ds_next = NULL;1059dsp->ds_ident = NULL;1060dsp->ds_ctfp = NULL;1061dsp->ds_type = CTF_ERR;1062dsp->ds_class = DT_DC_DEFAULT;1063dsp->ds_enumval = -1;1064}10651066void1067dt_scope_destroy(dt_scope_t *dsp)1068{1069dt_scope_t *nsp;10701071for (; dsp != NULL; dsp = nsp) {1072dt_decl_free(dsp->ds_decl);1073free(dsp->ds_ident);1074nsp = dsp->ds_next;1075if (dsp != &yypcb->pcb_dstack)1076free(dsp);1077}1078}10791080void1081dt_scope_push(ctf_file_t *ctfp, ctf_id_t type)1082{1083dt_scope_t *rsp = &yypcb->pcb_dstack;1084dt_scope_t *dsp = malloc(sizeof (dt_scope_t));10851086if (dsp == NULL)1087longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);10881089dsp->ds_decl = rsp->ds_decl;1090dsp->ds_next = rsp->ds_next;1091dsp->ds_ident = rsp->ds_ident;1092dsp->ds_ctfp = ctfp;1093dsp->ds_type = type;1094dsp->ds_class = rsp->ds_class;1095dsp->ds_enumval = rsp->ds_enumval;10961097dt_scope_create(rsp);1098rsp->ds_next = dsp;1099}11001101dt_decl_t *1102dt_scope_pop(void)1103{1104dt_scope_t *rsp = &yypcb->pcb_dstack;1105dt_scope_t *dsp = rsp->ds_next;11061107if (dsp == NULL)1108longjmp(yypcb->pcb_jmpbuf, EDT_NOSCOPE);11091110if (dsp->ds_ctfp != NULL && ctf_update(dsp->ds_ctfp) == CTF_ERR) {1111xyerror(D_UNKNOWN, "failed to update type definitions: %s\n",1112ctf_errmsg(ctf_errno(dsp->ds_ctfp)));1113}11141115dt_decl_free(rsp->ds_decl);1116free(rsp->ds_ident);11171118rsp->ds_decl = dsp->ds_decl;1119rsp->ds_next = dsp->ds_next;1120rsp->ds_ident = dsp->ds_ident;1121rsp->ds_ctfp = dsp->ds_ctfp;1122rsp->ds_type = dsp->ds_type;1123rsp->ds_class = dsp->ds_class;1124rsp->ds_enumval = dsp->ds_enumval;11251126free(dsp);1127return (rsp->ds_decl);1128}112911301131