54e646842e1b26586753a81a1f7dfc9f78e82e25
9 PSI_Validator
*PSI_ValidatorInit(PSI_Validator
*V
, PSI_Lexer
*L
)
12 V
= malloc(sizeof(*V
));
14 memset(V
, 0, sizeof(*V
));
16 PSI_DataExchange((PSI_Data
*) V
, (PSI_Data
*) L
);
21 void PSI_ValidatorDtor(PSI_Validator
*V
)
24 free_decl_typedefs(V
->defs
);
35 memset(V
, 0, sizeof(*V
));
38 void PSI_ValidatorFree(PSI_Validator
**V
)
41 PSI_ValidatorDtor(*V
);
47 static inline int validate_lib(PSI_Validator
*V
) {
49 const char *ptr
= V
->lib
;
53 /* FIXME: assume stdlib */
55 fprintf(stderr
, "No import library defined;"
56 " use 'lib \"<libname>\";' statement.\n");
57 } else if (!strchr(ptr
, '/')) {
59 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.dylib", ptr
);
61 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.so", ptr
);
63 if (MAXPATHLEN
== len
) {
64 fprintf(stderr
, "Library name too long: '%s'\n", ptr
);
69 if (!(V
->dlopened
= dlopen(ptr
, RTLD_LAZY
|RTLD_LOCAL
))) {
75 static inline int locate_decl_type_alias(decl_typedefs
*defs
, decl_type
*type
) {
81 for (i
= 0; i
< defs
->count
; ++i
) {
82 if (!strcmp(defs
->list
[i
]->alias
, type
->name
)) {
83 type
->real
= defs
->list
[i
]->type
;
89 static inline int validate_decl_type(PSI_Validator
*V
, decl_arg
*arg
, decl_type
*type
) {
90 if (type
->type
== PSI_T_NAME
) {
93 if (!V
->defs
|| !locate_decl_type_alias(V
->defs
, type
)) {
94 fprintf(stderr
, "Cannot use '%s' as type for '%s';"
95 " Use 'typedef <type> <basic_type>;' statement.\n",
96 type
->name
, arg
->var
->name
);
101 static inline int validate_typedef(PSI_Validator
*V
, decl_typedef
*def
) {
102 /* FIXME: check def->alias */
103 if (def
->type
->type
== PSI_T_NAME
) {
104 fprintf(stderr
, "Type '%s' cannot be aliased to '%s'\n",
105 def
->type
->name
, def
->alias
);
110 static inline int validate_typedefs(PSI_Validator
*V
) {
113 for (i
= 0; i
< V
->defs
->count
; ++i
) {
114 if (!validate_typedef(V
, V
->defs
->list
[i
])) {
121 static inline int validate_decl_func(PSI_Validator
*V
, decl
*decl
, decl_arg
*func
)
125 if (!strcmp(func
->var
->name
, "dlsym")) {
126 fprintf(stderr
, "Cannot dlsym dlsym (sic!)\n");
130 if (!validate_decl_type(V
, func
, func
->type
)) {
134 decl
->dlptr
= dlsym(V
->dlopened
?: RTLD_DEFAULT
, func
->var
->name
);
136 fprintf(stderr
, "Failed to located symbol '%s': %s\n",
137 func
->var
->name
, dlerror());
141 static inline int validate_decl_abi(PSI_Validator
*V
, decl_abi
*abi
) {
142 if (strcasecmp(abi
->convention
, "default")) {
143 fprintf(stderr
, "Invalid calling convention: '%s'\n", abi
->convention
);
149 static inline int validate_decl_arg(PSI_Validator
*V
, decl
*decl
, decl_arg
*arg
) {
150 if (!validate_decl_type(V
, arg
, arg
->type
)) {
155 static inline int validate_decl_args(PSI_Validator
*V
, decl
*decl
, decl_args
*args
) {
158 for (i
= 0; i
< args
->count
; ++i
) {
159 if (!validate_decl_arg(V
, decl
, args
->args
[i
])) {
165 static inline int validate_decl(PSI_Validator
*V
, decl
*decl
) {
166 if (!validate_decl_abi(V
, decl
->abi
)) {
169 if (!validate_decl_func(V
, decl
, decl
->func
)) {
172 if (decl
->args
&& !validate_decl_args(V
, decl
, decl
->args
)) {
177 static inline int validate_decls(PSI_Validator
*V
) {
180 for (i
= 0; i
< V
->decls
->count
; ++i
) {
181 if (!validate_decl(V
, V
->decls
->list
[i
])) {
188 static inline int validate_impl_type(PSI_Validator
*V
, impl
*impl
, impl_type
*type
) {
192 static inline int validate_impl_arg(PSI_Validator
*V
, impl
*impl
, impl_arg
*arg
) {
195 static inline int validate_impl_args(PSI_Validator
*V
, impl
*impl
, impl_args
*args
) {
198 for (i
= 0; i
< args
->count
; ++i
) {
199 if (!validate_impl_arg(V
, impl
, args
->args
[i
])) {
205 static inline int validate_impl_func(PSI_Validator
*V
, impl
*impl
, impl_func
*func
) {
206 /* FIXME: does name need any validation? */
207 if (!validate_impl_type(V
, impl
, func
->return_type
)) {
210 if (func
->args
&& !validate_impl_args(V
, impl
, func
->args
)) {
215 static inline int validate_impl(PSI_Validator
*V
, impl
*impl
) {
216 if (!validate_impl_func(V
, impl
, impl
->func
)) {
219 if (!validate_impl_stmts(V
, impl
, impl
->stmts
)) {
224 static inline int validate_impls(PSI_Validator
*V
) {
227 for (i
= 0; i
< V
->impls
->count
; ++i
) {
228 if (!validate_impl(V
, V
->impls
->list
[i
])) {
235 int PSI_ValidatorValidate(PSI_Validator
*V
)
237 if (V
->defs
&& !validate_typedefs(V
)) {
240 if (V
->decls
&& !validate_decls(V
)) {
243 if (V
->impls
&& !validate_impls(V
)) {