pdecl.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Does declaration (but not type) parsing for Free Pascal
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pdecl;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. { global }
  23. globals,
  24. { symtable }
  25. symsym,
  26. { pass_1 }
  27. node;
  28. function readconstant(const name:string;const filepos:tfileposinfo):tconstsym;
  29. procedure const_dec;
  30. procedure label_dec;
  31. procedure type_dec;
  32. procedure var_dec;
  33. procedure threadvar_dec;
  34. procedure resourcestring_dec;
  35. implementation
  36. uses
  37. { common }
  38. cutils,cclasses,
  39. { global }
  40. globtype,tokens,verbose,
  41. systems,
  42. { aasm }
  43. aasm,
  44. { symtable }
  45. symconst,symbase,symtype,symdef,symtable,
  46. { pass 1 }
  47. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  48. { parser }
  49. scanner,
  50. pbase,pexpr,ptype,ptconst,pdecsub,pdecvar,pdecobj;
  51. function readconstant(const name:string;const filepos:tfileposinfo):tconstsym;
  52. var
  53. hp : tconstsym;
  54. p : tnode;
  55. ps : pconstset;
  56. pd : pbestreal;
  57. sp : pchar;
  58. storetokenpos : tfileposinfo;
  59. begin
  60. readconstant:=nil;
  61. if name='' then
  62. internalerror(9584582);
  63. hp:=nil;
  64. p:=comp_expr(true);
  65. storetokenpos:=akttokenpos;
  66. akttokenpos:=filepos;
  67. case p.nodetype of
  68. ordconstn:
  69. begin
  70. if is_constintnode(p) then
  71. hp:=tconstsym.create_typed(name,constint,tordconstnode(p).value,tordconstnode(p).resulttype)
  72. else if is_constcharnode(p) then
  73. hp:=tconstsym.create(name,constchar,tordconstnode(p).value)
  74. else if is_constboolnode(p) then
  75. hp:=tconstsym.create(name,constbool,tordconstnode(p).value)
  76. else if p.resulttype.def.deftype=enumdef then
  77. hp:=tconstsym.create_typed(name,constord,tordconstnode(p).value,p.resulttype)
  78. else if p.resulttype.def.deftype=pointerdef then
  79. hp:=tconstsym.create_typed(name,constord,tordconstnode(p).value,p.resulttype)
  80. else internalerror(111);
  81. end;
  82. stringconstn:
  83. begin
  84. getmem(sp,tstringconstnode(p).len+1);
  85. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  86. hp:=tconstsym.create_string(name,conststring,sp,tstringconstnode(p).len);
  87. end;
  88. realconstn :
  89. begin
  90. new(pd);
  91. pd^:=trealconstnode(p).value_real;
  92. hp:=tconstsym.create(name,constreal,longint(pd));
  93. end;
  94. setconstn :
  95. begin
  96. new(ps);
  97. ps^:=tsetconstnode(p).value_set^;
  98. hp:=tconstsym.create_typed(name,constset,longint(ps),p.resulttype);
  99. end;
  100. pointerconstn :
  101. begin
  102. hp:=tconstsym.create_typed(name,constpointer,tordconstnode(p).value,p.resulttype);
  103. end;
  104. niln :
  105. begin
  106. hp:=tconstsym.create_typed(name,constnil,0,p.resulttype);
  107. end;
  108. else
  109. Message(cg_e_illegal_expression);
  110. end;
  111. akttokenpos:=storetokenpos;
  112. p.free;
  113. readconstant:=hp;
  114. end;
  115. procedure const_dec;
  116. var
  117. name : stringid;
  118. tt : ttype;
  119. sym : tsym;
  120. storetokenpos,filepos : tfileposinfo;
  121. old_block_type : tblock_type;
  122. skipequal : boolean;
  123. begin
  124. consume(_CONST);
  125. old_block_type:=block_type;
  126. block_type:=bt_const;
  127. repeat
  128. name:=pattern;
  129. filepos:=akttokenpos;
  130. consume(_ID);
  131. case token of
  132. _EQUAL:
  133. begin
  134. consume(_EQUAL);
  135. sym:=readconstant(name,filepos);
  136. if assigned(sym) then
  137. symtablestack.insert(sym);
  138. consume(_SEMICOLON);
  139. end;
  140. _COLON:
  141. begin
  142. { set the blocktype first so a consume also supports a
  143. caret, to support const s : ^string = nil }
  144. block_type:=bt_type;
  145. consume(_COLON);
  146. ignore_equal:=true;
  147. read_type(tt,'');
  148. ignore_equal:=false;
  149. block_type:=bt_const;
  150. skipequal:=false;
  151. { create symbol }
  152. storetokenpos:=akttokenpos;
  153. akttokenpos:=filepos;
  154. {$ifdef DELPHI_CONST_IN_RODATA}
  155. if m_delphi in aktmodeswitches then
  156. begin
  157. if assigned(readtypesym) then
  158. sym:=ttypedconstsym.createsym(name,readtypesym,true)
  159. else
  160. sym:=ttypedconstsym.create(name,def,true)
  161. end
  162. else
  163. {$endif DELPHI_CONST_IN_RODATA}
  164. begin
  165. sym:=ttypedconstsym.createtype(name,tt,false)
  166. end;
  167. akttokenpos:=storetokenpos;
  168. symtablestack.insert(sym);
  169. { procvar can have proc directives }
  170. if (tt.def.deftype=procvardef) then
  171. begin
  172. { support p : procedure;stdcall=nil; }
  173. if (token=_SEMICOLON) then
  174. begin
  175. consume(_SEMICOLON);
  176. if is_proc_directive(token) then
  177. parse_var_proc_directives(sym)
  178. else
  179. begin
  180. Message(parser_e_proc_directive_expected);
  181. skipequal:=true;
  182. end;
  183. end
  184. else
  185. { support p : procedure stdcall=nil; }
  186. begin
  187. if is_proc_directive(token) then
  188. parse_var_proc_directives(sym);
  189. end;
  190. end;
  191. if not skipequal then
  192. begin
  193. { get init value }
  194. consume(_EQUAL);
  195. {$ifdef DELPHI_CONST_IN_RODATA}
  196. if m_delphi in aktmodeswitches then
  197. readtypedconst(tt,ttypedconstsym(sym),true)
  198. else
  199. {$endif DELPHI_CONST_IN_RODATA}
  200. readtypedconst(tt,ttypedconstsym(sym),false);
  201. consume(_SEMICOLON);
  202. end;
  203. end;
  204. else
  205. { generate an error }
  206. consume(_EQUAL);
  207. end;
  208. until token<>_ID;
  209. block_type:=old_block_type;
  210. end;
  211. procedure label_dec;
  212. var
  213. hl : tasmlabel;
  214. begin
  215. consume(_LABEL);
  216. if not(cs_support_goto in aktmoduleswitches) then
  217. Message(sym_e_goto_and_label_not_supported);
  218. repeat
  219. if not(token in [_ID,_INTCONST]) then
  220. consume(_ID)
  221. else
  222. begin
  223. if (cs_create_smart in aktmoduleswitches) then
  224. begin
  225. getdatalabel(hl);
  226. { we still want a warning if unused }
  227. hl.refs:=0;
  228. end
  229. else
  230. getlabel(hl);
  231. symtablestack.insert(tlabelsym.create(pattern,hl));
  232. consume(token);
  233. end;
  234. if token<>_SEMICOLON then consume(_COMMA);
  235. until not(token in [_ID,_INTCONST]);
  236. consume(_SEMICOLON);
  237. end;
  238. { search in symtablestack used, but not defined type }
  239. procedure resolve_type_forward(p : tnamedindexitem);
  240. var
  241. hpd,pd : tdef;
  242. stpos : tfileposinfo;
  243. again : boolean;
  244. srsym : tsym;
  245. srsymtable : tsymtable;
  246. begin
  247. { Check only typesyms or record/object fields }
  248. case tsym(p).typ of
  249. typesym :
  250. pd:=ttypesym(p).restype.def;
  251. varsym :
  252. if (tsym(p).owner.symtabletype in [objectsymtable,recordsymtable]) then
  253. pd:=tvarsym(p).vartype.def
  254. else
  255. exit;
  256. else
  257. exit;
  258. end;
  259. repeat
  260. again:=false;
  261. case pd.deftype of
  262. arraydef :
  263. begin
  264. { elementtype could also be defined using a forwarddef }
  265. pd:=tarraydef(pd).elementtype.def;
  266. again:=true;
  267. end;
  268. pointerdef,
  269. classrefdef :
  270. begin
  271. { classrefdef inherits from pointerdef }
  272. hpd:=tpointerdef(pd).pointertype.def;
  273. { still a forward def ? }
  274. if hpd.deftype=forwarddef then
  275. begin
  276. { try to resolve the forward }
  277. { get the correct position for it }
  278. stpos:=akttokenpos;
  279. akttokenpos:=tforwarddef(hpd).forwardpos;
  280. resolving_forward:=true;
  281. make_ref:=false;
  282. searchsym(tforwarddef(hpd).tosymname,srsym,srsymtable);
  283. make_ref:=true;
  284. resolving_forward:=false;
  285. akttokenpos:=stpos;
  286. { we don't need the forwarddef anymore, dispose it }
  287. hpd.free;
  288. tpointerdef(pd).pointertype.def:=nil; { if error occurs }
  289. { was a type sym found ? }
  290. if assigned(srsym) and
  291. (srsym.typ=typesym) then
  292. begin
  293. tpointerdef(pd).pointertype.setsym(srsym);
  294. { avoid wrong unused warnings web bug 801 PM }
  295. inc(tstoredsym(srsym).refs);
  296. {$ifdef GDB}
  297. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  298. (tsym(p).owner.symtabletype in [globalsymtable,staticsymtable]) then
  299. begin
  300. ttypesym(p).isusedinstab := true;
  301. ttypesym(p).concatstabto(debuglist);
  302. end;
  303. {$endif GDB}
  304. { we need a class type for classrefdef }
  305. if (pd.deftype=classrefdef) and
  306. not(is_class(ttypesym(srsym).restype.def)) then
  307. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename);
  308. end
  309. else
  310. begin
  311. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  312. { try to recover }
  313. tpointerdef(pd).pointertype:=generrortype;
  314. end;
  315. end;
  316. end;
  317. recorddef :
  318. trecorddef(pd).symtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}resolve_type_forward);
  319. objectdef :
  320. begin
  321. if not(m_fpc in aktmodeswitches) and
  322. (oo_is_forward in tobjectdef(pd).objectoptions) then
  323. begin
  324. { only give an error as the implementation may follow in an
  325. other type block which is allowed by FPC modes }
  326. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  327. end
  328. else
  329. begin
  330. { Check all fields of the object declaration, but don't
  331. check objectdefs in objects/records, because these
  332. can't exist (anonymous objects aren't allowed) }
  333. if not(tsym(p).owner.symtabletype in [objectsymtable,recordsymtable]) then
  334. tobjectdef(pd).symtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}resolve_type_forward);
  335. end;
  336. end;
  337. end;
  338. until not again;
  339. end;
  340. { reads a type declaration to the symbol table }
  341. procedure type_dec;
  342. var
  343. typename,orgtypename : stringid;
  344. newtype : ttypesym;
  345. sym : tsym;
  346. srsymtable : tsymtable;
  347. tt : ttype;
  348. defpos,storetokenpos : tfileposinfo;
  349. old_block_type : tblock_type;
  350. begin
  351. old_block_type:=block_type;
  352. block_type:=bt_type;
  353. consume(_TYPE);
  354. typecanbeforward:=true;
  355. repeat
  356. typename:=pattern;
  357. orgtypename:=orgpattern;
  358. defpos:=akttokenpos;
  359. consume(_ID);
  360. consume(_EQUAL);
  361. { support 'ttype=type word' syntax }
  362. if token=_TYPE then
  363. Consume(_TYPE);
  364. { is the type already defined? }
  365. searchsym(typename,sym,srsymtable);
  366. newtype:=nil;
  367. { found a symbol with this name? }
  368. if assigned(sym) then
  369. begin
  370. if (sym.typ=typesym) then
  371. begin
  372. if ((token=_CLASS) or
  373. (token=_INTERFACE)) and
  374. (assigned(ttypesym(sym).restype.def)) and
  375. is_class_or_interface(ttypesym(sym).restype.def) and
  376. (oo_is_forward in tobjectdef(ttypesym(sym).restype.def).objectoptions) then
  377. begin
  378. { we can ignore the result }
  379. { the definition is modified }
  380. object_dec(orgtypename,tobjectdef(ttypesym(sym).restype.def));
  381. newtype:=ttypesym(sym);
  382. end;
  383. end;
  384. end;
  385. { no old type reused ? Then insert this new type }
  386. if not assigned(newtype) then
  387. begin
  388. { insert the new type first with an errordef, so that
  389. referencing the type before it's really set it
  390. will give an error (PFV) }
  391. tt:=generrortype;
  392. storetokenpos:=akttokenpos;
  393. newtype:=ttypesym.create(orgtypename,tt);
  394. symtablestack.insert(newtype);
  395. akttokenpos:=defpos;
  396. akttokenpos:=storetokenpos;
  397. { read the type definition }
  398. read_type(tt,orgtypename);
  399. { update the definition of the type }
  400. newtype.restype:=tt;
  401. if not assigned(tt.sym) then
  402. tt.sym:=newtype;
  403. if assigned(tt.def) and not assigned(tt.def.typesym) then
  404. tt.def.typesym:=newtype;
  405. { KAZ: handle TGUID declaration in system unit }
  406. if (cs_compilesystem in aktmoduleswitches) and not assigned(rec_tguid) and
  407. (typename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  408. assigned(tt.def) and (tt.def.deftype=recorddef) and (tt.def.size=16) then
  409. rec_tguid:=trecorddef(tt.def);
  410. end;
  411. if assigned(newtype.restype.def) then
  412. begin
  413. case newtype.restype.def.deftype of
  414. pointerdef :
  415. begin
  416. consume(_SEMICOLON);
  417. if try_to_consume(_FAR) then
  418. begin
  419. tpointerdef(newtype.restype.def).is_far:=true;
  420. consume(_SEMICOLON);
  421. end;
  422. end;
  423. procvardef :
  424. begin
  425. if not is_proc_directive(token) then
  426. consume(_SEMICOLON);
  427. parse_var_proc_directives(tsym(newtype));
  428. end;
  429. else
  430. consume(_SEMICOLON);
  431. end;
  432. end;
  433. until token<>_ID;
  434. typecanbeforward:=false;
  435. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}resolve_type_forward);
  436. block_type:=old_block_type;
  437. end;
  438. procedure var_dec;
  439. { parses variable declarations and inserts them in }
  440. { the top symbol table of symtablestack }
  441. begin
  442. consume(_VAR);
  443. read_var_decs(false,false,false);
  444. end;
  445. procedure threadvar_dec;
  446. { parses thread variable declarations and inserts them in }
  447. { the top symbol table of symtablestack }
  448. begin
  449. consume(_THREADVAR);
  450. if not(symtablestack.symtabletype in [staticsymtable,globalsymtable]) then
  451. message(parser_e_threadvars_only_sg);
  452. read_var_decs(false,false,true);
  453. end;
  454. procedure resourcestring_dec;
  455. var
  456. name : stringid;
  457. p : tnode;
  458. storetokenpos,filepos : tfileposinfo;
  459. old_block_type : tblock_type;
  460. sp : pchar;
  461. begin
  462. consume(_RESOURCESTRING);
  463. if not(symtablestack.symtabletype in [staticsymtable,globalsymtable]) then
  464. message(parser_e_resourcestring_only_sg);
  465. old_block_type:=block_type;
  466. block_type:=bt_const;
  467. repeat
  468. name:=pattern;
  469. filepos:=akttokenpos;
  470. consume(_ID);
  471. case token of
  472. _EQUAL:
  473. begin
  474. consume(_EQUAL);
  475. p:=comp_expr(true);
  476. storetokenpos:=akttokenpos;
  477. akttokenpos:=filepos;
  478. case p.nodetype of
  479. ordconstn:
  480. begin
  481. if is_constcharnode(p) then
  482. begin
  483. getmem(sp,2);
  484. sp[0]:=chr(tordconstnode(p).value);
  485. sp[1]:=#0;
  486. symtablestack.insert(tconstsym.create_string(name,constresourcestring,sp,1));
  487. end
  488. else
  489. Message(cg_e_illegal_expression);
  490. end;
  491. stringconstn:
  492. begin
  493. getmem(sp,tstringconstnode(p).len+1);
  494. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  495. symtablestack.insert(tconstsym.create_string(name,constresourcestring,sp,tstringconstnode(p).len));
  496. end;
  497. else
  498. Message(cg_e_illegal_expression);
  499. end;
  500. akttokenpos:=storetokenpos;
  501. consume(_SEMICOLON);
  502. p.free;
  503. end;
  504. else consume(_EQUAL);
  505. end;
  506. until token<>_ID;
  507. block_type:=old_block_type;
  508. end;
  509. end.
  510. {
  511. $Log$
  512. Revision 1.29 2001-04-13 01:22:11 peter
  513. * symtable change to classes
  514. * range check generation and errors fixed, make cycle DEBUG=1 works
  515. * memory leaks fixed
  516. Revision 1.28 2001/04/04 22:43:50 peter
  517. * remove unnecessary calls to firstpass
  518. Revision 1.27 2001/04/04 21:30:43 florian
  519. * applied several fixes to get the DD8 Delphi Unit compiled
  520. e.g. "forward"-interfaces are working now
  521. Revision 1.26 2001/04/02 21:20:31 peter
  522. * resulttype rewrite
  523. Revision 1.25 2001/03/11 22:58:49 peter
  524. * getsym redesign, removed the globals srsym,srsymtable
  525. Revision 1.24 2000/12/25 00:07:27 peter
  526. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  527. tlinkedlist objects)
  528. Revision 1.23 2000/12/07 17:19:42 jonas
  529. * new constant handling: from now on, hex constants >$7fffffff are
  530. parsed as unsigned constants (otherwise, $80000000 got sign extended
  531. and became $ffffffff80000000), all constants in the longint range
  532. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  533. are cardinals and the rest are int64's.
  534. * added lots of longint typecast to prevent range check errors in the
  535. compiler and rtl
  536. * type casts of symbolic ordinal constants are now preserved
  537. * fixed bug where the original resulttype.def wasn't restored correctly
  538. after doing a 64bit rangecheck
  539. Revision 1.22 2000/11/29 00:30:35 florian
  540. * unused units removed from uses clause
  541. * some changes for widestrings
  542. Revision 1.21 2000/11/12 22:17:46 peter
  543. * some realname updates for messages
  544. Revision 1.20 2000/11/11 16:19:11 peter
  545. * allow far directive for pointer type declarations
  546. Revision 1.19 2000/11/04 14:25:20 florian
  547. + merged Attila's changes for interfaces, not tested yet
  548. Revision 1.18 2000/10/31 22:02:49 peter
  549. * symtable splitted, no real code changes
  550. Revision 1.17 2000/10/14 10:14:51 peter
  551. * moehrendorf oct 2000 rewrite
  552. Revision 1.16 2000/09/24 21:19:50 peter
  553. * delphi compile fixes
  554. Revision 1.15 2000/09/24 15:06:21 peter
  555. * use defines.inc
  556. Revision 1.14 2000/09/11 17:00:23 florian
  557. + first implementation of Netware Module support, thanks to
  558. Armin Diehl ([email protected]) for providing the patches
  559. Revision 1.13 2000/08/27 20:19:39 peter
  560. * store strings with case in ppu, when an internal symbol is created
  561. a '$' is prefixed so it's not automatic uppercased
  562. Revision 1.12 2000/08/27 16:11:51 peter
  563. * moved some util functions from globals,cobjects to cutils
  564. * splitted files into finput,fmodule
  565. Revision 1.11 2000/08/20 15:01:17 peter
  566. * don't allow forward class in separate type blocks for delphi (merged)
  567. Revision 1.10 2000/08/17 09:17:19 pierre
  568. * fix go32v2 cycle problem
  569. Revision 1.9 2000/08/16 18:33:53 peter
  570. * splitted namedobjectitem.next into indexnext and listnext so it
  571. can be used in both lists
  572. * don't allow "word = word" type definitions (merged)
  573. Revision 1.8 2000/08/13 13:11:28 peter
  574. * put defaultpara values in parast and changed the name to
  575. 'def<Parameter name>'
  576. Revision 1.7 2000/08/13 08:42:59 peter
  577. * support absolute refering to funcret (merged)
  578. Revision 1.6 2000/08/02 19:49:59 peter
  579. * first things for default parameters
  580. Revision 1.5 2000/07/30 17:04:43 peter
  581. * merged fixes
  582. Revision 1.4 2000/07/14 05:11:49 michael
  583. + Patch to 1.1
  584. Revision 1.3 2000/07/13 12:08:26 michael
  585. + patched to 1.1.0 with former 1.09patch from peter
  586. Revision 1.2 2000/07/13 11:32:44 michael
  587. + removed logs
  588. }