pdecvar.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. abssym:=tabsolutesym.create(vs.realname,tt);
  211. abssym.fileinfo:=vs.fileinfo;
  212. abssym.abstyp:=tovar;
  213. abssym.ref:=tstoredsym(tloadnode(pt).symtableentry);
  214. symtablestack.replace(vs,abssym);
  215. vs.free;
  216. end
  217. { funcret }
  218. else if (pt.nodetype=funcretn) then
  219. begin
  220. abssym:=tabsolutesym.create(vs.realname,tt);
  221. abssym.fileinfo:=vs.fileinfo;
  222. abssym.abstyp:=tovar;
  223. abssym.ref:=tstoredsym(tfuncretnode(pt).funcretsym);
  224. symtablestack.replace(vs,abssym);
  225. vs.free;
  226. end
  227. { address }
  228. else if is_constintnode(pt) and
  229. ((target_info.system=system_i386_go32v2) or
  230. (m_objfpc in aktmodeswitches) or
  231. (m_delphi in aktmodeswitches)) then
  232. begin
  233. abssym:=tabsolutesym.create(vs.realname,tt);
  234. abssym.fileinfo:=vs.fileinfo;
  235. abssym.abstyp:=toaddr;
  236. abssym.absseg:=false;
  237. abssym.address:=tordconstnode(pt).value;
  238. if (token=_COLON) and
  239. (target_info.system=system_i386_go32v2) then
  240. begin
  241. consume(token);
  242. pt.free;
  243. pt:=expr;
  244. if is_constintnode(pt) then
  245. begin
  246. abssym.address:=abssym.address shl 4+tordconstnode(pt).value;
  247. abssym.absseg:=true;
  248. end
  249. else
  250. Message(parser_e_absolute_only_to_var_or_const);
  251. end;
  252. symtablestack.replace(vs,abssym);
  253. vs.free;
  254. end
  255. else
  256. Message(parser_e_absolute_only_to_var_or_const);
  257. pt.free;
  258. symdone:=true;
  259. end;
  260. { Handling of Delphi typed const = initialized vars ! }
  261. { When should this be rejected ?
  262. - in parasymtable
  263. - in record or object
  264. - ... (PM) }
  265. if (token=_EQUAL) and
  266. not(m_tp7 in aktmodeswitches) and
  267. not(symtablestack.symtabletype in [parasymtable]) and
  268. not is_record and
  269. not is_object then
  270. begin
  271. vs:=tvarsym(sc.first);
  272. if assigned(vs.listnext) then
  273. Message(parser_e_initialized_only_one_var);
  274. tconstsym:=ttypedconstsym.createtype(vs.realname,tt,true);
  275. tconstsym.fileinfo:=vs.fileinfo;
  276. symtablestack.replace(vs,tconstsym);
  277. vs.free;
  278. symtablestack.insertconstdata(tconstsym);
  279. consume(_EQUAL);
  280. readtypedconst(tt,tconstsym,true);
  281. symdone:=true;
  282. end;
  283. { hint directive }
  284. {$warning hintdirective not stored in syms}
  285. dummysymoptions:=[];
  286. try_consume_hintdirective(dummysymoptions);
  287. { for a record there doesn't need to be a ; before the END or ) }
  288. if not((is_record or is_object) and (token in [_END,_RKLAMMER])) then
  289. consume(_SEMICOLON);
  290. { procvar handling }
  291. if (tt.def.deftype=procvardef) and (tt.def.typesym=nil) then
  292. begin
  293. newtype:=ttypesym.create('unnamed',tt);
  294. parse_var_proc_directives(tsym(newtype));
  295. paramanager.create_param_loc_info(tabstractprocdef(tt.def));
  296. newtype.restype.def:=nil;
  297. tt.def.typesym:=nil;
  298. newtype.free;
  299. end;
  300. { Check for variable directives }
  301. if not symdone and (token=_ID) then
  302. begin
  303. { Check for C Variable declarations }
  304. if (m_cvar_support in aktmodeswitches) and
  305. not(is_record or is_object or is_threadvar) and
  306. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  307. begin
  308. { only allowed for one var }
  309. vs:=tvarsym(sc.first);
  310. if assigned(vs.listnext) then
  311. Message(parser_e_absolute_only_one_var);
  312. { set type of the var }
  313. vs.vartype:=tt;
  314. { defaults }
  315. is_dll:=false;
  316. is_cdecl:=false;
  317. extern_var:=false;
  318. export_var:=false;
  319. C_name:=sorg;
  320. { cdecl }
  321. if idtoken=_CVAR then
  322. begin
  323. consume(_CVAR);
  324. consume(_SEMICOLON);
  325. is_cdecl:=true;
  326. C_name:=target_info.Cprefix+sorg;
  327. end;
  328. { external }
  329. if idtoken=_EXTERNAL then
  330. begin
  331. consume(_EXTERNAL);
  332. extern_var:=true;
  333. end;
  334. { export }
  335. if idtoken in [_EXPORT,_PUBLIC] then
  336. begin
  337. consume(_ID);
  338. if extern_var or
  339. (symtablestack.symtabletype in [parasymtable,localsymtable]) then
  340. Message(parser_e_not_external_and_export)
  341. else
  342. export_var:=true;
  343. end;
  344. { external and export need a name after when no cdecl is used }
  345. if not is_cdecl then
  346. begin
  347. { dll name ? }
  348. if (extern_var) and (idtoken<>_NAME) then
  349. begin
  350. is_dll:=true;
  351. dll_name:=get_stringconst;
  352. end;
  353. consume(_NAME);
  354. C_name:=get_stringconst;
  355. end;
  356. { consume the ; when export or external is used }
  357. if extern_var or export_var then
  358. consume(_SEMICOLON);
  359. { set some vars options }
  360. if is_dll then
  361. include(vs.varoptions,vo_is_dll_var)
  362. else
  363. include(vs.varoptions,vo_is_C_var);
  364. vs.set_mangledname(C_Name);
  365. if export_var then
  366. begin
  367. inc(vs.refs);
  368. include(vs.varoptions,vo_is_exported);
  369. end;
  370. if extern_var then
  371. include(vs.varoptions,vo_is_external);
  372. { insert in the datasegment when it is not external }
  373. if not extern_var then
  374. symtablestack.insertvardata(vs);
  375. { now we can insert it in the import lib if its a dll, or
  376. add it to the externals }
  377. if extern_var then
  378. begin
  379. if is_dll then
  380. begin
  381. if not(current_module.uses_imports) then
  382. begin
  383. current_module.uses_imports:=true;
  384. importlib.preparelib(current_module.modulename^);
  385. end;
  386. importlib.importvariable(vs,C_name,dll_name);
  387. end
  388. else
  389. if target_info.DllScanSupported then
  390. current_module.Externals.insert(tExternalsItem.create(vs.mangledname));
  391. end;
  392. symdone:=true;
  393. end
  394. else
  395. if (is_object) and (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  396. begin
  397. include(current_object_option,sp_static);
  398. insert_syms(sc,tt,false);
  399. exclude(current_object_option,sp_static);
  400. consume(_STATIC);
  401. consume(_SEMICOLON);
  402. symdone:=true;
  403. end;
  404. end;
  405. { insert it in the symtable, if not done yet }
  406. if not symdone then
  407. begin
  408. { save object option, because we can turn of the sp_published }
  409. if (sp_published in current_object_option) and
  410. not(is_class(tt.def)) then
  411. begin
  412. Message(parser_e_cant_publish_that);
  413. exclude(current_object_option,sp_published);
  414. end
  415. else
  416. if (sp_published in current_object_option) and
  417. not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
  418. begin
  419. Message(parser_e_only_publishable_classes_can__be_published);
  420. exclude(current_object_option,sp_published);
  421. end;
  422. insert_syms(sc,tt,is_threadvar);
  423. current_object_option:=old_current_object_option;
  424. end;
  425. end;
  426. { Check for Case }
  427. if is_record and (token=_CASE) then
  428. begin
  429. maxsize:=0;
  430. maxalignment:=0;
  431. consume(_CASE);
  432. sorg:=orgpattern;
  433. hs:=pattern;
  434. searchsym(hs,srsym,srsymtable);
  435. { may be only a type: }
  436. if assigned(srsym) and (srsym.typ in [typesym,unitsym]) then
  437. begin
  438. { for records, don't search the recordsymtable for
  439. the symbols of the types }
  440. oldsymtablestack:=symtablestack;
  441. symtablestack:=symtablestack.next;
  442. read_type(casetype,'');
  443. symtablestack:=oldsymtablestack;
  444. end
  445. else
  446. begin
  447. consume(_ID);
  448. consume(_COLON);
  449. { for records, don't search the recordsymtable for
  450. the symbols of the types }
  451. oldsymtablestack:=symtablestack;
  452. symtablestack:=symtablestack.next;
  453. read_type(casetype,'');
  454. symtablestack:=oldsymtablestack;
  455. vs:=tvarsym.create(sorg,casetype);
  456. symtablestack.insert(vs);
  457. symtablestack.insertvardata(vs);
  458. end;
  459. if not(is_ordinal(casetype.def)) or is_64bitint(casetype.def) then
  460. Message(type_e_ordinal_expr_expected);
  461. consume(_OF);
  462. UnionSymtable:=trecordsymtable.create;
  463. Unionsymtable.next:=symtablestack;
  464. registerdef:=false;
  465. UnionDef:=trecorddef.create(unionsymtable);
  466. uniondef.isunion:=true;
  467. registerdef:=true;
  468. symtablestack:=UnionSymtable;
  469. startvarrecsize:=symtablestack.datasize;
  470. startvarrecalign:=symtablestack.dataalignment;
  471. repeat
  472. repeat
  473. pt:=comp_expr(true);
  474. if not(pt.nodetype=ordconstn) then
  475. Message(cg_e_illegal_expression);
  476. pt.free;
  477. if token=_COMMA then
  478. consume(_COMMA)
  479. else
  480. break;
  481. until false;
  482. consume(_COLON);
  483. { read the vars }
  484. consume(_LKLAMMER);
  485. inc(variantrecordlevel);
  486. if token<>_RKLAMMER then
  487. read_var_decs(true,false,false);
  488. dec(variantrecordlevel);
  489. consume(_RKLAMMER);
  490. { calculates maximal variant size }
  491. maxsize:=max(maxsize,symtablestack.datasize);
  492. maxalignment:=max(maxalignment,symtablestack.dataalignment);
  493. { the items of the next variant are overlayed }
  494. symtablestack.datasize:=startvarrecsize;
  495. symtablestack.dataalignment:=startvarrecalign;
  496. if (token<>_END) and (token<>_RKLAMMER) then
  497. consume(_SEMICOLON)
  498. else
  499. break;
  500. until (token=_END) or (token=_RKLAMMER);
  501. { at last set the record size to that of the biggest variant }
  502. symtablestack.datasize:=maxsize;
  503. symtablestack.dataalignment:=maxalignment;
  504. uniontype.def:=uniondef;
  505. uniontype.sym:=nil;
  506. UnionSym:=tvarsym.create('case',uniontype);
  507. symtablestack:=symtablestack.next;
  508. { we do NOT call symtablestack.insert
  509. on purpose PM }
  510. if aktalignment.recordalignmax=-1 then
  511. begin
  512. {$ifdef i386}
  513. if maxalignment>2 then
  514. minalignment:=4
  515. else if maxalignment>1 then
  516. minalignment:=2
  517. else
  518. minalignment:=1;
  519. {$else}
  520. {$ifdef m68k}
  521. minalignment:=2;
  522. {$endif}
  523. minalignment:=1;
  524. {$endif}
  525. end
  526. else
  527. minalignment:=maxalignment;
  528. usedalign:=used_align(maxalignment,minalignment,maxalignment);
  529. offset:=align(symtablestack.datasize,usedalign);
  530. symtablestack.datasize:=offset+unionsymtable.datasize;
  531. if maxalignment>symtablestack.dataalignment then
  532. symtablestack.dataalignment:=maxalignment;
  533. trecordsymtable(Unionsymtable).Insert_in(symtablestack,offset);
  534. Unionsym.owner:=nil;
  535. unionsym.free;
  536. uniondef.free;
  537. end;
  538. block_type:=old_block_type;
  539. current_object_option:=old_current_object_option;
  540. end;
  541. end.
  542. {
  543. $Log$
  544. Revision 1.33 2002-09-16 18:08:45 peter
  545. * fix setting of sp_static
  546. Revision 1.32 2002/09/09 17:34:15 peter
  547. * tdicationary.replace added to replace and item in a dictionary. This
  548. is only allowed for the same name
  549. * varsyms are inserted in symtable before the types are parsed. This
  550. fixes the long standing "var longint : longint" bug
  551. - consume_idlist and idstringlist removed. The loops are inserted
  552. at the callers place and uses the symtable for duplicate id checking
  553. Revision 1.31 2002/08/25 19:25:20 peter
  554. * sym.insert_in_data removed
  555. * symtable.insertvardata/insertconstdata added
  556. * removed insert_in_data call from symtable.insert, it needs to be
  557. called separatly. This allows to deref the address calculation
  558. * procedures now calculate the parast addresses after the procedure
  559. directives are parsed. This fixes the cdecl parast problem
  560. * push_addr_param has an extra argument that specifies if cdecl is used
  561. or not
  562. Revision 1.30 2002/07/29 21:23:44 florian
  563. * more fixes for the ppc
  564. + wrappers for the tcnvnode.first_* stuff introduced
  565. Revision 1.29 2002/07/26 21:15:40 florian
  566. * rewrote the system handling
  567. Revision 1.28 2002/07/20 11:57:55 florian
  568. * types.pas renamed to defbase.pas because D6 contains a types
  569. unit so this would conflicts if D6 programms are compiled
  570. + Willamette/SSE2 instructions to assembler added
  571. Revision 1.27 2002/06/10 13:41:26 jonas
  572. * fixed bug 1985
  573. Revision 1.26 2002/05/18 13:34:12 peter
  574. * readded missing revisions
  575. Revision 1.25 2002/05/16 19:46:43 carl
  576. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  577. + try to fix temp allocation (still in ifdef)
  578. + generic constructor calls
  579. + start of tassembler / tmodulebase class cleanup
  580. Revision 1.23 2002/04/21 18:57:24 peter
  581. * fixed memleaks when file can't be opened
  582. }