pdecvar.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Parses variable declarations. Used for var statement and record
  5. definitions
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit pdecvar;
  20. {$i defines.inc}
  21. interface
  22. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  23. implementation
  24. uses
  25. { common }
  26. cutils,
  27. { global }
  28. globtype,globals,tokens,verbose,
  29. systems,
  30. { symtable }
  31. symconst,symbase,symtype,symdef,symsym,symtable,types,fmodule,
  32. { pass 1 }
  33. node,
  34. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  35. { parser }
  36. scanner,
  37. pbase,pexpr,ptype,ptconst,pdecsub,
  38. { link }
  39. import;
  40. const
  41. variantrecordlevel : longint = 0;
  42. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  43. { reads the filed of a record into a }
  44. { symtablestack, if record=false }
  45. { variants are forbidden, so this procedure }
  46. { can be used to read object fields }
  47. { if absolute is true, ABSOLUTE and file }
  48. { types are allowed }
  49. { => the procedure is also used to read }
  50. { a sequence of variable declaration }
  51. procedure insert_syms(st : tsymtable;sc : tidstringlist;tt : ttype;is_threadvar : boolean);
  52. { inserts the symbols of sc in st with def as definition or sym as ttypesym, sc is disposed }
  53. var
  54. s : string;
  55. filepos : tfileposinfo;
  56. ss : tvarsym;
  57. begin
  58. filepos:=akttokenpos;
  59. while not sc.empty do
  60. begin
  61. s:=sc.get(akttokenpos);
  62. ss:=tvarsym.Create(s,tt);
  63. if is_threadvar then
  64. include(ss.varoptions,vo_is_thread_var);
  65. st.insert(ss);
  66. { static data fields are inserted in the globalsymtable }
  67. if (st.symtabletype=objectsymtable) and
  68. (sp_static in current_object_option) then
  69. begin
  70. s:='$'+lower(st.name^)+'_'+upper(s);
  71. st.defowner.owner.insert(tvarsym.create(s,tt));
  72. end;
  73. end;
  74. {$ifdef fixLeaksOnError}
  75. if strContStack.pop <> sc then
  76. writeln('problem with strContStack in pdecl (2)');
  77. {$endif fixLeaksOnError}
  78. sc.free;
  79. akttokenpos:=filepos;
  80. end;
  81. var
  82. sc : tidstringList;
  83. s : stringid;
  84. old_block_type : tblock_type;
  85. declarepos,storetokenpos : tfileposinfo;
  86. oldsymtablestack : tsymtable;
  87. symdone : boolean;
  88. { to handle absolute }
  89. abssym : tabsolutesym;
  90. l : longint;
  91. code : integer;
  92. { c var }
  93. newtype : ttypesym;
  94. is_dll,
  95. is_gpc_name,is_cdecl,extern_aktvarsym,export_aktvarsym : boolean;
  96. old_current_object_option : tsymoptions;
  97. dll_name,
  98. C_name : string;
  99. tt,casetype : ttype;
  100. { Delphi initialized vars }
  101. tconstsym : ttypedconstsym;
  102. { maxsize contains the max. size of a variant }
  103. { startvarrec contains the start of the variant part of a record }
  104. maxsize,maxalignment,startvarrecalign,startvarrecsize : longint;
  105. pt : tnode;
  106. srsym : tsym;
  107. srsymtable : tsymtable;
  108. unionsymtable : tsymtable;
  109. offset : longint;
  110. uniondef : trecorddef;
  111. unionsym : tvarsym;
  112. uniontype : ttype;
  113. dummysymoptions : tsymoptions;
  114. begin
  115. old_current_object_option:=current_object_option;
  116. { all variables are public if not in a object declaration }
  117. if not is_object then
  118. current_object_option:=[sp_public];
  119. old_block_type:=block_type;
  120. block_type:=bt_type;
  121. is_gpc_name:=false;
  122. { Force an expected ID error message }
  123. if not (token in [_ID,_CASE,_END]) then
  124. consume(_ID);
  125. { read vars }
  126. while (token=_ID) and
  127. not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
  128. begin
  129. C_name:=orgpattern;
  130. sc:=consume_idlist;
  131. {$ifdef fixLeaksOnError}
  132. strContStack.push(sc);
  133. {$endif fixLeaksOnError}
  134. consume(_COLON);
  135. if (m_gpc in aktmodeswitches) and
  136. not(is_record or is_object or is_threadvar) and
  137. (token=_ID) and (orgpattern='__asmname__') then
  138. begin
  139. consume(_ID);
  140. C_name:=get_stringconst;
  141. Is_gpc_name:=true;
  142. end;
  143. { this is needed for Delphi mode at least
  144. but should be OK for all modes !! (PM) }
  145. ignore_equal:=true;
  146. if is_record then
  147. begin
  148. { for records, don't search the recordsymtable for
  149. the symbols of the types }
  150. oldsymtablestack:=symtablestack;
  151. symtablestack:=symtablestack.next;
  152. read_type(tt,'');
  153. symtablestack:=oldsymtablestack;
  154. end
  155. else
  156. read_type(tt,'');
  157. if (variantrecordlevel>0) and tt.def.needs_inittable then
  158. Message(parser_e_cant_use_inittable_here);
  159. ignore_equal:=false;
  160. symdone:=false;
  161. if is_gpc_name then
  162. begin
  163. storetokenpos:=akttokenpos;
  164. s:=sc.get(akttokenpos);
  165. if not sc.empty then
  166. Message(parser_e_absolute_only_one_var);
  167. {$ifdef fixLeaksOnError}
  168. if strContStack.pop <> sc then
  169. writeln('problem with strContStack in pdecl (3)');
  170. {$endif fixLeaksOnError}
  171. sc.free;
  172. aktvarsym:=tvarsym.create_C(s,target_info.Cprefix+C_name,tt);
  173. include(aktvarsym.varoptions,vo_is_external);
  174. symtablestack.insert(aktvarsym);
  175. akttokenpos:=storetokenpos;
  176. symdone:=true;
  177. end;
  178. { check for absolute }
  179. if not symdone and
  180. (idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
  181. begin
  182. consume(_ABSOLUTE);
  183. { only allowed for one var }
  184. s:=sc.get(declarepos);
  185. if not sc.empty then
  186. Message(parser_e_absolute_only_one_var);
  187. {$ifdef fixLeaksOnError}
  188. if strContStack.pop <> sc then
  189. writeln('problem with strContStack in pdecl (4)');
  190. {$endif fixLeaksOnError}
  191. sc.free;
  192. { parse the rest }
  193. if token=_ID then
  194. begin
  195. consume_sym(srsym,srsymtable);
  196. { we should check the result type of srsym }
  197. if not (srsym.typ in [varsym,typedconstsym,funcretsym]) then
  198. Message(parser_e_absolute_only_to_var_or_const);
  199. storetokenpos:=akttokenpos;
  200. akttokenpos:=declarepos;
  201. abssym:=tabsolutesym.create(s,tt);
  202. abssym.abstyp:=tovar;
  203. abssym.ref:=tstoredsym(srsym);
  204. symtablestack.insert(abssym);
  205. akttokenpos:=storetokenpos;
  206. end
  207. else
  208. if (token=_CSTRING) or (token=_CCHAR) then
  209. begin
  210. storetokenpos:=akttokenpos;
  211. akttokenpos:=declarepos;
  212. abssym:=tabsolutesym.create(s,tt);
  213. s:=pattern;
  214. consume(token);
  215. abssym.abstyp:=toasm;
  216. abssym.asmname:=stringdup(s);
  217. symtablestack.insert(abssym);
  218. akttokenpos:=storetokenpos;
  219. end
  220. else
  221. { absolute address ?!? }
  222. if token=_INTCONST then
  223. begin
  224. if (target_info.target=target_i386_go32v2)
  225. or (m_objfpc in aktmodeswitches)
  226. or (m_delphi in aktmodeswitches) then
  227. begin
  228. storetokenpos:=akttokenpos;
  229. akttokenpos:=declarepos;
  230. abssym:=tabsolutesym.create(s,tt);
  231. abssym.abstyp:=toaddr;
  232. abssym.absseg:=false;
  233. s:=pattern;
  234. consume(_INTCONST);
  235. val(s,abssym.address,code);
  236. if (token=_COLON) and
  237. (target_info.target=target_i386_go32v2) then
  238. begin
  239. consume(token);
  240. s:=pattern;
  241. consume(_INTCONST);
  242. val(s,l,code);
  243. abssym.address:=abssym.address shl 4+l;
  244. abssym.absseg:=true;
  245. end;
  246. symtablestack.insert(abssym);
  247. akttokenpos:=storetokenpos;
  248. end
  249. else
  250. Message(parser_e_absolute_only_to_var_or_const);
  251. end
  252. else
  253. Message(parser_e_absolute_only_to_var_or_const);
  254. symdone:=true;
  255. end;
  256. { Handling of Delphi typed const = initialized vars ! }
  257. { When should this be rejected ?
  258. - in parasymtable
  259. - in record or object
  260. - ... (PM) }
  261. if (m_delphi in aktmodeswitches) and (token=_EQUAL) and
  262. not (symtablestack.symtabletype in [parasymtable]) and
  263. not is_record and not is_object then
  264. begin
  265. storetokenpos:=akttokenpos;
  266. s:=sc.get(akttokenpos);
  267. if not sc.empty then
  268. Message(parser_e_initialized_only_one_var);
  269. tconstsym:=ttypedconstsym.createtype(s,tt,false);
  270. symtablestack.insert(tconstsym);
  271. akttokenpos:=storetokenpos;
  272. consume(_EQUAL);
  273. readtypedconst(tt,tconstsym,false);
  274. symdone:=true;
  275. end;
  276. { hint directive }
  277. {$warning hintdirective not stored in syms}
  278. dummysymoptions:=[];
  279. try_consume_hintdirective(dummysymoptions);
  280. { for a record there doesn't need to be a ; before the END or ) }
  281. if not((is_record or is_object) and (token in [_END,_RKLAMMER])) then
  282. consume(_SEMICOLON);
  283. { procvar handling }
  284. if (tt.def.deftype=procvardef) and (tt.def.typesym=nil) then
  285. begin
  286. newtype:=ttypesym.create('unnamed',tt);
  287. parse_var_proc_directives(tsym(newtype));
  288. newtype.restype.def:=nil;
  289. tt.def.typesym:=nil;
  290. newtype.free;
  291. end;
  292. { Check for variable directives }
  293. if not symdone and (token=_ID) then
  294. begin
  295. { Check for C Variable declarations }
  296. if (m_cvar_support in aktmodeswitches) and
  297. not(is_record or is_object or is_threadvar) and
  298. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  299. begin
  300. { only allowed for one var }
  301. s:=sc.get(declarepos);
  302. if not sc.empty then
  303. Message(parser_e_absolute_only_one_var);
  304. {$ifdef fixLeaksOnError}
  305. if strContStack.pop <> sc then
  306. writeln('problem with strContStack in pdecl (5)');
  307. {$endif fixLeaksOnError}
  308. sc.free;
  309. { defaults }
  310. is_dll:=false;
  311. is_cdecl:=false;
  312. extern_aktvarsym:=false;
  313. export_aktvarsym:=false;
  314. { cdecl }
  315. if idtoken=_CVAR then
  316. begin
  317. consume(_CVAR);
  318. consume(_SEMICOLON);
  319. is_cdecl:=true;
  320. C_name:=target_info.Cprefix+C_name;
  321. end;
  322. { external }
  323. if idtoken=_EXTERNAL then
  324. begin
  325. consume(_EXTERNAL);
  326. extern_aktvarsym:=true;
  327. end;
  328. { export }
  329. if idtoken in [_EXPORT,_PUBLIC] then
  330. begin
  331. consume(_ID);
  332. if extern_aktvarsym or
  333. (symtablestack.symtabletype in [parasymtable,localsymtable]) then
  334. Message(parser_e_not_external_and_export)
  335. else
  336. export_aktvarsym:=true;
  337. end;
  338. { external and export need a name after when no cdecl is used }
  339. if not is_cdecl then
  340. begin
  341. { dll name ? }
  342. if (extern_aktvarsym) and (idtoken<>_NAME) then
  343. begin
  344. is_dll:=true;
  345. dll_name:=get_stringconst;
  346. end;
  347. consume(_NAME);
  348. C_name:=get_stringconst;
  349. end;
  350. { consume the ; when export or external is used }
  351. if extern_aktvarsym or export_aktvarsym then
  352. consume(_SEMICOLON);
  353. { insert in the symtable }
  354. storetokenpos:=akttokenpos;
  355. akttokenpos:=declarepos;
  356. if is_dll then
  357. aktvarsym:=tvarsym.create_dll(s,tt)
  358. else
  359. aktvarsym:=tvarsym.create_C(s,C_name,tt);
  360. { set some vars options }
  361. if export_aktvarsym then
  362. begin
  363. inc(aktvarsym.refs);
  364. include(aktvarsym.varoptions,vo_is_exported);
  365. end;
  366. if extern_aktvarsym then
  367. include(aktvarsym.varoptions,vo_is_external);
  368. { insert in the stack/datasegment }
  369. symtablestack.insert(aktvarsym);
  370. akttokenpos:=storetokenpos;
  371. { now we can insert it in the import lib if its a dll, or
  372. add it to the externals }
  373. if extern_aktvarsym then
  374. begin
  375. if is_dll then
  376. begin
  377. if not(current_module.uses_imports) then
  378. begin
  379. current_module.uses_imports:=true;
  380. importlib.preparelib(current_module.modulename^);
  381. end;
  382. importlib.importvariable(aktvarsym.mangledname,dll_name,C_name)
  383. end
  384. else
  385. if target_info.DllScanSupported then
  386. current_module.Externals.insert(tExternalsItem.create(aktvarsym.mangledname));
  387. end;
  388. symdone:=true;
  389. end
  390. else
  391. if (is_object) and (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  392. begin
  393. include(current_object_option,sp_static);
  394. insert_syms(symtablestack,sc,tt,false);
  395. exclude(current_object_option,sp_static);
  396. consume(_STATIC);
  397. consume(_SEMICOLON);
  398. symdone:=true;
  399. end;
  400. end;
  401. { insert it in the symtable, if not done yet }
  402. if not symdone then
  403. begin
  404. { save object option, because we can turn of the sp_published }
  405. if (sp_published in current_object_option) and
  406. not(is_class(tt.def)) then
  407. begin
  408. Message(parser_e_cant_publish_that);
  409. exclude(current_object_option,sp_published);
  410. end
  411. else
  412. if (sp_published in current_object_option) and
  413. not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
  414. begin
  415. Message(parser_e_only_publishable_classes_can__be_published);
  416. exclude(current_object_option,sp_published);
  417. end;
  418. insert_syms(symtablestack,sc,tt,is_threadvar);
  419. current_object_option:=old_current_object_option;
  420. end;
  421. end;
  422. { Check for Case }
  423. if is_record and (token=_CASE) then
  424. begin
  425. maxsize:=0;
  426. maxalignment:=0;
  427. consume(_CASE);
  428. s:=pattern;
  429. searchsym(s,srsym,srsymtable);
  430. { may be only a type: }
  431. if assigned(srsym) and (srsym.typ in [typesym,unitsym]) then
  432. begin
  433. { for records, don't search the recordsymtable for
  434. the symbols of the types }
  435. oldsymtablestack:=symtablestack;
  436. symtablestack:=symtablestack.next;
  437. read_type(casetype,'');
  438. symtablestack:=oldsymtablestack;
  439. end
  440. else
  441. begin
  442. consume(_ID);
  443. consume(_COLON);
  444. { for records, don't search the recordsymtable for
  445. the symbols of the types }
  446. oldsymtablestack:=symtablestack;
  447. symtablestack:=symtablestack.next;
  448. read_type(casetype,'');
  449. symtablestack:=oldsymtablestack;
  450. symtablestack.insert(tvarsym.create(s,casetype));
  451. end;
  452. if not(is_ordinal(casetype.def)) or is_64bitint(casetype.def) then
  453. Message(type_e_ordinal_expr_expected);
  454. consume(_OF);
  455. UnionSymtable:=trecordsymtable.create;
  456. Unionsymtable.next:=symtablestack;
  457. registerdef:=false;
  458. UnionDef:=trecorddef.create(unionsymtable);
  459. registerdef:=true;
  460. symtablestack:=UnionSymtable;
  461. startvarrecsize:=symtablestack.datasize;
  462. startvarrecalign:=symtablestack.dataalignment;
  463. repeat
  464. repeat
  465. pt:=comp_expr(true);
  466. if not(pt.nodetype=ordconstn) then
  467. Message(cg_e_illegal_expression);
  468. pt.free;
  469. if token=_COMMA then
  470. consume(_COMMA)
  471. else
  472. break;
  473. until false;
  474. consume(_COLON);
  475. { read the vars }
  476. consume(_LKLAMMER);
  477. inc(variantrecordlevel);
  478. if token<>_RKLAMMER then
  479. read_var_decs(true,false,false);
  480. dec(variantrecordlevel);
  481. consume(_RKLAMMER);
  482. { calculates maximal variant size }
  483. maxsize:=max(maxsize,symtablestack.datasize);
  484. maxalignment:=max(maxalignment,symtablestack.dataalignment);
  485. { the items of the next variant are overlayed }
  486. symtablestack.datasize:=startvarrecsize;
  487. symtablestack.dataalignment:=startvarrecalign;
  488. if (token<>_END) and (token<>_RKLAMMER) then
  489. consume(_SEMICOLON)
  490. else
  491. break;
  492. until (token=_END) or (token=_RKLAMMER);
  493. { at last set the record size to that of the biggest variant }
  494. symtablestack.datasize:=maxsize;
  495. symtablestack.dataalignment:=maxalignment;
  496. uniontype.def:=uniondef;
  497. uniontype.sym:=nil;
  498. UnionSym:=tvarsym.create('case',uniontype);
  499. symtablestack:=symtablestack.next;
  500. { we do NOT call symtablestack.insert
  501. on purpose PM }
  502. offset:=align_from_size(symtablestack.datasize,maxalignment);
  503. symtablestack.datasize:=offset+unionsymtable.datasize;
  504. if maxalignment>symtablestack.dataalignment then
  505. symtablestack.dataalignment:=maxalignment;
  506. trecordsymtable(Unionsymtable).Insert_in(symtablestack,offset);
  507. Unionsym.owner:=nil;
  508. unionsym.free;
  509. uniondef.free;
  510. end;
  511. block_type:=old_block_type;
  512. current_object_option:=old_current_object_option;
  513. end;
  514. end.
  515. {
  516. $Log$
  517. Revision 1.17 2001-06-03 21:57:36 peter
  518. + hint directive parsing support
  519. Revision 1.16 2001/04/18 22:01:57 peter
  520. * registration of targets and assemblers
  521. Revision 1.15 2001/04/13 01:22:12 peter
  522. * symtable change to classes
  523. * range check generation and errors fixed, make cycle DEBUG=1 works
  524. * memory leaks fixed
  525. Revision 1.14 2001/04/04 22:43:52 peter
  526. * remove unnecessary calls to firstpass
  527. Revision 1.13 2001/04/04 21:30:45 florian
  528. * applied several fixes to get the DD8 Delphi Unit compiled
  529. e.g. "forward"-interfaces are working now
  530. Revision 1.12 2001/04/02 21:20:33 peter
  531. * resulttype rewrite
  532. Revision 1.11 2001/03/11 22:58:50 peter
  533. * getsym redesign, removed the globals srsym,srsymtable
  534. Revision 1.10 2001/03/06 18:28:02 peter
  535. * patch from Pavel with a new and much faster DLL Scanner for
  536. automatic importing so $linklib works for DLLs. Thanks Pavel!
  537. Revision 1.9 2001/02/20 21:42:54 peter
  538. * record and object declaration with same field as type fixed
  539. Revision 1.7 2001/02/20 11:19:45 marco
  540. * Fix passing tvarrec to array of const
  541. Revision 1.6 2000/12/25 00:07:27 peter
  542. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  543. tlinkedlist objects)
  544. Revision 1.5 2000/12/17 14:00:18 peter
  545. * fixed static variables
  546. Revision 1.4 2000/11/29 00:30:36 florian
  547. * unused units removed from uses clause
  548. * some changes for widestrings
  549. Revision 1.3 2000/11/04 14:25:20 florian
  550. + merged Attila's changes for interfaces, not tested yet
  551. Revision 1.2 2000/10/31 22:02:49 peter
  552. * symtable splitted, no real code changes
  553. Revision 1.1 2000/10/14 10:14:51 peter
  554. * moehrendorf oct 2000 rewrite
  555. }