user32/tests: Factor out compare_bitmap_bits() function.
[wine.git] / tools / winebuild / parser.c
blobad996547a5bd3ec823164d8b0736dadb5d6e0d9d
1 /*
2 * Spec file parser
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997, 2004 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "build.h"
37 int current_line = 0;
39 static char ParseBuffer[512];
40 static char TokenBuffer[512];
41 static char *ParseNext = ParseBuffer;
42 static FILE *input_file;
44 static const char *separator_chars;
45 static const char *comment_chars;
47 /* valid characters in ordinal names */
48 static const char valid_ordname_chars[] = "/$:-_@?<>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
50 static const char * const TypeNames[TYPE_NBTYPES] =
52 "variable", /* TYPE_VARIABLE */
53 "pascal", /* TYPE_PASCAL */
54 "equate", /* TYPE_ABS */
55 "stub", /* TYPE_STUB */
56 "stdcall", /* TYPE_STDCALL */
57 "cdecl", /* TYPE_CDECL */
58 "varargs", /* TYPE_VARARGS */
59 "thiscall", /* TYPE_THISCALL */
60 "extern" /* TYPE_EXTERN */
63 static const char * const FlagNames[] =
65 "norelay", /* FLAG_NORELAY */
66 "noname", /* FLAG_NONAME */
67 "ret16", /* FLAG_RET16 */
68 "ret64", /* FLAG_RET64 */
69 "register", /* FLAG_REGISTER */
70 "private", /* FLAG_PRIVATE */
71 "ordinal", /* FLAG_ORDINAL */
72 NULL
75 static const char * const ArgNames[ARG_MAXARG + 1] =
77 "word", /* ARG_WORD */
78 "s_word", /* ARG_SWORD */
79 "segptr", /* ARG_SEGPTR */
80 "segstr", /* ARG_SEGSTR */
81 "long", /* ARG_LONG */
82 "ptr", /* ARG_PTR */
83 "str", /* ARG_STR */
84 "wstr", /* ARG_WSTR */
85 "int64", /* ARG_INT64 */
86 "int128", /* ARG_INT128 */
87 "float", /* ARG_FLOAT */
88 "double" /* ARG_DOUBLE */
91 static int IsNumberString(const char *s)
93 while (*s) if (!isdigit(*s++)) return 0;
94 return 1;
97 static inline int is_token_separator( char ch )
99 return strchr( separator_chars, ch ) != NULL;
102 static inline int is_token_comment( char ch )
104 return strchr( comment_chars, ch ) != NULL;
107 /* get the next line from the input file, or return 0 if at eof */
108 static int get_next_line(void)
110 ParseNext = ParseBuffer;
111 current_line++;
112 return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
115 static const char * GetToken( int allow_eol )
117 char *p;
118 char *token = TokenBuffer;
120 for (;;)
122 /* remove initial white space */
123 p = ParseNext;
124 while (isspace(*p)) p++;
126 if (*p == '\\' && p[1] == '\n') /* line continuation */
128 if (!get_next_line())
130 if (!allow_eol) error( "Unexpected end of file\n" );
131 return NULL;
134 else break;
137 if ((*p == '\0') || is_token_comment(*p))
139 if (!allow_eol) error( "Declaration not terminated properly\n" );
140 return NULL;
144 * Find end of token.
146 if (is_token_separator(*p))
148 /* a separator is always a complete token */
149 *token++ = *p++;
151 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
153 if (*p == '\\') p++;
154 if (*p) *token++ = *p++;
156 *token = '\0';
157 ParseNext = p;
158 return TokenBuffer;
162 static ORDDEF *add_entry_point( DLLSPEC *spec )
164 ORDDEF *ret;
166 if (spec->nb_entry_points == spec->alloc_entry_points)
168 spec->alloc_entry_points += 128;
169 spec->entry_points = xrealloc( spec->entry_points,
170 spec->alloc_entry_points * sizeof(*spec->entry_points) );
172 ret = &spec->entry_points[spec->nb_entry_points++];
173 memset( ret, 0, sizeof(*ret) );
174 return ret;
177 /*******************************************************************
178 * parse_spec_variable
180 * Parse a variable definition in a .spec file.
182 static int parse_spec_variable( ORDDEF *odp, DLLSPEC *spec )
184 char *endptr;
185 unsigned int *value_array;
186 int n_values;
187 int value_array_size;
188 const char *token;
190 if (spec->type == SPEC_WIN32)
192 error( "'variable' not supported in Win32, use 'extern' instead\n" );
193 return 0;
196 if (!(token = GetToken(0))) return 0;
197 if (*token != '(')
199 error( "Expected '(' got '%s'\n", token );
200 return 0;
203 n_values = 0;
204 value_array_size = 25;
205 value_array = xmalloc(sizeof(*value_array) * value_array_size);
207 for (;;)
209 if (!(token = GetToken(0)))
211 free( value_array );
212 return 0;
214 if (*token == ')')
215 break;
217 value_array[n_values++] = strtoul(token, &endptr, 0);
218 if (n_values == value_array_size)
220 value_array_size += 25;
221 value_array = xrealloc(value_array,
222 sizeof(*value_array) * value_array_size);
225 if (endptr == NULL || *endptr != '\0')
227 error( "Expected number value, got '%s'\n", token );
228 free( value_array );
229 return 0;
233 odp->u.var.n_values = n_values;
234 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
235 return 1;
239 /*******************************************************************
240 * parse_spec_arguments
242 * Parse the arguments of an entry point.
244 static int parse_spec_arguments( ORDDEF *odp, DLLSPEC *spec, int optional )
246 const char *token;
247 unsigned int i, arg;
248 int is_win32 = (spec->type == SPEC_WIN32) || (odp->flags & FLAG_EXPORT32);
250 if (!(token = GetToken( optional ))) return optional;
251 if (*token != '(')
253 error( "Expected '(' got '%s'\n", token );
254 return 0;
257 odp->u.func.nb_args = 0;
258 for (i = 0; i < MAX_ARGUMENTS; i++)
260 if (!(token = GetToken(0))) return 0;
261 if (*token == ')')
262 break;
264 for (arg = 0; arg <= ARG_MAXARG; arg++)
265 if (!strcmp( ArgNames[arg], token )) break;
267 if (arg > ARG_MAXARG)
269 error( "Unknown argument type '%s'\n", token );
270 return 0;
272 if (is_win32) switch (arg)
274 case ARG_WORD:
275 case ARG_SWORD:
276 case ARG_SEGPTR:
277 case ARG_SEGSTR:
278 error( "Argument type '%s' only allowed for Win16\n", token );
279 return 0;
281 odp->u.func.args[i] = arg;
283 if (*token != ')')
285 error( "Too many arguments\n" );
286 return 0;
289 odp->u.func.nb_args = i;
290 if (odp->type == TYPE_THISCALL && (!i || odp->u.func.args[0] != ARG_PTR))
292 error( "First argument of a thiscall function must be a pointer\n" );
293 return 0;
295 return 1;
299 /*******************************************************************
300 * parse_spec_export
302 * Parse an exported function definition in a .spec file.
304 static int parse_spec_export( ORDDEF *odp, DLLSPEC *spec )
306 const char *token;
307 int is_win32 = (spec->type == SPEC_WIN32) || (odp->flags & FLAG_EXPORT32);
309 if (!is_win32 && odp->type == TYPE_STDCALL)
311 error( "'stdcall' not supported for Win16\n" );
312 return 0;
314 if (!is_win32 && odp->type == TYPE_THISCALL)
316 error( "'thiscall' not supported for Win16\n" );
317 return 0;
319 if (is_win32 && odp->type == TYPE_PASCAL)
321 error( "'pascal' not supported for Win32\n" );
322 return 0;
325 if (!parse_spec_arguments( odp, spec, 0 )) return 0;
327 if (odp->type == TYPE_VARARGS)
328 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
330 if (!(token = GetToken(1)))
332 if (!strcmp( odp->name, "@" ))
334 error( "Missing handler name for anonymous function\n" );
335 return 0;
337 odp->link_name = xstrdup( odp->name );
339 else
341 odp->link_name = xstrdup( token );
342 if (strchr( odp->link_name, '.' ))
344 if (!is_win32)
346 error( "Forwarded functions not supported for Win16\n" );
347 return 0;
349 odp->flags |= FLAG_FORWARD;
352 if (target_cpu == CPU_x86 && odp->type == TYPE_THISCALL && !(odp->flags & FLAG_FORWARD))
354 char *link_name = strmake( "__thiscall_%s", odp->link_name );
355 free( odp->link_name );
356 odp->link_name = link_name;
358 return 1;
362 /*******************************************************************
363 * parse_spec_equate
365 * Parse an 'equate' definition in a .spec file.
367 static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
369 char *endptr;
370 int value;
371 const char *token;
373 if (spec->type == SPEC_WIN32)
375 error( "'equate' not supported for Win32\n" );
376 return 0;
378 if (!(token = GetToken(0))) return 0;
379 value = strtol(token, &endptr, 0);
380 if (endptr == NULL || *endptr != '\0')
382 error( "Expected number value, got '%s'\n", token );
383 return 0;
385 if (value < -0x8000 || value > 0xffff)
387 error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value );
388 value = 0;
390 odp->u.abs.value = value;
391 return 1;
395 /*******************************************************************
396 * parse_spec_stub
398 * Parse a 'stub' definition in a .spec file
400 static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
402 odp->u.func.nb_args = -1;
403 odp->link_name = xstrdup("");
404 /* don't bother generating stubs for Winelib */
405 if (odp->flags & FLAG_CPU_MASK)
406 odp->flags &= FLAG_CPU(CPU_x86) | FLAG_CPU(CPU_x86_64) | FLAG_CPU(CPU_ARM) | FLAG_CPU(CPU_ARM64);
407 else
408 odp->flags |= FLAG_CPU(CPU_x86) | FLAG_CPU(CPU_x86_64) | FLAG_CPU(CPU_ARM) | FLAG_CPU(CPU_ARM64);
410 return parse_spec_arguments( odp, spec, 1 );
414 /*******************************************************************
415 * parse_spec_extern
417 * Parse an 'extern' definition in a .spec file.
419 static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
421 const char *token;
423 if (spec->type == SPEC_WIN16)
425 error( "'extern' not supported for Win16, use 'variable' instead\n" );
426 return 0;
428 if (!(token = GetToken(1)))
430 if (!strcmp( odp->name, "@" ))
432 error( "Missing handler name for anonymous extern\n" );
433 return 0;
435 odp->link_name = xstrdup( odp->name );
437 else
439 odp->link_name = xstrdup( token );
440 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
442 return 1;
446 /*******************************************************************
447 * parse_spec_flags
449 * Parse the optional flags for an entry point in a .spec file.
451 static const char *parse_spec_flags( DLLSPEC *spec, ORDDEF *odp )
453 unsigned int i;
454 const char *token;
458 if (!(token = GetToken(0))) break;
459 if (!strncmp( token, "arch=", 5))
461 char *args = xstrdup( token + 5 );
462 char *cpu_name = strtok( args, "," );
463 while (cpu_name)
465 if (!strcmp( cpu_name, "win32" ))
467 if (spec->type == SPEC_WIN32)
468 odp->flags |= FLAG_CPU_WIN32;
469 else
470 odp->flags |= FLAG_EXPORT32;
472 else if (!strcmp( cpu_name, "win64" ))
473 odp->flags |= FLAG_CPU_WIN64;
474 else
476 int cpu = get_cpu_from_name( cpu_name );
477 if (cpu == -1)
479 error( "Unknown architecture '%s'\n", cpu_name );
480 return NULL;
482 odp->flags |= FLAG_CPU( cpu );
484 cpu_name = strtok( NULL, "," );
486 free( args );
488 else if (!strcmp( token, "i386" )) /* backwards compatibility */
490 odp->flags |= FLAG_CPU(CPU_x86);
492 else
494 for (i = 0; FlagNames[i]; i++)
495 if (!strcmp( FlagNames[i], token )) break;
496 if (!FlagNames[i])
498 error( "Unknown flag '%s'\n", token );
499 return NULL;
501 switch (1 << i)
503 case FLAG_RET16:
504 case FLAG_REGISTER:
505 if (spec->type == SPEC_WIN32)
506 error( "Flag '%s' is not supported in Win32\n", FlagNames[i] );
507 break;
508 case FLAG_RET64:
509 if (spec->type == SPEC_WIN16)
510 error( "Flag '%s' is not supported in Win16\n", FlagNames[i] );
511 break;
513 odp->flags |= 1 << i;
515 token = GetToken(0);
516 } while (token && *token == '-');
518 return token;
522 /*******************************************************************
523 * parse_spec_ordinal
525 * Parse an ordinal definition in a .spec file.
527 static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
529 const char *token;
530 size_t len;
531 ORDDEF *odp = add_entry_point( spec );
533 if (!(token = GetToken(0))) goto error;
535 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
536 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
537 break;
539 if (odp->type >= TYPE_NBTYPES)
541 error( "Expected type after ordinal, found '%s' instead\n", token );
542 goto error;
545 if (!(token = GetToken(0))) goto error;
546 if (*token == '-' && !(token = parse_spec_flags( spec, odp ))) goto error;
548 if (ordinal == -1 && spec->type != SPEC_WIN32 && !(odp->flags & FLAG_EXPORT32))
550 error( "'@' ordinals not supported for Win16\n" );
551 goto error;
554 odp->name = xstrdup( token );
555 odp->lineno = current_line;
556 odp->ordinal = ordinal;
558 len = strspn( odp->name, valid_ordname_chars );
559 if (len < strlen( odp->name ))
561 error( "Character '%c' is not allowed in exported name '%s'\n", odp->name[len], odp->name );
562 goto error;
565 switch(odp->type)
567 case TYPE_VARIABLE:
568 if (!parse_spec_variable( odp, spec )) goto error;
569 break;
570 case TYPE_PASCAL:
571 case TYPE_STDCALL:
572 case TYPE_VARARGS:
573 case TYPE_CDECL:
574 case TYPE_THISCALL:
575 if (!parse_spec_export( odp, spec )) goto error;
576 break;
577 case TYPE_ABS:
578 if (!parse_spec_equate( odp, spec )) goto error;
579 break;
580 case TYPE_STUB:
581 if (!parse_spec_stub( odp, spec )) goto error;
582 break;
583 case TYPE_EXTERN:
584 if (!parse_spec_extern( odp, spec )) goto error;
585 break;
586 default:
587 assert( 0 );
590 if ((odp->flags & FLAG_CPU_MASK) && !(odp->flags & FLAG_CPU(target_cpu)))
592 /* ignore this entry point */
593 spec->nb_entry_points--;
594 return 1;
597 if (ordinal != -1)
599 if (!ordinal)
601 error( "Ordinal 0 is not valid\n" );
602 goto error;
604 if (ordinal >= MAX_ORDINALS)
606 error( "Ordinal number %d too large\n", ordinal );
607 goto error;
609 if (ordinal > spec->limit) spec->limit = ordinal;
610 if (ordinal < spec->base) spec->base = ordinal;
611 odp->ordinal = ordinal;
614 if (odp->type == TYPE_STDCALL && !(odp->flags & FLAG_PRIVATE))
616 if (!strcmp( odp->name, "DllRegisterServer" ) ||
617 !strcmp( odp->name, "DllUnregisterServer" ) ||
618 !strcmp( odp->name, "DllMain" ) ||
619 !strcmp( odp->name, "DllGetClassObject" ) ||
620 !strcmp( odp->name, "DllGetVersion" ) ||
621 !strcmp( odp->name, "DllInstall" ) ||
622 !strcmp( odp->name, "DllCanUnloadNow" ))
624 warning( "Function %s should be marked private\n", odp->name );
625 if (strcmp( odp->name, odp->link_name ))
626 warning( "Function %s should not use a different internal name (%s)\n",
627 odp->name, odp->link_name );
631 if (!strcmp( odp->name, "@" ) || odp->flags & (FLAG_NONAME | FLAG_ORDINAL))
633 if (ordinal == -1)
635 if (!strcmp( odp->name, "@" ))
636 error( "Nameless function needs an explicit ordinal number\n" );
637 else
638 error( "Function imported by ordinal needs an explicit ordinal number\n" );
639 goto error;
641 if (spec->type != SPEC_WIN32)
643 error( "Nameless functions not supported for Win16\n" );
644 goto error;
646 if (!strcmp( odp->name, "@" ))
648 free( odp->name );
649 odp->name = NULL;
651 else if (!(odp->flags & FLAG_ORDINAL)) /* -ordinal only affects the import library */
653 odp->export_name = odp->name;
654 odp->name = NULL;
657 return 1;
659 error:
660 spec->nb_entry_points--;
661 free( odp->name );
662 return 0;
666 static int name_compare( const void *ptr1, const void *ptr2 )
668 const ORDDEF *odp1 = *(const ORDDEF * const *)ptr1;
669 const ORDDEF *odp2 = *(const ORDDEF * const *)ptr2;
670 const char *name1 = odp1->name ? odp1->name : odp1->export_name;
671 const char *name2 = odp2->name ? odp2->name : odp2->export_name;
672 return strcmp( name1, name2 );
675 /*******************************************************************
676 * assign_names
678 * Build the name array and catch duplicates.
680 static void assign_names( DLLSPEC *spec )
682 int i, j, nb_exp_names = 0;
683 ORDDEF **all_names;
685 spec->nb_names = 0;
686 for (i = 0; i < spec->nb_entry_points; i++)
687 if (spec->entry_points[i].name) spec->nb_names++;
688 else if (spec->entry_points[i].export_name) nb_exp_names++;
690 if (!spec->nb_names && !nb_exp_names) return;
692 /* check for duplicates */
694 all_names = xmalloc( (spec->nb_names + nb_exp_names) * sizeof(all_names[0]) );
695 for (i = j = 0; i < spec->nb_entry_points; i++)
696 if (spec->entry_points[i].name || spec->entry_points[i].export_name)
697 all_names[j++] = &spec->entry_points[i];
699 qsort( all_names, j, sizeof(all_names[0]), name_compare );
701 for (i = 0; i < j - 1; i++)
703 const char *name1 = all_names[i]->name ? all_names[i]->name : all_names[i]->export_name;
704 const char *name2 = all_names[i+1]->name ? all_names[i+1]->name : all_names[i+1]->export_name;
705 if (!strcmp( name1, name2 ) &&
706 !((all_names[i]->flags ^ all_names[i+1]->flags) & FLAG_EXPORT32))
708 current_line = max( all_names[i]->lineno, all_names[i+1]->lineno );
709 error( "'%s' redefined\n%s:%d: First defined here\n",
710 name1, input_file_name,
711 min( all_names[i]->lineno, all_names[i+1]->lineno ) );
714 free( all_names );
716 if (spec->nb_names)
718 spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
719 for (i = j = 0; i < spec->nb_entry_points; i++)
720 if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
722 /* sort the list of names */
723 qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
727 /*******************************************************************
728 * assign_ordinals
730 * Build the ordinal array.
732 static void assign_ordinals( DLLSPEC *spec )
734 int i, count, ordinal;
736 /* start assigning from base, or from 1 if no ordinal defined yet */
738 spec->base = MAX_ORDINALS;
739 spec->limit = 0;
740 for (i = 0; i < spec->nb_entry_points; i++)
742 ordinal = spec->entry_points[i].ordinal;
743 if (ordinal == -1) continue;
744 if (ordinal > spec->limit) spec->limit = ordinal;
745 if (ordinal < spec->base) spec->base = ordinal;
747 if (spec->base == MAX_ORDINALS) spec->base = 1;
748 if (spec->limit < spec->base) spec->limit = spec->base;
750 count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
751 spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
752 memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
754 /* fill in all explicitly specified ordinals */
755 for (i = 0; i < spec->nb_entry_points; i++)
757 ordinal = spec->entry_points[i].ordinal;
758 if (ordinal == -1) continue;
759 if (spec->ordinals[ordinal])
761 current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
762 error( "ordinal %d redefined\n%s:%d: First defined here\n",
763 ordinal, input_file_name,
764 min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
766 else spec->ordinals[ordinal] = &spec->entry_points[i];
769 /* now assign ordinals to the rest */
770 for (i = 0, ordinal = spec->base; i < spec->nb_entry_points; i++)
772 if (spec->entry_points[i].ordinal != -1) continue;
773 while (spec->ordinals[ordinal]) ordinal++;
774 if (ordinal >= MAX_ORDINALS)
776 current_line = spec->entry_points[i].lineno;
777 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
779 spec->entry_points[i].ordinal = ordinal;
780 spec->ordinals[ordinal] = &spec->entry_points[i];
782 if (ordinal > spec->limit) spec->limit = ordinal;
786 /*******************************************************************
787 * add_16bit_exports
789 * Add the necessary exports to the 32-bit counterpart of a 16-bit module.
791 void add_16bit_exports( DLLSPEC *spec32, DLLSPEC *spec16 )
793 int i;
794 ORDDEF *odp;
796 spec32->file_name = xstrdup( spec16->file_name );
797 if (spec16->characteristics & IMAGE_FILE_DLL)
799 spec32->characteristics = IMAGE_FILE_DLL;
800 spec32->init_func = xstrdup( "__wine_spec_dll_entry" );
803 /* add an export for the NE module */
805 odp = add_entry_point( spec32 );
806 odp->type = TYPE_EXTERN;
807 odp->flags = FLAG_PRIVATE;
808 odp->name = xstrdup( "__wine_spec_dos_header" );
809 odp->lineno = 0;
810 odp->ordinal = 1;
811 odp->link_name = xstrdup( ".L__wine_spec_dos_header" );
813 if (spec16->main_module)
815 odp = add_entry_point( spec32 );
816 odp->type = TYPE_EXTERN;
817 odp->flags = FLAG_PRIVATE;
818 odp->name = xstrdup( "__wine_spec_main_module" );
819 odp->lineno = 0;
820 odp->ordinal = 2;
821 odp->link_name = xstrdup( ".L__wine_spec_main_module" );
824 /* add the explicit win32 exports */
826 for (i = 1; i <= spec16->limit; i++)
828 ORDDEF *odp16 = spec16->ordinals[i];
830 if (!odp16 || !odp16->name) continue;
831 if (!(odp16->flags & FLAG_EXPORT32)) continue;
833 odp = add_entry_point( spec32 );
834 odp->flags = odp16->flags & ~FLAG_EXPORT32;
835 odp->type = odp16->type;
836 odp->name = xstrdup( odp16->name );
837 odp->lineno = odp16->lineno;
838 odp->ordinal = -1;
839 odp->link_name = xstrdup( odp16->link_name );
840 odp->u.func.nb_args = odp16->u.func.nb_args;
841 if (odp->u.func.nb_args > 0) memcpy( odp->u.func.args, odp16->u.func.args,
842 odp->u.func.nb_args * sizeof(odp->u.func.args[0]) );
845 assign_names( spec32 );
846 assign_ordinals( spec32 );
850 /*******************************************************************
851 * parse_spec_file
853 * Parse a .spec file.
855 int parse_spec_file( FILE *file, DLLSPEC *spec )
857 const char *token;
859 input_file = file;
860 current_line = 0;
862 comment_chars = "#;";
863 separator_chars = "()-";
865 while (get_next_line())
867 if (!(token = GetToken(1))) continue;
868 if (strcmp(token, "@") == 0)
870 if (!parse_spec_ordinal( -1, spec )) continue;
872 else if (IsNumberString(token))
874 if (!parse_spec_ordinal( atoi(token), spec )) continue;
876 else
878 error( "Expected ordinal declaration, got '%s'\n", token );
879 continue;
881 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
884 current_line = 0; /* no longer parsing the input file */
885 assign_names( spec );
886 assign_ordinals( spec );
887 return !nb_errors;
891 /*******************************************************************
892 * parse_def_library
894 * Parse a LIBRARY declaration in a .def file.
896 static int parse_def_library( DLLSPEC *spec )
898 const char *token = GetToken(1);
900 if (!token) return 1;
901 if (strcmp( token, "BASE" ))
903 free( spec->file_name );
904 spec->file_name = xstrdup( token );
905 if (!(token = GetToken(1))) return 1;
907 if (strcmp( token, "BASE" ))
909 error( "Expected library name or BASE= declaration, got '%s'\n", token );
910 return 0;
912 if (!(token = GetToken(0))) return 0;
913 if (strcmp( token, "=" ))
915 error( "Expected '=' after BASE, got '%s'\n", token );
916 return 0;
918 if (!(token = GetToken(0))) return 0;
919 /* FIXME: do something with base address */
921 return 1;
925 /*******************************************************************
926 * parse_def_stack_heap_size
928 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
930 static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
932 const char *token = GetToken(0);
933 char *end;
934 unsigned long size;
936 if (!token) return 0;
937 size = strtoul( token, &end, 0 );
938 if (*end)
940 error( "Invalid number '%s'\n", token );
941 return 0;
943 if (is_stack) spec->stack_size = size / 1024;
944 else spec->heap_size = size / 1024;
945 if (!(token = GetToken(1))) return 1;
946 if (strcmp( token, "," ))
948 error( "Expected ',' after size, got '%s'\n", token );
949 return 0;
951 if (!(token = GetToken(0))) return 0;
952 /* FIXME: do something with reserve size */
953 return 1;
957 /*******************************************************************
958 * parse_def_export
960 * Parse an export declaration in a .def file.
962 static int parse_def_export( char *name, DLLSPEC *spec )
964 int i, args;
965 const char *token = GetToken(1);
966 ORDDEF *odp = add_entry_point( spec );
968 odp->lineno = current_line;
969 odp->ordinal = -1;
970 odp->name = name;
971 args = remove_stdcall_decoration( odp->name );
972 if (args == -1)
974 odp->type = TYPE_CDECL;
975 args = 0;
977 else
979 odp->type = TYPE_STDCALL;
980 args /= get_ptr_size();
981 if (args >= MAX_ARGUMENTS)
983 error( "Too many arguments in stdcall function '%s'\n", odp->name );
984 return 0;
986 for (i = 0; i < args; i++) odp->u.func.args[i] = ARG_LONG;
988 odp->u.func.nb_args = args;
990 /* check for optional internal name */
992 if (token && !strcmp( token, "=" ))
994 if (!(token = GetToken(0))) goto error;
995 odp->link_name = xstrdup( token );
996 remove_stdcall_decoration( odp->link_name );
997 token = GetToken(1);
999 else
1001 odp->link_name = xstrdup( name );
1004 /* check for optional ordinal */
1006 if (token && token[0] == '@')
1008 int ordinal;
1010 if (!IsNumberString( token+1 ))
1012 error( "Expected number after '@', got '%s'\n", token+1 );
1013 goto error;
1015 ordinal = atoi( token+1 );
1016 if (!ordinal)
1018 error( "Ordinal 0 is not valid\n" );
1019 goto error;
1021 if (ordinal >= MAX_ORDINALS)
1023 error( "Ordinal number %d too large\n", ordinal );
1024 goto error;
1026 odp->ordinal = ordinal;
1027 token = GetToken(1);
1030 /* check for other optional keywords */
1032 while (token)
1034 if (!strcmp( token, "NONAME" ))
1036 if (odp->ordinal == -1)
1038 error( "NONAME requires an ordinal\n" );
1039 goto error;
1041 odp->export_name = odp->name;
1042 odp->name = NULL;
1043 odp->flags |= FLAG_NONAME;
1045 else if (!strcmp( token, "PRIVATE" ))
1047 odp->flags |= FLAG_PRIVATE;
1049 else if (!strcmp( token, "DATA" ))
1051 odp->type = TYPE_EXTERN;
1053 else
1055 error( "Garbage text '%s' found at end of export declaration\n", token );
1056 goto error;
1058 token = GetToken(1);
1060 return 1;
1062 error:
1063 spec->nb_entry_points--;
1064 free( odp->name );
1065 return 0;
1069 /*******************************************************************
1070 * parse_def_file
1072 * Parse a .def file.
1074 int parse_def_file( FILE *file, DLLSPEC *spec )
1076 const char *token;
1077 int in_exports = 0;
1079 input_file = file;
1080 current_line = 0;
1082 comment_chars = ";";
1083 separator_chars = ",=";
1085 while (get_next_line())
1087 if (!(token = GetToken(1))) continue;
1089 if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
1091 if (!parse_def_library( spec )) continue;
1092 goto end_of_line;
1094 else if (!strcmp( token, "STACKSIZE" ))
1096 if (!parse_def_stack_heap_size( 1, spec )) continue;
1097 goto end_of_line;
1099 else if (!strcmp( token, "HEAPSIZE" ))
1101 if (!parse_def_stack_heap_size( 0, spec )) continue;
1102 goto end_of_line;
1104 else if (!strcmp( token, "EXPORTS" ))
1106 in_exports = 1;
1107 if (!(token = GetToken(1))) continue;
1109 else if (!strcmp( token, "IMPORTS" ))
1111 in_exports = 0;
1112 if (!(token = GetToken(1))) continue;
1114 else if (!strcmp( token, "SECTIONS" ))
1116 in_exports = 0;
1117 if (!(token = GetToken(1))) continue;
1120 if (!in_exports) continue; /* ignore this line */
1121 if (!parse_def_export( xstrdup(token), spec )) continue;
1123 end_of_line:
1124 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
1127 current_line = 0; /* no longer parsing the input file */
1128 assign_names( spec );
1129 assign_ordinals( spec );
1130 return !nb_errors;