ptype.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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,symtable;
  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,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:=tokenpos;
  77. { classes can be used also in classes }
  78. if (curobjectname=pattern) and aktobjectdef^.is_class 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:=tokenpos;
  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. exit;
  133. end;
  134. { Only use the definitions for system/current unit, becuase
  135. they can be refered from the parameters and symbols are not
  136. loaded at that time. A symbol reference to an other unit
  137. is still possible, because it's already loaded (PFV)
  138. can't use in [] here, becuase unitid can be > 255 }
  139. if (ptypesym(srsym)^.owner^.unitid=0) or
  140. (ptypesym(srsym)^.owner^.unitid=1) then
  141. tt.setdef(ptypesym(srsym)^.restype.def)
  142. else
  143. tt.setsym(srsym);
  144. end;
  145. procedure single_type(var tt:ttype;var s : string;isforwarddef:boolean);
  146. { reads a string, file type or a type id and returns a name and }
  147. { pdef }
  148. var
  149. hs : string;
  150. t2 : ttype;
  151. begin
  152. case token of
  153. _STRING:
  154. begin
  155. tt.setdef(string_dec);
  156. s:='STRING';
  157. end;
  158. _FILE:
  159. begin
  160. consume(_FILE);
  161. if token=_OF then
  162. begin
  163. consume(_OF);
  164. single_type(t2,hs,false);
  165. tt.setdef(new(pfiledef,inittyped(t2)));
  166. s:='FILE$OF$'+hs;
  167. end
  168. else
  169. begin
  170. tt.setdef(cfiledef);
  171. s:='FILE';
  172. end;
  173. end;
  174. else
  175. begin
  176. id_type(tt,s,isforwarddef);
  177. end;
  178. end;
  179. end;
  180. { reads a record declaration }
  181. function record_dec : pdef;
  182. var
  183. symtable : psymtable;
  184. storetypecanbeforward : boolean;
  185. begin
  186. { create recdef }
  187. symtable:=new(psymtable,init(recordsymtable));
  188. record_dec:=new(precorddef,init(symtable));
  189. { update symtable stack }
  190. symtable^.next:=symtablestack;
  191. symtablestack:=symtable;
  192. { parse record }
  193. consume(_RECORD);
  194. storetypecanbeforward:=typecanbeforward;
  195. { for tp mode don't allow forward types }
  196. if m_tp in aktmodeswitches then
  197. typecanbeforward:=false;
  198. read_var_decs(true,false,false);
  199. consume(_END);
  200. typecanbeforward:=storetypecanbeforward;
  201. { may be scale record size to a size of n*4 ? }
  202. symtablestack^.datasize:=align(symtablestack^.datasize,symtablestack^.dataalignment);
  203. { restore symtable stack }
  204. symtablestack:=symtable^.next;
  205. end;
  206. { reads a type definition and returns a pointer to it }
  207. procedure read_type(var tt : ttype;const name : stringid);
  208. var
  209. pt : tnode;
  210. tt2 : ttype;
  211. aktenumdef : penumdef;
  212. ap : parraydef;
  213. s : stringid;
  214. l,v : longint;
  215. oldaktpackrecords : tpackrecords;
  216. hs : string;
  217. defpos,storepos : tfileposinfo;
  218. procedure expr_type;
  219. var
  220. pt1,pt2 : tnode;
  221. begin
  222. { use of current parsed object ? }
  223. if (token=_ID) and (testcurobject=2) and (curobjectname=pattern) then
  224. begin
  225. consume(_ID);
  226. tt.setdef(aktobjectdef);
  227. exit;
  228. end;
  229. { classes can be used also in classes }
  230. if (curobjectname=pattern) and aktobjectdef^.is_class then
  231. begin
  232. tt.setdef(aktobjectdef);
  233. consume(_ID);
  234. exit;
  235. end;
  236. { we can't accept a equal in type }
  237. pt1:=comp_expr(not(ignore_equal));
  238. do_firstpass(pt1);
  239. if (token=_POINTPOINT) then
  240. begin
  241. consume(_POINTPOINT);
  242. { get high value of range }
  243. pt2:=comp_expr(not(ignore_equal));
  244. do_firstpass(pt2);
  245. { both must be evaluated to constants now }
  246. if (pt1.nodetype=ordconstn) and
  247. (pt2.nodetype=ordconstn) then
  248. begin
  249. { check types }
  250. if CheckTypes(pt1.resulttype,pt2.resulttype) then
  251. begin
  252. { Check bounds }
  253. if tordconstnode(pt2).value<tordconstnode(pt1).value then
  254. Message(cg_e_upper_lower_than_lower)
  255. else
  256. begin
  257. { All checks passed, create the new def }
  258. case pt1.resulttype^.deftype of
  259. enumdef :
  260. tt.setdef(new(penumdef,init_subrange(penumdef(pt1.resulttype),tordconstnode(pt1).value,tordconstnode(pt2).value)));
  261. orddef :
  262. begin
  263. if is_char(pt1.resulttype) then
  264. tt.setdef(new(porddef,init(uchar,tordconstnode(pt1).value,tordconstnode(pt2).value)))
  265. else
  266. if is_boolean(pt1.resulttype) then
  267. tt.setdef(new(porddef,init(bool8bit,tordconstnode(pt1).value,tordconstnode(pt2).value)))
  268. else
  269. tt.setdef(new(porddef,init(uauto,tordconstnode(pt1).value,tordconstnode(pt2).value)));
  270. end;
  271. end;
  272. end;
  273. end;
  274. end
  275. else
  276. Message(sym_e_error_in_type_def);
  277. pt2.free;
  278. end
  279. else
  280. begin
  281. { a simple type renaming }
  282. if (pt1.nodetype=typen) then
  283. begin
  284. if assigned(ttypenode(pt1).typenodesym) then
  285. tt.setsym(ttypenode(pt1).typenodesym)
  286. else
  287. tt.setdef(pt1.resulttype);
  288. end
  289. else
  290. Message(sym_e_error_in_type_def);
  291. end;
  292. pt1.free;
  293. end;
  294. procedure array_dec;
  295. var
  296. lowval,
  297. highval : longint;
  298. arraytype : pdef;
  299. ht : ttype;
  300. procedure setdefdecl(p:pdef);
  301. begin
  302. case p^.deftype of
  303. enumdef :
  304. begin
  305. lowval:=penumdef(p)^.min;
  306. highval:=penumdef(p)^.max;
  307. arraytype:=p;
  308. end;
  309. orddef :
  310. begin
  311. lowval:=porddef(p)^.low;
  312. highval:=porddef(p)^.high;
  313. arraytype:=p;
  314. end;
  315. else
  316. Message(sym_e_error_in_type_def);
  317. end;
  318. end;
  319. begin
  320. consume(_ARRAY);
  321. consume(_LECKKLAMMER);
  322. { defaults }
  323. arraytype:=generrordef;
  324. lowval:=$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. consume(_OF);
  384. read_type(tt2,'');
  385. { if no error, set element type }
  386. if assigned(ap) then
  387. ap^.elementtype:=tt2;
  388. end;
  389. begin
  390. tt.reset;
  391. case token of
  392. _STRING,_FILE:
  393. begin
  394. single_type(tt,hs,false);
  395. end;
  396. _LKLAMMER:
  397. begin
  398. consume(_LKLAMMER);
  399. { allow negativ value_str }
  400. l:=-1;
  401. aktenumdef:=new(penumdef,init);
  402. repeat
  403. s:=orgpattern;
  404. defpos:=tokenpos;
  405. consume(_ID);
  406. { only allow assigning of specific numbers under fpc mode }
  407. if (m_fpc in aktmodeswitches) and
  408. (token=_ASSIGNMENT) then
  409. begin
  410. consume(_ASSIGNMENT);
  411. v:=get_intconst;
  412. { please leave that a note, allows type save }
  413. { declarations in the win32 units ! }
  414. if v<=l then
  415. Message(parser_n_duplicate_enum);
  416. l:=v;
  417. end
  418. else
  419. inc(l);
  420. storepos:=tokenpos;
  421. tokenpos:=defpos;
  422. constsymtable^.insert(new(penumsym,init(s,aktenumdef,l)));
  423. tokenpos:=storepos;
  424. until not try_to_consume(_COMMA);
  425. tt.setdef(aktenumdef);
  426. consume(_RKLAMMER);
  427. end;
  428. _ARRAY:
  429. begin
  430. array_dec;
  431. end;
  432. _SET:
  433. begin
  434. consume(_SET);
  435. consume(_OF);
  436. read_type(tt2,'');
  437. if assigned(tt2.def) then
  438. begin
  439. case tt2.def^.deftype of
  440. { don't forget that min can be negativ PM }
  441. enumdef :
  442. if penumdef(tt2.def)^.min>=0 then
  443. tt.setdef(new(psetdef,init(tt2.def,penumdef(tt2.def)^.max)))
  444. else
  445. Message(sym_e_ill_type_decl_set);
  446. orddef :
  447. begin
  448. case porddef(tt2.def)^.typ of
  449. uchar :
  450. tt.setdef(new(psetdef,init(tt2.def,255)));
  451. u8bit,u16bit,u32bit,
  452. s8bit,s16bit,s32bit :
  453. begin
  454. if (porddef(tt2.def)^.low>=0) then
  455. tt.setdef(new(psetdef,init(tt2.def,porddef(tt2.def)^.high)))
  456. else
  457. Message(sym_e_ill_type_decl_set);
  458. end;
  459. else
  460. Message(sym_e_ill_type_decl_set);
  461. end;
  462. end;
  463. else
  464. Message(sym_e_ill_type_decl_set);
  465. end;
  466. end
  467. else
  468. tt.setdef(generrordef);
  469. end;
  470. _CARET:
  471. begin
  472. consume(_CARET);
  473. single_type(tt2,hs,typecanbeforward);
  474. tt.setdef(new(ppointerdef,init(tt2)));
  475. end;
  476. _RECORD:
  477. begin
  478. tt.setdef(record_dec);
  479. end;
  480. _PACKED:
  481. begin
  482. consume(_PACKED);
  483. if token=_ARRAY then
  484. array_dec
  485. else
  486. begin
  487. oldaktpackrecords:=aktpackrecords;
  488. aktpackrecords:=packrecord_1;
  489. if token in [_CLASS,_OBJECT] then
  490. tt.setdef(object_dec(name,nil))
  491. else
  492. tt.setdef(record_dec);
  493. aktpackrecords:=oldaktpackrecords;
  494. end;
  495. end;
  496. _CLASS,
  497. _CPPCLASS,
  498. {$ifdef SUPPORTINTERFACES}
  499. _INTERFACE,
  500. {$endif SUPPORTINTERFACES}
  501. _OBJECT:
  502. begin
  503. tt.setdef(object_dec(name,nil));
  504. end;
  505. _PROCEDURE:
  506. begin
  507. consume(_PROCEDURE);
  508. tt.setdef(new(pprocvardef,init));
  509. if token=_LKLAMMER then
  510. parameter_dec(pprocvardef(tt.def));
  511. if token=_OF then
  512. begin
  513. consume(_OF);
  514. consume(_OBJECT);
  515. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  516. end;
  517. end;
  518. _FUNCTION:
  519. begin
  520. consume(_FUNCTION);
  521. tt.def:=new(pprocvardef,init);
  522. if token=_LKLAMMER then
  523. parameter_dec(pprocvardef(tt.def));
  524. consume(_COLON);
  525. single_type(pprocvardef(tt.def)^.rettype,hs,false);
  526. if token=_OF then
  527. begin
  528. consume(_OF);
  529. consume(_OBJECT);
  530. include(pprocvardef(tt.def)^.procoptions,po_methodpointer);
  531. end;
  532. end;
  533. else
  534. expr_type;
  535. end;
  536. if tt.def=nil then
  537. tt.setdef(generrordef);
  538. end;
  539. end.
  540. {
  541. $Log$
  542. Revision 1.10 2000-10-14 10:14:52 peter
  543. * moehrendorf oct 2000 rewrite
  544. Revision 1.9 2000/09/24 15:06:25 peter
  545. * use defines.inc
  546. Revision 1.8 2000/08/27 20:19:39 peter
  547. * store strings with case in ppu, when an internal symbol is created
  548. a '$' is prefixed so it's not automatic uppercased
  549. Revision 1.7 2000/08/27 16:11:52 peter
  550. * moved some util functions from globals,cobjects to cutils
  551. * splitted files into finput,fmodule
  552. Revision 1.6 2000/08/16 18:33:54 peter
  553. * splitted namedobjectitem.next into indexnext and listnext so it
  554. can be used in both lists
  555. * don't allow "word = word" type definitions (merged)
  556. Revision 1.5 2000/08/06 14:17:15 peter
  557. * overload fixes (merged)
  558. Revision 1.4 2000/07/30 17:04:43 peter
  559. * merged fixes
  560. Revision 1.3 2000/07/13 12:08:27 michael
  561. + patched to 1.1.0 with former 1.09patch from peter
  562. Revision 1.2 2000/07/13 11:32:47 michael
  563. + removed logs
  564. }