Code Coverage: vinyl-cache/lib/libvcc/vcc_compile.h
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 */
31
32
#include <stdio.h>
33
#include <stdint.h>
34
35
#include "miniobj.h"
36
#include "vdef.h"
37
#include "vas.h"
38
#include "vrt.h"
39
#include "vcl.h"
40
#include "vqueue.h"
41
#include "vsb.h"
42
43
#include "vcc_token_defs.h"
44
45
/*---------------------------------------------------------------------
46
 * VCL version stuff
47
 */
48
49 152249
#define VCL_LOW         40              // Lowest VCC supports
50 142684
#define VCL_HIGH        41              // Highest VCC supports
51
52
// Specific VCL versions
53
#define VCL_40          40
54 3745463
#define VCL_41          41
55
56
/*---------------------------------------------------------------------*/
57
58
struct acl;
59
struct acl_e;
60
struct expr;
61
struct method;
62
struct proc;
63
struct sockaddr_storage;
64
struct symbol;
65
struct symtab;
66
struct token;
67
struct vcc;
68
struct vjsn_val;
69
struct vmod_obj;
70
struct vsb;
71
72
unsigned vcl_fixed_token(const char *p, const char **q);
73
extern const char * const vcl_tnames[256];
74
void vcl_output_lang_h(struct vsb *sb);
75
76 190267
#define PF(t)   (int)((t)->e - (t)->b), (t)->b
77
78 1509629
#define INDENT          2
79
80
struct source {
81
        unsigned                magic;
82
#define SOURCE_MAGIC            0xf756fe82
83
        VTAILQ_ENTRY(source)    list;
84
        const char              *kind;
85
        char                    *name;
86
        const char              *b;
87
        const char              *e;
88
        unsigned                idx;
89
        const struct source     *parent;
90
        const struct token      *parent_tok;
91
        VTAILQ_HEAD(, token)    src_tokens;
92
};
93
94
struct token {
95
        unsigned                tok;
96
        const char              *b;
97
        const char              *e;
98
        const struct source     *src;
99
        VTAILQ_ENTRY(token)     list;
100
        VTAILQ_ENTRY(token)     src_list;
101
        unsigned                cnt;
102
        char                    *dec;
103
        double                  num;
104
};
105
106
/*---------------------------------------------------------------------*/
107
108
typedef const struct type       *vcc_type_t;
109
110
struct type {
111
        unsigned                magic;
112
#define TYPE_MAGIC              0xfae932d9
113
114
        const char              *name;
115
        const struct vcc_method *methods;
116
117
        const char              *global_pfx;
118
        const char              *tostring;
119
        vcc_type_t              multype;
120
        struct symbol           *default_sym;
121
122
        int                     stringform;
123
        int                     bodyform;
124
        int                     noindent;
125
};
126
127
#define VCC_TYPE(UC, lc)        extern const struct type UC[1];
128
#include "vcc_types.h"
129
130
extern const struct type DEFAULT[1]; /* type for pseudo-symbol "default" */
131
132
/*---------------------------------------------------------------------*/
133
134
typedef const struct kind       *vcc_kind_t;
135
136
struct kind {
137
        unsigned                magic;
138
#define KIND_MAGIC              0xfad72443
139
140
        const char              *name;
141
};
142
143
#define VCC_KIND(U,l)   extern const struct kind SYM_##U[1];
144
#include "tbl/symbol_kind.h"
145
146
/*---------------------------------------------------------------------*/
147
148
typedef const struct vcc_namespace *const vcc_ns_t;
149
150
#define VCC_NAMESPACE(U, l)     extern vcc_ns_t SYM_##U;
151
#include "vcc_namespace.h"
152
153
enum vcc_namespace_e {
154
#define VCC_NAMESPACE(U, l)     VCC_NAMESPACE_##U,
155
#include "vcc_namespace.h"
156
        VCC_NAMESPACE__MAX
157
};
158
159
/*---------------------------------------------------------------------*/
160
161
typedef void sym_expr_t(struct vcc *tl, struct expr **,
162
    struct token *, struct symbol *sym, vcc_type_t);
