htypechk.pas 40 KB

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