ptype.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Does parsing types 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 ptype;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. globtype,symtype;
  23. const
  24. { forward types should only be possible inside a TYPE statement }
  25. typecanbeforward : boolean = false;
  26. var
  27. { hack, which allows to use the current parsed }
  28. { object type as function argument type }
  29. testcurobject : byte;
  30. curobjectname : stringid;
  31. { reads a string, file type or a type id and returns a name and }
  32. { pdef }
  33. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  34. procedure read_type(var tt:ttype;const name : stringid);
  35. { reads a type definition }
  36. { to a appropriating pdef, s gets the name of }
  37. { the type to allow name mangling }
  38. procedure id_type(var tt : ttype;var s : string;isforwarddef:boolean);
  39. implementation
  40. uses
  41. { common }
  42. cutils,cobjects,cpuinfo,
  43. { global }
  44. globals,tokens,verbose,
  45. systems,
  46. { symtable }
  47. symconst,symbase,symdef,symsym,symtable,types,
  48. { pass 1 }
  49. node,pass_1,
  50. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  51. { parser }
  52. scanner,
  53. pbase,pexpr,pdecsub,pdecvar,pdecobj;
  54. procedure id_type(var tt : ttype;var s : string;isforwarddef:boolean);
  55. { reads a type definition }
  56. { to a appropriating pdef, s gets the name of }
  57. { the type to allow name mangling }
  58. var
  59. is_unit_specific : boolean;
  60. pos : tfileposinfo;
  61. begin
  62. s:=pattern;
  63. pos:=akttokenpos;
  64. { classes can be used also in classes }
  65. if (curobjectname=pattern) and is_class_or_interface(aktobjectdef) then
  66. begin
  67. tt.setdef(aktobjectdef);
  68. consume(_ID);
  69. exit;
  70. end;
  71. { objects can be parameters }
  72. if (testcurobject=2) and (curobjectname=pattern) then
  73. begin
  74. tt.setdef(aktobjectdef);
  75. consume(_ID);
  76. exit;
  77. end;
  78. { try to load the symbol to see if it's a unitsym }
  79. is_unit_specific:=false;
  80. getsym(s,false);
  81. consume(_ID);
  82. if assigned(srsym) and
  83. (srsym^.typ=unitsym) then
  84. begin
  85. consume(_POINT);
  86. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  87. pos:=akttokenpos;
  88. s:=pattern;
  89. consume(_ID);
  90. is_unit_specific:=true;
  91. end;
  92. { are we parsing a possible forward def ? }
  93. if isforwarddef and
  94. not(is_unit_specific) then
  95. begin
  96. tt.setdef(new(pforwarddef,init(s,pos)));
  97. exit;
  98. end;
  99. { unknown sym ? }
  100. if not assigned(srsym) then
  101. begin
  102. Message1(sym_e_id_not_found,s);
  103. tt.setdef(generrordef);
  104. exit;
  105. end;
  106. { type sym ? }
  107. if (srsym^.typ<>typesym) then
  108. begin
  109. Message(type_e_type_id_expected);
  110. tt.setdef(generrordef);
  111. exit;
  112. end;
  113. { Types are first defined with an error def before assigning
  114. the real type so check if it's an errordef. if so then
  115. give an error }
  116. if (ptypesym(srsym)^.restype.def=generrordef) then
  117. begin
  118. Message(sym_e_error_in_type_def);
  119. tt.setdef(generrordef);
  120. exit;
  121. end;
  122. { Only use the definitions for system/current unit, becuase
  123. they can be refered from the parameters and symbols are not
  124. loaded at that time. A symbol reference to an other unit
  125. is still possible, because it's already loaded (PFV)
  126. can't use in [] here, becuase unitid can be > 255 }
  127. if (ptypesym(srsym)^.owner^.unitid=0) or
  128. (ptypesym(srsym)^.owner^.unitid=1) then
  129. tt.setdef(ptypesym(srsym)^.restype.def)
  130. else
  131. tt.setsym(srsym);
  132. end;
  133. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  134. { reads a string, file type or a type id and returns a name and }
  135. { pdef }
  136. var
  137. hs : string;
  138. t2 : ttype;
  139. begin
  140. case token of
  141. _STRING:
  142. begin
  143. tt.setdef(string_dec);
  144. s:='STRING';
  145. end;
  146. _FILE:
  147. begin
  148. consume(_FILE);
  149. if token=_OF then
  150. begin
  151. consume(_OF);
  152. single_type(t2,hs,false);
  153. tt.setdef(new(pfiledef,inittyped(t2)));
  154. s:='FILE$OF$'+hs;
  155. end
  156. else
  157. begin
  158. tt.setdef(cfiledef);
  159. s:='FILE';
  160. end;
  161. end;
  162. else
  163. begin
  164. id_type(tt,s,isforwarddef);
  165. end;
  166. end;
  167. end;
  168. { reads a record declaration }
  169. function record_dec : pdef;
  170. var
  171. symtable : psymtable;
  172. storetypecanbeforward : boolean;
  173. begin
  174. { create recdef }
  175. symtable:=new(pstoredsymtable,init(recordsymtable));
  176. record_dec:=new(precorddef,init(symtable));
  177. { update symtable stack }
  178. symtable^.next:=symtablestack;
  179. symtablestack:=symtable;
  180. { parse record }
  181. consume(_RECORD);
  182. storetypecanbeforward:=typecanbeforward;
  183. { for tp mode don't allow forward types }
  184. if m_tp in aktmodeswitches then
  185. typecanbeforward:=false;
  186. read_var_decs(true,false,false);
  187. consume(_END);
  188. typecanbeforward:=storetypecanbeforward;
  189. { may be scale record size to a size of n*4 ? }
  190. symtablestack^.datasize:=align(symtablestack^.datasize,symtablestack^.dataalignment);
  191. { restore symtable stack }
  192. symtablestack:=symtable^.next;
  193. end;
  194. { reads a type definition and returns a pointer to it }
  195. procedure read_type(var tt : ttype;const name : stringid);
  196. var
  197. pt : tnode;
  198. tt2 : ttype;
  199. aktenumdef : penumdef;
  200. ap : parraydef;
  201. s : stringid;
  202. l,v : TConstExprInt;
  203. oldaktpackrecords : tpackrecords;
  204. hs : string;
  205. defpos,storepos : tfileposinfo;
  206. procedure expr_type;
  207. var
  208. pt1,pt2 : tnode;
  209. begin
  210. { use of current parsed object ? }
  211. if (token=_ID) and (testcurobject=2) and (curobjectname=pattern) then
  212. begin
  213. consume(_ID);
  214. tt.setdef(aktobjectdef);
  215. exit;
  216. end;
  217. { classes can be used also in classes }
  218. if (curobjectname=pattern) and is_class_or_interface(aktobjectdef) then
  219. begin
  220. tt.setdef(aktobjectdef);
  221. consume(_ID);
  222. exit;
  223. end;
  224. { we can't accept a equal in type }
  225. pt1:=comp_expr(not(ignore_equal));
  226. do_firstpass(pt1);
  227. if (token=_POINTPOINT) then
  228. begin
  229. consume(_POINTPOINT);
  230. { get high value of range }
  231. pt2:=comp_expr(not(ignore_equal));
  232. do_firstpass(pt2);
  233. { both must be evaluated to constants now }
  234. if (pt1.nodetype=ordconstn) and
  235. (pt2.nodetype=ordconstn) then
  236. begin
  237. { check types }
  238. if CheckTypes(pt1.resulttype,pt2.resulttype) then
  239. begin
  240. { Check bounds }
  241. if tordconstnode(pt2).value<tordconstnode(pt1).value then
  242. Message(cg_e_upper_lower_than_lower)
  243. else
  244. begin
  245. { All checks passed, create the new def }
  246. case pt1.resulttype^.deftype of
  247. enumdef :
  248. tt.setdef(new(penumdef,init_subrange(penumdef(pt1.resulttype),tordconstnode(pt1).value,tordconstnode(pt2).value)));
  249. orddef :
  250. begin
  251. if is_char(pt1.resulttype) then
  252. tt.setdef(new(porddef,init(uchar,tordconstnode(pt1).value,tordconstnode(pt2).value)))
  253. else
  254. if is_boolean(pt1.resulttype) then
  255. tt.setdef(new(porddef,init(bool8bit,tordconstnode(pt1).value,tordconstnode(pt2).value)))
  256. else
  257. tt.setdef(new(porddef,init(uauto,tordconstnode(pt1).value,tordconstnode(pt2).value)));
  258. end;
  259. end;
  260. end;
  261. end;
  262. end
  263. else
  264. Message(sym_e_error_in_type_def);
  265. pt2.free;
  266. end
  267. else
  268. begin
  269. { a simple type renaming }
  270. if (pt1.nodetype=typen) then
  271. begin
  272. if assigned(ttypenode(pt1).typenodesym) then
  273. tt.setsym(ttypenode(pt1).typenodesym)
  274. else
  275. tt.setdef(pt1.resulttype);
  276. end
  277. else
  278. Message(sym_e_error_in_type_def);
  279. end;
  280. pt1.free;
  281. end;
  282. procedure array_dec;
  283. var
  284. lowval,
  285. highval : longint;
  286. arraytype : pdef;
  287. ht : ttype;
  288. procedure setdefdecl(p:pdef);
  289. begin
  290. case p^.deftype of
  291. enumdef :
  292. begin
  293. lowval:=penumdef(p)^.min;
  294. highval:=penumdef(p)^.max;
  295. arraytype:=p;
  296. end;
  297. orddef :
  298. begin
  299. if porddef(p)^.typ in [uchar,
  300. u8bit,u16bit,
  301. s8bit,s16bit,s32bit,
  302. bool8bit,bool16bit,bool32bit,
  303. uwidechar] then
  304. begin
  305. lowval:=porddef(p)^.low;
  306. highval:=porddef(p)^.high;
  307. arraytype:=p;
  308. end
  309. else
  310. Message1(parser_e_type_cant_be_used_in_array_index,p^.gettypename);
  311. end;
  312. else
  313. Message(sym_e_error_in_type_def);
  314. end;
  315. end;
  316. begin
  317. consume(_ARRAY);
  318. { open array? }
  319. if token=_LECKKLAMMER then
  320. begin
  321. consume(_LECKKLAMMER);
  322. { defaults }
  323. arraytype:=generrordef;
  324. lowval:=longint($80000000);
  325. highval:=$7fffffff;
  326. tt.reset;
  327. repeat
  328. { read the expression and check it, check apart if the
  329. declaration is an enum declaration because that needs to
  330. be parsed by readtype (PFV) }
  331. if token=_LKLAMMER then
  332. begin
  333. read_type(ht,'');
  334. setdefdecl(ht.def);
  335. end
  336. else
  337. begin
  338. pt:=expr;
  339. if pt.nodetype=typen then
  340. setdefdecl(pt.resulttype)
  341. else
  342. begin
  343. do_firstpass(pt);
  344. if (pt.nodetype=rangen) then
  345. begin
  346. if (trangenode(pt).left.nodetype=ordconstn) and
  347. (trangenode(pt).right.nodetype=ordconstn) then
  348. begin
  349. lowval:=tordconstnode(trangenode(pt).left).value;
  350. highval:=tordconstnode(trangenode(pt).right).value;
  351. if highval<lowval then
  352. begin
  353. Message(parser_e_array_lower_less_than_upper_bound);
  354. highval:=lowval;
  355. end;
  356. arraytype:=trangenode(pt).right.resulttype;
  357. end
  358. else
  359. Message(type_e_cant_eval_constant_expr);
  360. end
  361. else
  362. Message(sym_e_error_in_type_def)
  363. end;
  364. pt.free;
  365. end;
  366. { create arraydef }
  367. if not assigned(tt.def) then
  368. begin
  369. ap:=new(parraydef,init(lowval,highval,arraytype));
  370. tt.setdef(ap);
  371. end
  372. else
  373. begin
  374. ap^.elementtype.setdef(new(parraydef,init(lowval,highval,arraytype)));
  375. ap:=parraydef(ap^.elementtype.def);
  376. end;
  377. if token=_COMMA then
  378. consume(_COMMA)
  379. else
  380. break;
  381. until false;
  382. consume(_RECKKLAMMER);
  383. end
  384. else
  385. begin
  386. ap:=new(parraydef,init(0,-1,s32bitdef));
  387. ap^.IsDynamicArray:=true;
  388. tt.setdef(ap);
  389. end;
  390. consume(_OF);
  391. read_type(tt2,'');
  392. { if no error, set element type }
  393. if assigned(ap) then
  394. ap^.elementtype:=tt2;
  395. end;
  396. begin
  397. tt.reset;
  398. case token of
  399. _STRING,_FILE:
  400. begin
  401. single_type(tt,hs,false);
  402. end;
  403. _LKLAMMER:
  404. begin
  405. consume(_LKLAMMER);
  406. { allow negativ value_str }
  407. l:=-1;
  408. aktenumdef:=new(penumdef,init);
  409. repeat
  410. s:=orgpattern;
  411. defpos:=akttokenpos;
  412. consume(_ID);
  413. { only allow assigning of specific numbers under fpc mode }
  414. if (m_fpc in aktmodeswitches) and
  415. (token=_ASSIGNMENT) then
  416. begin
  417. consume(_ASSIGNMENT);
  418. v:=get_intconst;
  419. { please leave that a note, allows type save }
  420. { declarations in the win32 units ! }
  421. if v<=l then
  422. Message(parser_n_duplicate_enum);
  423. l:=v;
  424. end
  425. else
  426. inc(l);
  427. storepos:=akttokenpos;
  428. akttokenpos:=defpos;
  429. constsymtable^.insert(new(penumsym,init(s,aktenumdef,l)));
  430. akttokenpos:=storepos;
  431. until not try_to_consume(_COMMA);
  432. tt.setdef(aktenumdef);
  433. consume(_RKLAMMER);
  434. end;
  435. _ARRAY:
  436. begin
  437. array_dec;
  438. end;
  439. _SET:
  440. begin
  441. consume(_SET);
  442. consume(_OF);
  443. read_type(tt2,'');
  444. if assigned(tt2.def) then
  445. begin
  446. case tt2.def^.deftype of
  447. { don't forget that min can be negativ PM }
  448. enumdef :
  449. if penumdef(tt2.def)^.min>=0 then
  450. tt.setdef(new(psetdef,init(tt2.def,penumdef(tt2.def)^.max)))
  451. else
  452. Message(sym_e_ill_type_decl_set);
  453. orddef :
  454. begin
  455. case porddef(tt2.def)^.typ of
  456. uchar :
  457. tt.setdef(new(psetdef,init(tt2.def,255)));
  458. u8bit,u16bit,u32bit,
  459. s8bit,s16bit,s32bit :
  460. begin
  461. if (porddef(tt2.def)^.low>=0) then
  462. tt.setdef(new(psetdef,init(tt2.def,porddef(tt2.def)^.high)))
  463. else
  464. Message(sym_e_ill_type_decl_set);
  465. end;
  466. else
  467. Message(sym_e_ill_type_decl_set);
  468. end;
  469. end;
  470. else
  471. Message(sym_e_ill_type_decl_set);
  472. end;
  473. end
  474. else
  475. tt.setdef(generrordef);
  476. end;
  477. _CARET:
  478. begin
  479. consume(_CARET);
  480. single_type(tt2,hs,typecanbeforward);
  481. tt.setdef(new(ppointerdef,init(tt2)));
  482. end;
  483. _RECORD:
  484. begin
  485. tt.setdef(record_dec);
  486. end;
  487. _PACKED:
  488. begin
  489. consume(_PACKED);
  490. if token=_ARRAY then
  491. array_dec
  492. else
  493. begin
  494. oldaktpackrecords:=aktpackrecords;
  495. aktpackrecords:=packrecord_1;
  496. if token in [_CLASS,_OBJECT] then
  497. tt.setdef(object_dec(name,nil))
  498. else
  499. tt.setdef(record_dec);
  500. aktpackrecords:=oldaktpackrecords;
  501. end;
  502. end;
  503. _CLASS,
  504. _CPPCLASS,
  505. _INTERFACE,
  506. _OBJECT:
  507. begin
  508. tt.setdef(object_dec(name,nil));
  509. end;
  510. _PROCEDURE:
  511. begin
  512. consume(_PROCEDURE);
  513. tt.setdef(new(pprocvardef,init));
  514. if token=_LKLAMMER then
  515. parameter_dec(pprocvardef(tt.def));
  516. if token=_OF then
  517. begin
  518. consume(_OF);
  519. consume(_OBJECT);
  520. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  521. end;
  522. end;
  523. _FUNCTION:
  524. begin
  525. consume(_FUNCTION);
  526. tt.def:=new(pprocvardef,init);
  527. if token=_LKLAMMER then
  528. parameter_dec(pprocvardef(tt.def));
  529. consume(_COLON);
  530. single_type(pprocvardef(tt.def)^.rettype,hs,false);
  531. if token=_OF then
  532. begin
  533. consume(_OF);
  534. consume(_OBJECT);
  535. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  536. end;
  537. end;
  538. else
  539. expr_type;
  540. end;
  541. if tt.def=nil then
  542. tt.setdef(generrordef);
  543. end;
  544. end.
  545. {
  546. $Log$
  547. Revision 1.17 2000-12-07 17:19:43 jonas
  548. * new constant handling: from now on, hex constants >$7fffffff are
  549. parsed as unsigned constants (otherwise, $80000000 got sign extended
  550. and became $ffffffff80000000), all constants in the longint range
  551. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  552. are cardinals and the rest are int64's.
  553. * added lots of longint typecast to prevent range check errors in the
  554. compiler and rtl
  555. * type casts of symbolic ordinal constants are now preserved
  556. * fixed bug where the original resulttype wasn't restored correctly
  557. after doing a 64bit rangecheck
  558. Revision 1.16 2000/11/29 00:30:38 florian
  559. * unused units removed from uses clause
  560. * some changes for widestrings
  561. Revision 1.15 2000/11/14 23:43:38 florian
  562. * fixed 1238
  563. Revision 1.14 2000/11/04 14:25:21 florian
  564. + merged Attila's changes for interfaces, not tested yet
  565. Revision 1.13 2000/10/31 22:02:51 peter
  566. * symtable splitted, no real code changes
  567. Revision 1.12 2000/10/26 21:54:03 peter
  568. * fixed crash with error in child definition (merged)
  569. Revision 1.11 2000/10/21 18:16:12 florian
  570. * a lot of changes:
  571. - basic dyn. array support
  572. - basic C++ support
  573. - some work for interfaces done
  574. ....
  575. Revision 1.10 2000/10/14 10:14:52 peter
  576. * moehrendorf oct 2000 rewrite
  577. Revision 1.9 2000/09/24 15:06:25 peter
  578. * use defines.inc
  579. Revision 1.8 2000/08/27 20:19:39 peter
  580. * store strings with case in ppu, when an internal symbol is created
  581. a '$' is prefixed so it's not automatic uppercased
  582. Revision 1.7 2000/08/27 16:11:52 peter
  583. * moved some util functions from globals,cobjects to cutils
  584. * splitted files into finput,fmodule
  585. Revision 1.6 2000/08/16 18:33:54 peter
  586. * splitted namedobjectitem.next into indexnext and listnext so it
  587. can be used in both lists
  588. * don't allow "word = word" type definitions (merged)
  589. Revision 1.5 2000/08/06 14:17:15 peter
  590. * overload fixes (merged)
  591. Revision 1.4 2000/07/30 17:04:43 peter
  592. * merged fixes
  593. Revision 1.3 2000/07/13 12:08:27 michael
  594. + patched to 1.1.0 with former 1.09patch from peter
  595. Revision 1.2 2000/07/13 11:32:47 michael
  596. + removed logs
  597. }