pdecvar.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.inc}
  21. interface
  22. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  23. implementation
  24. uses
  25. { common }
  26. cutils,cclasses,
  27. { global }
  28. globtype,globals,tokens,verbose,
  29. systems,
  30. { symtable }
  31. symconst,symbase,symtype,symdef,symsym,symtable,defbase,fmodule,paramgr,
  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(sc : tsinglelist;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. vs,vs2 : tvarsym;
  55. begin
  56. vs:=tvarsym(sc.first);
  57. while assigned(vs) do
  58. begin
  59. vs.vartype:=tt;
  60. if (sp_static in current_object_option) then
  61. include(vs.symoptions,sp_static);
  62. if is_threadvar then
  63. include(vs.varoptions,vo_is_thread_var);
  64. { static data fields are inserted in the globalsymtable }
  65. if (symtablestack.symtabletype=objectsymtable) and
  66. (sp_static in current_object_option) then
  67. begin
  68. vs2:=tvarsym.create('$'+lower(symtablestack.name^)+'_'+vs.name,tt);
  69. symtablestack.defowner.owner.insert(vs2);
  70. symtablestack.defowner.owner.insertvardata(vs2);
  71. end
  72. else
  73. begin
  74. { external data is not possible here }
  75. symtablestack.insertvardata(vs);
  76. end;
  77. vs:=tvarsym(vs.listnext);
  78. end;
  79. end;
  80. var
  81. sc : tsinglelist;
  82. old_block_type : tblock_type;
  83. declarepos,storetokenpos : tfileposinfo;
  84. oldsymtablestack : tsymtable;
  85. symdone : boolean;
  86. { to handle absolute }
  87. abssym : tabsolutesym;
  88. { c var }
  89. newtype : ttypesym;
  90. is_dll,
  91. is_gpc_name,is_cdecl,
  92. extern_var,export_var : boolean;
  93. old_current_object_option : tsymoptions;
  94. hs,sorg,C_name,dll_name : string;
  95. tt,casetype : ttype;
  96. { Delphi initialized vars }
  97. tconstsym : ttypedconstsym;
  98. { maxsize contains the max. size of a variant }
  99. { startvarrec contains the start of the variant part of a record }
  100. usedalign,
  101. maxsize,minalignment,maxalignment,startvarrecalign,startvarrecsize : longint;
  102. pt : tnode;
  103. vs : tvarsym;
  104. srsym : tsym;
  105. srsymtable : tsymtable;
  106. unionsymtable : tsymtable;
  107. offset : longint;
  108. uniondef : trecorddef;
  109. unionsym : tvarsym;
  110. uniontype : ttype;
  111. dummysymoptions : tsymoptions;
  112. begin
  113. old_current_object_option:=current_object_option;
  114. { all variables are public if not in a object declaration }
  115. if not is_object then
  116. current_object_option:=[sp_public];
  117. old_block_type:=block_type;
  118. block_type:=bt_type;
  119. is_gpc_name:=false;
  120. { Force an expected ID error message }
  121. if not (token in [_ID,_CASE,_END]) then
  122. consume(_ID);
  123. { read vars }
  124. sc:=tsinglelist.create;
  125. while (token=_ID) and
  126. not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
  127. begin
  128. sorg:=orgpattern;
  129. sc.reset;
  130. repeat
  131. vs:=tvarsym.create(orgpattern,generrortype);
  132. symtablestack.insert(vs);
  133. sc.insert(vs);
  134. consume(_ID);
  135. until not try_to_consume(_COMMA);
  136. consume(_COLON);
  137. if (m_gpc in aktmodeswitches) and
  138. not(is_record or is_object or is_threadvar) and
  139. (token=_ID) and (orgpattern='__asmname__') then
  140. begin
  141. consume(_ID);
  142. C_name:=get_stringconst;
  143. Is_gpc_name:=true;
  144. end;
  145. { this is needed for Delphi mode at least
  146. but should be OK for all modes !! (PM) }
  147. ignore_equal:=true;
  148. if is_record then
  149. begin
  150. { for records, don't search the recordsymtable for
  151. the symbols of the types }
  152. oldsymtablestack:=symtablestack;
  153. symtablestack:=symtablestack.next;
  154. read_type(tt,'');
  155. symtablestack:=oldsymtablestack;
  156. end
  157. else
  158. read_type(tt,'');
  159. { types that use init/final are not allowed in variant parts, but
  160. classes are allowed }
  161. if (variantrecordlevel>0) and
  162. (tt.def.needs_inittable and not is_class(tt.def)) then
  163. Message(parser_e_cant_use_inittable_here);
  164. ignore_equal:=false;
  165. symdone:=false;
  166. if is_gpc_name then
  167. begin
  168. vs:=tvarsym(sc.first);
  169. if assigned(vs.listnext) then
  170. Message(parser_e_absolute_only_one_var);
  171. vs.vartype:=tt;
  172. include(vs.varoptions,vo_is_C_var);
  173. vs.set_mangledname(target_info.Cprefix+sorg);
  174. include(vs.varoptions,vo_is_external);
  175. symdone:=true;
  176. end;
  177. { check for absolute }
  178. if not symdone and
  179. (idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
  180. begin
  181. consume(_ABSOLUTE);
  182. { only allowed for one var }
  183. vs:=tvarsym(sc.first);
  184. if assigned(vs.listnext) then
  185. Message(parser_e_absolute_only_one_var);
  186. { parse the rest }
  187. pt:=expr;
  188. if (pt.nodetype=stringconstn) or
  189. (is_constcharnode(pt)) then
  190. begin
  191. abssym:=tabsolutesym.create(vs.realname,tt);
  192. abssym.fileinfo:=vs.fileinfo;
  193. if pt.nodetype=stringconstn then
  194. hs:=strpas(tstringconstnode(pt).value_str)
  195. else
  196. hs:=chr(tordconstnode(pt).value);
  197. consume(token);
  198. abssym.abstyp:=toasm;
  199. abssym.asmname:=stringdup(hs);
  200. { replace the varsym }
  201. symtablestack.replace(vs,abssym);
  202. vs.free;
  203. end
  204. { variable }
  205. else if (pt.nodetype=loadn) then
  206. begin
  207. { we should check the result type of srsym }
  208. if not (tloadnode(pt).symtableentry.typ in [varsym,typedconstsym,funcretsym]) then
  209. Message(parser_e_absolute_only_to_var_or_const);
  210. { the variable cannot be put into a register
  211. if the size definition is not the same.
  212. }
  213. if tloadnode(pt).symtableentry.typ = varsym then
  214. begin
  215. if abssym.vartype.def <>
  216. tvarsym(tloadnode(pt).symtableentry).vartype.def then
  217. tvarsym(tloadnode(pt).symtableentry).varoptions:=
  218. tvarsym(tloadnode(pt).symtableentry).varoptions-[vo_regable,vo_fpuregable]
  219. end;
  220. abssym:=tabsolutesym.create(vs.realname,tt);
  221. abssym.fileinfo:=vs.fileinfo;
  222. abssym.abstyp:=tovar;
  223. abssym.ref:=tstoredsym(tloadnode(pt).symtableentry);
  224. symtablestack.replace(vs,abssym);
  225. vs.free;
  226. end
  227. { funcret }
  228. else if (pt.nodetype=funcretn) then
  229. begin
  230. abssym:=tabsolutesym.create(vs.realname,tt);
  231. abssym.fileinfo:=vs.fileinfo;
  232. abssym.abstyp:=tovar;
  233. abssym.ref:=tstoredsym(tfuncretnode(pt).funcretsym);
  234. symtablestack.replace(vs,abssym);
  235. vs.free;
  236. end
  237. { address }
  238. else if is_constintnode(pt) and
  239. ((target_info.system=system_i386_go32v2) or
  240. (m_objfpc in aktmodeswitches) or
  241. (m_delphi in aktmodeswitches)) then
  242. begin
  243. abssym:=tabsolutesym.create(vs.realname,tt);
  244. abssym.fileinfo:=vs.fileinfo;
  245. abssym.abstyp:=toaddr;
  246. abssym.absseg:=false;
  247. abssym.address:=tordconstnode(pt).value;
  248. if (token=_COLON) and
  249. (target_info.system=system_i386_go32v2) then
  250. begin
  251. consume(token);
  252. pt.free;
  253. pt:=expr;
  254. if is_constintnode(pt) then
  255. begin
  256. abssym.address:=abssym.address shl 4+tordconstnode(pt).value;
  257. abssym.absseg:=true;
  258. end
  259. else
  260. Message(parser_e_absolute_only_to_var_or_const);
  261. end;
  262. symtablestack.replace(vs,abssym);
  263. vs.free;
  264. end
  265. else
  266. Message(parser_e_absolute_only_to_var_or_const);
  267. pt.free;
  268. symdone:=true;
  269. end;
  270. { Handling of Delphi typed const = initialized vars ! }
  271. { When should this be rejected ?
  272. - in parasymtable
  273. - in record or object
  274. - ... (PM) }
  275. if (token=_EQUAL) and
  276. not(m_tp7 in aktmodeswitches) and
  277. not(symtablestack.symtabletype in [parasymtable]) and
  278. not is_record and
  279. not is_object then
  280. begin
  281. vs:=tvarsym(sc.first);
  282. if assigned(vs.listnext) then
  283. Message(parser_e_initialized_only_one_var);
  284. tconstsym:=ttypedconstsym.createtype(vs.realname,tt,true);
  285. tconstsym.fileinfo:=vs.fileinfo;
  286. symtablestack.replace(vs,tconstsym);
  287. vs.free;
  288. symtablestack.insertconstdata(tconstsym);
  289. consume(_EQUAL);
  290. readtypedconst(tt,tconstsym,true);
  291. symdone:=true;
  292. end;
  293. { hint directive }
  294. {$warning hintdirective not stored in syms}
  295. dummysymoptions:=[];
  296. try_consume_hintdirective(dummysymoptions);
  297. { for a record there doesn't need to be a ; before the END or ) }
  298. if not((is_record or is_object) and (token in [_END,_RKLAMMER])) then
  299. consume(_SEMICOLON);
  300. { procvar handling }
  301. if (tt.def.deftype=procvardef) and (tt.def.typesym=nil) then
  302. begin
  303. newtype:=ttypesym.create('unnamed',tt);
  304. parse_var_proc_directives(tsym(newtype));
  305. paramanager.create_param_loc_info(tabstractprocdef(tt.def));
  306. newtype.restype.def:=nil;
  307. tt.def.typesym:=nil;
  308. newtype.free;
  309. end;
  310. { Check for variable directives }
  311. if not symdone and (token=_ID) then
  312. begin
  313. { Check for C Variable declarations }
  314. if (m_cvar_support in aktmodeswitches) and
  315. not(is_record or is_object or is_threadvar) and
  316. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  317. begin
  318. { only allowed for one var }
  319. vs:=tvarsym(sc.first);
  320. if assigned(vs.listnext) then
  321. Message(parser_e_absolute_only_one_var);
  322. { set type of the var }
  323. vs.vartype:=tt;
  324. { defaults }
  325. is_dll:=false;
  326. is_cdecl:=false;
  327. extern_var:=false;
  328. export_var:=false;
  329. C_name:=sorg;
  330. { cdecl }
  331. if idtoken=_CVAR then
  332. begin
  333. consume(_CVAR);
  334. consume(_SEMICOLON);
  335. is_cdecl:=true;
  336. C_name:=target_info.Cprefix+sorg;
  337. end;
  338. { external }
  339. if idtoken=_EXTERNAL then
  340. begin
  341. consume(_EXTERNAL);
  342. extern_var:=true;
  343. end;
  344. { export }
  345. if idtoken in [_EXPORT,_PUBLIC] then
  346. begin
  347. consume(_ID);
  348. if extern_var or
  349. (symtablestack.symtabletype in [parasymtable,localsymtable]) then
  350. Message(parser_e_not_external_and_export)
  351. else
  352. export_var:=true;
  353. end;
  354. { external and export need a name after when no cdecl is used }
  355. if not is_cdecl then
  356. begin
  357. { dll name ? }
  358. if (extern_var) and (idtoken<>_NAME) then
  359. begin
  360. is_dll:=true;
  361. dll_name:=get_stringconst;
  362. end;
  363. consume(_NAME);
  364. C_name:=get_stringconst;
  365. end;
  366. { consume the ; when export or external is used }
  367. if extern_var or export_var then
  368. consume(_SEMICOLON);
  369. { set some vars options }
  370. if is_dll then
  371. include(vs.varoptions,vo_is_dll_var)
  372. else
  373. include(vs.varoptions,vo_is_C_var);
  374. vs.set_mangledname(C_Name);
  375. if export_var then
  376. begin
  377. inc(vs.refs);
  378. include(vs.varoptions,vo_is_exported);
  379. end;
  380. if extern_var then
  381. include(vs.varoptions,vo_is_external);
  382. { insert in the datasegment when it is not external }
  383. if not extern_var then
  384. symtablestack.insertvardata(vs);
  385. { now we can insert it in the import lib if its a dll, or
  386. add it to the externals }
  387. if extern_var then
  388. begin
  389. if is_dll then
  390. begin
  391. if not(current_module.uses_imports) then
  392. begin
  393. current_module.uses_imports:=true;
  394. importlib.preparelib(current_module.modulename^);
  395. end;
  396. importlib.importvariable(vs,C_name,dll_name);
  397. end
  398. else
  399. if target_info.DllScanSupported then
  400. current_module.Externals.insert(tExternalsItem.create(vs.mangledname));
  401. end;
  402. symdone:=true;
  403. end
  404. else
  405. if (is_object) and (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  406. begin
  407. include(current_object_option,sp_static);
  408. insert_syms(sc,tt,false);
  409. exclude(current_object_option,sp_static);
  410. consume(_STATIC);
  411. consume(_SEMICOLON);
  412. symdone:=true;
  413. end;
  414. end;
  415. { insert it in the symtable, if not done yet }
  416. if not symdone then
  417. begin
  418. { save object option, because we can turn of the sp_published }
  419. if (sp_published in current_object_option) and
  420. not(is_class(tt.def)) then
  421. begin
  422. Message(parser_e_cant_publish_that);
  423. exclude(current_object_option,sp_published);
  424. end
  425. else
  426. if (sp_published in current_object_option) and
  427. not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
  428. begin
  429. Message(parser_e_only_publishable_classes_can__be_published);
  430. exclude(current_object_option,sp_published);
  431. end;
  432. insert_syms(sc,tt,is_threadvar);
  433. current_object_option:=old_current_object_option;
  434. end;
  435. end;
  436. { Check for Case }
  437. if is_record and (token=_CASE) then
  438. begin
  439. maxsize:=0;
  440. maxalignment:=0;
  441. consume(_CASE);
  442. sorg:=orgpattern;
  443. hs:=pattern;
  444. searchsym(hs,srsym,srsymtable);
  445. { may be only a type: }
  446. if assigned(srsym) and (srsym.typ in [typesym,unitsym]) then
  447. begin
  448. { for records, don't search the recordsymtable for
  449. the symbols of the types }
  450. oldsymtablestack:=symtablestack;
  451. symtablestack:=symtablestack.next;
  452. read_type(casetype,'');
  453. symtablestack:=oldsymtablestack;
  454. end
  455. else
  456. begin
  457. consume(_ID);
  458. consume(_COLON);
  459. { for records, don't search the recordsymtable for
  460. the symbols of the types }
  461. oldsymtablestack:=symtablestack;
  462. symtablestack:=symtablestack.next;
  463. read_type(casetype,'');
  464. symtablestack:=oldsymtablestack;
  465. vs:=tvarsym.create(sorg,casetype);
  466. symtablestack.insert(vs);
  467. symtablestack.insertvardata(vs);
  468. end;
  469. if not(is_ordinal(casetype.def)) or is_64bitint(casetype.def) then
  470. Message(type_e_ordinal_expr_expected);
  471. consume(_OF);
  472. UnionSymtable:=trecordsymtable.create;
  473. Unionsymtable.next:=symtablestack;
  474. registerdef:=false;
  475. UnionDef:=trecorddef.create(unionsymtable);
  476. uniondef.isunion:=true;
  477. registerdef:=true;
  478. symtablestack:=UnionSymtable;
  479. startvarrecsize:=symtablestack.datasize;
  480. startvarrecalign:=symtablestack.dataalignment;
  481. repeat
  482. repeat
  483. pt:=comp_expr(true);
  484. if not(pt.nodetype=ordconstn) then
  485. Message(cg_e_illegal_expression);
  486. pt.free;
  487. if token=_COMMA then
  488. consume(_COMMA)
  489. else
  490. break;
  491. until false;
  492. consume(_COLON);
  493. { read the vars }
  494. consume(_LKLAMMER);
  495. inc(variantrecordlevel);
  496. if token<>_RKLAMMER then
  497. read_var_decs(true,false,false);
  498. dec(variantrecordlevel);
  499. consume(_RKLAMMER);
  500. { calculates maximal variant size }
  501. maxsize:=max(maxsize,symtablestack.datasize);
  502. maxalignment:=max(maxalignment,symtablestack.dataalignment);
  503. { the items of the next variant are overlayed }
  504. symtablestack.datasize:=startvarrecsize;
  505. symtablestack.dataalignment:=startvarrecalign;
  506. if (token<>_END) and (token<>_RKLAMMER) then
  507. consume(_SEMICOLON)
  508. else
  509. break;
  510. until (token=_END) or (token=_RKLAMMER);
  511. { at last set the record size to that of the biggest variant }
  512. symtablestack.datasize:=maxsize;
  513. symtablestack.dataalignment:=maxalignment;
  514. uniontype.def:=uniondef;
  515. uniontype.sym:=nil;
  516. UnionSym:=tvarsym.create('case',uniontype);
  517. symtablestack:=symtablestack.next;
  518. { we do NOT call symtablestack.insert
  519. on purpose PM }
  520. if aktalignment.recordalignmax=-1 then
  521. begin
  522. {$ifdef i386}
  523. if maxalignment>2 then
  524. minalignment:=4
  525. else if maxalignment>1 then
  526. minalignment:=2
  527. else
  528. minalignment:=1;
  529. {$else}
  530. {$ifdef m68k}
  531. minalignment:=2;
  532. {$endif}
  533. minalignment:=1;
  534. {$endif}
  535. end
  536. else
  537. minalignment:=maxalignment;
  538. usedalign:=used_align(maxalignment,minalignment,maxalignment);
  539. offset:=align(symtablestack.datasize,usedalign);
  540. symtablestack.datasize:=offset+unionsymtable.datasize;
  541. if maxalignment>symtablestack.dataalignment then
  542. symtablestack.dataalignment:=maxalignment;
  543. trecordsymtable(Unionsymtable).Insert_in(symtablestack,offset);
  544. Unionsym.owner:=nil;
  545. unionsym.free;
  546. uniondef.free;
  547. end;
  548. block_type:=old_block_type;
  549. current_object_option:=old_current_object_option;
  550. end;
  551. end.
  552. {
  553. $Log$
  554. Revision 1.34 2002-10-03 21:22:01 carl
  555. * don't make the vars regable if they are absolute and their definitions
  556. are not the same.
  557. Revision 1.33 2002/09/16 18:08:45 peter
  558. * fix setting of sp_static
  559. Revision 1.32 2002/09/09 17:34:15 peter
  560. * tdicationary.replace added to replace and item in a dictionary. This
  561. is only allowed for the same name
  562. * varsyms are inserted in symtable before the types are parsed. This
  563. fixes the long standing "var longint : longint" bug
  564. - consume_idlist and idstringlist removed. The loops are inserted
  565. at the callers place and uses the symtable for duplicate id checking
  566. Revision 1.31 2002/08/25 19:25:20 peter
  567. * sym.insert_in_data removed
  568. * symtable.insertvardata/insertconstdata added
  569. * removed insert_in_data call from symtable.insert, it needs to be
  570. called separatly. This allows to deref the address calculation
  571. * procedures now calculate the parast addresses after the procedure
  572. directives are parsed. This fixes the cdecl parast problem
  573. * push_addr_param has an extra argument that specifies if cdecl is used
  574. or not
  575. Revision 1.30 2002/07/29 21:23:44 florian
  576. * more fixes for the ppc
  577. + wrappers for the tcnvnode.first_* stuff introduced
  578. Revision 1.29 2002/07/26 21:15:40 florian
  579. * rewrote the system handling
  580. Revision 1.28 2002/07/20 11:57:55 florian
  581. * types.pas renamed to defbase.pas because D6 contains a types
  582. unit so this would conflicts if D6 programms are compiled
  583. + Willamette/SSE2 instructions to assembler added
  584. Revision 1.27 2002/06/10 13:41:26 jonas
  585. * fixed bug 1985
  586. Revision 1.26 2002/05/18 13:34:12 peter
  587. * readded missing revisions
  588. Revision 1.25 2002/05/16 19:46:43 carl
  589. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  590. + try to fix temp allocation (still in ifdef)
  591. + generic constructor calls
  592. + start of tassembler / tmodulebase class cleanup
  593. Revision 1.23 2002/04/21 18:57:24 peter
  594. * fixed memleaks when file can't be opened
  595. }