pdecvar.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. begin
  114. old_current_object_option:=current_object_option;
  115. { all variables are public if not in a object declaration }
  116. if not is_object then
  117. current_object_option:=[sp_public];
  118. old_block_type:=block_type;
  119. block_type:=bt_type;
  120. is_gpc_name:=false;
  121. { Force an expected ID error message }
  122. if not (token in [_ID,_CASE,_END]) then
  123. consume(_ID);
  124. { read vars }
  125. while (token=_ID) and
  126. not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
  127. begin
  128. C_name:=orgpattern;
  129. sc:=idlist;
  130. {$ifdef fixLeaksOnError}
  131. strContStack.push(sc);
  132. {$endif fixLeaksOnError}
  133. consume(_COLON);
  134. if (m_gpc in aktmodeswitches) and
  135. not(is_record or is_object or is_threadvar) and
  136. (token=_ID) and (orgpattern='__asmname__') then
  137. begin
  138. consume(_ID);
  139. C_name:=pattern;
  140. if token=_CCHAR then
  141. consume(_CCHAR)
  142. else
  143. consume(_CSTRING);
  144. Is_gpc_name:=true;
  145. end;
  146. { this is needed for Delphi mode at least
  147. but should be OK for all modes !! (PM) }
  148. ignore_equal:=true;
  149. if is_record then
  150. begin
  151. { for records, don't search the recordsymtable for
  152. the symbols of the types }
  153. oldsymtablestack:=symtablestack;
  154. symtablestack:=symtablestack.next;
  155. read_type(tt,'');
  156. symtablestack:=oldsymtablestack;
  157. end
  158. else
  159. read_type(tt,'');
  160. if (variantrecordlevel>0) and tt.def.needs_inittable then
  161. Message(parser_e_cant_use_inittable_here);
  162. ignore_equal:=false;
  163. symdone:=false;
  164. if is_gpc_name then
  165. begin
  166. storetokenpos:=akttokenpos;
  167. s:=sc.get(akttokenpos);
  168. if not sc.empty then
  169. Message(parser_e_absolute_only_one_var);
  170. {$ifdef fixLeaksOnError}
  171. if strContStack.pop <> sc then
  172. writeln('problem with strContStack in pdecl (3)');
  173. {$endif fixLeaksOnError}
  174. sc.free;
  175. aktvarsym:=tvarsym.create_C(s,target_info.Cprefix+C_name,tt);
  176. include(aktvarsym.varoptions,vo_is_external);
  177. symtablestack.insert(aktvarsym);
  178. akttokenpos:=storetokenpos;
  179. symdone:=true;
  180. end;
  181. { check for absolute }
  182. if not symdone and
  183. (idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
  184. begin
  185. consume(_ABSOLUTE);
  186. { only allowed for one var }
  187. s:=sc.get(declarepos);
  188. if not sc.empty then
  189. Message(parser_e_absolute_only_one_var);
  190. {$ifdef fixLeaksOnError}
  191. if strContStack.pop <> sc then
  192. writeln('problem with strContStack in pdecl (4)');
  193. {$endif fixLeaksOnError}
  194. sc.free;
  195. { parse the rest }
  196. if token=_ID then
  197. begin
  198. consume_sym(srsym,srsymtable);
  199. { we should check the result type of srsym }
  200. if not (srsym.typ in [varsym,typedconstsym,funcretsym]) then
  201. Message(parser_e_absolute_only_to_var_or_const);
  202. storetokenpos:=akttokenpos;
  203. akttokenpos:=declarepos;
  204. abssym:=tabsolutesym.create(s,tt);
  205. abssym.abstyp:=tovar;
  206. abssym.ref:=tstoredsym(srsym);
  207. symtablestack.insert(abssym);
  208. akttokenpos:=storetokenpos;
  209. end
  210. else
  211. if (token=_CSTRING) or (token=_CCHAR) then
  212. begin
  213. storetokenpos:=akttokenpos;
  214. akttokenpos:=declarepos;
  215. abssym:=tabsolutesym.create(s,tt);
  216. s:=pattern;
  217. consume(token);
  218. abssym.abstyp:=toasm;
  219. abssym.asmname:=stringdup(s);
  220. symtablestack.insert(abssym);
  221. akttokenpos:=storetokenpos;
  222. end
  223. else
  224. { absolute address ?!? }
  225. if token=_INTCONST then
  226. begin
  227. if (target_info.target=target_i386_go32v2)
  228. or (m_objfpc in aktmodeswitches)
  229. or (m_delphi in aktmodeswitches) then
  230. begin
  231. storetokenpos:=akttokenpos;
  232. akttokenpos:=declarepos;
  233. abssym:=tabsolutesym.create(s,tt);
  234. abssym.abstyp:=toaddr;
  235. abssym.absseg:=false;
  236. s:=pattern;
  237. consume(_INTCONST);
  238. val(s,abssym.address,code);
  239. if (token=_COLON) and
  240. (target_info.target=target_i386_go32v2) then
  241. begin
  242. consume(token);
  243. s:=pattern;
  244. consume(_INTCONST);
  245. val(s,l,code);
  246. abssym.address:=abssym.address shl 4+l;
  247. abssym.absseg:=true;
  248. end;
  249. symtablestack.insert(abssym);
  250. akttokenpos:=storetokenpos;
  251. end
  252. else
  253. Message(parser_e_absolute_only_to_var_or_const);
  254. end
  255. else
  256. Message(parser_e_absolute_only_to_var_or_const);
  257. symdone:=true;
  258. end;
  259. { Handling of Delphi typed const = initialized vars ! }
  260. { When should this be rejected ?
  261. - in parasymtable
  262. - in record or object
  263. - ... (PM) }
  264. if (m_delphi in aktmodeswitches) and (token=_EQUAL) and
  265. not (symtablestack.symtabletype in [parasymtable]) and
  266. not is_record and not is_object then
  267. begin
  268. storetokenpos:=akttokenpos;
  269. s:=sc.get(akttokenpos);
  270. if not sc.empty then
  271. Message(parser_e_initialized_only_one_var);
  272. tconstsym:=ttypedconstsym.createtype(s,tt,false);
  273. symtablestack.insert(tconstsym);
  274. akttokenpos:=storetokenpos;
  275. consume(_EQUAL);
  276. readtypedconst(tt,tconstsym,false);
  277. symdone:=true;
  278. end;
  279. { for a record there doesn't need to be a ; before the END or ) }
  280. if not((is_record or is_object) and (token in [_END,_RKLAMMER])) then
  281. consume(_SEMICOLON);
  282. { procvar handling }
  283. if (tt.def.deftype=procvardef) and (tt.def.typesym=nil) then
  284. begin
  285. newtype:=ttypesym.create('unnamed',tt);
  286. parse_var_proc_directives(tsym(newtype));
  287. newtype.restype.def:=nil;
  288. tt.def.typesym:=nil;
  289. newtype.free;
  290. end;
  291. { Check for variable directives }
  292. if not symdone and (token=_ID) then
  293. begin
  294. { Check for C Variable declarations }
  295. if (m_cvar_support in aktmodeswitches) and
  296. not(is_record or is_object or is_threadvar) and
  297. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  298. begin
  299. { only allowed for one var }
  300. s:=sc.get(declarepos);
  301. if not sc.empty then
  302. Message(parser_e_absolute_only_one_var);
  303. {$ifdef fixLeaksOnError}
  304. if strContStack.pop <> sc then
  305. writeln('problem with strContStack in pdecl (5)');
  306. {$endif fixLeaksOnError}
  307. sc.free;
  308. { defaults }
  309. is_dll:=false;
  310. is_cdecl:=false;
  311. extern_aktvarsym:=false;
  312. export_aktvarsym:=false;
  313. { cdecl }
  314. if idtoken=_CVAR then
  315. begin
  316. consume(_CVAR);
  317. consume(_SEMICOLON);
  318. is_cdecl:=true;
  319. C_name:=target_info.Cprefix+C_name;
  320. end;
  321. { external }
  322. if idtoken=_EXTERNAL then
  323. begin
  324. consume(_EXTERNAL);
  325. extern_aktvarsym:=true;
  326. end;
  327. { export }
  328. if idtoken in [_EXPORT,_PUBLIC] then
  329. begin
  330. consume(_ID);
  331. if extern_aktvarsym or
  332. (symtablestack.symtabletype in [parasymtable,localsymtable]) then
  333. Message(parser_e_not_external_and_export)
  334. else
  335. export_aktvarsym:=true;
  336. end;
  337. { external and export need a name after when no cdecl is used }
  338. if not is_cdecl then
  339. begin
  340. { dll name ? }
  341. if (extern_aktvarsym) and (idtoken<>_NAME) then
  342. begin
  343. is_dll:=true;
  344. dll_name:=get_stringconst;
  345. end;
  346. consume(_NAME);
  347. C_name:=get_stringconst;
  348. end;
  349. { consume the ; when export or external is used }
  350. if extern_aktvarsym or export_aktvarsym then
  351. consume(_SEMICOLON);
  352. { insert in the symtable }
  353. storetokenpos:=akttokenpos;
  354. akttokenpos:=declarepos;
  355. if is_dll then
  356. aktvarsym:=tvarsym.create_dll(s,tt)
  357. else
  358. aktvarsym:=tvarsym.create_C(s,C_name,tt);
  359. { set some vars options }
  360. if export_aktvarsym then
  361. begin
  362. inc(aktvarsym.refs);
  363. include(aktvarsym.varoptions,vo_is_exported);
  364. end;
  365. if extern_aktvarsym then
  366. include(aktvarsym.varoptions,vo_is_external);
  367. { insert in the stack/datasegment }
  368. symtablestack.insert(aktvarsym);
  369. akttokenpos:=storetokenpos;
  370. { now we can insert it in the import lib if its a dll, or
  371. add it to the externals }
  372. if extern_aktvarsym then
  373. begin
  374. if is_dll then
  375. begin
  376. if not(current_module.uses_imports) then
  377. begin
  378. current_module.uses_imports:=true;
  379. importlib.preparelib(current_module.modulename^);
  380. end;
  381. importlib.importvariable(aktvarsym.mangledname,dll_name,C_name)
  382. end
  383. else
  384. if target_info.DllScanSupported then
  385. current_module.Externals.insert(tExternalsItem.create(aktvarsym.mangledname));
  386. end;
  387. symdone:=true;
  388. end
  389. else
  390. if (is_object) and (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  391. begin
  392. include(current_object_option,sp_static);
  393. insert_syms(symtablestack,sc,tt,false);
  394. exclude(current_object_option,sp_static);
  395. consume(_STATIC);
  396. consume(_SEMICOLON);
  397. symdone:=true;
  398. end;
  399. end;
  400. { insert it in the symtable, if not done yet }
  401. if not symdone then
  402. begin
  403. { save object option, because we can turn of the sp_published }
  404. if (sp_published in current_object_option) and
  405. not(is_class(tt.def)) then
  406. begin
  407. Message(parser_e_cant_publish_that);
  408. exclude(current_object_option,sp_published);
  409. end
  410. else
  411. if (sp_published in current_object_option) and
  412. not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
  413. begin
  414. Message(parser_e_only_publishable_classes_can__be_published);
  415. exclude(current_object_option,sp_published);
  416. end;
  417. insert_syms(symtablestack,sc,tt,is_threadvar);
  418. current_object_option:=old_current_object_option;
  419. end;
  420. end;
  421. { Check for Case }
  422. if is_record and (token=_CASE) then
  423. begin
  424. maxsize:=0;
  425. maxalignment:=0;
  426. consume(_CASE);
  427. s:=pattern;
  428. searchsym(s,srsym,srsymtable);
  429. { may be only a type: }
  430. if assigned(srsym) and (srsym.typ in [typesym,unitsym]) then
  431. begin
  432. { for records, don't search the recordsymtable for
  433. the symbols of the types }
  434. oldsymtablestack:=symtablestack;
  435. symtablestack:=symtablestack.next;
  436. read_type(casetype,'');
  437. symtablestack:=oldsymtablestack;
  438. end
  439. else
  440. begin
  441. consume(_ID);
  442. consume(_COLON);
  443. { for records, don't search the recordsymtable for
  444. the symbols of the types }
  445. oldsymtablestack:=symtablestack;
  446. symtablestack:=symtablestack.next;
  447. read_type(casetype,'');
  448. symtablestack:=oldsymtablestack;
  449. symtablestack.insert(tvarsym.create(s,casetype));
  450. end;
  451. if not(is_ordinal(casetype.def)) or is_64bitint(casetype.def) then
  452. Message(type_e_ordinal_expr_expected);
  453. consume(_OF);
  454. UnionSymtable:=trecordsymtable.create;
  455. Unionsymtable.next:=symtablestack;
  456. registerdef:=false;
  457. UnionDef:=trecorddef.create(unionsymtable);
  458. registerdef:=true;
  459. symtablestack:=UnionSymtable;
  460. startvarrecsize:=symtablestack.datasize;
  461. startvarrecalign:=symtablestack.dataalignment;
  462. repeat
  463. repeat
  464. pt:=comp_expr(true);
  465. if not(pt.nodetype=ordconstn) then
  466. Message(cg_e_illegal_expression);
  467. pt.free;
  468. if token=_COMMA then
  469. consume(_COMMA)
  470. else
  471. break;
  472. until false;
  473. consume(_COLON);
  474. { read the vars }
  475. consume(_LKLAMMER);
  476. inc(variantrecordlevel);
  477. if token<>_RKLAMMER then
  478. read_var_decs(true,false,false);
  479. dec(variantrecordlevel);
  480. consume(_RKLAMMER);
  481. { calculates maximal variant size }
  482. maxsize:=max(maxsize,symtablestack.datasize);
  483. maxalignment:=max(maxalignment,symtablestack.dataalignment);
  484. { the items of the next variant are overlayed }
  485. symtablestack.datasize:=startvarrecsize;
  486. symtablestack.dataalignment:=startvarrecalign;
  487. if (token<>_END) and (token<>_RKLAMMER) then
  488. consume(_SEMICOLON)
  489. else
  490. break;
  491. until (token=_END) or (token=_RKLAMMER);
  492. { at last set the record size to that of the biggest variant }
  493. symtablestack.datasize:=maxsize;
  494. symtablestack.dataalignment:=maxalignment;
  495. uniontype.def:=uniondef;
  496. uniontype.sym:=nil;
  497. UnionSym:=tvarsym.create('case',uniontype);
  498. symtablestack:=symtablestack.next;
  499. { we do NOT call symtablestack.insert
  500. on purpose PM }
  501. offset:=align_from_size(symtablestack.datasize,maxalignment);
  502. symtablestack.datasize:=offset+unionsymtable.datasize;
  503. if maxalignment>symtablestack.dataalignment then
  504. symtablestack.dataalignment:=maxalignment;
  505. trecordsymtable(Unionsymtable).Insert_in(symtablestack,offset);
  506. Unionsym.owner:=nil;
  507. unionsym.free;
  508. uniondef.free;
  509. end;
  510. block_type:=old_block_type;
  511. current_object_option:=old_current_object_option;
  512. end;
  513. end.
  514. {
  515. $Log$
  516. Revision 1.16 2001-04-18 22:01:57 peter
  517. * registration of targets and assemblers
  518. Revision 1.15 2001/04/13 01:22:12 peter
  519. * symtable change to classes
  520. * range check generation and errors fixed, make cycle DEBUG=1 works
  521. * memory leaks fixed
  522. Revision 1.14 2001/04/04 22:43:52 peter
  523. * remove unnecessary calls to firstpass
  524. Revision 1.13 2001/04/04 21:30:45 florian
  525. * applied several fixes to get the DD8 Delphi Unit compiled
  526. e.g. "forward"-interfaces are working now
  527. Revision 1.12 2001/04/02 21:20:33 peter
  528. * resulttype rewrite
  529. Revision 1.11 2001/03/11 22:58:50 peter
  530. * getsym redesign, removed the globals srsym,srsymtable
  531. Revision 1.10 2001/03/06 18:28:02 peter
  532. * patch from Pavel with a new and much faster DLL Scanner for
  533. automatic importing so $linklib works for DLLs. Thanks Pavel!
  534. Revision 1.9 2001/02/20 21:42:54 peter
  535. * record and object declaration with same field as type fixed
  536. Revision 1.7 2001/02/20 11:19:45 marco
  537. * Fix passing tvarrec to array of const
  538. Revision 1.6 2000/12/25 00:07:27 peter
  539. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  540. tlinkedlist objects)
  541. Revision 1.5 2000/12/17 14:00:18 peter
  542. * fixed static variables
  543. Revision 1.4 2000/11/29 00:30:36 florian
  544. * unused units removed from uses clause
  545. * some changes for widestrings
  546. Revision 1.3 2000/11/04 14:25:20 florian
  547. + merged Attila's changes for interfaces, not tested yet
  548. Revision 1.2 2000/10/31 22:02:49 peter
  549. * symtable splitted, no real code changes
  550. Revision 1.1 2000/10/14 10:14:51 peter
  551. * moehrendorf oct 2000 rewrite
  552. }