pdecl.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. do_firstpass(p);
  69. storetokenpos:=akttokenpos;
  70. akttokenpos:=filepos;
  71. case p.nodetype of
  72. ordconstn:
  73. begin
  74. if is_constintnode(p) then
  75. hp:=new(pconstsym,init_typed(name,constint,tordconstnode(p).value,tordconstnode(p).resulttype))
  76. else if is_constcharnode(p) then
  77. hp:=new(pconstsym,init(name,constchar,tordconstnode(p).value))
  78. else if is_constboolnode(p) then
  79. hp:=new(pconstsym,init(name,constbool,tordconstnode(p).value))
  80. else if p.resulttype.def^.deftype=enumdef then
  81. hp:=new(pconstsym,init_typed(name,constord,tordconstnode(p).value,p.resulttype))
  82. else if p.resulttype.def^.deftype=pointerdef then
  83. hp:=new(pconstsym,init_typed(name,constord,tordconstnode(p).value,p.resulttype))
  84. else internalerror(111);
  85. end;
  86. stringconstn:
  87. begin
  88. getmem(sp,tstringconstnode(p).len+1);
  89. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  90. hp:=new(pconstsym,init_string(name,conststring,sp,tstringconstnode(p).len));
  91. end;
  92. realconstn :
  93. begin
  94. new(pd);
  95. pd^:=trealconstnode(p).value_real;
  96. hp:=new(pconstsym,init(name,constreal,longint(pd)));
  97. end;
  98. setconstn :
  99. begin
  100. new(ps);
  101. ps^:=tsetconstnode(p).value_set^;
  102. hp:=new(pconstsym,init_typed(name,constset,longint(ps),p.resulttype));
  103. end;
  104. pointerconstn :
  105. begin
  106. hp:=new(pconstsym,init_typed(name,constpointer,tordconstnode(p).value,p.resulttype));
  107. end;
  108. niln :
  109. begin
  110. hp:=new(pconstsym,init_typed(name,constnil,0,p.resulttype));
  111. end;
  112. else
  113. Message(cg_e_illegal_expression);
  114. end;
  115. akttokenpos:=storetokenpos;
  116. p.free;
  117. readconstant:=hp;
  118. end;
  119. procedure const_dec;
  120. var
  121. name : stringid;
  122. tt : ttype;
  123. sym : psym;
  124. storetokenpos,filepos : tfileposinfo;
  125. old_block_type : tblock_type;
  126. skipequal : boolean;
  127. begin
  128. consume(_CONST);
  129. old_block_type:=block_type;
  130. block_type:=bt_const;
  131. repeat
  132. name:=pattern;
  133. filepos:=akttokenpos;
  134. consume(_ID);
  135. case token of
  136. _EQUAL:
  137. begin
  138. consume(_EQUAL);
  139. sym:=readconstant(name,filepos);
  140. if assigned(sym) then
  141. symtablestack^.insert(sym);
  142. consume(_SEMICOLON);
  143. end;
  144. _COLON:
  145. begin
  146. { set the blocktype first so a consume also supports a
  147. caret, to support const s : ^string = nil }
  148. block_type:=bt_type;
  149. consume(_COLON);
  150. ignore_equal:=true;
  151. read_type(tt,'');
  152. ignore_equal:=false;
  153. block_type:=bt_const;
  154. skipequal:=false;
  155. { create symbol }
  156. storetokenpos:=akttokenpos;
  157. akttokenpos:=filepos;
  158. {$ifdef DELPHI_CONST_IN_RODATA}
  159. if m_delphi in aktmodeswitches then
  160. begin
  161. if assigned(readtypesym) then
  162. sym:=new(ptypedconstsym,initsym(name,readtypesym,true))
  163. else
  164. sym:=new(ptypedconstsym,init(name,def,true))
  165. end
  166. else
  167. {$endif DELPHI_CONST_IN_RODATA}
  168. begin
  169. sym:=new(ptypedconstsym,inittype(name,tt,false))
  170. end;
  171. akttokenpos:=storetokenpos;
  172. symtablestack^.insert(sym);
  173. { procvar can have proc directives }
  174. if (tt.def^.deftype=procvardef) then
  175. begin
  176. { support p : procedure;stdcall=nil; }
  177. if (token=_SEMICOLON) then
  178. begin
  179. consume(_SEMICOLON);
  180. if is_proc_directive(token) then
  181. parse_var_proc_directives(sym)
  182. else
  183. begin
  184. Message(parser_e_proc_directive_expected);
  185. skipequal:=true;
  186. end;
  187. end
  188. else
  189. { support p : procedure stdcall=nil; }
  190. begin
  191. if is_proc_directive(token) then
  192. parse_var_proc_directives(sym);
  193. end;
  194. end;
  195. if not skipequal then
  196. begin
  197. { get init value }
  198. consume(_EQUAL);
  199. {$ifdef DELPHI_CONST_IN_RODATA}
  200. if m_delphi in aktmodeswitches then
  201. readtypedconst(tt,ptypedconstsym(sym),true)
  202. else
  203. {$endif DELPHI_CONST_IN_RODATA}
  204. readtypedconst(tt,ptypedconstsym(sym),false);
  205. consume(_SEMICOLON);
  206. end;
  207. end;
  208. else
  209. { generate an error }
  210. consume(_EQUAL);
  211. end;
  212. until token<>_ID;
  213. block_type:=old_block_type;
  214. end;
  215. procedure label_dec;
  216. var
  217. hl : pasmlabel;
  218. begin
  219. consume(_LABEL);
  220. if not(cs_support_goto in aktmoduleswitches) then
  221. Message(sym_e_goto_and_label_not_supported);
  222. repeat
  223. if not(token in [_ID,_INTCONST]) then
  224. consume(_ID)
  225. else
  226. begin
  227. if (cs_create_smart in aktmoduleswitches) then
  228. begin
  229. getdatalabel(hl);
  230. { we still want a warning if unused }
  231. hl^.refs:=0;
  232. end
  233. else
  234. getlabel(hl);
  235. symtablestack^.insert(new(plabelsym,init(pattern,hl)));
  236. consume(token);
  237. end;
  238. if token<>_SEMICOLON then consume(_COMMA);
  239. until not(token in [_ID,_INTCONST]);
  240. consume(_SEMICOLON);
  241. end;
  242. { search in symtablestack used, but not defined type }
  243. procedure resolve_type_forward(p : pnamedindexobject);
  244. var
  245. hpd,pd : pdef;
  246. stpos : tfileposinfo;
  247. again : boolean;
  248. srsym : psym;
  249. srsymtable : psymtable;
  250. begin
  251. { Check only typesyms or record/object fields }
  252. case psym(p)^.typ of
  253. typesym :
  254. pd:=ptypesym(p)^.restype.def;
  255. varsym :
  256. if (psym(p)^.owner^.symtabletype in [objectsymtable,recordsymtable]) then
  257. pd:=pvarsym(p)^.vartype.def
  258. else
  259. exit;
  260. else
  261. exit;
  262. end;
  263. repeat
  264. again:=false;
  265. case pd^.deftype of
  266. arraydef :
  267. begin
  268. { elementtype could also be defined using a forwarddef }
  269. pd:=parraydef(pd)^.elementtype.def;
  270. again:=true;
  271. end;
  272. pointerdef,
  273. classrefdef :
  274. begin
  275. { classrefdef inherits from pointerdef }
  276. hpd:=ppointerdef(pd)^.pointertype.def;
  277. { still a forward def ? }
  278. if hpd^.deftype=forwarddef then
  279. begin
  280. { try to resolve the forward }
  281. { get the correct position for it }
  282. stpos:=akttokenpos;
  283. akttokenpos:=pforwarddef(hpd)^.forwardpos;
  284. resolving_forward:=true;
  285. make_ref:=false;
  286. searchsym(pforwarddef(hpd)^.tosymname,srsym,srsymtable);
  287. make_ref:=true;
  288. resolving_forward:=false;
  289. akttokenpos:=stpos;
  290. { we don't need the forwarddef anymore, dispose it }
  291. dispose(hpd,done);
  292. ppointerdef(pd)^.pointertype.def:=nil; { if error occurs }
  293. { was a type sym found ? }
  294. if assigned(srsym) and
  295. (srsym^.typ=typesym) then
  296. begin
  297. ppointerdef(pd)^.pointertype.setsym(srsym);
  298. { avoid wrong unused warnings web bug 801 PM }
  299. inc(pstoredsym(srsym)^.refs);
  300. {$ifdef GDB}
  301. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  302. (psym(p)^.owner^.symtabletype in [globalsymtable,staticsymtable]) then
  303. begin
  304. ptypesym(p)^.isusedinstab := true;
  305. ptypesym(p)^.concatstabto(debuglist);
  306. end;
  307. {$endif GDB}
  308. { we need a class type for classrefdef }
  309. if (pd^.deftype=classrefdef) and
  310. not(is_class(ptypesym(srsym)^.restype.def)) then
  311. Message1(type_e_class_type_expected,ptypesym(srsym)^.restype.def^.typename);
  312. end
  313. else
  314. begin
  315. MessagePos1(psym(p)^.fileinfo,sym_e_forward_type_not_resolved,psym(p)^.realname);
  316. { try to recover }
  317. ppointerdef(pd)^.pointertype:=generrortype;
  318. end;
  319. end;
  320. end;
  321. recorddef :
  322. precorddef(pd)^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}resolve_type_forward);
  323. objectdef :
  324. begin
  325. if not(m_fpc in aktmodeswitches) and
  326. (oo_is_forward in pobjectdef(pd)^.objectoptions) then
  327. begin
  328. { only give an error as the implementation may follow in an
  329. other type block which is allowed by FPC modes }
  330. MessagePos1(psym(p)^.fileinfo,sym_e_forward_type_not_resolved,psym(p)^.realname);
  331. end
  332. else
  333. begin
  334. { Check all fields of the object declaration, but don't
  335. check objectdefs in objects/records, because these
  336. can't exist (anonymous objects aren't allowed) }
  337. if not(psym(p)^.owner^.symtabletype in [objectsymtable,recordsymtable]) then
  338. pobjectdef(pd)^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}resolve_type_forward);
  339. end;
  340. end;
  341. end;
  342. until not again;
  343. end;
  344. { reads a type declaration to the symbol table }
  345. procedure type_dec;
  346. var
  347. typename,orgtypename : stringid;
  348. newtype : ptypesym;
  349. sym : psym;
  350. srsymtable : psymtable;
  351. tt : ttype;
  352. defpos,storetokenpos : tfileposinfo;
  353. old_block_type : tblock_type;
  354. begin
  355. old_block_type:=block_type;
  356. block_type:=bt_type;
  357. consume(_TYPE);
  358. typecanbeforward:=true;
  359. repeat
  360. typename:=pattern;
  361. orgtypename:=orgpattern;
  362. defpos:=akttokenpos;
  363. consume(_ID);
  364. consume(_EQUAL);
  365. { support 'ttype=type word' syntax }
  366. if token=_TYPE then
  367. Consume(_TYPE);
  368. { is the type already defined? }
  369. searchsym(typename,sym,srsymtable);
  370. newtype:=nil;
  371. { found a symbol with this name? }
  372. if assigned(sym) then
  373. begin
  374. if (sym^.typ=typesym) then
  375. begin
  376. if ((token=_CLASS) or
  377. (token=_INTERFACE)) and
  378. (assigned(ptypesym(sym)^.restype.def)) and
  379. is_class_or_interface(ptypesym(sym)^.restype.def) and
  380. (oo_is_forward in pobjectdef(ptypesym(sym)^.restype.def)^.objectoptions) then
  381. begin
  382. { we can ignore the result }
  383. { the definition is modified }
  384. object_dec(orgtypename,pobjectdef(ptypesym(sym)^.restype.def));
  385. newtype:=ptypesym(sym);
  386. end;
  387. end;
  388. end;
  389. { no old type reused ? Then insert this new type }
  390. if not assigned(newtype) then
  391. begin
  392. { insert the new type first with an errordef, so that
  393. referencing the type before it's really set it
  394. will give an error (PFV) }
  395. tt:=generrortype;
  396. storetokenpos:=akttokenpos;
  397. newtype:=new(ptypesym,init(orgtypename,tt));
  398. symtablestack^.insert(newtype);
  399. akttokenpos:=defpos;
  400. akttokenpos:=storetokenpos;
  401. { read the type definition }
  402. read_type(tt,orgtypename);
  403. { update the definition of the type }
  404. newtype^.restype:=tt;
  405. if not assigned(tt.sym) then
  406. tt.sym:=newtype;
  407. if assigned(tt.def) and not assigned(tt.def^.typesym) then
  408. tt.def^.typesym:=newtype;
  409. { KAZ: handle TGUID declaration in system unit }
  410. if (cs_compilesystem in aktmoduleswitches) and not assigned(rec_tguid) and
  411. (typename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  412. assigned(tt.def) and (tt.def^.deftype=recorddef) and (tt.def^.size=16) then
  413. rec_tguid:=precorddef(tt.def);
  414. end;
  415. if assigned(newtype^.restype.def) then
  416. begin
  417. case newtype^.restype.def^.deftype of
  418. pointerdef :
  419. begin
  420. consume(_SEMICOLON);
  421. if try_to_consume(_FAR) then
  422. begin
  423. ppointerdef(newtype^.restype.def)^.is_far:=true;
  424. consume(_SEMICOLON);
  425. end;
  426. end;
  427. procvardef :
  428. begin
  429. if not is_proc_directive(token) then
  430. consume(_SEMICOLON);
  431. parse_var_proc_directives(psym(newtype));
  432. end;
  433. else
  434. consume(_SEMICOLON);
  435. end;
  436. end;
  437. until token<>_ID;
  438. typecanbeforward:=false;
  439. symtablestack^.foreach({$ifdef FPCPROCVAR}@{$endif}resolve_type_forward);
  440. block_type:=old_block_type;
  441. end;
  442. procedure var_dec;
  443. { parses variable declarations and inserts them in }
  444. { the top symbol table of symtablestack }
  445. begin
  446. consume(_VAR);
  447. read_var_decs(false,false,false);
  448. end;
  449. procedure threadvar_dec;
  450. { parses thread variable declarations and inserts them in }
  451. { the top symbol table of symtablestack }
  452. begin
  453. consume(_THREADVAR);
  454. if not(symtablestack^.symtabletype in [staticsymtable,globalsymtable]) then
  455. message(parser_e_threadvars_only_sg);
  456. read_var_decs(false,false,true);
  457. end;
  458. procedure resourcestring_dec;
  459. var
  460. name : stringid;
  461. p : tnode;
  462. storetokenpos,filepos : tfileposinfo;
  463. old_block_type : tblock_type;
  464. sp : pchar;
  465. begin
  466. consume(_RESOURCESTRING);
  467. if not(symtablestack^.symtabletype in [staticsymtable,globalsymtable]) then
  468. message(parser_e_resourcestring_only_sg);
  469. old_block_type:=block_type;
  470. block_type:=bt_const;
  471. repeat
  472. name:=pattern;
  473. filepos:=akttokenpos;
  474. consume(_ID);
  475. case token of
  476. _EQUAL:
  477. begin
  478. consume(_EQUAL);
  479. p:=comp_expr(true);
  480. do_firstpass(p);
  481. storetokenpos:=akttokenpos;
  482. akttokenpos:=filepos;
  483. case p.nodetype of
  484. ordconstn:
  485. begin
  486. if is_constcharnode(p) then
  487. begin
  488. getmem(sp,2);
  489. sp[0]:=chr(tordconstnode(p).value);
  490. sp[1]:=#0;
  491. symtablestack^.insert(new(pconstsym,init_string(name,constresourcestring,sp,1)));
  492. end
  493. else
  494. Message(cg_e_illegal_expression);
  495. end;
  496. stringconstn:
  497. begin
  498. getmem(sp,tstringconstnode(p).len+1);
  499. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  500. symtablestack^.insert(new(pconstsym,init_string(name,constresourcestring,sp,tstringconstnode(p).len)));
  501. end;
  502. else
  503. Message(cg_e_illegal_expression);
  504. end;
  505. akttokenpos:=storetokenpos;
  506. consume(_SEMICOLON);
  507. p.free;
  508. end;
  509. else consume(_EQUAL);
  510. end;
  511. until token<>_ID;
  512. block_type:=old_block_type;
  513. end;
  514. end.
  515. {
  516. $Log$
  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. }