pexpr.pas 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing of expression for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pexpr;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,symdef,symbase,
  22. node,ncal,
  23. tokens,globtype,globals,constexp;
  24. { reads a whole expression }
  25. function expr(dotypecheck:boolean) : tnode;
  26. { reads an expression without assignements and .. }
  27. function comp_expr(accept_equal,typeonly:boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr,typeonly:boolean) : tnode;
  30. procedure string_dec(var def: tdef; allowtypedef: boolean);
  31. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  32. { the ID token has to be consumed before calling this function }
  33. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  34. function get_intconst:TConstExprInt;
  35. function get_stringconst:string;
  36. implementation
  37. uses
  38. { common }
  39. cutils,
  40. { global }
  41. verbose,
  42. systems,widestr,
  43. { symtable }
  44. symconst,symtable,symsym,defutil,defcmp,
  45. { module }
  46. fmodule,ppu,
  47. { pass 1 }
  48. pass_1,htypechk,
  49. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  50. { parser }
  51. scanner,
  52. pbase,pinline,ptype,
  53. { codegen }
  54. cgbase,procinfo,cpuinfo
  55. ;
  56. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  57. read as - (1**4) and not (-1)**4 PM }
  58. type
  59. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  60. const
  61. highest_precedence = oppower;
  62. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean):tnode;forward;
  63. const
  64. { true, if the inherited call is anonymous }
  65. anon_inherited : boolean = false;
  66. { last def found, only used by anon. inherited calls to insert proper type casts }
  67. srdef : tdef = nil;
  68. procedure string_dec(var def:tdef; allowtypedef: boolean);
  69. { reads a string type with optional length }
  70. { and returns a pointer to the string }
  71. { definition }
  72. var
  73. p : tnode;
  74. begin
  75. def:=cshortstringtype;
  76. consume(_STRING);
  77. if token=_LECKKLAMMER then
  78. begin
  79. if not(allowtypedef) then
  80. Message(parser_e_no_local_para_def);
  81. consume(_LECKKLAMMER);
  82. p:=comp_expr(true,false);
  83. if not is_constintnode(p) then
  84. begin
  85. Message(parser_e_illegal_expression);
  86. { error recovery }
  87. consume(_RECKKLAMMER);
  88. end
  89. else
  90. begin
  91. if (tordconstnode(p).value<=0) then
  92. begin
  93. Message(parser_e_invalid_string_size);
  94. tordconstnode(p).value:=255;
  95. end;
  96. if tordconstnode(p).value>255 then
  97. begin
  98. { longstring is currently unsupported (CEC)! }
  99. { t:=tstringdef.createlong(tordconstnode(p).value))}
  100. Message(parser_e_invalid_string_size);
  101. tordconstnode(p).value:=255;
  102. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  103. end
  104. else
  105. if tordconstnode(p).value<>255 then
  106. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  107. consume(_RECKKLAMMER);
  108. end;
  109. p.free;
  110. end
  111. else
  112. begin
  113. if cs_ansistrings in current_settings.localswitches then
  114. def:=cansistringtype
  115. else
  116. def:=cshortstringtype;
  117. end;
  118. end;
  119. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  120. var
  121. p1,p2,argname : tnode;
  122. prev_in_args,
  123. old_named_args_allowed,
  124. old_allow_array_constructor : boolean;
  125. begin
  126. if token=end_of_paras then
  127. begin
  128. parse_paras:=nil;
  129. exit;
  130. end;
  131. { save old values }
  132. prev_in_args:=in_args;
  133. old_allow_array_constructor:=allow_array_constructor;
  134. old_named_args_allowed:=named_args_allowed;
  135. { set para parsing values }
  136. in_args:=true;
  137. named_args_allowed:=false;
  138. allow_array_constructor:=true;
  139. p2:=nil;
  140. repeat
  141. if __namedpara then
  142. begin
  143. if token=_COMMA then
  144. begin
  145. { empty parameter }
  146. p2:=ccallparanode.create(cnothingnode.create,p2);
  147. end
  148. else
  149. begin
  150. named_args_allowed:=true;
  151. p1:=comp_expr(true,false);
  152. named_args_allowed:=false;
  153. if found_arg_name then
  154. begin
  155. argname:=p1;
  156. p1:=comp_expr(true,false);
  157. p2:=ccallparanode.create(p1,p2);
  158. tcallparanode(p2).parametername:=argname;
  159. end
  160. else
  161. p2:=ccallparanode.create(p1,p2);
  162. found_arg_name:=false;
  163. end;
  164. end
  165. else
  166. begin
  167. p1:=comp_expr(true,false);
  168. p2:=ccallparanode.create(p1,p2);
  169. end;
  170. { it's for the str(l:5,s); }
  171. if __colon and (token=_COLON) then
  172. begin
  173. consume(_COLON);
  174. p1:=comp_expr(true,false);
  175. p2:=ccallparanode.create(p1,p2);
  176. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  177. if try_to_consume(_COLON) then
  178. begin
  179. p1:=comp_expr(true,false);
  180. p2:=ccallparanode.create(p1,p2);
  181. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  182. end
  183. end;
  184. until not try_to_consume(_COMMA);
  185. allow_array_constructor:=old_allow_array_constructor;
  186. in_args:=prev_in_args;
  187. named_args_allowed:=old_named_args_allowed;
  188. parse_paras:=p2;
  189. end;
  190. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  191. var
  192. hp : tnode;
  193. hdef : tdef;
  194. temp : ttempcreatenode;
  195. newstatement : tstatementnode;
  196. begin
  197. { Properties are not allowed, because the write can
  198. be different from the read }
  199. if (nf_isproperty in p1.flags) then
  200. begin
  201. Message(type_e_variable_id_expected);
  202. { We can continue with the loading,
  203. it'll not create errors. Only the expected
  204. result can be wrong }
  205. end;
  206. hp:=p1;
  207. while assigned(hp) and
  208. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  209. hp:=tunarynode(hp).left;
  210. if not assigned(hp) then
  211. internalerror(200410121);
  212. if (hp.nodetype=calln) then
  213. begin
  214. typecheckpass(p1);
  215. result:=internalstatements(newstatement);
  216. hdef:=tpointerdef.create(p1.resultdef);
  217. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  218. addstatement(newstatement,temp);
  219. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  220. addstatement(newstatement,cassignmentnode.create(
  221. cderefnode.create(ctemprefnode.create(temp)),
  222. caddnode.create(ntyp,
  223. cderefnode.create(ctemprefnode.create(temp)),
  224. p2)));
  225. addstatement(newstatement,ctempdeletenode.create(temp));
  226. end
  227. else
  228. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  229. end;
  230. function statement_syssym(l : byte) : tnode;
  231. var
  232. p1,p2,paras : tnode;
  233. err,
  234. prev_in_args : boolean;
  235. begin
  236. prev_in_args:=in_args;
  237. case l of
  238. in_new_x :
  239. begin
  240. if afterassignment or in_args then
  241. statement_syssym:=new_function
  242. else
  243. statement_syssym:=new_dispose_statement(true);
  244. end;
  245. in_dispose_x :
  246. begin
  247. statement_syssym:=new_dispose_statement(false);
  248. end;
  249. in_ord_x :
  250. begin
  251. consume(_LKLAMMER);
  252. in_args:=true;
  253. p1:=comp_expr(true,false);
  254. consume(_RKLAMMER);
  255. p1:=geninlinenode(in_ord_x,false,p1);
  256. statement_syssym := p1;
  257. end;
  258. in_exit :
  259. begin
  260. if try_to_consume(_LKLAMMER) then
  261. begin
  262. if not (m_mac in current_settings.modeswitches) then
  263. begin
  264. if not(try_to_consume(_RKLAMMER)) then
  265. begin
  266. p1:=comp_expr(true,false);
  267. consume(_RKLAMMER);
  268. if (not assigned(current_procinfo) or
  269. is_void(current_procinfo.procdef.returndef)) then
  270. begin
  271. Message(parser_e_void_function);
  272. { recovery }
  273. p1.free;
  274. p1:=nil;
  275. end;
  276. end
  277. else
  278. p1:=nil;
  279. end
  280. else
  281. begin
  282. if not (current_procinfo.procdef.procsym.name = pattern) then
  283. Message(parser_e_macpas_exit_wrong_param);
  284. consume(_ID);
  285. consume(_RKLAMMER);
  286. p1:=nil;
  287. end
  288. end
  289. else
  290. p1:=nil;
  291. statement_syssym:=cexitnode.create(p1);
  292. end;
  293. in_break :
  294. begin
  295. statement_syssym:=cbreaknode.create
  296. end;
  297. in_continue :
  298. begin
  299. statement_syssym:=ccontinuenode.create
  300. end;
  301. in_leave :
  302. begin
  303. if m_mac in current_settings.modeswitches then
  304. statement_syssym:=cbreaknode.create
  305. else
  306. begin
  307. Message1(sym_e_id_not_found, orgpattern);
  308. statement_syssym:=cerrornode.create;
  309. end;
  310. end;
  311. in_cycle :
  312. begin
  313. if m_mac in current_settings.modeswitches then
  314. statement_syssym:=ccontinuenode.create
  315. else
  316. begin
  317. Message1(sym_e_id_not_found, orgpattern);
  318. statement_syssym:=cerrornode.create;
  319. end;
  320. end;
  321. in_typeof_x :
  322. begin
  323. consume(_LKLAMMER);
  324. in_args:=true;
  325. p1:=comp_expr(true,false);
  326. consume(_RKLAMMER);
  327. if p1.nodetype=typen then
  328. ttypenode(p1).allowed:=true;
  329. { Allow classrefdef, which is required for
  330. Typeof(self) in static class methods }
  331. if not(is_objc_class_or_protocol(p1.resultdef)) and
  332. ((p1.resultdef.typ = objectdef) or
  333. (assigned(current_procinfo) and
  334. ((po_classmethod in current_procinfo.procdef.procoptions) or
  335. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  336. (p1.resultdef.typ=classrefdef))) then
  337. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  338. else
  339. begin
  340. Message(parser_e_class_id_expected);
  341. p1.destroy;
  342. statement_syssym:=cerrornode.create;
  343. end;
  344. end;
  345. in_sizeof_x,
  346. in_bitsizeof_x :
  347. begin
  348. consume(_LKLAMMER);
  349. in_args:=true;
  350. p1:=comp_expr(true,false);
  351. consume(_RKLAMMER);
  352. if (p1.nodetype<>typen) and
  353. (
  354. (is_object(p1.resultdef) and
  355. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  356. is_open_array(p1.resultdef) or
  357. is_array_of_const(p1.resultdef) or
  358. is_open_string(p1.resultdef)
  359. ) then
  360. begin
  361. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  362. { no packed bit support for these things }
  363. if (l = in_bitsizeof_x) then
  364. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  365. end
  366. else
  367. begin
  368. { allow helpers for SizeOf and BitSizeOf }
  369. if p1.nodetype=typen then
  370. ttypenode(p1).helperallowed:=true;
  371. if (p1.resultdef.typ=forwarddef) then
  372. Message1(type_e_type_is_not_completly_defined,tforwarddef(p1.resultdef).tosymname^);
  373. if (l = in_sizeof_x) or
  374. (not((p1.nodetype = vecn) and
  375. is_packed_array(tvecnode(p1).left.resultdef)) and
  376. not((p1.nodetype = subscriptn) and
  377. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  378. begin
  379. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  380. if (l = in_bitsizeof_x) then
  381. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  382. end
  383. else
  384. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  385. { p1 not needed !}
  386. p1.destroy;
  387. end;
  388. end;
  389. in_typeinfo_x,
  390. in_objc_encode_x :
  391. begin
  392. if (l=in_typeinfo_x) or
  393. (m_objectivec1 in current_settings.modeswitches) then
  394. begin
  395. consume(_LKLAMMER);
  396. in_args:=true;
  397. p1:=comp_expr(true,false);
  398. { When reading a class type it is parsed as loadvmtaddrn,
  399. typeinfo only needs the type so we remove the loadvmtaddrn }
  400. if p1.nodetype=loadvmtaddrn then
  401. begin
  402. p2:=tloadvmtaddrnode(p1).left;
  403. tloadvmtaddrnode(p1).left:=nil;
  404. p1.free;
  405. p1:=p2;
  406. end;
  407. if p1.nodetype=typen then
  408. begin
  409. ttypenode(p1).allowed:=true;
  410. { allow helpers for TypeInfo }
  411. if l=in_typeinfo_x then
  412. ttypenode(p1).helperallowed:=true;
  413. end;
  414. { else
  415. begin
  416. p1.destroy;
  417. p1:=cerrornode.create;
  418. Message(parser_e_illegal_parameter_list);
  419. end;}
  420. consume(_RKLAMMER);
  421. p2:=geninlinenode(l,false,p1);
  422. statement_syssym:=p2;
  423. end
  424. else
  425. begin
  426. Message1(sym_e_id_not_found, orgpattern);
  427. statement_syssym:=cerrornode.create;
  428. end;
  429. end;
  430. in_unaligned_x :
  431. begin
  432. err:=false;
  433. consume(_LKLAMMER);
  434. in_args:=true;
  435. p1:=comp_expr(true,false);
  436. p2:=ccallparanode.create(p1,nil);
  437. p2:=geninlinenode(in_unaligned_x,false,p2);
  438. consume(_RKLAMMER);
  439. statement_syssym:=p2;
  440. end;
  441. in_assigned_x :
  442. begin
  443. err:=false;
  444. consume(_LKLAMMER);
  445. in_args:=true;
  446. p1:=comp_expr(true,false);
  447. { When reading a class type it is parsed as loadvmtaddrn,
  448. typeinfo only needs the type so we remove the loadvmtaddrn }
  449. if p1.nodetype=loadvmtaddrn then
  450. begin
  451. p2:=tloadvmtaddrnode(p1).left;
  452. tloadvmtaddrnode(p1).left:=nil;
  453. p1.free;
  454. p1:=p2;
  455. end;
  456. if not codegenerror then
  457. begin
  458. case p1.resultdef.typ of
  459. procdef, { procvar }
  460. pointerdef,
  461. procvardef,
  462. classrefdef : ;
  463. objectdef :
  464. if not is_implicit_pointer_object_type(p1.resultdef) then
  465. begin
  466. Message(parser_e_illegal_parameter_list);
  467. err:=true;
  468. end;
  469. arraydef :
  470. if not is_dynamic_array(p1.resultdef) then
  471. begin
  472. Message(parser_e_illegal_parameter_list);
  473. err:=true;
  474. end;
  475. else
  476. if p1.resultdef.typ<>undefineddef then
  477. begin
  478. Message(parser_e_illegal_parameter_list);
  479. err:=true;
  480. end;
  481. end;
  482. end
  483. else
  484. err:=true;
  485. if not err then
  486. begin
  487. p2:=ccallparanode.create(p1,nil);
  488. p2:=geninlinenode(in_assigned_x,false,p2);
  489. end
  490. else
  491. begin
  492. p1.free;
  493. p2:=cerrornode.create;
  494. end;
  495. consume(_RKLAMMER);
  496. statement_syssym:=p2;
  497. end;
  498. in_addr_x :
  499. begin
  500. consume(_LKLAMMER);
  501. in_args:=true;
  502. p1:=comp_expr(true,false);
  503. p1:=caddrnode.create(p1);
  504. consume(_RKLAMMER);
  505. statement_syssym:=p1;
  506. end;
  507. in_ofs_x :
  508. begin
  509. consume(_LKLAMMER);
  510. in_args:=true;
  511. p1:=comp_expr(true,false);
  512. p1:=caddrnode.create(p1);
  513. do_typecheckpass(p1);
  514. { Ofs() returns a cardinal/qword, not a pointer }
  515. p1.resultdef:=uinttype;
  516. consume(_RKLAMMER);
  517. statement_syssym:=p1;
  518. end;
  519. in_seg_x :
  520. begin
  521. consume(_LKLAMMER);
  522. in_args:=true;
  523. p1:=comp_expr(true,false);
  524. p1:=geninlinenode(in_seg_x,false,p1);
  525. consume(_RKLAMMER);
  526. statement_syssym:=p1;
  527. end;
  528. in_high_x,
  529. in_low_x :
  530. begin
  531. consume(_LKLAMMER);
  532. in_args:=true;
  533. p1:=comp_expr(true,false);
  534. p2:=geninlinenode(l,false,p1);
  535. consume(_RKLAMMER);
  536. statement_syssym:=p2;
  537. end;
  538. in_succ_x,
  539. in_pred_x :
  540. begin
  541. consume(_LKLAMMER);
  542. in_args:=true;
  543. p1:=comp_expr(true,false);
  544. p2:=geninlinenode(l,false,p1);
  545. consume(_RKLAMMER);
  546. statement_syssym:=p2;
  547. end;
  548. in_inc_x,
  549. in_dec_x :
  550. begin
  551. consume(_LKLAMMER);
  552. in_args:=true;
  553. p1:=comp_expr(true,false);
  554. if try_to_consume(_COMMA) then
  555. p2:=ccallparanode.create(comp_expr(true,false),nil)
  556. else
  557. p2:=nil;
  558. p2:=ccallparanode.create(p1,p2);
  559. statement_syssym:=geninlinenode(l,false,p2);
  560. consume(_RKLAMMER);
  561. end;
  562. in_slice_x:
  563. begin
  564. if not(in_args) then
  565. begin
  566. message(parser_e_illegal_slice);
  567. consume(_LKLAMMER);
  568. in_args:=true;
  569. comp_expr(true,false).free;
  570. if try_to_consume(_COMMA) then
  571. comp_expr(true,false).free;
  572. statement_syssym:=cerrornode.create;
  573. consume(_RKLAMMER);
  574. end
  575. else
  576. begin
  577. consume(_LKLAMMER);
  578. in_args:=true;
  579. p1:=comp_expr(true,false);
  580. Consume(_COMMA);
  581. if not(codegenerror) then
  582. p2:=ccallparanode.create(comp_expr(true,false),nil)
  583. else
  584. p2:=cerrornode.create;
  585. p2:=ccallparanode.create(p1,p2);
  586. statement_syssym:=geninlinenode(l,false,p2);
  587. consume(_RKLAMMER);
  588. end;
  589. end;
  590. in_initialize_x:
  591. begin
  592. statement_syssym:=inline_initialize;
  593. end;
  594. in_finalize_x:
  595. begin
  596. statement_syssym:=inline_finalize;
  597. end;
  598. in_copy_x:
  599. begin
  600. statement_syssym:=inline_copy;
  601. end;
  602. in_concat_x :
  603. begin
  604. consume(_LKLAMMER);
  605. in_args:=true;
  606. { Translate to x:=x+y[+z]. The addnode will do the
  607. type checking }
  608. p2:=nil;
  609. repeat
  610. p1:=comp_expr(true,false);
  611. if p2<>nil then
  612. p2:=caddnode.create(addn,p2,p1)
  613. else
  614. begin
  615. { Force string type if it isn't yet }
  616. if not(
  617. (p1.resultdef.typ=stringdef) or
  618. is_chararray(p1.resultdef) or
  619. is_char(p1.resultdef)
  620. ) then
  621. inserttypeconv(p1,cshortstringtype);
  622. p2:=p1;
  623. end;
  624. until not try_to_consume(_COMMA);
  625. consume(_RKLAMMER);
  626. statement_syssym:=p2;
  627. end;
  628. in_read_x,
  629. in_readln_x,
  630. in_readstr_x:
  631. begin
  632. if try_to_consume(_LKLAMMER) then
  633. begin
  634. paras:=parse_paras(false,false,_RKLAMMER);
  635. consume(_RKLAMMER);
  636. end
  637. else
  638. paras:=nil;
  639. p1:=geninlinenode(l,false,paras);
  640. statement_syssym := p1;
  641. end;
  642. in_setlength_x:
  643. begin
  644. statement_syssym := inline_setlength;
  645. end;
  646. in_objc_selector_x:
  647. begin
  648. if (m_objectivec1 in current_settings.modeswitches) then
  649. begin
  650. consume(_LKLAMMER);
  651. in_args:=true;
  652. { don't turn procsyms into calls (getaddr = true) }
  653. p1:=factor(true,false);
  654. p2:=geninlinenode(l,false,p1);
  655. consume(_RKLAMMER);
  656. statement_syssym:=p2;
  657. end
  658. else
  659. begin
  660. Message1(sym_e_id_not_found, orgpattern);
  661. statement_syssym:=cerrornode.create;
  662. end;
  663. end;
  664. in_length_x:
  665. begin
  666. consume(_LKLAMMER);
  667. in_args:=true;
  668. p1:=comp_expr(true,false);
  669. p2:=geninlinenode(l,false,p1);
  670. consume(_RKLAMMER);
  671. statement_syssym:=p2;
  672. end;
  673. in_write_x,
  674. in_writeln_x,
  675. in_writestr_x :
  676. begin
  677. if try_to_consume(_LKLAMMER) then
  678. begin
  679. paras:=parse_paras(true,false,_RKLAMMER);
  680. consume(_RKLAMMER);
  681. end
  682. else
  683. paras:=nil;
  684. p1 := geninlinenode(l,false,paras);
  685. statement_syssym := p1;
  686. end;
  687. in_str_x_string :
  688. begin
  689. consume(_LKLAMMER);
  690. paras:=parse_paras(true,false,_RKLAMMER);
  691. consume(_RKLAMMER);
  692. p1 := geninlinenode(l,false,paras);
  693. statement_syssym := p1;
  694. end;
  695. in_val_x:
  696. Begin
  697. consume(_LKLAMMER);
  698. in_args := true;
  699. p1:= ccallparanode.create(comp_expr(true,false), nil);
  700. consume(_COMMA);
  701. p2 := ccallparanode.create(comp_expr(true,false),p1);
  702. if try_to_consume(_COMMA) then
  703. p2 := ccallparanode.create(comp_expr(true,false),p2);
  704. consume(_RKLAMMER);
  705. p2 := geninlinenode(l,false,p2);
  706. statement_syssym := p2;
  707. End;
  708. in_include_x_y,
  709. in_exclude_x_y :
  710. begin
  711. consume(_LKLAMMER);
  712. in_args:=true;
  713. p1:=comp_expr(true,false);
  714. consume(_COMMA);
  715. p2:=comp_expr(true,false);
  716. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  717. consume(_RKLAMMER);
  718. end;
  719. in_pack_x_y_z,
  720. in_unpack_x_y_z :
  721. begin
  722. consume(_LKLAMMER);
  723. in_args:=true;
  724. p1:=comp_expr(true,false);
  725. consume(_COMMA);
  726. p2:=comp_expr(true,false);
  727. consume(_COMMA);
  728. paras:=comp_expr(true,false);
  729. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  730. consume(_RKLAMMER);
  731. end;
  732. in_assert_x_y :
  733. begin
  734. consume(_LKLAMMER);
  735. in_args:=true;
  736. p1:=comp_expr(true,false);
  737. if try_to_consume(_COMMA) then
  738. p2:=comp_expr(true,false)
  739. else
  740. begin
  741. { then insert an empty string }
  742. p2:=cstringconstnode.createstr('');
  743. end;
  744. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  745. consume(_RKLAMMER);
  746. end;
  747. in_get_frame:
  748. begin
  749. statement_syssym:=geninlinenode(l,false,nil);
  750. end;
  751. (*
  752. in_get_caller_frame:
  753. begin
  754. if try_to_consume(_LKLAMMER) then
  755. begin
  756. {You used to call get_caller_frame as get_caller_frame(get_frame),
  757. however, as a stack frame may not exist, it does more harm than
  758. good, so ignore it.}
  759. in_args:=true;
  760. p1:=comp_expr(true,false);
  761. p1.destroy;
  762. consume(_RKLAMMER);
  763. end;
  764. statement_syssym:=geninlinenode(l,false,nil);
  765. end;
  766. *)
  767. else
  768. internalerror(15);
  769. end;
  770. in_args:=prev_in_args;
  771. end;
  772. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  773. begin
  774. maybe_load_methodpointer:=false;
  775. if not assigned(p1) then
  776. begin
  777. case st.symtabletype of
  778. withsymtable :
  779. begin
  780. if (st.defowner.typ=objectdef) then
  781. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  782. end;
  783. ObjectSymtable,
  784. recordsymtable:
  785. begin
  786. { We are calling from the static class method which has no self node }
  787. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  788. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  789. else
  790. p1:=load_self_node;
  791. { We are calling a member }
  792. maybe_load_methodpointer:=true;
  793. end;
  794. end;
  795. end;
  796. end;
  797. { reads the parameter for a subroutine call }
  798. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tabstractrecorddef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  799. var
  800. membercall,
  801. prevafterassn : boolean;
  802. i : integer;
  803. para,p2 : tnode;
  804. currpara : tparavarsym;
  805. aprocdef : tprocdef;
  806. begin
  807. prevafterassn:=afterassignment;
  808. afterassignment:=false;
  809. membercall:=false;
  810. aprocdef:=nil;
  811. { when it is a call to a member we need to load the
  812. methodpointer first
  813. }
  814. membercall:=maybe_load_methodpointer(st,p1);
  815. { When we are expecting a procvar we also need
  816. to get the address in some cases }
  817. if assigned(getprocvardef) then
  818. begin
  819. if (block_type=bt_const) or
  820. getaddr then
  821. begin
  822. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  823. getaddr:=true;
  824. end
  825. else
  826. if (m_tp_procvar in current_settings.modeswitches) or
  827. (m_mac_procvar in current_settings.modeswitches) then
  828. begin
  829. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  830. if assigned(aprocdef) then
  831. getaddr:=true;
  832. end;
  833. end;
  834. { only need to get the address of the procedure? }
  835. if getaddr then
  836. begin
  837. { Retrieve info which procvar to call. For tp_procvar the
  838. aprocdef is already loaded above so we can reuse it }
  839. if not assigned(aprocdef) and
  840. assigned(getprocvardef) then
  841. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  842. { generate a methodcallnode or proccallnode }
  843. { we shouldn't convert things like @tcollection.load }
  844. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  845. if assigned(p1) then
  846. begin
  847. { for loading methodpointer of an inherited function
  848. we use self as instance and load the address of
  849. the function directly and not through the vmt (PFV) }
  850. if (cnf_inherited in callflags) then
  851. begin
  852. include(p2.flags,nf_inherited);
  853. p1.free;
  854. p1:=load_self_node;
  855. end;
  856. if (p1.nodetype<>typen) then
  857. tloadnode(p2).set_mp(p1)
  858. else
  859. p1.free;
  860. end;
  861. p1:=p2;
  862. { no postfix operators }
  863. again:=false;
  864. end
  865. else
  866. begin
  867. para:=nil;
  868. if anon_inherited then
  869. begin
  870. if not assigned(current_procinfo) then
  871. internalerror(200305054);
  872. for i:=0 to current_procinfo.procdef.paras.count-1 do
  873. begin
  874. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  875. if not(vo_is_hidden_para in currpara.varoptions) then
  876. begin
  877. { inheritance by msgint? }
  878. if assigned(srdef) then
  879. { anonymous inherited via msgid calls only require a var parameter for
  880. both methods, so we need some type casting here }
  881. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  882. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  883. para)
  884. else
  885. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  886. end;
  887. end;
  888. end
  889. else
  890. begin
  891. if try_to_consume(_LKLAMMER) then
  892. begin
  893. para:=parse_paras(false,false,_RKLAMMER);
  894. consume(_RKLAMMER);
  895. end;
  896. end;
  897. { indicate if this call was generated by a member and
  898. no explicit self is used, this is needed to determine
  899. how to handle a destructor call (PFV) }
  900. if membercall then
  901. include(callflags,cnf_member_call);
  902. if assigned(obj) then
  903. begin
  904. if not (st.symtabletype in [ObjectSymtable,recordsymtable]) then
  905. internalerror(200310031);
  906. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  907. end
  908. else
  909. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  910. end;
  911. afterassignment:=prevafterassn;
  912. end;
  913. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  914. var
  915. hp,hp2 : tnode;
  916. hpp : ^tnode;
  917. currprocdef : tprocdef;
  918. begin
  919. if not assigned(pv) then
  920. internalerror(200301121);
  921. if (m_tp_procvar in current_settings.modeswitches) or
  922. (m_mac_procvar in current_settings.modeswitches) then
  923. begin
  924. hp:=p2;
  925. hpp:=@p2;
  926. while assigned(hp) and
  927. (hp.nodetype=typeconvn) do
  928. begin
  929. hp:=ttypeconvnode(hp).left;
  930. { save orignal address of the old tree so we can replace the node }
  931. hpp:=@hp;
  932. end;
  933. if (hp.nodetype=calln) and
  934. { a procvar can't have parameters! }
  935. not assigned(tcallnode(hp).left) then
  936. begin
  937. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  938. if assigned(currprocdef) then
  939. begin
  940. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  941. if (po_methodpointer in pv.procoptions) then
  942. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  943. hp.destroy;
  944. { replace the old callnode with the new loadnode }
  945. hpp^:=hp2;
  946. end;
  947. end;
  948. end;
  949. end;
  950. { checks whether sym is a static field and if so, translates the access
  951. to the appropriate node tree }
  952. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  953. var
  954. static_name: shortstring;
  955. srsymtable: tsymtable;
  956. begin
  957. result:=false;
  958. { generate access code }
  959. if (sp_static in sym.symoptions) then
  960. begin
  961. result:=true;
  962. if not nested then
  963. static_name:=lower(sym.owner.name^)+'_'+sym.name
  964. else
  965. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  966. if sym.owner.defowner.typ=objectdef then
  967. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,true)
  968. else
  969. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  970. if assigned(sym) then
  971. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  972. p1.free;
  973. p1:=nil;
  974. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  975. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  976. end;
  977. end;
  978. { the following procedure handles the access to a property symbol }
  979. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  980. var
  981. paras : tnode;
  982. p2 : tnode;
  983. membercall : boolean;
  984. callflags : tcallnodeflags;
  985. propaccesslist : tpropaccesslist;
  986. sym: tsym;
  987. begin
  988. { property parameters? read them only if the property really }
  989. { has parameters }
  990. paras:=nil;
  991. if (ppo_hasparameters in propsym.propoptions) then
  992. begin
  993. if try_to_consume(_LECKKLAMMER) then
  994. begin
  995. paras:=parse_paras(false,false,_RECKKLAMMER);
  996. consume(_RECKKLAMMER);
  997. end;
  998. end;
  999. { indexed property }
  1000. if (ppo_indexed in propsym.propoptions) then
  1001. begin
  1002. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  1003. paras:=ccallparanode.create(p2,paras);
  1004. end;
  1005. { we need only a write property if a := follows }
  1006. { if not(afterassignment) and not(in_args) then }
  1007. if token=_ASSIGNMENT then
  1008. begin
  1009. if getpropaccesslist(propsym,palt_write,propaccesslist) then
  1010. begin
  1011. sym:=propaccesslist.firstsym^.sym;
  1012. case sym.typ of
  1013. procsym :
  1014. begin
  1015. callflags:=[];
  1016. { generate the method call }
  1017. membercall:=maybe_load_methodpointer(st,p1);
  1018. if membercall then
  1019. include(callflags,cnf_member_call);
  1020. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1021. addsymref(sym);
  1022. paras:=nil;
  1023. consume(_ASSIGNMENT);
  1024. { read the expression }
  1025. if propsym.propdef.typ=procvardef then
  1026. getprocvardef:=tprocvardef(propsym.propdef);
  1027. p2:=comp_expr(true,false);
  1028. if assigned(getprocvardef) then
  1029. handle_procvar(getprocvardef,p2);
  1030. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1031. { mark as property, both the tcallnode and the real call block }
  1032. include(p1.flags,nf_isproperty);
  1033. getprocvardef:=nil;
  1034. end;
  1035. fieldvarsym :
  1036. begin
  1037. { generate access code }
  1038. if not handle_staticfield_access(sym,false,p1) then
  1039. propaccesslist_to_node(p1,st,propaccesslist);
  1040. include(p1.flags,nf_isproperty);
  1041. consume(_ASSIGNMENT);
  1042. { read the expression }
  1043. p2:=comp_expr(true,false);
  1044. p1:=cassignmentnode.create(p1,p2);
  1045. end
  1046. else
  1047. begin
  1048. p1:=cerrornode.create;
  1049. Message(parser_e_no_procedure_to_access_property);
  1050. end;
  1051. end;
  1052. end
  1053. else
  1054. begin
  1055. p1:=cerrornode.create;
  1056. Message(parser_e_no_procedure_to_access_property);
  1057. end;
  1058. end
  1059. else
  1060. begin
  1061. if getpropaccesslist(propsym,palt_read,propaccesslist) then
  1062. begin
  1063. sym := propaccesslist.firstsym^.sym;
  1064. case sym.typ of
  1065. fieldvarsym :
  1066. begin
  1067. { generate access code }
  1068. if not handle_staticfield_access(sym,false,p1) then
  1069. propaccesslist_to_node(p1,st,propaccesslist);
  1070. include(p1.flags,nf_isproperty);
  1071. end;
  1072. procsym :
  1073. begin
  1074. callflags:=[];
  1075. { generate the method call }
  1076. membercall:=maybe_load_methodpointer(st,p1);
  1077. if membercall then
  1078. include(callflags,cnf_member_call);
  1079. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1080. paras:=nil;
  1081. include(p1.flags,nf_isproperty);
  1082. end
  1083. else
  1084. begin
  1085. p1:=cerrornode.create;
  1086. Message(type_e_mismatch);
  1087. end;
  1088. end;
  1089. end
  1090. else
  1091. begin
  1092. { error, no function to read property }
  1093. p1:=cerrornode.create;
  1094. Message(parser_e_no_procedure_to_access_property);
  1095. end;
  1096. end;
  1097. { release paras if not used }
  1098. if assigned(paras) then
  1099. paras.free;
  1100. end;
  1101. { the ID token has to be consumed before calling this function }
  1102. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  1103. var
  1104. isclassref : boolean;
  1105. begin
  1106. if sym=nil then
  1107. begin
  1108. { pattern is still valid unless
  1109. there is another ID just after the ID of sym }
  1110. Message1(sym_e_id_no_member,orgpattern);
  1111. p1.free;
  1112. p1:=cerrornode.create;
  1113. { try to clean up }
  1114. again:=false;
  1115. end
  1116. else
  1117. begin
  1118. if assigned(p1) then
  1119. begin
  1120. if not assigned(p1.resultdef) then
  1121. do_typecheckpass(p1);
  1122. isclassref:=(p1.resultdef.typ=classrefdef);
  1123. end
  1124. else
  1125. isclassref:=false;
  1126. { we assume, that only procsyms and varsyms are in an object }
  1127. { symbol table, for classes, properties are allowed }
  1128. case sym.typ of
  1129. procsym:
  1130. begin
  1131. do_proc_call(sym,sym.owner,structh,
  1132. (getaddr and not(token in [_CARET,_POINT])),
  1133. again,p1,callflags);
  1134. { we need to know which procedure is called }
  1135. do_typecheckpass(p1);
  1136. { calling using classref? }
  1137. if isclassref and
  1138. (p1.nodetype=calln) and
  1139. assigned(tcallnode(p1).procdefinition) and
  1140. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1141. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1142. Message(parser_e_only_class_members_via_class_ref);
  1143. end;
  1144. fieldvarsym:
  1145. begin
  1146. if not handle_staticfield_access(sym,true,p1) then
  1147. begin
  1148. if isclassref then
  1149. if assigned(p1) and
  1150. (
  1151. is_self_node(p1) or
  1152. (assigned(current_procinfo) and (current_procinfo.procdef.no_self_node) and
  1153. (current_procinfo.procdef.struct=structh))) then
  1154. Message(parser_e_only_class_members)
  1155. else
  1156. Message(parser_e_only_class_members_via_class_ref);
  1157. p1:=csubscriptnode.create(sym,p1);
  1158. end;
  1159. end;
  1160. propertysym:
  1161. begin
  1162. if isclassref and not (sp_static in sym.symoptions) then
  1163. Message(parser_e_only_class_members_via_class_ref);
  1164. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1165. end;
  1166. typesym:
  1167. begin
  1168. p1.free;
  1169. if try_to_consume(_LKLAMMER) then
  1170. begin
  1171. p1:=comp_expr(true,false);
  1172. consume(_RKLAMMER);
  1173. p1:=ctypeconvnode.create_explicit(p1,ttypesym(sym).typedef);
  1174. end
  1175. else
  1176. begin
  1177. p1:=ctypenode.create(ttypesym(sym).typedef);
  1178. if (is_class(ttypesym(sym).typedef) or is_objcclass(ttypesym(sym).typedef)) and
  1179. not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1180. p1:=cloadvmtaddrnode.create(p1);
  1181. end;
  1182. end;
  1183. constsym:
  1184. begin
  1185. p1.free;
  1186. p1:=genconstsymtree(tconstsym(sym));
  1187. end;
  1188. staticvarsym:
  1189. begin
  1190. { typed constant is a staticvarsym
  1191. now they are absolutevarsym }
  1192. p1.free;
  1193. p1:=cloadnode.create(sym,sym.Owner);
  1194. end;
  1195. absolutevarsym:
  1196. begin
  1197. p1.free;
  1198. p1:=nil;
  1199. { typed constants are absolutebarsyms now to handle storage properly }
  1200. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1201. end
  1202. else
  1203. internalerror(16);
  1204. end;
  1205. end;
  1206. end;
  1207. {****************************************************************************
  1208. Factor
  1209. ****************************************************************************}
  1210. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  1211. out memberparentdef: tdef): boolean;
  1212. var
  1213. hdef : tdef;
  1214. begin
  1215. result:=true;
  1216. memberparentdef:=nil;
  1217. case st.symtabletype of
  1218. ObjectSymtable,
  1219. recordsymtable:
  1220. begin
  1221. memberparentdef:=tdef(st.defowner);
  1222. exit;
  1223. end;
  1224. WithSymtable:
  1225. begin
  1226. if assigned(p1) then
  1227. internalerror(2007012002);
  1228. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  1229. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1230. if not(hdef.typ in [objectdef,classrefdef]) then
  1231. exit;
  1232. if (hdef.typ=classrefdef) then
  1233. hdef:=tclassrefdef(hdef).pointeddef;
  1234. memberparentdef:=hdef;
  1235. end;
  1236. else
  1237. result:=false;
  1238. end;
  1239. end;
  1240. {$maxfpuregisters 0}
  1241. function factor(getaddr,typeonly:boolean) : tnode;
  1242. {---------------------------------------------
  1243. Factor_read_id
  1244. ---------------------------------------------}
  1245. procedure factor_read_id(out p1:tnode;var again:boolean);
  1246. var
  1247. srsym : tsym;
  1248. srsymtable : TSymtable;
  1249. hdef : tdef;
  1250. orgstoredpattern,
  1251. storedpattern : string;
  1252. callflags: tcallnodeflags;
  1253. t : ttoken;
  1254. unit_found : boolean;
  1255. begin
  1256. { allow post fix operators }
  1257. again:=true;
  1258. { first check for identifier }
  1259. if token<>_ID then
  1260. begin
  1261. srsym:=generrorsym;
  1262. srsymtable:=nil;
  1263. consume(_ID);
  1264. end
  1265. else
  1266. begin
  1267. if typeonly then
  1268. searchsym_type(pattern,srsym,srsymtable)
  1269. else
  1270. searchsym(pattern,srsym,srsymtable);
  1271. { handle unit specification like System.Writeln }
  1272. unit_found:=try_consume_unitsym(srsym,srsymtable,t,true);
  1273. storedpattern:=pattern;
  1274. orgstoredpattern:=orgpattern;
  1275. consume(t);
  1276. { named parameter support }
  1277. found_arg_name:=false;
  1278. if not(unit_found) and
  1279. named_args_allowed and
  1280. (token=_ASSIGNMENT) then
  1281. begin
  1282. found_arg_name:=true;
  1283. p1:=cstringconstnode.createstr(storedpattern);
  1284. consume(_ASSIGNMENT);
  1285. exit;
  1286. end;
  1287. { if nothing found give error and return errorsym }
  1288. if assigned(srsym) then
  1289. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  1290. else
  1291. begin
  1292. identifier_not_found(orgstoredpattern);
  1293. srsym:=generrorsym;
  1294. srsymtable:=nil;
  1295. end;
  1296. end;
  1297. { Access to funcret or need to call the function? }
  1298. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1299. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1300. { result(x) is not allowed }
  1301. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  1302. (
  1303. (token=_LKLAMMER) or
  1304. (
  1305. (
  1306. (m_tp7 in current_settings.modeswitches) or
  1307. (m_delphi in current_settings.modeswitches)
  1308. ) and
  1309. (afterassignment or in_args)
  1310. )
  1311. ) then
  1312. begin
  1313. hdef:=tdef(srsym.owner.defowner);
  1314. if assigned(hdef) and
  1315. (hdef.typ=procdef) then
  1316. srsym:=tprocdef(hdef).procsym
  1317. else
  1318. begin
  1319. Message(parser_e_illegal_expression);
  1320. srsym:=generrorsym;
  1321. end;
  1322. srsymtable:=srsym.owner;
  1323. end;
  1324. begin
  1325. case srsym.typ of
  1326. absolutevarsym :
  1327. begin
  1328. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1329. begin
  1330. p1:=nil;
  1331. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1332. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1333. include(p1.flags,nf_absolute);
  1334. end
  1335. else
  1336. p1:=cloadnode.create(srsym,srsymtable);
  1337. end;
  1338. staticvarsym,
  1339. localvarsym,
  1340. paravarsym,
  1341. fieldvarsym :
  1342. begin
  1343. { check if we are reading a field of an object/class/ }
  1344. { record. is_member_read() will deal with withsymtables }
  1345. { if needed. }
  1346. p1:=nil;
  1347. if is_member_read(srsym,srsymtable,p1,hdef) then
  1348. begin
  1349. { if the field was originally found in an }
  1350. { objectsymtable, it means it's part of self
  1351. if only method from which it was called is
  1352. not class static }
  1353. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  1354. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  1355. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  1356. else
  1357. p1:=load_self_node;
  1358. { now, if the field itself is part of an objectsymtab }
  1359. { (it can be even if it was found in a withsymtable, }
  1360. { e.g., "with classinstance do field := 5"), then }
  1361. { let do_member_read handle it }
  1362. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1363. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  1364. else
  1365. { otherwise it's a regular record subscript }
  1366. p1:=csubscriptnode.create(srsym,p1);
  1367. end
  1368. else
  1369. { regular non-field load }
  1370. p1:=cloadnode.create(srsym,srsymtable);
  1371. end;
  1372. syssym :
  1373. begin
  1374. p1:=statement_syssym(tsyssym(srsym).number);
  1375. end;
  1376. typesym :
  1377. begin
  1378. hdef:=ttypesym(srsym).typedef;
  1379. if not assigned(hdef) then
  1380. begin
  1381. again:=false;
  1382. end
  1383. else
  1384. begin
  1385. { We need to know if this unit uses Variants }
  1386. if (hdef=cvarianttype) and
  1387. not(cs_compilesystem in current_settings.moduleswitches) then
  1388. current_module.flags:=current_module.flags or uf_uses_variants;
  1389. { if we get a generic then check that it is not an inline specialization }
  1390. if (df_generic in hdef.defoptions) and
  1391. (token=_LT) and
  1392. (m_delphi in current_settings.modeswitches) then
  1393. generate_specialization(hdef,false,'');
  1394. if not typeonly and try_to_consume(_LKLAMMER) then
  1395. begin
  1396. p1:=comp_expr(true,false);
  1397. consume(_RKLAMMER);
  1398. { type casts to class helpers aren't allowed }
  1399. if is_objectpascal_helper(hdef) then
  1400. Message(parser_e_no_category_as_types)
  1401. { recovery by not creating a conversion node }
  1402. else
  1403. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1404. end
  1405. else { not LKLAMMER }
  1406. if (token=_POINT) and
  1407. (is_object(hdef) or is_record(hdef)) then
  1408. begin
  1409. consume(_POINT);
  1410. { handles calling methods declared in parent objects
  1411. using "parentobject.methodname()" }
  1412. if assigned(current_structdef) and
  1413. not(getaddr) and
  1414. current_structdef.is_related(hdef) then
  1415. begin
  1416. p1:=ctypenode.create(hdef);
  1417. { search also in inherited methods }
  1418. searchsym_in_class(tobjectdef(hdef),tobjectdef(current_structdef),pattern,srsym,srsymtable,true);
  1419. if assigned(srsym) then
  1420. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1421. consume(_ID);
  1422. do_member_read(tabstractrecorddef(hdef),false,srsym,p1,again,[]);
  1423. end
  1424. else
  1425. begin
  1426. { handles:
  1427. * @TObject.Load
  1428. * static methods and variables }
  1429. p1:=ctypenode.create(hdef);
  1430. { TP allows also @TMenu.Load if Load is only }
  1431. { defined in an anchestor class }
  1432. srsym:=search_struct_member(tabstractrecorddef(hdef),pattern);
  1433. if assigned(srsym) then
  1434. begin
  1435. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1436. consume(_ID);
  1437. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[]);
  1438. end
  1439. else
  1440. Message1(sym_e_id_no_member,orgpattern);
  1441. end;
  1442. end
  1443. else
  1444. begin
  1445. { Normally here would be the check against the usage
  1446. of "TClassHelper.Something", but as that might be
  1447. used inside of system symbols like sizeof and
  1448. typeinfo this check is put into ttypenode.pass_1
  1449. (for "TClassHelper" alone) and tcallnode.pass_1
  1450. (for "TClassHelper.Something") }
  1451. { class reference ? }
  1452. if is_class(hdef) or
  1453. is_objcclass(hdef) then
  1454. begin
  1455. if getaddr and (token=_POINT) then
  1456. begin
  1457. consume(_POINT);
  1458. { allows @Object.Method }
  1459. { also allows static methods and variables }
  1460. p1:=ctypenode.create(hdef);
  1461. { TP allows also @TMenu.Load if Load is only }
  1462. { defined in an anchestor class }
  1463. srsym:=search_struct_member(tobjectdef(hdef),pattern);
  1464. if assigned(srsym) then
  1465. begin
  1466. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1467. consume(_ID);
  1468. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[]);
  1469. end
  1470. else
  1471. begin
  1472. Message1(sym_e_id_no_member,orgpattern);
  1473. consume(_ID);
  1474. end;
  1475. end
  1476. else
  1477. begin
  1478. p1:=ctypenode.create(hdef);
  1479. { For a type block we simply return only
  1480. the type. For all other blocks we return
  1481. a loadvmt node }
  1482. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1483. p1:=cloadvmtaddrnode.create(p1);
  1484. end;
  1485. end
  1486. else
  1487. p1:=ctypenode.create(hdef);
  1488. end;
  1489. end;
  1490. end;
  1491. enumsym :
  1492. begin
  1493. p1:=genenumnode(tenumsym(srsym));
  1494. end;
  1495. constsym :
  1496. begin
  1497. if tconstsym(srsym).consttyp=constresourcestring then
  1498. begin
  1499. p1:=cloadnode.create(srsym,srsymtable);
  1500. do_typecheckpass(p1);
  1501. p1.resultdef:=cansistringtype;
  1502. end
  1503. else
  1504. p1:=genconstsymtree(tconstsym(srsym));
  1505. end;
  1506. procsym :
  1507. begin
  1508. p1:=nil;
  1509. { check if it's a method/class method }
  1510. if is_member_read(srsym,srsymtable,p1,hdef) then
  1511. begin
  1512. { not srsymtable.symtabletype since that can be }
  1513. { withsymtable as well }
  1514. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1515. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  1516. else
  1517. { no procsyms in records (yet) }
  1518. internalerror(2007012006);
  1519. end
  1520. else
  1521. begin
  1522. { regular procedure/function call }
  1523. if not unit_found then
  1524. callflags:=[]
  1525. else
  1526. callflags:=[cnf_unit_specified];
  1527. do_proc_call(srsym,srsymtable,nil,
  1528. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  1529. again,p1,callflags);
  1530. end;
  1531. end;
  1532. propertysym :
  1533. begin
  1534. p1:=nil;
  1535. { property of a class/object? }
  1536. if is_member_read(srsym,srsymtable,p1,hdef) then
  1537. begin
  1538. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  1539. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  1540. { no self node in static class methods }
  1541. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  1542. else
  1543. p1:=load_self_node;
  1544. { not srsymtable.symtabletype since that can be }
  1545. { withsymtable as well }
  1546. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1547. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  1548. else
  1549. { no propertysyms in records (yet) }
  1550. internalerror(2009111510);
  1551. end
  1552. else
  1553. { no method pointer }
  1554. begin
  1555. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1556. end;
  1557. end;
  1558. labelsym :
  1559. begin
  1560. { Support @label }
  1561. if getaddr then
  1562. begin
  1563. if srsym.owner<>current_procinfo.procdef.localst then
  1564. CGMessage(parser_e_label_outside_proc);
  1565. p1:=cloadnode.create(srsym,srsym.owner)
  1566. end
  1567. else
  1568. begin
  1569. consume(_COLON);
  1570. if tlabelsym(srsym).defined then
  1571. Message(sym_e_label_already_defined);
  1572. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1573. begin
  1574. tlabelsym(srsym).nonlocal:=true;
  1575. exclude(current_procinfo.procdef.procoptions,po_inline);
  1576. end;
  1577. if tlabelsym(srsym).nonlocal and
  1578. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1579. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  1580. tlabelsym(srsym).defined:=true;
  1581. p1:=clabelnode.create(nil,tlabelsym(srsym));
  1582. tlabelsym(srsym).code:=p1;
  1583. end;
  1584. end;
  1585. errorsym :
  1586. begin
  1587. p1:=cerrornode.create;
  1588. if try_to_consume(_LKLAMMER) then
  1589. begin
  1590. parse_paras(false,false,_RKLAMMER);
  1591. consume(_RKLAMMER);
  1592. end;
  1593. end;
  1594. else
  1595. begin
  1596. p1:=cerrornode.create;
  1597. Message(parser_e_illegal_expression);
  1598. end;
  1599. end; { end case }
  1600. end;
  1601. end;
  1602. {---------------------------------------------
  1603. Factor_Read_Set
  1604. ---------------------------------------------}
  1605. { Read a set between [] }
  1606. function factor_read_set:tnode;
  1607. var
  1608. p1,p2 : tnode;
  1609. lastp,
  1610. buildp : tarrayconstructornode;
  1611. old_allow_array_constructor : boolean;
  1612. begin
  1613. buildp:=nil;
  1614. { be sure that a least one arrayconstructn is used, also for an
  1615. empty [] }
  1616. if token=_RECKKLAMMER then
  1617. buildp:=carrayconstructornode.create(nil,buildp)
  1618. else
  1619. repeat
  1620. { nested array constructors are not allowed, see also tests/webtbs/tw17213.pp }
  1621. old_allow_array_constructor:=allow_array_constructor;
  1622. allow_array_constructor:=false;
  1623. p1:=comp_expr(true,false);
  1624. if try_to_consume(_POINTPOINT) then
  1625. begin
  1626. p2:=comp_expr(true,false);
  1627. p1:=carrayconstructorrangenode.create(p1,p2);
  1628. end;
  1629. { insert at the end of the tree, to get the correct order }
  1630. if not assigned(buildp) then
  1631. begin
  1632. buildp:=carrayconstructornode.create(p1,nil);
  1633. lastp:=buildp;
  1634. end
  1635. else
  1636. begin
  1637. lastp.right:=carrayconstructornode.create(p1,nil);
  1638. lastp:=tarrayconstructornode(lastp.right);
  1639. end;
  1640. allow_array_constructor:=old_allow_array_constructor;
  1641. { there could be more elements }
  1642. until not try_to_consume(_COMMA);
  1643. factor_read_set:=buildp;
  1644. end;
  1645. {---------------------------------------------
  1646. PostFixOperators
  1647. ---------------------------------------------}
  1648. { returns whether or not p1 has been changed }
  1649. function postfixoperators(var p1:tnode;var again:boolean): boolean;
  1650. { tries to avoid syntax errors after invalid qualifiers }
  1651. procedure recoverconsume_postfixops;
  1652. begin
  1653. repeat
  1654. if not try_to_consume(_CARET) then
  1655. if try_to_consume(_POINT) then
  1656. try_to_consume(_ID)
  1657. else if try_to_consume(_LECKKLAMMER) then
  1658. begin
  1659. repeat
  1660. comp_expr(true,false);
  1661. until not try_to_consume(_COMMA);
  1662. consume(_RECKKLAMMER);
  1663. end
  1664. else if try_to_consume(_LKLAMMER) then
  1665. begin
  1666. repeat
  1667. comp_expr(true,false);
  1668. until not try_to_consume(_COMMA);
  1669. consume(_RKLAMMER);
  1670. end
  1671. else
  1672. break;
  1673. until false;
  1674. end;
  1675. procedure handle_variantarray;
  1676. var
  1677. p4 : tnode;
  1678. newstatement : tstatementnode;
  1679. tempresultvariant,
  1680. temp : ttempcreatenode;
  1681. paras : tcallparanode;
  1682. newblock : tnode;
  1683. countindices : aint;
  1684. begin
  1685. { create statements with call initialize the arguments and
  1686. call fpc_dynarr_setlength }
  1687. newblock:=internalstatements(newstatement);
  1688. { get temp for array of indicies,
  1689. we set the real size later }
  1690. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1691. addstatement(newstatement,temp);
  1692. countindices:=0;
  1693. repeat
  1694. p4:=comp_expr(true,false);
  1695. addstatement(newstatement,cassignmentnode.create(
  1696. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1697. inc(countindices);
  1698. until not try_to_consume(_COMMA);
  1699. { set real size }
  1700. temp.size:=countindices*s32inttype.size;
  1701. consume(_RECKKLAMMER);
  1702. { we need only a write access if a := follows }
  1703. if token=_ASSIGNMENT then
  1704. begin
  1705. consume(_ASSIGNMENT);
  1706. p4:=comp_expr(true,false);
  1707. { create call to fpc_vararray_put }
  1708. paras:=ccallparanode.create(cordconstnode.create
  1709. (countindices,s32inttype,true),
  1710. ccallparanode.create(caddrnode.create_internal
  1711. (ctemprefnode.create(temp)),
  1712. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1713. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1714. ,nil))));
  1715. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1716. addstatement(newstatement,ctempdeletenode.create(temp));
  1717. end
  1718. else
  1719. begin
  1720. { create temp for result }
  1721. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1722. addstatement(newstatement,tempresultvariant);
  1723. { create call to fpc_vararray_get }
  1724. paras:=ccallparanode.create(cordconstnode.create
  1725. (countindices,s32inttype,true),
  1726. ccallparanode.create(caddrnode.create_internal
  1727. (ctemprefnode.create(temp)),
  1728. ccallparanode.create(p1,
  1729. ccallparanode.create(
  1730. ctemprefnode.create(tempresultvariant)
  1731. ,nil))));
  1732. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1733. addstatement(newstatement,ctempdeletenode.create(temp));
  1734. { the last statement should return the value as
  1735. location and type, this is done be referencing the
  1736. temp and converting it first from a persistent temp to
  1737. normal temp }
  1738. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1739. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1740. end;
  1741. p1:=newblock;
  1742. end;
  1743. function parse_array_constructor(arrdef:tarraydef): tnode;
  1744. var
  1745. newstatement,assstatement:tstatementnode;
  1746. arrnode:ttempcreatenode;
  1747. temp2:ttempcreatenode;
  1748. assnode,paranode:tnode;
  1749. paracount:integer;
  1750. begin
  1751. result:=internalstatements(newstatement);
  1752. { create temp for result }
  1753. arrnode:=ctempcreatenode.create(arrdef,arrdef.size,tt_persistent,true);
  1754. addstatement(newstatement,arrnode);
  1755. paracount:=0;
  1756. { check arguments and create an assignment calls }
  1757. if try_to_consume(_LKLAMMER) then
  1758. begin
  1759. assnode:=internalstatements(assstatement);
  1760. repeat
  1761. { arr[i] := param_i }
  1762. addstatement(assstatement,
  1763. cassignmentnode.create(
  1764. cvecnode.create(
  1765. ctemprefnode.create(arrnode),
  1766. cordconstnode.create(paracount,arrdef.rangedef,false)),
  1767. comp_expr(true,false)));
  1768. inc(paracount);
  1769. until not try_to_consume(_COMMA);
  1770. consume(_RKLAMMER);
  1771. end
  1772. else
  1773. assnode:=nil;
  1774. { get temp for array of lengths }
  1775. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1776. addstatement(newstatement,temp2);
  1777. { one dimensional }
  1778. addstatement(newstatement,cassignmentnode.create(
  1779. ctemprefnode.create_offset(temp2,0),
  1780. cordconstnode.create
  1781. (paracount,s32inttype,true)));
  1782. { create call to fpc_dynarr_setlength }
  1783. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1784. ccallparanode.create(caddrnode.create_internal
  1785. (ctemprefnode.create(temp2)),
  1786. ccallparanode.create(cordconstnode.create
  1787. (1,s32inttype,true),
  1788. ccallparanode.create(caddrnode.create_internal
  1789. (crttinode.create(tstoreddef(arrdef),initrtti,rdt_normal)),
  1790. ccallparanode.create(
  1791. ctypeconvnode.create_internal(
  1792. ctemprefnode.create(arrnode),voidpointertype),
  1793. nil))))
  1794. ));
  1795. { add assignment statememnts }
  1796. addstatement(newstatement,ctempdeletenode.create(temp2));
  1797. if assigned(assnode) then
  1798. addstatement(newstatement,assnode);
  1799. { the last statement should return the value as
  1800. location and type, this is done be referencing the
  1801. temp and converting it first from a persistent temp to
  1802. normal temp }
  1803. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1804. addstatement(newstatement,ctemprefnode.create(arrnode));
  1805. end;
  1806. var
  1807. protsym : tpropertysym;
  1808. p2,p3 : tnode;
  1809. srsym : tsym;
  1810. srsymtable : TSymtable;
  1811. structh : tabstractrecorddef;
  1812. { shouldn't be used that often, so the extra overhead is ok to save
  1813. stack space }
  1814. dispatchstring : ansistring;
  1815. nodechanged : boolean;
  1816. calltype: tdispcalltype;
  1817. label
  1818. skipreckklammercheck;
  1819. begin
  1820. result:=false;
  1821. again:=true;
  1822. while again do
  1823. begin
  1824. { we need the resultdef }
  1825. do_typecheckpass_changed(p1,nodechanged);
  1826. result:=result or nodechanged;
  1827. if codegenerror then
  1828. begin
  1829. recoverconsume_postfixops;
  1830. exit;
  1831. end;
  1832. { handle token }
  1833. case token of
  1834. _CARET:
  1835. begin
  1836. consume(_CARET);
  1837. { support tp/mac procvar^ if the procvar returns a
  1838. pointer type }
  1839. if ((m_tp_procvar in current_settings.modeswitches) or
  1840. (m_mac_procvar in current_settings.modeswitches)) and
  1841. (p1.resultdef.typ=procvardef) and
  1842. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1843. begin
  1844. p1:=ccallnode.create_procvar(nil,p1);
  1845. typecheckpass(p1);
  1846. end;
  1847. if (p1.resultdef.typ<>pointerdef) then
  1848. begin
  1849. { ^ as binary operator is a problem!!!! (FK) }
  1850. again:=false;
  1851. Message(parser_e_invalid_qualifier);
  1852. recoverconsume_postfixops;
  1853. p1.destroy;
  1854. p1:=cerrornode.create;
  1855. end
  1856. else
  1857. p1:=cderefnode.create(p1);
  1858. end;
  1859. _LECKKLAMMER:
  1860. begin
  1861. if is_class_or_interface_or_object(p1.resultdef) or
  1862. is_dispinterface(p1.resultdef) or is_record(p1.resultdef) then
  1863. begin
  1864. { default property }
  1865. protsym:=search_default_property(tabstractrecorddef(p1.resultdef));
  1866. if not(assigned(protsym)) then
  1867. begin
  1868. p1.destroy;
  1869. p1:=cerrornode.create;
  1870. again:=false;
  1871. message(parser_e_no_default_property_available);
  1872. end
  1873. else
  1874. begin
  1875. { The property symbol is referenced indirect }
  1876. protsym.IncRefCount;
  1877. handle_propertysym(protsym,protsym.owner,p1);
  1878. end;
  1879. end
  1880. else
  1881. begin
  1882. consume(_LECKKLAMMER);
  1883. repeat
  1884. { in all of the cases below, p1 is changed }
  1885. case p1.resultdef.typ of
  1886. pointerdef:
  1887. begin
  1888. { support delphi autoderef }
  1889. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1890. (m_autoderef in current_settings.modeswitches) then
  1891. p1:=cderefnode.create(p1);
  1892. p2:=comp_expr(true,false);
  1893. { Support Pbytevar[0..9] which returns array [0..9].}
  1894. if try_to_consume(_POINTPOINT) then
  1895. p2:=crangenode.create(p2,comp_expr(true,false));
  1896. p1:=cvecnode.create(p1,p2);
  1897. end;
  1898. variantdef:
  1899. begin
  1900. handle_variantarray;
  1901. { the RECKKLAMMER is already read }
  1902. goto skipreckklammercheck;
  1903. end;
  1904. stringdef :
  1905. begin
  1906. p2:=comp_expr(true,false);
  1907. { Support string[0..9] which returns array [0..9] of char.}
  1908. if try_to_consume(_POINTPOINT) then
  1909. p2:=crangenode.create(p2,comp_expr(true,false));
  1910. p1:=cvecnode.create(p1,p2);
  1911. end;
  1912. arraydef:
  1913. begin
  1914. p2:=comp_expr(true,false);
  1915. { support SEG:OFS for go32v2 Mem[] }
  1916. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1917. (p1.nodetype=loadn) and
  1918. assigned(tloadnode(p1).symtableentry) and
  1919. assigned(tloadnode(p1).symtableentry.owner.name) and
  1920. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1921. ((tloadnode(p1).symtableentry.name='MEM') or
  1922. (tloadnode(p1).symtableentry.name='MEMW') or
  1923. (tloadnode(p1).symtableentry.name='MEML')) then
  1924. begin
  1925. if try_to_consume(_COLON) then
  1926. begin
  1927. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1928. p2:=comp_expr(true,false);
  1929. p2:=caddnode.create(addn,p2,p3);
  1930. if try_to_consume(_POINTPOINT) then
  1931. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1932. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true,false),p3.getcopy));
  1933. p1:=cvecnode.create(p1,p2);
  1934. include(tvecnode(p1).flags,nf_memseg);
  1935. include(tvecnode(p1).flags,nf_memindex);
  1936. end
  1937. else
  1938. begin
  1939. if try_to_consume(_POINTPOINT) then
  1940. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1941. p2:=crangenode.create(p2,comp_expr(true,false));
  1942. p1:=cvecnode.create(p1,p2);
  1943. include(tvecnode(p1).flags,nf_memindex);
  1944. end;
  1945. end
  1946. else
  1947. begin
  1948. if try_to_consume(_POINTPOINT) then
  1949. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1950. p2:=crangenode.create(p2,comp_expr(true,false));
  1951. p1:=cvecnode.create(p1,p2);
  1952. end;
  1953. end;
  1954. else
  1955. begin
  1956. if p1.resultdef.typ<>undefineddef then
  1957. Message(parser_e_invalid_qualifier);
  1958. p1.destroy;
  1959. p1:=cerrornode.create;
  1960. comp_expr(true,false);
  1961. again:=false;
  1962. end;
  1963. end;
  1964. do_typecheckpass(p1);
  1965. until not try_to_consume(_COMMA);
  1966. consume(_RECKKLAMMER);
  1967. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1968. skipreckklammercheck:
  1969. end;
  1970. end;
  1971. _POINT :
  1972. begin
  1973. consume(_POINT);
  1974. if (p1.resultdef.typ=pointerdef) and
  1975. (m_autoderef in current_settings.modeswitches) and
  1976. { don't auto-deref objc.id, because then the code
  1977. below for supporting id.anyobjcmethod isn't triggered }
  1978. (p1.resultdef<>objc_idtype) then
  1979. begin
  1980. p1:=cderefnode.create(p1);
  1981. do_typecheckpass(p1);
  1982. end;
  1983. { procvar.<something> can never mean anything so always
  1984. try to call it in case it returns a record/object/... }
  1985. maybe_call_procvar(p1,false);
  1986. case p1.resultdef.typ of
  1987. recorddef:
  1988. begin
  1989. if token=_ID then
  1990. begin
  1991. structh:=tabstractrecorddef(p1.resultdef);
  1992. searchsym_in_record(structh,pattern,srsym,srsymtable);
  1993. if assigned(srsym) then
  1994. begin
  1995. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1996. consume(_ID);
  1997. do_member_read(structh,getaddr,srsym,p1,again,[]);
  1998. end
  1999. else
  2000. begin
  2001. Message1(sym_e_id_no_member,orgpattern);
  2002. p1.destroy;
  2003. p1:=cerrornode.create;
  2004. { try to clean up }
  2005. consume(_ID);
  2006. end;
  2007. end
  2008. else
  2009. consume(_ID);
  2010. end;
  2011. enumdef:
  2012. begin
  2013. if token=_ID then
  2014. begin
  2015. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  2016. p1.destroy;
  2017. if assigned(srsym) and (srsym.typ=enumsym) then
  2018. begin
  2019. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2020. p1:=genenumnode(tenumsym(srsym));
  2021. end
  2022. else
  2023. begin
  2024. Message1(sym_e_id_no_member,orgpattern);
  2025. p1:=cerrornode.create;
  2026. end;
  2027. end;
  2028. consume(_ID);
  2029. end;
  2030. arraydef:
  2031. begin
  2032. if is_dynamic_array(p1.resultdef) then
  2033. begin
  2034. if token=_ID then
  2035. begin
  2036. if pattern='CREATE' then
  2037. begin
  2038. consume(_ID);
  2039. p2:=parse_array_constructor(tarraydef(p1.resultdef));
  2040. p1.destroy;
  2041. p1:=p2;
  2042. end
  2043. else
  2044. begin
  2045. Message2(scan_f_syn_expected,'CREATE',pattern);
  2046. p1.destroy;
  2047. p1:=cerrornode.create;
  2048. consume(_ID);
  2049. end;
  2050. end;
  2051. end
  2052. else
  2053. begin
  2054. Message(parser_e_invalid_qualifier);
  2055. p1.destroy;
  2056. p1:=cerrornode.create;
  2057. consume(_ID);
  2058. end;
  2059. end;
  2060. variantdef:
  2061. begin
  2062. { dispatch call? }
  2063. { lhs := v.ident[parameters] -> property get
  2064. lhs := v.ident(parameters) -> method call
  2065. v.ident[parameters] := rhs -> property put
  2066. v.ident(parameters) := rhs -> also property put }
  2067. if token=_ID then
  2068. begin
  2069. dispatchstring:=orgpattern;
  2070. consume(_ID);
  2071. calltype:=dct_method;
  2072. if try_to_consume(_LKLAMMER) then
  2073. begin
  2074. p2:=parse_paras(false,true,_RKLAMMER);
  2075. consume(_RKLAMMER);
  2076. end
  2077. else if try_to_consume(_LECKKLAMMER) then
  2078. begin
  2079. p2:=parse_paras(false,true,_RECKKLAMMER);
  2080. consume(_RECKKLAMMER);
  2081. calltype:=dct_propget;
  2082. end
  2083. else
  2084. p2:=nil;
  2085. { property setter? }
  2086. if (token=_ASSIGNMENT) and not(afterassignment) then
  2087. begin
  2088. consume(_ASSIGNMENT);
  2089. { read the expression }
  2090. p3:=comp_expr(true,false);
  2091. { concat value parameter too }
  2092. p2:=ccallparanode.create(p3,p2);
  2093. p1:=translate_disp_call(p1,p2,dct_propput,dispatchstring,0,voidtype);
  2094. end
  2095. else
  2096. { this is only an approximation
  2097. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  2098. if afterassignment or in_args or (token<>_SEMICOLON) then
  2099. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,cvarianttype)
  2100. else
  2101. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,voidtype);
  2102. end
  2103. else { Error }
  2104. Consume(_ID);
  2105. end;
  2106. classrefdef:
  2107. begin
  2108. if token=_ID then
  2109. begin
  2110. structh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  2111. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,true);
  2112. if assigned(srsym) then
  2113. begin
  2114. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2115. consume(_ID);
  2116. do_member_read(structh,getaddr,srsym,p1,again,[]);
  2117. end
  2118. else
  2119. begin
  2120. Message1(sym_e_id_no_member,orgpattern);
  2121. p1.destroy;
  2122. p1:=cerrornode.create;
  2123. { try to clean up }
  2124. consume(_ID);
  2125. end;
  2126. end
  2127. else { Error }
  2128. Consume(_ID);
  2129. end;
  2130. objectdef:
  2131. begin
  2132. if token=_ID then
  2133. begin
  2134. structh:=tobjectdef(p1.resultdef);
  2135. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,true);
  2136. if assigned(srsym) then
  2137. begin
  2138. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2139. consume(_ID);
  2140. do_member_read(structh,getaddr,srsym,p1,again,[]);
  2141. end
  2142. else
  2143. begin
  2144. Message1(sym_e_id_no_member,orgpattern);
  2145. p1.destroy;
  2146. p1:=cerrornode.create;
  2147. { try to clean up }
  2148. consume(_ID);
  2149. end;
  2150. end
  2151. else { Error }
  2152. Consume(_ID);
  2153. end;
  2154. pointerdef:
  2155. begin
  2156. if (p1.resultdef=objc_idtype) then
  2157. begin
  2158. { objc's id type can be used to call any
  2159. Objective-C method of any Objective-C class
  2160. type that's currently in scope }
  2161. if search_objc_method(pattern,srsym,srsymtable) then
  2162. begin
  2163. consume(_ID);
  2164. do_proc_call(srsym,srsymtable,nil,
  2165. (getaddr and not(token in [_CARET,_POINT])),
  2166. again,p1,[cnf_objc_id_call]);
  2167. { we need to know which procedure is called }
  2168. do_typecheckpass(p1);
  2169. end
  2170. else
  2171. begin
  2172. consume(_ID);
  2173. Message(parser_e_methode_id_expected);
  2174. end;
  2175. end
  2176. else
  2177. begin
  2178. Message(parser_e_invalid_qualifier);
  2179. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2180. Message(parser_h_maybe_deref_caret_missing);
  2181. end
  2182. end;
  2183. else
  2184. begin
  2185. if p1.resultdef.typ<>undefineddef then
  2186. Message(parser_e_invalid_qualifier);
  2187. p1.destroy;
  2188. p1:=cerrornode.create;
  2189. { Error }
  2190. consume(_ID);
  2191. end;
  2192. end;
  2193. end;
  2194. else
  2195. begin
  2196. { is this a procedure variable ? }
  2197. if assigned(p1.resultdef) and
  2198. (p1.resultdef.typ=procvardef) then
  2199. begin
  2200. { Typenode for typecasting or expecting a procvar }
  2201. if (p1.nodetype=typen) or
  2202. (
  2203. assigned(getprocvardef) and
  2204. equal_defs(p1.resultdef,getprocvardef)
  2205. ) then
  2206. begin
  2207. if try_to_consume(_LKLAMMER) then
  2208. begin
  2209. p1:=comp_expr(true,false);
  2210. consume(_RKLAMMER);
  2211. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2212. end
  2213. else
  2214. again:=false
  2215. end
  2216. else
  2217. begin
  2218. if try_to_consume(_LKLAMMER) then
  2219. begin
  2220. p2:=parse_paras(false,false,_RKLAMMER);
  2221. consume(_RKLAMMER);
  2222. p1:=ccallnode.create_procvar(p2,p1);
  2223. { proc():= is never possible }
  2224. if token=_ASSIGNMENT then
  2225. begin
  2226. Message(parser_e_illegal_expression);
  2227. p1.free;
  2228. p1:=cerrornode.create;
  2229. again:=false;
  2230. end;
  2231. end
  2232. else
  2233. again:=false;
  2234. end;
  2235. end
  2236. else
  2237. again:=false;
  2238. end;
  2239. end;
  2240. { we only try again if p1 was changed }
  2241. if again or
  2242. (p1.nodetype=errorn) then
  2243. result:=true;
  2244. end; { while again }
  2245. end;
  2246. {---------------------------------------------
  2247. Factor (Main)
  2248. ---------------------------------------------}
  2249. var
  2250. l : longint;
  2251. ic : int64;
  2252. qc : qword;
  2253. p1 : tnode;
  2254. code : integer;
  2255. srsym : tsym;
  2256. srsymtable : TSymtable;
  2257. pd : tprocdef;
  2258. hclassdef : tobjectdef;
  2259. d : bestreal;
  2260. cur : currency;
  2261. hs,hsorg : string;
  2262. hdef : tdef;
  2263. filepos : tfileposinfo;
  2264. callflags : tcallnodeflags;
  2265. again,
  2266. updatefpos,
  2267. nodechanged : boolean;
  2268. begin
  2269. { can't keep a copy of p1 and compare pointers afterwards, because
  2270. p1 may be freed and reallocated in the same place! }
  2271. updatefpos:=false;
  2272. p1:=nil;
  2273. filepos:=current_tokenpos;
  2274. again:=false;
  2275. if token=_ID then
  2276. begin
  2277. again:=true;
  2278. { Handle references to self }
  2279. if (idtoken=_SELF) and
  2280. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2281. assigned(current_structdef) then
  2282. begin
  2283. p1:=load_self_node;
  2284. consume(_ID);
  2285. again:=true;
  2286. end
  2287. else
  2288. factor_read_id(p1,again);
  2289. if assigned(p1) then
  2290. begin
  2291. { factor_read_id will set the filepos to after the id,
  2292. and in case of _SELF the filepos will already be the
  2293. same as filepos (so setting it again doesn't hurt). }
  2294. p1.fileinfo:=filepos;
  2295. filepos:=current_tokenpos;
  2296. end;
  2297. { handle post fix operators }
  2298. updatefpos:=postfixoperators(p1,again);
  2299. end
  2300. else
  2301. begin
  2302. updatefpos:=true;
  2303. case token of
  2304. _RETURN :
  2305. begin
  2306. consume(_RETURN);
  2307. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2308. p1 := cexitnode.create(comp_expr(true,false))
  2309. else
  2310. p1 := cexitnode.create(nil);
  2311. end;
  2312. _INHERITED :
  2313. begin
  2314. again:=true;
  2315. consume(_INHERITED);
  2316. if assigned(current_procinfo) and
  2317. assigned(current_structdef) and
  2318. (current_structdef.typ=objectdef) then
  2319. begin
  2320. { for record helpers in mode Delphi "inherited" is not
  2321. allowed }
  2322. if is_objectpascal_helper(current_structdef) and
  2323. (m_delphi in current_settings.modeswitches) and
  2324. is_record(tobjectdef(current_structdef).extendeddef) then
  2325. Message(parser_e_inherited_not_in_record);
  2326. hclassdef:=tobjectdef(current_structdef).childof;
  2327. { Objective-C categories *replace* methods in the class
  2328. they extend, or add methods to it. So calling an
  2329. inherited method always calls the method inherited from
  2330. the parent of the extended class }
  2331. if is_objccategory(current_structdef) then
  2332. hclassdef:=hclassdef.childof;
  2333. { if inherited; only then we need the method with
  2334. the same name }
  2335. if token <> _ID then
  2336. begin
  2337. hs:=current_procinfo.procdef.procsym.name;
  2338. hsorg:=current_procinfo.procdef.procsym.realname;
  2339. anon_inherited:=true;
  2340. { For message methods we need to search using the message
  2341. number or string }
  2342. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2343. srdef:=nil;
  2344. if (po_msgint in pd.procoptions) then
  2345. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2346. else
  2347. if (po_msgstr in pd.procoptions) then
  2348. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2349. else
  2350. { helpers have their own ways of dealing with inherited }
  2351. if is_objectpascal_helper(current_structdef) then
  2352. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,true)
  2353. else
  2354. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable,true);
  2355. end
  2356. else
  2357. begin
  2358. hs:=pattern;
  2359. hsorg:=orgpattern;
  2360. consume(_ID);
  2361. anon_inherited:=false;
  2362. { helpers have their own ways of dealing with inherited }
  2363. if is_objectpascal_helper(current_structdef) then
  2364. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,true)
  2365. else
  2366. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable,true);
  2367. end;
  2368. if assigned(srsym) then
  2369. begin
  2370. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2371. { load the procdef from the inherited class and
  2372. not from self }
  2373. case srsym.typ of
  2374. procsym:
  2375. begin
  2376. if is_objectpascal_helper(current_structdef) then
  2377. begin
  2378. { for a helper load the procdef either from the
  2379. extended type, from the parent helper or from
  2380. the extended type of the parent helper
  2381. depending on the def the found symbol belongs
  2382. to }
  2383. if (srsym.Owner.defowner.typ=objectdef) and
  2384. is_objectpascal_helper(tobjectdef(srsym.Owner.defowner)) then
  2385. if current_structdef.is_related(tdef(srsym.Owner.defowner)) and
  2386. assigned(tobjectdef(current_structdef).childof) then
  2387. hdef:=tobjectdef(current_structdef).childof
  2388. else
  2389. hdef:=tobjectdef(srsym.Owner.defowner).extendeddef
  2390. else
  2391. hdef:=tdef(srsym.Owner.defowner);
  2392. end
  2393. else
  2394. hdef:=hclassdef;
  2395. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2396. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2397. hdef:=tclassrefdef.create(hdef);
  2398. p1:=ctypenode.create(hdef);
  2399. { we need to allow helpers here }
  2400. ttypenode(p1).helperallowed:=true;
  2401. end;
  2402. propertysym:
  2403. ;
  2404. else
  2405. begin
  2406. Message(parser_e_methode_id_expected);
  2407. p1:=cerrornode.create;
  2408. end;
  2409. end;
  2410. callflags:=[cnf_inherited];
  2411. if anon_inherited then
  2412. include(callflags,cnf_anon_inherited);
  2413. do_member_read(hclassdef,getaddr,srsym,p1,again,callflags);
  2414. end
  2415. else
  2416. begin
  2417. if anon_inherited then
  2418. begin
  2419. { For message methods we need to call DefaultHandler }
  2420. if (po_msgint in pd.procoptions) or
  2421. (po_msgstr in pd.procoptions) then
  2422. begin
  2423. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable,true);
  2424. if not assigned(srsym) or
  2425. (srsym.typ<>procsym) then
  2426. internalerror(200303171);
  2427. p1:=nil;
  2428. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2429. end
  2430. else
  2431. begin
  2432. { we need to ignore the inherited; }
  2433. p1:=cnothingnode.create;
  2434. end;
  2435. end
  2436. else
  2437. begin
  2438. Message1(sym_e_id_no_member,hsorg);
  2439. p1:=cerrornode.create;
  2440. end;
  2441. again:=false;
  2442. end;
  2443. { turn auto inheriting off }
  2444. anon_inherited:=false;
  2445. end
  2446. else
  2447. begin
  2448. { in case of records we use a more clear error message }
  2449. if assigned(current_structdef) and
  2450. (current_structdef.typ=recorddef) then
  2451. Message(parser_e_inherited_not_in_record)
  2452. else
  2453. Message(parser_e_generic_methods_only_in_methods);
  2454. again:=false;
  2455. p1:=cerrornode.create;
  2456. end;
  2457. postfixoperators(p1,again);
  2458. end;
  2459. _INTCONST :
  2460. begin
  2461. {Try first wether the value fits in an int64.}
  2462. val(pattern,ic,code);
  2463. if code=0 then
  2464. begin
  2465. consume(_INTCONST);
  2466. int_to_type(ic,hdef);
  2467. p1:=cordconstnode.create(ic,hdef,true);
  2468. end
  2469. else
  2470. begin
  2471. { try qword next }
  2472. val(pattern,qc,code);
  2473. if code=0 then
  2474. begin
  2475. consume(_INTCONST);
  2476. int_to_type(qc,hdef);
  2477. p1:=cordconstnode.create(qc,hdef,true);
  2478. end;
  2479. end;
  2480. if code<>0 then
  2481. begin
  2482. { finally float }
  2483. val(pattern,d,code);
  2484. if code<>0 then
  2485. begin
  2486. Message(parser_e_invalid_integer);
  2487. consume(_INTCONST);
  2488. l:=1;
  2489. p1:=cordconstnode.create(l,sinttype,true);
  2490. end
  2491. else
  2492. begin
  2493. consume(_INTCONST);
  2494. p1:=crealconstnode.create(d,pbestrealtype^);
  2495. end;
  2496. end
  2497. else
  2498. { the necessary range checking has already been done by val }
  2499. tordconstnode(p1).rangecheck:=false;
  2500. end;
  2501. _REALNUMBER :
  2502. begin
  2503. val(pattern,d,code);
  2504. if code<>0 then
  2505. begin
  2506. Message(parser_e_error_in_real);
  2507. d:=1.0;
  2508. end;
  2509. consume(_REALNUMBER);
  2510. {$ifdef FPC_REAL2REAL_FIXED}
  2511. if current_settings.fputype=fpu_none then
  2512. Message(parser_e_unsupported_real);
  2513. if (current_settings.minfpconstprec=s32real) and
  2514. (d = single(d)) then
  2515. p1:=crealconstnode.create(d,s32floattype)
  2516. else if (current_settings.minfpconstprec=s64real) and
  2517. (d = double(d)) then
  2518. p1:=crealconstnode.create(d,s64floattype)
  2519. else
  2520. {$endif FPC_REAL2REAL_FIXED}
  2521. p1:=crealconstnode.create(d,pbestrealtype^);
  2522. {$ifdef FPC_HAS_STR_CURRENCY}
  2523. val(pattern,cur,code);
  2524. if code=0 then
  2525. trealconstnode(p1).value_currency:=cur;
  2526. {$endif FPC_HAS_STR_CURRENCY}
  2527. end;
  2528. _STRING :
  2529. begin
  2530. string_dec(hdef,true);
  2531. { STRING can be also a type cast }
  2532. if try_to_consume(_LKLAMMER) then
  2533. begin
  2534. p1:=comp_expr(true,false);
  2535. consume(_RKLAMMER);
  2536. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2537. { handle postfix operators here e.g. string(a)[10] }
  2538. again:=true;
  2539. postfixoperators(p1,again);
  2540. end
  2541. else
  2542. p1:=ctypenode.create(hdef);
  2543. end;
  2544. _FILE :
  2545. begin
  2546. hdef:=cfiletype;
  2547. consume(_FILE);
  2548. { FILE can be also a type cast }
  2549. if try_to_consume(_LKLAMMER) then
  2550. begin
  2551. p1:=comp_expr(true,false);
  2552. consume(_RKLAMMER);
  2553. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2554. { handle postfix operators here e.g. string(a)[10] }
  2555. again:=true;
  2556. postfixoperators(p1,again);
  2557. end
  2558. else
  2559. begin
  2560. p1:=ctypenode.create(hdef);
  2561. end;
  2562. end;
  2563. _CSTRING :
  2564. begin
  2565. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern));
  2566. consume(_CSTRING);
  2567. end;
  2568. _CCHAR :
  2569. begin
  2570. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2571. consume(_CCHAR);
  2572. end;
  2573. _CWSTRING:
  2574. begin
  2575. p1:=cstringconstnode.createwstr(patternw);
  2576. consume(_CWSTRING);
  2577. end;
  2578. _CWCHAR:
  2579. begin
  2580. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2581. consume(_CWCHAR);
  2582. end;
  2583. _KLAMMERAFFE :
  2584. begin
  2585. consume(_KLAMMERAFFE);
  2586. got_addrn:=true;
  2587. { support both @<x> and @(<x>) }
  2588. if try_to_consume(_LKLAMMER) then
  2589. begin
  2590. p1:=factor(true,false);
  2591. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2592. begin
  2593. again:=true;
  2594. postfixoperators(p1,again);
  2595. end
  2596. else
  2597. consume(_RKLAMMER);
  2598. end
  2599. else
  2600. p1:=factor(true,false);
  2601. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2602. begin
  2603. again:=true;
  2604. postfixoperators(p1,again);
  2605. end;
  2606. got_addrn:=false;
  2607. p1:=caddrnode.create(p1);
  2608. p1.fileinfo:=filepos;
  2609. if cs_typed_addresses in current_settings.localswitches then
  2610. include(p1.flags,nf_typedaddr);
  2611. { Store the procvar that we are expecting, the
  2612. addrn will use the information to find the correct
  2613. procdef or it will return an error }
  2614. if assigned(getprocvardef) and
  2615. (taddrnode(p1).left.nodetype = loadn) then
  2616. taddrnode(p1).getprocvardef:=getprocvardef;
  2617. end;
  2618. _LKLAMMER :
  2619. begin
  2620. consume(_LKLAMMER);
  2621. p1:=comp_expr(true,false);
  2622. consume(_RKLAMMER);
  2623. { it's not a good solution }
  2624. { but (a+b)^ makes some problems }
  2625. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2626. begin
  2627. again:=true;
  2628. postfixoperators(p1,again);
  2629. end;
  2630. end;
  2631. _LECKKLAMMER :
  2632. begin
  2633. consume(_LECKKLAMMER);
  2634. p1:=factor_read_set;
  2635. consume(_RECKKLAMMER);
  2636. end;
  2637. _PLUS :
  2638. begin
  2639. consume(_PLUS);
  2640. p1:=factor(false,false);
  2641. p1:=cunaryplusnode.create(p1);
  2642. end;
  2643. _MINUS :
  2644. begin
  2645. consume(_MINUS);
  2646. if (token = _INTCONST) and not(m_isolike_unary_minus in current_settings.modeswitches) then
  2647. begin
  2648. { ugly hack, but necessary to be able to parse }
  2649. { -9223372036854775808 as int64 (JM) }
  2650. pattern := '-'+pattern;
  2651. p1:=sub_expr(oppower,false,false);
  2652. { -1 ** 4 should be - (1 ** 4) and not
  2653. (-1) ** 4
  2654. This was the reason of tw0869.pp test failure PM }
  2655. if p1.nodetype=starstarn then
  2656. begin
  2657. if tbinarynode(p1).left.nodetype=ordconstn then
  2658. begin
  2659. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2660. p1:=cunaryminusnode.create(p1);
  2661. end
  2662. else if tbinarynode(p1).left.nodetype=realconstn then
  2663. begin
  2664. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2665. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2666. p1:=cunaryminusnode.create(p1);
  2667. end
  2668. else
  2669. internalerror(20021029);
  2670. end;
  2671. end
  2672. else
  2673. begin
  2674. if m_isolike_unary_minus in current_settings.modeswitches then
  2675. p1:=sub_expr(opmultiply,false,false)
  2676. else
  2677. p1:=sub_expr(oppower,false,false);
  2678. p1:=cunaryminusnode.create(p1);
  2679. end;
  2680. end;
  2681. _OP_NOT :
  2682. begin
  2683. consume(_OP_NOT);
  2684. p1:=factor(false,false);
  2685. p1:=cnotnode.create(p1);
  2686. end;
  2687. _TRUE :
  2688. begin
  2689. consume(_TRUE);
  2690. p1:=cordconstnode.create(1,pasbool8type,false);
  2691. end;
  2692. _FALSE :
  2693. begin
  2694. consume(_FALSE);
  2695. p1:=cordconstnode.create(0,pasbool8type,false);
  2696. end;
  2697. _NIL :
  2698. begin
  2699. consume(_NIL);
  2700. p1:=cnilnode.create;
  2701. { It's really ugly code nil^, but delphi allows it }
  2702. if token in [_CARET] then
  2703. begin
  2704. again:=true;
  2705. postfixoperators(p1,again);
  2706. end;
  2707. end;
  2708. _OBJCPROTOCOL:
  2709. begin
  2710. { The @protocol keyword is used in two ways in Objective-C:
  2711. 1) to declare protocols (~ Object Pascal interfaces)
  2712. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2713. of a declared protocol
  2714. This code is for handling the second case. Because of 1),
  2715. we cannot simply use a system unit symbol.
  2716. }
  2717. consume(_OBJCPROTOCOL);
  2718. consume(_LKLAMMER);
  2719. p1:=factor(false,false);
  2720. consume(_RKLAMMER);
  2721. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2722. end;
  2723. else
  2724. begin
  2725. Message(parser_e_illegal_expression);
  2726. p1:=cerrornode.create;
  2727. { recover }
  2728. consume(token);
  2729. end;
  2730. end;
  2731. end;
  2732. { generate error node if no node is created }
  2733. if not assigned(p1) then
  2734. begin
  2735. {$ifdef EXTDEBUG}
  2736. Comment(V_Warning,'factor: p1=nil');
  2737. {$endif}
  2738. p1:=cerrornode.create;
  2739. updatefpos:=true;
  2740. end;
  2741. { get the resultdef for the node }
  2742. if (not assigned(p1.resultdef)) then
  2743. begin
  2744. do_typecheckpass_changed(p1,nodechanged);
  2745. updatefpos:=updatefpos or nodechanged;
  2746. end;
  2747. if assigned(p1) and
  2748. updatefpos then
  2749. p1.fileinfo:=filepos;
  2750. factor:=p1;
  2751. end;
  2752. {$maxfpuregisters default}
  2753. {****************************************************************************
  2754. Sub_Expr
  2755. ****************************************************************************}
  2756. const
  2757. { Warning these stay be ordered !! }
  2758. operator_levels:array[Toperator_precedence] of set of NOTOKEN..last_operator=
  2759. ([_LT,_LTE,_GT,_GTE,_EQ,_NE,_OP_IN],
  2760. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2761. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2762. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2763. [_STARSTAR] );
  2764. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean):tnode;
  2765. {Reads a subexpression while the operators are of the current precedence
  2766. level, or any higher level. Replaces the old term, simpl_expr and
  2767. simpl2_expr.}
  2768. var
  2769. p1,p2 : tnode;
  2770. oldt : Ttoken;
  2771. filepos : tfileposinfo;
  2772. begin
  2773. if pred_level=highest_precedence then
  2774. p1:=factor(false,typeonly)
  2775. else
  2776. p1:=sub_expr(succ(pred_level),true,typeonly);
  2777. repeat
  2778. if (token in [NOTOKEN..last_operator]) and
  2779. (token in operator_levels[pred_level]) and
  2780. ((token<>_EQ) or accept_equal) then
  2781. begin
  2782. oldt:=token;
  2783. filepos:=current_tokenpos;
  2784. consume(token);
  2785. if pred_level=highest_precedence then
  2786. p2:=factor(false,false)
  2787. else
  2788. p2:=sub_expr(succ(pred_level),true,typeonly);
  2789. case oldt of
  2790. _PLUS :
  2791. p1:=caddnode.create(addn,p1,p2);
  2792. _MINUS :
  2793. p1:=caddnode.create(subn,p1,p2);
  2794. _STAR :
  2795. p1:=caddnode.create(muln,p1,p2);
  2796. _SLASH :
  2797. p1:=caddnode.create(slashn,p1,p2);
  2798. _EQ:
  2799. p1:=caddnode.create(equaln,p1,p2);
  2800. _GT :
  2801. p1:=caddnode.create(gtn,p1,p2);
  2802. _LT :
  2803. p1:=caddnode.create(ltn,p1,p2);
  2804. _GTE :
  2805. p1:=caddnode.create(gten,p1,p2);
  2806. _LTE :
  2807. p1:=caddnode.create(lten,p1,p2);
  2808. _SYMDIF :
  2809. p1:=caddnode.create(symdifn,p1,p2);
  2810. _STARSTAR :
  2811. p1:=caddnode.create(starstarn,p1,p2);
  2812. _OP_AS :
  2813. p1:=casnode.create(p1,p2);
  2814. _OP_IN :
  2815. p1:=cinnode.create(p1,p2);
  2816. _OP_IS :
  2817. p1:=cisnode.create(p1,p2);
  2818. _OP_OR,
  2819. _PIPE {macpas only} :
  2820. begin
  2821. p1:=caddnode.create(orn,p1,p2);
  2822. if (oldt = _PIPE) then
  2823. include(p1.flags,nf_short_bool);
  2824. end;
  2825. _OP_AND,
  2826. _AMPERSAND {macpas only} :
  2827. begin
  2828. p1:=caddnode.create(andn,p1,p2);
  2829. if (oldt = _AMPERSAND) then
  2830. include(p1.flags,nf_short_bool);
  2831. end;
  2832. _OP_DIV :
  2833. p1:=cmoddivnode.create(divn,p1,p2);
  2834. _OP_NOT :
  2835. p1:=cnotnode.create(p1);
  2836. _OP_MOD :
  2837. p1:=cmoddivnode.create(modn,p1,p2);
  2838. _OP_SHL :
  2839. p1:=cshlshrnode.create(shln,p1,p2);
  2840. _OP_SHR :
  2841. p1:=cshlshrnode.create(shrn,p1,p2);
  2842. _OP_XOR :
  2843. p1:=caddnode.create(xorn,p1,p2);
  2844. _ASSIGNMENT :
  2845. p1:=cassignmentnode.create(p1,p2);
  2846. _NE :
  2847. p1:=caddnode.create(unequaln,p1,p2);
  2848. end;
  2849. p1.fileinfo:=filepos;
  2850. end
  2851. else
  2852. break;
  2853. until false;
  2854. sub_expr:=p1;
  2855. end;
  2856. function comp_expr(accept_equal,typeonly:boolean):tnode;
  2857. var
  2858. oldafterassignment : boolean;
  2859. p1 : tnode;
  2860. begin
  2861. oldafterassignment:=afterassignment;
  2862. afterassignment:=true;
  2863. p1:=sub_expr(opcompare,accept_equal,typeonly);
  2864. { get the resultdef for this expression }
  2865. if not assigned(p1.resultdef) then
  2866. do_typecheckpass(p1);
  2867. afterassignment:=oldafterassignment;
  2868. comp_expr:=p1;
  2869. end;
  2870. function expr(dotypecheck : boolean) : tnode;
  2871. var
  2872. p1,p2 : tnode;
  2873. filepos : tfileposinfo;
  2874. oldafterassignment,
  2875. updatefpos : boolean;
  2876. begin
  2877. oldafterassignment:=afterassignment;
  2878. p1:=sub_expr(opcompare,true,false);
  2879. { get the resultdef for this expression }
  2880. if not assigned(p1.resultdef) and
  2881. dotypecheck then
  2882. do_typecheckpass(p1);
  2883. filepos:=current_tokenpos;
  2884. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2885. afterassignment:=true;
  2886. updatefpos:=true;
  2887. case token of
  2888. _POINTPOINT :
  2889. begin
  2890. consume(_POINTPOINT);
  2891. p2:=sub_expr(opcompare,true,false);
  2892. p1:=crangenode.create(p1,p2);
  2893. end;
  2894. _ASSIGNMENT :
  2895. begin
  2896. consume(_ASSIGNMENT);
  2897. if (p1.resultdef.typ=procvardef) then
  2898. getprocvardef:=tprocvardef(p1.resultdef);
  2899. p2:=sub_expr(opcompare,true,false);
  2900. if assigned(getprocvardef) then
  2901. handle_procvar(getprocvardef,p2);
  2902. getprocvardef:=nil;
  2903. p1:=cassignmentnode.create(p1,p2);
  2904. end;
  2905. _PLUSASN :
  2906. begin
  2907. consume(_PLUSASN);
  2908. p2:=sub_expr(opcompare,true,false);
  2909. p1:=gen_c_style_operator(addn,p1,p2);
  2910. end;
  2911. _MINUSASN :
  2912. begin
  2913. consume(_MINUSASN);
  2914. p2:=sub_expr(opcompare,true,false);
  2915. p1:=gen_c_style_operator(subn,p1,p2);
  2916. end;
  2917. _STARASN :
  2918. begin
  2919. consume(_STARASN );
  2920. p2:=sub_expr(opcompare,true,false);
  2921. p1:=gen_c_style_operator(muln,p1,p2);
  2922. end;
  2923. _SLASHASN :
  2924. begin
  2925. consume(_SLASHASN );
  2926. p2:=sub_expr(opcompare,true,false);
  2927. p1:=gen_c_style_operator(slashn,p1,p2);
  2928. end;
  2929. else
  2930. updatefpos:=false;
  2931. end;
  2932. { get the resultdef for this expression }
  2933. if not assigned(p1.resultdef) and
  2934. dotypecheck then
  2935. do_typecheckpass(p1);
  2936. afterassignment:=oldafterassignment;
  2937. if updatefpos then
  2938. p1.fileinfo:=filepos;
  2939. expr:=p1;
  2940. end;
  2941. function get_intconst:TConstExprInt;
  2942. {Reads an expression, tries to evalute it and check if it is an integer
  2943. constant. Then the constant is returned.}
  2944. var
  2945. p:tnode;
  2946. begin
  2947. result:=0;
  2948. p:=comp_expr(true,false);
  2949. if not codegenerror then
  2950. begin
  2951. if (p.nodetype<>ordconstn) or
  2952. not(is_integer(p.resultdef)) then
  2953. Message(parser_e_illegal_expression)
  2954. else
  2955. result:=tordconstnode(p).value;
  2956. end;
  2957. p.free;
  2958. end;
  2959. function get_stringconst:string;
  2960. {Reads an expression, tries to evaluate it and checks if it is a string
  2961. constant. Then the constant is returned.}
  2962. var
  2963. p:tnode;
  2964. begin
  2965. get_stringconst:='';
  2966. p:=comp_expr(true,false);
  2967. if p.nodetype<>stringconstn then
  2968. begin
  2969. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2970. get_stringconst:=char(int64(tordconstnode(p).value))
  2971. else
  2972. Message(parser_e_illegal_expression);
  2973. end
  2974. else
  2975. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2976. p.free;
  2977. end;
  2978. end.