pdecl.pas 23 KB

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