File: union.c

package info (click to toggle)
nickle 2.77-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,612 kB
  • ctags: 3,746
  • sloc: ansic: 26,986; yacc: 1,873; sh: 954; lex: 884; makefile: 225
file content (169 lines) | stat: -rw-r--r-- 3,110 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* $Header$ */

/*
 * Copyright © 1988-2004 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

#include	"nickle.h"

Value
UnionRef (Value uv, Atom name)
{
    ENTER ();
    Union	    *u = &uv->unions;
    StructType	    *st = u->type;
    int		    i;

    for (i = 0; i < st->nelements; i++)
	if (StructTypeAtoms(st)[i] == name)
	{
	    u->tag = name;
	    RETURN (NewRef (u->value, 0));
	}
    RETURN (0);
}

Value
UnionValue (Value uv, Atom name)
{
    ENTER ();
    Union	    *u = &uv->unions;

    if (u->tag != name)
	RETURN (0);
    RETURN (BoxValue (u->value, 0));
}

static Bool
UnionPrint (Value f, Value av, char format, int base, int width, int prec, int fill)
{
    Union	    *u = &av->unions;

    if (format == 'v')
	FileOutput (f, '{');
    if (u->tag)
    {
	Type	*t = StructMemType (u->type, u->tag);
        FilePuts (f, AtomName (u->tag));
	if (t != (Type*) 1)
	{
	    FilePuts (f, " = ");
	    if (!Print (f, BoxValue (u->value, 0), format, base, width, prec, fill))
		return False;
	}
    }
    else
	FilePuts (f, "<unset>");
    if (format == 'v')
	FileOutput (f, '}');
    return True;
}

static void
UnionMark (void *object)
{
    Union  *u = object;

    MemReference (u->type);
    MemReference (u->value);
}

static Value
UnionEqual (Value av, Value bv, int expandOk)
{
    Union	    *a = &av->unions, *b = &bv->unions;
    
    if (!ValueIsUnion(av))
	return Equal (av, BoxValue (b->value, 0));
    if (!ValueIsUnion(bv))
	return Equal (BoxValue (a->value, 0), bv);
    if (a->tag != b->tag)
	return FalseVal;
    return Equal (BoxValue (a->value, 0), BoxValue (b->value, 0));
}

ValueRep    UnionRep = { 
    { UnionMark, 0, "UnionRep" },	    /* base */
    rep_union,		    /* tag */
    {			    /* binary */
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	UnionEqual,
	0,
	0,
    },
    {			    /* unary */
	0,
	0,
	0,
    },
    0,
    0,
    UnionPrint,
    0,
};

Value
NewUnion (StructType *type, Bool constant)
{
    ENTER ();
    Value	    ret;

    ret = ALLOCATE (&UnionRep.data, sizeof (Union));
    ret->unions.type = type;
    ret->unions.tag = 0;
    ret->unions.value = 0;
    ret->unions.value = NewBox (constant, False, 1, 0);
    RETURN (ret);
}

Type *
BuildUnionType (int nelements, ...)
{
    ENTER ();
    StructType	*st;
    int		i;
    char	*name;
    Type	*type;
    va_list	ap;

    st = NewStructType (nelements);
    va_start (ap, nelements);
    for (i = 0; i < nelements; i++)
    {
	type = va_arg (ap, Type *);
	name = va_arg (ap, char *);
	AddBoxType (&st->types, type);
	StructTypeAtoms (st)[i] = AtomId (name);
    }
    va_end (ap);
    RETURN (NewTypeUnion (st, False));
}

Type *
BuildEnumType (int nelements, ...)
{
    ENTER ();
    StructType	*st;
    int		i;
    char	*name;
    va_list	ap;

    st = NewStructType (nelements);
    va_start (ap, nelements);
    for (i = 0; i < nelements; i++)
    {
	name = va_arg (ap, char *);
	AddBoxType (&st->types, typePrim[rep_void]);
	StructTypeAtoms (st)[i] = AtomId (name);
    }
    va_end (ap);
    RETURN (NewTypeUnion (st, True));
}