pdecvar.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  21. {$define UseUnionSymtable}
  22. interface
  23. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  24. implementation
  25. uses
  26. { common }
  27. cutils,cobjects,
  28. { global }
  29. globtype,globals,tokens,verbose,
  30. systems,
  31. { symtable }
  32. symconst,symbase,symtype,symdef,symsym,symtable,types,fmodule,
  33. { pass 1 }
  34. node,pass_1,
  35. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  36. { parser }
  37. scanner,
  38. pbase,pexpr,ptype,ptconst,pdecsub,
  39. { link }
  40. import;
  41. const
  42. variantrecordlevel : longint = 0;
  43. procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
  44. { reads the filed of a record into a }
  45. { symtablestack, if record=false }
  46. { variants are forbidden, so this procedure }
  47. { can be used to read object fields }
  48. { if absolute is true, ABSOLUTE and file }
  49. { types are allowed }
  50. { => the procedure is also used to read }
  51. { a sequence of variable declaration }
  52. procedure insert_syms(st : psymtable;sc : tidstringlist;tt : ttype;is_threadvar : boolean);
  53. { inserts the symbols of sc in st with def as definition or sym as ptypesym, sc is disposed }
  54. var
  55. s : string;
  56. filepos : tfileposinfo;
  57. ss : pvarsym;
  58. begin
  59. filepos:=akttokenpos;
  60. while not sc.empty do
  61. begin
  62. s:=sc.get(akttokenpos);
  63. ss:=new(pvarsym,init(s,tt));
  64. if is_threadvar then
  65. include(ss^.varoptions,vo_is_thread_var);
  66. st^.insert(ss);
  67. { static data fields are inserted in the globalsymtable }
  68. if (st^.symtabletype=objectsymtable) and
  69. (sp_static in current_object_option) then
  70. begin
  71. s:='$'+lower(st^.name^)+'_'+upper(s);
  72. st^.defowner^.owner^.insert(new(pvarsym,init(s,tt)));
  73. end;
  74. end;
  75. {$ifdef fixLeaksOnError}
  76. if strContStack.pop <> sc then
  77. writeln('problem with strContStack in pdecl (2)');
  78. {$endif fixLeaksOnError}
  79. sc.free;
  80. akttokenpos:=filepos;
  81. end;
  82. var
  83. sc : tidstringList;
  84. s : stringid;
  85. old_block_type : tblock_type;
  86. declarepos,storetokenpos : tfileposinfo;
  87. symdone : boolean;
  88. { to handle absolute }
  89. abssym : pabsolutesym;
  90. l : longint;
  91. code : integer;
  92. { c var }
  93. newtype : ptypesym;
  94. is_dll,
  95. is_gpc_name,is_cdecl,extern_aktvarsym,export_aktvarsym : boolean;
  96. old_current_object_option : tsymoptions;
  97. dll_name,
  98. C_name : string;
  99. tt,casetype : ttype;
  100. { Delphi initialized vars }
  101. pconstsym : ptypedconstsym;
  102. { maxsize contains the max. size of a variant }
  103. { startvarrec contains the start of the variant part of a record }
  104. maxsize,maxalignment,startvarrecalign,startvarrecsize : longint;
  105. pt : tnode;
  106. {$ifdef UseUnionSymtable}
  107. unionsymtable : psymtable;
  108. offset : longint;
  109. uniondef : precorddef;
  110. unionsym : pvarsym;
  111. uniontype : ttype;
  112. {$endif UseUnionSymtable}
  113. begin
  114. old_current_object_option:=current_object_option;
  115. { all variables are public if not in a object declaration }
  116. if not is_object then
  117. current_object_option:=[sp_public];
  118. old_block_type:=block_type;
  119. block_type:=bt_type;
  120. is_gpc_name:=false;
  121. { Force an expected ID error message }
  122. if not (token in [_ID,_CASE,_END]) then
  123. consume(_ID);
  124. { read vars }
  125. while (token=_ID) and
  126. not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
  127. begin
  128. C_name:=orgpattern;
  129. sc:=idlist;
  130. {$ifdef fixLeaksOnError}
  131. strContStack.push(sc);
  132. {$endif fixLeaksOnError}
  133. consume(_COLON);
  134. if (m_gpc in aktmodeswitches) and
  135. not(is_record or is_object or is_threadvar) and
  136. (token=_ID) and (orgpattern='__asmname__') then
  137. begin
  138. consume(_ID);
  139. C_name:=pattern;
  140. if token=_CCHAR then
  141. consume(_CCHAR)
  142. else
  143. consume(_CSTRING);
  144. Is_gpc_name:=true;
  145. end;
  146. { this is needed for Delphi mode at least
  147. but should be OK for all modes !! (PM) }
  148. ignore_equal:=true;
  149. read_type(tt,'');
  150. if (variantrecordlevel>0) and tt.def^.needs_inittable then
  151. Message(parser_e_cant_use_inittable_here);
  152. ignore_equal:=false;
  153. symdone:=false;
  154. if is_gpc_name then
  155. begin
  156. storetokenpos:=akttokenpos;
  157. s:=sc.get(akttokenpos);
  158. if not sc.empty then
  159. Message(parser_e_absolute_only_one_var);
  160. {$ifdef fixLeaksOnError}
  161. if strContStack.pop <> sc then
  162. writeln('problem with strContStack in pdecl (3)');
  163. {$endif fixLeaksOnError}
  164. sc.free;
  165. aktvarsym:=new(pvarsym,init_C(s,target_os.Cprefix+C_name,tt));
  166. include(aktvarsym^.varoptions,vo_is_external);
  167. symtablestack^.insert(aktvarsym);
  168. akttokenpos:=storetokenpos;
  169. symdone:=true;
  170. end;
  171. { check for absolute }
  172. if not symdone and
  173. (idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
  174. begin
  175. consume(_ABSOLUTE);
  176. { only allowed for one var }
  177. s:=sc.get(declarepos);
  178. if not sc.empty then
  179. Message(parser_e_absolute_only_one_var);
  180. {$ifdef fixLeaksOnError}
  181. if strContStack.pop <> sc then
  182. writeln('problem with strContStack in pdecl (4)');
  183. {$endif fixLeaksOnError}
  184. sc.free;
  185. { parse the rest }
  186. if token=_ID then
  187. begin
  188. getsym(pattern,true);
  189. consume(_ID);
  190. { support unit.variable }
  191. if srsym^.typ=unitsym then
  192. begin
  193. consume(_POINT);
  194. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  195. consume(_ID);
  196. end;
  197. { we should check the result type of srsym }
  198. if not (srsym^.typ in [varsym,typedconstsym,funcretsym]) then
  199. Message(parser_e_absolute_only_to_var_or_const);
  200. storetokenpos:=akttokenpos;
  201. akttokenpos:=declarepos;
  202. abssym:=new(pabsolutesym,init(s,tt));
  203. abssym^.abstyp:=tovar;
  204. abssym^.ref:=pstoredsym(srsym);
  205. symtablestack^.insert(abssym);
  206. akttokenpos:=storetokenpos;
  207. end
  208. else
  209. if (token=_CSTRING) or (token=_CCHAR) then
  210. begin
  211. storetokenpos:=akttokenpos;
  212. akttokenpos:=declarepos;
  213. abssym:=new(pabsolutesym,init(s,tt));
  214. s:=pattern;
  215. consume(token);
  216. abssym^.abstyp:=toasm;
  217. abssym^.asmname:=stringdup(s);
  218. symtablestack^.insert(abssym);
  219. akttokenpos:=storetokenpos;
  220. end
  221. else
  222. { absolute address ?!? }
  223. if token=_INTCONST then
  224. begin
  225. if (target_info.target=target_i386_go32v2) then
  226. begin
  227. storetokenpos:=akttokenpos;
  228. akttokenpos:=declarepos;
  229. abssym:=new(pabsolutesym,init(s,tt));
  230. abssym^.abstyp:=toaddr;
  231. abssym^.absseg:=false;
  232. s:=pattern;
  233. consume(_INTCONST);
  234. val(s,abssym^.address,code);
  235. if token=_COLON then
  236. begin
  237. consume(token);
  238. s:=pattern;
  239. consume(_INTCONST);
  240. val(s,l,code);
  241. abssym^.address:=abssym^.address shl 4+l;
  242. abssym^.absseg:=true;
  243. end;
  244. symtablestack^.insert(abssym);
  245. akttokenpos:=storetokenpos;
  246. end
  247. else
  248. Message(parser_e_absolute_only_to_var_or_const);
  249. end
  250. else
  251. Message(parser_e_absolute_only_to_var_or_const);
  252. symdone:=true;
  253. end;
  254. { Handling of Delphi typed const = initialized vars ! }
  255. { When should this be rejected ?
  256. - in parasymtable
  257. - in record or object
  258. - ... (PM) }
  259. if (m_delphi in aktmodeswitches) and (token=_EQUAL) and
  260. not (symtablestack^.symtabletype in [parasymtable]) and
  261. not is_record and not is_object then
  262. begin
  263. storetokenpos:=akttokenpos;
  264. s:=sc.get(akttokenpos);
  265. if not sc.empty then
  266. Message(parser_e_initialized_only_one_var);
  267. pconstsym:=new(ptypedconstsym,inittype(s,tt,false));
  268. symtablestack^.insert(pconstsym);
  269. akttokenpos:=storetokenpos;
  270. consume(_EQUAL);
  271. readtypedconst(tt.def,pconstsym,false);
  272. symdone:=true;
  273. end;
  274. { for a record there doesn't need to be a ; before the END or ) }
  275. if not((is_record or is_object) and (token in [_END,_RKLAMMER])) then
  276. consume(_SEMICOLON);
  277. { procvar handling }
  278. if (tt.def^.deftype=procvardef) and (tt.def^.typesym=nil) then
  279. begin
  280. newtype:=new(ptypesym,init('unnamed',tt));
  281. parse_var_proc_directives(psym(newtype));
  282. newtype^.restype.def:=nil;
  283. tt.def^.typesym:=nil;
  284. dispose(newtype,done);
  285. end;
  286. { Check for variable directives }
  287. if not symdone and (token=_ID) then
  288. begin
  289. { Check for C Variable declarations }
  290. if (m_cvar_support in aktmodeswitches) and
  291. not(is_record or is_object or is_threadvar) and
  292. (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR]) then
  293. begin
  294. { only allowed for one var }
  295. s:=sc.get(declarepos);
  296. if not sc.empty then
  297. Message(parser_e_absolute_only_one_var);
  298. {$ifdef fixLeaksOnError}
  299. if strContStack.pop <> sc then
  300. writeln('problem with strContStack in pdecl (5)');
  301. {$endif fixLeaksOnError}
  302. sc.free;
  303. { defaults }
  304. is_dll:=false;
  305. is_cdecl:=false;
  306. extern_aktvarsym:=false;
  307. export_aktvarsym:=false;
  308. { cdecl }
  309. if idtoken=_CVAR then
  310. begin
  311. consume(_CVAR);
  312. consume(_SEMICOLON);
  313. is_cdecl:=true;
  314. C_name:=target_os.Cprefix+C_name;
  315. end;
  316. { external }
  317. if idtoken=_EXTERNAL then
  318. begin
  319. consume(_EXTERNAL);
  320. extern_aktvarsym:=true;
  321. end;
  322. { export }
  323. if idtoken in [_EXPORT,_PUBLIC] then
  324. begin
  325. consume(_ID);
  326. if extern_aktvarsym or
  327. (symtablestack^.symtabletype in [parasymtable,localsymtable]) then
  328. Message(parser_e_not_external_and_export)
  329. else
  330. export_aktvarsym:=true;
  331. end;
  332. { external and export need a name after when no cdecl is used }
  333. if not is_cdecl then
  334. begin
  335. { dll name ? }
  336. if (extern_aktvarsym) and (idtoken<>_NAME) then
  337. begin
  338. is_dll:=true;
  339. dll_name:=get_stringconst;
  340. end;
  341. consume(_NAME);
  342. C_name:=get_stringconst;
  343. end;
  344. { consume the ; when export or external is used }
  345. if extern_aktvarsym or export_aktvarsym then
  346. consume(_SEMICOLON);
  347. { insert in the symtable }
  348. storetokenpos:=akttokenpos;
  349. akttokenpos:=declarepos;
  350. if is_dll then
  351. aktvarsym:=new(pvarsym,init_dll(s,tt))
  352. else
  353. aktvarsym:=new(pvarsym,init_C(s,C_name,tt));
  354. { set some vars options }
  355. if export_aktvarsym then
  356. begin
  357. inc(aktvarsym^.refs);
  358. include(aktvarsym^.varoptions,vo_is_exported);
  359. end;
  360. if extern_aktvarsym then
  361. include(aktvarsym^.varoptions,vo_is_external);
  362. { insert in the stack/datasegment }
  363. symtablestack^.insert(aktvarsym);
  364. akttokenpos:=storetokenpos;
  365. { now we can insert it in the import lib if its a dll, or
  366. add it to the externals }
  367. if extern_aktvarsym then
  368. begin
  369. if is_dll then
  370. begin
  371. if not(current_module.uses_imports) then
  372. begin
  373. current_module.uses_imports:=true;
  374. importlib.preparelib(current_module.modulename^);
  375. end;
  376. importlib.importvariable(aktvarsym^.mangledname,dll_name,C_name)
  377. end
  378. end;
  379. symdone:=true;
  380. end
  381. else
  382. if (is_object) and (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
  383. begin
  384. include(current_object_option,sp_static);
  385. insert_syms(symtablestack,sc,tt,false);
  386. exclude(current_object_option,sp_static);
  387. consume(_STATIC);
  388. consume(_SEMICOLON);
  389. symdone:=true;
  390. end;
  391. end;
  392. { insert it in the symtable, if not done yet }
  393. if not symdone then
  394. begin
  395. { save object option, because we can turn of the sp_published }
  396. if (sp_published in current_object_option) and
  397. not(is_class(tt.def)) then
  398. begin
  399. Message(parser_e_cant_publish_that);
  400. exclude(current_object_option,sp_published);
  401. end
  402. else
  403. if (sp_published in current_object_option) and
  404. not(oo_can_have_published in pobjectdef(tt.def)^.objectoptions) then
  405. begin
  406. Message(parser_e_only_publishable_classes_can__be_published);
  407. exclude(current_object_option,sp_published);
  408. end;
  409. insert_syms(symtablestack,sc,tt,is_threadvar);
  410. current_object_option:=old_current_object_option;
  411. end;
  412. end;
  413. { Check for Case }
  414. if is_record and (token=_CASE) then
  415. begin
  416. maxsize:=0;
  417. maxalignment:=0;
  418. consume(_CASE);
  419. s:=pattern;
  420. getsym(s,false);
  421. { may be only a type: }
  422. if assigned(srsym) and (srsym^.typ in [typesym,unitsym]) then
  423. read_type(casetype,'')
  424. else
  425. begin
  426. consume(_ID);
  427. consume(_COLON);
  428. read_type(casetype,'');
  429. symtablestack^.insert(new(pvarsym,init(s,casetype)));
  430. end;
  431. if not(is_ordinal(casetype.def)) or is_64bitint(casetype.def) then
  432. Message(type_e_ordinal_expr_expected);
  433. consume(_OF);
  434. {$ifdef UseUnionSymtable}
  435. UnionSymtable:=new(pstoredsymtable,init(recordsymtable));
  436. UnionSymtable^.next:=symtablestack;
  437. registerdef:=false;
  438. UnionDef:=new(precorddef,init(unionsymtable));
  439. registerdef:=true;
  440. symtablestack:=UnionSymtable;
  441. {$endif UseUnionSymtable}
  442. startvarrecsize:=symtablestack^.datasize;
  443. startvarrecalign:=symtablestack^.dataalignment;
  444. repeat
  445. repeat
  446. pt:=comp_expr(true);
  447. do_firstpass(pt);
  448. if not(pt.nodetype=ordconstn) then
  449. Message(cg_e_illegal_expression);
  450. pt.free;
  451. if token=_COMMA then
  452. consume(_COMMA)
  453. else
  454. break;
  455. until false;
  456. consume(_COLON);
  457. { read the vars }
  458. consume(_LKLAMMER);
  459. inc(variantrecordlevel);
  460. if token<>_RKLAMMER then
  461. read_var_decs(true,false,false);
  462. dec(variantrecordlevel);
  463. consume(_RKLAMMER);
  464. { calculates maximal variant size }
  465. maxsize:=max(maxsize,symtablestack^.datasize);
  466. maxalignment:=max(maxalignment,symtablestack^.dataalignment);
  467. { the items of the next variant are overlayed }
  468. symtablestack^.datasize:=startvarrecsize;
  469. symtablestack^.dataalignment:=startvarrecalign;
  470. if (token<>_END) and (token<>_RKLAMMER) then
  471. consume(_SEMICOLON)
  472. else
  473. break;
  474. until (token=_END) or (token=_RKLAMMER);
  475. { at last set the record size to that of the biggest variant }
  476. symtablestack^.datasize:=maxsize;
  477. symtablestack^.dataalignment:=maxalignment;
  478. {$ifdef UseUnionSymtable}
  479. uniontype.def:=uniondef;
  480. uniontype.sym:=nil;
  481. UnionSym:=new(pvarsym,init('case',uniontype));
  482. symtablestack:=symtablestack^.next;
  483. { we do NOT call symtablestack^.insert
  484. on purpose PM }
  485. offset:=align_from_size(symtablestack^.datasize,maxalignment);
  486. symtablestack^.datasize:=offset+unionsymtable^.datasize;
  487. if maxalignment>symtablestack^.dataalignment then
  488. symtablestack^.dataalignment:=maxalignment;
  489. pstoredsymtable(UnionSymtable)^.Insert_in(symtablestack,offset);
  490. UnionSym^.owner:=nil;
  491. dispose(unionsym,done);
  492. dispose(uniondef,done);
  493. {$endif UseUnionSymtable}
  494. end;
  495. block_type:=old_block_type;
  496. current_object_option:=old_current_object_option;
  497. end;
  498. end.
  499. {
  500. $Log$
  501. Revision 1.6 2000-12-25 00:07:27 peter
  502. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  503. tlinkedlist objects)
  504. Revision 1.5 2000/12/17 14:00:18 peter
  505. * fixed static variables
  506. Revision 1.4 2000/11/29 00:30:36 florian
  507. * unused units removed from uses clause
  508. * some changes for widestrings
  509. Revision 1.3 2000/11/04 14:25:20 florian
  510. + merged Attila's changes for interfaces, not tested yet
  511. Revision 1.2 2000/10/31 22:02:49 peter
  512. * symtable splitted, no real code changes
  513. Revision 1.1 2000/10/14 10:14:51 peter
  514. * moehrendorf oct 2000 rewrite
  515. }