ptype.pas 21 KB

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