File: vcc_types.c

package info (click to toggle)
varnish 7.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,256 kB
  • sloc: ansic: 104,222; python: 2,679; makefile: 1,303; sh: 1,077; awk: 114; perl: 105; ruby: 41
file content (330 lines) | stat: -rw-r--r-- 7,575 bytes parent folder | download | duplicates (2)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*-
 * Copyright (c) 2006 Verdens Gang AS
 * Copyright (c) 2006-2011 Varnish Software AS
 * All rights reserved.
 *
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include "config.h"

#include <string.h>

#include "vcc_compile.h"

/*
 * A type attribute is information already existing, requiring no processing
 * or resource usage.
 *
 * A type method is a call and may do significant processing, change things,
 * eat workspace etc.
 *
 * XXX: type methods might move in a more comprehensive direction.
 */
struct vcc_method {
	unsigned		magic;
#define VCC_METHOD_MAGIC	0x594108cd
	vcc_type_t		type;
	const char		*name;
	const char		*impl;
	int			func;
};

const struct type ACL[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"ACL",
	.global_pfx =		"vrt_acl",
	.tostring =		"((\v1)->name)",
}};

static const struct vcc_method backend_methods[] = {
	{ VCC_METHOD_MAGIC, BACKEND, "resolve",
	    "VRT_DirectorResolve(ctx, \v1)", 1 },
	{ VCC_METHOD_MAGIC, NULL },
};

static struct symbol default_backend[1] = {{
	.magic =		SYMBOL_MAGIC,
	.name =			"default",
	.lorev =		VCL_LOW,
	.hirev =		VCL_HIGH,
	.kind =			SYM_BACKEND,
	.type =			DEFAULT,
	.rname =		"*(VCL_conf.default_director)",
}};

const struct type BACKEND[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"BACKEND",
	.methods =		backend_methods,
	.global_pfx =		"vgc_backend",
	.tostring =		"VRT_BACKEND_string(\v1)",
	.default_sym =		default_backend,
}};

const struct type BLOB[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"BLOB",
	.bodyform =		1,
	.tostring =		"VRT_BLOB_string(ctx, \v1)",
}};

const struct type BODY[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"BODY",
	.noindent =		1,
}};

const struct type BOOL[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"BOOL",
	.tostring =		"VRT_BOOL_string(\v1)",
}};

const struct type BYTES[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"BYTES",
	.tostring =		"VRT_INT_string(ctx, \v1)",
	.multype =		REAL,	// XXX: wrong
}};

const struct type DEFAULT[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"DEFAULT",
}};

const struct type DURATION[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"DURATION",
	.tostring =		"VRT_REAL_string(ctx, \v1)", // XXX 's' suff?
	.multype =		REAL,
}};

const struct type ENUM[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"ENUM",
	.tostring =		"",
}};

const struct type HEADER[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"HEADER",
	.tostring =		"VRT_GetHdr(ctx, \v1)",
}};

const struct type HTTP[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"HTTP",
}};

const struct type INSTANCE[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"INSTANCE",
	.global_pfx =		"vo",
}};

const struct type INT[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"INT",
	.multype =		INT,
	.tostring =		"VRT_INT_string(ctx, \v1)",
}};

const struct type IP[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"IP",
	.tostring =		"VRT_IP_string(ctx, \v1)",
}};

static struct symbol default_probe[1] = {{
	.magic =		SYMBOL_MAGIC,
	.name =			"default",
	.lorev =		VCL_LOW,
	.hirev =		VCL_HIGH,
	.kind =			SYM_PROBE,
	.type =			DEFAULT,
	.rname =		"vgc_probe_default",
}};

const struct type PROBE[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"PROBE",
	.global_pfx =		"vgc_probe",
	.default_sym =		default_probe,
}};

const struct type REAL[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"REAL",
	.tostring =		"VRT_REAL_string(ctx, \v1)",
	.multype =		REAL,
}};