163
typedef void sym_wildcard_t(struct vcc *, struct symbol *, struct symbol *);
164
165
typedef void sym_act_f(struct vcc *, struct token *, struct symbol *);
166
167
struct symbol {
168
        unsigned                        magic;
169
#define SYMBOL_MAGIC                    0x3368c9fb
170
        VTAILQ_ENTRY(symbol)            list;
171
        VTAILQ_ENTRY(symbol)            sideways;
172
173
        const char                      *name;
174
175
        int                             lorev;
176
        int                             hirev;
177
178
        const struct symtab             *symtab;
179
        const char                      *vmod_name;
180
181
        sym_wildcard_t                  *wildcard;
182
        vcc_kind_t                      kind;
183
184
        sym_act_f                       *action;
185
        unsigned                        action_mask;
186
187
        const struct token              *def_b, *def_e, *ref_b;
188
189
        vcc_type_t                      type;
190
191
        sym_expr_t                      *eval;
192
        const void                      *eval_priv;
193
194
        /* xref.c */
195
        struct proc                     *proc;
196
        unsigned                        noref, nref, ndef;
197
198
        const char                      *extra;
199
200
        /* vcc_vmod.c */
201
        const struct vmod_import        *import;
202
203
        /* SYM_VAR */
204
        const char                      *rname;
205
        unsigned                        r_methods;
206
        const char                      *lname;
207
        unsigned                        w_methods;
208
        const char                      *uname;
209
        unsigned                        u_methods;
210
};
211
212
VTAILQ_HEAD(tokenhead, token);
213
214
struct proc {
215
        unsigned                magic;
216
#define PROC_MAGIC              0xd1d98499
217
        const struct method     *method;
218
        VTAILQ_HEAD(,proccall)  calls;
219
        VTAILQ_HEAD(,procuse)   uses;
220
        VTAILQ_ENTRY(proc)      list;
221
        struct token            *name;
222
        unsigned                ret_bitmap;
223
        unsigned                called;
224
        unsigned                active;
225
        unsigned                okmask;
226
        unsigned                calledfrom;
227
        struct token            *return_tok[VCL_RET_MAX];
228
        struct vsb              *cname;
229
        struct vsb              *prologue;
230
        struct vsb              *body;
231
        struct symbol           *sym;
232
};
233
234
struct inifin {
235
        unsigned                magic;
236
#define INIFIN_MAGIC            0x583c274c
237
        unsigned                n;
238
        unsigned                ignore_errors;
239
        struct vsb              *ini;
240
        struct vsb              *fin;
241
        struct vsb              *final;
242
        struct vsb              *event;
243
        VTAILQ_ENTRY(inifin)    list;
244
};
245
246
VTAILQ_HEAD(inifinhead, inifin);
247
248
struct vcc {
249
        unsigned                magic;
250
#define VCC_MAGIC               0x24ad719d
251
        int                     syntax;
252
253
        char                    *builtin_vcl;
254
        struct vfil_path        *vcl_path;
255
        struct vfil_path        *vmod_path;
256
#define VCC_FEATURE_BIT(U, l, d) unsigned l;
257
#include <tbl/vcc_feature_bits.h>
258
259
        struct symtab           *syms[VCC_NAMESPACE__MAX];
260
261
        struct inifinhead       inifin;
262
        unsigned                ninifin;
263
264
        /* Instance section */
265
        struct tokenhead        tokens;
266
        VTAILQ_HEAD(, source)   sources;
267
        unsigned                nsources;
268
        struct token            *t;
269
        int                     indent;
270
        int                     hindent;
271
        unsigned                cnt;
272
273
        struct vsb              *symtab;        /* VCC info to MGT */
274
        struct vsb              *fc;            /* C-code */
275
        struct vsb              *fh;            /* H-code (before C-code) */
276
        struct vsb              *fb;            /* Body of current sub
277
                                                 * NULL otherwise
278
                                                 */
279
        struct vsb              *sb;
280
        int                     err;
281
        unsigned                nsub;
282
        unsigned                subref; // SUB arguments present
283
        struct proc             *curproc;
284
        VTAILQ_HEAD(, proc)     procs;
285
286
        struct acl              *acl;
287
288
        int                     nprobe;
289
290
        int                     ndirector;
291
        struct symbol           *first_director;
292
        const char              *default_director;
293
        const char              *default_probe;
294
295
        VTAILQ_HEAD(, symbol)   sym_objects;
296
        VTAILQ_HEAD(, symbol)   sym_vmods;
297
        VTAILQ_HEAD(, vmod_obj) vmod_objects;
298
299
        unsigned                unique;
300
        unsigned                vmod_count;
301
};
302
303
extern struct vcc *vcc_builtin;
304
305
struct method {
306
        const char              *name;
307
        unsigned                ret_bitmap;
308
        unsigned                bitval;
309
};
310
311
/*--------------------------------------------------------------------*/
312
313
/* vcc_acl.c */
314
315
void vcc_ParseAcl(struct vcc *tl);
316
317
/* vcc_action.c */
318
void vcc_Action_Init(struct vcc *);
319
320
/* vcc_backend.c */
321
struct fld_spec;
322
323
void vcc_Backend_Init(struct vcc *tl);
324
void vcc_ParseProbe(struct vcc *tl);
325
void vcc_ParseBackend(struct vcc *tl);
326
struct fld_spec * vcc_FldSpec(struct vcc *tl, const char *first, ...);
327
void vcc_IsField(struct vcc *tl, const struct token **t, struct fld_spec *fs);
328
void vcc_FieldsOk(struct vcc *tl, const struct fld_spec *fs);
329
330
/* vcc_compile.c */
331
struct inifin *New_IniFin(struct vcc *);
332
struct proc *vcc_NewProc(struct vcc*, struct symbol*);
333
334
/*
335
 * H -> Header, before the C code
336
 * C -> C-code
337
 * B -> Body of function, ends up in C once function is completed
338
 * I -> Initializer function
339
 * F -> Finish function
340
 */
