-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimple-decl-proto.c
94 lines (76 loc) · 1.68 KB
/
simple-decl-proto.c
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
#include "simple-decl-proto.h"
#include "config.h"
#include <assert.h>
#include <err.h>
#include "libks/arena.h"
#include "libks/vector.h"
#include "lexer.h"
#include "token.h"
struct simple_decl_proto {
VECTOR(struct argument) arguments;
struct lexer *lx;
struct {
unsigned int ignore:1;
} flags;
};
struct argument {
struct token *tk;
};
struct simple_decl_proto *
simple_decl_proto_enter(struct lexer *lx, struct arena_scope *s)
{
struct simple_decl_proto *sp;
sp = arena_calloc(s, 1, sizeof(*sp));
if (VECTOR_INIT(sp->arguments))
err(1, NULL);
sp->lx = lx;
return sp;
}
void
simple_decl_proto_leave(struct simple_decl_proto *sp)
{
size_t nargs = VECTOR_LENGTH(sp->arguments);
size_t nunnamed = 0;
size_t i;
if (sp->flags.ignore)
return;
for (i = 0; i < VECTOR_LENGTH(sp->arguments); i++) {
struct argument *arg = &sp->arguments[i];
if (arg->tk == NULL)
nunnamed++;
}
if (nunnamed == 0 || nargs == nunnamed)
return;
for (i = 0; i < VECTOR_LENGTH(sp->arguments); i++) {
struct argument *arg = &sp->arguments[i];
if (arg->tk != NULL)
lexer_remove(sp->lx, arg->tk);
}
}
void
simple_decl_proto_free(struct simple_decl_proto *sp)
{
if (sp == NULL)
return;
VECTOR_FREE(sp->arguments);
}
void
simple_decl_proto_arg(struct simple_decl_proto *sp)
{
if (VECTOR_CALLOC(sp->arguments) == NULL)
err(1, NULL);
}
void
simple_decl_proto_arg_ident(struct simple_decl_proto *sp, struct token *tk)
{
struct argument *arg;
struct token *pv;
arg = VECTOR_LAST(sp->arguments);
assert(arg != NULL);
pv = token_prev(tk);
if (pv->tk_type == TOKEN_STAR ||
(pv->tk_flags & (TOKEN_FLAG_TYPE | TOKEN_FLAG_QUALIFIER)))
arg->tk = tk;
else
sp->flags.ignore = 1;
}