pexpr.pas 139 KB

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