pdecl.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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_def(name,constint,tordconstnode(p).value,
  76. tordconstnode(p).resulttype))
  77. else if is_constcharnode(p) then
  78. hp:=new(pconstsym,init_def(name,constchar,tordconstnode(p).value,nil))
  79. else if is_constboolnode(p) then
  80. hp:=new(pconstsym,init_def(name,constbool,tordconstnode(p).value,nil))
  81. else if p.resulttype^.deftype=enumdef then
  82. hp:=new(pconstsym,init_def(name,constord,tordconstnode(p).value,p.resulttype))
  83. else if p.resulttype^.deftype=pointerdef then
  84. hp:=new(pconstsym,init_def(name,constord,tordconstnode(p).value,p.resulttype))
  85. else internalerror(111);
  86. end;
  87. stringconstn:
  88. begin
  89. getmem(sp,tstringconstnode(p).len+1);
  90. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  91. hp:=new(pconstsym,init_string(name,conststring,sp,tstringconstnode(p).len));
  92. end;
  93. realconstn :
  94. begin
  95. new(pd);
  96. pd^:=trealconstnode(p).value_real;
  97. hp:=new(pconstsym,init(name,constreal,longint(pd)));
  98. end;
  99. setconstn :
  100. begin
  101. new(ps);
  102. ps^:=tsetconstnode(p).value_set^;
  103. hp:=new(pconstsym,init_def(name,constset,longint(ps),p.resulttype));
  104. end;
  105. pointerconstn :
  106. begin
  107. hp:=new(pconstsym,init_def(name,constpointer,tordconstnode(p).value,p.resulttype));
  108. end;
  109. niln :
  110. begin
  111. hp:=new(pconstsym,init_def(name,constnil,0,p.resulttype));
  112. end;
  113. else
  114. Message(cg_e_illegal_expression);
  115. end;
  116. akttokenpos:=storetokenpos;
  117. p.free;
  118. readconstant:=hp;
  119. end;
  120. procedure const_dec;
  121. var
  122. name : stringid;
  123. tt : ttype;
  124. sym : psym;
  125. storetokenpos,filepos : tfileposinfo;
  126. old_block_type : tblock_type;
  127. skipequal : boolean;
  128. begin
  129. consume(_CONST);
  130. old_block_type:=block_type;
  131. block_type:=bt_const;
  132. repeat
  133. name:=pattern;
  134. filepos:=akttokenpos;
  135. consume(_ID);
  136. case token of
  137. _EQUAL:
  138. begin
  139. consume(_EQUAL);
  140. sym:=readconstant(name,filepos);
  141. if assigned(sym) then
  142. symtablestack^.insert(sym);
  143. consume(_SEMICOLON);
  144. end;
  145. _COLON:
  146. begin
  147. { set the blocktype first so a consume also supports a
  148. caret, to support const s : ^string = nil }
  149. block_type:=bt_type;
  150. consume(_COLON);
  151. ignore_equal:=true;
  152. read_type(tt,'');
  153. ignore_equal:=false;
  154. block_type:=bt_const;
  155. skipequal:=false;
  156. { create symbol }
  157. storetokenpos:=akttokenpos;
  158. akttokenpos:=filepos;
  159. {$ifdef DELPHI_CONST_IN_RODATA}
  160. if m_delphi in aktmodeswitches then
  161. begin
  162. if assigned(readtypesym) then
  163. sym:=new(ptypedconstsym,initsym(name,readtypesym,true))
  164. else
  165. sym:=new(ptypedconstsym,init(name,def,true))
  166. end
  167. else
  168. {$endif DELPHI_CONST_IN_RODATA}
  169. begin
  170. sym:=new(ptypedconstsym,inittype(name,tt,false))
  171. end;
  172. akttokenpos:=storetokenpos;
  173. symtablestack^.insert(sym);
  174. { procvar can have proc directives }
  175. if (tt.def^.deftype=procvardef) then
  176. begin
  177. { support p : procedure;stdcall=nil; }
  178. if (token=_SEMICOLON) then
  179. begin
  180. consume(_SEMICOLON);
  181. if is_proc_directive(token) then
  182. parse_var_proc_directives(sym)
  183. else
  184. begin
  185. Message(parser_e_proc_directive_expected);
  186. skipequal:=true;
  187. end;
  188. end
  189. else
  190. { support p : procedure stdcall=nil; }
  191. begin
  192. if is_proc_directive(token) then
  193. parse_var_proc_directives(sym);
  194. end;
  195. end;
  196. if not skipequal then
  197. begin
  198. { get init value }
  199. consume(_EQUAL);
  200. {$ifdef DELPHI_CONST_IN_RODATA}
  201. if m_delphi in aktmodeswitches then
  202. readtypedconst(tt.def,ptypedconstsym(sym),true)
  203. else
  204. {$endif DELPHI_CONST_IN_RODATA}
  205. readtypedconst(tt.def,ptypedconstsym(sym),false);
  206. consume(_SEMICOLON);
  207. end;
  208. end;
  209. else
  210. { generate an error }
  211. consume(_EQUAL);
  212. end;
  213. until token<>_ID;
  214. block_type:=old_block_type;
  215. end;
  216. procedure label_dec;
  217. var
  218. hl : pasmlabel;
  219. begin
  220. consume(_LABEL);
  221. if not(cs_support_goto in aktmoduleswitches) then
  222. Message(sym_e_goto_and_label_not_supported);
  223. repeat
  224. if not(token in [_ID,_INTCONST]) then
  225. consume(_ID)
  226. else
  227. begin
  228. if (cs_create_smart in aktmoduleswitches) then
  229. begin
  230. getdatalabel(hl);
  231. { we still want a warning if unused }
  232. hl^.refs:=0;
  233. end
  234. else
  235. getlabel(hl);
  236. symtablestack^.insert(new(plabelsym,init(pattern,hl)));
  237. consume(token);
  238. end;
  239. if token<>_SEMICOLON then consume(_COMMA);
  240. until not(token in [_ID,_INTCONST]);
  241. consume(_SEMICOLON);
  242. end;
  243. { search in symtablestack used, but not defined type }
  244. procedure resolve_type_forward(p : pnamedindexobject);
  245. var
  246. hpd,pd : pdef;
  247. stpos : tfileposinfo;
  248. again : boolean;
  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. getsym(pforwarddef(hpd)^.tosymname,false);
  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.def:=generrordef;
  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. tt : ttype;
  350. defpos,storetokenpos : tfileposinfo;
  351. old_block_type : tblock_type;
  352. begin
  353. old_block_type:=block_type;
  354. block_type:=bt_type;
  355. consume(_TYPE);
  356. typecanbeforward:=true;
  357. repeat
  358. typename:=pattern;
  359. orgtypename:=orgpattern;
  360. defpos:=akttokenpos;
  361. consume(_ID);
  362. consume(_EQUAL);
  363. { support 'ttype=type word' syntax }
  364. if token=_TYPE then
  365. Consume(_TYPE);
  366. { is the type already defined? }
  367. getsym(typename,false);
  368. sym:=srsym;
  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) and
  376. (assigned(ptypesym(sym)^.restype.def)) and
  377. is_class(ptypesym(sym)^.restype.def) and
  378. (oo_is_forward in pobjectdef(ptypesym(sym)^.restype.def)^.objectoptions) then
  379. begin
  380. { we can ignore the result }
  381. { the definition is modified }
  382. object_dec(orgtypename,pobjectdef(ptypesym(sym)^.restype.def));
  383. newtype:=ptypesym(sym);
  384. end;
  385. end;
  386. end;
  387. { no old type reused ? Then insert this new type }
  388. if not assigned(newtype) then
  389. begin
  390. { insert the new type first with an errordef, so that
  391. referencing the type before it's really set it
  392. will give an error (PFV) }
  393. tt.setdef(generrordef);
  394. storetokenpos:=akttokenpos;
  395. newtype:=new(ptypesym,init(orgtypename,tt));
  396. symtablestack^.insert(newtype);
  397. akttokenpos:=defpos;
  398. akttokenpos:=storetokenpos;
  399. { read the type definition }
  400. read_type(tt,orgtypename);
  401. { update the definition of the type }
  402. newtype^.restype:=tt;
  403. if not assigned(tt.sym) then
  404. tt.sym:=newtype;
  405. if assigned(tt.def) and not assigned(tt.def^.typesym) then
  406. tt.def^.typesym:=newtype;
  407. { KAZ: handle TGUID declaration in system unit }
  408. if (cs_compilesystem in aktmoduleswitches) and not assigned(rec_tguid) and
  409. (typename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  410. assigned(tt.def) and (tt.def^.deftype=recorddef) and (tt.def^.size=16) then
  411. rec_tguid:=precorddef(tt.def);
  412. end;
  413. if assigned(newtype^.restype.def) then
  414. begin
  415. case newtype^.restype.def^.deftype of
  416. pointerdef :
  417. begin
  418. consume(_SEMICOLON);
  419. if try_to_consume(_FAR) then
  420. begin
  421. ppointerdef(newtype^.restype.def)^.is_far:=true;
  422. consume(_SEMICOLON);
  423. end;
  424. end;
  425. procvardef :
  426. begin
  427. if not is_proc_directive(token) then
  428. consume(_SEMICOLON);
  429. parse_var_proc_directives(psym(newtype));
  430. end;
  431. else
  432. consume(_SEMICOLON);
  433. end;
  434. end;
  435. until token<>_ID;
  436. typecanbeforward:=false;
  437. symtablestack^.foreach({$ifdef FPCPROCVAR}@{$endif}resolve_type_forward);
  438. block_type:=old_block_type;
  439. end;
  440. procedure var_dec;
  441. { parses variable declarations and inserts them in }
  442. { the top symbol table of symtablestack }
  443. begin
  444. consume(_VAR);
  445. read_var_decs(false,false,false);
  446. end;
  447. procedure threadvar_dec;
  448. { parses thread variable declarations and inserts them in }
  449. { the top symbol table of symtablestack }
  450. begin
  451. consume(_THREADVAR);
  452. if not(symtablestack^.symtabletype in [staticsymtable,globalsymtable]) then
  453. message(parser_e_threadvars_only_sg);
  454. read_var_decs(false,false,true);
  455. end;
  456. procedure resourcestring_dec;
  457. var
  458. name : stringid;
  459. p : tnode;
  460. storetokenpos,filepos : tfileposinfo;
  461. old_block_type : tblock_type;
  462. sp : pchar;
  463. begin
  464. consume(_RESOURCESTRING);
  465. if not(symtablestack^.symtabletype in [staticsymtable,globalsymtable]) then
  466. message(parser_e_resourcestring_only_sg);
  467. old_block_type:=block_type;
  468. block_type:=bt_const;
  469. repeat
  470. name:=pattern;
  471. filepos:=akttokenpos;
  472. consume(_ID);
  473. case token of
  474. _EQUAL:
  475. begin
  476. consume(_EQUAL);
  477. p:=comp_expr(true);
  478. do_firstpass(p);
  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.24 2000-12-25 00:07:27 peter
  516. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  517. tlinkedlist objects)
  518. Revision 1.23 2000/12/07 17:19:42 jonas
  519. * new constant handling: from now on, hex constants >$7fffffff are
  520. parsed as unsigned constants (otherwise, $80000000 got sign extended
  521. and became $ffffffff80000000), all constants in the longint range
  522. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  523. are cardinals and the rest are int64's.
  524. * added lots of longint typecast to prevent range check errors in the
  525. compiler and rtl
  526. * type casts of symbolic ordinal constants are now preserved
  527. * fixed bug where the original resulttype wasn't restored correctly
  528. after doing a 64bit rangecheck
  529. Revision 1.22 2000/11/29 00:30:35 florian
  530. * unused units removed from uses clause
  531. * some changes for widestrings
  532. Revision 1.21 2000/11/12 22:17:46 peter
  533. * some realname updates for messages
  534. Revision 1.20 2000/11/11 16:19:11 peter
  535. * allow far directive for pointer type declarations
  536. Revision 1.19 2000/11/04 14:25:20 florian
  537. + merged Attila's changes for interfaces, not tested yet
  538. Revision 1.18 2000/10/31 22:02:49 peter
  539. * symtable splitted, no real code changes
  540. Revision 1.17 2000/10/14 10:14:51 peter
  541. * moehrendorf oct 2000 rewrite
  542. Revision 1.16 2000/09/24 21:19:50 peter
  543. * delphi compile fixes
  544. Revision 1.15 2000/09/24 15:06:21 peter
  545. * use defines.inc
  546. Revision 1.14 2000/09/11 17:00:23 florian
  547. + first implementation of Netware Module support, thanks to
  548. Armin Diehl ([email protected]) for providing the patches
  549. Revision 1.13 2000/08/27 20:19:39 peter
  550. * store strings with case in ppu, when an internal symbol is created
  551. a '$' is prefixed so it's not automatic uppercased
  552. Revision 1.12 2000/08/27 16:11:51 peter
  553. * moved some util functions from globals,cobjects to cutils
  554. * splitted files into finput,fmodule
  555. Revision 1.11 2000/08/20 15:01:17 peter
  556. * don't allow forward class in separate type blocks for delphi (merged)
  557. Revision 1.10 2000/08/17 09:17:19 pierre
  558. * fix go32v2 cycle problem
  559. Revision 1.9 2000/08/16 18:33:53 peter
  560. * splitted namedobjectitem.next into indexnext and listnext so it
  561. can be used in both lists
  562. * don't allow "word = word" type definitions (merged)
  563. Revision 1.8 2000/08/13 13:11:28 peter
  564. * put defaultpara values in parast and changed the name to
  565. 'def<Parameter name>'
  566. Revision 1.7 2000/08/13 08:42:59 peter
  567. * support absolute refering to funcret (merged)
  568. Revision 1.6 2000/08/02 19:49:59 peter
  569. * first things for default parameters
  570. Revision 1.5 2000/07/30 17:04:43 peter
  571. * merged fixes
  572. Revision 1.4 2000/07/14 05:11:49 michael
  573. + Patch to 1.1
  574. Revision 1.3 2000/07/13 12:08:26 michael
  575. + patched to 1.1.0 with former 1.09patch from peter
  576. Revision 1.2 2000/07/13 11:32:44 michael
  577. + removed logs
  578. }