10 typedef struct PSI_Validator
{
20 PSI_Validator
*PSI_ValidatorInit(PSI_Validator
*V
, PSI_Lexer
*L
)
23 V
= malloc(sizeof(*V
));
25 memset(V
, 0, sizeof(*V
));
27 PSI_DataExchange((PSI_Data
*) V
, (PSI_Data
*) L
);
32 void PSI_ValidatorDtor(PSI_Validator
*V
)
35 free_decl_typedefs(V
->defs
);
46 memset(V
, 0, sizeof(*V
));
49 void PSI_ValidatorFree(PSI_Validator
**V
)
52 PSI_ValidatorDtor(*V
);
58 static inline int validate_lib(PSI_Validator
*V
) {
60 const char *ptr
= V
->lib
;
64 /* FIXME: assume stdlib */
66 fprintf(stderr
, "No import library defined;"
67 " use 'lib \"<libname>\";' statement.\n");
68 } else if (!strchr(ptr
, '/')) {
70 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.dylib", ptr
);
72 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.so", ptr
);
74 if (MAXPATHLEN
== len
) {
75 fprintf(stderr
, "Library name too long: '%s'\n", ptr
);
80 if (!(V
->dlopened
= dlopen(ptr
, RTLD_LAZY
|RTLD_LOCAL
))) {
86 static inline int locate_decl_type_alias(decl_typedefs
*defs
, decl_type
*type
) {
92 for (i
= 0; i
< defs
->count
; ++i
) {
93 if (!strcmp(defs
->list
[i
]->alias
, type
->name
)) {
94 type
->real
= defs
->list
[i
]->type
;
100 static inline int validate_decl_type(PSI_Validator
*V
, decl_arg
*arg
, decl_type
*type
) {
101 if (type
->type
== PSI_T_NAME
) {
104 if (!V
->defs
|| !locate_decl_type_alias(V
->defs
, type
)) {
105 fprintf(stderr
, "Cannot use '%s' as type for '%s';"
106 " Use 'typedef <type> <basic_type>;' statement.\n",
107 type
->name
, arg
->var
->name
);
112 static inline int validate_typedef(PSI_Validator
*V
, decl_typedef
*def
) {
113 /* FIXME: check def->alias */
114 if (def
->type
->type
== PSI_T_NAME
) {
115 fprintf(stderr
, "Type '%s' cannot be aliased to '%s'\n",
116 def
->type
->name
, def
->alias
);
121 static inline int validate_typedefs(PSI_Validator
*V
) {
124 for (i
= 0; i
< V
->defs
->count
; ++i
) {
125 if (!validate_typedef(V
, V
->defs
->list
[i
])) {
132 static inline int validate_decl_func(PSI_Validator
*V
, decl
*decl
, decl_arg
*func
)
136 if (!strcmp(func
->var
->name
, "dlsym")) {
137 fprintf(stderr
, "Cannot dlsym dlsym (sic!)\n");
141 if (!validate_decl_type(V
, func
, func
->type
)) {
145 decl
->dlptr
= dlsym(V
->dlopened
?: RTLD_DEFAULT
, func
->var
->name
);
147 fprintf(stderr
, "Failed to located symbol '%s': %s\n",
148 func
->var
->name
, dlerror());
152 static inline int validate_decl_abi(PSI_Validator
*V
, decl_abi
*abi
) {
153 if (strcasecmp(abi
->convention
, "default")) {
154 fprintf(stderr
, "Invalid calling convention: '%s'\n", abi
->convention
);
160 static inline int validate_decl_arg(PSI_Validator
*V
, decl
*decl
, decl_arg
*arg
) {
161 if (!validate_decl_type(V
, arg
, arg
->type
)) {
166 static inline int validate_decl_args(PSI_Validator
*V
, decl
*decl
, decl_args
*args
) {
169 for (i
= 0; i
< args
->count
; ++i
) {
170 if (!validate_decl_arg(V
, decl
, args
->args
[i
])) {
176 static inline int validate_decl(PSI_Validator
*V
, decl
*decl
) {
177 if (!validate_decl_abi(V
, decl
->abi
)) {
180 if (!validate_decl_func(V
, decl
, decl
->func
)) {
183 if (decl
->args
&& !validate_decl_args(V
, decl
, decl
->args
)) {
188 static inline int validate_decls(PSI_Validator
*V
) {
191 for (i
= 0; i
< V
->decls
->count
; ++i
) {
192 if (!validate_decl(V
, V
->decls
->list
[i
])) {
199 static inline int validate_impl_type(PSI_Validator
*V
, impl
*impl
, impl_type
*type
) {
203 static inline int validate_impl_arg(PSI_Validator
*V
, impl
*impl
, impl_arg
*arg
) {
206 static inline int validate_impl_args(PSI_Validator
*V
, impl
*impl
, impl_args
*args
) {
209 for (i
= 0; i
< args
->count
; ++i
) {
210 if (!validate_impl_arg(V
, impl
, args
->args
[i
])) {
216 static inline int validate_impl_func(PSI_Validator
*V
, impl
*impl
, impl_func
*func
) {
217 /* FIXME: does name need any validation? */
218 if (!validate_impl_type(V
, impl
, func
->return_type
)) {
221 if (func
->args
&& !validate_impl_args(V
, impl
, func
->args
)) {
226 static inline int validate_impl(PSI_Validator
*V
, impl
*impl
) {
227 if (!validate_impl_func(V
, impl
, impl
->func
)) {
230 if (!validate_impl_stmts(V
, impl
, impl
->stmts
)) {
235 static inline int validate_impls(PSI_Validator
*V
) {
238 for (i
= 0; i
< V
->impls
->count
; ++i
) {
239 if (!validate_impl(V
, V
->impls
->list
[i
])) {
246 int PSI_ValidatorValidate(PSI_Validator
*V
)
248 if (V
->defs
&& !validate_typedefs(V
)) {
251 if (V
->decls
&& !validate_decls(V
)) {
254 if (V
->impls
&& !validate_impls(V
)) {