pdecsub.pas 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  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 pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { scanner }
  24. tokens,
  25. { symtable }
  26. symconst,symtype,symdef,symsym;
  27. type
  28. tpdflag=(
  29. pd_body, { directive needs a body }
  30. pd_implemen, { directive can be used in implementation section }
  31. pd_interface, { directive can be used in interface section }
  32. pd_object, { directive can be used with object declaration }
  33. pd_record, { directive can be used with record declaration }
  34. pd_procvar, { directive can be used with procvar declaration }
  35. pd_notobject, { directive can not be used with object declaration }
  36. pd_notrecord, { directive can not be used with record declaration }
  37. pd_notobjintf, { directive can not be used with interface declaration }
  38. pd_notprocvar, { directive can not be used with procvar declaration }
  39. pd_dispinterface,{ directive can be used with dispinterface methods }
  40. pd_cppobject, { directive can be used with cppclass }
  41. pd_objcclass, { directive can be used with objcclass }
  42. pd_objcprot, { directive can be used with objcprotocol }
  43. pd_nothelper, { directive can not be used with record/class helper declaration }
  44. pd_javaclass, { directive can be used with Java class }
  45. pd_intfjava { directive can be used with Java interface }
  46. );
  47. tpdflags=set of tpdflag;
  48. tparse_proc_flag=(
  49. ppf_classmethod,
  50. ppf_generic,
  51. ppf_anonymous
  52. );
  53. tparse_proc_flags=set of tparse_proc_flag;
  54. function check_proc_directive(isprocvar:boolean):boolean;
  55. function proc_get_importname(pd:tprocdef):string;
  56. procedure proc_set_mangledname(pd:tprocdef);
  57. procedure parse_parameter_dec(pd:tabstractprocdef);
  58. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  59. procedure parse_proctype_directives(pd_or_invkdef:tdef);
  60. procedure parse_object_proc_directives(pd:tprocdef);
  61. procedure parse_record_proc_directives(pd:tprocdef);
  62. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;flags:tparse_proc_flags;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  63. function parse_proc_dec(flags:tparse_proc_flags;astruct:tabstractrecorddef):tprocdef;
  64. procedure parse_proc_dec_finish(pd:tprocdef;flags:tparse_proc_flags;astruct:tabstractrecorddef);
  65. { parse a record method declaration (not a (class) constructor/destructor) }
  66. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  67. { helper functions - they insert nested objects hierarchy to the symtablestack
  68. with object hierarchy
  69. }
  70. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  71. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  72. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  73. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  74. implementation
  75. uses
  76. SysUtils,
  77. { common }
  78. cutils,
  79. { global }
  80. globtype,globals,verbose,constexp,
  81. systems,
  82. cpuinfo,
  83. { assembler }
  84. aasmbase,
  85. { symtable }
  86. symbase,symcpu,symtable,symutil,defutil,defcmp,
  87. { parameter handling }
  88. paramgr,cpupara,
  89. { pass 1 }
  90. fmodule,node,htypechk,ncon,nld,
  91. objcutil,
  92. { parser }
  93. scanner,
  94. syscinfo,
  95. pbase,pexpr,ptype,pdecl,pparautl,pgenutil
  96. {$ifdef jvm}
  97. ,pjvm
  98. {$endif}
  99. ;
  100. const
  101. { Please leave this here, this module should NOT use
  102. these variables.
  103. Declaring it as string here results in an error when compiling (PFV) }
  104. current_procinfo = 'error';
  105. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  106. var
  107. _class,hp : tobjectdef;
  108. begin
  109. if obj.typ=recorddef then
  110. begin
  111. symtablestack.push(obj.symtable);
  112. result:=1;
  113. exit;
  114. end;
  115. result:=0;
  116. { insert class hierarchy in the reverse order }
  117. hp:=nil;
  118. repeat
  119. _class:=tobjectdef(obj);
  120. while _class.childof<>hp do
  121. _class:=_class.childof;
  122. hp:=_class;
  123. symtablestack.push(_class.symtable);
  124. inc(result);
  125. until hp=obj;
  126. end;
  127. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  128. begin
  129. result:=0;
  130. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  131. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  132. inc(result,push_child_hierarchy(obj));
  133. end;
  134. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  135. var
  136. _class : tobjectdef;
  137. begin
  138. if obj.typ=recorddef then
  139. begin
  140. symtablestack.pop(obj.symtable);
  141. result:=1;
  142. exit;
  143. end;
  144. result:=0;
  145. _class:=tobjectdef(obj);
  146. while assigned(_class) do
  147. begin
  148. symtablestack.pop(_class.symtable);
  149. _class:=_class.childof;
  150. inc(result);
  151. end;
  152. end;
  153. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  154. begin
  155. result:=pop_child_hierarchy(obj);
  156. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  157. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  158. end;
  159. procedure check_msg_para(p:TObject;arg:pointer);
  160. begin
  161. if (tsym(p).typ<>paravarsym) then
  162. exit;
  163. with tparavarsym(p) do
  164. begin
  165. { Count parameters }
  166. if (paranr>=10) then
  167. inc(plongint(arg)^);
  168. { First parameter must be var }
  169. if (paranr=10) and
  170. (varspez<>vs_var) then
  171. MessagePos(fileinfo,parser_e_ill_msg_param);
  172. end;
  173. end;
  174. procedure parse_parameter_dec(pd:tabstractprocdef);
  175. {
  176. handle_procvar needs the same changes
  177. }
  178. type
  179. tppv = (pv_none,pv_proc,pv_func);
  180. var
  181. sc : TFPObjectList;
  182. hdef : tdef;
  183. arrayelementdef : tdef;
  184. vs : tparavarsym;
  185. i : longint;
  186. srsym : tsym;
  187. pv : tprocvardef;
  188. varspez : Tvarspez;
  189. defaultvalue : tconstsym;
  190. defaultrequired : boolean;
  191. old_block_type : tblock_type;
  192. currparast : tparasymtable;
  193. parseprocvar : tppv;
  194. locationstr : string;
  195. paranr : integer;
  196. explicit_paraloc,
  197. need_array,
  198. is_univ: boolean;
  199. stoptions : TSingleTypeOptions;
  200. procedure handle_default_para_value;
  201. var
  202. convpd : tprocdef;
  203. doconv : tconverttype;
  204. nodetype : tnodetype;
  205. bt : tblock_type;
  206. begin
  207. { only allowed for types that can be represented by a
  208. constant expression }
  209. if try_to_consume(_EQ) then
  210. begin
  211. if (hdef.typ in [recorddef,variantdef,filedef,formaldef]) or
  212. is_object(hdef) or
  213. ((hdef.typ=arraydef) and
  214. not is_dynamic_array(hdef)) then
  215. Message1(type_e_invalid_default_value,FullTypeName(hdef,nil));
  216. vs:=tparavarsym(sc[0]);
  217. if sc.count>1 then
  218. Message(parser_e_default_value_only_one_para);
  219. if not(vs.varspez in [vs_value,vs_const,vs_constref]) then
  220. Message(parser_e_default_value_val_const);
  221. bt:=block_type;
  222. block_type:=bt_const;
  223. { prefix 'def' to the parameter name }
  224. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo,nodetype);
  225. block_type:=bt;
  226. if assigned(defaultvalue) then
  227. begin
  228. include(defaultvalue.symoptions,sp_internal);
  229. pd.parast.insertsym(defaultvalue);
  230. { check whether the default value is of the correct
  231. type }
  232. if compare_defs_ext(defaultvalue.constdef,hdef,nodetype,doconv,convpd,[])<=te_convert_operator then
  233. MessagePos2(defaultvalue.fileinfo,type_e_incompatible_types,FullTypeName(defaultvalue.constdef,hdef),FullTypeName(hdef,defaultvalue.constdef));
  234. end;
  235. defaultrequired:=true;
  236. end
  237. else
  238. begin
  239. if defaultrequired then
  240. Message1(parser_e_default_value_expected_for_para,vs.name);
  241. end;
  242. end;
  243. begin
  244. old_block_type:=block_type;
  245. explicit_paraloc:=false;
  246. consume(_LKLAMMER);
  247. { Delphi/Kylix supports nonsense like }
  248. { procedure p(); }
  249. if try_to_consume(_RKLAMMER) and
  250. not(m_tp7 in current_settings.modeswitches) then
  251. exit;
  252. { parsing a proc or procvar ? }
  253. currparast:=tparasymtable(pd.parast);
  254. { reset }
  255. sc:=TFPObjectList.create(false);
  256. defaultrequired:=false;
  257. paranr:=0;
  258. block_type:=bt_var;
  259. is_univ:=false;
  260. repeat
  261. parseprocvar:=pv_none;
  262. if try_to_consume(_VAR) then
  263. varspez:=vs_var
  264. else
  265. if try_to_consume(_CONST) then
  266. varspez:=vs_const
  267. else
  268. if (m_out in current_settings.modeswitches) and
  269. try_to_consume(_OUT) then
  270. varspez:=vs_out
  271. else
  272. if try_to_consume(_CONSTREF) then
  273. varspez:=vs_constref
  274. else
  275. if (m_mac in current_settings.modeswitches) and
  276. try_to_consume(_POINTPOINTPOINT) then
  277. begin
  278. include(pd.procoptions,po_varargs);
  279. break;
  280. end
  281. else
  282. if (m_nested_procvars in current_settings.modeswitches) and
  283. try_to_consume(_PROCEDURE) then
  284. begin
  285. parseprocvar:=pv_proc;
  286. varspez:=vs_const;
  287. end
  288. else
  289. if (m_nested_procvars in current_settings.modeswitches) and
  290. try_to_consume(_FUNCTION) then
  291. begin
  292. parseprocvar:=pv_func;
  293. varspez:=vs_const;
  294. end
  295. else
  296. varspez:=vs_value;
  297. defaultvalue:=nil;
  298. hdef:=nil;
  299. { read identifiers and insert with error type }
  300. sc.clear;
  301. repeat
  302. inc(paranr);
  303. vs:=cparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  304. currparast.insertsym(vs);
  305. if assigned(vs.owner) then
  306. sc.add(vs)
  307. else
  308. vs.free; // no nil needed
  309. consume(_ID);
  310. until not try_to_consume(_COMMA);
  311. locationstr:='';
  312. { macpas anonymous procvar }
  313. if parseprocvar<>pv_none then
  314. begin
  315. { inline procvar definitions are always nested procvars }
  316. pv:=cprocvardef.create(normal_function_level+1,true);
  317. if token=_LKLAMMER then
  318. parse_parameter_dec(pv);
  319. if parseprocvar=pv_func then
  320. begin
  321. block_type:=bt_var_type;
  322. consume(_COLON);
  323. single_type(pv.returndef,[]);
  324. block_type:=bt_var;
  325. end;
  326. { possible proc directives }
  327. if check_proc_directive(true) then
  328. parse_proctype_directives(pv);
  329. { Add implicit hidden parameters and function result }
  330. handle_calling_convention(pv,hcc_default_actions_intf);
  331. {$ifdef jvm}
  332. { anonymous -> no name }
  333. jvm_create_procvar_class('',pv);
  334. {$endif}
  335. hdef:=pv;
  336. end
  337. else
  338. { read type declaration, force reading for value paras }
  339. if (token=_COLON) or (varspez=vs_value) then
  340. begin
  341. consume(_COLON);
  342. { check for an open array }
  343. need_array:=false;
  344. { bitpacked open array are not yet supported }
  345. if (token=_PACKED) and
  346. not(cs_bitpacking in current_settings.localswitches) then
  347. begin
  348. consume(_PACKED);
  349. need_array:=true;
  350. end;
  351. if (token=_ARRAY) or
  352. need_array then
  353. begin
  354. consume(_ARRAY);
  355. consume(_OF);
  356. { define range and type of range }
  357. hdef:=carraydef.create_openarray;
  358. { array of const ? }
  359. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  360. begin
  361. consume(_CONST);
  362. srsym:=search_system_type('TVARREC');
  363. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  364. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  365. end
  366. else
  367. begin
  368. { define field type }
  369. if m_delphi in current_settings.modeswitches then
  370. stoptions:=[stoAllowSpecialization]
  371. else
  372. stoptions:=[];
  373. single_type(arrayelementdef,stoptions);
  374. if assigned(arrayelementdef.typesym) then
  375. check_hints(arrayelementdef.typesym,arrayelementdef.typesym.symoptions,arrayelementdef.typesym.deprecatedmsg);
  376. tarraydef(hdef).elementdef:=arrayelementdef;
  377. end;
  378. end
  379. else
  380. begin
  381. if (m_mac in current_settings.modeswitches) then
  382. is_univ:=try_to_consume(_UNIV);
  383. { this is not really working and generates internal errors
  384. if try_to_consume(_TYPE) then
  385. hdef:=ctypedformaltype
  386. else }
  387. begin
  388. block_type:=bt_var_type;
  389. single_type(hdef,[stoAllowSpecialization]);
  390. block_type:=bt_var;
  391. end;
  392. { open string ? }
  393. if is_shortstring(hdef) then
  394. begin
  395. case varspez of
  396. vs_var,vs_out:
  397. begin
  398. { not 100% Delphi-compatible: type xstr=string[255] cannot
  399. become an openstring there, while here it can }
  400. if (cs_openstring in current_settings.localswitches) and
  401. (tstringdef(hdef).len=255) then
  402. hdef:=openshortstringtype
  403. end;
  404. vs_value:
  405. begin
  406. { value "openstring" parameters don't make sense (the
  407. original string can never be modified, so there's no
  408. use in passing its original length), so change these
  409. into regular shortstring parameters (seems to be what
  410. Delphi also does) }
  411. if is_open_string(hdef) then
  412. hdef:=cshortstringtype;
  413. end;
  414. else
  415. ;
  416. end;
  417. end;
  418. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  419. begin
  420. if (idtoken=_LOCATION) then
  421. begin
  422. consume(_LOCATION);
  423. locationstr:=cstringpattern;
  424. consume(_CSTRING);
  425. end
  426. else
  427. begin
  428. if explicit_paraloc then
  429. Message(parser_e_paraloc_all_paras);
  430. locationstr:='';
  431. end;
  432. end
  433. else
  434. locationstr:='';
  435. { default parameter }
  436. if (m_default_para in current_settings.modeswitches) then
  437. handle_default_para_value;
  438. end;
  439. end
  440. else
  441. hdef:=cformaltype;
  442. if assigned(hdef.typesym) then
  443. check_hints(hdef.typesym,hdef.typesym.symoptions,hdef.typesym.deprecatedmsg);
  444. { File types are only allowed for var and out parameters }
  445. if (hdef.typ=filedef) and
  446. not(varspez in [vs_out,vs_var]) then
  447. CGMessage(cg_e_file_must_call_by_reference);
  448. { Dispinterfaces are restricted to using only automatable types }
  449. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  450. not is_automatable(hdef) then
  451. Message1(type_e_not_automatable,hdef.typename);
  452. { univ cannot be used with types whose size is not known at compile
  453. time }
  454. if is_univ and
  455. not is_valid_univ_para_type(hdef) then
  456. Message1(parser_e_invalid_univ_para,hdef.typename);
  457. for i:=0 to sc.count-1 do
  458. begin
  459. vs:=tparavarsym(sc[i]);
  460. vs.univpara:=is_univ;
  461. { update varsym }
  462. vs.vardef:=hdef;
  463. vs.defaultconstsym:=defaultvalue;
  464. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  465. begin
  466. if locationstr<>'' then
  467. begin
  468. if sc.count>1 then
  469. Message(parser_e_paraloc_only_one_para);
  470. if (paranr>1) and not(explicit_paraloc) then
  471. Message(parser_e_paraloc_all_paras);
  472. explicit_paraloc:=true;
  473. include(vs.varoptions,vo_has_explicit_paraloc);
  474. if not(paramanager.parseparaloc(vs,locationstr)) then
  475. message(parser_e_illegal_explicit_paraloc);
  476. end
  477. else
  478. if explicit_paraloc then
  479. Message(parser_e_paraloc_all_paras);
  480. end;
  481. {$ifdef wasm}
  482. if (vs.varspez in [vs_var,vs_constref,vs_out]) and is_wasm_reference_type(vs.vardef) then
  483. Message(parser_e_wasm_ref_types_can_only_be_passed_by_value);
  484. {$endif wasm}
  485. end;
  486. until not try_to_consume(_SEMICOLON);
  487. if explicit_paraloc then
  488. include(pd.procoptions,po_explicitparaloc);
  489. { remove parasymtable from stack }
  490. sc.free;
  491. sc := nil;
  492. { reset object options }
  493. block_type:=old_block_type;
  494. consume(_RKLAMMER);
  495. end;
  496. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;flags:tparse_proc_flags;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  497. var
  498. hs : string;
  499. orgsp,sp,orgspnongen,spnongen : TIDString;
  500. dummysym,srsym : tsym;
  501. checkstack : psymtablestackitem;
  502. oldfilepos,
  503. classstartfilepos,
  504. procstartfilepos : tfileposinfo;
  505. i,
  506. index : longint;
  507. addgendummy,
  508. hadspecialize,
  509. firstpart,
  510. found,
  511. searchagain : boolean;
  512. st,
  513. insertst,
  514. genericst: TSymtable;
  515. aprocsym : tprocsym;
  516. popclass : integer;
  517. parentdef : tobjectdef;
  518. ImplIntf : TImplementedInterface;
  519. old_parse_generic : boolean;
  520. old_current_structdef: tabstractrecorddef;
  521. old_current_genericdef,
  522. old_current_specializedef: tstoreddef;
  523. lasttoken,lastidtoken: ttoken;
  524. genericparams : tfphashobjectlist;
  525. procedure parse_operator_name;
  526. begin
  527. if (lasttoken in [first_overloaded..last_overloaded]) then
  528. begin
  529. optoken:=token;
  530. end
  531. else
  532. begin
  533. case lasttoken of
  534. _CARET:
  535. Message1(parser_e_overload_operator_failed,'**');
  536. _ID:
  537. case lastidtoken of
  538. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  539. _EXPLICIT:optoken:=_OP_EXPLICIT;
  540. _INC:optoken:=_OP_INC;
  541. _DEC:optoken:=_OP_DEC;
  542. _INITIALIZE:optoken:=_OP_INITIALIZE;
  543. _FINALIZE:optoken:=_OP_FINALIZE;
  544. _ADDREF:optoken:=_OP_ADDREF;
  545. _COPY:optoken:=_OP_COPY;
  546. else
  547. if (m_delphi in current_settings.modeswitches) then
  548. case lastidtoken of
  549. _IMPLICIT:optoken:=_ASSIGNMENT;
  550. _NEGATIVE:optoken:=_MINUS;
  551. _POSITIVE:optoken:=_PLUS;
  552. _LOGICALNOT:optoken:=_OP_NOT;
  553. _IN:optoken:=_OP_IN;
  554. _EQUAL:optoken:=_EQ;
  555. _NOTEQUAL:optoken:=_NE;
  556. _GREATERTHAN:optoken:=_GT;
  557. _GREATERTHANOREQUAL:optoken:=_GTE;
  558. _LESSTHAN:optoken:=_LT;
  559. _LESSTHANOREQUAL:optoken:=_LTE;
  560. _ADD:optoken:=_PLUS;
  561. _SUBTRACT:optoken:=_MINUS;
  562. _MULTIPLY:optoken:=_STAR;
  563. _DIVIDE:optoken:=_SLASH;
  564. _INTDIVIDE:optoken:=_OP_DIV;
  565. _MODULUS:optoken:=_OP_MOD;
  566. _LEFTSHIFT:optoken:=_OP_SHL;
  567. _RIGHTSHIFT:optoken:=_OP_SHR;
  568. _LOGICALAND:optoken:=_OP_AND;
  569. _LOGICALOR:optoken:=_OP_OR;
  570. _LOGICALXOR:optoken:=_OP_XOR;
  571. _BITWISEAND:optoken:=_OP_AND;
  572. _BITWISEOR:optoken:=_OP_OR;
  573. _BITWISEXOR:optoken:=_OP_XOR;
  574. else
  575. Message1(parser_e_overload_operator_failed,'');
  576. end
  577. else
  578. Message1(parser_e_overload_operator_failed,'');
  579. end
  580. else
  581. Message1(parser_e_overload_operator_failed,'');
  582. end;
  583. end;
  584. sp:=overloaded_names[optoken];
  585. orgsp:=sp;
  586. spnongen:=sp;
  587. orgspnongen:=orgsp;
  588. end;
  589. procedure consume_proc_name;
  590. var
  591. s : string;
  592. i : longint;
  593. sym : ttypesym;
  594. begin
  595. lasttoken:=token;
  596. lastidtoken:=idtoken;
  597. if assigned(genericparams) then
  598. for i:=0 to genericparams.count-1 do
  599. begin
  600. sym:=ttypesym(genericparams[i]);
  601. if (sym.typ<>constsym) and tstoreddef(sym.typedef).is_registered then
  602. begin
  603. sym.typedef.free;
  604. sym.typedef:=nil;
  605. end;
  606. sym.free;
  607. sym := nil;
  608. end;
  609. genericparams.free;
  610. genericparams:=nil;
  611. hadspecialize:=false;
  612. if potype=potype_operator then
  613. optoken:=NOTOKEN;
  614. if (potype=potype_operator) and (token<>_ID) then
  615. begin
  616. parse_operator_name;
  617. consume(token);
  618. end
  619. else
  620. begin
  621. sp:=pattern;
  622. orgsp:=orgpattern;
  623. spnongen:=sp;
  624. orgspnongen:=orgsp;
  625. if firstpart and
  626. not (m_delphi in current_settings.modeswitches) and
  627. (idtoken=_SPECIALIZE) then
  628. hadspecialize:=true;
  629. consume(_ID);
  630. if ((ppf_generic in flags) or (m_delphi in current_settings.modeswitches)) and
  631. (token in [_LT,_LSHARPBRACKET]) then
  632. begin
  633. consume(token);
  634. if token in [_GT,_RSHARPBRACKET] then
  635. message(type_e_type_id_expected)
  636. else
  637. begin
  638. genericparams:=parse_generic_parameters(true);
  639. if not assigned(genericparams) then
  640. internalerror(2015061201);
  641. if genericparams.count=0 then
  642. internalerror(2015061202);
  643. s:='';
  644. str(genericparams.count,s);
  645. spnongen:=sp;
  646. orgspnongen:=orgsp;
  647. sp:=sp+'$'+s;
  648. orgsp:=orgsp+'$'+s;
  649. end;
  650. if not try_to_consume(_GT) then
  651. consume(_RSHARPBRACKET);
  652. end;
  653. end;
  654. firstpart:=false;
  655. end;
  656. function search_object_name(const sp:TIDString;gen_error:boolean):tsym;
  657. var
  658. storepos:tfileposinfo;
  659. srsymtable:TSymtable;
  660. begin
  661. storepos:=current_tokenpos;
  662. current_tokenpos:=procstartfilepos;
  663. searchsym(sp,result,srsymtable);
  664. if not assigned(result) then
  665. begin
  666. if gen_error then
  667. identifier_not_found(orgsp);
  668. result:=generrorsym;
  669. end;
  670. current_tokenpos:=storepos;
  671. end;
  672. function handle_generic_interface:boolean;
  673. var
  674. i : longint;
  675. sym : ttypesym;
  676. typesrsym : tsym;
  677. typesrsymtable : tsymtable;
  678. hierarchy,
  679. specializename,
  680. prettyname: ansistring;
  681. error : boolean;
  682. genname,
  683. ugenname : tidstring;
  684. module : tmodule;
  685. begin
  686. result:=false;
  687. if not assigned(genericparams) then
  688. exit;
  689. specializename:='$';
  690. prettyname:='';
  691. error:=false;
  692. for i:=0 to genericparams.count-1 do
  693. begin
  694. sym:=ttypesym(genericparams[i]);
  695. { ToDo: position }
  696. if not searchsym(upper(sym.RealName),typesrsym,typesrsymtable) then
  697. begin
  698. message1(sym_e_id_not_found,sym.name);
  699. error:=true;
  700. continue;
  701. end;
  702. if typesrsym.typ<>typesym then
  703. begin
  704. message(type_e_type_id_expected);
  705. error:=true;
  706. continue;
  707. end;
  708. module:=find_module_from_symtable(ttypesym(typesrsym).typedef.owner);
  709. if not assigned(module) then
  710. internalerror(2016112803);
  711. specializename:=specializename+'_$'+hexstr(module.moduleid,8)+'$$'+ttypesym(typesrsym).typedef.unique_id_str;
  712. if i>0 then
  713. prettyname:=prettyname+',';
  714. prettyname:=prettyname+ttypesym(typesrsym).prettyname;
  715. end;
  716. result:=true;
  717. if error then
  718. begin
  719. srsym:=generrorsym;
  720. exit;
  721. end;
  722. if not searchsym(sp,typesrsym,typesrsymtable) or (typesrsym.typ<>typesym) then
  723. begin
  724. identifier_not_found(sp);
  725. srsym:=generrorsym;
  726. exit;
  727. end;
  728. module:=find_module_from_symtable(ttypesym(typesrsym).owner);
  729. if not assigned(module) then
  730. internalerror(2022102105);
  731. hierarchy:=ttypesym(typesrsym).typedef.ownerhierarchyname;
  732. if hierarchy<>'' then
  733. hierarchy:='.'+hierarchy;
  734. genname:=generate_generic_name(sp,specializename,module.modulename^+hierarchy);
  735. ugenname:=upper(genname);
  736. srsym:=search_object_name(ugenname,false);
  737. if not assigned(srsym) then
  738. begin
  739. Message1(type_e_generic_declaration_does_not_match,sp+'<'+prettyname+'>');
  740. srsym:=generrorsym;
  741. end;
  742. end;
  743. procedure specialize_generic_interface;
  744. var
  745. node : tnode;
  746. begin
  747. node:=factor(false,[ef_type_only,ef_had_specialize]);
  748. if node.nodetype=typen then
  749. begin
  750. sp:=ttypenode(node).typedef.typesym.name;
  751. end
  752. else
  753. sp:='';
  754. end;
  755. function check_generic_parameters(def:tstoreddef):boolean;
  756. var
  757. i : longint;
  758. declsym,
  759. implsym : tsym;
  760. impltype : ttypesym absolute implsym;
  761. implname : tsymstr;
  762. fileinfo : tfileposinfo;
  763. begin
  764. result:=true;
  765. if not assigned(def.genericparas) then
  766. internalerror(2018090102);
  767. if not assigned(genericparams) then
  768. internalerror(2018090103);
  769. if def.genericparas.count<>genericparams.count then
  770. internalerror(2018090104);
  771. for i:=0 to def.genericparas.count-1 do
  772. begin
  773. declsym:=tsym(def.genericparas[i]);
  774. implsym:=tsym(genericparams[i]);
  775. implname:=upper(genericparams.nameofindex(i));
  776. if declsym.name<>implname then
  777. begin
  778. messagepos1(implsym.fileinfo,sym_e_generic_type_param_mismatch,implsym.realname);
  779. messagepos1(declsym.fileinfo,sym_e_generic_type_param_decl,declsym.realname);
  780. result:=false;
  781. end;
  782. if ((implsym.typ=typesym) and (df_genconstraint in impltype.typedef.defoptions)) or
  783. (implsym.typ=constsym) then
  784. begin
  785. if implsym.typ=constsym then
  786. fileinfo:=impltype.fileinfo
  787. else
  788. fileinfo:=tstoreddef(impltype.typedef).genconstraintdata.fileinfo;
  789. messagepos(fileinfo,parser_e_generic_constraints_not_allowed_here);
  790. result:=false;
  791. end;
  792. end;
  793. end;
  794. begin
  795. sp:='';
  796. orgsp:='';
  797. spnongen:='';
  798. orgspnongen:='';
  799. { Save the position where this procedure really starts }
  800. procstartfilepos:=current_tokenpos;
  801. old_parse_generic:=parse_generic;
  802. firstpart:=true;
  803. result:=false;
  804. pd:=nil;
  805. aprocsym:=nil;
  806. srsym:=nil;
  807. genericparams:=nil;
  808. hadspecialize:=false;
  809. addgendummy:=false;
  810. { ensure that we don't insert into a withsymtable (can happen with
  811. anonymous functions) }
  812. checkstack:=symtablestack.stack;
  813. while checkstack^.symtable.symtabletype in [withsymtable] do
  814. checkstack:=checkstack^.next;
  815. insertst:=checkstack^.symtable;
  816. if not assigned(genericdef) then
  817. begin
  818. if ppf_anonymous in flags then
  819. begin
  820. if not (insertst.symtabletype in [localsymtable,staticsymtable]) then
  821. internalerror(2021050101);
  822. { generate a unique name for the anonymous function; don't use
  823. something like file position however as this might be inside
  824. an include file that's included multiple times }
  825. str(insertst.symlist.count,orgsp);
  826. orgsp:='__FPCINTERNAL__Anonymous_'+orgsp;
  827. sp:=upper(orgsp);
  828. spnongen:=sp;
  829. orgspnongen:=orgsp;
  830. end
  831. else
  832. consume_proc_name;
  833. { examine interface map: function/procedure iname.functionname=locfuncname }
  834. if assigned(astruct) and
  835. (astruct.typ=objectdef) and
  836. assigned(tobjectdef(astruct).ImplementedInterfaces) and
  837. (tobjectdef(astruct).ImplementedInterfaces.count>0) and
  838. (
  839. (token=_POINT) or
  840. (
  841. hadspecialize and
  842. (token=_ID)
  843. )
  844. ) then
  845. begin
  846. if hadspecialize and (token=_ID) then
  847. specialize_generic_interface;
  848. consume(_POINT);
  849. if hadspecialize or not handle_generic_interface then
  850. srsym:=search_object_name(sp,true);
  851. { qualifier is interface? }
  852. ImplIntf:=nil;
  853. if assigned(srsym) and
  854. (srsym.typ=typesym) and
  855. (ttypesym(srsym).typedef.typ=objectdef) then
  856. ImplIntf:=find_implemented_interface(tobjectdef(astruct),tobjectdef(ttypesym(srsym).typedef));
  857. if ImplIntf=nil then
  858. begin
  859. Message(parser_e_interface_id_expected);
  860. { error recovery }
  861. consume(_ID);
  862. if try_to_consume(_EQ) then
  863. consume(_ID);
  864. exit;
  865. end
  866. else
  867. { in case of a generic or specialized interface we need to use the
  868. name of the def instead of the symbol, so that always the correct
  869. name is used }
  870. if [df_generic,df_specialization]*ttypesym(srsym).typedef.defoptions<>[] then
  871. sp:=tobjectdef(ttypesym(srsym).typedef).objname^;
  872. { must be a directly implemented interface }
  873. if Assigned(ImplIntf.ImplementsGetter) then
  874. Message2(parser_e_implements_no_mapping,ImplIntf.IntfDef.typename,astruct.objrealname^);
  875. consume(_ID);
  876. { Create unique name <interface>.<method> }
  877. hs:=sp+'.'+pattern;
  878. consume(_EQ);
  879. if assigned(ImplIntf) and
  880. (token=_ID) then
  881. ImplIntf.AddMapping(hs,pattern);
  882. consume(_ID);
  883. result:=true;
  884. exit;
  885. end;
  886. if assigned(genericparams) and assigned(current_genericdef) then
  887. Message(parser_f_no_generic_inside_generic);
  888. { method ? }
  889. srsym:=nil;
  890. if not assigned(astruct) and
  891. (symtablestack.top.symtablelevel=main_program_level) and
  892. try_to_consume(_POINT) then
  893. begin
  894. repeat
  895. classstartfilepos:=procstartfilepos;
  896. searchagain:=false;
  897. { throw the error at the right location }
  898. oldfilepos:=current_filepos;
  899. current_filepos:=procstartfilepos;
  900. if not assigned(astruct) and not assigned(srsym) then
  901. srsym:=search_object_name(sp,true);
  902. current_filepos:=oldfilepos;
  903. { we need to check whether the names of the generic parameter
  904. types match with the one in the declaration of a class/record,
  905. but we need to do this before consume_proc_name frees the
  906. type parameters of the class part }
  907. if (srsym.typ=typesym) and
  908. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) and
  909. tstoreddef(ttypesym(srsym).typedef).is_generic and
  910. assigned(genericparams) then
  911. { this is recoverable, so no further action necessary }
  912. check_generic_parameters(tstoreddef(ttypesym(srsym).typedef));
  913. { consume proc name }
  914. procstartfilepos:=current_tokenpos;
  915. consume_proc_name;
  916. { qualifier is class name ? }
  917. if (srsym.typ=typesym) and
  918. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  919. begin
  920. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  921. if (token<>_POINT) then
  922. if (potype in [potype_class_constructor,potype_class_destructor]) then
  923. sp:=lower(sp)
  924. else
  925. if (potype=potype_operator) and (optoken=NOTOKEN) then
  926. parse_operator_name;
  927. srsym:=tsym(astruct.symtable.Find(sp));
  928. if assigned(srsym) then
  929. begin
  930. if srsym.typ=procsym then
  931. aprocsym:=tprocsym(srsym)
  932. else
  933. if (srsym.typ=typesym) and
  934. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  935. begin
  936. searchagain:=true;
  937. consume(_POINT);
  938. end
  939. else
  940. begin
  941. { we use a different error message for tp7 so it looks more compatible }
  942. if (m_fpc in current_settings.modeswitches) then
  943. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  944. else
  945. Message(parser_e_methode_id_expected);
  946. { rename the name to an unique name to avoid an
  947. error when inserting the symbol in the symtable }
  948. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  949. end;
  950. end
  951. else
  952. begin
  953. MessagePos(procstartfilepos,parser_e_methode_id_expected);
  954. { recover by making it a normal procedure instead of method }
  955. astruct:=nil;
  956. end;
  957. end
  958. else
  959. MessagePos(classstartfilepos,parser_e_class_id_expected);
  960. until not searchagain;
  961. end
  962. else
  963. begin
  964. { check for constructor/destructor/class operators which are not allowed here }
  965. if (not parse_only) and
  966. ((potype in [potype_constructor,potype_destructor,
  967. potype_class_constructor,potype_class_destructor]) or
  968. ((potype=potype_operator) and (m_delphi in current_settings.modeswitches))) then
  969. Message(parser_e_only_methods_allowed);
  970. repeat
  971. { only 1 class constructor and destructor is allowed in the class and
  972. the check was already done with oo_has_class_constructor or
  973. oo_has_class_destructor -> skip searching
  974. (bug #28801) }
  975. if (potype in [potype_class_constructor,potype_class_destructor]) then
  976. break;
  977. searchagain:=false;
  978. current_tokenpos:=procstartfilepos;
  979. if (potype=potype_operator)and(optoken=NOTOKEN) then
  980. parse_operator_name;
  981. srsym:=tsym(insertst.Find(sp));
  982. { Also look in the globalsymtable if we didn't found
  983. the symbol in the localsymtable }
  984. if not assigned(srsym) and
  985. not(parse_only) and
  986. (symtablestack.top=current_module.localsymtable) and
  987. assigned(current_module.globalsymtable) then
  988. srsym:=tsym(current_module.globalsymtable.Find(sp));
  989. { if the symbol isn't assigned, but we're parsing a class or
  990. object then check in the parent types for symbols of the same
  991. name that are generics and declare the new symbol as a generic
  992. dummy symbol }
  993. if not assigned(srsym) and is_class_or_object(astruct) then
  994. begin
  995. parentdef:=tobjectdef(astruct).childof;
  996. while assigned(parentdef) do
  997. begin
  998. srsym:=tsym(parentdef.symtable.Find(sp));
  999. if assigned(srsym) and (sp_generic_dummy in srsym.symoptions) then
  1000. begin
  1001. addgendummy:=true;
  1002. break;
  1003. end;
  1004. parentdef:=parentdef.childof;
  1005. end;
  1006. srsym:=nil;
  1007. end;
  1008. { Check if overloaded is a procsym }
  1009. if assigned(srsym) then
  1010. begin
  1011. if srsym.typ=procsym then
  1012. aprocsym:=tprocsym(srsym)
  1013. else
  1014. begin
  1015. { when the other symbol is a unit symbol then hide the unit
  1016. symbol, this is not supported in tp7 }
  1017. if not(m_tp7 in current_settings.modeswitches) and
  1018. (srsym.typ=unitsym) then
  1019. begin
  1020. HideSym(srsym);
  1021. searchagain:=true;
  1022. end
  1023. else
  1024. if (m_delphi in current_settings.modeswitches) and
  1025. (srsym.typ=absolutevarsym) and
  1026. ([vo_is_funcret,vo_is_result]*tabstractvarsym(srsym).varoptions=[vo_is_funcret]) then
  1027. begin
  1028. HideSym(srsym);
  1029. searchagain:=true;
  1030. end
  1031. else if (srsym.typ=typesym) and
  1032. (sp_generic_dummy in srsym.symoptions) and
  1033. (ttypesym(srsym).typedef.typ=undefineddef) and
  1034. not assigned(genericparams) then
  1035. begin
  1036. { this is a generic dummy symbol that has not yet
  1037. been used; so we rename the dummy symbol and continue
  1038. as if nothing happened }
  1039. hidesym(srsym);
  1040. searchagain:=true;
  1041. addgendummy:=true;
  1042. end
  1043. else
  1044. begin
  1045. { we use a different error message for tp7 so it looks more compatible }
  1046. if (m_fpc in current_settings.modeswitches) then
  1047. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  1048. else
  1049. Message1(sym_e_duplicate_id,srsym.realname);
  1050. { rename the name to an unique name to avoid an
  1051. error when inserting the symbol in the symtable }
  1052. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  1053. end;
  1054. end;
  1055. end;
  1056. until not searchagain;
  1057. end;
  1058. { test again if assigned, it can be reset to recover }
  1059. if not assigned(aprocsym) then
  1060. begin
  1061. { create a new procsym and set the real filepos }
  1062. current_tokenpos:=procstartfilepos;
  1063. { for operator we have only one procsym for each overloaded
  1064. operation }
  1065. if (potype=potype_operator) then
  1066. begin
  1067. aprocsym:=Tprocsym(insertst.Find(sp));
  1068. if aprocsym=nil then
  1069. aprocsym:=cprocsym.create('$'+sp);
  1070. end
  1071. else
  1072. if (potype in [potype_class_constructor,potype_class_destructor]) then
  1073. aprocsym:=cprocsym.create('$'+lower(sp))
  1074. else
  1075. aprocsym:=cprocsym.create(orgsp);
  1076. if ppf_anonymous in flags then
  1077. include(aprocsym.symoptions,sp_internal);
  1078. if addgendummy then
  1079. include(aprocsym.symoptions,sp_generic_dummy);
  1080. insertst.insertsym(aprocsym);
  1081. end;
  1082. end;
  1083. { to get the correct symtablelevel we must ignore ObjectSymtables }
  1084. st:=nil;
  1085. checkstack:=symtablestack.stack;
  1086. while assigned(checkstack) do
  1087. begin
  1088. st:=checkstack^.symtable;
  1089. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  1090. break;
  1091. checkstack:=checkstack^.next;
  1092. end;
  1093. pd:=cprocdef.create(st.symtablelevel+1,not assigned(genericdef));
  1094. pd.struct:=astruct;
  1095. pd.procsym:=aprocsym;
  1096. pd.proctypeoption:=potype;
  1097. if ppf_anonymous in flags then
  1098. begin
  1099. include(pd.procoptions,po_anonymous);
  1100. { inherit the "static" and "class" flag from the method the anonymous function
  1101. is contained in }
  1102. if (st.symtabletype=localsymtable) and
  1103. (st.defowner.typ=procdef) and
  1104. ([po_staticmethod,po_classmethod]*tprocdef(st.defowner).procoptions<>[]) then
  1105. pd.procoptions:=pd.procoptions+([po_staticmethod,po_classmethod]*tprocdef(st.defowner).procoptions);
  1106. end;
  1107. if assigned(genericparams) then
  1108. begin
  1109. if potype=potype_constructor then
  1110. begin
  1111. Message(parser_e_constructurs_cannot_take_type_parameters);
  1112. genericparams.free;
  1113. genericparams:=nil;
  1114. end
  1115. else
  1116. begin
  1117. include(pd.defoptions,df_generic);
  1118. { push the parameter symtable so that constraint definitions are added
  1119. there and not in the owner symtable }
  1120. symtablestack.push(pd.parast);
  1121. { register the parameters }
  1122. for i:=0 to genericparams.count-1 do
  1123. begin
  1124. tsym(genericparams[i]).register_sym;
  1125. if tsym(genericparams[i]).typ=typesym then
  1126. tstoreddef(ttypesym(genericparams[i]).typedef).register_def;
  1127. end;
  1128. insert_generic_parameter_types(pd,nil,genericparams,false);
  1129. { the list is no longer required }
  1130. genericparams.free;
  1131. genericparams:=nil;
  1132. symtablestack.pop(pd.parast);
  1133. parse_generic:=true;
  1134. { also generate a dummy symbol if none exists already }
  1135. if assigned(astruct) then
  1136. dummysym:=tsym(astruct.symtable.find(spnongen))
  1137. else
  1138. begin
  1139. dummysym:=tsym(insertst.find(spnongen));
  1140. if not assigned(dummysym) and
  1141. (symtablestack.top=current_module.localsymtable) and
  1142. assigned(current_module.globalsymtable) then
  1143. dummysym:=tsym(current_module.globalsymtable.find(spnongen));
  1144. end;
  1145. if not assigned(dummysym) then
  1146. begin
  1147. { overloading generic routines with non-generic types is not
  1148. allowed, so we create a procsym as dummy }
  1149. dummysym:=cprocsym.create(orgspnongen);
  1150. if assigned(astruct) then
  1151. astruct.symtable.insertsym(dummysym)
  1152. else
  1153. insertst.insertsym(dummysym);
  1154. end
  1155. else if (dummysym.typ<>procsym) and
  1156. (
  1157. { show error only for the declaration, not also the implementation }
  1158. not assigned(astruct) or
  1159. (symtablestack.top.symtablelevel<>main_program_level)
  1160. ) then
  1161. Message1(sym_e_duplicate_id,dummysym.realname);
  1162. if not (sp_generic_dummy in dummysym.symoptions) then
  1163. begin
  1164. include(dummysym.symoptions,sp_generic_dummy);
  1165. add_generic_dummysym(dummysym,'');
  1166. end;
  1167. if dummysym.typ=procsym then
  1168. tprocsym(dummysym).add_generic_overload(aprocsym);
  1169. { start token recorder for the declaration }
  1170. pd.init_genericdecl;
  1171. current_scanner.startrecordtokens(pd.genericdecltokenbuf);
  1172. end;
  1173. end
  1174. else if assigned(genericdef) then
  1175. insert_generic_parameter_types(pd,tstoreddef(genericdef),generictypelist,false);
  1176. { methods inherit df_generic or df_specialization from the objectdef }
  1177. if assigned(pd.struct) and
  1178. (pd.parast.symtablelevel=normal_function_level) then
  1179. begin
  1180. if (df_generic in pd.struct.defoptions) then
  1181. begin
  1182. include(pd.defoptions,df_generic);
  1183. parse_generic:=true;
  1184. end;
  1185. if (df_specialization in pd.struct.defoptions) then
  1186. begin
  1187. if assigned(current_specializedef) then
  1188. begin
  1189. include(pd.defoptions,df_specialization);
  1190. { Find corresponding genericdef, we need it later to
  1191. replay the tokens to generate the body }
  1192. if not assigned(pd.struct.genericdef) then
  1193. internalerror(200512113);
  1194. genericst:=pd.struct.genericdef.GetSymtable(gs_record);
  1195. if not assigned(genericst) then
  1196. internalerror(200512114);
  1197. { when searching for the correct procdef to use as genericdef we need to ignore
  1198. everything except procdefs so that we can find the correct indices }
  1199. index:=0;
  1200. found:=false;
  1201. for i:=0 to pd.owner.deflist.count-1 do
  1202. begin
  1203. if tdef(pd.owner.deflist[i]).typ<>procdef then
  1204. continue;
  1205. if pd.owner.deflist[i]=pd then
  1206. begin
  1207. found:=true;
  1208. break;
  1209. end;
  1210. inc(index);
  1211. end;
  1212. if not found then
  1213. internalerror(2014052301);
  1214. for i:=0 to genericst.deflist.count-1 do
  1215. begin
  1216. if tdef(genericst.deflist[i]).typ<>procdef then
  1217. continue;
  1218. if index=0 then
  1219. pd.genericdef:=tstoreddef(genericst.deflist[i]);
  1220. dec(index);
  1221. end;
  1222. if not assigned(pd.genericdef) or
  1223. (pd.genericdef.typ<>procdef) then
  1224. internalerror(200512115);
  1225. end
  1226. else
  1227. Message(parser_e_explicit_method_implementation_for_specializations_not_allowed);
  1228. end;
  1229. end;
  1230. { methods need to be exported }
  1231. if assigned(astruct) and
  1232. (
  1233. (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) or
  1234. (symtablestack.top.symtablelevel=main_program_level)
  1235. ) then
  1236. include(pd.procoptions,po_global);
  1237. { symbol options that need to be kept per procdef }
  1238. pd.fileinfo:=procstartfilepos;
  1239. pd.visibility:=insertst.currentvisibility;
  1240. if insertst.currentlyoptional then
  1241. include(pd.procoptions,po_optional);
  1242. { when extended rtti appears, then we must adapt this check}
  1243. if (target_cpu=tsystemcpu.cpu_wasm32) and
  1244. assigned(astruct) and
  1245. (astruct.typ=objectdef) and
  1246. (tobjectdef(astruct).objecttype in [odt_interfacecom,odt_interfacecorba]) and
  1247. (pd.visibility=vis_published) then
  1248. pd.synthetickind:=tsk_invoke_helper;
  1249. { parse parameters }
  1250. if token=_LKLAMMER then
  1251. begin
  1252. old_current_structdef:=nil;
  1253. old_current_genericdef:=current_genericdef;
  1254. old_current_specializedef:=nil;
  1255. { Add ObjectSymtable to be able to find nested type definitions }
  1256. popclass:=0;
  1257. if assigned(pd.struct) and
  1258. (pd.parast.symtablelevel>=normal_function_level) and
  1259. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1260. begin
  1261. popclass:=push_nested_hierarchy(pd.struct);
  1262. old_current_structdef:=current_structdef;
  1263. old_current_specializedef:=current_specializedef;
  1264. current_structdef:=pd.struct;
  1265. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1266. current_genericdef:=current_structdef;
  1267. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1268. current_specializedef:=current_structdef;
  1269. end;
  1270. if pd.is_generic then
  1271. current_genericdef:=pd;
  1272. { Add parameter symtable }
  1273. if pd.parast.symtabletype<>staticsymtable then
  1274. symtablestack.push(pd.parast);
  1275. parse_parameter_dec(pd);
  1276. if pd.parast.symtabletype<>staticsymtable then
  1277. symtablestack.pop(pd.parast);
  1278. current_genericdef:=old_current_genericdef;
  1279. if popclass>0 then
  1280. begin
  1281. current_structdef:=old_current_structdef;
  1282. current_specializedef:=old_current_specializedef;
  1283. dec(popclass,pop_nested_hierarchy(pd.struct));
  1284. if popclass<>0 then
  1285. internalerror(201011260); // 11 nov 2010 index 0
  1286. end;
  1287. end;
  1288. parse_generic:=old_parse_generic;
  1289. result:=true;
  1290. end;
  1291. procedure parse_proc_dec_finish(pd:tprocdef;flags:tparse_proc_flags;astruct:tabstractrecorddef);
  1292. var
  1293. locationstr: string;
  1294. i: integer;
  1295. found: boolean;
  1296. procedure read_returndef(pd: tprocdef);
  1297. var
  1298. popclass: integer;
  1299. old_parse_generic: boolean;
  1300. old_current_structdef: tabstractrecorddef;
  1301. old_current_genericdef,
  1302. old_current_specializedef: tstoreddef;
  1303. begin
  1304. old_parse_generic:=parse_generic;
  1305. { Add ObjectSymtable to be able to find generic type definitions }
  1306. popclass:=0;
  1307. old_current_structdef:=nil;
  1308. old_current_genericdef:=current_genericdef;
  1309. old_current_specializedef:=current_specializedef;
  1310. current_genericdef:=nil;
  1311. current_specializedef:=nil;
  1312. if assigned(pd.struct) and
  1313. (pd.parast.symtablelevel>=normal_function_level) and
  1314. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1315. begin
  1316. popclass:=push_nested_hierarchy(pd.struct);
  1317. old_current_structdef:=current_structdef;
  1318. current_structdef:=pd.struct;
  1319. end;
  1320. if df_generic in pd.defoptions then
  1321. begin
  1322. if pd.is_generic then
  1323. current_genericdef:=pd
  1324. else if assigned(pd.struct) then
  1325. current_genericdef:=pd.struct
  1326. else
  1327. internalerror(2016090202);
  1328. end;
  1329. if df_specialization in pd.defoptions then
  1330. begin
  1331. if pd.is_specialization then
  1332. current_specializedef:=pd
  1333. else if assigned(pd.struct) then
  1334. current_specializedef:=pd.struct
  1335. else
  1336. internalerror(2016090203);
  1337. end;
  1338. parse_generic:=(df_generic in pd.defoptions);
  1339. if pd.is_generic or pd.is_specialization then
  1340. symtablestack.push(pd.parast);
  1341. pd.returndef:=result_type([stoAllowSpecialization]);
  1342. // Issue #24863, enabled only for the main progra commented out for now because it breaks building of RTL and needs extensive
  1343. // testing and/or RTL patching.
  1344. {
  1345. if ((pd.returndef=cvarianttype) or (pd.returndef=colevarianttype)) and
  1346. not(cs_compilesystem in current_settings.moduleswitches) then
  1347. include(current_module.moduleflags,mf_uses_variants);
  1348. }
  1349. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  1350. Message1(type_e_not_automatable,pd.returndef.typename);
  1351. if assigned(pd.returndef.typesym) then
  1352. check_hints(pd.returndef.typesym,pd.returndef.typesym.symoptions,pd.returndef.typesym.deprecatedmsg);
  1353. if pd.is_generic or pd.is_specialization then
  1354. symtablestack.pop(pd.parast);
  1355. if popclass>0 then
  1356. begin
  1357. current_structdef:=old_current_structdef;
  1358. dec(popclass,pop_nested_hierarchy(pd.struct));
  1359. if popclass<>0 then
  1360. internalerror(201012020);
  1361. end;
  1362. current_genericdef:=old_current_genericdef;
  1363. current_specializedef:=old_current_specializedef;
  1364. parse_generic:=old_parse_generic;
  1365. end;
  1366. begin
  1367. locationstr:='';
  1368. case pd.proctypeoption of
  1369. potype_procedure:
  1370. begin
  1371. pd.returndef:=voidtype;
  1372. if ppf_classmethod in flags then
  1373. include(pd.procoptions,po_classmethod);
  1374. end;
  1375. potype_function:
  1376. begin
  1377. if po_anonymous in pd.procoptions then
  1378. begin
  1379. { allow a different result name for anonymous functions (especially
  1380. for modes without Result modeswitch), but for consistency with
  1381. operators we allow this in other modes as well }
  1382. if token<>_ID then
  1383. begin
  1384. if not(m_result in current_settings.modeswitches) then
  1385. consume(_ID);
  1386. end
  1387. else
  1388. begin
  1389. pd.resultname:=stringdup(orgpattern);
  1390. consume(_ID);
  1391. end;
  1392. end;
  1393. if try_to_consume(_COLON) then
  1394. begin
  1395. read_returndef(pd);
  1396. if (target_info.system in [system_m68k_amiga]) then
  1397. begin
  1398. if (idtoken=_LOCATION) then
  1399. begin
  1400. if po_explicitparaloc in pd.procoptions then
  1401. begin
  1402. consume(_LOCATION);
  1403. locationstr:=cstringpattern;
  1404. consume(_CSTRING);
  1405. end
  1406. else
  1407. { I guess this needs a new message... (KB) }
  1408. Message(parser_e_paraloc_all_paras);
  1409. end
  1410. else
  1411. begin
  1412. if po_explicitparaloc in pd.procoptions then
  1413. { assign default locationstr, if none specified }
  1414. { and we've arguments with explicit paraloc }
  1415. locationstr:='D0';
  1416. end;
  1417. end;
  1418. end
  1419. else
  1420. begin
  1421. if (
  1422. parse_only and
  1423. not(is_interface(pd.struct))
  1424. ) or
  1425. (m_repeat_forward in current_settings.modeswitches) then
  1426. begin
  1427. consume(_COLON);
  1428. consume_all_until(_SEMICOLON);
  1429. end;
  1430. end;
  1431. if ppf_classmethod in flags then
  1432. include(pd.procoptions,po_classmethod);
  1433. end;
  1434. potype_constructor,
  1435. potype_class_constructor:
  1436. begin
  1437. if not (ppf_classmethod in flags) and
  1438. assigned(pd) and
  1439. assigned(pd.struct) then
  1440. begin
  1441. { Set return type, class constructors return the
  1442. created instance, object constructors return boolean }
  1443. if is_class(pd.struct) or
  1444. is_record(pd.struct) or
  1445. is_javaclass(pd.struct) then
  1446. pd.returndef:=pd.struct
  1447. else
  1448. if is_objectpascal_helper(pd.struct) then
  1449. pd.returndef:=tobjectdef(pd.struct).extendeddef
  1450. else
  1451. {$ifdef CPU64bitaddr}
  1452. pd.returndef:=bool64type;
  1453. {$else CPU64bitaddr}
  1454. pd.returndef:=bool32type;
  1455. {$endif CPU64bitaddr}
  1456. end
  1457. else
  1458. pd.returndef:=voidtype;
  1459. end;
  1460. potype_class_destructor,
  1461. potype_destructor:
  1462. begin
  1463. if assigned(pd) then
  1464. pd.returndef:=voidtype;
  1465. end;
  1466. potype_operator:
  1467. begin
  1468. { operators always need to be searched in all units (that
  1469. contain operators) }
  1470. include(pd.procoptions,po_overload);
  1471. pd.procsym.owner.includeoption(sto_has_operator);
  1472. if pd.parast.symtablelevel>normal_function_level then
  1473. Message(parser_e_no_local_operator);
  1474. if ppf_classmethod in flags then
  1475. begin
  1476. include(pd.procoptions,po_classmethod);
  1477. { any class operator is also static }
  1478. include(pd.procoptions,po_staticmethod);
  1479. end;
  1480. if token<>_ID then
  1481. begin
  1482. if not(m_result in current_settings.modeswitches) then
  1483. consume(_ID);
  1484. end
  1485. else
  1486. begin
  1487. pd.resultname:=stringdup(orgpattern);
  1488. consume(_ID);
  1489. end;
  1490. { operators without result (management operators) }
  1491. if optoken in [_OP_INITIALIZE, _OP_FINALIZE, _OP_ADDREF, _OP_COPY] then
  1492. begin
  1493. { single var parameter to point the record }
  1494. if (optoken in [_OP_INITIALIZE, _OP_FINALIZE, _OP_ADDREF]) and
  1495. (
  1496. (pd.parast.SymList.Count<>1) or
  1497. (tparavarsym(pd.parast.SymList[0]).vardef<>pd.struct) or
  1498. (tparavarsym(pd.parast.SymList[0]).varspez<>vs_var)
  1499. ) then
  1500. Message(parser_e_overload_impossible)
  1501. { constref (source) and var (dest) parameter to point the records }
  1502. else if (optoken=_OP_COPY) and
  1503. (
  1504. (pd.parast.SymList.Count<>2) or
  1505. (tparavarsym(pd.parast.SymList[0]).vardef<>pd.struct) or
  1506. (tparavarsym(pd.parast.SymList[0]).varspez<>vs_constref) or
  1507. (tparavarsym(pd.parast.SymList[1]).vardef<>pd.struct) or
  1508. (tparavarsym(pd.parast.SymList[1]).varspez<>vs_var)
  1509. ) then
  1510. Message(parser_e_overload_impossible);
  1511. trecordsymtable(pd.procsym.Owner).includemanagementoperator(
  1512. token2managementoperator(optoken));
  1513. pd.returndef:=voidtype
  1514. end
  1515. else
  1516. if not try_to_consume(_COLON) then
  1517. begin
  1518. consume(_COLON);
  1519. pd.returndef:=generrordef;
  1520. consume_all_until(_SEMICOLON);
  1521. end
  1522. else
  1523. begin
  1524. read_returndef(pd);
  1525. { check that class operators have either return type of structure or }
  1526. { at least one argument of that type }
  1527. if (po_classmethod in pd.procoptions) and
  1528. (pd.returndef <> pd.struct) then
  1529. begin
  1530. found:=false;
  1531. for i := 0 to pd.parast.SymList.Count - 1 do
  1532. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1533. begin
  1534. found:=true;
  1535. break;
  1536. end;
  1537. if not found then
  1538. if assigned(pd.struct) then
  1539. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName)
  1540. else
  1541. MessagePos(pd.fileinfo,type_e_type_id_expected);
  1542. end;
  1543. if not assigned(pd.struct) or assigned(astruct) then
  1544. begin
  1545. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1546. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1547. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1548. message(parser_e_no_such_assignment)
  1549. else if not isoperatoracceptable(pd,optoken) then
  1550. Message(parser_e_overload_impossible);
  1551. end;
  1552. end;
  1553. end;
  1554. else
  1555. internalerror(2015052202);
  1556. end;
  1557. if (pd.proccalloption in cdecl_pocalls) and
  1558. (pd.paras.count>0) and
  1559. is_array_of_const(tparavarsym(pd.paras[pd.paras.count-1]).vardef) then
  1560. begin
  1561. include(pd.procoptions,po_variadic);
  1562. end;
  1563. { support procedure proc stdcall export; }
  1564. if not(check_proc_directive(false)) then
  1565. begin
  1566. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1567. begin
  1568. message(parser_e_field_not_allowed_here);
  1569. consume_all_until(_SEMICOLON);
  1570. end;
  1571. if not (ppf_anonymous in flags) then
  1572. consume(_SEMICOLON);
  1573. end;
  1574. if locationstr<>'' then
  1575. begin
  1576. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1577. { I guess this needs a new message... (KB) }
  1578. message(parser_e_illegal_explicit_paraloc);
  1579. end;
  1580. end;
  1581. function parse_proc_dec(flags:tparse_proc_flags;astruct:tabstractrecorddef):tprocdef;
  1582. var
  1583. pd : tprocdef;
  1584. old_block_type : tblock_type;
  1585. recover : boolean;
  1586. procedure finish_intf_mapping;
  1587. begin
  1588. if token=_COLON then
  1589. begin
  1590. message(parser_e_field_not_allowed_here);
  1591. consume_all_until(_SEMICOLON);
  1592. end;
  1593. consume(_SEMICOLON);
  1594. end;
  1595. begin
  1596. pd:=nil;
  1597. recover:=false;
  1598. case token of
  1599. _FUNCTION :
  1600. begin
  1601. consume(_FUNCTION);
  1602. if parse_proc_head(astruct,potype_function,flags,nil,nil,pd) then
  1603. begin
  1604. { pd=nil when it is a interface mapping }
  1605. if assigned(pd) then
  1606. parse_proc_dec_finish(pd,flags,astruct)
  1607. else
  1608. finish_intf_mapping;
  1609. end
  1610. else
  1611. begin
  1612. { recover }
  1613. consume(_COLON);
  1614. consume_all_until(_SEMICOLON);
  1615. recover:=true;
  1616. end;
  1617. end;
  1618. _PROCEDURE :
  1619. begin
  1620. consume(_PROCEDURE);
  1621. if parse_proc_head(astruct,potype_procedure,flags,nil,nil,pd) then
  1622. begin
  1623. { pd=nil when it is an interface mapping }
  1624. if assigned(pd) then
  1625. parse_proc_dec_finish(pd,flags,astruct)
  1626. else
  1627. finish_intf_mapping;
  1628. end
  1629. else
  1630. recover:=true;
  1631. end;
  1632. _CONSTRUCTOR :
  1633. begin
  1634. consume(_CONSTRUCTOR);
  1635. if ppf_classmethod in flags then
  1636. recover:=not parse_proc_head(astruct,potype_class_constructor,[],nil,nil,pd)
  1637. else
  1638. recover:=not parse_proc_head(astruct,potype_constructor,[],nil,nil,pd);
  1639. if not recover then
  1640. parse_proc_dec_finish(pd,flags,astruct);
  1641. end;
  1642. _DESTRUCTOR :
  1643. begin
  1644. consume(_DESTRUCTOR);
  1645. if ppf_classmethod in flags then
  1646. recover:=not parse_proc_head(astruct,potype_class_destructor,[],nil,nil,pd)
  1647. else
  1648. recover:=not parse_proc_head(astruct,potype_destructor,[],nil,nil,pd);
  1649. if not recover then
  1650. parse_proc_dec_finish(pd,flags,astruct);
  1651. end;
  1652. else
  1653. if (token=_OPERATOR) or
  1654. ((ppf_classmethod in flags) and (idtoken=_OPERATOR)) then
  1655. begin
  1656. { we need to set the block type to bt_body, so that operator names
  1657. like ">", "=>" or "<>" are parsed correctly instead of e.g.
  1658. _LSHARPBRACKET and _RSHARPBRACKET for "<>" }
  1659. old_block_type:=block_type;
  1660. block_type:=bt_body;
  1661. consume(_OPERATOR);
  1662. parse_proc_head(astruct,potype_operator,[],nil,nil,pd);
  1663. block_type:=old_block_type;
  1664. if assigned(pd) then
  1665. parse_proc_dec_finish(pd,flags,astruct)
  1666. else
  1667. begin
  1668. { recover }
  1669. try_to_consume(_ID);
  1670. consume(_COLON);
  1671. consume_all_until(_SEMICOLON);
  1672. recover:=true;
  1673. end;
  1674. end;
  1675. end;
  1676. if recover and not(check_proc_directive(false)) then
  1677. begin
  1678. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1679. begin
  1680. message(parser_e_field_not_allowed_here);
  1681. consume_all_until(_SEMICOLON);
  1682. end;
  1683. if not (ppf_anonymous in flags) then
  1684. consume(_SEMICOLON);
  1685. end;
  1686. { we've parsed the final semicolon, so stop recording tokens }
  1687. if assigned(pd) and
  1688. (df_generic in pd.defoptions) and
  1689. assigned(pd.genericdecltokenbuf) then
  1690. current_scanner.stoprecordtokens;
  1691. result:=pd;
  1692. end;
  1693. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  1694. var
  1695. oldparse_only: boolean;
  1696. flags : tparse_proc_flags;
  1697. begin
  1698. oldparse_only:=parse_only;
  1699. parse_only:=true;
  1700. flags:=[];
  1701. if is_classdef then
  1702. include(flags,ppf_classmethod);
  1703. if hadgeneric then
  1704. include(flags,ppf_generic);
  1705. result:=parse_proc_dec(flags,astruct);
  1706. { this is for error recovery as well as forward }
  1707. { interface mappings, i.e. mapping to a method }
  1708. { which isn't declared yet }
  1709. if assigned(result) then
  1710. begin
  1711. parse_record_proc_directives(result);
  1712. { since records have no inheritance, don't allow non-static
  1713. class methods. Delphi does the same. }
  1714. if (result.proctypeoption<>potype_operator) and
  1715. is_classdef and
  1716. not (po_staticmethod in result.procoptions) then
  1717. MessagePos(result.fileinfo, parser_e_class_methods_only_static_in_records);
  1718. // we can't add hidden params here because record is not yet defined
  1719. // and therefore record size which has influence on parameter passing rules may change too
  1720. // look at record_dec to see where calling conventions are applied (issue #0021044)
  1721. handle_calling_convention(result,hcc_default_actions_intf_struct);
  1722. { add definition to procsym }
  1723. proc_add_definition(result);
  1724. if result.is_generic then
  1725. astruct.symtable.includeoption(sto_has_generic);
  1726. end;
  1727. maybe_parse_hint_directives(result);
  1728. parse_only:=oldparse_only;
  1729. end;
  1730. {****************************************************************************
  1731. Procedure directive handlers
  1732. ****************************************************************************}
  1733. procedure pd_compilerproc(pd:tabstractprocdef);
  1734. var
  1735. v : Tconstexprint;
  1736. begin
  1737. { check for optional syssym index }
  1738. if try_to_consume(_COLON) then
  1739. begin
  1740. v:=get_intconst;
  1741. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1742. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1743. else if not assigned(tsyssym.find_by_number(longint(v.svalue))) then
  1744. message1(parser_e_invalid_internal_function_index,tostr(v))
  1745. else
  1746. tprocdef(pd).extnumber:=longint(v.svalue);
  1747. end;
  1748. end;
  1749. procedure pd_far(pd:tabstractprocdef);
  1750. begin
  1751. pd.declared_far;
  1752. end;
  1753. procedure pd_near(pd:tabstractprocdef);
  1754. begin
  1755. pd.declared_near;
  1756. end;
  1757. procedure pd_export(pd:tabstractprocdef);
  1758. begin
  1759. if pd.typ<>procdef then
  1760. internalerror(200304264);
  1761. if assigned(tprocdef(pd).struct) then
  1762. Message(parser_e_methods_dont_be_export);
  1763. if pd.parast.symtablelevel>normal_function_level then
  1764. Message(parser_e_dont_nest_export);
  1765. end;
  1766. procedure pd_forward(pd:tabstractprocdef);
  1767. begin
  1768. if pd.typ<>procdef then
  1769. internalerror(200304265);
  1770. tprocdef(pd).forwarddef:=true;
  1771. end;
  1772. procedure pd_alias(pd:tabstractprocdef);
  1773. begin
  1774. if pd.typ<>procdef then
  1775. internalerror(200304266);
  1776. consume(_COLON);
  1777. tprocdef(pd).aliasnames.insert(get_stringconst);
  1778. include(pd.procoptions,po_has_public_name);
  1779. end;
  1780. procedure pd_public(pd:tabstractprocdef);
  1781. begin
  1782. if pd.typ<>procdef then
  1783. internalerror(2003042601);
  1784. if try_to_consume(_NAME) then
  1785. begin
  1786. tprocdef(pd).aliasnames.insert(get_stringconst);
  1787. include(pd.procoptions,po_has_public_name);
  1788. end;
  1789. end;
  1790. procedure pd_asmname(pd:tabstractprocdef);
  1791. begin
  1792. if pd.typ<>procdef then
  1793. internalerror(200304267);
  1794. if token=_CCHAR then
  1795. begin
  1796. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1797. consume(_CCHAR)
  1798. end
  1799. else
  1800. begin
  1801. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1802. consume(_CSTRING);
  1803. end;
  1804. { we don't need anything else }
  1805. tprocdef(pd).forwarddef:=false;
  1806. end;
  1807. procedure pd_internconst(pd:tabstractprocdef);
  1808. var v:Tconstexprint;
  1809. begin
  1810. if pd.typ<>procdef then
  1811. internalerror(200304268);
  1812. consume(_COLON);
  1813. v:=get_intconst;
  1814. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1815. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1816. else
  1817. Tprocdef(pd).extnumber:=longint(v.svalue);
  1818. end;
  1819. procedure pd_internproc(pd:tabstractprocdef);
  1820. var v:Tconstexprint;
  1821. begin
  1822. if pd.typ<>procdef then
  1823. internalerror(2003042602);
  1824. consume(_COLON);
  1825. v:=get_intconst;
  1826. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1827. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1828. else
  1829. Tprocdef(pd).extnumber:=longint(v.svalue);
  1830. { the proc is defined }
  1831. tprocdef(pd).forwarddef:=false;
  1832. end;
  1833. procedure pd_interrupt(pd:tabstractprocdef);
  1834. begin
  1835. if pd.parast.symtablelevel>normal_function_level then
  1836. Message(parser_e_dont_nest_interrupt);
  1837. end;
  1838. procedure pd_abstract(pd:tabstractprocdef);
  1839. begin
  1840. if pd.typ<>procdef then
  1841. internalerror(200304269);
  1842. if is_objectpascal_helper(tprocdef(pd).struct) then
  1843. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1844. if assigned(tprocdef(pd).struct) and
  1845. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1846. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1847. else if (po_virtualmethod in pd.procoptions) then
  1848. begin
  1849. include(pd.procoptions,po_abstractmethod);
  1850. { one more abstract method }
  1851. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1852. end
  1853. else
  1854. Message(parser_e_only_virtual_methods_abstract);
  1855. { the method is defined }
  1856. tprocdef(pd).forwarddef:=false;
  1857. end;
  1858. procedure pd_final(pd:tabstractprocdef);
  1859. begin
  1860. if pd.typ<>procdef then
  1861. internalerror(200910170);
  1862. if is_objectpascal_helper(tprocdef(pd).struct) and
  1863. (m_objfpc in current_settings.modeswitches) then
  1864. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1865. if (po_virtualmethod in pd.procoptions) or
  1866. (is_javaclass(tprocdef(pd).struct) and
  1867. (po_classmethod in pd.procoptions)) then
  1868. include(pd.procoptions,po_finalmethod)
  1869. else
  1870. Message(parser_e_only_virtual_methods_final);
  1871. end;
  1872. procedure pd_enumerator(pd:tabstractprocdef);
  1873. begin
  1874. if pd.typ<>procdef then
  1875. internalerror(200910250);
  1876. if (token = _ID) then
  1877. begin
  1878. if pattern='MOVENEXT' then
  1879. begin
  1880. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1881. message(parser_e_only_one_enumerator_movenext);
  1882. pd.calcparas;
  1883. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1884. (pd.minparacount = 0) then
  1885. begin
  1886. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1887. include(pd.procoptions,po_enumerator_movenext);
  1888. end
  1889. else
  1890. Message(parser_e_enumerator_movenext_is_not_valid)
  1891. end
  1892. else
  1893. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1894. consume(token);
  1895. end
  1896. else
  1897. Message(parser_e_enumerator_identifier_required);
  1898. end;
  1899. procedure pd_virtual(pd:tabstractprocdef);
  1900. {$ifdef WITHDMT}
  1901. var
  1902. pt : tnode;
  1903. {$endif WITHDMT}
  1904. begin
  1905. if assigned(pd.owner) and
  1906. (not assigned(pd.owner.defowner) or
  1907. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1908. (po_external in pd.procoptions) then
  1909. Message2(parser_e_proc_dir_conflict,'EXTERNAL','"VIRTUAL"');
  1910. if pd.typ<>procdef then
  1911. internalerror(2003042610);
  1912. if (pd.proctypeoption=potype_constructor) and
  1913. is_object(tprocdef(pd).struct) then
  1914. Message(parser_e_constructor_cannot_be_not_virtual);
  1915. if pd.is_generic then
  1916. message(parser_e_genfuncs_cannot_be_virtual);
  1917. if is_objectpascal_helper(tprocdef(pd).struct) and
  1918. (m_objfpc in current_settings.modeswitches) then
  1919. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1920. {$ifdef WITHDMT}
  1921. if is_object(tprocdef(pd).struct) and
  1922. (token<>_SEMICOLON) then
  1923. begin
  1924. { any type of parameter is allowed here! }
  1925. pt:=comp_expr(true);
  1926. if is_constintnode(pt) then
  1927. begin
  1928. include(pd.procoptions,po_msgint);
  1929. pd.messageinf.i:=pt.value;
  1930. end
  1931. else
  1932. Message(parser_e_ill_msg_expr);
  1933. disposetree(pt);
  1934. end;
  1935. {$endif WITHDMT}
  1936. end;
  1937. procedure pd_dispid(pd:tabstractprocdef);
  1938. var pt:Tnode;
  1939. begin
  1940. if pd.typ<>procdef then
  1941. internalerror(200604301);
  1942. pt:=comp_expr([ef_accept_equal]);
  1943. if is_constintnode(pt) then
  1944. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1945. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(longint)),tostr(high(longint)))
  1946. else
  1947. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1948. else
  1949. message(parser_e_dispid_must_be_ord_const);
  1950. pt.free;
  1951. pt := nil;
  1952. end;
  1953. procedure pd_static(pd:tabstractprocdef);
  1954. begin
  1955. if pd.typ<>procdef then
  1956. internalerror(2013032001);
  1957. if not assigned(tprocdef(pd).struct) then
  1958. internalerror(2013032002);
  1959. include(tprocdef(pd).procsym.symoptions,sp_static);
  1960. { "static" is not allowed for operators or normal methods (except in objects) }
  1961. if (pd.proctypeoption=potype_operator) or
  1962. (
  1963. not (po_classmethod in pd.procoptions) and
  1964. not is_object(tprocdef(pd).struct)
  1965. )
  1966. then
  1967. Message1(parser_e_dir_not_allowed,arraytokeninfo[_STATIC].str);
  1968. include(pd.procoptions,po_staticmethod);
  1969. end;
  1970. procedure pd_override(pd:tabstractprocdef);
  1971. begin
  1972. if pd.typ<>procdef then
  1973. internalerror(2003042611);
  1974. if is_objectpascal_helper(tprocdef(pd).struct) then
  1975. begin
  1976. if m_objfpc in current_settings.modeswitches then
  1977. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1978. end
  1979. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1980. Message(parser_e_no_object_override)
  1981. else if is_objccategory(tprocdef(pd).struct) then
  1982. Message(parser_e_no_category_override)
  1983. else if (po_external in pd.procoptions) and
  1984. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1985. not is_cppclass(tprocdef(pd).struct) and
  1986. not is_java_class_or_interface(tprocdef(pd).struct) then
  1987. Message2(parser_e_proc_dir_conflict,'OVERRIDE','"EXTERNAL"');
  1988. end;
  1989. procedure pd_overload(pd:tabstractprocdef);
  1990. begin
  1991. if pd.typ<>procdef then
  1992. internalerror(2003042612);
  1993. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1994. end;
  1995. procedure pd_message(pd:tabstractprocdef);
  1996. var
  1997. pt : tnode;
  1998. paracnt : longint;
  1999. begin
  2000. if pd.typ<>procdef then
  2001. internalerror(2003042613);
  2002. if is_objectpascal_helper(tprocdef(pd).struct) then
  2003. begin
  2004. if m_objfpc in current_settings.modeswitches then
  2005. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  2006. end
  2007. else
  2008. if not is_class(tprocdef(pd).struct) and
  2009. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  2010. Message(parser_e_msg_only_for_classes);
  2011. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  2012. Message(parser_e_multiple_messages);
  2013. { check parameter type }
  2014. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  2015. begin
  2016. if po_external in pd.procoptions then
  2017. Message2(parser_e_proc_dir_conflict,'MESSAGE','"EXTERNAL"');
  2018. paracnt:=0;
  2019. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  2020. if paracnt<>1 then
  2021. Message(parser_e_ill_msg_param);
  2022. end;
  2023. pt:=comp_expr([ef_accept_equal]);
  2024. { message is 1-character long }
  2025. if is_constcharnode(pt) then
  2026. begin
  2027. include(pd.procoptions,po_msgstr);
  2028. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  2029. end
  2030. else if pt.nodetype=stringconstn then
  2031. begin
  2032. include(pd.procoptions,po_msgstr);
  2033. if (tstringconstnode(pt).len>255) then
  2034. Message(parser_e_message_string_too_long);
  2035. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).asconstpchar);
  2036. end
  2037. else
  2038. if is_constintnode(pt) and
  2039. (is_class(tprocdef(pd).struct) or
  2040. is_objectpascal_helper(tprocdef(pd).struct)) then
  2041. begin
  2042. include(pd.procoptions,po_msgint);
  2043. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  2044. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  2045. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(Tprocdef(pd).messageinf.i)),tostr(high(Tprocdef(pd).messageinf.i)))
  2046. else
  2047. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  2048. end
  2049. else
  2050. Message(parser_e_ill_msg_expr);
  2051. { check whether the selector name is valid in case of Objective-C }
  2052. if (po_msgstr in pd.procoptions) and
  2053. is_objc_class_or_protocol(tprocdef(pd).struct) and
  2054. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  2055. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  2056. pt.free;
  2057. pt := nil;
  2058. end;
  2059. procedure pd_reintroduce(pd:tabstractprocdef);
  2060. begin
  2061. if pd.typ<>procdef then
  2062. internalerror(200401211);
  2063. if is_objectpascal_helper(tprocdef(pd).struct) then
  2064. begin
  2065. if m_objfpc in current_settings.modeswitches then
  2066. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  2067. end
  2068. else
  2069. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  2070. not(is_objccategory(tprocdef(pd).struct)) and
  2071. not(is_javaclass(tprocdef(pd).struct)) then
  2072. Message(parser_e_no_object_reintroduce);
  2073. end;
  2074. procedure pd_syscall(pd:tabstractprocdef);
  2075. procedure include_po_syscall;
  2076. var
  2077. syscall: psyscallinfo;
  2078. begin
  2079. case target_info.system of
  2080. system_arm_palmos,
  2081. system_m68k_palmos,
  2082. system_m68k_human68k,
  2083. system_m68k_atari,
  2084. system_m68k_amiga,
  2085. system_powerpc_amiga:
  2086. include(pd.procoptions,get_default_syscall);
  2087. system_powerpc_morphos,
  2088. system_arm_aros,
  2089. system_i386_aros,
  2090. system_x86_64_aros:
  2091. begin
  2092. syscall:=get_syscall_by_token(idtoken);
  2093. if assigned(syscall) then
  2094. begin
  2095. if target_info.system in syscall^.validon then
  2096. begin
  2097. consume(idtoken);
  2098. include(pd.procoptions,syscall^.procoption);
  2099. end
  2100. end
  2101. else
  2102. include(pd.procoptions,get_default_syscall);
  2103. end;
  2104. else
  2105. Message(parser_e_syscall_format_not_support);
  2106. end;
  2107. end;
  2108. function po_syscall_to_varoptions: tvaroptions;
  2109. begin
  2110. result:=[vo_is_syscall_lib,vo_is_hidden_para];
  2111. if ([po_syscall_legacy,po_syscall_basereg,po_syscall_basenone] * tprocdef(pd).procoptions) <> [] then
  2112. include(result,vo_has_explicit_paraloc);
  2113. end;
  2114. function po_syscall_to_regname: string;
  2115. begin
  2116. if po_syscall_legacy in tprocdef(pd).procoptions then
  2117. result:='a6'
  2118. { let nobase on MorphOS store the libbase in r12 as well, because
  2119. we will need the libbase anyway during the call generation }
  2120. else if (po_syscall_basenone in tprocdef(pd).procoptions) and
  2121. (target_info.system = system_powerpc_morphos) then
  2122. result:='r12'
  2123. else if po_syscall_basereg in tprocdef(pd).procoptions then
  2124. begin
  2125. case target_info.system of
  2126. system_i386_aros:
  2127. result:='eax';
  2128. system_x86_64_aros:
  2129. result:='r12';
  2130. system_powerpc_morphos:
  2131. result:='r12';
  2132. else
  2133. internalerror(2016090201);
  2134. end;
  2135. end
  2136. else
  2137. internalerror(2016090101);
  2138. end;
  2139. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2140. const
  2141. syscall_paranr: array[boolean] of aint =
  2142. ( paranr_syscall_lib_last, paranr_syscall_lib_first );
  2143. var
  2144. vs : tparavarsym;
  2145. sym : tsym;
  2146. symtable : TSymtable;
  2147. v: Tconstexprint;
  2148. vo: tvaroptions;
  2149. paranr: aint;
  2150. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2151. begin
  2152. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  2153. internalerror(2003042614);
  2154. tprocdef(pd).forwarddef:=false;
  2155. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2156. include_po_syscall;
  2157. if target_info.system in [system_arm_palmos, system_m68k_palmos] then
  2158. begin
  2159. v:=get_intconst;
  2160. tprocdef(pd).extnumber:=longint(v.svalue);
  2161. if ((v<0) or (v>high(word))) then
  2162. message(parser_e_range_check_error);
  2163. if try_to_consume(_COMMA) then
  2164. begin
  2165. v:=get_intconst;
  2166. if ((v<0) or (v>high(word))) then
  2167. message(parser_e_range_check_error);
  2168. tprocdef(pd).import_nr:=longint(v.svalue);
  2169. include(pd.procoptions,po_syscall_has_importnr);
  2170. end;
  2171. exit;
  2172. end;
  2173. if target_info.system = system_m68k_atari then
  2174. begin
  2175. v:=get_intconst;
  2176. if ((v<0) or (v>15)) then
  2177. message(parser_e_range_check_error)
  2178. else
  2179. tprocdef(pd).extnumber:=longint(v.svalue);
  2180. v:=get_intconst;
  2181. if ((v<0) or (v>high(smallint))) then
  2182. message(parser_e_range_check_error)
  2183. else
  2184. tprocdef(pd).import_nr:=longint(v.svalue);
  2185. exit;
  2186. end;
  2187. if target_info.system = system_m68k_human68k then
  2188. begin
  2189. v:=get_intconst;
  2190. if ((v<$ff00) or (v>high(word))) then
  2191. message(parser_e_range_check_error)
  2192. else
  2193. tprocdef(pd).extnumber:=longint(v.svalue);
  2194. exit;
  2195. end;
  2196. if consume_sym(sym,symtable) then
  2197. if ((sym.typ=staticvarsym) or
  2198. (sym.typ=absolutevarsym) and (tabsolutevarsym(sym).abstyp=toaddr)) and
  2199. ((tabstractvarsym(sym).vardef.typ=pointerdef) or
  2200. is_32bitint(tabstractvarsym(sym).vardef)) then
  2201. begin
  2202. include(pd.procoptions,po_syscall_has_libsym);
  2203. tcpuprocdef(pd).libsym:=sym;
  2204. vo:=po_syscall_to_varoptions;
  2205. paranr:=syscall_paranr[po_syscall_basefirst in tprocdef(pd).procoptions];
  2206. vs:=cparavarsym.create('$syscalllib',paranr,vs_value,tabstractvarsym(sym).vardef,vo);
  2207. if vo_has_explicit_paraloc in vo then
  2208. if not paramanager.parseparaloc(vs,po_syscall_to_regname) then
  2209. internalerror(2016120301);
  2210. pd.parast.insertsym(vs);
  2211. end
  2212. else
  2213. Message(parser_e_32bitint_or_pointer_variable_expected);
  2214. paramanager.create_funcretloc_info(pd,calleeside);
  2215. paramanager.create_funcretloc_info(pd,callerside);
  2216. v:=get_intconst;
  2217. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  2218. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(Tprocdef(pd).extnumber)),tostr(high(Tprocdef(pd).extnumber)))
  2219. else
  2220. if target_info.system in [system_arm_aros,system_i386_aros,system_x86_64_aros] then
  2221. Tprocdef(pd).extnumber:=v.uvalue * sizeof(pint)
  2222. else
  2223. Tprocdef(pd).extnumber:=v.uvalue;
  2224. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2225. end;
  2226. procedure pd_external(pd:tabstractprocdef);
  2227. {
  2228. If import_dll=nil the procedure is assumed to be in another
  2229. object file. In that object file it should have the name to
  2230. which import_name is pointing to. Otherwise, the procedure is
  2231. assumed to be in the DLL to which import_dll is pointing to. In
  2232. that case either import_nr<>0 or import_name<>nil is true, so
  2233. the procedure is either imported by number or by name. (DM)
  2234. }
  2235. var
  2236. hs : string;
  2237. v:Tconstexprint;
  2238. is_java_external: boolean;
  2239. begin
  2240. if pd.typ<>procdef then
  2241. internalerror(2003042615);
  2242. { Allow specifying a separate external name for methods in external Java
  2243. because its identifier naming constraints are laxer than FPC's
  2244. (e.g., case sensitive).
  2245. Limitation: only allows specifying the symbol name and not the package name,
  2246. and only for external classes/interfaces }
  2247. is_java_external:=
  2248. (pd.typ=procdef) and
  2249. assigned(pd.owner) and
  2250. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  2251. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  2252. with tprocdef(pd) do
  2253. begin
  2254. forwarddef:=false;
  2255. { forbid local external procedures }
  2256. if parast.symtablelevel>normal_function_level then
  2257. Message(parser_e_no_local_proc_external);
  2258. { If the procedure should be imported from a DLL, a constant string follows.
  2259. This isn't really correct, an contant string expression follows
  2260. so we check if an semicolon follows, else a string constant have to
  2261. follow (FK) }
  2262. if not is_java_external and
  2263. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  2264. begin
  2265. { Always add library prefix and suffix to create an uniform name }
  2266. hs:=get_stringconst;
  2267. if ExtractFileExt(hs)='' then
  2268. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  2269. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  2270. hs:=target_info.sharedlibprefix+hs;
  2271. { the JVM expects java/lang/Object rather than java.lang.Object }
  2272. if target_info.system in systems_jvm then
  2273. Replace(hs,'.','/');
  2274. import_dll:=stringdup(hs);
  2275. include(procoptions,po_has_importdll);
  2276. if (idtoken=_NAME) then
  2277. begin
  2278. consume(_NAME);
  2279. import_name:=stringdup(get_stringconst);
  2280. include(procoptions,po_has_importname);
  2281. if import_name^='' then
  2282. message(parser_e_empty_import_name);
  2283. end;
  2284. if (idtoken=_INDEX) then
  2285. begin
  2286. {After the word index follows the index number in the DLL.}
  2287. consume(_INDEX);
  2288. v:=get_intconst;
  2289. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  2290. message(parser_e_range_check_error)
  2291. else
  2292. import_nr:=longint(v.svalue);
  2293. end;
  2294. if (idtoken=_SUSPENDING) then
  2295. begin
  2296. if (target_info.system in systems_wasm) then
  2297. begin
  2298. consume(_SUSPENDING);
  2299. include(procoptions,po_wasm_suspending);
  2300. synthetickind:=tsk_wasm_suspending_first;
  2301. if idtoken=_FIRST then
  2302. consume(_FIRST)
  2303. else if idtoken=_LAST then
  2304. begin
  2305. consume(_LAST);
  2306. synthetickind:=tsk_wasm_suspending_last;
  2307. end;
  2308. end
  2309. else
  2310. begin
  2311. message(parser_e_suspending_externals_not_supported_on_current_platform);
  2312. consume(_SUSPENDING);
  2313. if idtoken=_FIRST then
  2314. consume(_FIRST)
  2315. else if idtoken=_LAST then
  2316. consume(_LAST);
  2317. end;
  2318. end;
  2319. { default is to used the realname of the procedure }
  2320. if (import_nr=0) and not assigned(import_name) then
  2321. begin
  2322. import_name:=stringdup(procsym.realname);
  2323. include(procoptions,po_has_importname);
  2324. end;
  2325. end
  2326. else
  2327. begin
  2328. if (idtoken=_NAME) or
  2329. is_java_external then
  2330. begin
  2331. consume(_NAME);
  2332. import_name:=stringdup(get_stringconst);
  2333. include(procoptions,po_has_importname);
  2334. if import_name^='' then
  2335. message(parser_e_empty_import_name);
  2336. end;
  2337. end;
  2338. end;
  2339. end;
  2340. procedure pd_weakexternal(pd:tabstractprocdef);
  2341. begin
  2342. if not(target_info.system in systems_weak_linking) then
  2343. message(parser_e_weak_external_not_supported)
  2344. else
  2345. pd_external(pd);
  2346. end;
  2347. procedure pd_winapi(pd:tabstractprocdef);
  2348. begin
  2349. if not(target_info.system in systems_all_windows+[system_i386_nativent]) then
  2350. pd.proccalloption:=pocall_cdecl
  2351. else
  2352. pd.proccalloption:=pocall_stdcall;
  2353. include(pd.procoptions,po_hascallingconvention);
  2354. end;
  2355. procedure pd_hardfloat(pd:tabstractprocdef);
  2356. begin
  2357. if
  2358. {$if defined(arm)}
  2359. (current_settings.fputype=fpu_soft) or
  2360. {$endif defined(arm)}
  2361. (cs_fp_emulation in current_settings.moduleswitches) then
  2362. message(parser_e_cannot_use_hardfloat_in_a_softfloat_environment);
  2363. end;
  2364. procedure pd_section(pd:tabstractprocdef);
  2365. begin
  2366. if pd.typ<>procdef then
  2367. internalerror(2021032801);
  2368. if not (target_info.system in systems_allow_section) then
  2369. Message(parser_e_section_directive_not_allowed_for_target);
  2370. {$ifdef symansistr}
  2371. tprocdef(pd).section:=get_stringconst;
  2372. {$else symansistr}
  2373. tprocdef(pd).section:=stringdup(get_stringconst);
  2374. {$endif}
  2375. end;
  2376. type
  2377. pd_handler=procedure(pd:tabstractprocdef);
  2378. proc_dir_rec=record
  2379. idtok : ttoken;
  2380. pd_flags : tpdflags;
  2381. handler : pd_handler;
  2382. pocall : tproccalloption;
  2383. pooption : tprocoptions;
  2384. mutexclpocall : tproccalloptions;
  2385. mutexclpotype : tproctypeoptions;
  2386. mutexclpo : tprocoptions;
  2387. end;
  2388. const
  2389. {Should contain the number of procedure directives we support.}
  2390. num_proc_directives=55;
  2391. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  2392. (
  2393. (
  2394. idtok:_ABSTRACT;
  2395. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2396. handler : @pd_abstract;
  2397. pocall : pocall_none;
  2398. pooption : [po_abstractmethod];
  2399. mutexclpocall : [pocall_internproc];
  2400. mutexclpotype : [];
  2401. mutexclpo : [po_exports,po_interrupt,po_inline]
  2402. ),(
  2403. idtok:_ALIAS;
  2404. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2405. handler : @pd_alias;
  2406. pocall : pocall_none;
  2407. pooption : [];
  2408. mutexclpocall : [];
  2409. mutexclpotype : [];
  2410. mutexclpo : [po_external,po_inline]
  2411. ),(
  2412. idtok:_ASMNAME;
  2413. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  2414. handler : @pd_asmname;
  2415. pocall : pocall_cdecl;
  2416. pooption : [po_external];
  2417. mutexclpocall : [pocall_internproc];
  2418. mutexclpotype : [];
  2419. mutexclpo : [po_external,po_inline]
  2420. ),(
  2421. idtok:_ASSEMBLER;
  2422. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2423. handler : nil;
  2424. pocall : pocall_none;
  2425. pooption : [po_assembler];
  2426. mutexclpocall : [];
  2427. mutexclpotype : [];
  2428. mutexclpo : [po_external]
  2429. ),(
  2430. idtok:_C; {same as cdecl for mode mac}
  2431. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2432. handler : nil;
  2433. pocall : pocall_cdecl;
  2434. pooption : [];
  2435. mutexclpocall : [];
  2436. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2437. mutexclpo : [po_assembler,po_external]
  2438. ),(
  2439. idtok:_CBLOCK;
  2440. pd_flags : [pd_procvar];
  2441. handler : nil;
  2442. pocall : pocall_none;
  2443. pooption : [po_is_block];
  2444. mutexclpocall : [];
  2445. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2446. mutexclpo : [po_assembler,po_external]
  2447. ),(
  2448. idtok:_CDECL;
  2449. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2450. handler : nil;
  2451. pocall : pocall_cdecl;
  2452. pooption : [];
  2453. mutexclpocall : [];
  2454. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2455. mutexclpo : [po_assembler,po_external]
  2456. ),(
  2457. idtok:_DISPID;
  2458. pd_flags : [pd_dispinterface];
  2459. handler : @pd_dispid;
  2460. pocall : pocall_none;
  2461. pooption : [po_dispid];
  2462. mutexclpocall : [pocall_internproc];
  2463. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2464. mutexclpo : [po_interrupt,po_external,po_inline]
  2465. ),(
  2466. idtok:_DYNAMIC;
  2467. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2468. handler : @pd_virtual;
  2469. pocall : pocall_none;
  2470. pooption : [po_virtualmethod];
  2471. mutexclpocall : [pocall_internproc];
  2472. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2473. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  2474. ),(
  2475. idtok:_EXPORT;
  2476. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  2477. handler : @pd_export;
  2478. pocall : pocall_none;
  2479. pooption : [po_exports,po_global];
  2480. mutexclpocall : [pocall_internproc];
  2481. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2482. mutexclpo : [po_external,po_interrupt,po_inline]
  2483. ),(
  2484. idtok:_EXTERNAL;
  2485. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  2486. handler : @pd_external;
  2487. pocall : pocall_none;
  2488. pooption : [po_external];
  2489. mutexclpocall : [pocall_syscall];
  2490. { allowed for external cpp classes }
  2491. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2492. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2493. ),(
  2494. idtok:_FAR;
  2495. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2496. handler : @pd_far;
  2497. pocall : pocall_none;
  2498. pooption : [];
  2499. mutexclpocall : [pocall_internproc];
  2500. mutexclpotype : [];
  2501. mutexclpo : [po_inline]
  2502. ),(
  2503. idtok:_FAR16;
  2504. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  2505. handler : nil;
  2506. pocall : pocall_far16;
  2507. pooption : [];
  2508. mutexclpocall : [];
  2509. mutexclpotype : [];
  2510. mutexclpo : [po_external]
  2511. ),(
  2512. idtok:_FINAL;
  2513. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2514. handler : @pd_final;
  2515. pocall : pocall_none;
  2516. pooption : [po_finalmethod];
  2517. mutexclpocall : [pocall_internproc];
  2518. mutexclpotype : [];
  2519. mutexclpo : [po_exports,po_interrupt,po_inline]
  2520. ),(
  2521. idtok:_FORWARD;
  2522. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2523. handler : @pd_forward;
  2524. pocall : pocall_none;
  2525. pooption : [];
  2526. mutexclpocall : [pocall_internproc];
  2527. mutexclpotype : [];
  2528. mutexclpo : [po_external,po_inline]
  2529. ),(
  2530. idtok:_OLDFPCCALL;
  2531. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2532. handler : nil;
  2533. pocall : pocall_oldfpccall;
  2534. pooption : [];
  2535. mutexclpocall : [];
  2536. mutexclpotype : [];
  2537. mutexclpo : []
  2538. ),(
  2539. idtok:_INLINE;
  2540. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2541. handler : nil;
  2542. pocall : pocall_none;
  2543. pooption : [po_inline];
  2544. mutexclpocall : [pocall_safecall];
  2545. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2546. mutexclpo : [po_noinline,po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck]
  2547. ),(
  2548. idtok:_NOINLINE;
  2549. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2550. handler : nil;
  2551. pocall : pocall_none;
  2552. pooption : [po_noinline];
  2553. mutexclpocall : [];
  2554. mutexclpotype : [];
  2555. mutexclpo : [po_inline,po_external]
  2556. ),(
  2557. idtok:_INTERNCONST;
  2558. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2559. handler : @pd_internconst;
  2560. pocall : pocall_none;
  2561. pooption : [po_internconst];
  2562. mutexclpocall : [];
  2563. mutexclpotype : [potype_operator];
  2564. mutexclpo : []
  2565. ),(
  2566. idtok:_INTERNPROC;
  2567. pd_flags : [pd_interface,pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2568. handler : @pd_internproc;
  2569. pocall : pocall_internproc;
  2570. pooption : [];
  2571. mutexclpocall : [];
  2572. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2573. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2574. ),(
  2575. idtok:_INTERRUPT;
  2576. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2577. handler : @pd_interrupt;
  2578. {$ifdef i386}
  2579. pocall : pocall_oldfpccall;
  2580. {$else i386}
  2581. pocall : pocall_stdcall;
  2582. {$endif i386}
  2583. pooption : [po_interrupt];
  2584. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,pocall_mwpascal,
  2585. pocall_pascal,pocall_far16,pocall_oldfpccall,pocall_sysv_abi_cdecl,pocall_ms_abi_cdecl];
  2586. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2587. mutexclpo : [po_external,po_inline,po_exports]
  2588. ),(
  2589. idtok:_IOCHECK;
  2590. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2591. handler : nil;
  2592. pocall : pocall_none;
  2593. pooption : [po_iocheck];
  2594. mutexclpocall : [pocall_internproc];
  2595. mutexclpotype : [];
  2596. mutexclpo : [po_external]
  2597. ),(
  2598. idtok:_LOCAL;
  2599. pd_flags : [pd_implemen,pd_body];
  2600. handler : nil;
  2601. pocall : pocall_none;
  2602. pooption : [po_kylixlocal];
  2603. mutexclpocall : [pocall_internproc,pocall_far16];
  2604. mutexclpotype : [];
  2605. mutexclpo : [po_external,po_exports]
  2606. ),(
  2607. idtok:_MESSAGE;
  2608. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2609. handler : @pd_message;
  2610. pocall : pocall_none;
  2611. pooption : []; { can be po_msgstr or po_msgint }
  2612. mutexclpocall : [pocall_internproc];
  2613. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2614. mutexclpo : [po_interrupt,po_inline]
  2615. ),(
  2616. idtok:_MWPASCAL;
  2617. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2618. handler : nil;
  2619. pocall : pocall_mwpascal;
  2620. pooption : [];
  2621. mutexclpocall : [];
  2622. mutexclpotype : [];
  2623. mutexclpo : []
  2624. ),(
  2625. idtok:_NEAR;
  2626. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2627. handler : @pd_near;
  2628. pocall : pocall_none;
  2629. pooption : [];
  2630. mutexclpocall : [pocall_internproc];
  2631. mutexclpotype : [];
  2632. mutexclpo : []
  2633. ),(
  2634. idtok:_NORETURN;
  2635. pd_flags : [pd_implemen,pd_interface,pd_body,pd_notobjintf];
  2636. handler : nil;
  2637. pocall : pocall_none;
  2638. pooption : [po_noreturn];
  2639. mutexclpocall : [];
  2640. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2641. mutexclpo : [po_interrupt,po_virtualmethod,po_iocheck]
  2642. ),(
  2643. idtok:_NOSTACKFRAME;
  2644. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2645. handler : nil;
  2646. pocall : pocall_none;
  2647. pooption : [po_nostackframe];
  2648. mutexclpocall : [pocall_internproc];
  2649. mutexclpotype : [];
  2650. mutexclpo : []
  2651. ),(
  2652. idtok:_OVERLOAD;
  2653. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava,pd_objcclass,pd_objcprot];
  2654. handler : @pd_overload;
  2655. pocall : pocall_none;
  2656. pooption : [po_overload];
  2657. mutexclpocall : [pocall_internproc];
  2658. mutexclpotype : [];
  2659. mutexclpo : []
  2660. ),(
  2661. idtok:_OVERRIDE;
  2662. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2663. handler : @pd_override;
  2664. pocall : pocall_none;
  2665. pooption : [po_overridingmethod,po_virtualmethod];
  2666. mutexclpocall : [pocall_internproc];
  2667. mutexclpotype : [];
  2668. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2669. ),(
  2670. idtok:_PASCAL;
  2671. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2672. handler : nil;
  2673. pocall : pocall_pascal;
  2674. pooption : [];
  2675. mutexclpocall : [];
  2676. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2677. mutexclpo : [po_external]
  2678. ),(
  2679. idtok:_PUBLIC;
  2680. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2681. handler : @pd_public;
  2682. pocall : pocall_none;
  2683. pooption : [po_public,po_global];
  2684. mutexclpocall : [pocall_internproc];
  2685. mutexclpotype : [];
  2686. mutexclpo : [po_external,po_inline]
  2687. ),(
  2688. idtok:_REGISTER;
  2689. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2690. handler : nil;
  2691. pocall : pocall_register;
  2692. pooption : [];
  2693. mutexclpocall : [];
  2694. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2695. mutexclpo : [po_external]
  2696. ),(
  2697. idtok:_REINTRODUCE;
  2698. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2699. handler : @pd_reintroduce;
  2700. pocall : pocall_none;
  2701. pooption : [po_reintroduce];
  2702. mutexclpocall : [pocall_internproc];
  2703. mutexclpotype : [];
  2704. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2705. ),(
  2706. idtok:_SAFECALL;
  2707. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2708. handler : nil;
  2709. pocall : pocall_safecall;
  2710. pooption : [];
  2711. mutexclpocall : [];
  2712. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2713. mutexclpo : [po_external]
  2714. ),(
  2715. idtok:_SECTION;
  2716. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2717. handler : @pd_section;
  2718. pocall : pocall_none;
  2719. pooption : [po_public,po_global];
  2720. mutexclpocall : [pocall_internproc];
  2721. mutexclpotype : [];
  2722. mutexclpo : [po_external,po_inline,po_interrupt]
  2723. ),(
  2724. idtok:_SOFTFLOAT;
  2725. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2726. handler : nil;
  2727. pocall : pocall_softfloat;
  2728. pooption : [];
  2729. mutexclpocall : [];
  2730. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2731. { it's available with po_external because the libgcc floating point routines on the arm
  2732. uses this calling convention }
  2733. mutexclpo : []
  2734. ),(
  2735. idtok:_STATIC;
  2736. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2737. handler : @pd_static;
  2738. pocall : pocall_none;
  2739. pooption : [po_staticmethod];
  2740. mutexclpocall : [pocall_internproc];
  2741. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2742. mutexclpo : [po_interrupt,po_exports,po_virtualmethod]
  2743. ),(
  2744. idtok:_STDCALL;
  2745. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2746. handler : nil;
  2747. pocall : pocall_stdcall;
  2748. pooption : [];
  2749. mutexclpocall : [];
  2750. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2751. mutexclpo : [po_external]
  2752. ),(
  2753. idtok:_SYSCALL;
  2754. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2755. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2756. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2757. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2758. handler : @pd_syscall;
  2759. pocall : pocall_syscall;
  2760. pooption : [];
  2761. mutexclpocall : [];
  2762. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2763. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2764. ),(
  2765. idtok:_VIRTUAL;
  2766. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2767. handler : @pd_virtual;
  2768. pocall : pocall_none;
  2769. pooption : [po_virtualmethod];
  2770. mutexclpocall : [pocall_internproc];
  2771. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2772. mutexclpo : PD_VIRTUAL_MUTEXCLPO
  2773. ),(
  2774. idtok:_CPPDECL;
  2775. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2776. handler : nil;
  2777. pocall : pocall_cppdecl;
  2778. pooption : [];
  2779. mutexclpocall : [];
  2780. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2781. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2782. ),(
  2783. idtok:_VARARGS;
  2784. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2785. handler : nil;
  2786. pocall : pocall_none;
  2787. pooption : [po_varargs];
  2788. mutexclpocall : [pocall_internproc,pocall_register,
  2789. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2790. mutexclpotype : [];
  2791. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2792. ),(
  2793. idtok:_COMPILERPROC;
  2794. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2795. handler : @pd_compilerproc;
  2796. pocall : pocall_none;
  2797. pooption : [po_compilerproc];
  2798. mutexclpocall : [];
  2799. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2800. mutexclpo : [po_interrupt]
  2801. ),(
  2802. idtok:_WEAKEXTERNAL;
  2803. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2804. handler : @pd_weakexternal;
  2805. pocall : pocall_none;
  2806. { mark it both external and weak external, so we don't have to
  2807. adapt all code for external symbols to also check for weak external
  2808. }
  2809. pooption : [po_external,po_weakexternal];
  2810. mutexclpocall : [pocall_internproc,pocall_syscall];
  2811. { allowed for external cpp classes }
  2812. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2813. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2814. ),(
  2815. idtok:_WINAPI;
  2816. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2817. handler : @pd_winapi;
  2818. pocall : pocall_none;
  2819. pooption : [];
  2820. mutexclpocall : [pocall_stdcall,pocall_cdecl,pocall_mwpascal,pocall_sysv_abi_cdecl,pocall_ms_abi_cdecl];
  2821. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2822. mutexclpo : [po_external]
  2823. ),(
  2824. idtok:_ENUMERATOR;
  2825. pd_flags : [pd_interface,pd_object,pd_record];
  2826. handler : @pd_enumerator;
  2827. pocall : pocall_none;
  2828. pooption : [];
  2829. mutexclpocall : [pocall_internproc];
  2830. mutexclpotype : [];
  2831. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2832. ),(
  2833. idtok:_RTLPROC;
  2834. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2835. handler : nil;
  2836. pocall : pocall_none;
  2837. pooption : [po_rtlproc];
  2838. mutexclpocall : [];
  2839. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2840. mutexclpo : [po_interrupt]
  2841. ),(
  2842. idtok:_HARDFLOAT;
  2843. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2844. handler : @pd_hardfloat;
  2845. pocall : pocall_hardfloat;
  2846. pooption : [];
  2847. mutexclpocall : [];
  2848. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2849. { it's available with po_external because the libgcc floating point routines on the arm
  2850. uses this calling convention }
  2851. mutexclpo : []
  2852. ),(
  2853. idtok:_SYSV_ABI_DEFAULT;
  2854. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2855. handler : nil;
  2856. pocall : pocall_sysv_abi_default;
  2857. pooption : [];
  2858. mutexclpocall : [];
  2859. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2860. mutexclpo : [po_interrupt]
  2861. ),(
  2862. idtok:_SYSV_ABI_CDECL;
  2863. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2864. handler : nil;
  2865. pocall : pocall_sysv_abi_cdecl;
  2866. pooption : [];
  2867. mutexclpocall : [];
  2868. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2869. mutexclpo : [po_interrupt]
  2870. ),(
  2871. idtok:_MS_ABI_DEFAULT;
  2872. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2873. handler : nil;
  2874. pocall : pocall_ms_abi_default;
  2875. pooption : [];
  2876. mutexclpocall : [];
  2877. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2878. mutexclpo : [po_interrupt]
  2879. ),(
  2880. idtok:_MS_ABI_CDECL;
  2881. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2882. handler : nil;
  2883. pocall : pocall_ms_abi_cdecl;
  2884. pooption : [];
  2885. mutexclpocall : [];
  2886. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2887. mutexclpo : [po_interrupt]
  2888. ),(
  2889. idtok:_VECTORCALL;
  2890. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2891. handler : nil;
  2892. pocall : pocall_vectorcall;
  2893. pooption : [];
  2894. mutexclpocall : [];
  2895. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2896. mutexclpo : [po_interrupt]
  2897. ),(
  2898. idtok:_WASMFUNCREF;
  2899. pd_flags : [pd_procvar];
  2900. handler : nil;
  2901. pocall : pocall_none;
  2902. pooption : [po_wasm_funcref];
  2903. mutexclpocall : [];
  2904. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2905. mutexclpo : [po_interrupt]
  2906. )
  2907. );
  2908. function check_proc_directive(isprocvar:boolean):boolean;
  2909. var
  2910. i : longint;
  2911. begin
  2912. result:=false;
  2913. for i:=1 to num_proc_directives do
  2914. if proc_direcdata[i].idtok=idtoken then
  2915. begin
  2916. if ((not isprocvar) or
  2917. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2918. { don't eat a public directive in classes }
  2919. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2920. result:=true;
  2921. exit;
  2922. end;
  2923. end;
  2924. function find_proc_directive_index(tok: ttoken): longint; inline;
  2925. begin
  2926. result:=-1;
  2927. for result:=1 to num_proc_directives do
  2928. if proc_direcdata[result].idtok=tok then
  2929. exit;
  2930. result:=-1;
  2931. end;
  2932. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2933. {
  2934. Parse the procedure directive, returns true if a correct directive is found
  2935. }
  2936. var
  2937. p : longint;
  2938. name : TIDString;
  2939. po_comp : tprocoptions;
  2940. tokenloc : TFilePosInfo;
  2941. begin
  2942. parse_proc_direc:=false;
  2943. name:=tokeninfo^[idtoken].str;
  2944. { Hint directive? Then exit immediatly }
  2945. if (m_hintdirective in current_settings.modeswitches) then
  2946. begin
  2947. case idtoken of
  2948. _LIBRARY,
  2949. _PLATFORM,
  2950. _UNIMPLEMENTED,
  2951. _EXPERIMENTAL,
  2952. _DEPRECATED :
  2953. if (m_delphi in current_settings.modeswitches) and (pd.typ=procdef) then
  2954. begin
  2955. maybe_parse_hint_directives(tprocdef(pd));
  2956. { could the new token still be a directive? }
  2957. if token<>_ID then
  2958. exit;
  2959. end
  2960. else
  2961. exit;
  2962. else
  2963. ;
  2964. end;
  2965. end;
  2966. { C directive is MacPas only, because it breaks too much existing code
  2967. on other platforms (PFV) }
  2968. if (idtoken=_C) and
  2969. not(m_mac in current_settings.modeswitches) then
  2970. exit;
  2971. { retrieve data for directive if found }
  2972. p:=find_proc_directive_index(idtoken);
  2973. { Check if the procedure directive is known }
  2974. if p=-1 then
  2975. begin
  2976. { parsing a procvar type the name can be any
  2977. next variable !! }
  2978. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2979. not(idtoken in [_PROPERTY,_GENERIC]) then
  2980. Message1(parser_w_unknown_proc_directive_ignored,pattern);
  2981. exit;
  2982. end;
  2983. { check if method and directive not for object, like public.
  2984. This needs to be checked also for procvars }
  2985. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2986. (symtablestack.top.symtabletype=ObjectSymtable) and
  2987. { directive allowed for cpp classes? }
  2988. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2989. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2990. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2991. exit;
  2992. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2993. (symtablestack.top.symtabletype=recordsymtable) then
  2994. exit;
  2995. { check if method and directive not for java class }
  2996. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2997. is_javaclass(tdef(symtablestack.top.defowner)) then
  2998. exit;
  2999. { check if method and directive not for java interface }
  3000. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  3001. is_javainterface(tdef(symtablestack.top.defowner)) then
  3002. exit;
  3003. { Keep track of the token's position in the file so it's correctly indicated if an error occurs. }
  3004. tokenloc := current_tokenpos;
  3005. { consume directive, and turn flag on }
  3006. consume(token);
  3007. parse_proc_direc:=true;
  3008. { Conflicts between directives? }
  3009. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) then
  3010. begin
  3011. MessagePos2(tokenloc, parser_e_proc_dir_conflict,name,ProcTypeOptionKeywords[pd.proctypeoption]);
  3012. exit;
  3013. end;
  3014. if (pd.proccalloption in proc_direcdata[p].mutexclpocall) then
  3015. begin
  3016. MessagePos2(tokenloc, parser_e_proc_dir_conflict,name,'"' + UpCase(proccalloptionStr[pd.proccalloption]) + '"');
  3017. exit;
  3018. end;
  3019. po_comp := (pd.procoptions*proc_direcdata[p].mutexclpo);
  3020. if (po_comp<>[]) then
  3021. begin
  3022. MessagePos2(tokenloc, parser_e_proc_dir_conflict,name,get_first_proc_str(po_comp));
  3023. exit;
  3024. end;
  3025. { set calling convention }
  3026. if proc_direcdata[p].pocall<>pocall_none then
  3027. begin
  3028. if (po_hascallingconvention in pd.procoptions) then
  3029. begin
  3030. MessagePos2(tokenloc, parser_w_proc_overriding_calling,
  3031. proccalloptionStr[pd.proccalloption],
  3032. proccalloptionStr[proc_direcdata[p].pocall]);
  3033. end;
  3034. { check if the target processor supports this calling convention }
  3035. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  3036. begin
  3037. MessagePos1(tokenloc, parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  3038. { recover }
  3039. proc_direcdata[p].pocall:=pocall_stdcall;
  3040. end;
  3041. pd.proccalloption:=proc_direcdata[p].pocall;
  3042. include(pd.procoptions,po_hascallingconvention);
  3043. end;
  3044. if pd.typ=procdef then
  3045. begin
  3046. { Check if the directive is only for objects }
  3047. if (pd_object in proc_direcdata[p].pd_flags) and
  3048. not assigned(tprocdef(pd).struct) then
  3049. exit;
  3050. { Check if the directive is only for records }
  3051. if (pd_record in proc_direcdata[p].pd_flags) and
  3052. not assigned(tprocdef(pd).struct) then
  3053. exit;
  3054. { check if method and directive not for interface }
  3055. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  3056. is_interface(tprocdef(pd).struct) then
  3057. exit;
  3058. { check if method and directive not for interface }
  3059. if is_dispinterface(tprocdef(pd).struct) and
  3060. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  3061. exit;
  3062. { check if method and directive not for objcclass }
  3063. if is_objcclass(tprocdef(pd).struct) and
  3064. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  3065. exit;
  3066. { check if method and directive not for objcprotocol }
  3067. if is_objcprotocol(tprocdef(pd).struct) and
  3068. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  3069. exit;
  3070. { check if method and directive not for record/class helper }
  3071. if is_objectpascal_helper(tprocdef(pd).struct) and
  3072. (pd_nothelper in proc_direcdata[p].pd_flags) then
  3073. exit;
  3074. end;
  3075. { Check the pd_flags if the directive should be allowed }
  3076. if (pd_interface in pdflags) and
  3077. not(pd_interface in proc_direcdata[p].pd_flags) then
  3078. begin
  3079. MessagePos1(tokenloc, parser_e_proc_dir_not_allowed_in_interface,name);
  3080. exit;
  3081. end;
  3082. if (pd_implemen in pdflags) and
  3083. not(pd_implemen in proc_direcdata[p].pd_flags) then
  3084. begin
  3085. MessagePos1(tokenloc, parser_e_proc_dir_not_allowed_in_implementation,name);
  3086. exit;
  3087. end;
  3088. if (pd_procvar in pdflags) and
  3089. not(pd_procvar in proc_direcdata[p].pd_flags) then
  3090. begin
  3091. { reset mismatching calling convention to avoid further errors, see e.g. tests/webtbf/tw41041.pp }
  3092. if proc_direcdata[p].pocall<>pocall_none then
  3093. pd.proccalloption:=pocall_default;
  3094. MessagePos1(tokenloc, parser_e_proc_dir_not_allowed_in_procvar,name);
  3095. exit;
  3096. end;
  3097. { Return the new pd_flags }
  3098. if not(pd_body in proc_direcdata[p].pd_flags) then
  3099. exclude(pdflags,pd_body);
  3100. { Add the correct flag }
  3101. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  3102. { Call the handler }
  3103. if pointer(proc_direcdata[p].handler)<>nil then
  3104. proc_direcdata[p].handler(pd);
  3105. end;
  3106. function proc_get_importname(pd:tprocdef):string;
  3107. var
  3108. dllname, importname : string;
  3109. begin
  3110. result:='';
  3111. if not(po_external in pd.procoptions) then
  3112. internalerror(200412151);
  3113. { external name or number is specified }
  3114. if assigned(pd.import_name) or (pd.import_nr<>0) then
  3115. begin
  3116. if assigned(pd.import_dll) then
  3117. dllname:=pd.import_dll^
  3118. else
  3119. dllname:='';
  3120. if assigned(pd.import_name) then
  3121. importname:=pd.import_name^
  3122. else
  3123. importname:='';
  3124. proc_get_importname:=make_dllmangledname(dllname,
  3125. importname,pd.import_nr,pd.proccalloption);
  3126. end
  3127. else
  3128. begin
  3129. { Default names when importing variables }
  3130. case pd.proccalloption of
  3131. pocall_cdecl,
  3132. pocall_sysv_abi_cdecl,
  3133. pocall_ms_abi_cdecl:
  3134. begin
  3135. if assigned(pd.struct) then
  3136. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  3137. else
  3138. result:=target_info.Cprefix+pd.procsym.realname;
  3139. end;
  3140. pocall_cppdecl :
  3141. begin
  3142. result:=target_info.Cprefix+pd.cplusplusmangledname;
  3143. end;
  3144. else
  3145. begin
  3146. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  3147. { but according to MacPas mode description
  3148. Cprefix should still be used PM }
  3149. if (m_mac in current_settings.modeswitches) then
  3150. result:=target_info.Cprefix+tprocdef(pd).procsym.realname
  3151. else
  3152. result:=pd.procsym.realname;
  3153. {$ifdef i8086}
  3154. { Turbo Pascal expects names of external routines
  3155. to be all uppercase }
  3156. if (target_info.system=system_i8086_msdos) and
  3157. (m_tp7 in current_settings.modeswitches) and
  3158. (pd.proccalloption=pocall_pascal) then
  3159. result:=UpCase(result);
  3160. {$endif i8086}
  3161. end;
  3162. end;
  3163. end;
  3164. end;
  3165. procedure proc_set_mangledname(pd:tprocdef);
  3166. var
  3167. s : string;
  3168. begin
  3169. { When the mangledname is already set we aren't allowed to change
  3170. it because it can already be used somewhere (PFV) }
  3171. if not(po_has_mangledname in pd.procoptions) then
  3172. begin
  3173. if (po_external in pd.procoptions) and not (po_wasm_suspending in pd.procoptions) then
  3174. begin
  3175. { External Procedures are only allowed to change the mangledname
  3176. in their first declaration }
  3177. if (pd.forwarddef or (not pd.hasforward)) then
  3178. begin
  3179. s:=proc_get_importname(pd);
  3180. if s<>'' then
  3181. begin
  3182. pd.setmangledname(s);
  3183. end;
  3184. { since this is an external declaration, there won't be an
  3185. implementation that needs to match the original symbol
  3186. again -> immediately convert here }
  3187. if po_compilerproc in pd.procoptions then
  3188. pd.setcompilerprocname;
  3189. end
  3190. end
  3191. else
  3192. { Normal procedures }
  3193. begin
  3194. if (po_compilerproc in pd.procoptions) then
  3195. begin
  3196. pd.setmangledname(lower(pd.procsym.name));
  3197. end;
  3198. end;
  3199. end;
  3200. { Public/exported alias names }
  3201. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  3202. not(po_has_public_name in pd.procoptions) then
  3203. begin
  3204. case pd.proccalloption of
  3205. pocall_cdecl,
  3206. pocall_sysv_abi_cdecl,
  3207. pocall_ms_abi_cdecl:
  3208. begin
  3209. if assigned(pd.struct) then
  3210. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  3211. else
  3212. begin
  3213. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  3214. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  3215. (po_exports in pd.procoptions) then
  3216. pd.aliasnames.insert(pd.procsym.realname)
  3217. else
  3218. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  3219. end;
  3220. end;
  3221. pocall_cppdecl :
  3222. begin
  3223. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  3224. end;
  3225. else
  3226. ;
  3227. end;
  3228. { prevent adding the alias a second time }
  3229. include(pd.procoptions,po_has_public_name);
  3230. end;
  3231. end;
  3232. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  3233. {
  3234. Parse the procedure directives. It does not matter if procedure directives
  3235. are written using ;procdir; or ['procdir'] syntax.
  3236. }
  3237. var
  3238. stoprecording,
  3239. res : boolean;
  3240. begin
  3241. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  3242. begin
  3243. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  3244. include(pd.procoptions,po_public);
  3245. include(pd.procoptions,po_has_public_name);
  3246. include(pd.procoptions,po_global);
  3247. end;
  3248. { methods from external class definitions are all external themselves }
  3249. if (pd.typ=procdef) and
  3250. assigned(tprocdef(pd).struct) and
  3251. (tprocdef(pd).struct.typ=objectdef) and
  3252. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  3253. tprocdef(pd).make_external;
  3254. { Class constructors and destructor are static class methods in real. }
  3255. { There are many places in the compiler where either class or static }
  3256. { method flag changes the behavior. It is simplier to add them to }
  3257. { the class constructors/destructors options than to fix all the }
  3258. { occurencies. (Paul) }
  3259. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  3260. begin
  3261. include(pd.procoptions,po_classmethod);
  3262. include(pd.procoptions,po_staticmethod);
  3263. end;
  3264. { for a generic routine we also need to record the procedure }
  3265. { directives, but only if we aren't already recording for a }
  3266. { surrounding generic }
  3267. if pd.is_generic and (pd.typ=procdef) and not current_scanner.is_recording_tokens then
  3268. begin
  3269. current_scanner.startrecordtokens(tprocdef(pd).genericdecltokenbuf);
  3270. stoprecording:=true;
  3271. end
  3272. else
  3273. stoprecording:=false;
  3274. while (token=_ID) or
  3275. (
  3276. not (m_prefixed_attributes in current_settings.modeswitches) and
  3277. (token=_LECKKLAMMER)
  3278. ) do
  3279. begin
  3280. if not (m_prefixed_attributes in current_settings.modeswitches) and
  3281. try_to_consume(_LECKKLAMMER) then
  3282. begin
  3283. repeat
  3284. parse_proc_direc(pd,pdflags);
  3285. until not try_to_consume(_COMMA);
  3286. consume(_RECKKLAMMER);
  3287. { we always expect at least '[];' }
  3288. res:=true;
  3289. end
  3290. else
  3291. begin
  3292. res:=parse_proc_direc(pd,pdflags);
  3293. end;
  3294. { A procedure directive normally followed by a semicolon, but in
  3295. a const section or reading a type we should stop when _EQ is found,
  3296. because a constant/default value follows }
  3297. if res then
  3298. begin
  3299. if (block_type=bt_const_type) and
  3300. (token=_EQ) then
  3301. break;
  3302. { support procedure proc;stdcall export; }
  3303. if not(check_proc_directive((pd.typ=procvardef))) then
  3304. begin
  3305. { support "record p : procedure stdcall end;" and
  3306. "var p : procedure stdcall = nil;" }
  3307. if (
  3308. (pd_procvar in pdflags) and
  3309. (token in [_END,_RKLAMMER,_EQ])
  3310. ) or (
  3311. (po_anonymous in pd.procoptions) and
  3312. (token in [_BEGIN,_VAR,_CONST,_TYPE,_LABEL,_FUNCTION,_PROCEDURE,_OPERATOR])
  3313. ) then
  3314. break
  3315. else
  3316. begin
  3317. if (token=_COLON) then
  3318. begin
  3319. Message(parser_e_field_not_allowed_here);
  3320. consume_all_until(_SEMICOLON);
  3321. end;
  3322. consume(_SEMICOLON)
  3323. end;
  3324. end;
  3325. end
  3326. else
  3327. break;
  3328. end;
  3329. if stoprecording then
  3330. current_scanner.stoprecordtokens;
  3331. { nostackframe requires assembler, but assembler
  3332. may be specified in the implementation part only,
  3333. and in not required if the function is first forward declared
  3334. if it is a procdef that has forwardef set to true
  3335. we postpone the possible error message to the real implementation
  3336. parse_only does not need to be considered as po_nostackframe
  3337. is an implementation only directive }
  3338. if (po_nostackframe in pd.procoptions) and
  3339. not (po_assembler in pd.procoptions) and
  3340. ((pd.typ<>procdef) or not tprocdef(pd).forwarddef) then
  3341. message(parser_e_nostackframe_without_assembler);
  3342. end;
  3343. procedure parse_proctype_directives(pd_or_invkdef:tdef);
  3344. var
  3345. pdflags : tpdflags;
  3346. pd : tabstractprocdef;
  3347. begin
  3348. if is_funcref(pd_or_invkdef) then
  3349. pd:=get_invoke_procdef(tobjectdef(pd_or_invkdef))
  3350. else if pd_or_invkdef.typ=procvardef then
  3351. pd:=tprocvardef(pd_or_invkdef)
  3352. else
  3353. internalerror(2022012501);
  3354. pdflags:=[pd_procvar];
  3355. parse_proc_directives(pd,pdflags);
  3356. end;
  3357. procedure parse_object_proc_directives(pd:tprocdef);
  3358. var
  3359. pdflags : tpdflags;
  3360. begin
  3361. pdflags:=[pd_object];
  3362. parse_proc_directives(pd,pdflags);
  3363. end;
  3364. procedure parse_record_proc_directives(pd:tprocdef);
  3365. var
  3366. pdflags : tpdflags;
  3367. begin
  3368. pdflags:=[pd_record];
  3369. parse_proc_directives(pd,pdflags);
  3370. end;
  3371. end.