pdecvar.pas 24 KB

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