ptype.pas 22 KB

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