pexpr.pas 171 KB

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