htypechk.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit exports some help routines for the type checking
  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 htypechk;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. tokens,
  23. node,
  24. symtype,symdef;
  25. type
  26. Ttok2nodeRec=record
  27. tok : ttoken;
  28. nod : tnodetype;
  29. op_overloading_supported : boolean;
  30. end;
  31. const
  32. tok2nodes=25;
  33. tok2node:array[1..tok2nodes] of ttok2noderec=(
  34. (tok:_PLUS ;nod:addn;op_overloading_supported:true), { binary overloading supported }
  35. (tok:_MINUS ;nod:subn;op_overloading_supported:true), { binary and unary overloading supported }
  36. (tok:_STAR ;nod:muln;op_overloading_supported:true), { binary overloading supported }
  37. (tok:_SLASH ;nod:slashn;op_overloading_supported:true), { binary overloading supported }
  38. (tok:_EQUAL ;nod:equaln;op_overloading_supported:true), { binary overloading supported }
  39. (tok:_GT ;nod:gtn;op_overloading_supported:true), { binary overloading supported }
  40. (tok:_LT ;nod:ltn;op_overloading_supported:true), { binary overloading supported }
  41. (tok:_GTE ;nod:gten;op_overloading_supported:true), { binary overloading supported }
  42. (tok:_LTE ;nod:lten;op_overloading_supported:true), { binary overloading supported }
  43. (tok:_SYMDIF ;nod:symdifn;op_overloading_supported:true), { binary overloading supported }
  44. (tok:_STARSTAR;nod:starstarn;op_overloading_supported:true), { binary overloading supported }
  45. (tok:_OP_AS ;nod:asn;op_overloading_supported:false), { binary overloading NOT supported }
  46. (tok:_OP_IN ;nod:inn;op_overloading_supported:false), { binary overloading NOT supported }
  47. (tok:_OP_IS ;nod:isn;op_overloading_supported:false), { binary overloading NOT supported }
  48. (tok:_OP_OR ;nod:orn;op_overloading_supported:true), { binary overloading supported }
  49. (tok:_OP_AND ;nod:andn;op_overloading_supported:true), { binary overloading supported }
  50. (tok:_OP_DIV ;nod:divn;op_overloading_supported:true), { binary overloading supported }
  51. (tok:_OP_NOT ;nod:notn;op_overloading_supported:true), { unary overloading supported }
  52. (tok:_OP_MOD ;nod:modn;op_overloading_supported:true), { binary overloading supported }
  53. (tok:_OP_SHL ;nod:shln;op_overloading_supported:true), { binary overloading supported }
  54. (tok:_OP_SHR ;nod:shrn;op_overloading_supported:true), { binary overloading supported }
  55. (tok:_OP_XOR ;nod:xorn;op_overloading_supported:true), { binary overloading supported }
  56. (tok:_ASSIGNMENT;nod:assignn;op_overloading_supported:true), { unary overloading supported }
  57. (tok:_CARET ;nod:caretn;op_overloading_supported:false), { binary overloading NOT supported }
  58. (tok:_UNEQUAL ;nod:unequaln;op_overloading_supported:false) { binary overloading NOT supported overload = instead }
  59. );
  60. const
  61. { firstcallparan without varspez we don't count the ref }
  62. {$ifdef extdebug}
  63. count_ref : boolean = true;
  64. {$endif def extdebug}
  65. get_para_resulttype : boolean = false;
  66. allow_array_constructor : boolean = false;
  67. { is overloading of this operator allowed for this
  68. binary operator }
  69. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  70. { is overloading of this operator allowed for this
  71. unary operator }
  72. function isunaryoperatoroverloadable(rd,dd : tdef; treetyp : tnodetype) : boolean;
  73. { check operator args and result type }
  74. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  75. function isbinaryoverloaded(var t : tnode) : boolean;
  76. { Register Allocation }
  77. procedure make_not_regable(p : tnode);
  78. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  79. { subroutine handling }
  80. function is_procsym_load(p:tnode):boolean;
  81. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  82. {
  83. type
  84. tvarstaterequire = (vsr_can_be_undefined,vsr_must_be_valid,
  85. vsr_is_used_after,vsr_must_be_valid_and_is_used_after); }
  86. { sets varsym varstate field correctly }
  87. procedure unset_varstate(p : tnode);
  88. procedure set_varstate(p : tnode;must_be_valid : boolean);
  89. { sets the callunique flag, if the node is a vecn, }
  90. { takes care of type casts etc. }
  91. procedure set_unique(p : tnode);
  92. { sets funcret_is_valid to true, if p contains a funcref node }
  93. procedure set_funcret_is_valid(p : tnode);
  94. function valid_for_formal_var(p : tnode) : boolean;
  95. function valid_for_formal_const(p : tnode) : boolean;
  96. function valid_for_var(p:tnode):boolean;
  97. function valid_for_assignment(p:tnode):boolean;
  98. implementation
  99. uses
  100. globtype,systems,
  101. cutils,verbose,globals,
  102. symconst,symsym,symtable,
  103. defutil,defcmp,cpubase,
  104. ncnv,nld,
  105. nmem,ncal,nmat,
  106. cgbase
  107. ;
  108. type
  109. TValidAssign=(Valid_Property,Valid_Void);
  110. TValidAssigns=set of TValidAssign;
  111. function isbinaryoperatoroverloadable(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype) : boolean;
  112. function internal_check(treetyp:tnodetype;ld:tdef;lt:tnodetype;rd:tdef;rt:tnodetype;var allowed:boolean):boolean;
  113. begin
  114. internal_check:=true;
  115. case ld.deftype of
  116. recorddef,
  117. variantdef :
  118. begin
  119. allowed:=true;
  120. end;
  121. procvardef :
  122. begin
  123. if (rd.deftype in [pointerdef,procdef,procvardef]) and
  124. (treetyp in [equaln,unequaln]) then
  125. begin
  126. allowed:=false;
  127. exit;
  128. end;
  129. allowed:=true;
  130. end;
  131. pointerdef :
  132. begin
  133. if ((rd.deftype in [pointerdef,classrefdef,procvardef]) or
  134. is_class_or_interface(rd)) and
  135. (treetyp in [equaln,unequaln,gtn,gten,ltn,lten,addn,subn]) then
  136. begin
  137. allowed:=false;
  138. exit;
  139. end;
  140. { don't allow operations on pointer/integer }
  141. if is_integer(rd) then
  142. begin
  143. allowed:=false;
  144. exit;
  145. end;
  146. { don't allow pchar+string }
  147. if is_pchar(ld) and
  148. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  149. (is_chararray(rd) or
  150. is_char(rd) or
  151. (rd.deftype=stringdef)) then
  152. begin
  153. allowed:=false;
  154. exit;
  155. end;
  156. allowed:=true;
  157. end;
  158. arraydef :
  159. begin
  160. { not mmx }
  161. if (cs_mmx in aktlocalswitches) and
  162. is_mmx_able_array(ld) then
  163. begin
  164. allowed:=false;
  165. exit;
  166. end;
  167. { not chararray+[char,string,chararray] }
  168. if is_chararray(ld) and
  169. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) and
  170. (is_char(rd) or
  171. is_pchar(rd) or
  172. is_integer(rd) or
  173. (rd.deftype=stringdef) or
  174. is_chararray(rd) or
  175. (rt=niln)) then
  176. begin
  177. allowed:=false;
  178. exit;
  179. end;
  180. { dynamic array compare with niln }
  181. if is_dynamic_array(ld) and
  182. (rt=niln) and
  183. (treetyp in [equaln,unequaln]) then
  184. begin
  185. allowed:=false;
  186. exit;
  187. end;
  188. allowed:=true;
  189. end;
  190. objectdef :
  191. begin
  192. { <> and = are defined for classes }
  193. if (treetyp in [equaln,unequaln]) and
  194. is_class_or_interface(ld) then
  195. begin
  196. allowed:=false;
  197. exit;
  198. end;
  199. allowed:=true;
  200. end;
  201. stringdef :
  202. begin
  203. if ((rd.deftype=stringdef) or
  204. is_char(rd) or
  205. is_pchar(rd) or
  206. is_chararray(rd)) and
  207. (treetyp in [addn,equaln,unequaln,gtn,gten,ltn,lten]) then
  208. begin
  209. allowed:=false;
  210. exit;
  211. end;
  212. allowed:=true;
  213. end;
  214. else
  215. internal_check:=false;
  216. end;
  217. end;
  218. var
  219. allowed : boolean;
  220. begin
  221. { power ** is always possible }
  222. if (treetyp=starstarn) then
  223. begin
  224. isbinaryoperatoroverloadable:=true;
  225. exit;
  226. end;
  227. { order of arguments does not matter so we have to check also
  228. the reversed order }
  229. allowed:=false;
  230. if not internal_check(treetyp,ld,lt,rd,rt,allowed) then
  231. internal_check(treetyp,rd,rt,ld,lt,allowed);
  232. isbinaryoperatoroverloadable:=allowed;
  233. end;
  234. function isunaryoperatoroverloadable(rd,dd : tdef; treetyp : tnodetype) : boolean;
  235. begin
  236. isunaryoperatoroverloadable:=false;
  237. case treetyp of
  238. assignn :
  239. begin
  240. if (rd.deftype=orddef) and
  241. (dd.deftype=orddef) then
  242. begin
  243. isunaryoperatoroverloadable:=false;
  244. exit;
  245. end;
  246. isunaryoperatoroverloadable:=true;
  247. end;
  248. subn :
  249. begin
  250. if is_integer(rd) or
  251. (rd.deftype=floatdef) then
  252. begin
  253. isunaryoperatoroverloadable:=false;
  254. exit;
  255. end;
  256. {$ifdef SUPPORT_MMX}
  257. if (cs_mmx in aktlocalswitches) and
  258. is_mmx_able_array(rd) then
  259. begin
  260. isunaryoperatoroverloadable:=false;
  261. exit;
  262. end;
  263. {$endif SUPPORT_MMX}
  264. isunaryoperatoroverloadable:=true;
  265. end;
  266. notn :
  267. begin
  268. if is_integer(rd) or
  269. is_boolean(rd) then
  270. begin
  271. isunaryoperatoroverloadable:=false;
  272. exit;
  273. end;
  274. {$ifdef SUPPORT_MMX}
  275. if (cs_mmx in aktlocalswitches) and
  276. is_mmx_able_array(rd) then
  277. begin
  278. isunaryoperatoroverloadable:=false;
  279. exit;
  280. end;
  281. {$endif SUPPORT_MMX}
  282. isunaryoperatoroverloadable:=true;
  283. end;
  284. end;
  285. end;
  286. function isoperatoracceptable(pf : tprocdef; optoken : ttoken) : boolean;
  287. var
  288. ld,rd,dd : tdef;
  289. i : longint;
  290. begin
  291. case pf.parast.symindex.count of
  292. 2 : begin
  293. isoperatoracceptable:=false;
  294. for i:=1 to tok2nodes do
  295. if tok2node[i].tok=optoken then
  296. begin
  297. ld:=tvarsym(pf.parast.symindex.first).vartype.def;
  298. rd:=tvarsym(pf.parast.symindex.first.indexnext).vartype.def;
  299. dd:=pf.rettype.def;
  300. isoperatoracceptable:=
  301. tok2node[i].op_overloading_supported and
  302. isbinaryoperatoroverloadable(tok2node[i].nod,ld,nothingn,rd,nothingn);
  303. break;
  304. end;
  305. end;
  306. 1 : begin
  307. rd:=tvarsym(pf.parast.symindex.first).vartype.def;
  308. dd:=pf.rettype.def;
  309. for i:=1 to tok2nodes do
  310. if tok2node[i].tok=optoken then
  311. begin
  312. isoperatoracceptable:=
  313. tok2node[i].op_overloading_supported and
  314. isunaryoperatoroverloadable(rd,dd,tok2node[i].nod);
  315. break;
  316. end;
  317. end;
  318. else
  319. isoperatoracceptable:=false;
  320. end;
  321. end;
  322. function isbinaryoverloaded(var t : tnode) : boolean;
  323. var
  324. rd,ld : tdef;
  325. optoken : ttoken;
  326. operpd : tprocdef;
  327. ht : tnode;
  328. begin
  329. isbinaryoverloaded:=false;
  330. operpd:=nil;
  331. { load easier access variables }
  332. ld:=tbinarynode(t).left.resulttype.def;
  333. rd:=tbinarynode(t).right.resulttype.def;
  334. if isbinaryoperatoroverloadable(t.nodetype,ld,tbinarynode(t).left.nodetype,rd,tbinarynode(t).right.nodetype) then
  335. begin
  336. isbinaryoverloaded:=true;
  337. case t.nodetype of
  338. equaln,
  339. unequaln :
  340. optoken:=_EQUAL;
  341. addn:
  342. optoken:=_PLUS;
  343. subn:
  344. optoken:=_MINUS;
  345. muln:
  346. optoken:=_STAR;
  347. starstarn:
  348. optoken:=_STARSTAR;
  349. slashn:
  350. optoken:=_SLASH;
  351. ltn:
  352. optoken:=tokens._lt;
  353. gtn:
  354. optoken:=tokens._gt;
  355. lten:
  356. optoken:=_lte;
  357. gten:
  358. optoken:=_gte;
  359. symdifn :
  360. optoken:=_SYMDIF;
  361. modn :
  362. optoken:=_OP_MOD;
  363. orn :
  364. optoken:=_OP_OR;
  365. xorn :
  366. optoken:=_OP_XOR;
  367. andn :
  368. optoken:=_OP_AND;
  369. divn :
  370. optoken:=_OP_DIV;
  371. shln :
  372. optoken:=_OP_SHL;
  373. shrn :
  374. optoken:=_OP_SHR;
  375. else
  376. exit;
  377. end;
  378. { check if the operator contains overloaded procdefs }
  379. if overloaded_operators[optoken]=nil then
  380. begin
  381. CGMessage(parser_e_operator_not_overloaded);
  382. isbinaryoverloaded:=false;
  383. exit;
  384. end;
  385. { Check if the assignment is available, if not then
  386. give a message that the types are not compatible }
  387. if optoken in [_EQUAL] then
  388. begin
  389. operpd:=overloaded_operators[optoken].search_procdef_binary_operator(ld,rd);
  390. if not assigned(operpd) then
  391. begin
  392. CGMessage2(type_e_incompatible_types,ld.typename,rd.typename);
  393. isbinaryoverloaded:=false;
  394. exit;
  395. end;
  396. end;
  397. { the nil as symtable signs firstcalln that this is
  398. an overloaded operator }
  399. ht:=ccallnode.create(nil,overloaded_operators[optoken],nil,nil);
  400. inc(tcallnode(ht).symtableprocentry.refs);
  401. { we already know the procdef to use for equal, so it can
  402. skip the overload choosing in callnode.det_resulttype }
  403. if assigned(operpd) then
  404. tcallnode(ht).procdefinition:=operpd;
  405. { we need copies, because the originals will be destroyed when we give a }
  406. { changed node back to firstpass! (JM) }
  407. if assigned(tbinarynode(t).left) then
  408. if assigned(tbinarynode(t).right) then
  409. tcallnode(ht).left :=
  410. ccallparanode.create(tbinarynode(t).right.getcopy,
  411. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  412. else
  413. tcallnode(ht).left :=
  414. ccallparanode.create(nil,
  415. ccallparanode.create(tbinarynode(t).left.getcopy,nil))
  416. else if assigned(tbinarynode(t).right) then
  417. tcallnode(ht).left :=
  418. ccallparanode.create(tbinarynode(t).right.getcopy,
  419. ccallparanode.create(nil,nil));
  420. if t.nodetype=unequaln then
  421. ht:=cnotnode.create(ht);
  422. t:=ht;
  423. end;
  424. end;
  425. {****************************************************************************
  426. Register Calculation
  427. ****************************************************************************}
  428. { marks an lvalue as "unregable" }
  429. procedure make_not_regable(p : tnode);
  430. begin
  431. case p.nodetype of
  432. typeconvn :
  433. make_not_regable(ttypeconvnode(p).left);
  434. loadn :
  435. if tloadnode(p).symtableentry.typ=varsym then
  436. tvarsym(tloadnode(p).symtableentry).varoptions:=tvarsym(tloadnode(p).symtableentry).varoptions-[vo_regable,vo_fpuregable];
  437. end;
  438. end;
  439. { calculates the needed registers for a binary operator }
  440. procedure calcregisters(p : tbinarynode;r32,fpu,mmx : word);
  441. begin
  442. p.left_right_max;
  443. { Only when the difference between the left and right registers < the
  444. wanted registers allocate the amount of registers }
  445. if assigned(p.left) then
  446. begin
  447. if assigned(p.right) then
  448. begin
  449. { the location must be already filled in because we need it to }
  450. { calculate the necessary number of registers (JM) }
  451. if p.location.loc = LOC_INVALID then
  452. internalerror(200110101);
  453. if (abs(p.left.registers32-p.right.registers32)<r32) or
  454. ((p.location.loc = LOC_FPUREGISTER) and
  455. (p.right.registersfpu <= p.left.registersfpu) and
  456. ((p.right.registersfpu <> 0) or (p.left.registersfpu <> 0)) and
  457. (p.left.registers32 < p.right.registers32)) then
  458. inc(p.registers32,r32);
  459. if (abs(p.left.registersfpu-p.right.registersfpu)<fpu) then
  460. inc(p.registersfpu,fpu);
  461. {$ifdef SUPPORT_MMX}
  462. if (abs(p.left.registersmmx-p.right.registersmmx)<mmx) then
  463. inc(p.registersmmx,mmx);
  464. {$endif SUPPORT_MMX}
  465. { the following is a little bit guessing but I think }
  466. { it's the only way to solve same internalerrors: }
  467. { if the left and right node both uses registers }
  468. { and return a mem location, but the current node }
  469. { doesn't use an integer register we get probably }
  470. { trouble when restoring a node }
  471. if (p.left.registers32=p.right.registers32) and
  472. (p.registers32=p.left.registers32) and
  473. (p.registers32>0) and
  474. (p.left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  475. (p.right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  476. inc(p.registers32);
  477. end
  478. else
  479. begin
  480. if (p.left.registers32<r32) then
  481. inc(p.registers32,r32);
  482. if (p.left.registersfpu<fpu) then
  483. inc(p.registersfpu,fpu);
  484. {$ifdef SUPPORT_MMX}
  485. if (p.left.registersmmx<mmx) then
  486. inc(p.registersmmx,mmx);
  487. {$endif SUPPORT_MMX}
  488. end;
  489. end;
  490. { error CGMessage, if more than 8 floating point }
  491. { registers are needed }
  492. { if p.registersfpu>maxfpuregs then
  493. CGMessage(cg_e_too_complex_expr); now pushed if needed PM }
  494. end;
  495. {****************************************************************************
  496. Subroutine Handling
  497. ****************************************************************************}
  498. function is_procsym_load(p:tnode):boolean;
  499. begin
  500. { ignore vecn,subscriptn }
  501. repeat
  502. case p.nodetype of
  503. vecn :
  504. p:=tvecnode(p).left;
  505. subscriptn :
  506. p:=tsubscriptnode(p).left;
  507. else
  508. break;
  509. end;
  510. until false;
  511. is_procsym_load:=((p.nodetype=loadn) and (tloadnode(p).symtableentry.typ=procsym)) or
  512. ((p.nodetype=addrn) and (taddrnode(p).left.nodetype=loadn)
  513. and (tloadnode(taddrnode(p).left).symtableentry.typ=procsym)) ;
  514. end;
  515. { local routines can't be assigned to procvars }
  516. procedure test_local_to_procvar(from_def:tprocvardef;to_def:tdef);
  517. begin
  518. if (from_def.symtablelevel>1) and (to_def.deftype=procvardef) then
  519. CGMessage(type_e_cannot_local_proc_to_procvar);
  520. end;
  521. procedure set_varstate(p : tnode;must_be_valid : boolean);
  522. var
  523. hsym : tvarsym;
  524. begin
  525. while assigned(p) do
  526. begin
  527. if (nf_varstateset in p.flags) then
  528. exit;
  529. include(p.flags,nf_varstateset);
  530. case p.nodetype of
  531. typeconvn :
  532. begin
  533. case ttypeconvnode(p).convtype of
  534. tc_cchar_2_pchar,
  535. tc_cstring_2_pchar,
  536. tc_array_2_pointer :
  537. must_be_valid:=false;
  538. tc_pchar_2_string,
  539. tc_pointer_2_array :
  540. must_be_valid:=true;
  541. end;
  542. p:=tunarynode(p).left;
  543. end;
  544. subscriptn :
  545. p:=tunarynode(p).left;
  546. vecn:
  547. begin
  548. set_varstate(tbinarynode(p).right,true);
  549. if not(tunarynode(p).left.resulttype.def.deftype in [stringdef,arraydef]) then
  550. must_be_valid:=true;
  551. p:=tunarynode(p).left;
  552. end;
  553. { do not parse calln }
  554. calln :
  555. break;
  556. callparan :
  557. begin
  558. set_varstate(tbinarynode(p).right,must_be_valid);
  559. p:=tunarynode(p).left;
  560. end;
  561. loadn :
  562. begin
  563. if (tloadnode(p).symtableentry.typ=varsym) then
  564. begin
  565. hsym:=tvarsym(tloadnode(p).symtableentry);
  566. if must_be_valid and (nf_first in p.flags) then
  567. begin
  568. if (hsym.varstate=vs_declared_and_first_found) or
  569. (hsym.varstate=vs_set_but_first_not_passed) then
  570. begin
  571. if (assigned(hsym.owner) and
  572. assigned(aktprocsym) and
  573. (hsym.owner = aktprocdef.localst)) then
  574. begin
  575. if tloadnode(p).symtable.symtabletype=localsymtable then
  576. CGMessage1(sym_n_uninitialized_local_variable,hsym.realname)
  577. else
  578. CGMessage1(sym_n_uninitialized_variable,hsym.realname);
  579. end;
  580. end;
  581. end;
  582. if (nf_first in p.flags) then
  583. begin
  584. if hsym.varstate=vs_declared_and_first_found then
  585. begin
  586. { this can only happen at left of an assignment, no ? PM }
  587. if (parsing_para_level=0) and not must_be_valid then
  588. hsym.varstate:=vs_assigned
  589. else
  590. hsym.varstate:=vs_used;
  591. end
  592. else
  593. if hsym.varstate=vs_set_but_first_not_passed then
  594. hsym.varstate:=vs_used;
  595. exclude(p.flags,nf_first);
  596. end
  597. else
  598. begin
  599. if (hsym.varstate=vs_assigned) and
  600. (must_be_valid or (parsing_para_level>0) or
  601. (p.resulttype.def.deftype=procvardef)) then
  602. hsym.varstate:=vs_used;
  603. if (hsym.varstate=vs_declared_and_first_found) and
  604. (must_be_valid or (parsing_para_level>0) or
  605. (p.resulttype.def.deftype=procvardef)) then
  606. hsym.varstate:=vs_set_but_first_not_passed;
  607. end;
  608. end;
  609. break;
  610. end;
  611. funcretn:
  612. begin
  613. { no claim if setting higher return value_str }
  614. if must_be_valid and
  615. (lexlevel=tfuncretnode(p).funcretsym.owner.symtablelevel) and
  616. ((tfuncretnode(p).funcretsym.funcretstate=vs_declared) or
  617. ((nf_is_first_funcret in p.flags) and
  618. (tfuncretnode(p).funcretsym.funcretstate=vs_declared_and_first_found))) then
  619. begin
  620. CGMessage(sym_w_function_result_not_set);
  621. { avoid multiple warnings }
  622. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  623. end;
  624. if (nf_is_first_funcret in p.flags) and not must_be_valid then
  625. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  626. break;
  627. end;
  628. else
  629. break;
  630. end;{case }
  631. end;
  632. end;
  633. procedure unset_varstate(p : tnode);
  634. begin
  635. while assigned(p) do
  636. begin
  637. exclude(p.flags,nf_varstateset);
  638. case p.nodetype of
  639. typeconvn,
  640. subscriptn,
  641. vecn :
  642. p:=tunarynode(p).left;
  643. else
  644. break;
  645. end;
  646. end;
  647. end;
  648. procedure set_unique(p : tnode);
  649. begin
  650. while assigned(p) do
  651. begin
  652. case p.nodetype of
  653. vecn:
  654. begin
  655. include(p.flags,nf_callunique);
  656. break;
  657. end;
  658. typeconvn,
  659. subscriptn,
  660. derefn:
  661. p:=tunarynode(p).left;
  662. else
  663. break;
  664. end;
  665. end;
  666. end;
  667. procedure set_funcret_is_valid(p:tnode);
  668. begin
  669. while assigned(p) do
  670. begin
  671. case p.nodetype of
  672. funcretn:
  673. begin
  674. if (nf_is_first_funcret in p.flags) or
  675. (tfuncretnode(p).funcretsym.funcretstate=vs_declared_and_first_found) then
  676. tfuncretnode(p).funcretsym.funcretstate:=vs_assigned;
  677. break;
  678. end;
  679. vecn,
  680. {derefn,}
  681. typeconvn,
  682. subscriptn:
  683. p:=tunarynode(p).left;
  684. else
  685. break;
  686. end;
  687. end;
  688. end;
  689. function valid_for_assign(p:tnode;opts:TValidAssigns):boolean;
  690. var
  691. hp : tnode;
  692. gotwith,
  693. gotsubscript,
  694. gotpointer,
  695. gotvec,
  696. gotclass,
  697. gotderef : boolean;
  698. fromdef,
  699. todef : tdef;
  700. begin
  701. valid_for_assign:=false;
  702. gotsubscript:=false;
  703. gotvec:=false;
  704. gotderef:=false;
  705. gotclass:=false;
  706. gotpointer:=false;
  707. gotwith:=false;
  708. hp:=p;
  709. if not(valid_void in opts) and
  710. is_void(hp.resulttype.def) then
  711. begin
  712. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  713. exit;
  714. end;
  715. while assigned(hp) do
  716. begin
  717. { property allowed? calln has a property check itself }
  718. if (nf_isproperty in hp.flags) then
  719. begin
  720. if (valid_property in opts) then
  721. valid_for_assign:=true
  722. else
  723. begin
  724. { check return type }
  725. case hp.resulttype.def.deftype of
  726. pointerdef :
  727. gotpointer:=true;
  728. objectdef :
  729. gotclass:=is_class_or_interface(hp.resulttype.def);
  730. recorddef, { handle record like class it needs a subscription }
  731. classrefdef :
  732. gotclass:=true;
  733. end;
  734. { 1. if it returns a pointer and we've found a deref,
  735. 2. if it returns a class or record and a subscription or with is found }
  736. if (gotpointer and gotderef) or
  737. (gotclass and (gotsubscript or gotwith)) then
  738. valid_for_assign:=true
  739. else
  740. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  741. end;
  742. exit;
  743. end;
  744. case hp.nodetype of
  745. temprefn :
  746. begin
  747. valid_for_assign := true;
  748. exit;
  749. end;
  750. derefn :
  751. begin
  752. gotderef:=true;
  753. hp:=tderefnode(hp).left;
  754. end;
  755. typeconvn :
  756. begin
  757. { typecast sizes must match, exceptions:
  758. - from formaldef
  759. - from void
  760. - typecast from pointer to array }
  761. fromdef:=ttypeconvnode(hp).left.resulttype.def;
  762. todef:=hp.resulttype.def;
  763. if not((fromdef.deftype=formaldef) or
  764. is_void(fromdef) or
  765. ((fromdef.deftype=pointerdef) and (todef.deftype=arraydef)) or
  766. ((fromdef.deftype = objectdef) and (todef.deftype = objectdef) and
  767. (tobjectdef(fromdef).is_related(tobjectdef(todef))))) and
  768. (fromdef.size<>todef.size) then
  769. begin
  770. { in TP it is allowed to typecast to smaller types }
  771. if not(m_tp7 in aktmodeswitches) or
  772. (todef.size>fromdef.size) then
  773. CGMessagePos2(hp.fileinfo,type_e_typecast_wrong_size_for_assignment,tostr(fromdef.size),tostr(todef.size));
  774. end;
  775. case hp.resulttype.def.deftype of
  776. pointerdef :
  777. gotpointer:=true;
  778. objectdef :
  779. gotclass:=is_class_or_interface(hp.resulttype.def);
  780. classrefdef :
  781. gotclass:=true;
  782. arraydef :
  783. begin
  784. { pointer -> array conversion is done then we need to see it
  785. as a deref, because a ^ is then not required anymore }
  786. if (ttypeconvnode(hp).left.resulttype.def.deftype=pointerdef) then
  787. gotderef:=true;
  788. end;
  789. end;
  790. hp:=ttypeconvnode(hp).left;
  791. end;
  792. vecn :
  793. begin
  794. gotvec:=true;
  795. hp:=tunarynode(hp).left;
  796. end;
  797. asn :
  798. hp:=tunarynode(hp).left;
  799. subscriptn :
  800. begin
  801. gotsubscript:=true;
  802. { a class/interface access is an implicit }
  803. { dereferencing }
  804. hp:=tsubscriptnode(hp).left;
  805. if is_class_or_interface(hp.resulttype.def) then
  806. gotderef:=true;
  807. end;
  808. subn,
  809. addn :
  810. begin
  811. { Allow add/sub operators on a pointer, or an integer
  812. and a pointer typecast and deref has been found }
  813. if ((hp.resulttype.def.deftype=pointerdef) or
  814. (is_integer(hp.resulttype.def) and gotpointer)) and
  815. gotderef then
  816. valid_for_assign:=true
  817. else
  818. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  819. exit;
  820. end;
  821. addrn :
  822. begin
  823. if gotderef or
  824. (nf_procvarload in hp.flags) then
  825. valid_for_assign:=true
  826. else
  827. CGMessagePos(hp.fileinfo,type_e_no_assign_to_addr);
  828. exit;
  829. end;
  830. selfn,
  831. funcretn :
  832. begin
  833. valid_for_assign:=true;
  834. exit;
  835. end;
  836. calln :
  837. begin
  838. { check return type }
  839. case hp.resulttype.def.deftype of
  840. arraydef :
  841. begin
  842. { dynamic arrays are allowed when there is also a
  843. vec node }
  844. if is_dynamic_array(hp.resulttype.def) and
  845. gotvec then
  846. begin
  847. gotderef:=true;
  848. gotpointer:=true;
  849. end;
  850. end;
  851. pointerdef :
  852. gotpointer:=true;
  853. objectdef :
  854. gotclass:=is_class_or_interface(hp.resulttype.def);
  855. recorddef, { handle record like class it needs a subscription }
  856. classrefdef :
  857. gotclass:=true;
  858. end;
  859. { 1. if it returns a pointer and we've found a deref,
  860. 2. if it returns a class or record and a subscription or with is found }
  861. if (gotpointer and gotderef) or
  862. (gotclass and (gotsubscript or gotwith)) then
  863. valid_for_assign:=true
  864. else
  865. CGMessagePos(hp.fileinfo,type_e_argument_cant_be_assigned);
  866. exit;
  867. end;
  868. loadn :
  869. begin
  870. case tloadnode(hp).symtableentry.typ of
  871. absolutesym,
  872. varsym :
  873. begin
  874. if (tvarsym(tloadnode(hp).symtableentry).varspez=vs_const) then
  875. begin
  876. { allow p^:= constructions with p is const parameter }
  877. if gotderef then
  878. valid_for_assign:=true
  879. else
  880. CGMessagePos(tloadnode(hp).fileinfo,type_e_no_assign_to_const);
  881. exit;
  882. end;
  883. { Are we at a with symtable, then we need to process the
  884. withrefnode also to check for maybe a const load }
  885. if (tloadnode(hp).symtable.symtabletype=withsymtable) then
  886. begin
  887. { continue with processing the withref node }
  888. hp:=tnode(twithsymtable(tloadnode(hp).symtable).withrefnode);
  889. gotwith:=true;
  890. end
  891. else
  892. begin
  893. { set the assigned flag for varsyms }
  894. if (tvarsym(tloadnode(hp).symtableentry).varstate=vs_declared) then
  895. tvarsym(tloadnode(hp).symtableentry).varstate:=vs_assigned;
  896. valid_for_assign:=true;
  897. exit;
  898. end;
  899. end;
  900. funcretsym :
  901. begin
  902. valid_for_assign:=true;
  903. exit;
  904. end;
  905. typedconstsym :
  906. begin
  907. if ttypedconstsym(tloadnode(hp).symtableentry).is_writable then
  908. valid_for_assign:=true
  909. else
  910. CGMessagePos(hp.fileinfo,type_e_no_assign_to_const);
  911. exit;
  912. end;
  913. else
  914. begin
  915. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  916. exit;
  917. end;
  918. end;
  919. end;
  920. else
  921. begin
  922. CGMessagePos(hp.fileinfo,type_e_variable_id_expected);
  923. exit;
  924. end;
  925. end;
  926. end;
  927. end;
  928. function valid_for_var(p:tnode):boolean;
  929. begin
  930. valid_for_var:=valid_for_assign(p,[]);
  931. end;
  932. function valid_for_formal_var(p : tnode) : boolean;
  933. begin
  934. valid_for_formal_var:=valid_for_assign(p,[valid_void]);
  935. end;
  936. function valid_for_formal_const(p : tnode) : boolean;
  937. var
  938. v : boolean;
  939. begin
  940. { p must have been firstpass'd before }
  941. { accept about anything but not a statement ! }
  942. case p.nodetype of
  943. calln,
  944. statementn,
  945. addrn :
  946. begin
  947. { addrn is not allowed as this generate a constant value,
  948. but a tp procvar are allowed (PFV) }
  949. if nf_procvarload in p.flags then
  950. v:=true
  951. else
  952. v:=false;
  953. end;
  954. else
  955. v:=true;
  956. end;
  957. valid_for_formal_const:=v;
  958. end;
  959. function valid_for_assignment(p:tnode):boolean;
  960. begin
  961. valid_for_assignment:=valid_for_assign(p,[valid_property]);
  962. end;
  963. end.
  964. {
  965. $Log$
  966. Revision 1.57 2003-01-02 22:21:19 peter
  967. * fixed previous operator change
  968. Revision 1.56 2003/01/02 19:50:21 peter
  969. * fixed operator checking for objects
  970. * made binary operator checking simpeler
  971. Revision 1.55 2002/12/27 18:06:32 peter
  972. * fix overload error for dynarr:=nil
  973. Revision 1.54 2002/12/22 16:34:49 peter
  974. * proc-procvar crash fixed (tw2277)
  975. Revision 1.53 2002/12/11 22:39:24 peter
  976. * better error message when no operator is found for equal
  977. Revision 1.52 2002/11/27 22:11:59 peter
  978. * rewrote isbinaryoverloadable to use a case. it's now much easier
  979. to understand what is happening
  980. Revision 1.51 2002/11/25 17:43:17 peter
  981. * splitted defbase in defutil,symutil,defcmp
  982. * merged isconvertable and is_equal into compare_defs(_ext)
  983. * made operator search faster by walking the list only once
  984. Revision 1.50 2002/10/07 20:12:08 peter
  985. * ugly hack to fix tb0411
  986. Revision 1.49 2002/10/05 00:47:03 peter
  987. * support dynamicarray<>nil
  988. Revision 1.48 2002/10/04 21:13:59 peter
  989. * ignore vecn,subscriptn when checking for a procvar loadn
  990. Revision 1.47 2002/09/16 18:09:34 peter
  991. * set_funcret_valid fixed when result was already used in a nested
  992. procedure
  993. Revision 1.46 2002/07/20 11:57:53 florian
  994. * types.pas renamed to defbase.pas because D6 contains a types
  995. unit so this would conflicts if D6 programms are compiled
  996. + Willamette/SSE2 instructions to assembler added
  997. Revision 1.45 2002/05/18 13:34:08 peter
  998. * readded missing revisions
  999. Revision 1.44 2002/05/16 19:46:37 carl
  1000. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1001. + try to fix temp allocation (still in ifdef)
  1002. + generic constructor calls
  1003. + start of tassembler / tmodulebase class cleanup
  1004. Revision 1.42 2002/04/02 17:11:28 peter
  1005. * tlocation,treference update
  1006. * LOC_CONSTANT added for better constant handling
  1007. * secondadd splitted in multiple routines
  1008. * location_force_reg added for loading a location to a register
  1009. of a specified size
  1010. * secondassignment parses now first the right and then the left node
  1011. (this is compatible with Kylix). This saves a lot of push/pop especially
  1012. with string operations
  1013. * adapted some routines to use the new cg methods
  1014. Revision 1.41 2002/01/16 09:33:46 jonas
  1015. * no longer allow assignments to pointer expressions (unless there's a
  1016. deref), reported by John Lee
  1017. }