ptype.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. lowval:=porddef(p)^.low;
  313. highval:=porddef(p)^.high;
  314. arraytype:=p;
  315. end;
  316. else
  317. Message(sym_e_error_in_type_def);
  318. end;
  319. end;
  320. begin
  321. consume(_ARRAY);
  322. { open array? }
  323. if token=_LECKKLAMMER then
  324. begin
  325. consume(_LECKKLAMMER);
  326. { defaults }
  327. arraytype:=generrordef;
  328. lowval:=$80000000;
  329. highval:=$7fffffff;
  330. tt.reset;
  331. repeat
  332. { read the expression and check it, check apart if the
  333. declaration is an enum declaration because that needs to
  334. be parsed by readtype (PFV) }
  335. if token=_LKLAMMER then
  336. begin
  337. read_type(ht,'');
  338. setdefdecl(ht.def);
  339. end
  340. else
  341. begin
  342. pt:=expr;
  343. if pt.nodetype=typen then
  344. setdefdecl(pt.resulttype)
  345. else
  346. begin
  347. do_firstpass(pt);
  348. if (pt.nodetype=rangen) then
  349. begin
  350. if (trangenode(pt).left.nodetype=ordconstn) and
  351. (trangenode(pt).right.nodetype=ordconstn) then
  352. begin
  353. lowval:=tordconstnode(trangenode(pt).left).value;
  354. highval:=tordconstnode(trangenode(pt).right).value;
  355. if highval<lowval then
  356. begin
  357. Message(parser_e_array_lower_less_than_upper_bound);
  358. highval:=lowval;
  359. end;
  360. arraytype:=trangenode(pt).right.resulttype;
  361. end
  362. else
  363. Message(type_e_cant_eval_constant_expr);
  364. end
  365. else
  366. Message(sym_e_error_in_type_def)
  367. end;
  368. pt.free;
  369. end;
  370. { create arraydef }
  371. if not assigned(tt.def) then
  372. begin
  373. ap:=new(parraydef,init(lowval,highval,arraytype));
  374. tt.setdef(ap);
  375. end
  376. else
  377. begin
  378. ap^.elementtype.setdef(new(parraydef,init(lowval,highval,arraytype)));
  379. ap:=parraydef(ap^.elementtype.def);
  380. end;
  381. if token=_COMMA then
  382. consume(_COMMA)
  383. else
  384. break;
  385. until false;
  386. consume(_RECKKLAMMER);
  387. end
  388. else
  389. begin
  390. ap:=new(parraydef,init(0,-1,s32bitdef));
  391. ap^.IsDynamicArray:=true;
  392. tt.setdef(ap);
  393. end;
  394. consume(_OF);
  395. read_type(tt2,'');
  396. { if no error, set element type }
  397. if assigned(ap) then
  398. ap^.elementtype:=tt2;
  399. end;
  400. begin
  401. tt.reset;
  402. case token of
  403. _STRING,_FILE:
  404. begin
  405. single_type(tt,hs,false);
  406. end;
  407. _LKLAMMER:
  408. begin
  409. consume(_LKLAMMER);
  410. { allow negativ value_str }
  411. l:=-1;
  412. aktenumdef:=new(penumdef,init);
  413. repeat
  414. s:=orgpattern;
  415. defpos:=akttokenpos;
  416. consume(_ID);
  417. { only allow assigning of specific numbers under fpc mode }
  418. if (m_fpc in aktmodeswitches) and
  419. (token=_ASSIGNMENT) then
  420. begin
  421. consume(_ASSIGNMENT);
  422. v:=get_intconst;
  423. { please leave that a note, allows type save }
  424. { declarations in the win32 units ! }
  425. if v<=l then
  426. Message(parser_n_duplicate_enum);
  427. l:=v;
  428. end
  429. else
  430. inc(l);
  431. storepos:=akttokenpos;
  432. akttokenpos:=defpos;
  433. constsymtable^.insert(new(penumsym,init(s,aktenumdef,l)));
  434. akttokenpos:=storepos;
  435. until not try_to_consume(_COMMA);
  436. tt.setdef(aktenumdef);
  437. consume(_RKLAMMER);
  438. end;
  439. _ARRAY:
  440. begin
  441. array_dec;
  442. end;
  443. _SET:
  444. begin
  445. consume(_SET);
  446. consume(_OF);
  447. read_type(tt2,'');
  448. if assigned(tt2.def) then
  449. begin
  450. case tt2.def^.deftype of
  451. { don't forget that min can be negativ PM }
  452. enumdef :
  453. if penumdef(tt2.def)^.min>=0 then
  454. tt.setdef(new(psetdef,init(tt2.def,penumdef(tt2.def)^.max)))
  455. else
  456. Message(sym_e_ill_type_decl_set);
  457. orddef :
  458. begin
  459. case porddef(tt2.def)^.typ of
  460. uchar :
  461. tt.setdef(new(psetdef,init(tt2.def,255)));
  462. u8bit,u16bit,u32bit,
  463. s8bit,s16bit,s32bit :
  464. begin
  465. if (porddef(tt2.def)^.low>=0) then
  466. tt.setdef(new(psetdef,init(tt2.def,porddef(tt2.def)^.high)))
  467. else
  468. Message(sym_e_ill_type_decl_set);
  469. end;
  470. else
  471. Message(sym_e_ill_type_decl_set);
  472. end;
  473. end;
  474. else
  475. Message(sym_e_ill_type_decl_set);
  476. end;
  477. end
  478. else
  479. tt.setdef(generrordef);
  480. end;
  481. _CARET:
  482. begin
  483. consume(_CARET);
  484. single_type(tt2,hs,typecanbeforward);
  485. tt.setdef(new(ppointerdef,init(tt2)));
  486. end;
  487. _RECORD:
  488. begin
  489. tt.setdef(record_dec);
  490. end;
  491. _PACKED:
  492. begin
  493. consume(_PACKED);
  494. if token=_ARRAY then
  495. array_dec
  496. else
  497. begin
  498. oldaktpackrecords:=aktpackrecords;
  499. aktpackrecords:=packrecord_1;
  500. if token in [_CLASS,_OBJECT] then
  501. tt.setdef(object_dec(name,nil))
  502. else
  503. tt.setdef(record_dec);
  504. aktpackrecords:=oldaktpackrecords;
  505. end;
  506. end;
  507. _CLASS,
  508. _CPPCLASS,
  509. _INTERFACE,
  510. _OBJECT:
  511. begin
  512. tt.setdef(object_dec(name,nil));
  513. end;
  514. _PROCEDURE:
  515. begin
  516. consume(_PROCEDURE);
  517. tt.setdef(new(pprocvardef,init));
  518. if token=_LKLAMMER then
  519. parameter_dec(pprocvardef(tt.def));
  520. if token=_OF then
  521. begin
  522. consume(_OF);
  523. consume(_OBJECT);
  524. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  525. end;
  526. end;
  527. _FUNCTION:
  528. begin
  529. consume(_FUNCTION);
  530. tt.def:=new(pprocvardef,init);
  531. if token=_LKLAMMER then
  532. parameter_dec(pprocvardef(tt.def));
  533. consume(_COLON);
  534. single_type(pprocvardef(tt.def)^.rettype,hs,false);
  535. if token=_OF then
  536. begin
  537. consume(_OF);
  538. consume(_OBJECT);
  539. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  540. end;
  541. end;
  542. else
  543. expr_type;
  544. end;
  545. if tt.def=nil then
  546. tt.setdef(generrordef);
  547. end;
  548. end.
  549. {
  550. $Log$
  551. Revision 1.14 2000-11-04 14:25:21 florian
  552. + merged Attila's changes for interfaces, not tested yet
  553. Revision 1.13 2000/10/31 22:02:51 peter
  554. * symtable splitted, no real code changes
  555. Revision 1.12 2000/10/26 21:54:03 peter
  556. * fixed crash with error in child definition (merged)
  557. Revision 1.11 2000/10/21 18:16:12 florian
  558. * a lot of changes:
  559. - basic dyn. array support
  560. - basic C++ support
  561. - some work for interfaces done
  562. ....
  563. Revision 1.10 2000/10/14 10:14:52 peter
  564. * moehrendorf oct 2000 rewrite
  565. Revision 1.9 2000/09/24 15:06:25 peter
  566. * use defines.inc
  567. Revision 1.8 2000/08/27 20:19:39 peter
  568. * store strings with case in ppu, when an internal symbol is created
  569. a '$' is prefixed so it's not automatic uppercased
  570. Revision 1.7 2000/08/27 16:11:52 peter
  571. * moved some util functions from globals,cobjects to cutils
  572. * splitted files into finput,fmodule
  573. Revision 1.6 2000/08/16 18:33:54 peter
  574. * splitted namedobjectitem.next into indexnext and listnext so it
  575. can be used in both lists
  576. * don't allow "word = word" type definitions (merged)
  577. Revision 1.5 2000/08/06 14:17:15 peter
  578. * overload fixes (merged)
  579. Revision 1.4 2000/07/30 17:04:43 peter
  580. * merged fixes
  581. Revision 1.3 2000/07/13 12:08:27 michael
  582. + patched to 1.1.0 with former 1.09patch from peter
  583. Revision 1.2 2000/07/13 11:32:47 michael
  584. + removed logs
  585. }