pdecsub.pas 135 KB

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