pexpr.pas 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  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, we check here the
  1020. srsymtable, because a field in another object
  1021. also has objectsymtable. And withsymtable is
  1022. not possible for self in class methods (PFV) }
  1023. if (srsymtable.symtabletype=objectsymtable) and
  1024. assigned(aktprocsym) and
  1025. (po_classmethod in aktprocdef.procoptions) then
  1026. Message(parser_e_only_class_methods);
  1027. if (sp_static in srsym.symoptions) then
  1028. begin
  1029. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  1030. searchsym(static_name,srsym,srsymtable);
  1031. check_hints(srsym);
  1032. end;
  1033. p1:=cloadnode.create(srsym,srsymtable);
  1034. if tvarsym(srsym).varstate=vs_declared then
  1035. begin
  1036. include(p1.flags,nf_first);
  1037. { set special between first loaded until checked in resulttypepass }
  1038. tvarsym(srsym).varstate:=vs_declared_and_first_found;
  1039. end;
  1040. end;
  1041. typedconstsym :
  1042. begin
  1043. p1:=cloadnode.create(srsym,srsymtable);
  1044. end;
  1045. syssym :
  1046. begin
  1047. p1:=statement_syssym(tsyssym(srsym).number);
  1048. end;
  1049. typesym :
  1050. begin
  1051. htype.setsym(srsym);
  1052. if not assigned(htype.def) then
  1053. begin
  1054. again:=false;
  1055. end
  1056. else
  1057. begin
  1058. if token=_LKLAMMER then
  1059. begin
  1060. consume(_LKLAMMER);
  1061. p1:=comp_expr(true);
  1062. consume(_RKLAMMER);
  1063. p1:=ctypeconvnode.create(p1,htype);
  1064. include(p1.flags,nf_explizit);
  1065. end
  1066. else { not LKLAMMER }
  1067. if (token=_POINT) and
  1068. is_object(htype.def) then
  1069. begin
  1070. consume(_POINT);
  1071. if assigned(procinfo) and
  1072. assigned(procinfo._class) and
  1073. not(getaddr) then
  1074. begin
  1075. if procinfo._class.is_related(tobjectdef(htype.def)) then
  1076. begin
  1077. p1:=ctypenode.create(htype);
  1078. { search also in inherited methods }
  1079. srsym:=searchsym_in_class(tobjectdef(htype.def),pattern);
  1080. check_hints(srsym);
  1081. consume(_ID);
  1082. do_member_read(false,srsym,p1,again);
  1083. end
  1084. else
  1085. begin
  1086. Message(parser_e_no_super_class);
  1087. again:=false;
  1088. end;
  1089. end
  1090. else
  1091. begin
  1092. { allows @TObject.Load }
  1093. { also allows static methods and variables }
  1094. p1:=ctypenode.create(htype);
  1095. { TP allows also @TMenu.Load if Load is only }
  1096. { defined in an anchestor class }
  1097. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1098. check_hints(srsym);
  1099. if not assigned(srsym) then
  1100. Message1(sym_e_id_no_member,pattern)
  1101. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1102. Message(sym_e_only_static_in_static)
  1103. else
  1104. begin
  1105. consume(_ID);
  1106. do_member_read(getaddr,srsym,p1,again);
  1107. end;
  1108. end;
  1109. end
  1110. else
  1111. begin
  1112. { class reference ? }
  1113. if is_class(htype.def) then
  1114. begin
  1115. if getaddr and (token=_POINT) then
  1116. begin
  1117. consume(_POINT);
  1118. { allows @Object.Method }
  1119. { also allows static methods and variables }
  1120. p1:=ctypenode.create(htype);
  1121. { TP allows also @TMenu.Load if Load is only }
  1122. { defined in an anchestor class }
  1123. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1124. check_hints(srsym);
  1125. if not assigned(srsym) then
  1126. Message1(sym_e_id_no_member,pattern)
  1127. else
  1128. begin
  1129. consume(_ID);
  1130. do_member_read(getaddr,srsym,p1,again);
  1131. end;
  1132. end
  1133. else
  1134. begin
  1135. p1:=ctypenode.create(htype);
  1136. { For a type block we simply return only
  1137. the type. For all other blocks we return
  1138. a loadvmt node }
  1139. if (block_type<>bt_type) then
  1140. p1:=cloadvmtnode.create(p1);
  1141. end;
  1142. end
  1143. else
  1144. p1:=ctypenode.create(htype);
  1145. end;
  1146. end;
  1147. end;
  1148. enumsym :
  1149. begin
  1150. p1:=genenumnode(tenumsym(srsym));
  1151. end;
  1152. constsym :
  1153. begin
  1154. case tconstsym(srsym).consttyp of
  1155. constint :
  1156. begin
  1157. { do a very dirty trick to bootstrap this code }
  1158. if (tconstsym(srsym).value.valueord>=-(int64(2147483647)+int64(1))) and
  1159. (tconstsym(srsym).value.valueord<=2147483647) then
  1160. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,s32bittype,true)
  1161. else if (tconstsym(srsym).value.valueord > maxlongint) and
  1162. (tconstsym(srsym).value.valueord <= int64(maxlongint)+int64(maxlongint)+1) then
  1163. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,u32bittype,true)
  1164. else
  1165. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cs64bittype,true);
  1166. end;
  1167. conststring :
  1168. begin
  1169. len:=tconstsym(srsym).value.len;
  1170. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1171. len:=255;
  1172. getmem(pc,len+1);
  1173. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1174. pc[len]:=#0;
  1175. p1:=cstringconstnode.createpchar(pc,len);
  1176. end;
  1177. constchar :
  1178. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,cchartype,true);
  1179. constreal :
  1180. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1181. constbool :
  1182. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,booltype,true);
  1183. constset :
  1184. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).consttype);
  1185. constord :
  1186. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).consttype,true);
  1187. constpointer :
  1188. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).consttype);
  1189. constnil :
  1190. p1:=cnilnode.create;
  1191. constresourcestring:
  1192. begin
  1193. p1:=cloadnode.create(srsym,srsymtable);
  1194. do_resulttypepass(p1);
  1195. p1.resulttype:=cansistringtype;
  1196. end;
  1197. constguid :
  1198. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1199. end;
  1200. end;
  1201. procsym :
  1202. begin
  1203. { are we in a class method ? }
  1204. possible_error:=(srsym.owner.symtabletype=objectsymtable) and
  1205. not(is_interface(tdef(srsym.owner.defowner))) and
  1206. assigned(aktprocsym) and
  1207. (po_classmethod in aktprocdef.procoptions);
  1208. do_proc_call(srsym,srsymtable,
  1209. (getaddr and not(token in [_CARET,_POINT])),
  1210. again,p1);
  1211. { we need to know which procedure is called }
  1212. if possible_error then
  1213. begin
  1214. do_resulttypepass(p1);
  1215. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1216. Message(parser_e_only_class_methods);
  1217. end;
  1218. end;
  1219. propertysym :
  1220. begin
  1221. { access to property in a method }
  1222. { are we in a class method ? }
  1223. if (srsym.owner.symtabletype=objectsymtable) and
  1224. assigned(aktprocsym) and
  1225. (po_classmethod in aktprocdef.procoptions) then
  1226. Message(parser_e_only_class_methods);
  1227. { no method pointer }
  1228. p1:=nil;
  1229. handle_propertysym(srsym,srsymtable,p1,getaddr);
  1230. end;
  1231. labelsym :
  1232. begin
  1233. consume(_COLON);
  1234. if tlabelsym(srsym).defined then
  1235. Message(sym_e_label_already_defined);
  1236. tlabelsym(srsym).defined:=true;
  1237. p1:=clabelnode.create(tlabelsym(srsym),nil);
  1238. end;
  1239. errorsym :
  1240. begin
  1241. p1:=cerrornode.create;
  1242. if token=_LKLAMMER then
  1243. begin
  1244. consume(_LKLAMMER);
  1245. parse_paras(false,false);
  1246. consume(_RKLAMMER);
  1247. end;
  1248. end;
  1249. else
  1250. begin
  1251. p1:=cerrornode.create;
  1252. Message(cg_e_illegal_expression);
  1253. end;
  1254. end; { end case }
  1255. end;
  1256. end;
  1257. {---------------------------------------------
  1258. Factor_Read_Set
  1259. ---------------------------------------------}
  1260. { Read a set between [] }
  1261. function factor_read_set:tnode;
  1262. var
  1263. p1,p2 : tnode;
  1264. lastp,
  1265. buildp : tarrayconstructornode;
  1266. begin
  1267. buildp:=nil;
  1268. { be sure that a least one arrayconstructn is used, also for an
  1269. empty [] }
  1270. if token=_RECKKLAMMER then
  1271. buildp:=carrayconstructornode.create(nil,buildp)
  1272. else
  1273. begin
  1274. while true do
  1275. begin
  1276. p1:=comp_expr(true);
  1277. if token=_POINTPOINT then
  1278. begin
  1279. consume(_POINTPOINT);
  1280. p2:=comp_expr(true);
  1281. p1:=carrayconstructorrangenode.create(p1,p2);
  1282. end;
  1283. { insert at the end of the tree, to get the correct order }
  1284. if not assigned(buildp) then
  1285. begin
  1286. buildp:=carrayconstructornode.create(p1,nil);
  1287. lastp:=buildp;
  1288. end
  1289. else
  1290. begin
  1291. lastp.right:=carrayconstructornode.create(p1,nil);
  1292. lastp:=tarrayconstructornode(lastp.right);
  1293. end;
  1294. { there could be more elements }
  1295. if token=_COMMA then
  1296. consume(_COMMA)
  1297. else
  1298. break;
  1299. end;
  1300. end;
  1301. factor_read_set:=buildp;
  1302. end;
  1303. {---------------------------------------------
  1304. PostFixOperators
  1305. ---------------------------------------------}
  1306. procedure postfixoperators(var p1:tnode;var again:boolean);
  1307. { tries to avoid syntax errors after invalid qualifiers }
  1308. procedure recoverconsume_postfixops;
  1309. begin
  1310. while true do
  1311. begin
  1312. case token of
  1313. _CARET:
  1314. consume(_CARET);
  1315. _POINT:
  1316. begin
  1317. consume(_POINT);
  1318. if token=_ID then
  1319. consume(_ID);
  1320. end;
  1321. _LECKKLAMMER:
  1322. begin
  1323. consume(_LECKKLAMMER);
  1324. repeat
  1325. comp_expr(true);
  1326. if token=_COMMA then
  1327. consume(_COMMA)
  1328. else
  1329. break;
  1330. until false;
  1331. consume(_RECKKLAMMER);
  1332. end
  1333. else
  1334. break;
  1335. end;
  1336. end;
  1337. end;
  1338. var
  1339. store_static : boolean;
  1340. protsym : tpropertysym;
  1341. p2,p3 : tnode;
  1342. hsym : tsym;
  1343. classh : tobjectdef;
  1344. begin
  1345. again:=true;
  1346. while again do
  1347. begin
  1348. { we need the resulttype }
  1349. do_resulttypepass(p1);
  1350. if codegenerror then
  1351. begin
  1352. recoverconsume_postfixops;
  1353. exit;
  1354. end;
  1355. { handle token }
  1356. case token of
  1357. _CARET:
  1358. begin
  1359. consume(_CARET);
  1360. if (p1.resulttype.def.deftype<>pointerdef) then
  1361. begin
  1362. { ^ as binary operator is a problem!!!! (FK) }
  1363. again:=false;
  1364. Message(cg_e_invalid_qualifier);
  1365. recoverconsume_postfixops;
  1366. p1.destroy;
  1367. p1:=cerrornode.create;
  1368. end
  1369. else
  1370. begin
  1371. p1:=cderefnode.create(p1);
  1372. end;
  1373. end;
  1374. _LECKKLAMMER:
  1375. begin
  1376. if is_class_or_interface(p1.resulttype.def) then
  1377. begin
  1378. { default property }
  1379. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1380. if not(assigned(protsym)) then
  1381. begin
  1382. p1.destroy;
  1383. p1:=cerrornode.create;
  1384. again:=false;
  1385. message(parser_e_no_default_property_available);
  1386. end
  1387. else
  1388. handle_propertysym(protsym,protsym.owner,p1,getaddr);
  1389. end
  1390. else
  1391. begin
  1392. consume(_LECKKLAMMER);
  1393. repeat
  1394. case p1.resulttype.def.deftype of
  1395. pointerdef:
  1396. begin
  1397. { support delphi autoderef }
  1398. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1399. (m_autoderef in aktmodeswitches) then
  1400. begin
  1401. p1:=cderefnode.create(p1);
  1402. end;
  1403. p2:=comp_expr(true);
  1404. p1:=cvecnode.create(p1,p2);
  1405. end;
  1406. stringdef :
  1407. begin
  1408. p2:=comp_expr(true);
  1409. p1:=cvecnode.create(p1,p2);
  1410. end;
  1411. arraydef :
  1412. begin
  1413. p2:=comp_expr(true);
  1414. { support SEG:OFS for go32v2 Mem[] }
  1415. if (target_info.system=system_i386_go32v2) and
  1416. (p1.nodetype=loadn) and
  1417. assigned(tloadnode(p1).symtableentry) and
  1418. assigned(tloadnode(p1).symtableentry.owner.name) and
  1419. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1420. ((tloadnode(p1).symtableentry.name='MEM') or
  1421. (tloadnode(p1).symtableentry.name='MEMW') or
  1422. (tloadnode(p1).symtableentry.name='MEML')) then
  1423. begin
  1424. if (token=_COLON) then
  1425. begin
  1426. consume(_COLON);
  1427. p3:=caddnode.create(muln,cordconstnode.create($10,s32bittype,false),p2);
  1428. p2:=comp_expr(true);
  1429. p2:=caddnode.create(addn,p2,p3);
  1430. p1:=cvecnode.create(p1,p2);
  1431. include(tvecnode(p1).flags,nf_memseg);
  1432. include(tvecnode(p1).flags,nf_memindex);
  1433. end
  1434. else
  1435. begin
  1436. p1:=cvecnode.create(p1,p2);
  1437. include(tvecnode(p1).flags,nf_memindex);
  1438. end;
  1439. end
  1440. else
  1441. p1:=cvecnode.create(p1,p2);
  1442. end;
  1443. else
  1444. begin
  1445. Message(cg_e_invalid_qualifier);
  1446. p1.destroy;
  1447. p1:=cerrornode.create;
  1448. comp_expr(true);
  1449. again:=false;
  1450. end;
  1451. end;
  1452. do_resulttypepass(p1);
  1453. if token=_COMMA then
  1454. consume(_COMMA)
  1455. else
  1456. break;
  1457. until false;
  1458. consume(_RECKKLAMMER);
  1459. end;
  1460. end;
  1461. _POINT :
  1462. begin
  1463. consume(_POINT);
  1464. if (p1.resulttype.def.deftype=pointerdef) and
  1465. (m_autoderef in aktmodeswitches) then
  1466. begin
  1467. p1:=cderefnode.create(p1);
  1468. do_resulttypepass(p1);
  1469. end;
  1470. case p1.resulttype.def.deftype of
  1471. recorddef:
  1472. begin
  1473. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1474. check_hints(hsym);
  1475. if assigned(hsym) and
  1476. (hsym.typ=varsym) then
  1477. p1:=csubscriptnode.create(hsym,p1)
  1478. else
  1479. begin
  1480. Message1(sym_e_illegal_field,pattern);
  1481. p1.destroy;
  1482. p1:=cerrornode.create;
  1483. end;
  1484. consume(_ID);
  1485. end;
  1486. variantdef:
  1487. begin
  1488. end;
  1489. classrefdef:
  1490. begin
  1491. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1492. hsym:=searchsym_in_class(classh,pattern);
  1493. check_hints(hsym);
  1494. if hsym=nil then
  1495. begin
  1496. Message1(sym_e_id_no_member,pattern);
  1497. p1.destroy;
  1498. p1:=cerrornode.create;
  1499. { try to clean up }
  1500. consume(_ID);
  1501. end
  1502. else
  1503. begin
  1504. consume(_ID);
  1505. do_member_read(getaddr,hsym,p1,again);
  1506. end;
  1507. end;
  1508. objectdef:
  1509. begin
  1510. store_static:=allow_only_static;
  1511. allow_only_static:=false;
  1512. classh:=tobjectdef(p1.resulttype.def);
  1513. hsym:=searchsym_in_class(classh,pattern);
  1514. check_hints(hsym);
  1515. allow_only_static:=store_static;
  1516. if hsym=nil then
  1517. begin
  1518. Message1(sym_e_id_no_member,pattern);
  1519. p1.destroy;
  1520. p1:=cerrornode.create;
  1521. { try to clean up }
  1522. consume(_ID);
  1523. end
  1524. else
  1525. begin
  1526. consume(_ID);
  1527. do_member_read(getaddr,hsym,p1,again);
  1528. end;
  1529. end;
  1530. pointerdef:
  1531. begin
  1532. Message(cg_e_invalid_qualifier);
  1533. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1534. Message(parser_h_maybe_deref_caret_missing);
  1535. end;
  1536. else
  1537. begin
  1538. Message(cg_e_invalid_qualifier);
  1539. p1.destroy;
  1540. p1:=cerrornode.create;
  1541. consume(_ID);
  1542. end;
  1543. end;
  1544. end;
  1545. else
  1546. begin
  1547. { is this a procedure variable ? }
  1548. if assigned(p1.resulttype.def) then
  1549. begin
  1550. if (p1.resulttype.def.deftype=procvardef) then
  1551. begin
  1552. if assigned(getprocvardef) and
  1553. equal_defs(p1.resulttype.def,getprocvardef) then
  1554. again:=false
  1555. else
  1556. if (token=_LKLAMMER) or
  1557. ((tprocvardef(p1.resulttype.def).para.empty) and
  1558. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1559. (not afterassignment) and
  1560. (not in_args)) then
  1561. begin
  1562. { do this in a strange way }
  1563. { it's not a clean solution }
  1564. p2:=p1;
  1565. p1:=ccallnode.create(nil,nil,nil,nil);
  1566. tcallnode(p1).set_procvar(p2);
  1567. if try_to_consume(_LKLAMMER) then
  1568. begin
  1569. tcallnode(p1).left:=parse_paras(false,false);
  1570. consume(_RKLAMMER);
  1571. end;
  1572. { proc():= is never possible }
  1573. if token=_ASSIGNMENT then
  1574. begin
  1575. Message(cg_e_illegal_expression);
  1576. p1:=cerrornode.create;
  1577. again:=false;
  1578. end;
  1579. end
  1580. else
  1581. again:=false;
  1582. end
  1583. else
  1584. again:=false;
  1585. end
  1586. else
  1587. again:=false;
  1588. end;
  1589. end;
  1590. end; { while again }
  1591. end;
  1592. {---------------------------------------------
  1593. Factor (Main)
  1594. ---------------------------------------------}
  1595. var
  1596. l : longint;
  1597. card : cardinal;
  1598. ic : TConstExprInt;
  1599. oldp1,
  1600. p1 : tnode;
  1601. code : integer;
  1602. again : boolean;
  1603. sym : tsym;
  1604. classh : tobjectdef;
  1605. d : bestreal;
  1606. hs : string;
  1607. htype : ttype;
  1608. filepos : tfileposinfo;
  1609. {---------------------------------------------
  1610. Helpers
  1611. ---------------------------------------------}
  1612. procedure check_tokenpos;
  1613. begin
  1614. if (p1<>oldp1) then
  1615. begin
  1616. if assigned(p1) then
  1617. p1.set_tree_filepos(filepos);
  1618. oldp1:=p1;
  1619. filepos:=akttokenpos;
  1620. end;
  1621. end;
  1622. begin
  1623. oldp1:=nil;
  1624. p1:=nil;
  1625. filepos:=akttokenpos;
  1626. again:=false;
  1627. if token=_ID then
  1628. begin
  1629. factor_read_id(p1,again);
  1630. if again then
  1631. begin
  1632. check_tokenpos;
  1633. { handle post fix operators }
  1634. postfixoperators(p1,again);
  1635. end;
  1636. end
  1637. else
  1638. case token of
  1639. _SELF :
  1640. begin
  1641. again:=true;
  1642. consume(_SELF);
  1643. if not assigned(procinfo._class) then
  1644. begin
  1645. p1:=cerrornode.create;
  1646. again:=false;
  1647. Message(parser_e_self_not_in_method);
  1648. end
  1649. else
  1650. begin
  1651. if (po_classmethod in aktprocdef.procoptions) then
  1652. begin
  1653. { self in class methods is a class reference type }
  1654. htype.setdef(procinfo._class);
  1655. p1:=cselfnode.create(tclassrefdef.create(htype));
  1656. end
  1657. else
  1658. p1:=cselfnode.create(procinfo._class);
  1659. postfixoperators(p1,again);
  1660. end;
  1661. end;
  1662. _INHERITED :
  1663. begin
  1664. again:=true;
  1665. consume(_INHERITED);
  1666. if assigned(procinfo._class) then
  1667. begin
  1668. classh:=procinfo._class.childof;
  1669. { if inherited; only then we need the method with
  1670. the same name }
  1671. if token in endtokens then
  1672. begin
  1673. hs:=aktprocsym.name;
  1674. anon_inherited:=true;
  1675. { For message methods we need to search using the message
  1676. number or string }
  1677. if (po_msgint in aktprocsym.first_procdef.procoptions) then
  1678. sym:=searchsym_in_class_by_msgint(classh,aktprocsym.first_procdef.messageinf.i)
  1679. else
  1680. if (po_msgstr in aktprocsym.first_procdef.procoptions) then
  1681. sym:=searchsym_in_class_by_msgstr(classh,aktprocsym.first_procdef.messageinf.str)
  1682. else
  1683. sym:=searchsym_in_class(classh,hs);
  1684. end
  1685. else
  1686. begin
  1687. hs:=pattern;
  1688. consume(_ID);
  1689. anon_inherited:=false;
  1690. sym:=searchsym_in_class(classh,hs);
  1691. end;
  1692. if assigned(sym) then
  1693. begin
  1694. check_hints(sym);
  1695. if sym.typ=procsym then
  1696. begin
  1697. htype.setdef(classh);
  1698. p1:=ctypenode.create(htype);
  1699. end;
  1700. do_member_read(false,sym,p1,again);
  1701. { Add flag to indicate that inherited is used }
  1702. if p1.nodetype=calln then
  1703. include(p1.flags,nf_anon_inherited);
  1704. end
  1705. else
  1706. begin
  1707. if anon_inherited then
  1708. begin
  1709. { we didn't find a member in the parents call the
  1710. DefaultHandler }
  1711. sym:=searchsym_in_class(classh,'DEFAULTHANDLER');
  1712. if not assigned(sym) or
  1713. (sym.typ<>procsym) then
  1714. internalerror(200303171);
  1715. p1:=nil;
  1716. do_proc_call(sym,sym.owner,false,again,p1);
  1717. end
  1718. else
  1719. begin
  1720. Message1(sym_e_id_no_member,hs);
  1721. p1:=cerrornode.create;
  1722. end;
  1723. again:=false;
  1724. end;
  1725. { turn auto inheriting off }
  1726. anon_inherited:=false;
  1727. end
  1728. else
  1729. begin
  1730. Message(parser_e_generic_methods_only_in_methods);
  1731. again:=false;
  1732. p1:=cerrornode.create;
  1733. end;
  1734. postfixoperators(p1,again);
  1735. end;
  1736. _INTCONST :
  1737. begin
  1738. { try cardinal first }
  1739. val(pattern,card,code);
  1740. if code<>0 then
  1741. begin
  1742. { then longint }
  1743. valint(pattern,l,code);
  1744. if code <> 0 then
  1745. begin
  1746. { then int64 }
  1747. val(pattern,ic,code);
  1748. if code<>0 then
  1749. begin
  1750. {finally float }
  1751. val(pattern,d,code);
  1752. if code<>0 then
  1753. begin
  1754. Message(cg_e_invalid_integer);
  1755. consume(_INTCONST);
  1756. l:=1;
  1757. p1:=cordconstnode.create(l,s32bittype,true);
  1758. end
  1759. else
  1760. begin
  1761. consume(_INTCONST);
  1762. p1:=crealconstnode.create(d,pbestrealtype^);
  1763. end;
  1764. end
  1765. else
  1766. begin
  1767. consume(_INTCONST);
  1768. p1:=cordconstnode.create(ic,cs64bittype,true);
  1769. end
  1770. end
  1771. else
  1772. begin
  1773. consume(_INTCONST);
  1774. p1:=cordconstnode.create(l,s32bittype,true)
  1775. end
  1776. end
  1777. else
  1778. begin
  1779. consume(_INTCONST);
  1780. { check whether the value isn't in the longint range as well }
  1781. { (longint is easier to perform calculations with) (JM) }
  1782. if card <= $7fffffff then
  1783. { no sign extension necessary, so not longint typecast (JM) }
  1784. p1:=cordconstnode.create(card,s32bittype,true)
  1785. else
  1786. p1:=cordconstnode.create(card,u32bittype,true)
  1787. end;
  1788. end;
  1789. _REALNUMBER :
  1790. begin
  1791. val(pattern,d,code);
  1792. if code<>0 then
  1793. begin
  1794. Message(parser_e_error_in_real);
  1795. d:=1.0;
  1796. end;
  1797. consume(_REALNUMBER);
  1798. p1:=crealconstnode.create(d,pbestrealtype^);
  1799. end;
  1800. _STRING :
  1801. begin
  1802. string_dec(htype);
  1803. { STRING can be also a type cast }
  1804. if token=_LKLAMMER then
  1805. begin
  1806. consume(_LKLAMMER);
  1807. p1:=comp_expr(true);
  1808. consume(_RKLAMMER);
  1809. p1:=ctypeconvnode.create(p1,htype);
  1810. include(p1.flags,nf_explizit);
  1811. { handle postfix operators here e.g. string(a)[10] }
  1812. again:=true;
  1813. postfixoperators(p1,again);
  1814. end
  1815. else
  1816. p1:=ctypenode.create(htype);
  1817. end;
  1818. _FILE :
  1819. begin
  1820. htype:=cfiletype;
  1821. consume(_FILE);
  1822. { FILE can be also a type cast }
  1823. if token=_LKLAMMER then
  1824. begin
  1825. consume(_LKLAMMER);
  1826. p1:=comp_expr(true);
  1827. consume(_RKLAMMER);
  1828. p1:=ctypeconvnode.create(p1,htype);
  1829. include(p1.flags,nf_explizit);
  1830. { handle postfix operators here e.g. string(a)[10] }
  1831. again:=true;
  1832. postfixoperators(p1,again);
  1833. end
  1834. else
  1835. begin
  1836. p1:=ctypenode.create(htype);
  1837. end;
  1838. end;
  1839. _CSTRING :
  1840. begin
  1841. p1:=cstringconstnode.createstr(pattern,st_default);
  1842. consume(_CSTRING);
  1843. end;
  1844. _CCHAR :
  1845. begin
  1846. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  1847. consume(_CCHAR);
  1848. end;
  1849. _CWSTRING:
  1850. begin
  1851. p1:=cstringconstnode.createwstr(patternw);
  1852. consume(_CWSTRING);
  1853. end;
  1854. _CWCHAR:
  1855. begin
  1856. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  1857. consume(_CWCHAR);
  1858. end;
  1859. _KLAMMERAFFE :
  1860. begin
  1861. consume(_KLAMMERAFFE);
  1862. got_addrn:=true;
  1863. { support both @<x> and @(<x>) }
  1864. if try_to_consume(_LKLAMMER) then
  1865. begin
  1866. p1:=factor(true);
  1867. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1868. begin
  1869. again:=true;
  1870. postfixoperators(p1,again);
  1871. end;
  1872. consume(_RKLAMMER);
  1873. end
  1874. else
  1875. p1:=factor(true);
  1876. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1877. begin
  1878. again:=true;
  1879. postfixoperators(p1,again);
  1880. end;
  1881. got_addrn:=false;
  1882. p1:=caddrnode.create(p1);
  1883. if assigned(getprocvardef) and
  1884. (taddrnode(p1).left.nodetype = loadn) and
  1885. { make sure we found a valid procedure, otherwise the }
  1886. { "getprocvardef" will become the default in taddrnode }
  1887. { while there should be an error (JM) }
  1888. assigned(tloadnode(taddrnode(p1).left).procdef) then
  1889. taddrnode(p1).getprocvardef:=getprocvardef;
  1890. end;
  1891. _LKLAMMER :
  1892. begin
  1893. consume(_LKLAMMER);
  1894. p1:=comp_expr(true);
  1895. consume(_RKLAMMER);
  1896. { it's not a good solution }
  1897. { but (a+b)^ makes some problems }
  1898. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1899. begin
  1900. again:=true;
  1901. postfixoperators(p1,again);
  1902. end;
  1903. end;
  1904. _LECKKLAMMER :
  1905. begin
  1906. consume(_LECKKLAMMER);
  1907. p1:=factor_read_set;
  1908. consume(_RECKKLAMMER);
  1909. end;
  1910. _PLUS :
  1911. begin
  1912. consume(_PLUS);
  1913. p1:=factor(false);
  1914. end;
  1915. _MINUS :
  1916. begin
  1917. consume(_MINUS);
  1918. p1:=sub_expr(oppower,false);
  1919. p1:=cunaryminusnode.create(p1);
  1920. end;
  1921. _OP_NOT :
  1922. begin
  1923. consume(_OP_NOT);
  1924. p1:=factor(false);
  1925. p1:=cnotnode.create(p1);
  1926. end;
  1927. _TRUE :
  1928. begin
  1929. consume(_TRUE);
  1930. p1:=cordconstnode.create(1,booltype,false);
  1931. end;
  1932. _FALSE :
  1933. begin
  1934. consume(_FALSE);
  1935. p1:=cordconstnode.create(0,booltype,false);
  1936. end;
  1937. _NIL :
  1938. begin
  1939. consume(_NIL);
  1940. p1:=cnilnode.create;
  1941. { It's really ugly code nil^, but delphi allows it }
  1942. if token in [_CARET] then
  1943. begin
  1944. again:=true;
  1945. postfixoperators(p1,again);
  1946. end;
  1947. end;
  1948. else
  1949. begin
  1950. p1:=cerrornode.create;
  1951. consume(token);
  1952. Message(cg_e_illegal_expression);
  1953. end;
  1954. end;
  1955. { generate error node if no node is created }
  1956. if not assigned(p1) then
  1957. begin
  1958. {$ifdef EXTDEBUG}
  1959. Comment(V_Warning,'factor: p1=nil');
  1960. {$endif}
  1961. p1:=cerrornode.create;
  1962. end;
  1963. { get the resulttype for the node }
  1964. if (not assigned(p1.resulttype.def)) then
  1965. do_resulttypepass(p1);
  1966. { tp7 procvar handling, but not if the next token
  1967. will be a := }
  1968. check_tp_procvar(p1);
  1969. factor:=p1;
  1970. check_tokenpos;
  1971. end;
  1972. {$ifdef fpc}
  1973. {$maxfpuregisters default}
  1974. {$endif fpc}
  1975. {****************************************************************************
  1976. Sub_Expr
  1977. ****************************************************************************}
  1978. const
  1979. { Warning these stay be ordered !! }
  1980. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1981. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  1982. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  1983. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  1984. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  1985. [_STARSTAR] );
  1986. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  1987. {Reads a subexpression while the operators are of the current precedence
  1988. level, or any higher level. Replaces the old term, simpl_expr and
  1989. simpl2_expr.}
  1990. var
  1991. p1,p2 : tnode;
  1992. oldt : Ttoken;
  1993. filepos : tfileposinfo;
  1994. begin
  1995. if pred_level=highest_precedence then
  1996. p1:=factor(false)
  1997. else
  1998. p1:=sub_expr(succ(pred_level),true);
  1999. repeat
  2000. if (token in operator_levels[pred_level]) and
  2001. ((token<>_EQUAL) or accept_equal) then
  2002. begin
  2003. oldt:=token;
  2004. filepos:=akttokenpos;
  2005. consume(token);
  2006. if pred_level=highest_precedence then
  2007. p2:=factor(false)
  2008. else
  2009. p2:=sub_expr(succ(pred_level),true);
  2010. case oldt of
  2011. _PLUS :
  2012. p1:=caddnode.create(addn,p1,p2);
  2013. _MINUS :
  2014. p1:=caddnode.create(subn,p1,p2);
  2015. _STAR :
  2016. p1:=caddnode.create(muln,p1,p2);
  2017. _SLASH :
  2018. p1:=caddnode.create(slashn,p1,p2);
  2019. _EQUAL :
  2020. p1:=caddnode.create(equaln,p1,p2);
  2021. _GT :
  2022. p1:=caddnode.create(gtn,p1,p2);
  2023. _LT :
  2024. p1:=caddnode.create(ltn,p1,p2);
  2025. _GTE :
  2026. p1:=caddnode.create(gten,p1,p2);
  2027. _LTE :
  2028. p1:=caddnode.create(lten,p1,p2);
  2029. _SYMDIF :
  2030. p1:=caddnode.create(symdifn,p1,p2);
  2031. _STARSTAR :
  2032. p1:=caddnode.create(starstarn,p1,p2);
  2033. _OP_AS :
  2034. p1:=casnode.create(p1,p2);
  2035. _OP_IN :
  2036. p1:=cinnode.create(p1,p2);
  2037. _OP_IS :
  2038. p1:=cisnode.create(p1,p2);
  2039. _OP_OR :
  2040. p1:=caddnode.create(orn,p1,p2);
  2041. _OP_AND :
  2042. p1:=caddnode.create(andn,p1,p2);
  2043. _OP_DIV :
  2044. p1:=cmoddivnode.create(divn,p1,p2);
  2045. _OP_NOT :
  2046. p1:=cnotnode.create(p1);
  2047. _OP_MOD :
  2048. p1:=cmoddivnode.create(modn,p1,p2);
  2049. _OP_SHL :
  2050. p1:=cshlshrnode.create(shln,p1,p2);
  2051. _OP_SHR :
  2052. p1:=cshlshrnode.create(shrn,p1,p2);
  2053. _OP_XOR :
  2054. p1:=caddnode.create(xorn,p1,p2);
  2055. _ASSIGNMENT :
  2056. p1:=cassignmentnode.create(p1,p2);
  2057. _CARET :
  2058. p1:=caddnode.create(caretn,p1,p2);
  2059. _UNEQUAL :
  2060. p1:=caddnode.create(unequaln,p1,p2);
  2061. end;
  2062. p1.set_tree_filepos(filepos);
  2063. end
  2064. else
  2065. break;
  2066. until false;
  2067. sub_expr:=p1;
  2068. end;
  2069. function comp_expr(accept_equal : boolean):tnode;
  2070. var
  2071. oldafterassignment : boolean;
  2072. p1 : tnode;
  2073. begin
  2074. oldafterassignment:=afterassignment;
  2075. afterassignment:=true;
  2076. p1:=sub_expr(opcompare,accept_equal);
  2077. { get the resulttype for this expression }
  2078. if not assigned(p1.resulttype.def) then
  2079. do_resulttypepass(p1);
  2080. afterassignment:=oldafterassignment;
  2081. comp_expr:=p1;
  2082. end;
  2083. function expr : tnode;
  2084. var
  2085. p1,p2 : tnode;
  2086. oldafterassignment : boolean;
  2087. oldp1 : tnode;
  2088. filepos : tfileposinfo;
  2089. begin
  2090. oldafterassignment:=afterassignment;
  2091. p1:=sub_expr(opcompare,true);
  2092. { get the resulttype for this expression }
  2093. if not assigned(p1.resulttype.def) then
  2094. do_resulttypepass(p1);
  2095. filepos:=akttokenpos;
  2096. check_tp_procvar(p1);
  2097. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2098. afterassignment:=true;
  2099. oldp1:=p1;
  2100. case token of
  2101. _POINTPOINT :
  2102. begin
  2103. consume(_POINTPOINT);
  2104. p2:=sub_expr(opcompare,true);
  2105. p1:=crangenode.create(p1,p2);
  2106. end;
  2107. _ASSIGNMENT :
  2108. begin
  2109. consume(_ASSIGNMENT);
  2110. if (p1.resulttype.def.deftype=procvardef) then
  2111. getprocvardef:=tprocvardef(p1.resulttype.def);
  2112. p2:=sub_expr(opcompare,true);
  2113. if assigned(getprocvardef) then
  2114. handle_procvar(getprocvardef,p2);
  2115. getprocvardef:=nil;
  2116. p1:=cassignmentnode.create(p1,p2);
  2117. end;
  2118. _PLUSASN :
  2119. begin
  2120. consume(_PLUSASN);
  2121. p2:=sub_expr(opcompare,true);
  2122. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2123. end;
  2124. _MINUSASN :
  2125. begin
  2126. consume(_MINUSASN);
  2127. p2:=sub_expr(opcompare,true);
  2128. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2129. end;
  2130. _STARASN :
  2131. begin
  2132. consume(_STARASN );
  2133. p2:=sub_expr(opcompare,true);
  2134. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2135. end;
  2136. _SLASHASN :
  2137. begin
  2138. consume(_SLASHASN );
  2139. p2:=sub_expr(opcompare,true);
  2140. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2141. end;
  2142. end;
  2143. { get the resulttype for this expression }
  2144. if not assigned(p1.resulttype.def) then
  2145. do_resulttypepass(p1);
  2146. afterassignment:=oldafterassignment;
  2147. if p1<>oldp1 then
  2148. p1.set_tree_filepos(filepos);
  2149. expr:=p1;
  2150. end;
  2151. {$ifdef int64funcresok}
  2152. function get_intconst:TConstExprInt;
  2153. {$else int64funcresok}
  2154. function get_intconst:longint;
  2155. {$endif int64funcresok}
  2156. {Reads an expression, tries to evalute it and check if it is an integer
  2157. constant. Then the constant is returned.}
  2158. var
  2159. p:tnode;
  2160. begin
  2161. p:=comp_expr(true);
  2162. if not codegenerror then
  2163. begin
  2164. if (p.nodetype<>ordconstn) or
  2165. not(is_integer(p.resulttype.def)) then
  2166. Message(cg_e_illegal_expression)
  2167. else
  2168. get_intconst:=tordconstnode(p).value;
  2169. end;
  2170. p.free;
  2171. end;
  2172. function get_stringconst:string;
  2173. {Reads an expression, tries to evaluate it and checks if it is a string
  2174. constant. Then the constant is returned.}
  2175. var
  2176. p:tnode;
  2177. begin
  2178. get_stringconst:='';
  2179. p:=comp_expr(true);
  2180. if p.nodetype<>stringconstn then
  2181. begin
  2182. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2183. get_stringconst:=char(tordconstnode(p).value)
  2184. else
  2185. Message(cg_e_illegal_expression);
  2186. end
  2187. else
  2188. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2189. p.free;
  2190. end;
  2191. end.
  2192. {
  2193. $Log$
  2194. Revision 1.106 2003-04-11 14:50:08 peter
  2195. * fix tw2454
  2196. Revision 1.105 2003/03/27 17:44:13 peter
  2197. * fixed small mem leaks
  2198. Revision 1.104 2003/03/17 18:55:30 peter
  2199. * allow more tokens instead of only semicolon after inherited
  2200. Revision 1.103 2003/03/17 16:54:41 peter
  2201. * support DefaultHandler and anonymous inheritance fixed
  2202. for message methods
  2203. Revision 1.102 2003/01/30 21:46:57 peter
  2204. * self fixes for static methods (merged)
  2205. Revision 1.101 2003/01/16 22:12:22 peter
  2206. * Find the correct procvar to load when using @ in fpc mode
  2207. Revision 1.100 2003/01/15 01:44:32 peter
  2208. * merged methodpointer fixes from 1.0.x
  2209. Revision 1.98 2003/01/12 17:51:42 peter
  2210. * tp procvar handling fix for tb0448
  2211. Revision 1.97 2003/01/05 22:44:14 peter
  2212. * remove a lot of code to support typen in loadn-procsym
  2213. Revision 1.96 2002/12/11 22:40:36 peter
  2214. * assigned(procvar) fix for delphi mode, fixes tb0430
  2215. Revision 1.95 2002/11/30 11:12:48 carl
  2216. + checking for symbols used with hint directives is done mostly in pexpr
  2217. only now
  2218. Revision 1.94 2002/11/27 15:33:47 peter
  2219. * the never ending story of tp procvar hacks
  2220. Revision 1.93 2002/11/26 22:58:24 peter
  2221. * fix for tw2178. When a ^ or . follows a procsym then the procsym
  2222. needs to be called
  2223. Revision 1.92 2002/11/25 17:43:22 peter
  2224. * splitted defbase in defutil,symutil,defcmp
  2225. * merged isconvertable and is_equal into compare_defs(_ext)
  2226. * made operator search faster by walking the list only once
  2227. Revision 1.91 2002/11/22 22:48:10 carl
  2228. * memory optimization with tconstsym (1.5%)
  2229. Revision 1.90 2002/11/20 22:49:55 pierre
  2230. * commented check code tht was invalid in 1.1
  2231. Revision 1.89 2002/11/18 18:34:41 peter
  2232. * fix crash with EXTDEBUG code
  2233. Revision 1.88 2002/11/18 17:48:21 peter
  2234. * fix tw2209 (merged)
  2235. Revision 1.87 2002/11/18 17:31:58 peter
  2236. * pass proccalloption to ret_in_xxx and push_xxx functions
  2237. Revision 1.86 2002/10/05 00:48:57 peter
  2238. * support inherited; support for overload as it is handled by
  2239. delphi. This is only for delphi mode as it is working is
  2240. undocumented and hard to predict what is done
  2241. Revision 1.85 2002/10/04 21:13:59 peter
  2242. * ignore vecn,subscriptn when checking for a procvar loadn
  2243. Revision 1.84 2002/10/02 20:51:22 peter
  2244. * don't check interfaces for class methods
  2245. Revision 1.83 2002/10/02 18:20:52 peter
  2246. * Copy() is now internal syssym that calls compilerprocs
  2247. Revision 1.82 2002/09/30 07:00:48 florian
  2248. * fixes to common code to get the alpha compiler compiled applied
  2249. Revision 1.81 2002/09/16 19:06:14 peter
  2250. * allow ^ after nil
  2251. Revision 1.80 2002/09/07 15:25:07 peter
  2252. * old logs removed and tabs fixed
  2253. Revision 1.79 2002/09/07 12:16:03 carl
  2254. * second part bug report 1996 fix, testrange in cordconstnode
  2255. only called if option is set (also make parsing a tiny faster)
  2256. Revision 1.78 2002/09/03 16:26:27 daniel
  2257. * Make Tprocdef.defs protected
  2258. Revision 1.77 2002/08/18 20:06:24 peter
  2259. * inlining is now also allowed in interface
  2260. * renamed write/load to ppuwrite/ppuload
  2261. * tnode storing in ppu
  2262. * nld,ncon,nbas are already updated for storing in ppu
  2263. Revision 1.76 2002/08/17 09:23:39 florian
  2264. * first part of procinfo rewrite
  2265. Revision 1.75 2002/08/01 16:37:47 jonas
  2266. - removed some superfluous "in_paras := true" statements
  2267. Revision 1.74 2002/07/26 21:15:41 florian
  2268. * rewrote the system handling
  2269. Revision 1.73 2002/07/23 09:51:23 daniel
  2270. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2271. are worth comitting.
  2272. Revision 1.72 2002/07/20 11:57:55 florian
  2273. * types.pas renamed to defbase.pas because D6 contains a types
  2274. unit so this would conflicts if D6 programms are compiled
  2275. + Willamette/SSE2 instructions to assembler added
  2276. Revision 1.71 2002/07/16 15:34:20 florian
  2277. * exit is now a syssym instead of a keyword
  2278. Revision 1.70 2002/07/06 20:18:02 carl
  2279. * longstring declaration now gives parser error since its not supported!
  2280. Revision 1.69 2002/06/12 15:46:14 jonas
  2281. * fixed web bug 1995
  2282. Revision 1.68 2002/05/18 13:34:12 peter
  2283. * readded missing revisions
  2284. Revision 1.67 2002/05/16 19:46:43 carl
  2285. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2286. + try to fix temp allocation (still in ifdef)
  2287. + generic constructor calls
  2288. + start of tassembler / tmodulebase class cleanup
  2289. Revision 1.65 2002/05/12 16:53:09 peter
  2290. * moved entry and exitcode to ncgutil and cgobj
  2291. * foreach gets extra argument for passing local data to the
  2292. iterator function
  2293. * -CR checks also class typecasts at runtime by changing them
  2294. into as
  2295. * fixed compiler to cycle with the -CR option
  2296. * fixed stabs with elf writer, finally the global variables can
  2297. be watched
  2298. * removed a lot of routines from cga unit and replaced them by
  2299. calls to cgobj
  2300. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2301. u32bit then the other is typecasted also to u32bit without giving
  2302. a rangecheck warning/error.
  2303. * fixed pascal calling method with reversing also the high tree in
  2304. the parast, detected by tcalcst3 test
  2305. Revision 1.64 2002/04/23 19:16:34 peter
  2306. * add pinline unit that inserts compiler supported functions using
  2307. one or more statements
  2308. * moved finalize and setlength from ninl to pinline
  2309. Revision 1.63 2002/04/21 19:02:05 peter
  2310. * removed newn and disposen nodes, the code is now directly
  2311. inlined from pexpr
  2312. * -an option that will write the secondpass nodes to the .s file, this
  2313. requires EXTDEBUG define to actually write the info
  2314. * fixed various internal errors and crashes due recent code changes
  2315. Revision 1.62 2002/04/16 16:11:17 peter
  2316. * using inherited; without a parent having the same function
  2317. will do nothing like delphi
  2318. Revision 1.61 2002/04/07 13:31:36 carl
  2319. + change unit use
  2320. Revision 1.60 2002/04/01 20:57:13 jonas
  2321. * fixed web bug 1907
  2322. * fixed some other procvar related bugs (all related to accepting procvar
  2323. constructs with either too many or too little parameters)
  2324. (both merged, includes second typo fix of pexpr.pas)
  2325. Revision 1.59 2002/03/31 20:26:35 jonas
  2326. + a_loadfpu_* and a_loadmm_* methods in tcg
  2327. * register allocation is now handled by a class and is mostly processor
  2328. independent (+rgobj.pas and i386/rgcpu.pas)
  2329. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2330. * some small improvements and fixes to the optimizer
  2331. * some register allocation fixes
  2332. * some fpuvaroffset fixes in the unary minus node
  2333. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2334. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2335. also better optimizable)
  2336. * fixed and optimized register saving/restoring for new/dispose nodes
  2337. * LOC_FPU locations now also require their "register" field to be set to
  2338. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2339. - list field removed of the tnode class because it's not used currently
  2340. and can cause hard-to-find bugs
  2341. }