pexpr.pas 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Does parsing of expression for Free Pascal
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pexpr;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symtype,
  23. node,
  24. globals,
  25. cpuinfo;
  26. { reads a whole expression }
  27. function expr : tnode;
  28. { reads an expression without assignements and .. }
  29. function comp_expr(accept_equal : boolean):tnode;
  30. { reads a single factor }
  31. function factor(getaddr : boolean) : tnode;
  32. procedure string_dec(var t: ttype);
  33. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  34. { the ID token has to be consumed before calling this function }
  35. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean);
  36. {$ifdef int64funcresok}
  37. function get_intconst:TConstExprInt;
  38. {$else int64funcresok}
  39. function get_intconst:longint;
  40. {$endif int64funcresok}
  41. function get_stringconst:string;
  42. implementation
  43. uses
  44. {$ifdef delphi}
  45. SysUtils,
  46. {$endif}
  47. { common }
  48. cutils,
  49. { global }
  50. globtype,tokens,verbose,
  51. systems,widestr,
  52. { symtable }
  53. symconst,symbase,symdef,symsym,symtable,defutil,defcmp,
  54. { pass 1 }
  55. pass_1,htypechk,
  56. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,
  57. { parser }
  58. scanner,
  59. pbase,pinline,
  60. { codegen }
  61. cgbase
  62. ;
  63. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  64. read as - (1**4) and not (-1)**4 PM }
  65. type
  66. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  67. const
  68. highest_precedence = oppower;
  69. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;forward;
  70. const
  71. got_addrn : boolean = false;
  72. anon_inherited : boolean = false;
  73. procedure string_dec(var t: ttype);
  74. { reads a string type with optional length }
  75. { and returns a pointer to the string }
  76. { definition }
  77. var
  78. p : tnode;
  79. begin
  80. t:=cshortstringtype;
  81. consume(_STRING);
  82. if token=_LECKKLAMMER then
  83. begin
  84. consume(_LECKKLAMMER);
  85. p:=comp_expr(true);
  86. if not is_constintnode(p) then
  87. begin
  88. Message(cg_e_illegal_expression);
  89. { error recovery }
  90. consume(_RECKKLAMMER);
  91. end
  92. else
  93. begin
  94. if (tordconstnode(p).value<=0) then
  95. begin
  96. Message(parser_e_invalid_string_size);
  97. tordconstnode(p).value:=255;
  98. end;
  99. consume(_RECKKLAMMER);
  100. if tordconstnode(p).value>255 then
  101. begin
  102. { longstring is currently unsupported (CEC)! }
  103. { t.setdef(tstringdef.createlong(tordconstnode(p).value))}
  104. Message(parser_e_invalid_string_size);
  105. tordconstnode(p).value:=255;
  106. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  107. end
  108. else
  109. if tordconstnode(p).value<>255 then
  110. t.setdef(tstringdef.createshort(tordconstnode(p).value));
  111. end;
  112. p.free;
  113. end
  114. else
  115. begin
  116. if cs_ansistrings in aktlocalswitches then
  117. t:=cansistringtype
  118. else
  119. t:=cshortstringtype;
  120. end;
  121. end;
  122. function parse_paras(__colon,in_prop_paras : boolean) : tnode;
  123. var
  124. p1,p2 : tnode;
  125. end_of_paras : ttoken;
  126. prev_in_args : boolean;
  127. old_allow_array_constructor : boolean;
  128. begin
  129. if in_prop_paras then
  130. end_of_paras:=_RECKKLAMMER
  131. else
  132. end_of_paras:=_RKLAMMER;
  133. if token=end_of_paras then
  134. begin
  135. parse_paras:=nil;
  136. exit;
  137. end;
  138. { save old values }
  139. prev_in_args:=in_args;
  140. old_allow_array_constructor:=allow_array_constructor;
  141. { set para parsing values }
  142. in_args:=true;
  143. inc(parsing_para_level);
  144. allow_array_constructor:=true;
  145. p2:=nil;
  146. while true do
  147. begin
  148. p1:=comp_expr(true);
  149. p2:=ccallparanode.create(p1,p2);
  150. { it's for the str(l:5,s); }
  151. if __colon and (token=_COLON) then
  152. begin
  153. consume(_COLON);
  154. p1:=comp_expr(true);
  155. p2:=ccallparanode.create(p1,p2);
  156. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  157. if token=_COLON then
  158. begin
  159. consume(_COLON);
  160. p1:=comp_expr(true);
  161. p2:=ccallparanode.create(p1,p2);
  162. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  163. end
  164. end;
  165. if token=_COMMA then
  166. consume(_COMMA)
  167. else
  168. break;
  169. end;
  170. allow_array_constructor:=old_allow_array_constructor;
  171. dec(parsing_para_level);
  172. in_args:=prev_in_args;
  173. parse_paras:=p2;
  174. end;
  175. procedure check_tp_procvar(var p : tnode);
  176. var
  177. hp,
  178. p1 : tnode;
  179. begin
  180. if (m_tp_procvar in aktmodeswitches) and
  181. (token<>_ASSIGNMENT) and
  182. (not got_addrn) and
  183. (block_type=bt_general) then
  184. begin
  185. { ignore vecn,subscriptn }
  186. hp:=p;
  187. repeat
  188. case hp.nodetype of
  189. vecn :
  190. hp:=tvecnode(hp).left;
  191. subscriptn :
  192. hp:=tsubscriptnode(hp).left;
  193. else
  194. break;
  195. end;
  196. until false;
  197. if (hp.nodetype=loadn) then
  198. begin
  199. { get the resulttype of p }
  200. do_resulttypepass(p);
  201. { convert the procvar load to a call:
  202. - not expecting a procvar
  203. - the procvar does not get arguments, when it
  204. requires arguments the callnode will fail
  205. Note: When arguments were passed there was no loadn }
  206. if (getprocvardef=nil) and
  207. (p.resulttype.def.deftype=procvardef) and
  208. (tprocvardef(p.resulttype.def).minparacount=0) then
  209. begin
  210. p1:=ccallnode.create(nil,nil,nil,nil);
  211. tcallnode(p1).set_procvar(p);
  212. resulttypepass(p1);
  213. p:=p1;
  214. end;
  215. end;
  216. end;
  217. end;
  218. function statement_syssym(l : longint) : tnode;
  219. var
  220. p1,p2,paras : tnode;
  221. err,
  222. prev_in_args : boolean;
  223. begin
  224. prev_in_args:=in_args;
  225. case l of
  226. in_new_x :
  227. begin
  228. if afterassignment or in_args then
  229. statement_syssym:=new_function
  230. else
  231. statement_syssym:=new_dispose_statement(true);
  232. end;
  233. in_dispose_x :
  234. begin
  235. statement_syssym:=new_dispose_statement(false);
  236. end;
  237. in_ord_x :
  238. begin
  239. consume(_LKLAMMER);
  240. in_args:=true;
  241. p1:=comp_expr(true);
  242. consume(_RKLAMMER);
  243. p1:=geninlinenode(in_ord_x,false,p1);
  244. statement_syssym := p1;
  245. end;
  246. in_exit :
  247. begin
  248. if try_to_consume(_LKLAMMER) then
  249. begin
  250. p1:=comp_expr(true);
  251. consume(_RKLAMMER);
  252. if (block_type=bt_except) then
  253. Message(parser_e_exit_with_argument_not__possible);
  254. if is_void(aktprocdef.rettype.def) then
  255. Message(parser_e_void_function);
  256. end
  257. else
  258. p1:=nil;
  259. statement_syssym:=cexitnode.create(p1);
  260. end;
  261. in_break :
  262. begin
  263. statement_syssym:=cbreaknode.create;
  264. end;
  265. in_continue :
  266. begin
  267. statement_syssym:=ccontinuenode.create;
  268. end;
  269. in_typeof_x :
  270. begin
  271. consume(_LKLAMMER);
  272. in_args:=true;
  273. p1:=comp_expr(true);
  274. consume(_RKLAMMER);
  275. if p1.nodetype=typen then
  276. ttypenode(p1).allowed:=true;
  277. if (p1.resulttype.def.deftype = objectdef) or
  278. ((p1.resulttype.def.deftype = classrefdef) and
  279. (p1.nodetype in [selfn,loadvmtn])) then
  280. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  281. else
  282. begin
  283. Message(type_e_mismatch);
  284. p1.destroy;
  285. statement_syssym:=cerrornode.create;
  286. end;
  287. end;
  288. in_sizeof_x :
  289. begin
  290. consume(_LKLAMMER);
  291. in_args:=true;
  292. p1:=comp_expr(true);
  293. consume(_RKLAMMER);
  294. if (p1.nodetype<>typen) and
  295. (
  296. (is_object(p1.resulttype.def) and
  297. (oo_has_constructor in tobjectdef(p1.resulttype.def).objectoptions)) or
  298. is_open_array(p1.resulttype.def) or
  299. is_open_string(p1.resulttype.def)
  300. ) then
  301. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  302. else
  303. begin
  304. statement_syssym:=cordconstnode.create(p1.resulttype.def.size,s32bittype,true);
  305. { p1 not needed !}
  306. p1.destroy;
  307. end;
  308. end;
  309. in_typeinfo_x :
  310. begin
  311. consume(_LKLAMMER);
  312. in_args:=true;
  313. p1:=comp_expr(true);
  314. if p1.nodetype=typen then
  315. ttypenode(p1).allowed:=true
  316. else
  317. begin
  318. p1.destroy;
  319. p1:=cerrornode.create;
  320. Message(parser_e_illegal_parameter_list);
  321. end;
  322. consume(_RKLAMMER);
  323. p2:=ccallparanode.create(p1,nil);
  324. p2:=geninlinenode(in_typeinfo_x,false,p2);
  325. statement_syssym:=p2;
  326. end;
  327. in_assigned_x :
  328. begin
  329. err:=false;
  330. consume(_LKLAMMER);
  331. in_args:=true;
  332. p1:=comp_expr(true);
  333. if not codegenerror then
  334. begin
  335. { With tp procvars we allways need to load a
  336. procvar when it is passed }
  337. if (m_tp_procvar in aktmodeswitches) and
  338. (p1.nodetype=calln) then
  339. load_procvar_from_calln(p1);
  340. case p1.resulttype.def.deftype of
  341. pointerdef,
  342. procvardef,
  343. classrefdef : ;
  344. objectdef :
  345. if not is_class_or_interface(p1.resulttype.def) then
  346. begin
  347. Message(parser_e_illegal_parameter_list);
  348. err:=true;
  349. end;
  350. else
  351. begin
  352. Message(parser_e_illegal_parameter_list);
  353. err:=true;
  354. end;
  355. end;
  356. end
  357. else
  358. err:=true;
  359. if not err then
  360. begin
  361. p2:=ccallparanode.create(p1,nil);
  362. p2:=geninlinenode(in_assigned_x,false,p2);
  363. end
  364. else
  365. begin
  366. p1.free;
  367. p2:=cerrornode.create;
  368. end;
  369. consume(_RKLAMMER);
  370. statement_syssym:=p2;
  371. end;
  372. in_addr_x :
  373. begin
  374. consume(_LKLAMMER);
  375. in_args:=true;
  376. p1:=comp_expr(true);
  377. p1:=caddrnode.create(p1);
  378. consume(_RKLAMMER);
  379. statement_syssym:=p1;
  380. end;
  381. in_ofs_x :
  382. begin
  383. consume(_LKLAMMER);
  384. in_args:=true;
  385. p1:=comp_expr(true);
  386. p1:=caddrnode.create(p1);
  387. do_resulttypepass(p1);
  388. { Ofs() returns a cardinal, not a pointer }
  389. p1.resulttype:=u32bittype;
  390. consume(_RKLAMMER);
  391. statement_syssym:=p1;
  392. end;
  393. in_seg_x :
  394. begin
  395. consume(_LKLAMMER);
  396. in_args:=true;
  397. p1:=comp_expr(true);
  398. p1:=geninlinenode(in_seg_x,false,p1);
  399. consume(_RKLAMMER);
  400. statement_syssym:=p1;
  401. end;
  402. in_high_x,
  403. in_low_x :
  404. begin
  405. consume(_LKLAMMER);
  406. in_args:=true;
  407. p1:=comp_expr(true);
  408. p2:=geninlinenode(l,false,p1);
  409. consume(_RKLAMMER);
  410. statement_syssym:=p2;
  411. end;
  412. in_succ_x,
  413. in_pred_x :
  414. begin
  415. consume(_LKLAMMER);
  416. in_args:=true;
  417. p1:=comp_expr(true);
  418. p2:=geninlinenode(l,false,p1);
  419. consume(_RKLAMMER);
  420. statement_syssym:=p2;
  421. end;
  422. in_inc_x,
  423. in_dec_x :
  424. begin
  425. consume(_LKLAMMER);
  426. in_args:=true;
  427. p1:=comp_expr(true);
  428. if token=_COMMA then
  429. begin
  430. consume(_COMMA);
  431. p2:=ccallparanode.create(comp_expr(true),nil);
  432. end
  433. else
  434. p2:=nil;
  435. p2:=ccallparanode.create(p1,p2);
  436. statement_syssym:=geninlinenode(l,false,p2);
  437. consume(_RKLAMMER);
  438. end;
  439. in_finalize_x:
  440. begin
  441. statement_syssym:=inline_finalize;
  442. end;
  443. in_copy_x:
  444. begin
  445. statement_syssym:=inline_copy;
  446. end;
  447. in_concat_x :
  448. begin
  449. consume(_LKLAMMER);
  450. in_args:=true;
  451. p2:=nil;
  452. while true do
  453. begin
  454. p1:=comp_expr(true);
  455. set_varstate(p1,true);
  456. if not((p1.resulttype.def.deftype=stringdef) or
  457. ((p1.resulttype.def.deftype=orddef) and
  458. (torddef(p1.resulttype.def).typ=uchar))) then
  459. Message(parser_e_illegal_parameter_list);
  460. if p2<>nil then
  461. p2:=caddnode.create(addn,p2,p1)
  462. else
  463. p2:=p1;
  464. if token=_COMMA then
  465. consume(_COMMA)
  466. else
  467. break;
  468. end;
  469. consume(_RKLAMMER);
  470. statement_syssym:=p2;
  471. end;
  472. in_read_x,
  473. in_readln_x :
  474. begin
  475. if token=_LKLAMMER then
  476. begin
  477. consume(_LKLAMMER);
  478. paras:=parse_paras(false,false);
  479. consume(_RKLAMMER);
  480. end
  481. else
  482. paras:=nil;
  483. p1:=geninlinenode(l,false,paras);
  484. statement_syssym := p1;
  485. end;
  486. in_setlength_x:
  487. begin
  488. statement_syssym := inline_setlength;
  489. end;
  490. in_length_x:
  491. begin
  492. consume(_LKLAMMER);
  493. in_args:=true;
  494. p1:=comp_expr(true);
  495. p2:=geninlinenode(l,false,p1);
  496. consume(_RKLAMMER);
  497. statement_syssym:=p2;
  498. end;
  499. in_write_x,
  500. in_writeln_x :
  501. begin
  502. if token=_LKLAMMER then
  503. begin
  504. consume(_LKLAMMER);
  505. paras:=parse_paras(true,false);
  506. consume(_RKLAMMER);
  507. end
  508. else
  509. paras:=nil;
  510. p1 := geninlinenode(l,false,paras);
  511. statement_syssym := p1;
  512. end;
  513. in_str_x_string :
  514. begin
  515. consume(_LKLAMMER);
  516. paras:=parse_paras(true,false);
  517. consume(_RKLAMMER);
  518. p1 := geninlinenode(l,false,paras);
  519. statement_syssym := p1;
  520. end;
  521. in_val_x:
  522. Begin
  523. consume(_LKLAMMER);
  524. in_args := true;
  525. p1:= ccallparanode.create(comp_expr(true), nil);
  526. consume(_COMMA);
  527. p2 := ccallparanode.create(comp_expr(true),p1);
  528. if (token = _COMMA) then
  529. Begin
  530. consume(_COMMA);
  531. p2 := ccallparanode.create(comp_expr(true),p2)
  532. End;
  533. consume(_RKLAMMER);
  534. p2 := geninlinenode(l,false,p2);
  535. statement_syssym := p2;
  536. End;
  537. in_include_x_y,
  538. in_exclude_x_y :
  539. begin
  540. consume(_LKLAMMER);
  541. in_args:=true;
  542. p1:=comp_expr(true);
  543. consume(_COMMA);
  544. p2:=comp_expr(true);
  545. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  546. consume(_RKLAMMER);
  547. end;
  548. in_assert_x_y :
  549. begin
  550. consume(_LKLAMMER);
  551. in_args:=true;
  552. p1:=comp_expr(true);
  553. if token=_COMMA then
  554. begin
  555. consume(_COMMA);
  556. p2:=comp_expr(true);
  557. end
  558. else
  559. begin
  560. { then insert an empty string }
  561. p2:=cstringconstnode.createstr('',st_default);
  562. end;
  563. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  564. consume(_RKLAMMER);
  565. end;
  566. else
  567. internalerror(15);
  568. end;
  569. in_args:=prev_in_args;
  570. end;
  571. { reads the parameter for a subroutine call }
  572. procedure do_proc_call(sym:tsym;st:tsymtable;getaddr:boolean;var again : boolean;var p1:tnode);
  573. var
  574. prevafterassn : boolean;
  575. hs,hs1 : tvarsym;
  576. para,p2 : tnode;
  577. hst : tsymtable;
  578. aprocdef : tprocdef;
  579. begin
  580. prevafterassn:=afterassignment;
  581. afterassignment:=false;
  582. aprocdef:=nil;
  583. { When we are expecting a procvar we also need
  584. to get the address in some cases }
  585. if assigned(getprocvardef) then
  586. begin
  587. if (block_type=bt_const) or
  588. getaddr then
  589. begin
  590. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  591. getaddr:=true;
  592. end
  593. else
  594. if (m_tp_procvar in aktmodeswitches) then
  595. begin
  596. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  597. if assigned(aprocdef) then
  598. getaddr:=true;
  599. end;
  600. end;
  601. { want we only determine the address of }
  602. { a subroutine ? }
  603. if not(getaddr) then
  604. begin
  605. para:=nil;
  606. if anon_inherited then
  607. begin
  608. hst:=symtablestack;
  609. while assigned(hst) and (hst.symtabletype<>parasymtable) do
  610. hst:=hst.next;
  611. if assigned(hst) then
  612. begin
  613. hs:=tvarsym(hst.symindex.first);
  614. while assigned(hs) do
  615. begin
  616. if hs.typ<>varsym then
  617. internalerror(54382953);
  618. { if there is a localcopy then use that }
  619. if assigned(hs.localvarsym) then
  620. hs1:=hs.localvarsym
  621. else
  622. hs1:=hs;
  623. para:=ccallparanode.create(cloadnode.create(hs1,hs1.owner),para);
  624. hs:=tvarsym(hs.indexnext);
  625. end;
  626. end
  627. else
  628. internalerror(54382954);
  629. end
  630. else
  631. begin
  632. if try_to_consume(_LKLAMMER) then
  633. begin
  634. para:=parse_paras(false,false);
  635. consume(_RKLAMMER);
  636. end;
  637. end;
  638. p1:=ccallnode.create(para,tprocsym(sym),st,p1);
  639. end
  640. else
  641. begin
  642. { address operator @: }
  643. if not assigned(p1) then
  644. begin
  645. case st.symtabletype of
  646. withsymtable :
  647. begin
  648. if (st.defowner.deftype=objectdef) then
  649. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  650. end;
  651. objectsymtable :
  652. begin
  653. { we must provide a method pointer, if it isn't given, }
  654. { it is self }
  655. p1:=cselfnode.create(tobjectdef(st.defowner));
  656. end;
  657. end;
  658. end;
  659. { Retrieve info which procvar to call. For tp_procvar the
  660. aprocdef is already loaded above so we can reuse it }
  661. if not assigned(aprocdef) and
  662. assigned(getprocvardef) then
  663. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  664. { generate a methodcallnode or proccallnode }
  665. { we shouldn't convert things like @tcollection.load }
  666. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  667. if assigned(p1) then
  668. begin
  669. if (p1.nodetype<>typen) then
  670. tloadnode(p2).set_mp(p1)
  671. else
  672. p1.free;
  673. end;
  674. p1:=p2;
  675. { no postfix operators }
  676. again:=false;
  677. end;
  678. afterassignment:=prevafterassn;
  679. end;
  680. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  681. var
  682. hp,hp2 : tnode;
  683. hpp : ^tnode;
  684. currprocdef : tprocdef;
  685. begin
  686. if not assigned(pv) then
  687. internalerror(200301121);
  688. if (m_tp_procvar in aktmodeswitches) then
  689. begin
  690. hp:=p2;
  691. hpp:=@p2;
  692. while assigned(hp) and
  693. (hp.nodetype=typeconvn) do
  694. begin
  695. hp:=ttypeconvnode(hp).left;
  696. { save orignal address of the old tree so we can replace the node }
  697. hpp:=@hp;
  698. end;
  699. if (hp.nodetype=calln) and
  700. { a procvar can't have parameters! }
  701. not assigned(tcallnode(hp).left) then
  702. begin
  703. currprocdef:=tcallnode(hp).symtableprocentry.search_procdef_byprocvardef(pv);
  704. if assigned(currprocdef) then
  705. begin
  706. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  707. if (po_methodpointer in pv.procoptions) then
  708. tloadnode(hp2).set_mp(tnode(tcallnode(hp).methodpointer).getcopy);
  709. hp.destroy;
  710. { replace the old callnode with the new loadnode }
  711. hpp^:=hp2;
  712. end;
  713. end;
  714. end;
  715. end;
  716. { the following procedure handles the access to a property symbol }
  717. procedure handle_propertysym(sym : tsym;st : tsymtable;var p1 : tnode; getaddr: boolean);
  718. procedure symlist_to_node(var p1:tnode;pl:tsymlist);
  719. var
  720. plist : psymlistitem;
  721. begin
  722. plist:=pl.firstsym;
  723. while assigned(plist) do
  724. begin
  725. case plist^.sltype of
  726. sl_load :
  727. begin
  728. { p1 can already contain the loadnode of
  729. the class variable. Then we need to use a
  730. subscriptn. If no tree is found (with block), then
  731. generate a loadn }
  732. if assigned(p1) then
  733. p1:=csubscriptnode.create(plist^.sym,p1)
  734. else
  735. p1:=cloadnode.create(plist^.sym,st);
  736. end;
  737. sl_subscript :
  738. p1:=csubscriptnode.create(plist^.sym,p1);
  739. sl_vec :
  740. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,s32bittype,true));
  741. else
  742. internalerror(200110205);
  743. end;
  744. plist:=plist^.next;
  745. end;
  746. include(p1.flags,nf_isproperty);
  747. end;
  748. var
  749. paras : tnode;
  750. p2 : tnode;
  751. begin
  752. paras:=nil;
  753. { property parameters? read them only if the property really }
  754. { has parameters }
  755. if (ppo_hasparameters in tpropertysym(sym).propoptions) then
  756. begin
  757. if token=_LECKKLAMMER then
  758. begin
  759. consume(_LECKKLAMMER);
  760. paras:=parse_paras(false,true);
  761. consume(_RECKKLAMMER);
  762. end;
  763. end;
  764. { indexed property }
  765. if (ppo_indexed in tpropertysym(sym).propoptions) then
  766. begin
  767. p2:=cordconstnode.create(tpropertysym(sym).index,tpropertysym(sym).indextype,true);
  768. paras:=ccallparanode.create(p2,paras);
  769. end;
  770. { we need only a write property if a := follows }
  771. { if not(afterassignment) and not(in_args) then }
  772. if token=_ASSIGNMENT then
  773. begin
  774. { write property: }
  775. if not tpropertysym(sym).writeaccess.empty then
  776. begin
  777. case tpropertysym(sym).writeaccess.firstsym^.sym.typ of
  778. procsym :
  779. begin
  780. { generate the method call }
  781. p1:=ccallnode.create(paras,
  782. tprocsym(tpropertysym(sym).writeaccess.firstsym^.sym),st,p1);
  783. paras:=nil;
  784. consume(_ASSIGNMENT);
  785. { read the expression }
  786. if tpropertysym(sym).proptype.def.deftype=procvardef then
  787. getprocvardef:=tprocvardef(tpropertysym(sym).proptype.def);
  788. p2:=comp_expr(true);
  789. if assigned(getprocvardef) then
  790. handle_procvar(getprocvardef,p2);
  791. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  792. include(tcallnode(p1).flags,nf_isproperty);
  793. getprocvardef:=nil;
  794. end;
  795. varsym :
  796. begin
  797. { generate access code }
  798. symlist_to_node(p1,tpropertysym(sym).writeaccess);
  799. consume(_ASSIGNMENT);
  800. { read the expression }
  801. p2:=comp_expr(true);
  802. p1:=cassignmentnode.create(p1,p2);
  803. end
  804. else
  805. begin
  806. p1:=cerrornode.create;
  807. Message(parser_e_no_procedure_to_access_property);
  808. end;
  809. end;
  810. end
  811. else
  812. begin
  813. p1:=cerrornode.create;
  814. Message(parser_e_no_procedure_to_access_property);
  815. end;
  816. end
  817. else
  818. begin
  819. { read property: }
  820. if not tpropertysym(sym).readaccess.empty then
  821. begin
  822. case tpropertysym(sym).readaccess.firstsym^.sym.typ of
  823. varsym :
  824. begin
  825. { generate access code }
  826. symlist_to_node(p1,tpropertysym(sym).readaccess);
  827. end;
  828. procsym :
  829. begin
  830. { generate the method call }
  831. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).readaccess.firstsym^.sym),st,p1);
  832. paras:=nil;
  833. include(p1.flags,nf_isproperty);
  834. end
  835. else
  836. begin
  837. p1:=cerrornode.create;
  838. Message(type_e_mismatch);
  839. end;
  840. end;
  841. end
  842. else
  843. begin
  844. { error, no function to read property }
  845. p1:=cerrornode.create;
  846. Message(parser_e_no_procedure_to_access_property);
  847. end;
  848. end;
  849. { release paras if not used }
  850. if assigned(paras) then
  851. paras.free;
  852. end;
  853. { the ID token has to be consumed before calling this function }
  854. procedure do_member_read(getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean);
  855. var
  856. static_name : string;
  857. isclassref : boolean;
  858. srsymtable : tsymtable;
  859. begin
  860. if sym=nil then
  861. begin
  862. { pattern is still valid unless
  863. there is another ID just after the ID of sym }
  864. Message1(sym_e_id_no_member,pattern);
  865. p1.free;
  866. p1:=cerrornode.create;
  867. { try to clean up }
  868. again:=false;
  869. end
  870. else
  871. begin
  872. if assigned(p1) then
  873. begin
  874. if not assigned(p1.resulttype.def) then
  875. do_resulttypepass(p1);
  876. isclassref:=(p1.resulttype.def.deftype=classrefdef);
  877. end
  878. else
  879. isclassref:=false;
  880. { we assume, that only procsyms and varsyms are in an object }
  881. { symbol table, for classes, properties are allowed }
  882. case sym.typ of
  883. procsym:
  884. begin
  885. do_proc_call(sym,sym.owner,
  886. (getaddr and not(token in [_CARET,_POINT])),
  887. again,p1);
  888. { we need to know which procedure is called }
  889. do_resulttypepass(p1);
  890. { now we know the real method e.g. we can check for a class method }
  891. if isclassref and
  892. (p1.nodetype=calln) and
  893. assigned(tcallnode(p1).procdefinition) and
  894. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  895. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  896. Message(parser_e_only_class_methods_via_class_ref);
  897. end;
  898. varsym:
  899. begin
  900. if isclassref then
  901. Message(parser_e_only_class_methods_via_class_ref);
  902. if (sp_static in sym.symoptions) then
  903. begin
  904. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  905. searchsym(static_name,sym,srsymtable);
  906. check_hints(sym);
  907. p1.free;
  908. p1:=cloadnode.create(sym,srsymtable);
  909. end
  910. else
  911. p1:=csubscriptnode.create(sym,p1);
  912. end;
  913. propertysym:
  914. begin
  915. if isclassref then
  916. Message(parser_e_only_class_methods_via_class_ref);
  917. handle_propertysym(sym,sym.owner,p1,getaddr);
  918. end;
  919. else internalerror(16);
  920. end;
  921. end;
  922. end;
  923. {****************************************************************************
  924. Factor
  925. ****************************************************************************}
  926. {$ifdef fpc}
  927. {$maxfpuregisters 0}
  928. {$endif fpc}
  929. function factor(getaddr : boolean) : tnode;
  930. {---------------------------------------------
  931. Is_func_ret
  932. ---------------------------------------------}
  933. function is_func_ret(var p1:tnode;var sym : tsym;var srsymtable:tsymtable) : boolean;
  934. var
  935. p : tprocinfo;
  936. storesymtablestack : tsymtable;
  937. begin
  938. is_func_ret:=false;
  939. if not assigned(procinfo) or
  940. ((sym.typ<>funcretsym) and ((procinfo.flags and pi_operator)=0)) then
  941. exit;
  942. p:=procinfo;
  943. while assigned(p) do
  944. begin
  945. { is this an access to a function result? Accessing _RESULT is
  946. always allowed and funcretn is generated }
  947. if assigned(p.procdef.funcretsym) and
  948. ((sym=tsym(p.procdef.resultfuncretsym)) or
  949. ((sym=tsym(p.procdef.funcretsym)) or
  950. ((sym=tsym(otsym)) and ((p.flags and pi_operator)<>0))) and
  951. (not is_void(p.procdef.rettype.def)) and
  952. (token<>_LKLAMMER) and
  953. (not (not(m_fpc in aktmodeswitches) and (afterassignment or in_args)))
  954. ) then
  955. begin
  956. if ((sym=tsym(otsym)) and
  957. ((p.flags and pi_operator)<>0)) then
  958. inc(otsym.refs);
  959. p1:=cfuncretnode.create(p.procdef.funcretsym);
  960. is_func_ret:=true;
  961. if tfuncretsym(p.procdef.funcretsym).funcretstate=vs_declared then
  962. begin
  963. tfuncretsym(p.procdef.funcretsym).funcretstate:=vs_declared_and_first_found;
  964. include(p1.flags,nf_is_first_funcret);
  965. end;
  966. exit;
  967. end;
  968. p:=p.parent;
  969. end;
  970. { we must use the function call, update the
  971. sym to be the procsym }
  972. if (sym.typ=funcretsym) then
  973. begin
  974. storesymtablestack:=symtablestack;
  975. symtablestack:=sym.owner.next;
  976. searchsym(sym.name,sym,srsymtable);
  977. check_hints(sym);
  978. if not assigned(sym) then
  979. sym:=generrorsym;
  980. if (sym.typ<>procsym) then
  981. Message(cg_e_illegal_expression);
  982. symtablestack:=storesymtablestack;
  983. end;
  984. end;
  985. {---------------------------------------------
  986. Factor_read_id
  987. ---------------------------------------------}
  988. procedure factor_read_id(var p1:tnode;var again:boolean);
  989. var
  990. pc : pchar;
  991. len : longint;
  992. srsym : tsym;
  993. possible_error : boolean;
  994. srsymtable : tsymtable;
  995. htype : ttype;
  996. static_name : string;
  997. begin
  998. { allow post fix operators }
  999. again:=true;
  1000. consume_sym(srsym,srsymtable);
  1001. if not is_func_ret(p1,srsym,srsymtable) then
  1002. begin
  1003. { check semantics of private }
  1004. if (srsym.typ in [propertysym,procsym,varsym]) and
  1005. (srsym.owner.symtabletype=objectsymtable) then
  1006. begin
  1007. if (sp_private in srsym.symoptions) and
  1008. (tobjectdef(srsym.owner.defowner).owner.symtabletype=globalsymtable) and
  1009. (tobjectdef(srsym.owner.defowner).owner.unitid<>0) then
  1010. Message(parser_e_cant_access_private_member);
  1011. end;
  1012. case srsym.typ of
  1013. absolutesym :
  1014. begin
  1015. p1:=cloadnode.create(srsym,srsymtable);
  1016. end;
  1017. varsym :
  1018. begin
  1019. { are we in a class method ? }
  1020. if (srsym.owner.symtabletype=objectsymtable) and
  1021. assigned(aktprocsym) and
  1022. (po_classmethod in aktprocdef.procoptions) then
  1023. Message(parser_e_only_class_methods);
  1024. if (sp_static in srsym.symoptions) then
  1025. begin
  1026. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  1027. searchsym(static_name,srsym,srsymtable);
  1028. check_hints(srsym);
  1029. end;
  1030. p1:=cloadnode.create(srsym,srsymtable);
  1031. if tvarsym(srsym).varstate=vs_declared then
  1032. begin
  1033. include(p1.flags,nf_first);
  1034. { set special between first loaded until checked in resulttypepass }
  1035. tvarsym(srsym).varstate:=vs_declared_and_first_found;
  1036. end;
  1037. end;
  1038. typedconstsym :
  1039. begin
  1040. p1:=cloadnode.create(srsym,srsymtable);
  1041. end;
  1042. syssym :
  1043. begin
  1044. p1:=statement_syssym(tsyssym(srsym).number);
  1045. end;
  1046. typesym :
  1047. begin
  1048. htype.setsym(srsym);
  1049. if not assigned(htype.def) then
  1050. begin
  1051. again:=false;
  1052. end
  1053. else
  1054. begin
  1055. if token=_LKLAMMER then
  1056. begin
  1057. consume(_LKLAMMER);
  1058. p1:=comp_expr(true);
  1059. consume(_RKLAMMER);
  1060. p1:=ctypeconvnode.create(p1,htype);
  1061. include(p1.flags,nf_explizit);
  1062. end
  1063. else { not LKLAMMER }
  1064. if (token=_POINT) and
  1065. is_object(htype.def) then
  1066. begin
  1067. consume(_POINT);
  1068. if assigned(procinfo) and
  1069. assigned(procinfo._class) and
  1070. not(getaddr) then
  1071. begin
  1072. if procinfo._class.is_related(tobjectdef(htype.def)) then
  1073. begin
  1074. p1:=ctypenode.create(htype);
  1075. { search also in inherited methods }
  1076. srsym:=searchsym_in_class(tobjectdef(htype.def),pattern);
  1077. check_hints(srsym);
  1078. consume(_ID);
  1079. do_member_read(false,srsym,p1,again);
  1080. end
  1081. else
  1082. begin
  1083. Message(parser_e_no_super_class);
  1084. again:=false;
  1085. end;
  1086. end
  1087. else
  1088. begin
  1089. { allows @TObject.Load }
  1090. { also allows static methods and variables }
  1091. p1:=ctypenode.create(htype);
  1092. { TP allows also @TMenu.Load if Load is only }
  1093. { defined in an anchestor class }
  1094. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1095. check_hints(srsym);
  1096. if not assigned(srsym) then
  1097. Message1(sym_e_id_no_member,pattern)
  1098. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1099. Message(sym_e_only_static_in_static)
  1100. else
  1101. begin
  1102. consume(_ID);
  1103. do_member_read(getaddr,srsym,p1,again);
  1104. end;
  1105. end;
  1106. end
  1107. else
  1108. begin
  1109. { class reference ? }
  1110. if is_class(htype.def) then
  1111. begin
  1112. if getaddr and (token=_POINT) then
  1113. begin
  1114. consume(_POINT);
  1115. { allows @Object.Method }
  1116. { also allows static methods and variables }
  1117. p1:=ctypenode.create(htype);
  1118. { TP allows also @TMenu.Load if Load is only }
  1119. { defined in an anchestor class }
  1120. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1121. check_hints(srsym);
  1122. if not assigned(srsym) then
  1123. Message1(sym_e_id_no_member,pattern)
  1124. else
  1125. begin
  1126. consume(_ID);
  1127. do_member_read(getaddr,srsym,p1,again);
  1128. end;
  1129. end
  1130. else
  1131. begin
  1132. p1:=ctypenode.create(htype);
  1133. { For a type block we simply return only
  1134. the type. For all other blocks we return
  1135. a loadvmt node }
  1136. if (block_type<>bt_type) then
  1137. p1:=cloadvmtnode.create(p1);
  1138. end;
  1139. end
  1140. else
  1141. p1:=ctypenode.create(htype);
  1142. end;
  1143. end;
  1144. end;
  1145. enumsym :
  1146. begin
  1147. p1:=genenumnode(tenumsym(srsym));
  1148. end;
  1149. constsym :
  1150. begin
  1151. case tconstsym(srsym).consttyp of
  1152. constint :
  1153. begin
  1154. { do a very dirty trick to bootstrap this code }
  1155. if (tconstsym(srsym).value.valueord>=-(int64(2147483647)+int64(1))) and
  1156. (tconstsym(srsym).value.valueord<=2147483647) then
  1157. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,s32bittype,true)
  1158. else if (tconstsym(srsym).value.valueord > maxlongint) and
  1159. (tconstsym(srsym).value.valueord <= int64(maxlongint)+int64(maxlongint)+1) then
  1160. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,u32bittype,true)
  1161. else
  1162. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cs64bittype,true);
  1163. end;
  1164. conststring :
  1165. begin
  1166. len:=tconstsym(srsym).value.len;
  1167. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1168. len:=255;
  1169. getmem(pc,len+1);
  1170. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1171. pc[len]:=#0;
  1172. p1:=cstringconstnode.createpchar(pc,len);
  1173. end;
  1174. constchar :
  1175. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cchartype,true);
  1176. constreal :
  1177. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1178. constbool :
  1179. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,booltype,true);
  1180. constset :
  1181. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).consttype);
  1182. constord :
  1183. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).consttype,true);
  1184. constpointer :
  1185. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).consttype);
  1186. constnil :
  1187. p1:=cnilnode.create;
  1188. constresourcestring:
  1189. begin
  1190. p1:=cloadnode.create(srsym,srsymtable);
  1191. do_resulttypepass(p1);
  1192. p1.resulttype:=cansistringtype;
  1193. end;
  1194. constguid :
  1195. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1196. end;
  1197. end;
  1198. procsym :
  1199. begin
  1200. { are we in a class method ? }
  1201. possible_error:=(srsym.owner.symtabletype=objectsymtable) and
  1202. not(is_interface(tdef(srsym.owner.defowner))) and
  1203. assigned(aktprocsym) and
  1204. (po_classmethod in aktprocdef.procoptions);
  1205. do_proc_call(srsym,srsymtable,
  1206. (getaddr and not(token in [_CARET,_POINT])),
  1207. again,p1);
  1208. { we need to know which procedure is called }
  1209. if possible_error then
  1210. begin
  1211. do_resulttypepass(p1);
  1212. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1213. Message(parser_e_only_class_methods);
  1214. end;
  1215. end;
  1216. propertysym :
  1217. begin
  1218. { access to property in a method }
  1219. { are we in a class method ? }
  1220. if (srsym.owner.symtabletype=objectsymtable) and
  1221. assigned(aktprocsym) and
  1222. (po_classmethod in aktprocdef.procoptions) then
  1223. Message(parser_e_only_class_methods);
  1224. { no method pointer }
  1225. p1:=nil;
  1226. handle_propertysym(srsym,srsymtable,p1,getaddr);
  1227. end;
  1228. labelsym :
  1229. begin
  1230. consume(_COLON);
  1231. if tlabelsym(srsym).defined then
  1232. Message(sym_e_label_already_defined);
  1233. tlabelsym(srsym).defined:=true;
  1234. p1:=clabelnode.create(tlabelsym(srsym),nil);
  1235. end;
  1236. errorsym :
  1237. begin
  1238. p1:=cerrornode.create;
  1239. if token=_LKLAMMER then
  1240. begin
  1241. consume(_LKLAMMER);
  1242. parse_paras(false,false);
  1243. consume(_RKLAMMER);
  1244. end;
  1245. end;
  1246. else
  1247. begin
  1248. p1:=cerrornode.create;
  1249. Message(cg_e_illegal_expression);
  1250. end;
  1251. end; { end case }
  1252. end;
  1253. end;
  1254. {---------------------------------------------
  1255. Factor_Read_Set
  1256. ---------------------------------------------}
  1257. { Read a set between [] }
  1258. function factor_read_set:tnode;
  1259. var
  1260. p1,p2 : tnode;
  1261. lastp,
  1262. buildp : tarrayconstructornode;
  1263. begin
  1264. buildp:=nil;
  1265. { be sure that a least one arrayconstructn is used, also for an
  1266. empty [] }
  1267. if token=_RECKKLAMMER then
  1268. buildp:=carrayconstructornode.create(nil,buildp)
  1269. else
  1270. begin
  1271. while true do
  1272. begin
  1273. p1:=comp_expr(true);
  1274. if token=_POINTPOINT then
  1275. begin
  1276. consume(_POINTPOINT);
  1277. p2:=comp_expr(true);
  1278. p1:=carrayconstructorrangenode.create(p1,p2);
  1279. end;
  1280. { insert at the end of the tree, to get the correct order }
  1281. if not assigned(buildp) then
  1282. begin
  1283. buildp:=carrayconstructornode.create(p1,nil);
  1284. lastp:=buildp;
  1285. end
  1286. else
  1287. begin
  1288. lastp.right:=carrayconstructornode.create(p1,nil);
  1289. lastp:=tarrayconstructornode(lastp.right);
  1290. end;
  1291. { there could be more elements }
  1292. if token=_COMMA then
  1293. consume(_COMMA)
  1294. else
  1295. break;
  1296. end;
  1297. end;
  1298. factor_read_set:=buildp;
  1299. end;
  1300. {---------------------------------------------
  1301. PostFixOperators
  1302. ---------------------------------------------}
  1303. procedure postfixoperators(var p1:tnode;var again:boolean);
  1304. { tries to avoid syntax errors after invalid qualifiers }
  1305. procedure recoverconsume_postfixops;
  1306. begin
  1307. while true do
  1308. begin
  1309. case token of
  1310. _CARET:
  1311. consume(_CARET);
  1312. _POINT:
  1313. begin
  1314. consume(_POINT);
  1315. if token=_ID then
  1316. consume(_ID);
  1317. end;
  1318. _LECKKLAMMER:
  1319. begin
  1320. consume(_LECKKLAMMER);
  1321. repeat
  1322. comp_expr(true);
  1323. if token=_COMMA then
  1324. consume(_COMMA)
  1325. else
  1326. break;
  1327. until false;
  1328. consume(_RECKKLAMMER);
  1329. end
  1330. else
  1331. break;
  1332. end;
  1333. end;
  1334. end;
  1335. var
  1336. store_static : boolean;
  1337. protsym : tpropertysym;
  1338. p2,p3 : tnode;
  1339. hsym : tsym;
  1340. classh : tobjectdef;
  1341. begin
  1342. again:=true;
  1343. while again do
  1344. begin
  1345. { we need the resulttype }
  1346. do_resulttypepass(p1);
  1347. if codegenerror then
  1348. begin
  1349. recoverconsume_postfixops;
  1350. exit;
  1351. end;
  1352. { handle token }
  1353. case token of
  1354. _CARET:
  1355. begin
  1356. consume(_CARET);
  1357. if (p1.resulttype.def.deftype<>pointerdef) then
  1358. begin
  1359. { ^ as binary operator is a problem!!!! (FK) }
  1360. again:=false;
  1361. Message(cg_e_invalid_qualifier);
  1362. recoverconsume_postfixops;
  1363. p1.destroy;
  1364. p1:=cerrornode.create;
  1365. end
  1366. else
  1367. begin
  1368. p1:=cderefnode.create(p1);
  1369. end;
  1370. end;
  1371. _LECKKLAMMER:
  1372. begin
  1373. if is_class_or_interface(p1.resulttype.def) then
  1374. begin
  1375. { default property }
  1376. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1377. if not(assigned(protsym)) then
  1378. begin
  1379. p1.destroy;
  1380. p1:=cerrornode.create;
  1381. again:=false;
  1382. message(parser_e_no_default_property_available);
  1383. end
  1384. else
  1385. handle_propertysym(protsym,protsym.owner,p1,getaddr);
  1386. end
  1387. else
  1388. begin
  1389. consume(_LECKKLAMMER);
  1390. repeat
  1391. case p1.resulttype.def.deftype of
  1392. pointerdef:
  1393. begin
  1394. { support delphi autoderef }
  1395. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1396. (m_autoderef in aktmodeswitches) then
  1397. begin
  1398. p1:=cderefnode.create(p1);
  1399. end;
  1400. p2:=comp_expr(true);
  1401. p1:=cvecnode.create(p1,p2);
  1402. end;
  1403. stringdef :
  1404. begin
  1405. p2:=comp_expr(true);
  1406. p1:=cvecnode.create(p1,p2);
  1407. end;
  1408. arraydef :
  1409. begin
  1410. p2:=comp_expr(true);
  1411. { support SEG:OFS for go32v2 Mem[] }
  1412. if (target_info.system=system_i386_go32v2) and
  1413. (p1.nodetype=loadn) and
  1414. assigned(tloadnode(p1).symtableentry) and
  1415. assigned(tloadnode(p1).symtableentry.owner.name) and
  1416. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1417. ((tloadnode(p1).symtableentry.name='MEM') or
  1418. (tloadnode(p1).symtableentry.name='MEMW') or
  1419. (tloadnode(p1).symtableentry.name='MEML')) then
  1420. begin
  1421. if (token=_COLON) then
  1422. begin
  1423. consume(_COLON);
  1424. p3:=caddnode.create(muln,cordconstnode.create($10,s32bittype,false),p2);
  1425. p2:=comp_expr(true);
  1426. p2:=caddnode.create(addn,p2,p3);
  1427. p1:=cvecnode.create(p1,p2);
  1428. include(tvecnode(p1).flags,nf_memseg);
  1429. include(tvecnode(p1).flags,nf_memindex);
  1430. end
  1431. else
  1432. begin
  1433. p1:=cvecnode.create(p1,p2);
  1434. include(tvecnode(p1).flags,nf_memindex);
  1435. end;
  1436. end
  1437. else
  1438. p1:=cvecnode.create(p1,p2);
  1439. end;
  1440. else
  1441. begin
  1442. Message(cg_e_invalid_qualifier);
  1443. p1.destroy;
  1444. p1:=cerrornode.create;
  1445. comp_expr(true);
  1446. again:=false;
  1447. end;
  1448. end;
  1449. do_resulttypepass(p1);
  1450. if token=_COMMA then
  1451. consume(_COMMA)
  1452. else
  1453. break;
  1454. until false;
  1455. consume(_RECKKLAMMER);
  1456. end;
  1457. end;
  1458. _POINT :
  1459. begin
  1460. consume(_POINT);
  1461. if (p1.resulttype.def.deftype=pointerdef) and
  1462. (m_autoderef in aktmodeswitches) then
  1463. begin
  1464. p1:=cderefnode.create(p1);
  1465. do_resulttypepass(p1);
  1466. end;
  1467. case p1.resulttype.def.deftype of
  1468. recorddef:
  1469. begin
  1470. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1471. check_hints(hsym);
  1472. if assigned(hsym) and
  1473. (hsym.typ=varsym) then
  1474. p1:=csubscriptnode.create(hsym,p1)
  1475. else
  1476. begin
  1477. Message1(sym_e_illegal_field,pattern);
  1478. p1.destroy;
  1479. p1:=cerrornode.create;
  1480. end;
  1481. consume(_ID);
  1482. end;
  1483. variantdef:
  1484. begin
  1485. end;
  1486. classrefdef:
  1487. begin
  1488. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1489. hsym:=searchsym_in_class(classh,pattern);
  1490. check_hints(hsym);
  1491. if hsym=nil then
  1492. begin
  1493. Message1(sym_e_id_no_member,pattern);
  1494. p1.destroy;
  1495. p1:=cerrornode.create;
  1496. { try to clean up }
  1497. consume(_ID);
  1498. end
  1499. else
  1500. begin
  1501. consume(_ID);
  1502. do_member_read(getaddr,hsym,p1,again);
  1503. end;
  1504. end;
  1505. objectdef:
  1506. begin
  1507. store_static:=allow_only_static;
  1508. allow_only_static:=false;
  1509. classh:=tobjectdef(p1.resulttype.def);
  1510. hsym:=searchsym_in_class(classh,pattern);
  1511. check_hints(hsym);
  1512. allow_only_static:=store_static;
  1513. if hsym=nil then
  1514. begin
  1515. Message1(sym_e_id_no_member,pattern);
  1516. p1.destroy;
  1517. p1:=cerrornode.create;
  1518. { try to clean up }
  1519. consume(_ID);
  1520. end
  1521. else
  1522. begin
  1523. consume(_ID);
  1524. do_member_read(getaddr,hsym,p1,again);
  1525. end;
  1526. end;
  1527. pointerdef:
  1528. begin
  1529. Message(cg_e_invalid_qualifier);
  1530. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1531. Message(parser_h_maybe_deref_caret_missing);
  1532. end;
  1533. else
  1534. begin
  1535. Message(cg_e_invalid_qualifier);
  1536. p1.destroy;
  1537. p1:=cerrornode.create;
  1538. consume(_ID);
  1539. end;
  1540. end;
  1541. end;
  1542. else
  1543. begin
  1544. { is this a procedure variable ? }
  1545. if assigned(p1.resulttype.def) then
  1546. begin
  1547. if (p1.resulttype.def.deftype=procvardef) then
  1548. begin
  1549. if assigned(getprocvardef) and
  1550. equal_defs(p1.resulttype.def,getprocvardef) then
  1551. again:=false
  1552. else
  1553. if (token=_LKLAMMER) or
  1554. ((tprocvardef(p1.resulttype.def).para.empty) and
  1555. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1556. (not afterassignment) and
  1557. (not in_args)) then
  1558. begin
  1559. { do this in a strange way }
  1560. { it's not a clean solution }
  1561. p2:=p1;
  1562. p1:=ccallnode.create(nil,nil,nil,nil);
  1563. tcallnode(p1).set_procvar(p2);
  1564. if try_to_consume(_LKLAMMER) then
  1565. begin
  1566. tcallnode(p1).left:=parse_paras(false,false);
  1567. consume(_RKLAMMER);
  1568. end;
  1569. { proc():= is never possible }
  1570. if token=_ASSIGNMENT then
  1571. begin
  1572. Message(cg_e_illegal_expression);
  1573. p1:=cerrornode.create;
  1574. again:=false;
  1575. end;
  1576. end
  1577. else
  1578. again:=false;
  1579. end
  1580. else
  1581. again:=false;
  1582. end
  1583. else
  1584. again:=false;
  1585. end;
  1586. end;
  1587. end; { while again }
  1588. end;
  1589. {---------------------------------------------
  1590. Factor (Main)
  1591. ---------------------------------------------}
  1592. var
  1593. l : longint;
  1594. card : cardinal;
  1595. ic : TConstExprInt;
  1596. oldp1,
  1597. p1 : tnode;
  1598. code : integer;
  1599. again : boolean;
  1600. sym : tsym;
  1601. classh : tobjectdef;
  1602. d : bestreal;
  1603. hs : string;
  1604. htype : ttype;
  1605. filepos : tfileposinfo;
  1606. {---------------------------------------------
  1607. Helpers
  1608. ---------------------------------------------}
  1609. procedure check_tokenpos;
  1610. begin
  1611. if (p1<>oldp1) then
  1612. begin
  1613. if assigned(p1) then
  1614. p1.set_tree_filepos(filepos);
  1615. oldp1:=p1;
  1616. filepos:=akttokenpos;
  1617. end;
  1618. end;
  1619. begin
  1620. oldp1:=nil;
  1621. p1:=nil;
  1622. filepos:=akttokenpos;
  1623. again:=false;
  1624. if token=_ID then
  1625. begin
  1626. factor_read_id(p1,again);
  1627. if again then
  1628. begin
  1629. check_tokenpos;
  1630. { handle post fix operators }
  1631. postfixoperators(p1,again);
  1632. end;
  1633. end
  1634. else
  1635. case token of
  1636. _SELF :
  1637. begin
  1638. again:=true;
  1639. consume(_SELF);
  1640. if not assigned(procinfo._class) then
  1641. begin
  1642. p1:=cerrornode.create;
  1643. again:=false;
  1644. Message(parser_e_self_not_in_method);
  1645. end
  1646. else
  1647. begin
  1648. if (po_classmethod in aktprocdef.procoptions) then
  1649. begin
  1650. { self in class methods is a class reference type }
  1651. htype.setdef(procinfo._class);
  1652. p1:=cselfnode.create(tclassrefdef.create(htype));
  1653. end
  1654. else
  1655. p1:=cselfnode.create(procinfo._class);
  1656. postfixoperators(p1,again);
  1657. end;
  1658. end;
  1659. _INHERITED :
  1660. begin
  1661. again:=true;
  1662. consume(_INHERITED);
  1663. if assigned(procinfo._class) then
  1664. begin
  1665. classh:=procinfo._class.childof;
  1666. { if inherited; only then we need the method with
  1667. the same name }
  1668. if token in endtokens then
  1669. begin
  1670. hs:=aktprocsym.name;
  1671. anon_inherited:=true;
  1672. { For message methods we need to search using the message
  1673. number or string }
  1674. if (po_msgint in aktprocsym.first_procdef.procoptions) then
  1675. sym:=searchsym_in_class_by_msgint(classh,aktprocsym.first_procdef.messageinf.i)
  1676. else
  1677. if (po_msgstr in aktprocsym.first_procdef.procoptions) then
  1678. sym:=searchsym_in_class_by_msgstr(classh,aktprocsym.first_procdef.messageinf.str)
  1679. else
  1680. sym:=searchsym_in_class(classh,hs);
  1681. end
  1682. else
  1683. begin
  1684. hs:=pattern;
  1685. consume(_ID);
  1686. anon_inherited:=false;
  1687. sym:=searchsym_in_class(classh,hs);
  1688. end;
  1689. if assigned(sym) then
  1690. begin
  1691. check_hints(sym);
  1692. if sym.typ=procsym then
  1693. begin
  1694. htype.setdef(classh);
  1695. p1:=ctypenode.create(htype);
  1696. end;
  1697. do_member_read(false,sym,p1,again);
  1698. { Add flag to indicate that inherited is used }
  1699. if p1.nodetype=calln then
  1700. include(p1.flags,nf_anon_inherited);
  1701. end
  1702. else
  1703. begin
  1704. if anon_inherited then
  1705. begin
  1706. { we didn't find a member in the parents call the
  1707. DefaultHandler }
  1708. sym:=searchsym_in_class(classh,'DEFAULTHANDLER');
  1709. if not assigned(sym) or
  1710. (sym.typ<>procsym) then
  1711. internalerror(200303171);
  1712. p1:=nil;
  1713. do_proc_call(sym,sym.owner,false,again,p1);
  1714. end
  1715. else
  1716. begin
  1717. Message1(sym_e_id_no_member,hs);
  1718. p1:=cerrornode.create;
  1719. end;
  1720. again:=false;
  1721. end;
  1722. { turn auto inheriting off }
  1723. anon_inherited:=false;
  1724. end
  1725. else
  1726. begin
  1727. Message(parser_e_generic_methods_only_in_methods);
  1728. again:=false;
  1729. p1:=cerrornode.create;
  1730. end;
  1731. postfixoperators(p1,again);
  1732. end;
  1733. _INTCONST :
  1734. begin
  1735. { try cardinal first }
  1736. val(pattern,card,code);
  1737. if code<>0 then
  1738. begin
  1739. { then longint }
  1740. valint(pattern,l,code);
  1741. if code <> 0 then
  1742. begin
  1743. { then int64 }
  1744. val(pattern,ic,code);
  1745. if code<>0 then
  1746. begin
  1747. {finally float }
  1748. val(pattern,d,code);
  1749. if code<>0 then
  1750. begin
  1751. Message(cg_e_invalid_integer);
  1752. consume(_INTCONST);
  1753. l:=1;
  1754. p1:=cordconstnode.create(l,s32bittype,true);
  1755. end
  1756. else
  1757. begin
  1758. consume(_INTCONST);
  1759. p1:=crealconstnode.create(d,pbestrealtype^);
  1760. end;
  1761. end
  1762. else
  1763. begin
  1764. consume(_INTCONST);
  1765. p1:=cordconstnode.create(ic,cs64bittype,true);
  1766. end
  1767. end
  1768. else
  1769. begin
  1770. consume(_INTCONST);
  1771. p1:=cordconstnode.create(l,s32bittype,true)
  1772. end
  1773. end
  1774. else
  1775. begin
  1776. consume(_INTCONST);
  1777. { check whether the value isn't in the longint range as well }
  1778. { (longint is easier to perform calculations with) (JM) }
  1779. if card <= $7fffffff then
  1780. { no sign extension necessary, so not longint typecast (JM) }
  1781. p1:=cordconstnode.create(card,s32bittype,true)
  1782. else
  1783. p1:=cordconstnode.create(card,u32bittype,true)
  1784. end;
  1785. end;
  1786. _REALNUMBER :
  1787. begin
  1788. val(pattern,d,code);
  1789. if code<>0 then
  1790. begin
  1791. Message(parser_e_error_in_real);
  1792. d:=1.0;
  1793. end;
  1794. consume(_REALNUMBER);
  1795. p1:=crealconstnode.create(d,pbestrealtype^);
  1796. end;
  1797. _STRING :
  1798. begin
  1799. string_dec(htype);
  1800. { STRING can be also a type cast }
  1801. if token=_LKLAMMER then
  1802. begin
  1803. consume(_LKLAMMER);
  1804. p1:=comp_expr(true);
  1805. consume(_RKLAMMER);
  1806. p1:=ctypeconvnode.create(p1,htype);
  1807. include(p1.flags,nf_explizit);
  1808. { handle postfix operators here e.g. string(a)[10] }
  1809. again:=true;
  1810. postfixoperators(p1,again);
  1811. end
  1812. else
  1813. p1:=ctypenode.create(htype);
  1814. end;
  1815. _FILE :
  1816. begin
  1817. htype:=cfiletype;
  1818. consume(_FILE);
  1819. { FILE can be also a type cast }
  1820. if token=_LKLAMMER then
  1821. begin
  1822. consume(_LKLAMMER);
  1823. p1:=comp_expr(true);
  1824. consume(_RKLAMMER);
  1825. p1:=ctypeconvnode.create(p1,htype);
  1826. include(p1.flags,nf_explizit);
  1827. { handle postfix operators here e.g. string(a)[10] }
  1828. again:=true;
  1829. postfixoperators(p1,again);
  1830. end
  1831. else
  1832. begin
  1833. p1:=ctypenode.create(htype);
  1834. end;
  1835. end;
  1836. _CSTRING :
  1837. begin
  1838. p1:=cstringconstnode.createstr(pattern,st_default);
  1839. consume(_CSTRING);
  1840. end;
  1841. _CCHAR :
  1842. begin
  1843. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  1844. consume(_CCHAR);
  1845. end;
  1846. _CWSTRING:
  1847. begin
  1848. p1:=cstringconstnode.createwstr(patternw);
  1849. consume(_CWSTRING);
  1850. end;
  1851. _CWCHAR:
  1852. begin
  1853. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  1854. consume(_CWCHAR);
  1855. end;
  1856. _KLAMMERAFFE :
  1857. begin
  1858. consume(_KLAMMERAFFE);
  1859. got_addrn:=true;
  1860. { support both @<x> and @(<x>) }
  1861. if try_to_consume(_LKLAMMER) then
  1862. begin
  1863. p1:=factor(true);
  1864. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1865. begin
  1866. again:=true;
  1867. postfixoperators(p1,again);
  1868. end;
  1869. consume(_RKLAMMER);
  1870. end
  1871. else
  1872. p1:=factor(true);
  1873. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1874. begin
  1875. again:=true;
  1876. postfixoperators(p1,again);
  1877. end;
  1878. got_addrn:=false;
  1879. p1:=caddrnode.create(p1);
  1880. if assigned(getprocvardef) and
  1881. (taddrnode(p1).left.nodetype = loadn) and
  1882. { make sure we found a valid procedure, otherwise the }
  1883. { "getprocvardef" will become the default in taddrnode }
  1884. { while there should be an error (JM) }
  1885. assigned(tloadnode(taddrnode(p1).left).procdef) then
  1886. taddrnode(p1).getprocvardef:=getprocvardef;
  1887. end;
  1888. _LKLAMMER :
  1889. begin
  1890. consume(_LKLAMMER);
  1891. p1:=comp_expr(true);
  1892. consume(_RKLAMMER);
  1893. { it's not a good solution }
  1894. { but (a+b)^ makes some problems }
  1895. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1896. begin
  1897. again:=true;
  1898. postfixoperators(p1,again);
  1899. end;
  1900. end;
  1901. _LECKKLAMMER :
  1902. begin
  1903. consume(_LECKKLAMMER);
  1904. p1:=factor_read_set;
  1905. consume(_RECKKLAMMER);
  1906. end;
  1907. _PLUS :
  1908. begin
  1909. consume(_PLUS);
  1910. p1:=factor(false);
  1911. end;
  1912. _MINUS :
  1913. begin
  1914. consume(_MINUS);
  1915. p1:=sub_expr(oppower,false);
  1916. p1:=cunaryminusnode.create(p1);
  1917. end;
  1918. _OP_NOT :
  1919. begin
  1920. consume(_OP_NOT);
  1921. p1:=factor(false);
  1922. p1:=cnotnode.create(p1);
  1923. end;
  1924. _TRUE :
  1925. begin
  1926. consume(_TRUE);
  1927. p1:=cordconstnode.create(1,booltype,false);
  1928. end;
  1929. _FALSE :
  1930. begin
  1931. consume(_FALSE);
  1932. p1:=cordconstnode.create(0,booltype,false);
  1933. end;
  1934. _NIL :
  1935. begin
  1936. consume(_NIL);
  1937. p1:=cnilnode.create;
  1938. { It's really ugly code nil^, but delphi allows it }
  1939. if token in [_CARET] then
  1940. begin
  1941. again:=true;
  1942. postfixoperators(p1,again);
  1943. end;
  1944. end;
  1945. else
  1946. begin
  1947. p1:=cerrornode.create;
  1948. consume(token);
  1949. Message(cg_e_illegal_expression);
  1950. end;
  1951. end;
  1952. { generate error node if no node is created }
  1953. if not assigned(p1) then
  1954. begin
  1955. {$ifdef EXTDEBUG}
  1956. Comment(V_Warning,'factor: p1=nil');
  1957. {$endif}
  1958. p1:=cerrornode.create;
  1959. end;
  1960. { get the resulttype for the node }
  1961. if (not assigned(p1.resulttype.def)) then
  1962. do_resulttypepass(p1);
  1963. { tp7 procvar handling, but not if the next token
  1964. will be a := }
  1965. check_tp_procvar(p1);
  1966. factor:=p1;
  1967. check_tokenpos;
  1968. end;
  1969. {$ifdef fpc}
  1970. {$maxfpuregisters default}
  1971. {$endif fpc}
  1972. {****************************************************************************
  1973. Sub_Expr
  1974. ****************************************************************************}
  1975. const
  1976. { Warning these stay be ordered !! }
  1977. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1978. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  1979. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  1980. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  1981. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  1982. [_STARSTAR] );
  1983. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  1984. {Reads a subexpression while the operators are of the current precedence
  1985. level, or any higher level. Replaces the old term, simpl_expr and
  1986. simpl2_expr.}
  1987. var
  1988. p1,p2 : tnode;
  1989. oldt : Ttoken;
  1990. filepos : tfileposinfo;
  1991. begin
  1992. if pred_level=highest_precedence then
  1993. p1:=factor(false)
  1994. else
  1995. p1:=sub_expr(succ(pred_level),true);
  1996. repeat
  1997. if (token in operator_levels[pred_level]) and
  1998. ((token<>_EQUAL) or accept_equal) then
  1999. begin
  2000. oldt:=token;
  2001. filepos:=akttokenpos;
  2002. consume(token);
  2003. if pred_level=highest_precedence then
  2004. p2:=factor(false)
  2005. else
  2006. p2:=sub_expr(succ(pred_level),true);
  2007. case oldt of
  2008. _PLUS :
  2009. p1:=caddnode.create(addn,p1,p2);
  2010. _MINUS :
  2011. p1:=caddnode.create(subn,p1,p2);
  2012. _STAR :
  2013. p1:=caddnode.create(muln,p1,p2);
  2014. _SLASH :
  2015. p1:=caddnode.create(slashn,p1,p2);
  2016. _EQUAL :
  2017. p1:=caddnode.create(equaln,p1,p2);
  2018. _GT :
  2019. p1:=caddnode.create(gtn,p1,p2);
  2020. _LT :
  2021. p1:=caddnode.create(ltn,p1,p2);
  2022. _GTE :
  2023. p1:=caddnode.create(gten,p1,p2);
  2024. _LTE :
  2025. p1:=caddnode.create(lten,p1,p2);
  2026. _SYMDIF :
  2027. p1:=caddnode.create(symdifn,p1,p2);
  2028. _STARSTAR :
  2029. p1:=caddnode.create(starstarn,p1,p2);
  2030. _OP_AS :
  2031. p1:=casnode.create(p1,p2);
  2032. _OP_IN :
  2033. p1:=cinnode.create(p1,p2);
  2034. _OP_IS :
  2035. p1:=cisnode.create(p1,p2);
  2036. _OP_OR :
  2037. p1:=caddnode.create(orn,p1,p2);
  2038. _OP_AND :
  2039. p1:=caddnode.create(andn,p1,p2);
  2040. _OP_DIV :
  2041. p1:=cmoddivnode.create(divn,p1,p2);
  2042. _OP_NOT :
  2043. p1:=cnotnode.create(p1);
  2044. _OP_MOD :
  2045. p1:=cmoddivnode.create(modn,p1,p2);
  2046. _OP_SHL :
  2047. p1:=cshlshrnode.create(shln,p1,p2);
  2048. _OP_SHR :
  2049. p1:=cshlshrnode.create(shrn,p1,p2);
  2050. _OP_XOR :
  2051. p1:=caddnode.create(xorn,p1,p2);
  2052. _ASSIGNMENT :
  2053. p1:=cassignmentnode.create(p1,p2);
  2054. _CARET :
  2055. p1:=caddnode.create(caretn,p1,p2);
  2056. _UNEQUAL :
  2057. p1:=caddnode.create(unequaln,p1,p2);
  2058. end;
  2059. p1.set_tree_filepos(filepos);
  2060. end
  2061. else
  2062. break;
  2063. until false;
  2064. sub_expr:=p1;
  2065. end;
  2066. function comp_expr(accept_equal : boolean):tnode;
  2067. var
  2068. oldafterassignment : boolean;
  2069. p1 : tnode;
  2070. begin
  2071. oldafterassignment:=afterassignment;
  2072. afterassignment:=true;
  2073. p1:=sub_expr(opcompare,accept_equal);
  2074. { get the resulttype for this expression }
  2075. if not assigned(p1.resulttype.def) then
  2076. do_resulttypepass(p1);
  2077. afterassignment:=oldafterassignment;
  2078. comp_expr:=p1;
  2079. end;
  2080. function expr : tnode;
  2081. var
  2082. p1,p2 : tnode;
  2083. oldafterassignment : boolean;
  2084. oldp1 : tnode;
  2085. filepos : tfileposinfo;
  2086. begin
  2087. oldafterassignment:=afterassignment;
  2088. p1:=sub_expr(opcompare,true);
  2089. { get the resulttype for this expression }
  2090. if not assigned(p1.resulttype.def) then
  2091. do_resulttypepass(p1);
  2092. filepos:=akttokenpos;
  2093. check_tp_procvar(p1);
  2094. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2095. afterassignment:=true;
  2096. oldp1:=p1;
  2097. case token of
  2098. _POINTPOINT :
  2099. begin
  2100. consume(_POINTPOINT);
  2101. p2:=sub_expr(opcompare,true);
  2102. p1:=crangenode.create(p1,p2);
  2103. end;
  2104. _ASSIGNMENT :
  2105. begin
  2106. consume(_ASSIGNMENT);
  2107. if (p1.resulttype.def.deftype=procvardef) then
  2108. getprocvardef:=tprocvardef(p1.resulttype.def);
  2109. p2:=sub_expr(opcompare,true);
  2110. if assigned(getprocvardef) then
  2111. handle_procvar(getprocvardef,p2);
  2112. getprocvardef:=nil;
  2113. p1:=cassignmentnode.create(p1,p2);
  2114. end;
  2115. _PLUSASN :
  2116. begin
  2117. consume(_PLUSASN);
  2118. p2:=sub_expr(opcompare,true);
  2119. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2120. end;
  2121. _MINUSASN :
  2122. begin
  2123. consume(_MINUSASN);
  2124. p2:=sub_expr(opcompare,true);
  2125. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2126. end;
  2127. _STARASN :
  2128. begin
  2129. consume(_STARASN );
  2130. p2:=sub_expr(opcompare,true);
  2131. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2132. end;
  2133. _SLASHASN :
  2134. begin
  2135. consume(_SLASHASN );
  2136. p2:=sub_expr(opcompare,true);
  2137. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2138. end;
  2139. end;
  2140. { get the resulttype for this expression }
  2141. if not assigned(p1.resulttype.def) then
  2142. do_resulttypepass(p1);
  2143. afterassignment:=oldafterassignment;
  2144. if p1<>oldp1 then
  2145. p1.set_tree_filepos(filepos);
  2146. expr:=p1;
  2147. end;
  2148. {$ifdef int64funcresok}
  2149. function get_intconst:TConstExprInt;
  2150. {$else int64funcresok}
  2151. function get_intconst:longint;
  2152. {$endif int64funcresok}
  2153. {Reads an expression, tries to evalute it and check if it is an integer
  2154. constant. Then the constant is returned.}
  2155. var
  2156. p:tnode;
  2157. begin
  2158. p:=comp_expr(true);
  2159. if not codegenerror then
  2160. begin
  2161. if (p.nodetype<>ordconstn) or
  2162. not(is_integer(p.resulttype.def)) then
  2163. Message(cg_e_illegal_expression)
  2164. else
  2165. get_intconst:=tordconstnode(p).value;
  2166. end;
  2167. p.free;
  2168. end;
  2169. function get_stringconst:string;
  2170. {Reads an expression, tries to evaluate it and checks if it is a string
  2171. constant. Then the constant is returned.}
  2172. var
  2173. p:tnode;
  2174. begin
  2175. get_stringconst:='';
  2176. p:=comp_expr(true);
  2177. if p.nodetype<>stringconstn then
  2178. begin
  2179. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2180. get_stringconst:=char(tordconstnode(p).value)
  2181. else
  2182. Message(cg_e_illegal_expression);
  2183. end
  2184. else
  2185. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2186. p.free;
  2187. end;
  2188. end.
  2189. {
  2190. $Log$
  2191. Revision 1.105 2003-03-27 17:44:13 peter
  2192. * fixed small mem leaks
  2193. Revision 1.104 2003/03/17 18:55:30 peter
  2194. * allow more tokens instead of only semicolon after inherited
  2195. Revision 1.103 2003/03/17 16:54:41 peter
  2196. * support DefaultHandler and anonymous inheritance fixed
  2197. for message methods
  2198. Revision 1.102 2003/01/30 21:46:57 peter
  2199. * self fixes for static methods (merged)
  2200. Revision 1.101 2003/01/16 22:12:22 peter
  2201. * Find the correct procvar to load when using @ in fpc mode
  2202. Revision 1.100 2003/01/15 01:44:32 peter
  2203. * merged methodpointer fixes from 1.0.x
  2204. Revision 1.98 2003/01/12 17:51:42 peter
  2205. * tp procvar handling fix for tb0448
  2206. Revision 1.97 2003/01/05 22:44:14 peter
  2207. * remove a lot of code to support typen in loadn-procsym
  2208. Revision 1.96 2002/12/11 22:40:36 peter
  2209. * assigned(procvar) fix for delphi mode, fixes tb0430
  2210. Revision 1.95 2002/11/30 11:12:48 carl
  2211. + checking for symbols used with hint directives is done mostly in pexpr
  2212. only now
  2213. Revision 1.94 2002/11/27 15:33:47 peter
  2214. * the never ending story of tp procvar hacks
  2215. Revision 1.93 2002/11/26 22:58:24 peter
  2216. * fix for tw2178. When a ^ or . follows a procsym then the procsym
  2217. needs to be called
  2218. Revision 1.92 2002/11/25 17:43:22 peter
  2219. * splitted defbase in defutil,symutil,defcmp
  2220. * merged isconvertable and is_equal into compare_defs(_ext)
  2221. * made operator search faster by walking the list only once
  2222. Revision 1.91 2002/11/22 22:48:10 carl
  2223. * memory optimization with tconstsym (1.5%)
  2224. Revision 1.90 2002/11/20 22:49:55 pierre
  2225. * commented check code tht was invalid in 1.1
  2226. Revision 1.89 2002/11/18 18:34:41 peter
  2227. * fix crash with EXTDEBUG code
  2228. Revision 1.88 2002/11/18 17:48:21 peter
  2229. * fix tw2209 (merged)
  2230. Revision 1.87 2002/11/18 17:31:58 peter
  2231. * pass proccalloption to ret_in_xxx and push_xxx functions
  2232. Revision 1.86 2002/10/05 00:48:57 peter
  2233. * support inherited; support for overload as it is handled by
  2234. delphi. This is only for delphi mode as it is working is
  2235. undocumented and hard to predict what is done
  2236. Revision 1.85 2002/10/04 21:13:59 peter
  2237. * ignore vecn,subscriptn when checking for a procvar loadn
  2238. Revision 1.84 2002/10/02 20:51:22 peter
  2239. * don't check interfaces for class methods
  2240. Revision 1.83 2002/10/02 18:20:52 peter
  2241. * Copy() is now internal syssym that calls compilerprocs
  2242. Revision 1.82 2002/09/30 07:00:48 florian
  2243. * fixes to common code to get the alpha compiler compiled applied
  2244. Revision 1.81 2002/09/16 19:06:14 peter
  2245. * allow ^ after nil
  2246. Revision 1.80 2002/09/07 15:25:07 peter
  2247. * old logs removed and tabs fixed
  2248. Revision 1.79 2002/09/07 12:16:03 carl
  2249. * second part bug report 1996 fix, testrange in cordconstnode
  2250. only called if option is set (also make parsing a tiny faster)
  2251. Revision 1.78 2002/09/03 16:26:27 daniel
  2252. * Make Tprocdef.defs protected
  2253. Revision 1.77 2002/08/18 20:06:24 peter
  2254. * inlining is now also allowed in interface
  2255. * renamed write/load to ppuwrite/ppuload
  2256. * tnode storing in ppu
  2257. * nld,ncon,nbas are already updated for storing in ppu
  2258. Revision 1.76 2002/08/17 09:23:39 florian
  2259. * first part of procinfo rewrite
  2260. Revision 1.75 2002/08/01 16:37:47 jonas
  2261. - removed some superfluous "in_paras := true" statements
  2262. Revision 1.74 2002/07/26 21:15:41 florian
  2263. * rewrote the system handling
  2264. Revision 1.73 2002/07/23 09:51:23 daniel
  2265. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2266. are worth comitting.
  2267. Revision 1.72 2002/07/20 11:57:55 florian
  2268. * types.pas renamed to defbase.pas because D6 contains a types
  2269. unit so this would conflicts if D6 programms are compiled
  2270. + Willamette/SSE2 instructions to assembler added
  2271. Revision 1.71 2002/07/16 15:34:20 florian
  2272. * exit is now a syssym instead of a keyword
  2273. Revision 1.70 2002/07/06 20:18:02 carl
  2274. * longstring declaration now gives parser error since its not supported!
  2275. Revision 1.69 2002/06/12 15:46:14 jonas
  2276. * fixed web bug 1995
  2277. Revision 1.68 2002/05/18 13:34:12 peter
  2278. * readded missing revisions
  2279. Revision 1.67 2002/05/16 19:46:43 carl
  2280. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2281. + try to fix temp allocation (still in ifdef)
  2282. + generic constructor calls
  2283. + start of tassembler / tmodulebase class cleanup
  2284. Revision 1.65 2002/05/12 16:53:09 peter
  2285. * moved entry and exitcode to ncgutil and cgobj
  2286. * foreach gets extra argument for passing local data to the
  2287. iterator function
  2288. * -CR checks also class typecasts at runtime by changing them
  2289. into as
  2290. * fixed compiler to cycle with the -CR option
  2291. * fixed stabs with elf writer, finally the global variables can
  2292. be watched
  2293. * removed a lot of routines from cga unit and replaced them by
  2294. calls to cgobj
  2295. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2296. u32bit then the other is typecasted also to u32bit without giving
  2297. a rangecheck warning/error.
  2298. * fixed pascal calling method with reversing also the high tree in
  2299. the parast, detected by tcalcst3 test
  2300. Revision 1.64 2002/04/23 19:16:34 peter
  2301. * add pinline unit that inserts compiler supported functions using
  2302. one or more statements
  2303. * moved finalize and setlength from ninl to pinline
  2304. Revision 1.63 2002/04/21 19:02:05 peter
  2305. * removed newn and disposen nodes, the code is now directly
  2306. inlined from pexpr
  2307. * -an option that will write the secondpass nodes to the .s file, this
  2308. requires EXTDEBUG define to actually write the info
  2309. * fixed various internal errors and crashes due recent code changes
  2310. Revision 1.62 2002/04/16 16:11:17 peter
  2311. * using inherited; without a parent having the same function
  2312. will do nothing like delphi
  2313. Revision 1.61 2002/04/07 13:31:36 carl
  2314. + change unit use
  2315. Revision 1.60 2002/04/01 20:57:13 jonas
  2316. * fixed web bug 1907
  2317. * fixed some other procvar related bugs (all related to accepting procvar
  2318. constructs with either too many or too little parameters)
  2319. (both merged, includes second typo fix of pexpr.pas)
  2320. Revision 1.59 2002/03/31 20:26:35 jonas
  2321. + a_loadfpu_* and a_loadmm_* methods in tcg
  2322. * register allocation is now handled by a class and is mostly processor
  2323. independent (+rgobj.pas and i386/rgcpu.pas)
  2324. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2325. * some small improvements and fixes to the optimizer
  2326. * some register allocation fixes
  2327. * some fpuvaroffset fixes in the unary minus node
  2328. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2329. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2330. also better optimizable)
  2331. * fixed and optimized register saving/restoring for new/dispose nodes
  2332. * LOC_FPU locations now also require their "register" field to be set to
  2333. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2334. - list field removed of the tnode class because it's not used currently
  2335. and can cause hard-to-find bugs
  2336. }