PageRenderTime 98ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/widl/parser.y

https://bitbucket.org/arty/arty-newcc-reactos
Happy | 2827 lines | 2542 code | 285 blank | 0 comment | 0 complexity | c34bb2d6da8d2f13311d3fed158711cc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-3.0, CC-BY-SA-3.0, AGPL-3.0, GPL-3.0, CPL-1.0
  1. %{
  2. /*
  3. * IDL Compiler
  4. *
  5. * Copyright 2002 Ove Kaaven
  6. * Copyright 2006-2008 Robert Shearman
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #include "config.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. #include <assert.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include "widl.h"
  30. #include "utils.h"
  31. #include "parser.h"
  32. #include "header.h"
  33. #include "typelib.h"
  34. #include "typegen.h"
  35. #include "expr.h"
  36. #include "typetree.h"
  37. #if defined(YYBYACC)
  38. /* Berkeley yacc (byacc) doesn't seem to know about these */
  39. /* Some *BSD supplied versions do define these though */
  40. # ifndef YYEMPTY
  41. # define YYEMPTY (-1) /* Empty lookahead value of yychar */
  42. # endif
  43. # ifndef YYLEX
  44. # define YYLEX yylex()
  45. # endif
  46. #elif defined(YYBISON)
  47. /* Bison was used for original development */
  48. /* #define YYEMPTY -2 */
  49. /* #define YYLEX yylex() */
  50. #else
  51. /* No yacc we know yet */
  52. # if !defined(YYEMPTY) || !defined(YYLEX)
  53. # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
  54. # elif defined(__GNUC__) /* gcc defines the #warning directive */
  55. # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
  56. /* #else we just take a chance that it works... */
  57. # endif
  58. #endif
  59. #define YYERROR_VERBOSE
  60. static unsigned char pointer_default = RPC_FC_UP;
  61. typedef struct list typelist_t;
  62. struct typenode {
  63. type_t *type;
  64. struct list entry;
  65. };
  66. struct _import_t
  67. {
  68. char *name;
  69. int import_performed;
  70. };
  71. typedef struct _decl_spec_t
  72. {
  73. type_t *type;
  74. attr_list_t *attrs;
  75. enum storage_class stgclass;
  76. } decl_spec_t;
  77. typelist_t incomplete_types = LIST_INIT(incomplete_types);
  78. static void fix_incomplete(void);
  79. static void fix_incomplete_types(type_t *complete_type);
  80. static str_list_t *append_str(str_list_t *list, char *str);
  81. static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
  82. static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list);
  83. static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right, attr_t *attr, enum storage_class stgclass);
  84. static attr_t *make_attr(enum attr_type type);
  85. static attr_t *make_attrv(enum attr_type type, unsigned int val);
  86. static attr_t *make_attrp(enum attr_type type, void *val);
  87. static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
  88. static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
  89. static var_t *declare_var(attr_list_t *attrs, decl_spec_t *decl_spec, const declarator_t *decl, int top);
  90. static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls);
  91. static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
  92. static ifref_t *make_ifref(type_t *iface);
  93. static var_list_t *append_var_list(var_list_t *list, var_list_t *vars);
  94. static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *p);
  95. static declarator_t *make_declarator(var_t *var);
  96. static type_t *make_safearray(type_t *type);
  97. static typelib_t *make_library(const char *name, const attr_list_t *attrs);
  98. static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type);
  99. static type_t *reg_typedefs(decl_spec_t *decl_spec, var_list_t *names, attr_list_t *attrs);
  100. static type_t *find_type_or_error(const char *name, int t);
  101. static type_t *find_type_or_error2(char *name, int t);
  102. static var_t *reg_const(var_t *var);
  103. static char *gen_name(void);
  104. static void check_arg_attrs(const var_t *arg);
  105. static void check_statements(const statement_list_t *stmts, int is_inside_library);
  106. static void check_all_user_types(const statement_list_t *stmts);
  107. static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs);
  108. static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs);
  109. static attr_list_t *check_typedef_attrs(attr_list_t *attrs);
  110. static attr_list_t *check_enum_attrs(attr_list_t *attrs);
  111. static attr_list_t *check_struct_attrs(attr_list_t *attrs);
  112. static attr_list_t *check_union_attrs(attr_list_t *attrs);
  113. static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs);
  114. static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs);
  115. static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
  116. static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs);
  117. static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs);
  118. const char *get_attr_display_name(enum attr_type type);
  119. static void add_explicit_handle_if_necessary(const type_t *iface, var_t *func);
  120. static void check_def(const type_t *t);
  121. static statement_t *make_statement(enum statement_type type);
  122. static statement_t *make_statement_type_decl(type_t *type);
  123. static statement_t *make_statement_reference(type_t *type);
  124. static statement_t *make_statement_declaration(var_t *var);
  125. static statement_t *make_statement_library(typelib_t *typelib);
  126. static statement_t *make_statement_cppquote(const char *str);
  127. static statement_t *make_statement_importlib(const char *str);
  128. static statement_t *make_statement_module(type_t *type);
  129. static statement_t *make_statement_typedef(var_list_t *names);
  130. static statement_t *make_statement_import(const char *str);
  131. static statement_t *make_statement_typedef(var_list_t *names);
  132. static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt);
  133. %}
  134. %union {
  135. attr_t *attr;
  136. attr_list_t *attr_list;
  137. str_list_t *str_list;
  138. expr_t *expr;
  139. expr_list_t *expr_list;
  140. array_dims_t *array_dims;
  141. type_t *type;
  142. var_t *var;
  143. var_list_t *var_list;
  144. declarator_t *declarator;
  145. declarator_list_t *declarator_list;
  146. statement_t *statement;
  147. statement_list_t *stmt_list;
  148. ifref_t *ifref;
  149. ifref_list_t *ifref_list;
  150. char *str;
  151. UUID *uuid;
  152. unsigned int num;
  153. double dbl;
  154. interface_info_t ifinfo;
  155. typelib_t *typelib;
  156. struct _import_t *import;
  157. struct _decl_spec_t *declspec;
  158. enum storage_class stgclass;
  159. }
  160. %token <str> aIDENTIFIER
  161. %token <str> aKNOWNTYPE
  162. %token <num> aNUM aHEXNUM
  163. %token <dbl> aDOUBLE
  164. %token <str> aSTRING aWSTRING aSQSTRING
  165. %token <uuid> aUUID
  166. %token aEOF
  167. %token SHL SHR
  168. %token MEMBERPTR
  169. %token EQUALITY INEQUALITY
  170. %token GREATEREQUAL LESSEQUAL
  171. %token LOGICALOR LOGICALAND
  172. %token ELLIPSIS
  173. %token tAGGREGATABLE tALLOCATE tANNOTATION tAPPOBJECT tASYNC tASYNCUUID
  174. %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
  175. %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
  176. %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
  177. %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
  178. %token tDECODE tDEFAULT tDEFAULTBIND
  179. %token tDEFAULTCOLLELEM
  180. %token tDEFAULTVALUE
  181. %token tDEFAULTVTABLE
  182. %token tDISABLECONSISTENCYCHECK tDISPLAYBIND
  183. %token tDISPINTERFACE
  184. %token tDLLNAME tDOUBLE tDUAL
  185. %token tENABLEALLOCATE tENCODE tENDPOINT
  186. %token tENTRY tENUM tERRORSTATUST
  187. %token tEXPLICITHANDLE tEXTERN
  188. %token tFALSE
  189. %token tFASTCALL tFAULTSTATUS
  190. %token tFLOAT tFORCEALLOCATE
  191. %token tHANDLE
  192. %token tHANDLET
  193. %token tHELPCONTEXT tHELPFILE
  194. %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
  195. %token tHIDDEN
  196. %token tHYPER tID tIDEMPOTENT
  197. %token tIGNORE tIIDIS
  198. %token tIMMEDIATEBIND
  199. %token tIMPLICITHANDLE
  200. %token tIMPORT tIMPORTLIB
  201. %token tIN tIN_LINE tINLINE
  202. %token tINPUTSYNC
  203. %token tINT tINT3264 tINT64
  204. %token tINTERFACE
  205. %token tLCID
  206. %token tLENGTHIS tLIBRARY
  207. %token tLICENSED tLOCAL
  208. %token tLONG
  209. %token tMAYBE tMESSAGE
  210. %token tMETHODS
  211. %token tMODULE
  212. %token tNOCODE tNONBROWSABLE
  213. %token tNONCREATABLE
  214. %token tNONEXTENSIBLE
  215. %token tNOTIFY tNOTIFYFLAG
  216. %token tNULL
  217. %token tOBJECT tODL tOLEAUTOMATION
  218. %token tOPTIMIZE tOPTIONAL
  219. %token tOUT
  220. %token tPARTIALIGNORE tPASCAL
  221. %token tPOINTERDEFAULT
  222. %token tPROGID tPROPERTIES
  223. %token tPROPGET tPROPPUT tPROPPUTREF
  224. %token tPROXY tPTR
  225. %token tPUBLIC
  226. %token tRANGE
  227. %token tREADONLY tREF
  228. %token tREGISTER tREPRESENTAS
  229. %token tREQUESTEDIT
  230. %token tRESTRICTED
  231. %token tRETVAL
  232. %token tSAFEARRAY
  233. %token tSHORT
  234. %token tSIGNED
  235. %token tSIZEIS tSIZEOF
  236. %token tSMALL
  237. %token tSOURCE
  238. %token tSTATIC
  239. %token tSTDCALL
  240. %token tSTRICTCONTEXTHANDLE
  241. %token tSTRING tSTRUCT
  242. %token tSWITCH tSWITCHIS tSWITCHTYPE
  243. %token tTHREADING tTRANSMITAS
  244. %token tTRUE
  245. %token tTYPEDEF
  246. %token tUIDEFAULT tUNION
  247. %token tUNIQUE
  248. %token tUNSIGNED
  249. %token tUSESGETLASTERROR tUSERMARSHAL tUUID
  250. %token tV1ENUM
  251. %token tVARARG
  252. %token tVERSION tVIPROGID
  253. %token tVOID
  254. %token tWCHAR tWIREMARSHAL
  255. %token tAPARTMENT tNEUTRAL tSINGLE tFREE tBOTH
  256. %type <attr> attribute type_qualifier function_specifier
  257. %type <attr_list> m_attributes attributes attrib_list m_type_qual_list
  258. %type <str_list> str_list
  259. %type <expr> m_expr expr expr_const expr_int_const array m_bitfield
  260. %type <expr_list> m_exprs /* exprs expr_list */ expr_list_int_const
  261. %type <ifinfo> interfacehdr
  262. %type <stgclass> storage_cls_spec
  263. %type <declspec> decl_spec decl_spec_no_type m_decl_spec_no_type
  264. %type <type> inherit interface interfacedef interfacedec
  265. %type <type> dispinterface dispinterfacehdr dispinterfacedef
  266. %type <type> module modulehdr moduledef
  267. %type <type> base_type int_std
  268. %type <type> enumdef structdef uniondef typedecl
  269. %type <type> type
  270. %type <ifref> coclass_int
  271. %type <ifref_list> coclass_ints
  272. %type <var> arg ne_union_field union_field s_field case enum declaration
  273. %type <var> funcdef
  274. %type <var_list> m_args arg_list args dispint_meths
  275. %type <var_list> fields ne_union_fields cases enums enum_list dispint_props field
  276. %type <var> m_ident ident
  277. %type <declarator> declarator direct_declarator init_declarator struct_declarator
  278. %type <declarator> m_any_declarator any_declarator any_declarator_no_direct any_direct_declarator
  279. %type <declarator> m_abstract_declarator abstract_declarator abstract_declarator_no_direct abstract_direct_declarator
  280. %type <declarator_list> declarator_list struct_declarator_list
  281. %type <type> coclass coclasshdr coclassdef
  282. %type <num> pointer_type threading_type version
  283. %type <str> libraryhdr callconv cppquote importlib import t_ident
  284. %type <uuid> uuid_string
  285. %type <import> import_start
  286. %type <typelib> library_start librarydef
  287. %type <statement> statement typedef
  288. %type <stmt_list> gbl_statements imp_statements int_statements
  289. %left ','
  290. %right '?' ':'
  291. %left LOGICALOR
  292. %left LOGICALAND
  293. %left '|'
  294. %left '^'
  295. %left '&'
  296. %left EQUALITY INEQUALITY
  297. %left '<' '>' LESSEQUAL GREATEREQUAL
  298. %left SHL SHR
  299. %left '-' '+'
  300. %left '*' '/' '%'
  301. %right '!' '~' CAST PPTR POS NEG ADDRESSOF tSIZEOF
  302. %left '.' MEMBERPTR '[' ']'
  303. %%
  304. input: gbl_statements { fix_incomplete();
  305. check_statements($1, FALSE);
  306. check_all_user_types($1);
  307. write_header($1);
  308. write_id_data($1);
  309. write_proxies($1);
  310. write_client($1);
  311. write_server($1);
  312. write_regscript($1);
  313. write_dlldata($1);
  314. write_local_stubs($1);
  315. }
  316. ;
  317. gbl_statements: { $$ = NULL; }
  318. | gbl_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); }
  319. | gbl_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
  320. | gbl_statements coclass ';' { $$ = $1;
  321. reg_type($2, $2->name, 0);
  322. }
  323. | gbl_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
  324. reg_type($2, $2->name, 0);
  325. }
  326. | gbl_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
  327. | gbl_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
  328. | gbl_statements statement { $$ = append_statement($1, $2); }
  329. ;
  330. imp_statements: { $$ = NULL; }
  331. | imp_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); }
  332. | imp_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
  333. | imp_statements coclass ';' { $$ = $1; reg_type($2, $2->name, 0); }
  334. | imp_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
  335. reg_type($2, $2->name, 0);
  336. }
  337. | imp_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
  338. | imp_statements statement { $$ = append_statement($1, $2); }
  339. | imp_statements importlib { $$ = append_statement($1, make_statement_importlib($2)); }
  340. | imp_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
  341. ;
  342. int_statements: { $$ = NULL; }
  343. | int_statements statement { $$ = append_statement($1, $2); }
  344. ;
  345. semicolon_opt:
  346. | ';'
  347. ;
  348. statement:
  349. cppquote { $$ = make_statement_cppquote($1); }
  350. | typedecl ';' { $$ = make_statement_type_decl($1); }
  351. | declaration ';' { $$ = make_statement_declaration($1); }
  352. | import { $$ = make_statement_import($1); }
  353. | typedef ';' { $$ = $1; }
  354. ;
  355. typedecl:
  356. enumdef
  357. | tENUM aIDENTIFIER { $$ = type_new_enum($2, FALSE, NULL); }
  358. | structdef
  359. | tSTRUCT aIDENTIFIER { $$ = type_new_struct($2, FALSE, NULL); }
  360. | uniondef
  361. | tUNION aIDENTIFIER { $$ = type_new_nonencapsulated_union($2, FALSE, NULL); }
  362. | attributes enumdef { $$ = $2; $$->attrs = check_enum_attrs($1); }
  363. | attributes structdef { $$ = $2; $$->attrs = check_struct_attrs($1); }
  364. | attributes uniondef { $$ = $2; $$->attrs = check_union_attrs($1); }
  365. ;
  366. cppquote: tCPPQUOTE '(' aSTRING ')' { $$ = $3; }
  367. ;
  368. import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
  369. $$ = xmalloc(sizeof(struct _import_t));
  370. $$->name = $2;
  371. $$->import_performed = do_import($2);
  372. if (!$$->import_performed) yychar = aEOF;
  373. }
  374. ;
  375. import: import_start imp_statements aEOF { $$ = $1->name;
  376. if ($1->import_performed) pop_import();
  377. free($1);
  378. }
  379. ;
  380. importlib: tIMPORTLIB '(' aSTRING ')'
  381. semicolon_opt { $$ = $3; if(!parse_only) add_importlib($3); }
  382. ;
  383. libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
  384. ;
  385. library_start: attributes libraryhdr '{' { $$ = make_library($2, check_library_attrs($2, $1));
  386. if (!parse_only) start_typelib($$);
  387. }
  388. ;
  389. librarydef: library_start imp_statements '}'
  390. semicolon_opt { $$ = $1;
  391. $$->stmts = $2;
  392. if (!parse_only) end_typelib();
  393. }
  394. ;
  395. m_args: { $$ = NULL; }
  396. | args
  397. ;
  398. arg_list: arg { check_arg_attrs($1); $$ = append_var( NULL, $1 ); }
  399. | arg_list ',' arg { check_arg_attrs($3); $$ = append_var( $1, $3 ); }
  400. ;
  401. args: arg_list
  402. | arg_list ',' ELLIPSIS { $$ = append_var( $1, make_var(strdup("...")) ); }
  403. ;
  404. /* split into two rules to get bison to resolve a tVOID conflict */
  405. arg: attributes decl_spec m_any_declarator { if ($2->stgclass != STG_NONE && $2->stgclass != STG_REGISTER)
  406. error_loc("invalid storage class for function parameter\n");
  407. $$ = declare_var($1, $2, $3, TRUE);
  408. free($2); free($3);
  409. }
  410. | decl_spec m_any_declarator { if ($1->stgclass != STG_NONE && $1->stgclass != STG_REGISTER)
  411. error_loc("invalid storage class for function parameter\n");
  412. $$ = declare_var(NULL, $1, $2, TRUE);
  413. free($1); free($2);
  414. }
  415. ;
  416. array: '[' expr ']' { $$ = $2;
  417. if (!$$->is_const)
  418. error_loc("array dimension is not an integer constant\n");
  419. }
  420. | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
  421. | '[' ']' { $$ = make_expr(EXPR_VOID); }
  422. ;
  423. m_attributes: { $$ = NULL; }
  424. | attributes
  425. ;
  426. attributes:
  427. '[' attrib_list ']' { $$ = $2; }
  428. ;
  429. attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
  430. | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
  431. | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
  432. ;
  433. str_list: aSTRING { $$ = append_str( NULL, $1 ); }
  434. | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
  435. ;
  436. attribute: { $$ = NULL; }
  437. | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
  438. | tANNOTATION '(' aSTRING ')' { $$ = make_attrp(ATTR_ANNOTATION, $3); }
  439. | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
  440. | tASYNC { $$ = make_attr(ATTR_ASYNC); }
  441. | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
  442. | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
  443. | tBROADCAST { $$ = make_attr(ATTR_BROADCAST); }
  444. | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
  445. | tCASE '(' expr_list_int_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
  446. | tCODE { $$ = make_attr(ATTR_CODE); }
  447. | tCOMMSTATUS { $$ = make_attr(ATTR_COMMSTATUS); }
  448. | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
  449. | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
  450. | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
  451. | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
  452. | tDECODE { $$ = make_attr(ATTR_DECODE); }
  453. | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
  454. | tDEFAULTBIND { $$ = make_attr(ATTR_DEFAULTBIND); }
  455. | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
  456. | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE, $3); }
  457. | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
  458. | tDISABLECONSISTENCYCHECK { $$ = make_attr(ATTR_DISABLECONSISTENCYCHECK); }
  459. | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
  460. | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
  461. | tDUAL { $$ = make_attr(ATTR_DUAL); }
  462. | tENABLEALLOCATE { $$ = make_attr(ATTR_ENABLEALLOCATE); }
  463. | tENCODE { $$ = make_attr(ATTR_ENCODE); }
  464. | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
  465. | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY, $3); }
  466. | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
  467. | tFAULTSTATUS { $$ = make_attr(ATTR_FAULTSTATUS); }
  468. | tFORCEALLOCATE { $$ = make_attr(ATTR_FORCEALLOCATE); }
  469. | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
  470. | tHELPCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
  471. | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
  472. | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
  473. | tHELPSTRINGCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
  474. | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
  475. | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
  476. | tID '(' expr_int_const ')' { $$ = make_attrp(ATTR_ID, $3); }
  477. | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
  478. | tIGNORE { $$ = make_attr(ATTR_IGNORE); }
  479. | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
  480. | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
  481. | tIMPLICITHANDLE '(' arg ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $3); }
  482. | tIN { $$ = make_attr(ATTR_IN); }
  483. | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
  484. | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
  485. | tLCID '(' expr_int_const ')' { $$ = make_attrp(ATTR_LIBLCID, $3); }
  486. | tLCID { $$ = make_attr(ATTR_PARAMLCID); }
  487. | tLICENSED { $$ = make_attr(ATTR_LICENSED); }
  488. | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
  489. | tMAYBE { $$ = make_attr(ATTR_MAYBE); }
  490. | tMESSAGE { $$ = make_attr(ATTR_MESSAGE); }
  491. | tNOCODE { $$ = make_attr(ATTR_NOCODE); }
  492. | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
  493. | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
  494. | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
  495. | tNOTIFY { $$ = make_attr(ATTR_NOTIFY); }
  496. | tNOTIFYFLAG { $$ = make_attr(ATTR_NOTIFYFLAG); }
  497. | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
  498. | tODL { $$ = make_attr(ATTR_ODL); }
  499. | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
  500. | tOPTIMIZE '(' aSTRING ')' { $$ = make_attrp(ATTR_OPTIMIZE, $3); }
  501. | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
  502. | tOUT { $$ = make_attr(ATTR_OUT); }
  503. | tPARTIALIGNORE { $$ = make_attr(ATTR_PARTIALIGNORE); }
  504. | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
  505. | tPROGID '(' aSTRING ')' { $$ = make_attrp(ATTR_PROGID, $3); }
  506. | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
  507. | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
  508. | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
  509. | tPROXY { $$ = make_attr(ATTR_PROXY); }
  510. | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
  511. | tRANGE '(' expr_int_const ',' expr_int_const ')'
  512. { expr_list_t *list = append_expr( NULL, $3 );
  513. list = append_expr( list, $5 );
  514. $$ = make_attrp(ATTR_RANGE, list); }
  515. | tREADONLY { $$ = make_attr(ATTR_READONLY); }
  516. | tREPRESENTAS '(' type ')' { $$ = make_attrp(ATTR_REPRESENTAS, $3); }
  517. | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
  518. | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
  519. | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
  520. | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
  521. | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
  522. | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
  523. | tSTRING { $$ = make_attr(ATTR_STRING); }
  524. | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
  525. | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
  526. | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
  527. | tTHREADING '(' threading_type ')' { $$ = make_attrv(ATTR_THREADING, $3); }
  528. | tUIDEFAULT { $$ = make_attr(ATTR_UIDEFAULT); }
  529. | tUSESGETLASTERROR { $$ = make_attr(ATTR_USESGETLASTERROR); }
  530. | tUSERMARSHAL '(' type ')' { $$ = make_attrp(ATTR_USERMARSHAL, $3); }
  531. | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
  532. | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
  533. | tVARARG { $$ = make_attr(ATTR_VARARG); }
  534. | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
  535. | tVIPROGID '(' aSTRING ')' { $$ = make_attrp(ATTR_VIPROGID, $3); }
  536. | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
  537. | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
  538. ;
  539. uuid_string:
  540. aUUID
  541. | aSTRING { if (!is_valid_uuid($1))
  542. error_loc("invalid UUID: %s\n", $1);
  543. $$ = parse_uuid($1); }
  544. ;
  545. callconv: tCDECL { $$ = xstrdup("__cdecl"); }
  546. | tFASTCALL { $$ = xstrdup("__fastcall"); }
  547. | tPASCAL { $$ = xstrdup("__pascal"); }
  548. | tSTDCALL { $$ = xstrdup("__stdcall"); }
  549. ;
  550. cases: { $$ = NULL; }
  551. | cases case { $$ = append_var( $1, $2 ); }
  552. ;
  553. case: tCASE expr_int_const ':' union_field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
  554. $$ = $4; if (!$$) $$ = make_var(NULL);
  555. $$->attrs = append_attr( $$->attrs, a );
  556. }
  557. | tDEFAULT ':' union_field { attr_t *a = make_attr(ATTR_DEFAULT);
  558. $$ = $3; if (!$$) $$ = make_var(NULL);
  559. $$->attrs = append_attr( $$->attrs, a );
  560. }
  561. ;
  562. enums: { $$ = NULL; }
  563. | enum_list ',' { $$ = $1; }
  564. | enum_list
  565. ;
  566. enum_list: enum { if (!$1->eval)
  567. $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
  568. $$ = append_var( NULL, $1 );
  569. }
  570. | enum_list ',' enum { if (!$3->eval)
  571. {
  572. var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
  573. $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
  574. }
  575. $$ = append_var( $1, $3 );
  576. }
  577. ;
  578. enum: ident '=' expr_int_const { $$ = reg_const($1);
  579. $$->eval = $3;
  580. $$->type = type_new_int(TYPE_BASIC_INT, 0);
  581. }
  582. | ident { $$ = reg_const($1);
  583. $$->type = type_new_int(TYPE_BASIC_INT, 0);
  584. }
  585. ;
  586. enumdef: tENUM t_ident '{' enums '}' { $$ = type_new_enum($2, TRUE, $4); }
  587. ;
  588. m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
  589. | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
  590. ;
  591. m_expr: { $$ = make_expr(EXPR_VOID); }
  592. | expr
  593. ;
  594. expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
  595. | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
  596. | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
  597. | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
  598. | tNULL { $$ = make_exprl(EXPR_NUM, 0); }
  599. | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
  600. | aSTRING { $$ = make_exprs(EXPR_STRLIT, $1); }
  601. | aWSTRING { $$ = make_exprs(EXPR_WSTRLIT, $1); }
  602. | aSQSTRING { $$ = make_exprs(EXPR_CHARCONST, $1); }
  603. | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
  604. | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
  605. | expr LOGICALOR expr { $$ = make_expr2(EXPR_LOGOR, $1, $3); }
  606. | expr LOGICALAND expr { $$ = make_expr2(EXPR_LOGAND, $1, $3); }
  607. | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
  608. | expr '^' expr { $$ = make_expr2(EXPR_XOR, $1, $3); }
  609. | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
  610. | expr EQUALITY expr { $$ = make_expr2(EXPR_EQUALITY, $1, $3); }
  611. | expr INEQUALITY expr { $$ = make_expr2(EXPR_INEQUALITY, $1, $3); }
  612. | expr '>' expr { $$ = make_expr2(EXPR_GTR, $1, $3); }
  613. | expr '<' expr { $$ = make_expr2(EXPR_LESS, $1, $3); }
  614. | expr GREATEREQUAL expr { $$ = make_expr2(EXPR_GTREQL, $1, $3); }
  615. | expr LESSEQUAL expr { $$ = make_expr2(EXPR_LESSEQL, $1, $3); }
  616. | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
  617. | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
  618. | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
  619. | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
  620. | expr '%' expr { $$ = make_expr2(EXPR_MOD, $1, $3); }
  621. | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
  622. | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
  623. | '!' expr { $$ = make_expr1(EXPR_LOGNOT, $2); }
  624. | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
  625. | '+' expr %prec POS { $$ = make_expr1(EXPR_POS, $2); }
  626. | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
  627. | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
  628. | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
  629. | expr MEMBERPTR aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, make_expr1(EXPR_PPTR, $1), make_exprs(EXPR_IDENTIFIER, $3)); }
  630. | expr '.' aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, $1, make_exprs(EXPR_IDENTIFIER, $3)); }
  631. | '(' decl_spec m_abstract_declarator ')' expr %prec CAST
  632. { $$ = make_exprt(EXPR_CAST, declare_var(NULL, $2, $3, 0), $5); free($2); free($3); }
  633. | tSIZEOF '(' decl_spec m_abstract_declarator ')'
  634. { $$ = make_exprt(EXPR_SIZEOF, declare_var(NULL, $3, $4, 0), NULL); free($3); free($4); }
  635. | expr '[' expr ']' { $$ = make_expr2(EXPR_ARRAY, $1, $3); }
  636. | '(' expr ')' { $$ = $2; }
  637. ;
  638. expr_list_int_const: expr_int_const { $$ = append_expr( NULL, $1 ); }
  639. | expr_list_int_const ',' expr_int_const { $$ = append_expr( $1, $3 ); }
  640. ;
  641. expr_int_const: expr { $$ = $1;
  642. if (!$$->is_const)
  643. error_loc("expression is not an integer constant\n");
  644. }
  645. ;
  646. expr_const: expr { $$ = $1;
  647. if (!$$->is_const && $$->type != EXPR_STRLIT && $$->type != EXPR_WSTRLIT)
  648. error_loc("expression is not constant\n");
  649. }
  650. ;
  651. fields: { $$ = NULL; }
  652. | fields field { $$ = append_var_list($1, $2); }
  653. ;
  654. field: m_attributes decl_spec struct_declarator_list ';'
  655. { const char *first = LIST_ENTRY(list_head($3), declarator_t, entry)->var->name;
  656. check_field_attrs(first, $1);
  657. $$ = set_var_types($1, $2, $3);
  658. }
  659. | m_attributes uniondef ';' { var_t *v = make_var(NULL);
  660. v->type = $2; v->attrs = $1;
  661. $$ = append_var(NULL, v);
  662. }
  663. ;
  664. ne_union_field:
  665. s_field ';' { $$ = $1; }
  666. | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
  667. ;
  668. ne_union_fields: { $$ = NULL; }
  669. | ne_union_fields ne_union_field { $$ = append_var( $1, $2 ); }
  670. ;
  671. union_field:
  672. s_field ';' { $$ = $1; }
  673. | ';' { $$ = NULL; }
  674. ;
  675. s_field: m_attributes decl_spec declarator { $$ = declare_var(check_field_attrs($3->var->name, $1),
  676. $2, $3, FALSE);
  677. free($3);
  678. }
  679. ;
  680. funcdef: declaration { $$ = $1;
  681. if (type_get_type($$->type) != TYPE_FUNCTION)
  682. error_loc("only methods may be declared inside the methods section of a dispinterface\n");
  683. check_function_attrs($$->name, $$->attrs);
  684. }
  685. ;
  686. declaration:
  687. attributes decl_spec init_declarator
  688. { $$ = declare_var($1, $2, $3, FALSE);
  689. free($3);
  690. }
  691. | decl_spec init_declarator { $$ = declare_var(NULL, $1, $2, FALSE);
  692. free($2);
  693. }
  694. ;
  695. m_ident: { $$ = NULL; }
  696. | ident
  697. ;
  698. t_ident: { $$ = NULL; }
  699. | aIDENTIFIER { $$ = $1; }
  700. | aKNOWNTYPE { $$ = $1; }
  701. ;
  702. ident: aIDENTIFIER { $$ = make_var($1); }
  703. /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
  704. | aKNOWNTYPE { $$ = make_var($<str>1); }
  705. ;
  706. base_type: tBYTE { $$ = find_type_or_error($<str>1, 0); }
  707. | tWCHAR { $$ = find_type_or_error($<str>1, 0); }
  708. | int_std
  709. | tSIGNED int_std { $$ = type_new_int(type_basic_get_type($2), -1); }
  710. | tUNSIGNED int_std { $$ = type_new_int(type_basic_get_type($2), 1); }
  711. | tUNSIGNED { $$ = type_new_int(TYPE_BASIC_INT, 1); }
  712. | tFLOAT { $$ = find_type_or_error($<str>1, 0); }
  713. | tDOUBLE { $$ = find_type_or_error($<str>1, 0); }
  714. | tBOOLEAN { $$ = find_type_or_error($<str>1, 0); }
  715. | tERRORSTATUST { $$ = find_type_or_error($<str>1, 0); }
  716. | tHANDLET { $$ = find_type_or_error($<str>1, 0); }
  717. ;
  718. m_int:
  719. | tINT
  720. ;
  721. int_std: tINT { $$ = type_new_int(TYPE_BASIC_INT, 0); }
  722. | tSHORT m_int { $$ = type_new_int(TYPE_BASIC_INT16, 0); }
  723. | tSMALL { $$ = type_new_int(TYPE_BASIC_INT8, 0); }
  724. | tLONG m_int { $$ = type_new_int(TYPE_BASIC_INT32, 0); }
  725. | tHYPER m_int { $$ = type_new_int(TYPE_BASIC_HYPER, 0); }
  726. | tINT64 { $$ = type_new_int(TYPE_BASIC_INT64, 0); }
  727. | tCHAR { $$ = type_new_int(TYPE_BASIC_CHAR, 0); }
  728. | tINT3264 { $$ = type_new_int(TYPE_BASIC_INT3264, 0); }
  729. ;
  730. coclass: tCOCLASS aIDENTIFIER { $$ = type_new_coclass($2); }
  731. | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
  732. if (type_get_type_detect_alias($$) != TYPE_COCLASS)
  733. error_loc("%s was not declared a coclass at %s:%d\n",
  734. $2, $$->loc_info.input_name,
  735. $$->loc_info.line_number);
  736. }
  737. ;
  738. coclasshdr: attributes coclass { $$ = $2;
  739. check_def($$);
  740. $$->attrs = check_coclass_attrs($2->name, $1);
  741. }
  742. ;
  743. coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
  744. { $$ = type_coclass_define($1, $3); }
  745. ;
  746. coclass_ints: { $$ = NULL; }
  747. | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
  748. ;
  749. coclass_int:
  750. m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
  751. ;
  752. dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(TYPE_INTERFACE, $2, 0); }
  753. | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(TYPE_INTERFACE, $2, 0); }
  754. ;
  755. dispinterfacehdr: attributes dispinterface { attr_t *attrs;
  756. $$ = $2;
  757. check_def($$);
  758. attrs = make_attr(ATTR_DISPINTERFACE);
  759. $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
  760. $$->defined = TRUE;
  761. }
  762. ;
  763. dispint_props: tPROPERTIES ':' { $$ = NULL; }
  764. | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
  765. ;
  766. dispint_meths: tMETHODS ':' { $$ = NULL; }
  767. | dispint_meths funcdef ';' { $$ = append_var( $1, $2 ); }
  768. ;
  769. dispinterfacedef: dispinterfacehdr '{'
  770. dispint_props
  771. dispint_meths
  772. '}' { $$ = $1;
  773. type_dispinterface_define($$, $3, $4);
  774. }
  775. | dispinterfacehdr
  776. '{' interface ';' '}' { $$ = $1;
  777. type_dispinterface_define_from_iface($$, $3);
  778. }
  779. ;
  780. inherit: { $$ = NULL; }
  781. | ':' aKNOWNTYPE { $$ = find_type_or_error2($2, 0); }
  782. ;
  783. interface: tINTERFACE aIDENTIFIER { $$ = get_type(TYPE_INTERFACE, $2, 0); }
  784. | tINTERFACE aKNOWNTYPE { $$ = get_type(TYPE_INTERFACE, $2, 0); }
  785. ;
  786. interfacehdr: attributes interface { $$.interface = $2;
  787. $$.old_pointer_default = pointer_default;
  788. if (is_attr($1, ATTR_POINTERDEFAULT))
  789. pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
  790. check_def($2);
  791. $2->attrs = check_iface_attrs($2->name, $1);
  792. $2->defined = TRUE;
  793. }
  794. ;
  795. interfacedef: interfacehdr inherit
  796. '{' int_statements '}' semicolon_opt { $$ = $1.interface;
  797. type_interface_define($$, $2, $4);
  798. pointer_default = $1.old_pointer_default;
  799. }
  800. /* MIDL is able to import the definition of a base class from inside the
  801. * definition of a derived class, I'll try to support it with this rule */
  802. | interfacehdr ':' aIDENTIFIER
  803. '{' import int_statements '}'
  804. semicolon_opt { $$ = $1.interface;
  805. type_interface_define($$, find_type_or_error2($3, 0), $6);
  806. pointer_default = $1.old_pointer_default;
  807. }
  808. | dispinterfacedef semicolon_opt { $$ = $1; }
  809. ;
  810. interfacedec:
  811. interface ';' { $$ = $1; }
  812. | dispinterface ';' { $$ = $1; }
  813. ;
  814. module: tMODULE aIDENTIFIER { $$ = type_new_module($2); }
  815. | tMODULE aKNOWNTYPE { $$ = type_new_module($2); }
  816. ;
  817. modulehdr: attributes module { $$ = $2;
  818. $$->attrs = check_module_attrs($2->name, $1);
  819. }
  820. ;
  821. moduledef: modulehdr '{' int_statements '}'
  822. semicolon_opt { $$ = $1;
  823. type_module_define($$, $3);
  824. }
  825. ;
  826. storage_cls_spec:
  827. tEXTERN { $$ = STG_EXTERN; }
  828. | tSTATIC { $$ = STG_STATIC; }
  829. | tREGISTER { $$ = STG_REGISTER; }
  830. ;
  831. function_specifier:
  832. tINLINE { $$ = make_attr(ATTR_INLINE); }
  833. ;
  834. type_qualifier:
  835. tCONST { $$ = make_attr(ATTR_CONST); }
  836. ;
  837. m_type_qual_list: { $$ = NULL; }
  838. | m_type_qual_list type_qualifier { $$ = append_attr($1, $2); }
  839. ;
  840. decl_spec: type m_decl_spec_no_type { $$ = make_decl_spec($1, $2, NULL, NULL, STG_NONE); }
  841. | decl_spec_no_type type m_decl_spec_no_type
  842. { $$ = make_decl_spec($2, $1, $3, NULL, STG_NONE); }
  843. ;
  844. m_decl_spec_no_type: { $$ = NULL; }
  845. | decl_spec_no_type
  846. ;
  847. decl_spec_no_type:
  848. type_qualifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
  849. | function_specifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
  850. | storage_cls_spec m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, NULL, $1); }
  851. ;
  852. declarator:
  853. '*' m_type_qual_list declarator %prec PPTR
  854. { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
  855. | callconv declarator { $$ = $2; if ($$->func_type) $$->func_type->attrs = append_attr($$->func_type->attrs, make_attrp(ATTR_CALLCONV, $1));
  856. else if ($$->type) $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
  857. | direct_declarator
  858. ;
  859. direct_declarator:
  860. ident { $$ = make_declarator($1); }
  861. | '(' declarator ')' { $$ = $2; }
  862. | direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
  863. | direct_declarator '(' m_args ')' { $$ = $1;
  864. $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
  865. $$->type = NULL;
  866. }
  867. ;
  868. /* abstract declarator */
  869. abstract_declarator:
  870. '*' m_type_qual_list m_abstract_declarator %prec PPTR
  871. { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
  872. | callconv m_abstract_declarator { $$ = $2; if ($$->func_type) $$->func_type->attrs = append_attr($$->func_type->attrs, make_attrp(ATTR_CALLCONV, $1));
  873. else if ($$->type) $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
  874. | abstract_direct_declarator
  875. ;
  876. /* abstract declarator without accepting direct declarator */
  877. abstract_declarator_no_direct:
  878. '*' m_type_qual_list m_any_declarator %prec PPTR
  879. { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
  880. | callconv m_any_declarator { $$ = $2; if ($$->func_type) $$->func_type->attrs = append_attr($$->func_type->attrs, make_attrp(ATTR_CALLCONV, $1));
  881. else if ($$->type) $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
  882. ;
  883. /* abstract declarator or empty */
  884. m_abstract_declarator: { $$ = make_declarator(NULL); }
  885. | abstract_declarator
  886. ;
  887. /* abstract direct declarator */
  888. abstract_direct_declarator:
  889. '(' abstract_declarator_no_direct ')' { $$ = $2; }
  890. | abstract_direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
  891. | array { $$ = make_declarator(NULL); $$->array = append_array($$->array, $1); }
  892. | '(' m_args ')'
  893. { $$ = make_declarator(NULL);
  894. $$->func_type = append_ptrchain_type($$->type, type_new_function($2));
  895. $$->type = NULL;
  896. }
  897. | abstract_direct_declarator '(' m_args ')'
  898. { $$ = $1;
  899. $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
  900. $$->type = NULL;
  901. }
  902. ;
  903. /* abstract or non-abstract declarator */
  904. any_declarator:
  905. '*' m_type_qual_list m_any_declarator %prec PPTR
  906. { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
  907. | callconv m_any_declarator { $$ = $2; $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
  908. | any_direct_declarator
  909. ;
  910. /* abstract or non-abstract declarator without accepting direct declarator */
  911. any_declarator_no_direct:
  912. '*' m_type_qual_list m_any_declarator %prec PPTR
  913. { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(pointer_default, NULL, $2)); }
  914. | callconv m_any_declarator { $$ = $2; $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
  915. ;
  916. /* abstract or non-abstract declarator or empty */
  917. m_any_declarator: { $$ = make_declarator(NULL); }
  918. | any_declarator
  919. ;
  920. /* abstract or non-abstract direct declarator. note: direct declarators
  921. * aren't accepted inside brackets to avoid ambiguity with the rule for
  922. * function arguments */
  923. any_direct_declarator:
  924. ident { $$ = make_declarator($1); }
  925. | '(' any_declarator_no_direct ')' { $$ = $2; }
  926. | any_direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
  927. | array { $$ = make_declarator(NULL); $$->array = append_array($$->array, $1); }
  928. | '(' m_args ')'
  929. { $$ = make_declarator(NULL);
  930. $$->func_type = append_ptrchain_type($$->type, type_new_function($2));
  931. $$->type = NULL;
  932. }
  933. | any_direct_declarator '(' m_args ')'
  934. { $$ = $1;
  935. $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
  936. $$->type = NULL;
  937. }
  938. ;
  939. declarator_list:
  940. declarator { $$ = append_declarator( NULL, $1 ); }
  941. | declarator_list ',' declarator { $$ = append_declarator( $1, $3 ); }
  942. ;
  943. m_bitfield: { $$ = NULL; }
  944. | ':' expr_const { $$ = $2; }
  945. ;
  946. struct_declarator: any_declarator m_bitfield { $$ = $1; $$->bits = $2;
  947. if (!$$->bits && !$$->var->name)
  948. error_loc("unnamed fields are not allowed\n");
  949. }
  950. ;
  951. struct_declarator_list:
  952. struct_declarator { $$ = append_declarator( NULL, $1 ); }
  953. | struct_declarator_list ',' struct_declarator
  954. { $$ = append_declarator( $1, $3 ); }
  955. ;
  956. init_declarator:
  957. declarator { $$ = $1; }
  958. | declarator '=' expr_const { $$ = $1; $1->var->eval = $3; }
  959. ;
  960. threading_type:
  961. tAPARTMENT { $$ = THREADING_APARTMENT; }
  962. | tNEUTRAL { $$ = THREADING_NEUTRAL; }
  963. | tSINGLE { $$ = THREADING_SINGLE; }
  964. | tFREE { $$ = THREADING_FREE; }
  965. | tBOTH { $$ = THREADING_BOTH; }
  966. ;
  967. pointer_type:
  968. tREF { $$ = RPC_FC_RP; }
  969. | tUNIQUE { $$ = RPC_FC_UP; }
  970. | tPTR { $$ = RPC_FC_FP; }
  971. ;
  972. structdef: tSTRUCT t_ident '{' fields '}' { $$ = type_new_struct($2, TRUE, $4); }
  973. ;
  974. type: tVOID { $$ = type_new_void(); }
  975. | aKNOWNTYPE { $$ = find_type_or_error($1, 0); }
  976. | base_type { $$ = $1; }
  977. | enumdef { $$ = $1; }
  978. | tENUM aIDENTIFIER { $$ = type_new_enum($2, FALSE, NULL); }
  979. | structdef { $$ = $1; }
  980. | tSTRUCT aIDENTIFIER { $$ = type_new_struct($2, FALSE, NULL); }
  981. | uniondef { $$ = $1; }
  982. | tUNION aIDENTIFIER { $$ = type_new_nonencapsulated_union($2, FALSE, NULL); }
  983. | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
  984. ;
  985. typedef: tTYPEDEF m_attributes decl_spec declarator_list
  986. { reg_typedefs($3, $4, check_typedef_attrs($2));
  987. $$ = make_statement_typedef($4);
  988. }
  989. ;
  990. uniondef: tUNION t_ident '{' ne_union_fields '}'
  991. { $$ = type_new_nonencapsulated_union($2, TRUE, $4); }
  992. | tUNION t_ident
  993. tSWITCH '(' s_field ')'
  994. m_ident '{' cases '}' { $$ = type_new_encapsulated_union($2, $5, $7, $9); }
  995. ;
  996. version:
  997. aNUM { $$ = MAKEVERSION($1, 0); }
  998. | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
  999. ;
  1000. %%
  1001. static void decl_builtin_basic(const char *name, enum type_basic_type type)
  1002. {
  1003. type_t *t = type_new_basic(type);
  1004. reg_type(t, name, 0);
  1005. }
  1006. static void decl_builtin_alias(const char *name, type_t *t)
  1007. {
  1008. reg_type(type_new_alias(t, name), name, 0);
  1009. }
  1010. void init_types(void)
  1011. {
  1012. decl_builtin_basic("byte", TYPE_BASIC_BYTE);
  1013. decl_builtin_basic("wchar_t", TYPE_BASIC_WCHAR);
  1014. decl_builtin_basic("float", TYPE_BASIC_FLOAT);
  1015. decl_builtin_basic("double", TYPE_BASIC_DOUBLE);
  1016. decl_builtin_basic("error_status_t", TYPE_BASIC_ERROR_STATUS_T);
  1017. decl_builtin_basic("handle_t", TYPE_BASIC_HANDLE);
  1018. decl_builtin_alias("boolean", type_new_basic(TYPE_BASIC_BYTE));
  1019. }
  1020. static str_list_t *append_str(str_list_t *list, char *str)
  1021. {
  1022. struct str_list_entry_t *entry;
  1023. if (!str) return list;
  1024. if (!list)
  1025. {
  1026. list = xmalloc( sizeof(*list) );
  1027. list_init( list );
  1028. }
  1029. entry = xmalloc( sizeof(*entry) );
  1030. entry->str = str;
  1031. list_add_tail( list, &entry->entry );
  1032. return list;
  1033. }
  1034. static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
  1035. {
  1036. attr_t *attr_existing;
  1037. if (!attr) return list;
  1038. if (!list)
  1039. {
  1040. list = xmalloc( sizeof(*list) );
  1041. list_init( list );
  1042. }
  1043. LIST_FOR_EACH_ENTRY(attr_existing, list, attr_t, entry)
  1044. if (attr_existing->type == attr->type)
  1045. {
  1046. parser_warning("duplicate attribute %s\n", get_attr_display_name(attr->type));
  1047. /* use the last attribute, like MIDL does */
  1048. list_remove(&attr_existing->entry);
  1049. break;
  1050. }
  1051. list_add_tail( list, &attr->entry );
  1052. return list;
  1053. }
  1054. static attr_list_t *move_attr(attr_list_t *dst, attr_list_t *src, enum attr_type type)
  1055. {
  1056. attr_t *attr;
  1057. if (!src) return dst;
  1058. LIST_FOR_EACH_ENTRY(attr, src, attr_t, entry)
  1059. if (attr->type == type)
  1060. {
  1061. list_remove(&attr->entry);
  1062. return append_attr(dst, attr);
  1063. }
  1064. return dst;
  1065. }
  1066. static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list)
  1067. {
  1068. struct list *entry;
  1069. if (!old_list) return new_list;
  1070. while ((entry = list_head(old_list)))
  1071. {
  1072. attr_t *attr = LIST_ENTRY(entry, attr_t, entry);
  1073. list_remove(entry);
  1074. new_list = append_attr(new_list, attr);
  1075. }
  1076. return new_list;
  1077. }
  1078. static attr_list_t *dupattrs(const attr_list_t *list)
  1079. {
  1080. attr_list_t *new_list;
  1081. const attr_t *attr;
  1082. if (!list) return NULL;
  1083. new_list = xmalloc( sizeof(*list) );
  1084. list_init( new_list );
  1085. LIST_FOR_EACH_ENTRY(attr, list, const attr_t, entry)
  1086. {
  1087. attr_t *new_attr = xmalloc(sizeof(*new_attr));
  1088. *new_attr = *attr;
  1089. list_add_tail(new_list, &new_attr->entry);
  1090. }
  1091. return new_list;
  1092. }
  1093. static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right, attr_t *attr, enum storage_class stgclass)
  1094. {
  1095. decl_spec_t *declspec = left ? left : right;
  1096. if (!declspec)
  1097. {
  1098. declspec = xmalloc(sizeof(*declspec));
  1099. declspec->type = NULL;
  1100. declspec->attrs = NULL;
  1101. declspec->stgclass = STG_NONE;
  1102. }
  1103. declspec->type = type;
  1104. if (left && declspec != left)
  1105. {
  1106. declspec->attrs = append_attr_list(declspec->attrs, left->attrs);
  1107. if (declspec->stgclass == STG_NONE)
  1108. declspec->stgclass = left->stgclass;
  1109. else if (left->stgclass != STG_NONE)
  1110. error_loc("only one storage class can be specified\n");
  1111. assert(!left->type);
  1112. free(left);
  1113. }
  1114. if (right && declspec != right)
  1115. {
  1116. declspec->attrs = append_attr_list(declspec->attrs, right->attrs);
  1117. if (declspec->stgclass == STG_NONE)
  1118. declspec->stgclass = right->stgclass;
  1119. else if (right->stgclass != STG_NONE)
  1120. error_loc("only one storage class can be specified\n");
  1121. assert(!right->type);
  1122. free(right);
  1123. }
  1124. declspec->attrs = append_attr(declspec->attrs, attr);
  1125. if (declspec->stgclass == STG_NONE)
  1126. declspec->stgclass = stgclass;
  1127. else if (stgclass != STG_NONE)
  1128. error_loc("only one storage class can be specified\n");
  1129. /* apply attributes to type */
  1130. if (type && declspec->attrs)
  1131. {
  1132. attr_list_t *attrs;
  1133. declspec->type = duptype(type, 1);
  1134. attrs = dupattrs(type->attrs);
  1135. declspec->type->attrs = append_attr_list(attrs, declspec->attrs);
  1136. declspec->attrs = NULL;
  1137. }
  1138. return declspec;
  1139. }
  1140. static attr_t *make_attr(enum attr_type type)
  1141. {
  1142. attr_t *a = xmalloc(sizeof(attr_t));
  1143. a->type = type;
  1144. a->u.ival = 0;
  1145. return a;
  1146. }
  1147. static attr_t *make_attrv(enum attr_type type, unsigned int val)
  1148. {
  1149. attr_t *a = xmalloc(sizeof(attr_t));
  1150. a->type = type;
  1151. a->u.ival = val;
  1152. return a;
  1153. }
  1154. static attr_t *make_attrp(enum attr_type type, void *val)
  1155. {
  1156. attr_t *a = xmalloc(sizeof(attr_t));
  1157. a->type = type;
  1158. a->u.pval = val;
  1159. return a;
  1160. }
  1161. static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
  1162. {
  1163. if (!expr) return list;
  1164. if (!list)
  1165. {
  1166. list = xmalloc( sizeof(*list) );
  1167. list_init( list );
  1168. }
  1169. list_add_tail( list, &expr->entry );
  1170. return list;
  1171. }
  1172. static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
  1173. {
  1174. if (!expr) return list;
  1175. if (!list)
  1176. {
  1177. list = xmalloc( sizeof(*list) );
  1178. list_init( list );
  1179. }
  1180. list_add_tail( list, &expr->entry );
  1181. return list;
  1182. }
  1183. static struct list type_pool = LIST_INIT(type_pool);
  1184. typedef struct
  1185. {
  1186. type_t data;
  1187. struct list link;
  1188. } type_pool_node_t;
  1189. type_t *alloc_type(void)
  1190. {
  1191. type_pool_node_t *node = xmalloc(sizeof *node);
  1192. list_add_tail(&type_pool, &node->link);
  1193. return &node->data;
  1194. }
  1195. void set_all_tfswrite(int val)
  1196. {
  1197. type_pool_node_t *node;
  1198. LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
  1199. node->data.tfswrite = val;
  1200. }
  1201. void clear_all_offsets(void)
  1202. {
  1203. type_pool_node_t *node;
  1204. LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
  1205. node->data.typestring_offset = node->data.ptrdesc = 0;
  1206. }
  1207. static void type_function_add_head_arg(type_t *type, var_t *arg)
  1208. {
  1209. if (!type->details.function->args)
  1210. {
  1211. type->details.function->args = xmalloc( sizeof(*type->details.function->args) );
  1212. list_init( type->details.function->args );
  1213. }
  1214. list_add_head( type->details.function->args, &arg->entry );
  1215. }
  1216. static int is_allowed_range_type(const type_t *type)
  1217. {
  1218. switch (type_get_type(type))
  1219. {
  1220. case TYPE_ENUM:
  1221. return TRUE;
  1222. case TYPE_BASIC:
  1223. switch (type_basic_get_type(type))
  1224. {
  1225. case TYPE_BASIC_INT8:
  1226. case TYPE_BASIC_INT16:
  1227. case TYPE_BASIC_INT32:
  1228. case TYPE_BASIC_INT64:
  1229. case TYPE_BASIC_INT:
  1230. case TYPE_BASIC_INT3264:
  1231. case TYPE_BASIC_BYTE:
  1232. case TYPE_BASIC_CHAR:
  1233. case TYPE_BASIC_WCHAR:
  1234. case TYPE_BASIC_HYPER:
  1235. return TRUE;
  1236. case TYPE_BASIC_FLOAT:
  1237. case TYPE_BASIC_DOUBLE:
  1238. case TYPE_BASIC_ERROR_STATUS_T:
  1239. case TYPE_BASIC_HANDLE:
  1240. return FALSE;
  1241. }
  1242. return FALSE;
  1243. default:
  1244. return FALSE;
  1245. }
  1246. }
  1247. static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type)
  1248. {
  1249. type_t *ptrchain_type;
  1250. if (!ptrchain)
  1251. return type;
  1252. for (ptrchain_type = ptrchain; type_pointer_get_ref(ptrchain_type); ptrchain_type = type_pointer_get_ref(ptrchain_type))
  1253. ;
  1254. assert(ptrchain_type->type_type == TYPE_POINTER);
  1255. ptrchain_type->details.pointer.ref = type;
  1256. return ptrchain;
  1257. }
  1258. static var_t *declare_var(attr_list_t *attrs, decl_spec_t *decl_spec, const declarator_t *decl,
  1259. int top)
  1260. {
  1261. var_t *v = decl->var;
  1262. expr_list_t *sizes = get_attrp(attrs, ATTR_SIZEIS);
  1263. expr_list_t *lengs = get_attrp(attrs, ATTR_LENGTHIS);
  1264. int sizeless;
  1265. expr_t *dim;
  1266. type_t **ptype;
  1267. array_dims_t *arr = decl ? decl->array : NULL;
  1268. type_t *func_type = decl ? decl->func_type : NULL;
  1269. type_t *type = decl_spec->type;
  1270. /* In case of a range attribute, duplicate the type to keep track of
  1271. * the min/max values in the type format string */
  1272. if(is_attr(attrs, ATTR_RANGE))
  1273. type = duptype(type, 1);
  1274. if (is_attr(type->attrs, ATTR_INLINE))
  1275. {
  1276. if (!func_type)
  1277. error_loc("inline attribute applied to non-function type\n");
  1278. else
  1279. {
  1280. type_t *t;
  1281. /* move inline attribute from return type node to function node */
  1282. for (t = func_type; is_ptr(t); t = type_pointer_get_ref(t))
  1283. ;
  1284. t->attrs = move_attr(t->attrs, type->attrs, ATTR_INLINE);
  1285. }
  1286. }
  1287. /* add type onto the end of the pointers in pident->type */
  1288. v->type = append_ptrchain_type(decl ? decl->type : NULL, type);
  1289. v->stgclass = decl_spec->stgclass;
  1290. v->attrs = attrs;
  1291. /* check for pointer attribute being applied to non-pointer, non-array
  1292. * type */
  1293. if (!arr)
  1294. {
  1295. int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
  1296. const type_t *ptr = NULL;
  1297. /* pointer attributes on the left side of the type belong to the function
  1298. * pointer, if one is being declared */
  1299. type_t **pt = func_type ? &func_type : &v->type;
  1300. for (ptr = *pt; ptr && !ptr_attr; )
  1301. {
  1302. ptr_attr = get_attrv(ptr->attrs, ATTR_POINTERTYPE);
  1303. if (!ptr_attr && type_is_alias(ptr))
  1304. ptr = type_alias_get_aliasee(ptr);
  1305. else
  1306. break;
  1307. }
  1308. if (is_ptr(ptr))
  1309. {
  1310. if (ptr_attr && ptr_attr != RPC_FC_UP &&
  1311. type_get_type(type_pointer_get_ref(ptr)) == TYPE_INTERFACE)
  1312. warning_loc_info(&v->loc_info,
  1313. "%s: pointer attribute applied to interface "
  1314. "pointer type has no effect\n", v->name);
  1315. if (!ptr_attr && top && (*pt)->details.pointer.def_fc != RPC_FC_RP)
  1316. {
  1317. /* FIXME: this is a horrible hack to cope with the issue that we
  1318. * store an offset to the typeformat string in the type object, but
  1319. * two typeformat strings may be written depending on whether the
  1320. * pointer is a toplevel parameter or not */
  1321. *pt = duptype(*pt, 1);
  1322. }
  1323. }
  1324. else if (ptr_attr)
  1325. error_loc("%s: pointer attribute applied to non-pointer type\n", v->name);
  1326. }
  1327. if (is_attr(v->attrs, ATTR_STRING))
  1328. {
  1329. type_t *t = type;
  1330. if (!is_ptr(v->type) && !arr)
  1331. error_loc("'%s': [string] attribute applied to non-pointer, non-array type\n",
  1332. v->name);
  1333. while (is_ptr(t))
  1334. t = type_pointer_get_ref(t);
  1335. if (type_get_type(t) != TYPE_BASIC &&
  1336. (get_basic_fc(t) != RPC_FC_CHAR &&
  1337. get_basic_fc(t) != RPC_FC_BYTE &&
  1338. get_basic_fc(t) != RPC_FC_WCHAR))
  1339. {
  1340. error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
  1341. v->name);
  1342. }
  1343. }
  1344. if (is_attr(v->attrs, ATTR_V1ENUM))
  1345. {
  1346. if (type_get_type_detect_alias(v->type) != TYPE_ENUM)
  1347. error_loc("'%s': [v1_enum] attribute applied to non-enum type\n", v->name);
  1348. }
  1349. if (is_attr(v->attrs, ATTR_RANGE) && !is_allowed_range_type(v->type))
  1350. error_loc("'%s': [range] attribute applied to non-integer type\n",
  1351. v->name);
  1352. ptype = &v->type;
  1353. sizeless = FALSE;
  1354. if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
  1355. {
  1356. if (sizeless)
  1357. error_loc("%s: only the first array dimension can be unspecified\n", v->name);
  1358. if (dim->is_const)
  1359. {
  1360. if (dim->cval <= 0)
  1361. error_loc("%s: array dimension must be positive\n", v->name);
  1362. /* FIXME: should use a type_memsize that allows us to pass in a pointer size */
  1363. if (0)
  1364. {
  1365. unsigned int size = type_memsize(v->type);
  1366. if (0xffffffffu / size < dim->cval)
  1367. error_loc("%s: total array size is too large\n", v->name);
  1368. }
  1369. }
  1370. else
  1371. sizeless = TRUE;
  1372. *ptype = type_new_array(NULL, *ptype, FALSE,
  1373. dim->is_const ? dim->cval : 0,
  1374. dim->is_const ? NULL : dim, NULL,
  1375. pointer_default);
  1376. }
  1377. ptype = &v->type;
  1378. if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
  1379. {
  1380. if (dim->type != EXPR_VOID)
  1381. {
  1382. if (is_array(*ptype))
  1383. {
  1384. if (!type_array_get_conformance(*ptype) ||
  1385. type_array_get_conformance(*ptype)->type != EXPR_VOID)
  1386. error_loc("%s: cannot specify size_is for an already sized array\n", v->name);
  1387. else
  1388. *ptype = type_new_array((*ptype)->name,
  1389. type_array_get_element(*ptype), FALSE,
  1390. 0, dim, NULL, 0);
  1391. }
  1392. else if (is_ptr(*ptype))
  1393. *ptype = type_new_array((*ptype)->name, type_pointer_get_ref(*ptype), TRUE,
  1394. 0, dim, NULL, pointer_default);
  1395. else
  1396. error_loc("%s: size_is attribute applied to illegal type\n", v->name);
  1397. }
  1398. if (is_ptr(*ptype))
  1399. ptype = &(*ptype)->details.pointer.ref;
  1400. else if (is_array(*ptype))
  1401. ptype = &(*ptype)->details.array.elem;
  1402. else
  1403. error_loc("%s: too many expressions in size_is attribute\n", v->name);
  1404. }
  1405. ptype = &v->type;
  1406. if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
  1407. {
  1408. if (dim->type != EXPR_VOID)
  1409. {
  1410. if (is_array(*ptype))
  1411. {
  1412. *ptype = type_new_array((*ptype)->name,
  1413. type_array_get_element(*ptype),
  1414. type_array_is_decl_as_ptr(*ptype),
  1415. type_array_get_dim(*ptype),
  1416. type_array_get_conformance(*ptype),
  1417. dim, type_array_get_ptr_default_fc(*ptype));
  1418. }
  1419. else
  1420. error_loc("%s: length_is attribute applied to illegal type\n", v->name);
  1421. }
  1422. if (is_ptr(*ptype))
  1423. ptype = &(*ptype)->details.pointer.ref;
  1424. else if (is_array(*ptype))
  1425. ptype = &(*ptype)->details.array.elem;
  1426. else
  1427. error_loc("%s: too many expressions in length_is attribute\n", v->name);
  1428. }
  1429. /* v->type is currently pointing to the type on the left-side of the
  1430. * declaration, so we need to fix this up so that it is the return type of the
  1431. * function and make v->type point to the function side of the declaration */
  1432. if (func_type)
  1433. {
  1434. type_t *ft, *t;
  1435. type_t *return_type = v->type;
  1436. v->type = func_type;
  1437. for (ft = v->type; is_ptr(ft); ft = type_pointer_get_ref(ft))
  1438. ;
  1439. assert(type_get_type_detect_alias(ft) == TYPE_FUNCTION);
  1440. ft->details.function->rettype = return_type;
  1441. /* move calling convention attribute, if present, from pointer nodes to
  1442. * function node */
  1443. for (t = v->type; is_ptr(t); t = type_pointer_get_ref(t))
  1444. ft->attrs = move_attr(ft->attrs, t->attrs, ATTR_CALLCONV);
  1445. }
  1446. else
  1447. {
  1448. type_t *t;
  1449. for (t = v->type; is_ptr(t); t = type_pointer_get_ref(t))
  1450. if (is_attr(t->attrs, ATTR_CALLCONV))
  1451. error_loc("calling convention applied to non-function-pointer type\n");
  1452. }
  1453. if (decl->bits)
  1454. v->type = type_new_bitfield(v->type, decl->bits);
  1455. return v;
  1456. }
  1457. static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls)
  1458. {
  1459. declarator_t *decl, *next;
  1460. var_list_t *var_list = NULL;
  1461. LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
  1462. {
  1463. var_t *var = declare_var(attrs, decl_spec, decl, 0);
  1464. var_list = append_var(var_list, var);
  1465. free(decl);
  1466. }
  1467. free(decl_spec);
  1468. return var_list;
  1469. }
  1470. static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
  1471. {
  1472. if (!iface) return list;
  1473. if (!list)
  1474. {
  1475. list = xmalloc( sizeof(*list) );
  1476. list_init( list );
  1477. }
  1478. list_add_tail( list, &iface->entry );
  1479. return list;
  1480. }
  1481. static ifref_t *make_ifref(type_t *iface)
  1482. {
  1483. ifref_t *l = xmalloc(sizeof(ifref_t));
  1484. l->iface = iface;
  1485. l->attrs = NULL;
  1486. return l;
  1487. }
  1488. var_list_t *append_var(var_list_t *list, var_t *var)
  1489. {
  1490. if (!var) return list;
  1491. if (!list)
  1492. {
  1493. list = xmalloc( sizeof(*list) );
  1494. list_init( list );
  1495. }
  1496. list_add_tail( list, &var->entry );
  1497. return list;
  1498. }
  1499. static var_list_t *append_var_list(var_list_t *list, var_list_t *vars)
  1500. {
  1501. if (!vars) return list;
  1502. if (!list)
  1503. {
  1504. list = xmalloc( sizeof(*list) );
  1505. list_init( list );
  1506. }
  1507. list_move_tail( list, vars );
  1508. return list;
  1509. }
  1510. var_t *make_var(char *name)
  1511. {
  1512. var_t *v = xmalloc(sizeof(var_t));
  1513. v->name = name;
  1514. v->type = NULL;
  1515. v->attrs = NULL;
  1516. v->eval = NULL;
  1517. v->stgclass = STG_NONE;
  1518. init_loc_info(&v->loc_info);
  1519. return v;
  1520. }
  1521. static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *d)
  1522. {
  1523. if (!d) return list;
  1524. if (!list) {
  1525. list = xmalloc(sizeof(*list));
  1526. list_init(list);
  1527. }
  1528. list_add_tail(list, &d->entry);
  1529. return list;
  1530. }
  1531. static declarator_t *make_declarator(var_t *var)
  1532. {
  1533. declarator_t *d = xmalloc(sizeof(*d));
  1534. d->var = var ? var : make_var(NULL);
  1535. d->type = NULL;
  1536. d->func_type = NULL;
  1537. d->array = NULL;
  1538. d->bits = NULL;
  1539. return d;
  1540. }
  1541. static type_t *make_safearray(type_t *type)
  1542. {
  1543. return type_new_array(NULL, type_new_alias(type, "SAFEARRAY"), TRUE, 0,
  1544. NULL, NULL, RPC_FC_RP);
  1545. }
  1546. static typelib_t *make_library(const char *name, const attr_list_t *attrs)
  1547. {
  1548. typelib_t *typelib = xmalloc(sizeof(*typelib));
  1549. typelib->name = xstrdup(name);
  1550. typelib->attrs = attrs;
  1551. list_init( &typelib->importlibs );
  1552. return typelib;
  1553. }
  1554. #define HASHMAX 64
  1555. static int hash_ident(const char *name)
  1556. {
  1557. const char *p = name;
  1558. int sum = 0;
  1559. /* a simple sum hash is probably good enough */
  1560. while (*p) {
  1561. sum += *p;
  1562. p++;
  1563. }
  1564. return sum & (HASHMAX-1);
  1565. }
  1566. /***** type repository *****/
  1567. struct rtype {
  1568. const char *name;
  1569. type_t *type;
  1570. int t;
  1571. struct rtype *next;
  1572. };
  1573. struct rtype *type_hash[HASHMAX];
  1574. type_t *reg_type(type_t *type, const char *name, int t)
  1575. {
  1576. struct rtype *nt;
  1577. int hash;
  1578. if (!name) {
  1579. error_loc("registering named type without name\n");
  1580. return type;
  1581. }
  1582. hash = hash_ident(name);
  1583. nt = xmalloc(sizeof(struct rtype));
  1584. nt->name = name;
  1585. nt->type = type;
  1586. nt->t = t;
  1587. nt->next = type_hash[hash];
  1588. type_hash[hash] = nt;
  1589. if ((t == tsSTRUCT || t == tsUNION))
  1590. fix_incomplete_types(type);
  1591. return type;
  1592. }
  1593. static int is_incomplete(const type_t *t)
  1594. {
  1595. return !t->defined &&
  1596. (type_get_type_detect_alias(t) == TYPE_STRUCT ||
  1597. type_get_type_detect_alias(t) == TYPE_UNION ||
  1598. type_get_type_detect_alias(t) == TYPE_ENCAPSULATED_UNION);
  1599. }
  1600. void add_incomplete(type_t *t)
  1601. {
  1602. struct typenode *tn = xmalloc(sizeof *tn);
  1603. tn->type = t;
  1604. list_add_tail(&incomplete_types, &tn->entry);
  1605. }
  1606. static void fix_type(type_t *t)
  1607. {
  1608. if (type_is_alias(t) && is_incomplete(t)) {
  1609. type_t *ot = type_alias_get_aliasee(t);
  1610. fix_type(ot);
  1611. if (type_get_type_detect_alias(ot) == TYPE_STRUCT ||
  1612. type_get_type_detect_alias(ot) == TYPE_UNION ||
  1613. type_get_type_detect_alias(ot) == TYPE_ENCAPSULATED_UNION)
  1614. t->details.structure = ot->details.structure;
  1615. t->defined = ot->defined;
  1616. }
  1617. }
  1618. static void fix_incomplete(void)
  1619. {
  1620. struct typenode *tn, *next;
  1621. LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
  1622. fix_type(tn->type);
  1623. list_remove(&tn->entry);
  1624. free(tn);
  1625. }
  1626. }
  1627. static void fix_incomplete_types(type_t *complete_type)
  1628. {
  1629. struct typenode *tn, *next;
  1630. LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry)
  1631. {
  1632. if (type_is_equal(complete_type, tn->type))
  1633. {
  1634. tn->type->details.structure = complete_type->details.structure;
  1635. list_remove(&tn->entry);
  1636. free(tn);
  1637. }
  1638. }
  1639. }
  1640. static type_t *reg_typedefs(decl_spec_t *decl_spec, declarator_list_t *decls, attr_list_t *attrs)
  1641. {
  1642. const declarator_t *decl;
  1643. type_t *type = decl_spec->type;
  1644. /* We must generate names for tagless enum, struct or union.
  1645. Typedef-ing a tagless enum, struct or union means we want the typedef
  1646. to be included in a library hence the public attribute. */
  1647. if ((type_get_type_detect_alias(type) == TYPE_ENUM ||
  1648. type_get_type_detect_alias(type) == TYPE_STRUCT ||
  1649. type_get_type_detect_alias(type) == TYPE_UNION ||
  1650. type_get_type_detect_alias(type) == TYPE_ENCAPSULATED_UNION) &&
  1651. !type->name && !parse_only)
  1652. {
  1653. if (! is_attr(attrs, ATTR_PUBLIC))
  1654. attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
  1655. type->name = gen_name();
  1656. }
  1657. else if (is_attr(attrs, ATTR_UUID) && !is_attr(attrs, ATTR_PUBLIC))
  1658. attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
  1659. /* Append the SWITCHTYPE attribute to a non-encapsulated union if it does not already have it. */
  1660. if (type_get_type_detect_alias(type) == TYPE_UNION &&
  1661. is_attr(attrs, ATTR_SWITCHTYPE) &&
  1662. !is_attr(type->attrs, ATTR_SWITCHTYPE))
  1663. type->attrs = append_attr(type->attrs, make_attrp(ATTR_SWITCHTYPE, get_attrp(attrs, ATTR_SWITCHTYPE)));
  1664. LIST_FOR_EACH_ENTRY( decl, decls, const declarator_t, entry )
  1665. {
  1666. if (decl->var->name) {
  1667. type_t *cur;
  1668. var_t *name;
  1669. cur = find_type(decl->var->name, 0);
  1670. if (cur)
  1671. error_loc("%s: redefinition error; original definition was at %s:%d\n",
  1672. cur->name, cur->loc_info.input_name,
  1673. cur->loc_info.line_number);
  1674. name = declare_var(attrs, decl_spec, decl, 0);
  1675. cur = type_new_alias(name->type, name->name);
  1676. cur->attrs = attrs;
  1677. if (is_incomplete(cur))
  1678. add_incomplete(cur);
  1679. reg_type(cur, cur->name, 0);
  1680. }
  1681. }
  1682. return type;
  1683. }
  1684. type_t *find_type(const char *name, int t)
  1685. {
  1686. struct rtype *cur = type_hash[hash_ident(name)];
  1687. while (cur && (cur->t != t || strcmp(cur->name, name)))
  1688. cur = cur->next;
  1689. return cur ? cur->type : NULL;
  1690. }
  1691. static type_t *find_type_or_error(const char *name, int t)
  1692. {
  1693. type_t *type = find_type(name, t);
  1694. if (!type) {
  1695. error_loc("type '%s' not found\n", name);
  1696. return NULL;
  1697. }
  1698. return type;
  1699. }
  1700. static type_t *find_type_or_error2(char *name, int t)
  1701. {
  1702. type_t *tp = find_type_or_error(name, t);
  1703. free(name);
  1704. return tp;
  1705. }
  1706. int is_type(const char *name)
  1707. {
  1708. return find_type(name, 0) != NULL;
  1709. }
  1710. type_t *get_type(enum type_type type, char *name, int t)
  1711. {
  1712. type_t *tp;
  1713. if (name) {
  1714. tp = find_type(name, t);
  1715. if (tp) {
  1716. free(name);
  1717. return tp;
  1718. }
  1719. }
  1720. tp = make_type(type);
  1721. tp->name = name;
  1722. if (!name) return tp;
  1723. return reg_type(tp, name, t);
  1724. }
  1725. /***** constant repository *****/
  1726. struct rconst {
  1727. char *name;
  1728. var_t *var;
  1729. struct rconst *next;
  1730. };
  1731. struct rconst *const_hash[HASHMAX];
  1732. static var_t *reg_const(var_t *var)
  1733. {
  1734. struct rconst *nc;
  1735. int hash;
  1736. if (!var->name) {
  1737. error_loc("registering constant without name\n");
  1738. return var;
  1739. }
  1740. hash = hash_ident(var->name);
  1741. nc = xmalloc(sizeof(struct rconst));
  1742. nc->name = var->name;
  1743. nc->var = var;
  1744. nc->next = const_hash[hash];
  1745. const_hash[hash] = nc;
  1746. return var;
  1747. }
  1748. var_t *find_const(const char *name, int f)
  1749. {
  1750. struct rconst *cur = const_hash[hash_ident(name)];
  1751. while (cur && strcmp(cur->name, name))
  1752. cur = cur->next;
  1753. if (!cur) {
  1754. if (f) error_loc("constant '%s' not found\n", name);
  1755. return NULL;
  1756. }
  1757. return cur->var;
  1758. }
  1759. static char *gen_name(void)
  1760. {
  1761. static const char format[] = "__WIDL_%s_generated_name_%08lX";
  1762. static unsigned long n = 0;
  1763. static const char *file_id;
  1764. static size_t size;
  1765. char *name;
  1766. if (! file_id)
  1767. {
  1768. char *dst = dup_basename(input_name, ".idl");
  1769. file_id = dst;
  1770. for (; *dst; ++dst)
  1771. if (! isalnum((unsigned char) *dst))
  1772. *dst = '_';
  1773. size = sizeof format - 7 + strlen(file_id) + 8;
  1774. }
  1775. name = xmalloc(size);
  1776. sprintf(name, format, file_id, n++);
  1777. return name;
  1778. }
  1779. struct allowed_attr
  1780. {
  1781. unsigned int dce_compatible : 1;
  1782. unsigned int acf : 1;
  1783. unsigned int on_interface : 1;
  1784. unsigned int on_function : 1;
  1785. unsigned int on_arg : 1;
  1786. unsigned int on_type : 1;
  1787. unsigned int on_enum : 1;
  1788. unsigned int on_struct : 1;
  1789. unsigned int on_union : 1;
  1790. unsigned int on_field : 1;
  1791. unsigned int on_library : 1;
  1792. unsigned int on_dispinterface : 1;
  1793. unsigned int on_module : 1;
  1794. unsigned int on_coclass : 1;
  1795. const char *display_name;
  1796. };
  1797. struct allowed_attr allowed_attr[] =
  1798. {
  1799. /* attr { D ACF I Fn ARG T En St Un Fi L DI M C <display name> } */
  1800. /* ATTR_AGGREGATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
  1801. /* ATTR_ANNOTATION */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "annotation" },
  1802. /* ATTR_APPOBJECT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
  1803. /* ATTR_ASYNC */ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "async" },
  1804. /* ATTR_AUTO_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
  1805. /* ATTR_BINDABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "bindable" },
  1806. /* ATTR_BROADCAST */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "broadcast" },
  1807. /* ATTR_CALLAS */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "call_as" },
  1808. /* ATTR_CALLCONV */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
  1809. /* ATTR_CASE */ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
  1810. /* ATTR_CODE */ { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "code" },
  1811. /* ATTR_COMMSTATUS */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "comm_status" },
  1812. /* ATTR_CONST */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "const" },
  1813. /* ATTR_CONTEXTHANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "context_handle" },
  1814. /* ATTR_CONTROL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
  1815. /* ATTR_DECODE */ { 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "decode" },
  1816. /* ATTR_DEFAULT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, "default" },
  1817. /* ATTR_DEFAULTBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultbind" },
  1818. /* ATTR_DEFAULTCOLLELEM */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
  1819. /* ATTR_DEFAULTVALUE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultvalue" },
  1820. /* ATTR_DEFAULTVTABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
  1821. /* ATTR_DISABLECONSISTENCYCHECK */{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "disable_consistency_check" },
  1822. /* ATTR_DISPINTERFACE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
  1823. /* ATTR_DISPLAYBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
  1824. /* ATTR_DLLNAME */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
  1825. /* ATTR_DUAL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
  1826. /* ATTR_ENABLEALLOCATE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "enable_allocate" },
  1827. /* ATTR_ENCODE */ { 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "encode" },
  1828. /* ATTR_ENDPOINT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
  1829. /* ATTR_ENTRY */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "entry" },
  1830. /* ATTR_EXPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
  1831. /* ATTR_FAULTSTATUS */ { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "fault_status" },
  1832. /* ATTR_FORCEALLOCATE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "force_allocate" },
  1833. /* ATTR_HANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "handle" },
  1834. /* ATTR_HELPCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpcontext" },
  1835. /* ATTR_HELPFILE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
  1836. /* ATTR_HELPSTRING */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstring" },
  1837. /* ATTR_HELPSTRINGCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstringcontext" },
  1838. /* ATTR_HELPSTRINGDLL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
  1839. /* ATTR_HIDDEN */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, "hidden" },
  1840. /* ATTR_ID */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "id" },
  1841. /* ATTR_IDEMPOTENT */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
  1842. /* ATTR_IGNORE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "ignore" },
  1843. /* ATTR_IIDIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "iid_is" },
  1844. /* ATTR_IMMEDIATEBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
  1845. /* ATTR_IMPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
  1846. /* ATTR_IN */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "in" },
  1847. /* ATTR_INLINE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inline" },
  1848. /* ATTR_INPUTSYNC */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
  1849. /* ATTR_LENGTHIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "length_is" },
  1850. /* ATTR_LIBLCID */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "lcid" },
  1851. /* ATTR_LICENSED */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "licensed" },
  1852. /* ATTR_LOCAL */ { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "local" },
  1853. /* ATTR_MAYBE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "maybe" },
  1854. /* ATTR_MESSAGE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "message" },
  1855. /* ATTR_NOCODE */ { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nocode" },
  1856. /* ATTR_NONBROWSABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
  1857. /* ATTR_NONCREATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
  1858. /* ATTR_NONEXTENSIBLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
  1859. /* ATTR_NOTIFY */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "notify" },
  1860. /* ATTR_NOTIFYFLAG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "notify_flag" },
  1861. /* ATTR_OBJECT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
  1862. /* ATTR_ODL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
  1863. /* ATTR_OLEAUTOMATION */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
  1864. /* ATTR_OPTIMIZE */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "optimize" },
  1865. /* ATTR_OPTIONAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "optional" },
  1866. /* ATTR_OUT */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "out" },
  1867. /* ATTR_PARAMLCID */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "lcid" },
  1868. /* ATTR_PARTIALIGNORE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "partial_ignore" },
  1869. /* ATTR_POINTERDEFAULT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
  1870. /* ATTR_POINTERTYPE */ { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "ref, unique or ptr" },
  1871. /* ATTR_PROGID */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "progid" },
  1872. /* ATTR_PROPGET */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propget" },
  1873. /* ATTR_PROPPUT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propput" },
  1874. /* ATTR_PROPPUTREF */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propputref" },
  1875. /* ATTR_PROXY */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "proxy" },
  1876. /* ATTR_PUBLIC */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "public" },
  1877. /* ATTR_RANGE */ { 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, "range" },
  1878. /* ATTR_READONLY */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "readonly" },
  1879. /* ATTR_REPRESENTAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "represent_as" },
  1880. /* ATTR_REQUESTEDIT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
  1881. /* ATTR_RESTRICTED */ { 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, "restricted" },
  1882. /* ATTR_RETVAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "retval" },
  1883. /* ATTR_SIZEIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "size_is" },
  1884. /* ATTR_SOURCE */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "source" },
  1885. /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
  1886. /* ATTR_STRING */ { 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "string" },
  1887. /* ATTR_SWITCHIS */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "switch_is" },
  1888. /* ATTR_SWITCHTYPE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, "switch_type" },
  1889. /* ATTR_THREADING */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "threading" },
  1890. /* ATTR_TRANSMITAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "transmit_as" },
  1891. /* ATTR_UIDEFAULT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "uidefault" },
  1892. /* ATTR_USESGETLASTERROR */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "usesgetlasterror" },
  1893. /* ATTR_USERMARSHAL */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "user_marshal" },
  1894. /* ATTR_UUID */ { 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "uuid" },
  1895. /* ATTR_V1ENUM */ { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "v1_enum" },
  1896. /* ATTR_VARARG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "vararg" },
  1897. /* ATTR_VERSION */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "version" },
  1898. /* ATTR_VIPROGID */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "vi_progid" },
  1899. /* ATTR_WIREMARSHAL */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "wire_marshal" },
  1900. };
  1901. const char *get_attr_display_name(enum attr_type type)
  1902. {
  1903. return allowed_attr[type].display_name;
  1904. }
  1905. static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs)
  1906. {
  1907. const attr_t *attr;
  1908. if (!attrs) return attrs;
  1909. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  1910. {
  1911. if (!allowed_attr[attr->type].on_interface)
  1912. error_loc("inapplicable attribute %s for interface %s\n",
  1913. allowed_attr[attr->type].display_name, name);
  1914. if (attr->type == ATTR_IMPLICIT_HANDLE)
  1915. {
  1916. const var_t *var = attr->u.pval;
  1917. if (type_get_type( var->type) == TYPE_BASIC &&
  1918. type_basic_get_type( var->type ) == TYPE_BASIC_HANDLE)
  1919. continue;
  1920. if (is_aliaschain_attr( var->type, ATTR_HANDLE ))
  1921. continue;
  1922. error_loc("attribute %s requires a handle type in interface %s\n",
  1923. allowed_attr[attr->type].display_name, name);
  1924. }
  1925. }
  1926. return attrs;
  1927. }
  1928. static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs)
  1929. {
  1930. const attr_t *attr;
  1931. if (!attrs) return attrs;
  1932. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  1933. {
  1934. if (!allowed_attr[attr->type].on_function)
  1935. error_loc("inapplicable attribute %s for function %s\n",
  1936. allowed_attr[attr->type].display_name, name);
  1937. }
  1938. return attrs;
  1939. }
  1940. static void check_arg_attrs(const var_t *arg)
  1941. {
  1942. const attr_t *attr;
  1943. if (arg->attrs)
  1944. {
  1945. LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
  1946. {
  1947. if (!allowed_attr[attr->type].on_arg)
  1948. error_loc("inapplicable attribute %s for argument %s\n",
  1949. allowed_attr[attr->type].display_name, arg->name);
  1950. }
  1951. }
  1952. }
  1953. static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
  1954. {
  1955. const attr_t *attr;
  1956. if (!attrs) return attrs;
  1957. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  1958. {
  1959. if (!allowed_attr[attr->type].on_type)
  1960. error_loc("inapplicable attribute %s for typedef\n",
  1961. allowed_attr[attr->type].display_name);
  1962. }
  1963. return attrs;
  1964. }
  1965. static attr_list_t *check_enum_attrs(attr_list_t *attrs)
  1966. {
  1967. const attr_t *attr;
  1968. if (!attrs) return attrs;
  1969. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  1970. {
  1971. if (!allowed_attr[attr->type].on_enum)
  1972. error_loc("inapplicable attribute %s for enum\n",
  1973. allowed_attr[attr->type].display_name);
  1974. }
  1975. return attrs;
  1976. }
  1977. static attr_list_t *check_struct_attrs(attr_list_t *attrs)
  1978. {
  1979. const attr_t *attr;
  1980. if (!attrs) return attrs;
  1981. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  1982. {
  1983. if (!allowed_attr[attr->type].on_struct)
  1984. error_loc("inapplicable attribute %s for struct\n",
  1985. allowed_attr[attr->type].display_name);
  1986. }
  1987. return attrs;
  1988. }
  1989. static attr_list_t *check_union_attrs(attr_list_t *attrs)
  1990. {
  1991. const attr_t *attr;
  1992. if (!attrs) return attrs;
  1993. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  1994. {
  1995. if (!allowed_attr[attr->type].on_union)
  1996. error_loc("inapplicable attribute %s for union\n",
  1997. allowed_attr[attr->type].display_name);
  1998. }
  1999. return attrs;
  2000. }
  2001. static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
  2002. {
  2003. const attr_t *attr;
  2004. if (!attrs) return attrs;
  2005. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  2006. {
  2007. if (!allowed_attr[attr->type].on_field)
  2008. error_loc("inapplicable attribute %s for field %s\n",
  2009. allowed_attr[attr->type].display_name, name);
  2010. }
  2011. return attrs;
  2012. }
  2013. static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs)
  2014. {
  2015. const attr_t *attr;
  2016. if (!attrs) return attrs;
  2017. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  2018. {
  2019. if (!allowed_attr[attr->type].on_library)
  2020. error_loc("inapplicable attribute %s for library %s\n",
  2021. allowed_attr[attr->type].display_name, name);
  2022. }
  2023. return attrs;
  2024. }
  2025. static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
  2026. {
  2027. const attr_t *attr;
  2028. if (!attrs) return attrs;
  2029. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  2030. {
  2031. if (!allowed_attr[attr->type].on_dispinterface)
  2032. error_loc("inapplicable attribute %s for dispinterface %s\n",
  2033. allowed_attr[attr->type].display_name, name);
  2034. }
  2035. return attrs;
  2036. }
  2037. static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs)
  2038. {
  2039. const attr_t *attr;
  2040. if (!attrs) return attrs;
  2041. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  2042. {
  2043. if (!allowed_attr[attr->type].on_module)
  2044. error_loc("inapplicable attribute %s for module %s\n",
  2045. allowed_attr[attr->type].display_name, name);
  2046. }
  2047. return attrs;
  2048. }
  2049. static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs)
  2050. {
  2051. const attr_t *attr;
  2052. if (!attrs) return attrs;
  2053. LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
  2054. {
  2055. if (!allowed_attr[attr->type].on_coclass)
  2056. error_loc("inapplicable attribute %s for coclass %s\n",
  2057. allowed_attr[attr->type].display_name, name);
  2058. }
  2059. return attrs;
  2060. }
  2061. static int is_allowed_conf_type(const type_t *type)
  2062. {
  2063. switch (type_get_type(type))
  2064. {
  2065. case TYPE_ENUM:
  2066. return TRUE;
  2067. case TYPE_BASIC:
  2068. switch (type_basic_get_type(type))
  2069. {
  2070. case TYPE_BASIC_INT8:
  2071. case TYPE_BASIC_INT16:
  2072. case TYPE_BASIC_INT32:
  2073. case TYPE_BASIC_INT64:
  2074. case TYPE_BASIC_INT:
  2075. case TYPE_BASIC_CHAR:
  2076. case TYPE_BASIC_HYPER:
  2077. case TYPE_BASIC_BYTE:
  2078. case TYPE_BASIC_WCHAR:
  2079. return TRUE;
  2080. default:
  2081. return FALSE;
  2082. }
  2083. case TYPE_ALIAS:
  2084. /* shouldn't get here because of type_get_type call above */
  2085. assert(0);
  2086. /* fall through */
  2087. case TYPE_STRUCT:
  2088. case TYPE_UNION:
  2089. case TYPE_ENCAPSULATED_UNION:
  2090. case TYPE_ARRAY:
  2091. case TYPE_POINTER:
  2092. case TYPE_VOID:
  2093. case TYPE_MODULE:
  2094. case TYPE_COCLASS:
  2095. case TYPE_FUNCTION:
  2096. case TYPE_INTERFACE:
  2097. case TYPE_BITFIELD:
  2098. return FALSE;
  2099. }
  2100. return FALSE;
  2101. }
  2102. static int is_ptr_guid_type(const type_t *type)
  2103. {
  2104. /* first, make sure it is a pointer to something */
  2105. if (!is_ptr(type)) return FALSE;
  2106. /* second, make sure it is a pointer to something of size sizeof(GUID),
  2107. * i.e. 16 bytes */
  2108. return (type_memsize(type_pointer_get_ref(type)) == 16);
  2109. }
  2110. static void check_conformance_expr_list(const char *attr_name, const var_t *arg, const type_t *container_type, expr_list_t *expr_list)
  2111. {
  2112. expr_t *dim;
  2113. struct expr_loc expr_loc;
  2114. expr_loc.v = arg;
  2115. expr_loc.attr = attr_name;
  2116. if (expr_list) LIST_FOR_EACH_ENTRY(dim, expr_list, expr_t, entry)
  2117. {
  2118. if (dim->type != EXPR_VOID)
  2119. {
  2120. const type_t *expr_type = expr_resolve_type(&expr_loc, container_type, dim);
  2121. if (!is_allowed_conf_type(expr_type))
  2122. error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
  2123. attr_name);
  2124. }
  2125. }
  2126. }
  2127. static void check_remoting_fields(const var_t *var, type_t *type);
  2128. /* checks that properties common to fields and arguments are consistent */
  2129. static void check_field_common(const type_t *container_type,
  2130. const char *container_name, const var_t *arg)
  2131. {
  2132. type_t *type = arg->type;
  2133. int more_to_do;
  2134. const char *container_type_name;
  2135. const char *var_type;
  2136. switch (type_get_type(container_type))
  2137. {
  2138. case TYPE_STRUCT:
  2139. container_type_name = "struct";
  2140. var_type = "field";
  2141. break;
  2142. case TYPE_UNION:
  2143. container_type_name = "union";
  2144. var_type = "arm";
  2145. break;
  2146. case TYPE_ENCAPSULATED_UNION:
  2147. container_type_name = "encapsulated union";
  2148. var_type = "arm";
  2149. break;
  2150. case TYPE_FUNCTION:
  2151. container_type_name = "function";
  2152. var_type = "parameter";
  2153. break;
  2154. default:
  2155. /* should be no other container types */
  2156. assert(0);
  2157. return;
  2158. }
  2159. if (is_attr(arg->attrs, ATTR_LENGTHIS) &&
  2160. (is_attr(arg->attrs, ATTR_STRING) || is_aliaschain_attr(arg->type, ATTR_STRING)))
  2161. error_loc_info(&arg->loc_info,
  2162. "string and length_is specified for argument %s are mutually exclusive attributes\n",
  2163. arg->name);
  2164. if (is_attr(arg->attrs, ATTR_SIZEIS))
  2165. {
  2166. expr_list_t *size_is_exprs = get_attrp(arg->attrs, ATTR_SIZEIS);
  2167. check_conformance_expr_list("size_is", arg, container_type, size_is_exprs);
  2168. }
  2169. if (is_attr(arg->attrs, ATTR_LENGTHIS))
  2170. {
  2171. expr_list_t *length_is_exprs = get_attrp(arg->attrs, ATTR_LENGTHIS);
  2172. check_conformance_expr_list("length_is", arg, container_type, length_is_exprs);
  2173. }
  2174. if (is_attr(arg->attrs, ATTR_IIDIS))
  2175. {
  2176. struct expr_loc expr_loc;
  2177. expr_t *expr = get_attrp(arg->attrs, ATTR_IIDIS);
  2178. if (expr->type != EXPR_VOID)
  2179. {
  2180. const type_t *expr_type;
  2181. expr_loc.v = arg;
  2182. expr_loc.attr = "iid_is";
  2183. expr_type = expr_resolve_type(&expr_loc, container_type, expr);
  2184. if (!expr_type || !is_ptr_guid_type(expr_type))
  2185. error_loc_info(&arg->loc_info, "expression must resolve to pointer to GUID type for attribute iid_is\n");
  2186. }
  2187. }
  2188. if (is_attr(arg->attrs, ATTR_SWITCHIS))
  2189. {
  2190. struct expr_loc expr_loc;
  2191. expr_t *expr = get_attrp(arg->attrs, ATTR_SWITCHIS);
  2192. if (expr->type != EXPR_VOID)
  2193. {
  2194. const type_t *expr_type;
  2195. expr_loc.v = arg;
  2196. expr_loc.attr = "switch_is";
  2197. expr_type = expr_resolve_type(&expr_loc, container_type, expr);
  2198. if (!expr_type || !is_allowed_conf_type(expr_type))
  2199. error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
  2200. expr_loc.attr);
  2201. }
  2202. }
  2203. do
  2204. {
  2205. more_to_do = FALSE;
  2206. switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
  2207. {
  2208. case TGT_STRUCT:
  2209. case TGT_UNION:
  2210. check_remoting_fields(arg, type);
  2211. break;
  2212. case TGT_INVALID:
  2213. {
  2214. const char *reason = "is invalid";
  2215. switch (type_get_type(type))
  2216. {
  2217. case TYPE_VOID:
  2218. reason = "cannot derive from void *";
  2219. break;
  2220. case TYPE_FUNCTION:
  2221. reason = "cannot be a function pointer";
  2222. break;
  2223. case TYPE_BITFIELD:
  2224. reason = "cannot be a bit-field";
  2225. break;
  2226. case TYPE_COCLASS:
  2227. reason = "cannot be a class";
  2228. break;
  2229. case TYPE_INTERFACE:
  2230. reason = "cannot be a non-pointer to an interface";
  2231. break;
  2232. case TYPE_MODULE:
  2233. reason = "cannot be a module";
  2234. break;
  2235. default:
  2236. break;
  2237. }
  2238. error_loc_info(&arg->loc_info, "%s \'%s\' of %s \'%s\' %s\n",
  2239. var_type, arg->name, container_type_name, container_name, reason);
  2240. break;
  2241. }
  2242. case TGT_CTXT_HANDLE:
  2243. case TGT_CTXT_HANDLE_POINTER:
  2244. if (type_get_type(container_type) != TYPE_FUNCTION)
  2245. error_loc_info(&arg->loc_info,
  2246. "%s \'%s\' of %s \'%s\' cannot be a context handle\n",
  2247. var_type, arg->name, container_type_name,
  2248. container_name);
  2249. break;
  2250. case TGT_STRING:
  2251. {
  2252. const type_t *t = type;
  2253. while (is_ptr(t))
  2254. t = type_pointer_get_ref(t);
  2255. if (is_aliaschain_attr(t, ATTR_RANGE))
  2256. warning_loc_info(&arg->loc_info, "%s: range not verified for a string of ranged types\n", arg->name);
  2257. break;
  2258. }
  2259. case TGT_POINTER:
  2260. type = type_pointer_get_ref(type);
  2261. more_to_do = TRUE;
  2262. break;
  2263. case TGT_ARRAY:
  2264. type = type_array_get_element(type);
  2265. more_to_do = TRUE;
  2266. break;
  2267. case TGT_USER_TYPE:
  2268. case TGT_IFACE_POINTER:
  2269. case TGT_BASIC:
  2270. case TGT_ENUM:
  2271. case TGT_RANGE:
  2272. /* nothing to do */
  2273. break;
  2274. }
  2275. } while (more_to_do);
  2276. }
  2277. static void check_remoting_fields(const var_t *var, type_t *type)
  2278. {
  2279. const var_t *field;
  2280. const var_list_t *fields = NULL;
  2281. type = type_get_real_type(type);
  2282. if (type->checked)
  2283. return;
  2284. type->checked = TRUE;
  2285. if (type_get_type(type) == TYPE_STRUCT)
  2286. {
  2287. if (type_is_complete(type))
  2288. fields = type_struct_get_fields(type);
  2289. else
  2290. error_loc_info(&var->loc_info, "undefined type declaration %s\n", type->name);
  2291. }
  2292. else if (type_get_type(type) == TYPE_UNION || type_get_type(type) == TYPE_ENCAPSULATED_UNION)
  2293. fields = type_union_get_cases(type);
  2294. if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
  2295. if (field->type) check_field_common(type, type->name, field);
  2296. }
  2297. /* checks that arguments for a function make sense for marshalling and unmarshalling */
  2298. static void check_remoting_args(const var_t *func)
  2299. {
  2300. const char *funcname = func->name;
  2301. const var_t *arg;
  2302. if (func->type->details.function->args) LIST_FOR_EACH_ENTRY( arg, func->type->details.function->args, const var_t, entry )
  2303. {
  2304. const type_t *type = arg->type;
  2305. /* check that [out] parameters have enough pointer levels */
  2306. if (is_attr(arg->attrs, ATTR_OUT))
  2307. {
  2308. switch (typegen_detect_type(type, arg->attrs, TDT_ALL_TYPES))
  2309. {
  2310. case TGT_BASIC:
  2311. case TGT_ENUM:
  2312. case TGT_RANGE:
  2313. case TGT_STRUCT:
  2314. case TGT_UNION:
  2315. case TGT_CTXT_HANDLE:
  2316. case TGT_USER_TYPE:
  2317. error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' is not a pointer\n", arg->name, funcname);
  2318. break;
  2319. case TGT_IFACE_POINTER:
  2320. error_loc_info(&arg->loc_info, "out interface pointer \'%s\' of function \'%s\' is not a double pointer\n", arg->name, funcname);
  2321. break;
  2322. case TGT_STRING:
  2323. if (is_array(type))
  2324. {
  2325. /* needs conformance or fixed dimension */
  2326. if (type_array_has_conformance(type) &&
  2327. type_array_get_conformance(type)->type != EXPR_VOID) break;
  2328. if (!type_array_has_conformance(type) && type_array_get_dim(type)) break;
  2329. }
  2330. if (is_attr( arg->attrs, ATTR_IN )) break;
  2331. error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' cannot be an unsized string\n", arg->name, funcname);
  2332. break;
  2333. case TGT_INVALID:
  2334. /* already error'd before we get here */
  2335. case TGT_CTXT_HANDLE_POINTER:
  2336. case TGT_POINTER:
  2337. case TGT_ARRAY:
  2338. /* OK */
  2339. break;
  2340. }
  2341. }
  2342. check_field_common(func->type, funcname, arg);
  2343. }
  2344. if (type_get_type(type_function_get_rettype(func->type)) != TYPE_VOID)
  2345. {
  2346. var_t var;
  2347. var = *func;
  2348. var.type = type_function_get_rettype(func->type);
  2349. var.name = xstrdup("return value");
  2350. check_field_common(func->type, funcname, &var);
  2351. free(var.name);
  2352. }
  2353. }
  2354. static void add_explicit_handle_if_necessary(const type_t *iface, var_t *func)
  2355. {
  2356. unsigned char explicit_fc, implicit_fc;
  2357. /* check for a defined binding handle */
  2358. if (!get_func_handle_var( iface, func, &explicit_fc, &implicit_fc ) || !explicit_fc)
  2359. {
  2360. /* no explicit handle specified so add
  2361. * "[in] handle_t IDL_handle" as the first parameter to the
  2362. * function */
  2363. var_t *idl_handle = make_var(xstrdup("IDL_handle"));
  2364. idl_handle->attrs = append_attr(NULL, make_attr(ATTR_IN));
  2365. idl_handle->type = find_type_or_error("handle_t", 0);
  2366. type_function_add_head_arg(func->type, idl_handle);
  2367. }
  2368. }
  2369. static void check_functions(const type_t *iface, int is_inside_library)
  2370. {
  2371. const statement_t *stmt;
  2372. if (is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE))
  2373. {
  2374. STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
  2375. {
  2376. var_t *func = stmt->u.var;
  2377. add_explicit_handle_if_necessary(iface, func);
  2378. }
  2379. }
  2380. if (!is_inside_library && !is_attr(iface->attrs, ATTR_LOCAL))
  2381. {
  2382. STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
  2383. {
  2384. const var_t *func = stmt->u.var;
  2385. if (!is_attr(func->attrs, ATTR_LOCAL))
  2386. check_remoting_args(func);
  2387. }
  2388. }
  2389. }
  2390. static void check_statements(const statement_list_t *stmts, int is_inside_library)
  2391. {
  2392. const statement_t *stmt;
  2393. if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
  2394. {
  2395. if (stmt->type == STMT_LIBRARY)
  2396. check_statements(stmt->u.lib->stmts, TRUE);
  2397. else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
  2398. check_functions(stmt->u.type, is_inside_library);
  2399. }
  2400. }
  2401. static void check_all_user_types(const statement_list_t *stmts)
  2402. {
  2403. const statement_t *stmt;
  2404. if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
  2405. {
  2406. if (stmt->type == STMT_LIBRARY)
  2407. check_all_user_types(stmt->u.lib->stmts);
  2408. else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE &&
  2409. !is_local(stmt->u.type->attrs))
  2410. {
  2411. const statement_t *stmt_func;
  2412. STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type)) {
  2413. const var_t *func = stmt_func->u.var;
  2414. check_for_additional_prototype_types(func->type->details.function->args);
  2415. }
  2416. }
  2417. }
  2418. }
  2419. int is_valid_uuid(const char *s)
  2420. {
  2421. int i;
  2422. for (i = 0; i < 36; ++i)
  2423. if (i == 8 || i == 13 || i == 18 || i == 23)
  2424. {
  2425. if (s[i] != '-')
  2426. return FALSE;
  2427. }
  2428. else
  2429. if (!isxdigit(s[i]))
  2430. return FALSE;
  2431. return s[i] == '\0';
  2432. }
  2433. static statement_t *make_statement(enum statement_type type)
  2434. {
  2435. statement_t *stmt = xmalloc(sizeof(*stmt));
  2436. stmt->type = type;
  2437. return stmt;
  2438. }
  2439. static statement_t *make_statement_type_decl(type_t *type)
  2440. {
  2441. statement_t *stmt = make_statement(STMT_TYPE);
  2442. stmt->u.type = type;
  2443. return stmt;
  2444. }
  2445. static statement_t *make_statement_reference(type_t *type)
  2446. {
  2447. statement_t *stmt = make_statement(STMT_TYPEREF);
  2448. stmt->u.type = type;
  2449. return stmt;
  2450. }
  2451. static statement_t *make_statement_declaration(var_t *var)
  2452. {
  2453. statement_t *stmt = make_statement(STMT_DECLARATION);
  2454. stmt->u.var = var;
  2455. if (var->stgclass == STG_EXTERN && var->eval)
  2456. warning("'%s' initialised and declared extern\n", var->name);
  2457. if (is_const_decl(var))
  2458. {
  2459. if (var->eval)
  2460. reg_const(var);
  2461. }
  2462. else if (type_get_type(var->type) == TYPE_FUNCTION)
  2463. check_function_attrs(var->name, var->attrs);
  2464. else if (var->stgclass == STG_NONE || var->stgclass == STG_REGISTER)
  2465. error_loc("instantiation of data is illegal\n");
  2466. return stmt;
  2467. }
  2468. static statement_t *make_statement_library(typelib_t *typelib)
  2469. {
  2470. statement_t *stmt = make_statement(STMT_LIBRARY);
  2471. stmt->u.lib = typelib;
  2472. return stmt;
  2473. }
  2474. static statement_t *make_statement_cppquote(const char *str)
  2475. {
  2476. statement_t *stmt = make_statement(STMT_CPPQUOTE);
  2477. stmt->u.str = str;
  2478. return stmt;
  2479. }
  2480. static statement_t *make_statement_importlib(const char *str)
  2481. {
  2482. statement_t *stmt = make_statement(STMT_IMPORTLIB);
  2483. stmt->u.str = str;
  2484. return stmt;
  2485. }
  2486. static statement_t *make_statement_import(const char *str)
  2487. {
  2488. statement_t *stmt = make_statement(STMT_IMPORT);
  2489. stmt->u.str = str;
  2490. return stmt;
  2491. }
  2492. static statement_t *make_statement_module(type_t *type)
  2493. {
  2494. statement_t *stmt = make_statement(STMT_MODULE);
  2495. stmt->u.type = type;
  2496. return stmt;
  2497. }
  2498. static statement_t *make_statement_typedef(declarator_list_t *decls)
  2499. {
  2500. declarator_t *decl, *next;
  2501. statement_t *stmt;
  2502. type_list_t **type_list;
  2503. if (!decls) return NULL;
  2504. stmt = make_statement(STMT_TYPEDEF);
  2505. stmt->u.type_list = NULL;
  2506. type_list = &stmt->u.type_list;
  2507. LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
  2508. {
  2509. var_t *var = decl->var;
  2510. type_t *type = find_type_or_error(var->name, 0);
  2511. *type_list = xmalloc(sizeof(type_list_t));
  2512. (*type_list)->type = type;
  2513. (*type_list)->next = NULL;
  2514. type_list = &(*type_list)->next;
  2515. free(decl);
  2516. free(var);
  2517. }
  2518. return stmt;
  2519. }
  2520. static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt)
  2521. {
  2522. if (!stmt) return list;
  2523. if (!list)
  2524. {
  2525. list = xmalloc( sizeof(*list) );
  2526. list_init( list );
  2527. }
  2528. list_add_tail( list, &stmt->entry );
  2529. return list;
  2530. }
  2531. void init_loc_info(loc_info_t *i)
  2532. {
  2533. i->input_name = input_name ? input_name : "stdin";
  2534. i->line_number = line_number;
  2535. i->near_text = parser_text;
  2536. }
  2537. static void check_def(const type_t *t)
  2538. {
  2539. if (t->defined)
  2540. error_loc("%s: redefinition error; original definition was at %s:%d\n",
  2541. t->name, t->loc_info.input_name, t->loc_info.line_number);
  2542. }