341
void Fh(const struct vcc *tl, int indent, const char *fmt, ...)
342
    v_printflike_(3, 4);
343
void Fc(const struct vcc *tl, int indent, const char *fmt, ...)
344
    v_printflike_(3, 4);
345
void Fb(const struct vcc *tl, int indent, const char *fmt, ...)
346
    v_printflike_(3, 4);
347
void EncToken(struct vsb *sb, const struct token *t);
348
void *TlAlloc(struct vcc *tl, unsigned len);
349
char *TlDup(struct vcc *tl, const char *s);
350
351
/* vcc_expr.c */
352
void vcc_Expr(struct vcc *tl, vcc_type_t typ);
353
sym_act_f vcc_Act_Call;
354
sym_act_f vcc_Act_Obj;
355
void vcc_Expr_Init(struct vcc *tl);
356
sym_expr_t vcc_Eval_Var;
357
sym_expr_t vcc_Eval_ProtectedHeader;
358
sym_expr_t vcc_Eval_Handle;
359
sym_expr_t vcc_Eval_Sub;
360
sym_expr_t vcc_Eval_SymFunc;
361
sym_expr_t vcc_Eval_TypeMethod;
362
void vcc_Eval_Func(struct vcc *, const struct vjsn_val *,
363
    const char *, struct symbol *);
364
void VCC_GlobalSymbol(struct symbol *, vcc_type_t fmt);
365
struct symbol *VCC_HandleSymbol(struct vcc *, vcc_type_t);
366
void VCC_SymName(struct vsb *, const struct symbol *);
367
368
/* vcc_obj.c */
369
void vcc_Var_Init(struct vcc *);
370
371
/* vcc_parse.c */
372
void vcc_Parse(struct vcc *);
373
void vcc_Parse_Init(struct vcc *);
374
sym_act_f vcc_Act_If;
375
376
/* vcc_source.c */
377
struct source * vcc_new_source(const char *src, const char *kind,
378
    const char *name);
379
struct source *vcc_file_source(struct vcc *tl, const char *fn);
380
void vcc_lex_source(struct vcc *tl, struct source *sp, int eoi);
381
382
/* vcc_storage.c */
383
void vcc_stevedore(struct vcc *vcc, const char *stv_name);
384
385
/* vcc_symb.c */
386
vcc_kind_t VCC_HandleKind(vcc_type_t fmt);
387
void VCC_PrintCName(struct vsb *vsb, const char *b, const char *e);
388
struct symbol *VCC_MkSym(struct vcc *tl, const char *b, vcc_ns_t, vcc_kind_t,
389
    int, int);
390
struct symbol *VCC_MkSymAlias(struct vcc *tl, const char *, const char *);
391
392
struct symxref { const char *name; };
393
extern const struct symxref XREF_NONE[1];
394
extern const struct symxref XREF_DEF[1];
395
extern const struct symxref XREF_REF[1];
396
397
struct symmode {
398
        const char      *name;
399
        unsigned        noerr;
400
        unsigned        partial;
401
};
402
extern const struct symmode SYMTAB_NOERR[1];
403
extern const struct symmode SYMTAB_CREATE[1];
404
extern const struct symmode SYMTAB_EXISTING[1];
405
extern const struct symmode SYMTAB_PARTIAL[1];
406
extern const struct symmode SYMTAB_PARTIAL_NOERR[1];
407
408
struct symbol *VCC_SymbolGet(struct vcc *, vcc_ns_t, vcc_kind_t,
409
    const struct symmode *, const struct symxref *);
