pdecvar.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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,
  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. newtype.restype.def:=nil;
  323. tt.def.typesym:=nil;
  324. newtype.free;
  325. end;
  326. { Check for variable directives }
  327. if not symdone and (token=_ID) then
  328. begin
  329. { Check for C Variable declarations }
  330. if (m_cvar_support in aktmodeswitches) and
  331. not(is_record or is_object or is_threadvar) and
  332. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  333. begin
  334. { only allowed for one var }
  335. s:=sc.get(declarepos);
  336. if not sc.empty then
  337. Message(parser_e_absolute_only_one_var);
  338. {$ifdef fixLeaksOnError}
  339. if strContStack.pop <> sc then
  340. writeln('problem with strContStack in pdecl (5)');
  341. {$endif fixLeaksOnError}
  342. sc.free;
  343. { defaults }
  344. is_dll:=false;
  345. is_cdecl:=false;
  346. extern_aktvarsym:=false;
  347. export_aktvarsym:=false;
  348. { cdecl }
  349. if idtoken=_CVAR then
  350. begin
  351. consume(_CVAR);
  352. consume(_SEMICOLON);
  353. is_cdecl:=true;
  354. C_name:=target_info.Cprefix+C_name;
  355. end;
  356. { external }
  357. if idtoken=_EXTERNAL then
  358. begin
  359. consume(_EXTERNAL);
  360. extern_aktvarsym:=true;
  361. end;
  362. { export }
  363. if idtoken in [_EXPORT,_PUBLIC] then
  364. begin
  365. consume(_ID);
  366. if extern_aktvarsym or
  367. (symtablestack.symtabletype in [parasymtable,localsymtable]) then
  368. Message(parser_e_not_external_and_export)
  369. else
  370. export_aktvarsym:=true;
  371. end;
  372. { external and export need a name after when no cdecl is used }
  373. if not is_cdecl then
  374. begin
  375. { dll name ? }
  376. if (extern_aktvarsym) and (idtoken<>_NAME) then
  377. begin
  378. is_dll:=true;
  379. dll_name:=get_stringconst;
  380. end;
  381. consume(_NAME);
  382. C_name:=get_stringconst;
  383. end;
  384. { consume the ; when export or external is used }
  385. if extern_aktvarsym or export_aktvarsym then
  386. consume(_SEMICOLON);
  387. { insert in the symtable }
  388. storetokenpos:=akttokenpos;
  389. akttokenpos:=declarepos;
  390. if is_dll then
  391. aktvarsym:=tvarsym.create_dll(s,tt)
  392. else
  393. aktvarsym:=tvarsym.create_C(s,C_name,tt);
  394. { set some vars options }
  395. if export_aktvarsym then
  396. begin
  397. inc(aktvarsym.refs);
  398. include(aktvarsym.varoptions,vo_is_exported);
  399. end;
  400. if extern_aktvarsym then
  401. include(aktvarsym.varoptions,vo_is_external);
  402. { insert in the stack/datasegment }
  403. symtablestack.insert(aktvarsym);
  404. akttokenpos:=storetokenpos;
  405. { now we can insert it in the import lib if its a dll, or
  406. add it to the externals }
  407. if extern_aktvarsym then
  408. begin
  409. if is_dll then
  410. begin
  411. if not(current_module.uses_imports) then
  412. begin
  413. current_module.uses_imports:=true;
  414. importlib.preparelib(current_module.modulename^);
  415. end;
  416. importlib.importvariable(aktvarsym.mangledname,dll_name,C_name)
  417. end
  418. else
  419. if target_info.DllScanSupported then
  420. current_module.Externals.insert(tExternalsItem.create(aktvarsym.mangledname));
  421. end;
  422. symdone:=true;
  423. end
  424. else
  425. if (is_object) and (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  426. begin
  427. include(current_object_option,sp_static);
  428. insert_syms(symtablestack,sc,tt,false);
  429. exclude(current_object_option,sp_static);
  430. consume(_STATIC);
  431. consume(_SEMICOLON);
  432. symdone:=true;
  433. end;
  434. end;
  435. { insert it in the symtable, if not done yet }
  436. if not symdone then
  437. begin
  438. { save object option, because we can turn of the sp_published }
  439. if (sp_published in current_object_option) and
  440. not(is_class(tt.def)) then
  441. begin
  442. Message(parser_e_cant_publish_that);
  443. exclude(current_object_option,sp_published);
  444. end
  445. else
  446. if (sp_published in current_object_option) and
  447. not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
  448. begin
  449. Message(parser_e_only_publishable_classes_can__be_published);
  450. exclude(current_object_option,sp_published);
  451. end;
  452. insert_syms(symtablestack,sc,tt,is_threadvar);
  453. current_object_option:=old_current_object_option;
  454. end;
  455. end;
  456. { Check for Case }
  457. if is_record and (token=_CASE) then
  458. begin
  459. maxsize:=0;
  460. maxalignment:=0;
  461. consume(_CASE);
  462. s:=pattern;
  463. searchsym(s,srsym,srsymtable);
  464. { may be only a type: }
  465. if assigned(srsym) and (srsym.typ in [typesym,unitsym]) then
  466. begin
  467. { for records, don't search the recordsymtable for
  468. the symbols of the types }
  469. oldsymtablestack:=symtablestack;
  470. symtablestack:=symtablestack.next;
  471. read_type(casetype,'');
  472. symtablestack:=oldsymtablestack;
  473. end
  474. else
  475. begin
  476. consume(_ID);
  477. consume(_COLON);
  478. { for records, don't search the recordsymtable for
  479. the symbols of the types }
  480. oldsymtablestack:=symtablestack;
  481. symtablestack:=symtablestack.next;
  482. read_type(casetype,'');
  483. symtablestack:=oldsymtablestack;
  484. symtablestack.insert(tvarsym.create(s,casetype));
  485. end;
  486. if not(is_ordinal(casetype.def)) or is_64bitint(casetype.def) then
  487. Message(type_e_ordinal_expr_expected);
  488. consume(_OF);
  489. UnionSymtable:=trecordsymtable.create;
  490. Unionsymtable.next:=symtablestack;
  491. registerdef:=false;
  492. UnionDef:=trecorddef.create(unionsymtable);
  493. registerdef:=true;
  494. symtablestack:=UnionSymtable;
  495. startvarrecsize:=symtablestack.datasize;
  496. startvarrecalign:=symtablestack.dataalignment;
  497. repeat
  498. repeat
  499. pt:=comp_expr(true);
  500. if not(pt.nodetype=ordconstn) then
  501. Message(cg_e_illegal_expression);
  502. pt.free;
  503. if token=_COMMA then
  504. consume(_COMMA)
  505. else
  506. break;
  507. until false;
  508. consume(_COLON);
  509. { read the vars }
  510. consume(_LKLAMMER);
  511. inc(variantrecordlevel);
  512. if token<>_RKLAMMER then
  513. read_var_decs(true,false,false);
  514. dec(variantrecordlevel);
  515. consume(_RKLAMMER);
  516. { calculates maximal variant size }
  517. maxsize:=max(maxsize,symtablestack.datasize);
  518. maxalignment:=max(maxalignment,symtablestack.dataalignment);
  519. { the items of the next variant are overlayed }
  520. symtablestack.datasize:=startvarrecsize;
  521. symtablestack.dataalignment:=startvarrecalign;
  522. if (token<>_END) and (token<>_RKLAMMER) then
  523. consume(_SEMICOLON)
  524. else
  525. break;
  526. until (token=_END) or (token=_RKLAMMER);
  527. { at last set the record size to that of the biggest variant }
  528. symtablestack.datasize:=maxsize;
  529. symtablestack.dataalignment:=maxalignment;
  530. uniontype.def:=uniondef;
  531. uniontype.sym:=nil;
  532. UnionSym:=tvarsym.create('case',uniontype);
  533. symtablestack:=symtablestack.next;
  534. { we do NOT call symtablestack.insert
  535. on purpose PM }
  536. if aktalignment.recordalignmax=-1 then
  537. begin
  538. {$ifdef i386}
  539. if maxalignment>2 then
  540. minalignment:=4
  541. else if maxalignment>1 then
  542. minalignment:=2
  543. else
  544. minalignment:=1;
  545. {$else}
  546. {$ifdef m68k}
  547. minalignment:=2;
  548. {$endif}
  549. minalignment:=1;
  550. {$endif}
  551. end
  552. else
  553. minalignment:=maxalignment;
  554. usedalign:=used_align(maxalignment,minalignment,maxalignment);
  555. offset:=align(symtablestack.datasize,usedalign);
  556. symtablestack.datasize:=offset+unionsymtable.datasize;
  557. if maxalignment>symtablestack.dataalignment then
  558. symtablestack.dataalignment:=maxalignment;
  559. trecordsymtable(Unionsymtable).Insert_in(symtablestack,offset);
  560. Unionsym.owner:=nil;
  561. unionsym.free;
  562. uniondef.free;
  563. end;
  564. block_type:=old_block_type;
  565. current_object_option:=old_current_object_option;
  566. end;
  567. end.
  568. {
  569. $Log$
  570. Revision 1.29 2002-07-26 21:15:40 florian
  571. * rewrote the system handling
  572. Revision 1.28 2002/07/20 11:57:55 florian
  573. * types.pas renamed to defbase.pas because D6 contains a types
  574. unit so this would conflicts if D6 programms are compiled
  575. + Willamette/SSE2 instructions to assembler added
  576. Revision 1.27 2002/06/10 13:41:26 jonas
  577. * fixed bug 1985
  578. Revision 1.26 2002/05/18 13:34:12 peter
  579. * readded missing revisions
  580. Revision 1.25 2002/05/16 19:46:43 carl
  581. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  582. + try to fix temp allocation (still in ifdef)
  583. + generic constructor calls
  584. + start of tassembler / tmodulebase class cleanup
  585. Revision 1.23 2002/04/21 18:57:24 peter
  586. * fixed memleaks when file can't be opened
  587. }