pdecsub.pas 133 KB

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