const struct type REGEX[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"REGEX",
}};

static const struct vcc_method stevedore_methods[] = {
#define VRTSTVVAR(nm, vtype, ctype, dval) \
	{ VCC_METHOD_MAGIC, vtype, #nm, "VRT_stevedore_" #nm "(\v1)", 0},
#include "tbl/vrt_stv_var.h"
	{ VCC_METHOD_MAGIC, NULL },
};

const struct type STEVEDORE[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"STEVEDORE",
	.methods =		stevedore_methods,
	.tostring =		"VRT_STEVEDORE_string(\v1)",
}};

const struct type STRING[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"STRING",
	.stringform =		1,
}};

const struct type STRANDS[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"STRANDS",
	.stringform =		1,
	.tostring =		"VRT_STRANDS_string(ctx,\v+\n\v1\v-\n)",
}};

static const struct vcc_method strings_methods[] = {
	{ VCC_METHOD_MAGIC, STRING, "upper",
	    "VRT_UpperLowerStrands(ctx, \vT, 1)", 1 },
	{ VCC_METHOD_MAGIC, STRING, "lower",
	    "VRT_UpperLowerStrands(ctx, \vT, 0)", 1 },
	{ VCC_METHOD_MAGIC, NULL },
};

const struct type STRINGS[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"STRINGS",
	.methods =		strings_methods,
	.bodyform =		1,
	.tostring =		"",
}};

const struct type SUB[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"SUB",
	.global_pfx =		"VGC_function",
}};

const struct type TIME[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"TIME",
	.tostring =		"VRT_TIME_string(ctx, \v1)",
}};

const struct type VCL[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"VCL",
}};

const struct type VOID[1] = {{
	.magic =		TYPE_MAGIC,
	.name =			"VOID",
}};

vcc_type_t
VCC_Type(const char *p)
{

#define VCC_TYPE(UC, lc)	if (!strcmp(p, #UC)) return (UC);
#include "vcc_types.h"
	return (NULL);
}

static void
vcc_type_init(struct vcc *tl, vcc_type_t type)
{
	const struct vcc_method *vm;
	struct symbol *sym;
	struct vsb *buf;

	/* NB: Don't bother even creating a type symbol if there are no
	 * methods attached to it.
	 */
	if (type->methods == NULL)
		return;

	buf = VSB_new_auto();
	AN(buf);
	AN(VCC_MkSym(tl, type->name, SYM_TYPE, SYM_NONE, VCL_LOW, VCL_HIGH));

	for (vm = type->methods; vm->type != NULL; vm++) {
		VSB_printf(buf, "%s.%s", type->name, vm->name);
		AZ(VSB_finish(buf));
		sym = VCC_MkSym(tl, VSB_data(buf), SYM_TYPE, SYM_METHOD,
		    VCL_LOW, VCL_HIGH);
		VSB_clear(buf);
		if (tl->err)
			break;
		AN(sym);
		sym->type = vm->type;
		sym->eval = vcc_Eval_TypeMethod;
		sym->eval_priv = vm;
	}

	VSB_destroy(&buf);
}

const char *
VCC_Type_EvalMethod(struct vcc *tl, const struct symbol *sym)
{
	const struct vcc_method *vm;

	AN(sym);
	AN(sym->kind == SYM_METHOD);
	CAST_OBJ_NOTNULL(vm, sym->eval_priv, VCC_METHOD_MAGIC);

	vcc_NextToken(tl);
	if (vm->func) {
		Expect(tl, '(');
		if (tl->err)
			return (NULL);
		vcc_NextToken(tl);
		Expect(tl, ')');
		if (tl->err)
			return (NULL);
		vcc_NextToken(tl);
	}

	return (vm->impl);
}

void
vcc_Type_Init(struct vcc *tl)
{

#define VCC_TYPE(UC, lc)	vcc_type_init(tl, UC);
#include "vcc_types.h"
}