pdecl.pas 22 KB

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