11 PSI_Validator
*PSI_ValidatorInit(PSI_Validator
*V
, PSI_Lexer
*L
)
14 V
= malloc(sizeof(*V
));
16 memset(V
, 0, sizeof(*V
));
18 PSI_DataExchange((PSI_Data
*) V
, (PSI_Data
*) L
);
23 void PSI_ValidatorDtor(PSI_Validator
*V
)
26 free_decl_typedefs(V
->defs
);
37 memset(V
, 0, sizeof(*V
));
40 void PSI_ValidatorFree(PSI_Validator
**V
)
43 PSI_ValidatorDtor(*V
);
49 static inline int validate_lib(PSI_Validator
*V
) {
51 const char *ptr
= V
->lib
;
55 /* FIXME: assume stdlib */
57 fprintf(stderr
, "No import library defined;"
58 " use 'lib \"<libname>\";' statement.\n");
59 } else if (!strchr(ptr
, '/')) {
61 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.dylib", ptr
);
63 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.so", ptr
);
65 if (MAXPATHLEN
== len
) {
66 fprintf(stderr
, "Library name too long: '%s'\n", ptr
);
71 if (!(V
->dlopened
= dlopen(ptr
, RTLD_LAZY
|RTLD_LOCAL
))) {
77 static inline int locate_decl_type_alias(decl_typedefs
*defs
, decl_type
*type
) {
83 for (i
= 0; i
< defs
->count
; ++i
) {
84 if (!strcmp(defs
->list
[i
]->alias
, type
->name
)) {
85 type
->real
= defs
->list
[i
]->type
;
91 static inline int validate_decl_type(PSI_Validator
*V
, decl_arg
*arg
, decl_type
*type
) {
92 if (type
->type
== PSI_T_NAME
) {
95 if (!V
->defs
|| !locate_decl_type_alias(V
->defs
, type
)) {
96 fprintf(stderr
, "Cannot use '%s' as type for '%s';"
97 " Use 'typedef <type> <basic_type>;' statement.\n",
98 type
->name
, arg
->var
->name
);
103 static inline int validate_typedef(PSI_Validator
*V
, decl_typedef
*def
) {
104 /* FIXME: check def->alias */
105 if (def
->type
->type
== PSI_T_NAME
) {
106 fprintf(stderr
, "Type '%s' cannot be aliased to '%s'\n",
107 def
->type
->name
, def
->alias
);
112 static inline int validate_typedefs(PSI_Validator
*V
) {
115 for (i
= 0; i
< V
->defs
->count
; ++i
) {
116 if (!validate_typedef(V
, V
->defs
->list
[i
])) {
123 static inline int validate_decl_func(PSI_Validator
*V
, decl
*decl
, decl_arg
*func
)
127 if (!strcmp(func
->var
->name
, "dlsym")) {
128 fprintf(stderr
, "Cannot dlsym dlsym (sic!)\n");
132 if (!validate_decl_type(V
, func
, func
->type
)) {
136 decl
->dlptr
= dlsym(V
->dlopened
?: RTLD_DEFAULT
, func
->var
->name
);
138 fprintf(stderr
, "Failed to located symbol '%s': %s\n",
139 func
->var
->name
, dlerror());
143 static inline int validate_decl_abi(PSI_Validator
*V
, decl_abi
*abi
) {
144 if (strcasecmp(abi
->convention
, "default")) {
145 fprintf(stderr
, "Invalid calling convention: '%s'\n", abi
->convention
);
151 static inline int validate_decl_arg(PSI_Validator
*V
, decl
*decl
, decl_arg
*arg
) {
152 if (!validate_decl_type(V
, arg
, arg
->type
)) {
157 static inline int validate_decl_args(PSI_Validator
*V
, decl
*decl
, decl_args
*args
) {
160 for (i
= 0; i
< args
->count
; ++i
) {
161 if (!validate_decl_arg(V
, decl
, args
->args
[i
])) {
167 static inline int validate_decl(PSI_Validator
*V
, decl
*decl
) {
168 if (!validate_decl_abi(V
, decl
->abi
)) {
171 if (!validate_decl_func(V
, decl
, decl
->func
)) {
174 if (decl
->args
&& !validate_decl_args(V
, decl
, decl
->args
)) {
179 static inline int validate_decls(PSI_Validator
*V
) {
182 for (i
= 0; i
< V
->decls
->count
; ++i
) {
183 if (!validate_decl(V
, V
->decls
->list
[i
])) {
190 static inline int validate_impl_type(PSI_Validator
*V
, impl
*impl
, impl_type
*type
) {
194 static inline int validate_impl_arg(PSI_Validator
*V
, impl
*impl
, impl_arg
*arg
) {
197 static inline int validate_impl_args(PSI_Validator
*V
, impl
*impl
, impl_args
*args
) {
200 for (i
= 0; i
< args
->count
; ++i
) {
201 if (!validate_impl_arg(V
, impl
, args
->args
[i
])) {
207 static inline int validate_impl_func(PSI_Validator
*V
, impl
*impl
, impl_func
*func
) {
208 /* FIXME: does name need any validation? */
209 if (!validate_impl_type(V
, impl
, func
->return_type
)) {
212 if (func
->args
&& !validate_impl_args(V
, impl
, func
->args
)) {
217 static inline int validate_impl(PSI_Validator
*V
, impl
*impl
) {
218 if (!validate_impl_func(V
, impl
, impl
->func
)) {
221 if (!validate_impl_stmts(V
, impl
, impl
->stmts
)) {
226 static inline int validate_impls(PSI_Validator
*V
) {
229 for (i
= 0; i
< V
->impls
->count
; ++i
) {
230 if (!validate_impl(V
, V
->impls
->list
[i
])) {
237 int PSI_ValidatorValidate(PSI_Validator
*V
)
239 if (V
->defs
&& !validate_typedefs(V
)) {
242 if (V
->decls
&& !validate_decls(V
)) {
245 if (V
->impls
&& !validate_impls(V
)) {