11 PSI_Validator
*PSI_ValidatorInit(PSI_Validator
*V
, PSI_Parser
*P
)
14 V
= malloc(sizeof(*V
));
16 memset(V
, 0, sizeof(*V
));
18 PSI_DataExchange((PSI_Data
*) V
, (PSI_Data
*) P
);
23 void PSI_ValidatorDtor(PSI_Validator
*V
)
25 PSI_DataDtor((PSI_Data
*) V
);
26 memset(V
, 0, sizeof(*V
));
29 void PSI_ValidatorFree(PSI_Validator
**V
)
32 PSI_ValidatorDtor(*V
);
38 static int validate_lib(PSI_Validator
*V
) {
40 const char *ptr
= V
->lib
;
44 /* FIXME: assume stdlib */
46 } else if (!strchr(ptr
, '/')) {
48 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.dylib", ptr
);
50 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.so", ptr
);
52 if (MAXPATHLEN
== len
) {
53 V
->error(PSI_WARNING
, "Library name too long: '%s'", ptr
);
58 if (!(V
->dlopened
= dlopen(ptr
, RTLD_LAZY
|RTLD_LOCAL
))) {
59 V
->error(PSI_WARNING
, "Could not open library '%s': %s.", V
->lib
, dlerror());
65 static inline int locate_decl_type_alias(decl_typedefs
*defs
, decl_type
*type
) {
71 for (i
= 0; i
< defs
->count
; ++i
) {
72 if (!strcmp(defs
->list
[i
]->alias
, type
->name
)) {
73 type
->real
= defs
->list
[i
]->type
;
79 static inline int locate_decl_type_struct(decl_structs
*structs
, decl_type
*type
) {
85 for (i
= 0; i
< structs
->count
; ++i
) {
86 if (!strcmp(structs
->list
[i
]->name
, type
->name
)) {
87 type
->strct
= structs
->list
[i
];
93 static inline int validate_decl_type(PSI_Validator
*V
, decl_arg
*arg
, decl_type
*type
) {
96 if (!V
->defs
|| !locate_decl_type_alias(V
->defs
, type
)) {
97 V
->error(PSI_WARNING
, "Cannot use '%s' as type for '%s';"
98 " Use 'typedef <type> <alias>;' statement",
99 type
->name
, arg
->var
->name
);
102 return validate_decl_type(V
, arg
, type
->real
);
104 if (!V
->structs
|| !locate_decl_type_struct(V
->structs
, type
)) {
105 V
->error(PSI_WARNING
, "Cannot use '%s' as type for '%s';"
106 " Use 'typedef struct <name> <alias>;' statement",
107 type
->name
, arg
->var
->name
);
114 static inline int validate_typedef(PSI_Validator
*V
, decl_typedef
*def
) {
115 /* FIXME: check def->alias */
116 if (def
->type
->type
== PSI_T_NAME
) {
117 V
->error(PSI_WARNING
, "Type '%s' cannot be aliased to '%s'",
118 def
->type
->name
, def
->alias
);
123 static inline int validate_typedefs(PSI_Validator
*V
) {
126 for (i
= 0; i
< V
->defs
->count
; ++i
) {
127 if (!validate_typedef(V
, V
->defs
->list
[i
])) {
134 static inline int validate_decl_func(PSI_Validator
*V
, decl
*decl
, decl_arg
*func
)
136 if (!strcmp(func
->var
->name
, "dlsym")) {
137 V
->error(PSI_WARNING
, "Cannot dlsym dlsym (sic!)");
141 if (!validate_decl_type(V
, func
, func
->type
)) {
145 decl
->dlptr
= dlsym(V
->dlopened
?: RTLD_NEXT
, func
->var
->name
);
147 V
->error(PSI_WARNING
, "Failed to located symbol '%s': %s",
148 func
->var
->name
, dlerror());
152 static const char * const abi_ccs
[] = {
154 "extern", /* > - all the same */
159 static inline int validate_decl_abi(PSI_Validator
*V
, decl_abi
*abi
) {
162 for (i
= 0; i
< sizeof(abi_ccs
)/sizeof(char*); ++ i
) {
163 if (strcasecmp(abi
->convention
, abi_ccs
[i
])) {
167 V
->error(PSI_WARNING
, "Invalid calling convention: '%s'", abi
->convention
);
170 static inline int validate_decl_arg(PSI_Validator
*V
, decl_arg
*arg
) {
171 if (!validate_decl_type(V
, arg
, arg
->type
)) {
176 static inline int validate_decl_args(PSI_Validator
*V
, decl_args
*args
) {
179 for (i
= 0; i
< args
->count
; ++i
) {
180 if (!validate_decl_arg(V
, args
->args
[i
])) {
186 static inline int validate_decl(PSI_Validator
*V
, decl
*decl
) {
187 if (!validate_decl_abi(V
, decl
->abi
)) {
190 if (!validate_decl_func(V
, decl
, decl
->func
)) {
193 if (decl
->args
&& !validate_decl_args(V
, decl
->args
)) {
198 static inline int validate_decls(PSI_Validator
*V
) {
201 for (i
= 0; i
< V
->decls
->count
; ++i
) {
202 if (!validate_decl(V
, V
->decls
->list
[i
])) {
208 static inline int validate_struct(PSI_Validator
*V
, decl_struct
*s
) {
211 if (!validate_decl_args(V
, s
->args
)) {
215 s
->layout
= calloc(s
->args
->count
, sizeof(*s
->layout
));
216 for (i
= 0; i
< s
->args
->count
; ++i
) {
217 decl_type
*t
= real_decl_type(s
->args
->args
[i
]->type
);
220 decl_struct_layout
*l
= &s
->layout
[i
-1];
221 s
->layout
[i
].pos
= psi_t_align(t
->type
, l
->pos
+ l
->size
);
223 s
->layout
[i
].pos
= 0;
225 s
->layout
[i
].len
= psi_t_size(t
->type
);
229 static inline int validate_structs(PSI_Validator
*V
) {
232 for (i
= 0; i
< V
->structs
->count
; ++i
) {
233 if (!validate_struct(V
->structs
->list
[i
])) {
240 static inline int validate_impl_type(PSI_Validator
*V
, impl
*impl
, impl_type
*type
) {
244 static inline int validate_impl_arg(PSI_Validator
*V
, impl
*impl
, impl_arg
*arg
) {
247 static inline int validate_impl_args(PSI_Validator
*V
, impl
*impl
, impl_args
*args
) {
250 for (i
= 0; i
< args
->count
; ++i
) {
251 if (!validate_impl_arg(V
, impl
, args
->args
[i
])) {
257 static inline int validate_impl_func(PSI_Validator
*V
, impl
*impl
, impl_func
*func
) {
258 /* FIXME: does name need any validation? */
259 if (!validate_impl_type(V
, impl
, func
->return_type
)) {
262 if (func
->args
&& !validate_impl_args(V
, impl
, func
->args
)) {
267 static inline decl
*locate_impl_decl(decls
*decls
, return_stmt
*ret
) {
270 for (i
= 0; i
< decls
->count
; ++i
) {
271 if (!strcmp(decls
->list
[i
]->func
->var
->name
, ret
->decl
->name
)) {
272 return decls
->list
[i
];
278 static inline int validate_impl_stmts(PSI_Validator
*V
, impl
*impl
, impl_stmts
*stmts
) {
280 * - we must have exactly one ret stmt delcaring the native func to call and which type cast to apply
281 * - we can have multiple let stmts; every arg of the ret stmts var (the function to call) must have one
282 * - we can have any count of set stmts; processing out vars
283 * - we can have any count of free stmts; freeing any out vars
290 V
->error(PSI_WARNING
, "Missing body for implementation %s!",
294 if (stmts
->ret
.count
!= 1) {
295 if (stmts
->ret
.count
> 1) {
296 V
->error(PSI_WARNING
, "Too many `ret` statements for implmentation %s;"
297 "found %zu, exactly one is needed",
298 impl
->func
->name
, stmts
->ret
.count
);
300 V
->error(PSI_WARNING
, "Missing `ret` statement for implementation %s",
306 ret
= stmts
->ret
.list
[0];
307 decl
= locate_impl_decl(V
->decls
, ret
);
309 V
->error(PSI_WARNING
, "Missing declaration for implementation %s",
314 /* check that we have a let stmt for every decl arg */
315 for (i
= 0; i
< decl
->args
->count
; ++i
) {
316 decl_arg
*darg
= decl
->args
->args
[i
];
319 for (j
= 0; j
< stmts
->let
.count
; ++j
) {
320 let_stmt
*let
= stmts
->let
.list
[j
];
322 if (!strcmp(let
->var
->name
, darg
->var
->name
)) {
329 V
->error(PSI_WARNING
, "Missing `let` statement for arg '%s %.*s%s'"
330 "of declaration '%s' for implementation '%s'",
331 darg
->type
->name
, (int) darg
->var
->pointer_level
, "*****",
332 darg
->var
->name
, decl
->func
->var
->name
, impl
->func
->name
);
336 /* check that the let_value references a known variable or NULL */
337 for (i
= 0; i
< stmts
->let
.count
; ++i
) {
338 let_stmt
*let
= stmts
->let
.list
[i
];
341 if (let
->val
&& let
->val
->var
) {
342 for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
343 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
345 if (!strcmp(let
->val
->var
->name
, iarg
->var
->name
)) {
352 V
->error(PSI_WARNING
, "Unknown value '$%s' of `let` statement"
353 " for variable '%s' of implementation '%s'",
354 let
->val
->var
->name
, let
->var
->name
, impl
->func
->name
);
359 /* check that set stmts reference known variables */
360 for (i
= 0; i
< stmts
->set
.count
; ++i
) {
361 set_stmt
*set
= stmts
->set
.list
[i
];
364 for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
365 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
367 if (!strcmp(set
->var
->name
, iarg
->var
->name
)) {
374 V
->error(PSI_WARNING
, "Unknown variable '$%s' of `set` statement"
375 " of implementation '%s'",
376 set
->var
->name
, impl
->func
->name
);
380 for (j
= 0; j
< set
->val
->vars
->count
; ++j
) {
381 decl_var
*set_var
= set
->val
->vars
->vars
[j
];
384 for (k
= 0; k
< decl
->args
->count
; ++k
) {
385 decl_arg
*set_arg
= decl
->args
->args
[k
];
387 if (!strcmp(set_var
->name
, set_arg
->var
->name
)) {
389 set_var
->arg
= set_arg
;
395 V
->error(PSI_WARNING
, "Unknown value '%s' of `set` statement"
396 " for variable '$%s' of implementation '%s'",
397 set_var
->name
, set
->arg
->var
->name
, impl
->func
->name
);
402 /* check free stmts */
403 for (i
= 0; i
< stmts
->fre
.count
; ++i
) {
404 free_stmt
*fre
= stmts
->fre
.list
[i
];
406 for (j
= 0; j
< fre
->vars
->count
; ++j
) {
407 decl_var
*free_var
= fre
->vars
->vars
[j
];
410 if (!strcmp(free_var
->name
, decl
->func
->var
->name
)) {
413 for (k
= 0; k
< decl
->args
->count
; ++k
) {
414 decl_arg
*free_arg
= decl
->args
->args
[k
];
416 if (!strcmp(free_var
->name
, free_arg
->var
->name
)) {
418 free_var
->arg
= free_arg
;
424 V
->error(PSI_WARNING
, "Unknown variable '%s' of `free` statement"
425 " of implementation '%s'",
426 free_var
->name
, impl
->func
->name
);
437 static inline int validate_impl(PSI_Validator
*V
, impl
*impl
) {
438 if (!validate_impl_func(V
, impl
, impl
->func
)) {
441 if (!validate_impl_stmts(V
, impl
, impl
->stmts
)) {
446 static inline int validate_impls(PSI_Validator
*V
) {
449 for (i
= 0; i
< V
->impls
->count
; ++i
) {
450 if (!validate_impl(V
, V
->impls
->list
[i
])) {
457 int PSI_ValidatorValidate(PSI_Validator
*V
)
459 if (!validate_lib(V
)) {
462 if (V
->defs
&& !validate_typedefs(V
)) {
465 if (V
->structs
&& !validate_structs(V
)) {
468 if (V
->decls
&& !validate_decls(V
)) {
471 if (!V
->impls
|| !validate_impls(V
)) {