pdecvar.pas 24 KB

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