410
411
struct symbol *VCC_TypeSymbol(struct vcc *, vcc_kind_t, vcc_type_t);
412
413
typedef void symwalk_f(struct vcc *tl, const struct symbol *s);
414
void VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, vcc_ns_t, vcc_kind_t);
415
416
/* vcc_token.c */
417
void vcc_Coord(const struct vcc *tl, struct vsb *vsb,
418
    const struct token *t);
419
void vcc_ErrToken(const struct vcc *tl, const struct token *t);
420
void vcc_ErrWhere(struct vcc *, const struct token *);
421
void vcc_ErrWhere2(struct vcc *, const struct token *, const struct token *);
422
void vcc_Warn(struct vcc *);
423
424
void vcc__Expect(struct vcc *tl, unsigned tok, unsigned line);
425
int vcc_IdIs(const struct token *t, const char *p);
426
void vcc_PrintTokens(const struct vcc *tl, const struct token *tb,
427
    const struct token *te);
428
void vcc_ExpectVid(struct vcc *tl, const char *what);
429
void vcc_Lexer(struct vcc *tl, struct source *sp);
430
void vcc_NextToken(struct vcc *tl);
431
struct token * vcc_PeekToken(const struct vcc *tl);
432
struct token * vcc_PeekTokenFrom(const struct vcc *tl, const struct token *t);
433
void vcc__ErrInternal(struct vcc *tl, const char *func,
434
    unsigned line);
435
436
/* vcc_types.c */
437
vcc_type_t VCC_Type(const char *p);
438
const char * VCC_Type_EvalMethod(struct vcc *, const struct symbol *);
439
void vcc_Type_Init(struct vcc *tl);
440
441
/* vcc_utils.c */
442
void vcc_regexp(struct vcc *tl, struct vsb *vgc_name);
443
void Resolve_Sockaddr(struct vcc *tl, const char *host, const char *defport,
444
    const char **ipv4, const char **ipv4_ascii, const char **ipv6,
445
    const char **ipv6_ascii, const char **p_ascii, int maxips,
446
    const struct token *t_err, const char *errid);
447
double vcc_DurationUnit(struct vcc *);
448
void vcc_ByteVal(struct vcc *, VCL_INT *);
449
void vcc_Duration(struct vcc *tl, double *);
450
uint64_t vcc_UintVal(struct vcc *tl);
451
uint8_t vcc_BoolVal(struct vcc *tl);
452
int vcc_IsFlag(struct vcc *tl);
453
int vcc_IsFlagRaw(struct vcc *, const struct token *, const struct token *);
454
char *vcc_Dup_be(const char *b, const char *e);
455
int vcc_Has_vcl_prefix(const char *b);
456
457
/* vcc_var.c */
458
void vcc_Header_Fh(const struct vcc *, const struct symbol *);
459
sym_wildcard_t vcc_Var_Wildcard;
460
461
/* vcc_vmod.c */
462
void vcc_ParseImport(struct vcc *tl);
463
void vcc_ImportVext(struct vcc *tl, const char *filename);
464
sym_act_f vcc_Act_New;
465
466
/* vcc_xref.c */
467
int vcc_CheckReferences(struct vcc *tl);
468
void VCC_InstanceInfo(struct vcc *tl);
469
void VCC_XrefTable(struct vcc *);
470
471
void vcc_AddCall(struct vcc *, struct token *, struct symbol *);
472
void vcc_ProcAction(struct proc *, unsigned, unsigned, struct token *);
473
int vcc_CheckAction(struct vcc *tl);
474
475
476
struct xrefuse { const char *name, *err; };
477
extern const struct xrefuse XREF_READ[1];
478
extern const struct xrefuse XREF_WRITE[1];
479
extern const struct xrefuse XREF_UNSET[1];
480
extern const struct xrefuse XREF_ACTION[1];
481
482
void vcc_AddUses(struct vcc *, const struct token *, const struct token *,
483
    const struct symbol *sym, const struct xrefuse *use);
484
int vcc_CheckUses(struct vcc *tl);
485
486 18390682
#define ERRCHK(tl)      do { if ((tl)->err) return; } while (0)
487 0
#define ErrInternal(tl) vcc__ErrInternal(tl, __func__, __LINE__)
488 10722
#define Expect(a, b) vcc__Expect(a, b, __LINE__)
489
#define ExpectErr(a, b) \
490 373187
    do { vcc__Expect(a, b, __LINE__); ERRCHK(a);} while (0)
491
#define SkipToken(a, b) \
492 827253
    do { vcc__Expect(a, b, __LINE__); ERRCHK(a); vcc_NextToken(a); } while (0)