ptype.pas 23 KB

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