msvcr100: Use Context blocking functions in critical_section class.
[wine.git] / tools / winebuild / parser.c
blobeaf90a8596652b4c150ae8382361420748bb17c4
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"
27 #include <assert.h>
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "build.h"
36 int current_line = 0;
38 static char ParseBuffer[512];
39 static char TokenBuffer[512];
40 static char *ParseNext = ParseBuffer;
41 static FILE *input_file;
43 static const char *separator_chars;
44 static const char *comment_chars;
46 /* valid characters in ordinal names */
47 static const char valid_ordname_chars[] = "/$:-_@?<>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
49 static const char * const TypeNames[TYPE_NBTYPES] =
51 "variable", /* TYPE_VARIABLE */
52 "pascal", /* TYPE_PASCAL */
53 "equate", /* TYPE_ABS */
54 "stub", /* TYPE_STUB */
55 "stdcall", /* TYPE_STDCALL */
56 "cdecl", /* TYPE_CDECL */
57 "varargs", /* TYPE_VARARGS */
58 "extern" /* TYPE_EXTERN */
61 static const char * const FlagNames[] =
63 "norelay", /* FLAG_NORELAY */
64 "noname", /* FLAG_NONAME */
65 "ret16", /* FLAG_RET16 */
66 "ret64", /* FLAG_RET64 */
67 "register", /* FLAG_REGISTER */
68 "private", /* FLAG_PRIVATE */
69 "ordinal", /* FLAG_ORDINAL */
70 "thiscall", /* FLAG_THISCALL */
71 "fastcall", /* FLAG_FASTCALL */
72 "syscall", /* FLAG_SYSCALL */
73 "import", /* FLAG_IMPORT */
74 NULL
77 static const char * const ArgNames[ARG_MAXARG + 1] =
79 "word", /* ARG_WORD */
80 "s_word", /* ARG_SWORD */
81 "segptr", /* ARG_SEGPTR */
82 "segstr", /* ARG_SEGSTR */
83 "long", /* ARG_LONG */
84 "ptr", /* ARG_PTR */
85 "str", /* ARG_STR */
86 "wstr", /* ARG_WSTR */
87 "int64", /* ARG_INT64 */
88 "int128", /* ARG_INT128 */
89 "float", /* ARG_FLOAT */
90 "double" /* ARG_DOUBLE */
93 static int IsNumberString(const char *s)
95 while (*s) if (!isdigit(*s++)) return 0;
96 return 1;
99 static inline int is_token_separator( char ch )
101 return strchr( separator_chars, ch ) != NULL;
104 static inline int is_token_comment( char ch )
106 return strchr( comment_chars, ch ) != NULL;
109 /* get the next line from the input file, or return 0 if at eof */
110 static int get_next_line(void)
112 ParseNext = ParseBuffer;
113 current_line++;
114 return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
117 static const char * GetToken( int allow_eol )
119 char *p;
120 char *token = TokenBuffer;
122 for (;;)
124 /* remove initial white space */
125 p = ParseNext;
126 while (isspace(*p)) p++;
128 if (*p == '\\' && p[1] == '\n') /* line continuation */
130 if (!get_next_line())
132 if (!allow_eol) error( "Unexpected end of file\n" );
133 return NULL;
136 else break;
139 if ((*p == '\0') || is_token_comment(*p))
141 if (!allow_eol) error( "Declaration not terminated properly\n" );
142 return NULL;
146 * Find end of token.
148 if (is_token_separator(*p))
150 /* a separator is always a complete token */
151 *token++ = *p++;
153 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
155 if (*p == '\\') p++;
156 if (*p) *token++ = *p++;
158 *token = '\0';
159 ParseNext = p;
160 return TokenBuffer;
164 static ORDDEF *add_entry_point( DLLSPEC *spec )
166 ORDDEF *ret;
168 if (spec->nb_entry_points == spec->alloc_entry_points)
170 spec->alloc_entry_points += 128;
171 spec->entry_points = xrealloc( spec->entry_points,
172 spec->alloc_entry_points * sizeof(*spec->entry_points) );
174 ret = &spec->entry_points[spec->nb_entry_points++];
175 memset( ret, 0, sizeof(*ret) );
176 return ret;
179 /*******************************************************************
180 * parse_spec_variable
182 * Parse a variable definition in a .spec file.
184 static int parse_spec_variable( ORDDEF *odp, DLLSPEC *spec )
186 char *endptr;
187 unsigned int *value_array;
188 int n_values;
189 int value_array_size;
190 const char *token;
192 if (spec->type == SPEC_WIN32)
194 error( "'variable' not supported in Win32, use 'extern' instead\n" );
195 return 0;
198 if (!(token = GetToken(0))) return 0;
199 if (*token != '(')
201 error( "Expected '(' got '%s'\n", token );
202 return 0;
205 n_values = 0;
206 value_array_size = 25;
207 value_array = xmalloc(sizeof(*value_array) * value_array_size);
209 for (;;)
211 if (!(token = GetToken(0)))
213 free( value_array );
214 return 0;
216 if (*token == ')')
217 break;
219 value_array[n_values++] = strtoul(token, &endptr, 0);
220 if (n_values == value_array_size)
222 value_array_size += 25;
223 value_array = xrealloc(value_array,
224 sizeof(*value_array) * value_array_size);
227 if (endptr == NULL || *endptr != '\0')
229 error( "Expected number value, got '%s'\n", token );
230 free( value_array );
231 return 0;
235 odp->u.var.n_values = n_values;
236 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
237 return 1;
241 /*******************************************************************
242 * parse_spec_arguments
244 * Parse the arguments of an entry point.
246 static int parse_spec_arguments( ORDDEF *odp, DLLSPEC *spec, int optional )
248 const char *token;
249 unsigned int i, arg;
250 int is_win32 = (spec->type == SPEC_WIN32) || (odp->flags & FLAG_EXPORT32);
252 if (!(token = GetToken( optional ))) return optional;
253 if (*token != '(')
255 error( "Expected '(' got '%s'\n", token );
256 return 0;
259 odp->u.func.nb_args = 0;
260 for (i = 0; i < MAX_ARGUMENTS; i++)
262 if (!(token = GetToken(0))) return 0;
263 if (*token == ')')
264 break;
266 for (arg = 0; arg <= ARG_MAXARG; arg++)
267 if (!strcmp( ArgNames[arg], token )) break;
269 if (arg > ARG_MAXARG)
271 error( "Unknown argument type '%s'\n", token );
272 return 0;
274 switch (arg)
276 case ARG_WORD:
277 case ARG_SWORD:
278 case ARG_SEGPTR:
279 case ARG_SEGSTR:
280 if (!is_win32) break;
281 error( "Argument type '%s' only allowed for Win16\n", token );
282 return 0;
283 case ARG_FLOAT:
284 case ARG_DOUBLE:
285 if (!(odp->flags & FLAG_SYSCALL)) break;
286 error( "Argument type '%s' not allowed for syscall function\n", token );
287 return 0;
289 odp->u.func.args[i] = arg;
291 if (*token != ')')
293 error( "Too many arguments\n" );
294 return 0;
297 odp->u.func.nb_args = i;
298 if (odp->flags & FLAG_THISCALL)
300 if (odp->type != TYPE_STDCALL)
302 error( "A thiscall function must use the stdcall convention\n" );
303 return 0;
305 if (!i || odp->u.func.args[0] != ARG_PTR)
307 error( "First argument of a thiscall function must be a pointer\n" );
308 return 0;
311 if (odp->flags & FLAG_FASTCALL)
313 if (odp->type != TYPE_STDCALL)
315 error( "A fastcall function must use the stdcall convention\n" );
316 return 0;
318 if ((i && odp->u.func.args[0] != ARG_PTR && odp->u.func.args[0] != ARG_LONG) ||
319 (i > 1 && odp->u.func.args[1] != ARG_PTR && odp->u.func.args[1] != ARG_LONG))
320 odp->flags |= FLAG_NORELAY; /* no relay debug possible for non-standard fastcall args */
322 if (odp->flags & FLAG_SYSCALL)
324 if (odp->type != TYPE_STDCALL)
326 error( "A syscall function must use the stdcall convention\n" );
327 return 0;
330 return 1;
334 /*******************************************************************
335 * parse_spec_export
337 * Parse an exported function definition in a .spec file.
339 static int parse_spec_export( ORDDEF *odp, DLLSPEC *spec )
341 const char *token;
342 int is_win32 = (spec->type == SPEC_WIN32) || (odp->flags & FLAG_EXPORT32);
344 if (!is_win32 && odp->type == TYPE_STDCALL)
346 error( "'stdcall' not supported for Win16\n" );
347 return 0;
349 if (is_win32 && odp->type == TYPE_PASCAL)
351 error( "'pascal' not supported for Win32\n" );
352 return 0;
355 if (!parse_spec_arguments( odp, spec, 0 )) return 0;
357 if (odp->type == TYPE_VARARGS)
358 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
360 if (target.cpu != CPU_i386)
361 odp->flags &= ~(FLAG_THISCALL | FLAG_FASTCALL);
363 if (!(token = GetToken(1)))
365 if (!strcmp( odp->name, "@" ))
367 error( "Missing handler name for anonymous function\n" );
368 return 0;
370 odp->link_name = xstrdup( odp->name );
372 else
374 odp->link_name = xstrdup( token );
375 if (strchr( odp->link_name, '.' ))
377 if (!is_win32)
379 error( "Forwarded functions not supported for Win16\n" );
380 return 0;
382 odp->flags |= FLAG_FORWARD;
385 return 1;
389 /*******************************************************************
390 * parse_spec_equate
392 * Parse an 'equate' definition in a .spec file.
394 static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
396 char *endptr;
397 int value;
398 const char *token;
400 if (spec->type == SPEC_WIN32)
402 error( "'equate' not supported for Win32\n" );
403 return 0;
405 if (!(token = GetToken(0))) return 0;
406 value = strtol(token, &endptr, 0);
407 if (endptr == NULL || *endptr != '\0')
409 error( "Expected number value, got '%s'\n", token );
410 return 0;
412 if (value < -0x8000 || value > 0xffff)
414 error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value );
415 value = 0;
417 odp->u.abs.value = value;
418 return 1;
422 /*******************************************************************
423 * parse_spec_stub
425 * Parse a 'stub' definition in a .spec file
427 static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
429 odp->u.func.nb_args = -1;
430 odp->link_name = xstrdup("");
432 return parse_spec_arguments( odp, spec, 1 );
436 /*******************************************************************
437 * parse_spec_extern
439 * Parse an 'extern' definition in a .spec file.
441 static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
443 const char *token;
445 if (spec->type == SPEC_WIN16)
447 error( "'extern' not supported for Win16, use 'variable' instead\n" );
448 return 0;
450 if (!(token = GetToken(1)))
452 if (!strcmp( odp->name, "@" ))
454 error( "Missing handler name for anonymous extern\n" );
455 return 0;
457 odp->link_name = xstrdup( odp->name );
459 else
461 odp->link_name = xstrdup( token );
462 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
464 return 1;
468 /*******************************************************************
469 * parse_spec_flags
471 * Parse the optional flags for an entry point in a .spec file.
473 static const char *parse_spec_flags( DLLSPEC *spec, ORDDEF *odp, const char *token )
475 unsigned int i, cpu_mask = 0;
479 token++;
480 if (!strncmp( token, "arch=", 5))
482 char *args = xstrdup( token + 5 );
483 char *cpu_name = strtok( args, "," );
484 while (cpu_name)
486 if (!strcmp( cpu_name, "win32" ))
488 if (spec->type == SPEC_WIN32)
489 odp->flags |= FLAG_CPU_WIN32;
490 else
491 odp->flags |= FLAG_EXPORT32;
493 else if (!strcmp( cpu_name, "win64" ))
494 odp->flags |= FLAG_CPU_WIN64;
495 else
497 int cpu = get_cpu_from_name( cpu_name + (cpu_name[0] == '!') );
498 if (cpu == -1)
500 error( "Unknown architecture '%s'\n", cpu_name );
501 return NULL;
503 if (cpu_name[0] == '!') cpu_mask |= FLAG_CPU( cpu );
504 else odp->flags |= FLAG_CPU( cpu );
506 cpu_name = strtok( NULL, "," );
508 free( args );
510 else if (!strcmp( token, "i386" )) /* backwards compatibility */
512 odp->flags |= FLAG_CPU(CPU_i386);
514 else
516 for (i = 0; FlagNames[i]; i++)
517 if (!strcmp( FlagNames[i], token )) break;
518 if (!FlagNames[i])
520 error( "Unknown flag '%s'\n", token );
521 return NULL;
523 switch (1 << i)
525 case FLAG_RET16:
526 case FLAG_REGISTER:
527 if (spec->type == SPEC_WIN32)
528 error( "Flag '%s' is not supported in Win32\n", FlagNames[i] );
529 break;
530 case FLAG_RET64:
531 case FLAG_THISCALL:
532 case FLAG_FASTCALL:
533 if (spec->type == SPEC_WIN16)
534 error( "Flag '%s' is not supported in Win16\n", FlagNames[i] );
535 break;
537 odp->flags |= 1 << i;
539 token = GetToken(0);
540 } while (token && *token == '-');
542 if (cpu_mask) odp->flags |= FLAG_CPU_MASK & ~cpu_mask;
543 return token;
547 /*******************************************************************
548 * parse_spec_ordinal
550 * Parse an ordinal definition in a .spec file.
552 static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
554 const char *token;
555 size_t len;
556 ORDDEF *odp = add_entry_point( spec );
558 if (!(token = GetToken(0))) goto error;
560 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
561 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
562 break;
564 if (odp->type >= TYPE_NBTYPES)
566 if (!strcmp( token, "thiscall" )) /* for backwards compatibility */
568 odp->type = TYPE_STDCALL;
569 odp->flags |= FLAG_THISCALL;
571 else
573 error( "Expected type after ordinal, found '%s' instead\n", token );
574 goto error;
578 if (!(token = GetToken(0))) goto error;
579 if (*token == '-' && !(token = parse_spec_flags( spec, odp, token ))) goto error;
581 if (ordinal == -1 && spec->type != SPEC_WIN32 && !(odp->flags & FLAG_EXPORT32))
583 error( "'@' ordinals not supported for Win16\n" );
584 goto error;
587 odp->name = xstrdup( token );
588 odp->lineno = current_line;
589 odp->ordinal = ordinal;
591 len = strspn( odp->name, valid_ordname_chars );
592 if (len < strlen( odp->name ))
594 error( "Character '%c' is not allowed in exported name '%s'\n", odp->name[len], odp->name );
595 goto error;
598 switch(odp->type)
600 case TYPE_VARIABLE:
601 if (!parse_spec_variable( odp, spec )) goto error;
602 break;
603 case TYPE_PASCAL:
604 case TYPE_STDCALL:
605 case TYPE_VARARGS:
606 case TYPE_CDECL:
607 if (!parse_spec_export( odp, spec )) goto error;
608 break;
609 case TYPE_ABS:
610 if (!parse_spec_equate( odp, spec )) goto error;
611 break;
612 case TYPE_STUB:
613 if (!parse_spec_stub( odp, spec )) goto error;
614 break;
615 case TYPE_EXTERN:
616 if (!parse_spec_extern( odp, spec )) goto error;
617 break;
618 default:
619 assert( 0 );
622 if ((odp->flags & FLAG_CPU_MASK) && !(odp->flags & FLAG_CPU(target.cpu)))
624 /* ignore this entry point */
625 spec->nb_entry_points--;
626 return 1;
629 if (data_only && !(odp->flags & FLAG_FORWARD))
631 error( "Only forwarded entry points are allowed in data-only mode\n" );
632 goto error;
635 if (ordinal != -1)
637 if (!ordinal)
639 error( "Ordinal 0 is not valid\n" );
640 goto error;
642 if (ordinal >= MAX_ORDINALS)
644 error( "Ordinal number %d too large\n", ordinal );
645 goto error;
647 if (ordinal > spec->limit) spec->limit = ordinal;
648 if (ordinal < spec->base) spec->base = ordinal;
649 odp->ordinal = ordinal;
652 if (odp->type == TYPE_STDCALL && !(odp->flags & FLAG_PRIVATE))
654 if (!strcmp( odp->name, "DllRegisterServer" ) ||
655 !strcmp( odp->name, "DllUnregisterServer" ) ||
656 !strcmp( odp->name, "DllMain" ) ||
657 !strcmp( odp->name, "DllGetClassObject" ) ||
658 !strcmp( odp->name, "DllGetVersion" ) ||
659 !strcmp( odp->name, "DllInstall" ) ||
660 !strcmp( odp->name, "DllCanUnloadNow" ))
662 warning( "Function %s should be marked private\n", odp->name );
663 if (strcmp( odp->name, odp->link_name ))
664 warning( "Function %s should not use a different internal name (%s)\n",
665 odp->name, odp->link_name );
669 if (!strcmp( odp->name, "@" ) || odp->flags & (FLAG_NONAME | FLAG_ORDINAL))
671 if (ordinal == -1)
673 if (!strcmp( odp->name, "@" ))
674 error( "Nameless function needs an explicit ordinal number\n" );
675 else
676 error( "Function imported by ordinal needs an explicit ordinal number\n" );
677 goto error;
679 if (spec->type != SPEC_WIN32)
681 error( "Nameless functions not supported for Win16\n" );
682 goto error;
684 if (!strcmp( odp->name, "@" ))
686 free( odp->name );
687 odp->name = NULL;
689 else if (!(odp->flags & FLAG_ORDINAL)) /* -ordinal only affects the import library */
691 odp->export_name = odp->name;
692 odp->name = NULL;
695 return 1;
697 error:
698 spec->nb_entry_points--;
699 free( odp->name );
700 return 0;
704 static unsigned int apiset_hash_len( const char *str )
706 return strrchr( str, '-' ) - str;
709 static unsigned int apiset_hash( const char *str )
711 unsigned int ret = 0, len = apiset_hash_len( str );
712 while (len--) ret = ret * apiset_hash_factor + *str++;
713 return ret;
716 static unsigned int apiset_add_str( struct apiset *apiset, const char *str, unsigned int len )
718 char *ret;
720 if (!apiset->strings || !(ret = strstr( apiset->strings, str )))
722 if (apiset->str_pos + len >= apiset->str_size)
724 apiset->str_size = max( apiset->str_size * 2, 1024 );
725 apiset->strings = xrealloc( apiset->strings, apiset->str_size );
727 ret = apiset->strings + apiset->str_pos;
728 memcpy( ret, str, len );
729 ret[len] = 0;
730 apiset->str_pos += len;
732 return ret - apiset->strings;
735 static void add_apiset( struct apiset *apiset, const char *api )
737 struct apiset_entry *entry;
739 if (apiset->count == apiset->size)
741 apiset->size = max( apiset->size * 2, 64 );
742 apiset->entries = xrealloc( apiset->entries, apiset->size * sizeof(*apiset->entries) );
744 entry = &apiset->entries[apiset->count++];
745 entry->name_len = strlen( api );
746 entry->name_off = apiset_add_str( apiset, api, entry->name_len );
747 entry->hash = apiset_hash( api );
748 entry->hash_len = apiset_hash_len( api );
749 entry->val_count = 0;
752 static void add_apiset_value( struct apiset *apiset, const char *value )
754 struct apiset_entry *entry = &apiset->entries[apiset->count - 1];
756 if (entry->val_count < ARRAY_SIZE(entry->values) - 1)
758 struct apiset_value *val = &entry->values[entry->val_count++];
759 char *sep = strchr( value, ':' );
761 if (sep)
763 val->name_len = sep - value;
764 val->name_off = apiset_add_str( apiset, value, val->name_len );
765 val->val_len = strlen( sep + 1 );
766 val->val_off = apiset_add_str( apiset, sep + 1, val->val_len );
768 else
770 val->name_len = val->name_off = 0;
771 val->val_len = strlen( value );
772 val->val_off = apiset_add_str( apiset, value, val->val_len );
775 else error( "Too many values for api '%.*s'\n", entry->name_len, apiset->strings + entry->name_off );
778 /*******************************************************************
779 * parse_spec_apiset
781 static int parse_spec_apiset( DLLSPEC *spec )
783 struct apiset_entry *entry;
784 const char *token;
785 unsigned int i, hash;
787 if (!data_only)
789 error( "Apiset definitions are only allowed in data-only mode\n" );
790 return 0;
793 if (!(token = GetToken(0))) return 0;
795 if (!strncmp( token, "api-", 4 ) && !strncmp( token, "ext-", 4 ))
797 error( "Unrecognized API set name '%s'\n", token );
798 return 0;
801 hash = apiset_hash( token );
802 for (i = 0, entry = spec->apiset.entries; i < spec->apiset.count; i++, entry++)
804 if (entry->name_len == strlen( token ) &&
805 !strncmp( spec->apiset.strings + entry->name_off, token, entry->name_len ))
807 error( "Duplicate API set '%s'\n", token );
808 return 0;
810 if (entry->hash == hash)
812 error( "Duplicate hash code '%.*s' and '%s'\n",
813 entry->name_len, spec->apiset.strings + entry->name_off, token );
814 return 0;
817 add_apiset( &spec->apiset, token );
819 if (!(token = GetToken(0)) || strcmp( token, "=" ))
821 error( "Syntax error near '%s'\n", token );
822 return 0;
825 while ((token = GetToken(1))) add_apiset_value( &spec->apiset, token );
826 return 1;
830 static int name_compare( const void *ptr1, const void *ptr2 )
832 const ORDDEF *odp1 = *(const ORDDEF * const *)ptr1;
833 const ORDDEF *odp2 = *(const ORDDEF * const *)ptr2;
834 const char *name1 = odp1->name ? odp1->name : odp1->export_name;
835 const char *name2 = odp2->name ? odp2->name : odp2->export_name;
836 return strcmp( name1, name2 );
839 /*******************************************************************
840 * assign_names
842 * Build the name array and catch duplicates.
844 static void assign_names( DLLSPEC *spec )
846 int i, j, nb_exp_names = 0;
847 ORDDEF **all_names;
849 spec->nb_names = 0;
850 for (i = 0; i < spec->nb_entry_points; i++)
851 if (spec->entry_points[i].name) spec->nb_names++;
852 else if (spec->entry_points[i].export_name) nb_exp_names++;
854 if (!spec->nb_names && !nb_exp_names) return;
856 /* check for duplicates */
858 all_names = xmalloc( (spec->nb_names + nb_exp_names) * sizeof(all_names[0]) );
859 for (i = j = 0; i < spec->nb_entry_points; i++)
860 if (spec->entry_points[i].name || spec->entry_points[i].export_name)
861 all_names[j++] = &spec->entry_points[i];
863 qsort( all_names, j, sizeof(all_names[0]), name_compare );
865 for (i = 0; i < j - 1; i++)
867 const char *name1 = all_names[i]->name ? all_names[i]->name : all_names[i]->export_name;
868 const char *name2 = all_names[i+1]->name ? all_names[i+1]->name : all_names[i+1]->export_name;
869 if (!strcmp( name1, name2 ) &&
870 !((all_names[i]->flags ^ all_names[i+1]->flags) & FLAG_EXPORT32))
872 current_line = max( all_names[i]->lineno, all_names[i+1]->lineno );
873 error( "'%s' redefined\n%s:%d: First defined here\n",
874 name1, input_file_name,
875 min( all_names[i]->lineno, all_names[i+1]->lineno ) );
878 free( all_names );
880 if (spec->nb_names)
882 spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
883 for (i = j = 0; i < spec->nb_entry_points; i++)
884 if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
886 /* sort the list of names */
887 qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
888 for (i = 0; i < spec->nb_names; i++) spec->names[i]->hint = i;
892 /*******************************************************************
893 * assign_ordinals
895 * Build the ordinal array.
897 static void assign_ordinals( DLLSPEC *spec )
899 int i, count, ordinal;
901 /* start assigning from base, or from 1 if no ordinal defined yet */
903 spec->base = MAX_ORDINALS;
904 spec->limit = 0;
905 for (i = 0; i < spec->nb_entry_points; i++)
907 ordinal = spec->entry_points[i].ordinal;
908 if (ordinal == -1) continue;
909 if (ordinal > spec->limit) spec->limit = ordinal;
910 if (ordinal < spec->base) spec->base = ordinal;
912 if (spec->base == MAX_ORDINALS) spec->base = 1;
913 if (spec->limit < spec->base) spec->limit = spec->base;
915 count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
916 spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
917 memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
919 /* fill in all explicitly specified ordinals */
920 for (i = 0; i < spec->nb_entry_points; i++)
922 ordinal = spec->entry_points[i].ordinal;
923 if (ordinal == -1) continue;
924 if (spec->ordinals[ordinal])
926 current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
927 error( "ordinal %d redefined\n%s:%d: First defined here\n",
928 ordinal, input_file_name,
929 min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
931 else spec->ordinals[ordinal] = &spec->entry_points[i];
934 /* now assign ordinals to the rest */
935 for (i = 0, ordinal = spec->base; i < spec->nb_entry_points; i++)
937 if (spec->entry_points[i].ordinal != -1) continue;
938 while (spec->ordinals[ordinal]) ordinal++;
939 if (ordinal >= MAX_ORDINALS)
941 current_line = spec->entry_points[i].lineno;
942 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
944 spec->entry_points[i].ordinal = ordinal;
945 spec->ordinals[ordinal] = &spec->entry_points[i];
947 if (ordinal > spec->limit) spec->limit = ordinal;
951 /*******************************************************************
952 * add_16bit_exports
954 * Add the necessary exports to the 32-bit counterpart of a 16-bit module.
956 void add_16bit_exports( DLLSPEC *spec32, DLLSPEC *spec16 )
958 int i;
959 ORDDEF *odp;
961 spec32->file_name = xstrdup( spec16->file_name );
962 spec32->characteristics = IMAGE_FILE_DLL;
963 spec32->init_func = xstrdup( "DllMain" );
965 /* add an export for the NE module */
967 odp = add_entry_point( spec32 );
968 odp->type = TYPE_EXTERN;
969 odp->flags = FLAG_PRIVATE;
970 odp->name = xstrdup( "__wine_spec_dos_header" );
971 odp->lineno = 0;
972 odp->ordinal = 1;
973 odp->link_name = xstrdup( ".L__wine_spec_dos_header" );
975 if (spec16->main_module)
977 odp = add_entry_point( spec32 );
978 odp->type = TYPE_EXTERN;
979 odp->flags = FLAG_PRIVATE;
980 odp->name = xstrdup( "__wine_spec_main_module" );
981 odp->lineno = 0;
982 odp->ordinal = 2;
983 odp->link_name = xstrdup( ".L__wine_spec_main_module" );
986 /* add the explicit win32 exports */
988 for (i = 1; i <= spec16->limit; i++)
990 ORDDEF *odp16 = spec16->ordinals[i];
992 if (!odp16 || !odp16->name) continue;
993 if (!(odp16->flags & FLAG_EXPORT32)) continue;
995 odp = add_entry_point( spec32 );
996 odp->flags = odp16->flags & ~FLAG_EXPORT32;
997 odp->type = odp16->type;
998 odp->name = xstrdup( odp16->name );
999 odp->lineno = odp16->lineno;
1000 odp->ordinal = -1;
1001 odp->link_name = xstrdup( odp16->link_name );
1002 odp->u.func.nb_args = odp16->u.func.nb_args;
1003 if (odp->u.func.nb_args > 0) memcpy( odp->u.func.args, odp16->u.func.args,
1004 odp->u.func.nb_args * sizeof(odp->u.func.args[0]) );
1007 assign_names( spec32 );
1008 assign_ordinals( spec32 );
1012 /*******************************************************************
1013 * parse_spec_file
1015 * Parse a .spec file.
1017 int parse_spec_file( FILE *file, DLLSPEC *spec )
1019 const char *token;
1021 input_file = file;
1022 current_line = 0;
1024 comment_chars = "#;";
1025 separator_chars = "()";
1027 while (get_next_line())
1029 if (!(token = GetToken(1))) continue;
1030 if (strcmp(token, "@") == 0)
1032 if (!parse_spec_ordinal( -1, spec )) continue;
1034 else if (IsNumberString(token))
1036 if (!parse_spec_ordinal( atoi(token), spec )) continue;
1038 else if (strcmp(token, "apiset") == 0)
1040 if (!parse_spec_apiset( spec )) continue;
1042 else
1044 error( "Expected ordinal declaration, got '%s'\n", token );
1045 continue;
1047 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
1050 current_line = 0; /* no longer parsing the input file */
1051 assign_names( spec );
1052 assign_ordinals( spec );
1053 return !nb_errors;
1057 /*******************************************************************
1058 * parse_def_library
1060 * Parse a LIBRARY declaration in a .def file.
1062 static int parse_def_library( DLLSPEC *spec )
1064 const char *token = GetToken(1);
1066 if (!token) return 1;
1067 if (strcmp( token, "BASE" ))
1069 free( spec->file_name );
1070 spec->file_name = xstrdup( token );
1071 if (!(token = GetToken(1))) return 1;
1073 if (strcmp( token, "BASE" ))
1075 error( "Expected library name or BASE= declaration, got '%s'\n", token );
1076 return 0;
1078 if (!(token = GetToken(0))) return 0;
1079 if (strcmp( token, "=" ))
1081 error( "Expected '=' after BASE, got '%s'\n", token );
1082 return 0;
1084 if (!(token = GetToken(0))) return 0;
1085 /* FIXME: do something with base address */
1087 return 1;
1091 /*******************************************************************
1092 * parse_def_stack_heap_size
1094 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
1096 static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
1098 const char *token = GetToken(0);
1099 char *end;
1100 unsigned long size;
1102 if (!token) return 0;
1103 size = strtoul( token, &end, 0 );
1104 if (*end)
1106 error( "Invalid number '%s'\n", token );
1107 return 0;
1109 if (is_stack) spec->stack_size = size / 1024;
1110 else spec->heap_size = size / 1024;
1111 if (!(token = GetToken(1))) return 1;
1112 if (strcmp( token, "," ))
1114 error( "Expected ',' after size, got '%s'\n", token );
1115 return 0;
1117 if (!(token = GetToken(0))) return 0;
1118 /* FIXME: do something with reserve size */
1119 return 1;
1123 /*******************************************************************
1124 * parse_def_export
1126 * Parse an export declaration in a .def file.
1128 static int parse_def_export( char *name, DLLSPEC *spec )
1130 int i, args;
1131 const char *token = GetToken(1);
1132 ORDDEF *odp = add_entry_point( spec );
1134 odp->lineno = current_line;
1135 odp->ordinal = -1;
1136 odp->name = name;
1137 args = remove_stdcall_decoration( odp->name );
1138 if (args == -1)
1140 odp->type = TYPE_CDECL;
1141 args = 0;
1143 else
1145 odp->type = TYPE_STDCALL;
1146 args /= get_ptr_size();
1147 if (args >= MAX_ARGUMENTS)
1149 error( "Too many arguments in stdcall function '%s'\n", odp->name );
1150 return 0;
1152 for (i = 0; i < args; i++) odp->u.func.args[i] = ARG_LONG;
1154 odp->u.func.nb_args = args;
1156 /* check for optional internal name */
1158 if (token && !strcmp( token, "=" ))
1160 if (!(token = GetToken(0))) goto error;
1161 odp->link_name = xstrdup( token );
1162 remove_stdcall_decoration( odp->link_name );
1163 token = GetToken(1);
1165 else
1167 odp->link_name = xstrdup( name );
1170 /* check for optional ordinal */
1172 if (token && token[0] == '@')
1174 int ordinal;
1176 if (!IsNumberString( token+1 ))
1178 error( "Expected number after '@', got '%s'\n", token+1 );
1179 goto error;
1181 ordinal = atoi( token+1 );
1182 if (!ordinal)
1184 error( "Ordinal 0 is not valid\n" );
1185 goto error;
1187 if (ordinal >= MAX_ORDINALS)
1189 error( "Ordinal number %d too large\n", ordinal );
1190 goto error;
1192 odp->ordinal = ordinal;
1193 token = GetToken(1);
1196 /* check for other optional keywords */
1198 while (token)
1200 if (!strcmp( token, "NONAME" ))
1202 if (odp->ordinal == -1)
1204 error( "NONAME requires an ordinal\n" );
1205 goto error;
1207 odp->export_name = odp->name;
1208 odp->name = NULL;
1209 odp->flags |= FLAG_NONAME;
1211 else if (!strcmp( token, "PRIVATE" ))
1213 odp->flags |= FLAG_PRIVATE;
1215 else if (!strcmp( token, "DATA" ))
1217 odp->type = TYPE_EXTERN;
1219 else
1221 error( "Garbage text '%s' found at end of export declaration\n", token );
1222 goto error;
1224 token = GetToken(1);
1226 return 1;
1228 error:
1229 spec->nb_entry_points--;
1230 free( odp->name );
1231 return 0;
1235 /*******************************************************************
1236 * parse_def_file
1238 * Parse a .def file.
1240 int parse_def_file( FILE *file, DLLSPEC *spec )
1242 const char *token;
1243 int in_exports = 0;
1245 input_file = file;
1246 current_line = 0;
1248 comment_chars = ";";
1249 separator_chars = ",=";
1251 while (get_next_line())
1253 if (!(token = GetToken(1))) continue;
1255 if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
1257 if (!parse_def_library( spec )) continue;
1258 goto end_of_line;
1260 else if (!strcmp( token, "STACKSIZE" ))
1262 if (!parse_def_stack_heap_size( 1, spec )) continue;
1263 goto end_of_line;
1265 else if (!strcmp( token, "HEAPSIZE" ))
1267 if (!parse_def_stack_heap_size( 0, spec )) continue;
1268 goto end_of_line;
1270 else if (!strcmp( token, "EXPORTS" ))
1272 in_exports = 1;
1273 if (!(token = GetToken(1))) continue;
1275 else if (!strcmp( token, "IMPORTS" ))
1277 in_exports = 0;
1278 if (!(token = GetToken(1))) continue;
1280 else if (!strcmp( token, "SECTIONS" ))
1282 in_exports = 0;
1283 if (!(token = GetToken(1))) continue;
1286 if (!in_exports) continue; /* ignore this line */
1287 if (!parse_def_export( xstrdup(token), spec )) continue;
1289 end_of_line:
1290 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
1293 current_line = 0; /* no longer parsing the input file */
1294 assign_names( spec );
1295 assign_ordinals( spec );
1296 return !nb_errors;