htypechk.pas 40 KB

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