pexpr.pas 135 KB

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