pdecsub.pas 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550
  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. { when extended rtti appears, then we must adapt this check}
  1216. if (target_cpu=tsystemcpu.cpu_wasm32) and
  1217. assigned(astruct) and
  1218. (astruct.typ=objectdef) and
  1219. (tobjectdef(astruct).objecttype in [odt_interfacecom,odt_interfacecorba]) and
  1220. (pd.visibility=vis_published) then
  1221. pd.synthetickind:=tsk_invoke_helper;
  1222. { parse parameters }
  1223. if token=_LKLAMMER then
  1224. begin
  1225. old_current_structdef:=nil;
  1226. old_current_genericdef:=current_genericdef;
  1227. old_current_specializedef:=nil;
  1228. { Add ObjectSymtable to be able to find nested type definitions }
  1229. popclass:=0;
  1230. if assigned(pd.struct) and
  1231. (pd.parast.symtablelevel>=normal_function_level) and
  1232. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1233. begin
  1234. popclass:=push_nested_hierarchy(pd.struct);
  1235. old_current_structdef:=current_structdef;
  1236. old_current_specializedef:=current_specializedef;
  1237. current_structdef:=pd.struct;
  1238. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1239. current_genericdef:=current_structdef;
  1240. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1241. current_specializedef:=current_structdef;
  1242. end;
  1243. if pd.is_generic then
  1244. current_genericdef:=pd;
  1245. { Add parameter symtable }
  1246. if pd.parast.symtabletype<>staticsymtable then
  1247. symtablestack.push(pd.parast);
  1248. parse_parameter_dec(pd);
  1249. if pd.parast.symtabletype<>staticsymtable then
  1250. symtablestack.pop(pd.parast);
  1251. current_genericdef:=old_current_genericdef;
  1252. if popclass>0 then
  1253. begin
  1254. current_structdef:=old_current_structdef;
  1255. current_specializedef:=old_current_specializedef;
  1256. dec(popclass,pop_nested_hierarchy(pd.struct));
  1257. if popclass<>0 then
  1258. internalerror(201011260); // 11 nov 2010 index 0
  1259. end;
  1260. end;
  1261. parse_generic:=old_parse_generic;
  1262. result:=true;
  1263. end;
  1264. procedure parse_proc_dec_finish(pd:tprocdef;flags:tparse_proc_flags;astruct:tabstractrecorddef);
  1265. var
  1266. locationstr: string;
  1267. i: integer;
  1268. found: boolean;
  1269. procedure read_returndef(pd: tprocdef);
  1270. var
  1271. popclass: integer;
  1272. old_parse_generic: boolean;
  1273. old_current_structdef: tabstractrecorddef;
  1274. old_current_genericdef,
  1275. old_current_specializedef: tstoreddef;
  1276. begin
  1277. old_parse_generic:=parse_generic;
  1278. { Add ObjectSymtable to be able to find generic type definitions }
  1279. popclass:=0;
  1280. old_current_structdef:=nil;
  1281. old_current_genericdef:=current_genericdef;
  1282. old_current_specializedef:=current_specializedef;
  1283. current_genericdef:=nil;
  1284. current_specializedef:=nil;
  1285. if assigned(pd.struct) and
  1286. (pd.parast.symtablelevel>=normal_function_level) and
  1287. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1288. begin
  1289. popclass:=push_nested_hierarchy(pd.struct);
  1290. old_current_structdef:=current_structdef;
  1291. current_structdef:=pd.struct;
  1292. end;
  1293. if df_generic in pd.defoptions then
  1294. begin
  1295. if pd.is_generic then
  1296. current_genericdef:=pd
  1297. else if assigned(pd.struct) then
  1298. current_genericdef:=pd.struct
  1299. else
  1300. internalerror(2016090202);
  1301. end;
  1302. if df_specialization in pd.defoptions then
  1303. begin
  1304. if pd.is_specialization then
  1305. current_specializedef:=pd
  1306. else if assigned(pd.struct) then
  1307. current_specializedef:=pd.struct
  1308. else
  1309. internalerror(2016090203);
  1310. end;
  1311. parse_generic:=(df_generic in pd.defoptions);
  1312. if pd.is_generic or pd.is_specialization then
  1313. symtablestack.push(pd.parast);
  1314. pd.returndef:=result_type([stoAllowSpecialization]);
  1315. // Issue #24863, enabled only for the main progra commented out for now because it breaks building of RTL and needs extensive
  1316. // testing and/or RTL patching.
  1317. {
  1318. if ((pd.returndef=cvarianttype) or (pd.returndef=colevarianttype)) and
  1319. not(cs_compilesystem in current_settings.moduleswitches) then
  1320. include(current_module.moduleflags,mf_uses_variants);
  1321. }
  1322. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  1323. Message1(type_e_not_automatable,pd.returndef.typename);
  1324. if pd.is_generic or pd.is_specialization then
  1325. symtablestack.pop(pd.parast);
  1326. if popclass>0 then
  1327. begin
  1328. current_structdef:=old_current_structdef;
  1329. dec(popclass,pop_nested_hierarchy(pd.struct));
  1330. if popclass<>0 then
  1331. internalerror(201012020);
  1332. end;
  1333. current_genericdef:=old_current_genericdef;
  1334. current_specializedef:=old_current_specializedef;
  1335. parse_generic:=old_parse_generic;
  1336. end;
  1337. begin
  1338. locationstr:='';
  1339. case pd.proctypeoption of
  1340. potype_procedure:
  1341. begin
  1342. pd.returndef:=voidtype;
  1343. if ppf_classmethod in flags then
  1344. include(pd.procoptions,po_classmethod);
  1345. end;
  1346. potype_function:
  1347. begin
  1348. if po_anonymous in pd.procoptions then
  1349. begin
  1350. { allow a different result name for anonymous functions (especially
  1351. for modes without Result modeswitch), but for consistency with
  1352. operators we allow this in other modes as well }
  1353. if token<>_ID then
  1354. begin
  1355. if not(m_result in current_settings.modeswitches) then
  1356. consume(_ID);
  1357. end
  1358. else
  1359. begin
  1360. pd.resultname:=stringdup(orgpattern);
  1361. consume(_ID);
  1362. end;
  1363. end;
  1364. if try_to_consume(_COLON) then
  1365. begin
  1366. read_returndef(pd);
  1367. if (target_info.system in [system_m68k_amiga]) then
  1368. begin
  1369. if (idtoken=_LOCATION) then
  1370. begin
  1371. if po_explicitparaloc in pd.procoptions then
  1372. begin
  1373. consume(_LOCATION);
  1374. locationstr:=cstringpattern;
  1375. consume(_CSTRING);
  1376. end
  1377. else
  1378. { I guess this needs a new message... (KB) }
  1379. Message(parser_e_paraloc_all_paras);
  1380. end
  1381. else
  1382. begin
  1383. if po_explicitparaloc in pd.procoptions then
  1384. { assign default locationstr, if none specified }
  1385. { and we've arguments with explicit paraloc }
  1386. locationstr:='D0';
  1387. end;
  1388. end;
  1389. end
  1390. else
  1391. begin
  1392. if (
  1393. parse_only and
  1394. not(is_interface(pd.struct))
  1395. ) or
  1396. (m_repeat_forward in current_settings.modeswitches) then
  1397. begin
  1398. consume(_COLON);
  1399. consume_all_until(_SEMICOLON);
  1400. end;
  1401. end;
  1402. if ppf_classmethod in flags then
  1403. include(pd.procoptions,po_classmethod);
  1404. end;
  1405. potype_constructor,
  1406. potype_class_constructor:
  1407. begin
  1408. if not (ppf_classmethod in flags) and
  1409. assigned(pd) and
  1410. assigned(pd.struct) then
  1411. begin
  1412. { Set return type, class constructors return the
  1413. created instance, object constructors return boolean }
  1414. if is_class(pd.struct) or
  1415. is_record(pd.struct) or
  1416. is_javaclass(pd.struct) then
  1417. pd.returndef:=pd.struct
  1418. else
  1419. if is_objectpascal_helper(pd.struct) then
  1420. pd.returndef:=tobjectdef(pd.struct).extendeddef
  1421. else
  1422. {$ifdef CPU64bitaddr}
  1423. pd.returndef:=bool64type;
  1424. {$else CPU64bitaddr}
  1425. pd.returndef:=bool32type;
  1426. {$endif CPU64bitaddr}
  1427. end
  1428. else
  1429. pd.returndef:=voidtype;
  1430. end;
  1431. potype_class_destructor,
  1432. potype_destructor:
  1433. begin
  1434. if assigned(pd) then
  1435. pd.returndef:=voidtype;
  1436. end;
  1437. potype_operator:
  1438. begin
  1439. { operators always need to be searched in all units (that
  1440. contain operators) }
  1441. include(pd.procoptions,po_overload);
  1442. pd.procsym.owner.includeoption(sto_has_operator);
  1443. if pd.parast.symtablelevel>normal_function_level then
  1444. Message(parser_e_no_local_operator);
  1445. if ppf_classmethod in flags then
  1446. begin
  1447. include(pd.procoptions,po_classmethod);
  1448. { any class operator is also static }
  1449. include(pd.procoptions,po_staticmethod);
  1450. end;
  1451. if token<>_ID then
  1452. begin
  1453. if not(m_result in current_settings.modeswitches) then
  1454. consume(_ID);
  1455. end
  1456. else
  1457. begin
  1458. pd.resultname:=stringdup(orgpattern);
  1459. consume(_ID);
  1460. end;
  1461. { operators without result (management operators) }
  1462. if optoken in [_OP_INITIALIZE, _OP_FINALIZE, _OP_ADDREF, _OP_COPY] then
  1463. begin
  1464. { single var parameter to point the record }
  1465. if (optoken in [_OP_INITIALIZE, _OP_FINALIZE, _OP_ADDREF]) and
  1466. (
  1467. (pd.parast.SymList.Count<>1) or
  1468. (tparavarsym(pd.parast.SymList[0]).vardef<>pd.struct) or
  1469. (tparavarsym(pd.parast.SymList[0]).varspez<>vs_var)
  1470. ) then
  1471. Message(parser_e_overload_impossible)
  1472. { constref (source) and var (dest) parameter to point the records }
  1473. else if (optoken=_OP_COPY) and
  1474. (
  1475. (pd.parast.SymList.Count<>2) or
  1476. (tparavarsym(pd.parast.SymList[0]).vardef<>pd.struct) or
  1477. (tparavarsym(pd.parast.SymList[0]).varspez<>vs_constref) or
  1478. (tparavarsym(pd.parast.SymList[1]).vardef<>pd.struct) or
  1479. (tparavarsym(pd.parast.SymList[1]).varspez<>vs_var)
  1480. ) then
  1481. Message(parser_e_overload_impossible);
  1482. trecordsymtable(pd.procsym.Owner).includemanagementoperator(
  1483. token2managementoperator(optoken));
  1484. pd.returndef:=voidtype
  1485. end
  1486. else
  1487. if not try_to_consume(_COLON) then
  1488. begin
  1489. consume(_COLON);
  1490. pd.returndef:=generrordef;
  1491. consume_all_until(_SEMICOLON);
  1492. end
  1493. else
  1494. begin
  1495. read_returndef(pd);
  1496. { check that class operators have either return type of structure or }
  1497. { at least one argument of that type }
  1498. if (po_classmethod in pd.procoptions) and
  1499. (pd.returndef <> pd.struct) then
  1500. begin
  1501. found:=false;
  1502. for i := 0 to pd.parast.SymList.Count - 1 do
  1503. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1504. begin
  1505. found:=true;
  1506. break;
  1507. end;
  1508. if not found then
  1509. if assigned(pd.struct) then
  1510. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName)
  1511. else
  1512. MessagePos(pd.fileinfo,type_e_type_id_expected);
  1513. end;
  1514. if not assigned(pd.struct) or assigned(astruct) then
  1515. begin
  1516. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1517. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1518. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1519. message(parser_e_no_such_assignment)
  1520. else if not isoperatoracceptable(pd,optoken) then
  1521. Message(parser_e_overload_impossible);
  1522. end;
  1523. end;
  1524. end;
  1525. else
  1526. internalerror(2015052202);
  1527. end;
  1528. if (pd.proccalloption in cdecl_pocalls) and
  1529. (pd.paras.count>0) and
  1530. is_array_of_const(tparavarsym(pd.paras[pd.paras.count-1]).vardef) then
  1531. begin
  1532. include(pd.procoptions,po_variadic);
  1533. end;
  1534. { support procedure proc stdcall export; }
  1535. if not(check_proc_directive(false)) then
  1536. begin
  1537. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1538. begin
  1539. message(parser_e_field_not_allowed_here);
  1540. consume_all_until(_SEMICOLON);
  1541. end;
  1542. if not (ppf_anonymous in flags) then
  1543. consume(_SEMICOLON);
  1544. end;
  1545. if locationstr<>'' then
  1546. begin
  1547. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1548. { I guess this needs a new message... (KB) }
  1549. message(parser_e_illegal_explicit_paraloc);
  1550. end;
  1551. end;
  1552. function parse_proc_dec(flags:tparse_proc_flags;astruct:tabstractrecorddef):tprocdef;
  1553. var
  1554. pd : tprocdef;
  1555. old_block_type : tblock_type;
  1556. recover : boolean;
  1557. procedure finish_intf_mapping;
  1558. begin
  1559. if token=_COLON then
  1560. begin
  1561. message(parser_e_field_not_allowed_here);
  1562. consume_all_until(_SEMICOLON);
  1563. end;
  1564. consume(_SEMICOLON);
  1565. end;
  1566. begin
  1567. pd:=nil;
  1568. recover:=false;
  1569. case token of
  1570. _FUNCTION :
  1571. begin
  1572. consume(_FUNCTION);
  1573. if parse_proc_head(astruct,potype_function,flags,nil,nil,pd) then
  1574. begin
  1575. { pd=nil when it is a interface mapping }
  1576. if assigned(pd) then
  1577. parse_proc_dec_finish(pd,flags,astruct)
  1578. else
  1579. finish_intf_mapping;
  1580. end
  1581. else
  1582. begin
  1583. { recover }
  1584. consume(_COLON);
  1585. consume_all_until(_SEMICOLON);
  1586. recover:=true;
  1587. end;
  1588. end;
  1589. _PROCEDURE :
  1590. begin
  1591. consume(_PROCEDURE);
  1592. if parse_proc_head(astruct,potype_procedure,flags,nil,nil,pd) then
  1593. begin
  1594. { pd=nil when it is an interface mapping }
  1595. if assigned(pd) then
  1596. parse_proc_dec_finish(pd,flags,astruct)
  1597. else
  1598. finish_intf_mapping;
  1599. end
  1600. else
  1601. recover:=true;
  1602. end;
  1603. _CONSTRUCTOR :
  1604. begin
  1605. consume(_CONSTRUCTOR);
  1606. if ppf_classmethod in flags then
  1607. recover:=not parse_proc_head(astruct,potype_class_constructor,[],nil,nil,pd)
  1608. else
  1609. recover:=not parse_proc_head(astruct,potype_constructor,[],nil,nil,pd);
  1610. if not recover then
  1611. parse_proc_dec_finish(pd,flags,astruct);
  1612. end;
  1613. _DESTRUCTOR :
  1614. begin
  1615. consume(_DESTRUCTOR);
  1616. if ppf_classmethod in flags then
  1617. recover:=not parse_proc_head(astruct,potype_class_destructor,[],nil,nil,pd)
  1618. else
  1619. recover:=not parse_proc_head(astruct,potype_destructor,[],nil,nil,pd);
  1620. if not recover then
  1621. parse_proc_dec_finish(pd,flags,astruct);
  1622. end;
  1623. else
  1624. if (token=_OPERATOR) or
  1625. ((ppf_classmethod in flags) and (idtoken=_OPERATOR)) then
  1626. begin
  1627. { we need to set the block type to bt_body, so that operator names
  1628. like ">", "=>" or "<>" are parsed correctly instead of e.g.
  1629. _LSHARPBRACKET and _RSHARPBRACKET for "<>" }
  1630. old_block_type:=block_type;
  1631. block_type:=bt_body;
  1632. consume(_OPERATOR);
  1633. parse_proc_head(astruct,potype_operator,[],nil,nil,pd);
  1634. block_type:=old_block_type;
  1635. if assigned(pd) then
  1636. parse_proc_dec_finish(pd,flags,astruct)
  1637. else
  1638. begin
  1639. { recover }
  1640. try_to_consume(_ID);
  1641. consume(_COLON);
  1642. consume_all_until(_SEMICOLON);
  1643. recover:=true;
  1644. end;
  1645. end;
  1646. end;
  1647. if recover and not(check_proc_directive(false)) then
  1648. begin
  1649. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1650. begin
  1651. message(parser_e_field_not_allowed_here);
  1652. consume_all_until(_SEMICOLON);
  1653. end;
  1654. if not (ppf_anonymous in flags) then
  1655. consume(_SEMICOLON);
  1656. end;
  1657. { we've parsed the final semicolon, so stop recording tokens }
  1658. if assigned(pd) and
  1659. (df_generic in pd.defoptions) and
  1660. assigned(pd.genericdecltokenbuf) then
  1661. current_scanner.stoprecordtokens;
  1662. result:=pd;
  1663. end;
  1664. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  1665. var
  1666. oldparse_only: boolean;
  1667. flags : tparse_proc_flags;
  1668. begin
  1669. oldparse_only:=parse_only;
  1670. parse_only:=true;
  1671. flags:=[];
  1672. if is_classdef then
  1673. include(flags,ppf_classmethod);
  1674. if hadgeneric then
  1675. include(flags,ppf_generic);
  1676. result:=parse_proc_dec(flags,astruct);
  1677. { this is for error recovery as well as forward }
  1678. { interface mappings, i.e. mapping to a method }
  1679. { which isn't declared yet }
  1680. if assigned(result) then
  1681. begin
  1682. parse_record_proc_directives(result);
  1683. { since records have no inheritance, don't allow non-static
  1684. class methods. Delphi does the same. }
  1685. if (result.proctypeoption<>potype_operator) and
  1686. is_classdef and
  1687. not (po_staticmethod in result.procoptions) then
  1688. MessagePos(result.fileinfo, parser_e_class_methods_only_static_in_records);
  1689. // we can't add hidden params here because record is not yet defined
  1690. // and therefore record size which has influence on parameter passing rules may change too
  1691. // look at record_dec to see where calling conventions are applied (issue #0021044)
  1692. handle_calling_convention(result,hcc_default_actions_intf_struct);
  1693. { add definition to procsym }
  1694. proc_add_definition(result);
  1695. if result.is_generic then
  1696. astruct.symtable.includeoption(sto_has_generic);
  1697. end;
  1698. maybe_parse_hint_directives(result);
  1699. parse_only:=oldparse_only;
  1700. end;
  1701. {****************************************************************************
  1702. Procedure directive handlers
  1703. ****************************************************************************}
  1704. procedure pd_compilerproc(pd:tabstractprocdef);
  1705. var
  1706. v : Tconstexprint;
  1707. begin
  1708. { check for optional syssym index }
  1709. if try_to_consume(_COLON) then
  1710. begin
  1711. v:=get_intconst;
  1712. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1713. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1714. else if not assigned(tsyssym.find_by_number(longint(v.svalue))) then
  1715. message1(parser_e_invalid_internal_function_index,tostr(v))
  1716. else
  1717. tprocdef(pd).extnumber:=longint(v.svalue);
  1718. end;
  1719. end;
  1720. procedure pd_far(pd:tabstractprocdef);
  1721. begin
  1722. pd.declared_far;
  1723. end;
  1724. procedure pd_near(pd:tabstractprocdef);
  1725. begin
  1726. pd.declared_near;
  1727. end;
  1728. procedure pd_export(pd:tabstractprocdef);
  1729. begin
  1730. if pd.typ<>procdef then
  1731. internalerror(200304264);
  1732. if assigned(tprocdef(pd).struct) then
  1733. Message(parser_e_methods_dont_be_export);
  1734. if pd.parast.symtablelevel>normal_function_level then
  1735. Message(parser_e_dont_nest_export);
  1736. end;
  1737. procedure pd_forward(pd:tabstractprocdef);
  1738. begin
  1739. if pd.typ<>procdef then
  1740. internalerror(200304265);
  1741. tprocdef(pd).forwarddef:=true;
  1742. end;
  1743. procedure pd_alias(pd:tabstractprocdef);
  1744. begin
  1745. if pd.typ<>procdef then
  1746. internalerror(200304266);
  1747. consume(_COLON);
  1748. tprocdef(pd).aliasnames.insert(get_stringconst);
  1749. include(pd.procoptions,po_has_public_name);
  1750. end;
  1751. procedure pd_public(pd:tabstractprocdef);
  1752. begin
  1753. if pd.typ<>procdef then
  1754. internalerror(2003042601);
  1755. if try_to_consume(_NAME) then
  1756. begin
  1757. tprocdef(pd).aliasnames.insert(get_stringconst);
  1758. include(pd.procoptions,po_has_public_name);
  1759. end;
  1760. end;
  1761. procedure pd_asmname(pd:tabstractprocdef);
  1762. begin
  1763. if pd.typ<>procdef then
  1764. internalerror(200304267);
  1765. if token=_CCHAR then
  1766. begin
  1767. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1768. consume(_CCHAR)
  1769. end
  1770. else
  1771. begin
  1772. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1773. consume(_CSTRING);
  1774. end;
  1775. { we don't need anything else }
  1776. tprocdef(pd).forwarddef:=false;
  1777. end;
  1778. procedure pd_internconst(pd:tabstractprocdef);
  1779. var v:Tconstexprint;
  1780. begin
  1781. if pd.typ<>procdef then
  1782. internalerror(200304268);
  1783. consume(_COLON);
  1784. v:=get_intconst;
  1785. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1786. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1787. else
  1788. Tprocdef(pd).extnumber:=longint(v.svalue);
  1789. end;
  1790. procedure pd_internproc(pd:tabstractprocdef);
  1791. var v:Tconstexprint;
  1792. begin
  1793. if pd.typ<>procdef then
  1794. internalerror(2003042602);
  1795. consume(_COLON);
  1796. v:=get_intconst;
  1797. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1798. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1799. else
  1800. Tprocdef(pd).extnumber:=longint(v.svalue);
  1801. { the proc is defined }
  1802. tprocdef(pd).forwarddef:=false;
  1803. end;
  1804. procedure pd_interrupt(pd:tabstractprocdef);
  1805. begin
  1806. if pd.parast.symtablelevel>normal_function_level then
  1807. Message(parser_e_dont_nest_interrupt);
  1808. end;
  1809. procedure pd_abstract(pd:tabstractprocdef);
  1810. begin
  1811. if pd.typ<>procdef then
  1812. internalerror(200304269);
  1813. if is_objectpascal_helper(tprocdef(pd).struct) then
  1814. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1815. if assigned(tprocdef(pd).struct) and
  1816. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1817. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1818. else if (po_virtualmethod in pd.procoptions) then
  1819. begin
  1820. include(pd.procoptions,po_abstractmethod);
  1821. { one more abstract method }
  1822. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1823. end
  1824. else
  1825. Message(parser_e_only_virtual_methods_abstract);
  1826. { the method is defined }
  1827. tprocdef(pd).forwarddef:=false;
  1828. end;
  1829. procedure pd_final(pd:tabstractprocdef);
  1830. begin
  1831. if pd.typ<>procdef then
  1832. internalerror(200910170);
  1833. if is_objectpascal_helper(tprocdef(pd).struct) and
  1834. (m_objfpc in current_settings.modeswitches) then
  1835. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1836. if (po_virtualmethod in pd.procoptions) or
  1837. (is_javaclass(tprocdef(pd).struct) and
  1838. (po_classmethod in pd.procoptions)) then
  1839. include(pd.procoptions,po_finalmethod)
  1840. else
  1841. Message(parser_e_only_virtual_methods_final);
  1842. end;
  1843. procedure pd_enumerator(pd:tabstractprocdef);
  1844. begin
  1845. if pd.typ<>procdef then
  1846. internalerror(200910250);
  1847. if (token = _ID) then
  1848. begin
  1849. if pattern='MOVENEXT' then
  1850. begin
  1851. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1852. message(parser_e_only_one_enumerator_movenext);
  1853. pd.calcparas;
  1854. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1855. (pd.minparacount = 0) then
  1856. begin
  1857. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1858. include(pd.procoptions,po_enumerator_movenext);
  1859. end
  1860. else
  1861. Message(parser_e_enumerator_movenext_is_not_valid)
  1862. end
  1863. else
  1864. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1865. consume(token);
  1866. end
  1867. else
  1868. Message(parser_e_enumerator_identifier_required);
  1869. end;
  1870. procedure pd_virtual(pd:tabstractprocdef);
  1871. {$ifdef WITHDMT}
  1872. var
  1873. pt : tnode;
  1874. {$endif WITHDMT}
  1875. begin
  1876. if assigned(pd.owner) and
  1877. (not assigned(pd.owner.defowner) or
  1878. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1879. (po_external in pd.procoptions) then
  1880. Message2(parser_e_proc_dir_conflict,'EXTERNAL','"VIRTUAL"');
  1881. if pd.typ<>procdef then
  1882. internalerror(2003042610);
  1883. if (pd.proctypeoption=potype_constructor) and
  1884. is_object(tprocdef(pd).struct) then
  1885. Message(parser_e_constructor_cannot_be_not_virtual);
  1886. if pd.is_generic then
  1887. message(parser_e_genfuncs_cannot_be_virtual);
  1888. if is_objectpascal_helper(tprocdef(pd).struct) and
  1889. (m_objfpc in current_settings.modeswitches) then
  1890. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1891. {$ifdef WITHDMT}
  1892. if is_object(tprocdef(pd).struct) and
  1893. (token<>_SEMICOLON) then
  1894. begin
  1895. { any type of parameter is allowed here! }
  1896. pt:=comp_expr(true);
  1897. if is_constintnode(pt) then
  1898. begin
  1899. include(pd.procoptions,po_msgint);
  1900. pd.messageinf.i:=pt.value;
  1901. end
  1902. else
  1903. Message(parser_e_ill_msg_expr);
  1904. disposetree(pt);
  1905. end;
  1906. {$endif WITHDMT}
  1907. end;
  1908. procedure pd_dispid(pd:tabstractprocdef);
  1909. var pt:Tnode;
  1910. begin
  1911. if pd.typ<>procdef then
  1912. internalerror(200604301);
  1913. pt:=comp_expr([ef_accept_equal]);
  1914. if is_constintnode(pt) then
  1915. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1916. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(longint)),tostr(high(longint)))
  1917. else
  1918. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1919. else
  1920. message(parser_e_dispid_must_be_ord_const);
  1921. pt.free;
  1922. end;
  1923. procedure pd_static(pd:tabstractprocdef);
  1924. begin
  1925. if pd.typ<>procdef then
  1926. internalerror(2013032001);
  1927. if not assigned(tprocdef(pd).struct) then
  1928. internalerror(2013032002);
  1929. include(tprocdef(pd).procsym.symoptions,sp_static);
  1930. { "static" is not allowed for operators or normal methods (except in objects) }
  1931. if (pd.proctypeoption=potype_operator) or
  1932. (
  1933. not (po_classmethod in pd.procoptions) and
  1934. not is_object(tprocdef(pd).struct)
  1935. )
  1936. then
  1937. Message1(parser_e_dir_not_allowed,arraytokeninfo[_STATIC].str);
  1938. include(pd.procoptions,po_staticmethod);
  1939. end;
  1940. procedure pd_override(pd:tabstractprocdef);
  1941. begin
  1942. if pd.typ<>procdef then
  1943. internalerror(2003042611);
  1944. if is_objectpascal_helper(tprocdef(pd).struct) then
  1945. begin
  1946. if m_objfpc in current_settings.modeswitches then
  1947. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1948. end
  1949. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1950. Message(parser_e_no_object_override)
  1951. else if is_objccategory(tprocdef(pd).struct) then
  1952. Message(parser_e_no_category_override)
  1953. else if (po_external in pd.procoptions) and
  1954. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1955. not is_cppclass(tprocdef(pd).struct) and
  1956. not is_java_class_or_interface(tprocdef(pd).struct) then
  1957. Message2(parser_e_proc_dir_conflict,'OVERRIDE','"EXTERNAL"');
  1958. end;
  1959. procedure pd_overload(pd:tabstractprocdef);
  1960. begin
  1961. if pd.typ<>procdef then
  1962. internalerror(2003042612);
  1963. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1964. end;
  1965. procedure pd_message(pd:tabstractprocdef);
  1966. var
  1967. pt : tnode;
  1968. paracnt : longint;
  1969. begin
  1970. if pd.typ<>procdef then
  1971. internalerror(2003042613);
  1972. if is_objectpascal_helper(tprocdef(pd).struct) then
  1973. begin
  1974. if m_objfpc in current_settings.modeswitches then
  1975. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1976. end
  1977. else
  1978. if not is_class(tprocdef(pd).struct) and
  1979. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1980. Message(parser_e_msg_only_for_classes);
  1981. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1982. Message(parser_e_multiple_messages);
  1983. { check parameter type }
  1984. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1985. begin
  1986. if po_external in pd.procoptions then
  1987. Message2(parser_e_proc_dir_conflict,'MESSAGE','"EXTERNAL"');
  1988. paracnt:=0;
  1989. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1990. if paracnt<>1 then
  1991. Message(parser_e_ill_msg_param);
  1992. end;
  1993. pt:=comp_expr([ef_accept_equal]);
  1994. { message is 1-character long }
  1995. if is_constcharnode(pt) then
  1996. begin
  1997. include(pd.procoptions,po_msgstr);
  1998. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1999. end
  2000. else if pt.nodetype=stringconstn then
  2001. begin
  2002. include(pd.procoptions,po_msgstr);
  2003. if (tstringconstnode(pt).len>255) then
  2004. Message(parser_e_message_string_too_long);
  2005. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  2006. end
  2007. else
  2008. if is_constintnode(pt) and
  2009. (is_class(tprocdef(pd).struct) or
  2010. is_objectpascal_helper(tprocdef(pd).struct)) then
  2011. begin
  2012. include(pd.procoptions,po_msgint);
  2013. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  2014. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  2015. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(Tprocdef(pd).messageinf.i)),tostr(high(Tprocdef(pd).messageinf.i)))
  2016. else
  2017. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  2018. end
  2019. else
  2020. Message(parser_e_ill_msg_expr);
  2021. { check whether the selector name is valid in case of Objective-C }
  2022. if (po_msgstr in pd.procoptions) and
  2023. is_objc_class_or_protocol(tprocdef(pd).struct) and
  2024. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  2025. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  2026. pt.free;
  2027. end;
  2028. procedure pd_reintroduce(pd:tabstractprocdef);
  2029. begin
  2030. if pd.typ<>procdef then
  2031. internalerror(200401211);
  2032. if is_objectpascal_helper(tprocdef(pd).struct) then
  2033. begin
  2034. if m_objfpc in current_settings.modeswitches then
  2035. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  2036. end
  2037. else
  2038. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  2039. not(is_objccategory(tprocdef(pd).struct)) and
  2040. not(is_javaclass(tprocdef(pd).struct)) then
  2041. Message(parser_e_no_object_reintroduce);
  2042. end;
  2043. procedure pd_syscall(pd:tabstractprocdef);
  2044. procedure include_po_syscall;
  2045. var
  2046. syscall: psyscallinfo;
  2047. begin
  2048. case target_info.system of
  2049. system_arm_palmos,
  2050. system_m68k_palmos,
  2051. system_m68k_atari,
  2052. system_m68k_amiga,
  2053. system_powerpc_amiga:
  2054. include(pd.procoptions,get_default_syscall);
  2055. system_powerpc_morphos,
  2056. system_arm_aros,
  2057. system_i386_aros,
  2058. system_x86_64_aros:
  2059. begin
  2060. syscall:=get_syscall_by_token(idtoken);
  2061. if assigned(syscall) then
  2062. begin
  2063. if target_info.system in syscall^.validon then
  2064. begin
  2065. consume(idtoken);
  2066. include(pd.procoptions,syscall^.procoption);
  2067. end
  2068. end
  2069. else
  2070. include(pd.procoptions,get_default_syscall);
  2071. end;
  2072. else
  2073. Message(parser_e_syscall_format_not_support);
  2074. end;
  2075. end;
  2076. function po_syscall_to_varoptions: tvaroptions;
  2077. begin
  2078. result:=[vo_is_syscall_lib,vo_is_hidden_para];
  2079. if ([po_syscall_legacy,po_syscall_basereg,po_syscall_basenone] * tprocdef(pd).procoptions) <> [] then
  2080. include(result,vo_has_explicit_paraloc);
  2081. end;
  2082. function po_syscall_to_regname: string;
  2083. begin
  2084. if po_syscall_legacy in tprocdef(pd).procoptions then
  2085. result:='a6'
  2086. { let nobase on MorphOS store the libbase in r12 as well, because
  2087. we will need the libbase anyway during the call generation }
  2088. else if (po_syscall_basenone in tprocdef(pd).procoptions) and
  2089. (target_info.system = system_powerpc_morphos) then
  2090. result:='r12'
  2091. else if po_syscall_basereg in tprocdef(pd).procoptions then
  2092. begin
  2093. case target_info.system of
  2094. system_i386_aros:
  2095. result:='eax';
  2096. system_x86_64_aros:
  2097. result:='r12';
  2098. system_powerpc_morphos:
  2099. result:='r12';
  2100. else
  2101. internalerror(2016090201);
  2102. end;
  2103. end
  2104. else
  2105. internalerror(2016090101);
  2106. end;
  2107. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2108. const
  2109. syscall_paranr: array[boolean] of aint =
  2110. ( paranr_syscall_lib_last, paranr_syscall_lib_first );
  2111. var
  2112. vs : tparavarsym;
  2113. sym : tsym;
  2114. symtable : TSymtable;
  2115. v: Tconstexprint;
  2116. vo: tvaroptions;
  2117. paranr: aint;
  2118. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2119. begin
  2120. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  2121. internalerror(2003042614);
  2122. tprocdef(pd).forwarddef:=false;
  2123. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2124. include_po_syscall;
  2125. if target_info.system in [system_arm_palmos, system_m68k_palmos] then
  2126. begin
  2127. v:=get_intconst;
  2128. tprocdef(pd).extnumber:=longint(v.svalue);
  2129. if ((v<0) or (v>high(word))) then
  2130. message(parser_e_range_check_error);
  2131. if try_to_consume(_COMMA) then
  2132. begin
  2133. v:=get_intconst;
  2134. if ((v<0) or (v>high(word))) then
  2135. message(parser_e_range_check_error);
  2136. tprocdef(pd).import_nr:=longint(v.svalue);
  2137. include(pd.procoptions,po_syscall_has_importnr);
  2138. end;
  2139. exit;
  2140. end;
  2141. if target_info.system = system_m68k_atari then
  2142. begin
  2143. v:=get_intconst;
  2144. if ((v<0) or (v>15)) then
  2145. message(parser_e_range_check_error)
  2146. else
  2147. tprocdef(pd).extnumber:=longint(v.svalue);
  2148. v:=get_intconst;
  2149. if ((v<0) or (v>high(smallint))) then
  2150. message(parser_e_range_check_error)
  2151. else
  2152. tprocdef(pd).import_nr:=longint(v.svalue);
  2153. exit;
  2154. end;
  2155. if consume_sym(sym,symtable) then
  2156. if ((sym.typ=staticvarsym) or
  2157. (sym.typ=absolutevarsym) and (tabsolutevarsym(sym).abstyp=toaddr)) and
  2158. ((tabstractvarsym(sym).vardef.typ=pointerdef) or
  2159. is_32bitint(tabstractvarsym(sym).vardef)) then
  2160. begin
  2161. include(pd.procoptions,po_syscall_has_libsym);
  2162. tcpuprocdef(pd).libsym:=sym;
  2163. vo:=po_syscall_to_varoptions;
  2164. paranr:=syscall_paranr[po_syscall_basefirst in tprocdef(pd).procoptions];
  2165. vs:=cparavarsym.create('$syscalllib',paranr,vs_value,tabstractvarsym(sym).vardef,vo);
  2166. if vo_has_explicit_paraloc in vo then
  2167. if not paramanager.parseparaloc(vs,po_syscall_to_regname) then
  2168. internalerror(2016120301);
  2169. pd.parast.insertsym(vs);
  2170. end
  2171. else
  2172. Message(parser_e_32bitint_or_pointer_variable_expected);
  2173. paramanager.create_funcretloc_info(pd,calleeside);
  2174. paramanager.create_funcretloc_info(pd,callerside);
  2175. v:=get_intconst;
  2176. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  2177. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(Tprocdef(pd).extnumber)),tostr(high(Tprocdef(pd).extnumber)))
  2178. else
  2179. if target_info.system in [system_arm_aros,system_i386_aros,system_x86_64_aros] then
  2180. Tprocdef(pd).extnumber:=v.uvalue * sizeof(pint)
  2181. else
  2182. Tprocdef(pd).extnumber:=v.uvalue;
  2183. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2184. end;
  2185. procedure pd_external(pd:tabstractprocdef);
  2186. {
  2187. If import_dll=nil the procedure is assumed to be in another
  2188. object file. In that object file it should have the name to
  2189. which import_name is pointing to. Otherwise, the procedure is
  2190. assumed to be in the DLL to which import_dll is pointing to. In
  2191. that case either import_nr<>0 or import_name<>nil is true, so
  2192. the procedure is either imported by number or by name. (DM)
  2193. }
  2194. var
  2195. hs : string;
  2196. v:Tconstexprint;
  2197. is_java_external: boolean;
  2198. begin
  2199. if pd.typ<>procdef then
  2200. internalerror(2003042615);
  2201. { Allow specifying a separate external name for methods in external Java
  2202. because its identifier naming constraints are laxer than FPC's
  2203. (e.g., case sensitive).
  2204. Limitation: only allows specifying the symbol name and not the package name,
  2205. and only for external classes/interfaces }
  2206. is_java_external:=
  2207. (pd.typ=procdef) and
  2208. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  2209. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  2210. with tprocdef(pd) do
  2211. begin
  2212. forwarddef:=false;
  2213. { forbid local external procedures }
  2214. if parast.symtablelevel>normal_function_level then
  2215. Message(parser_e_no_local_proc_external);
  2216. { If the procedure should be imported from a DLL, a constant string follows.
  2217. This isn't really correct, an contant string expression follows
  2218. so we check if an semicolon follows, else a string constant have to
  2219. follow (FK) }
  2220. if not is_java_external and
  2221. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  2222. begin
  2223. { Always add library prefix and suffix to create an uniform name }
  2224. hs:=get_stringconst;
  2225. if ExtractFileExt(hs)='' then
  2226. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  2227. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  2228. hs:=target_info.sharedlibprefix+hs;
  2229. { the JVM expects java/lang/Object rather than java.lang.Object }
  2230. if target_info.system in systems_jvm then
  2231. Replace(hs,'.','/');
  2232. import_dll:=stringdup(hs);
  2233. include(procoptions,po_has_importdll);
  2234. if (idtoken=_NAME) then
  2235. begin
  2236. consume(_NAME);
  2237. import_name:=stringdup(get_stringconst);
  2238. include(procoptions,po_has_importname);
  2239. if import_name^='' then
  2240. message(parser_e_empty_import_name);
  2241. end;
  2242. if (idtoken=_INDEX) then
  2243. begin
  2244. {After the word index follows the index number in the DLL.}
  2245. consume(_INDEX);
  2246. v:=get_intconst;
  2247. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  2248. message(parser_e_range_check_error)
  2249. else
  2250. import_nr:=longint(v.svalue);
  2251. end;
  2252. if (idtoken=_SUSPENDING) then
  2253. begin
  2254. if (target_info.system in systems_wasm) then
  2255. begin
  2256. consume(_SUSPENDING);
  2257. include(procoptions,po_wasm_suspending);
  2258. synthetickind:=tsk_wasm_suspending_first;
  2259. if idtoken=_FIRST then
  2260. consume(_FIRST)
  2261. else if idtoken=_LAST then
  2262. begin
  2263. consume(_LAST);
  2264. synthetickind:=tsk_wasm_suspending_last;
  2265. end;
  2266. end
  2267. else
  2268. begin
  2269. message(parser_e_suspending_externals_not_supported_on_current_platform);
  2270. consume(_SUSPENDING);
  2271. if idtoken=_FIRST then
  2272. consume(_FIRST)
  2273. else if idtoken=_LAST then
  2274. consume(_LAST);
  2275. end;
  2276. end;
  2277. { default is to used the realname of the procedure }
  2278. if (import_nr=0) and not assigned(import_name) then
  2279. begin
  2280. import_name:=stringdup(procsym.realname);
  2281. include(procoptions,po_has_importname);
  2282. end;
  2283. end
  2284. else
  2285. begin
  2286. if (idtoken=_NAME) or
  2287. is_java_external then
  2288. begin
  2289. consume(_NAME);
  2290. import_name:=stringdup(get_stringconst);
  2291. include(procoptions,po_has_importname);
  2292. if import_name^='' then
  2293. message(parser_e_empty_import_name);
  2294. end;
  2295. end;
  2296. end;
  2297. end;
  2298. procedure pd_weakexternal(pd:tabstractprocdef);
  2299. begin
  2300. if not(target_info.system in systems_weak_linking) then
  2301. message(parser_e_weak_external_not_supported)
  2302. else
  2303. pd_external(pd);
  2304. end;
  2305. procedure pd_winapi(pd:tabstractprocdef);
  2306. begin
  2307. if not(target_info.system in systems_all_windows+[system_i386_nativent]) then
  2308. pd.proccalloption:=pocall_cdecl
  2309. else
  2310. pd.proccalloption:=pocall_stdcall;
  2311. include(pd.procoptions,po_hascallingconvention);
  2312. end;
  2313. procedure pd_hardfloat(pd:tabstractprocdef);
  2314. begin
  2315. if
  2316. {$if defined(arm)}
  2317. (current_settings.fputype=fpu_soft) or
  2318. {$endif defined(arm)}
  2319. (cs_fp_emulation in current_settings.moduleswitches) then
  2320. message(parser_e_cannot_use_hardfloat_in_a_softfloat_environment);
  2321. end;
  2322. procedure pd_section(pd:tabstractprocdef);
  2323. begin
  2324. if pd.typ<>procdef then
  2325. internalerror(2021032801);
  2326. if not (target_info.system in systems_allow_section) then
  2327. Message(parser_e_section_directive_not_allowed_for_target);
  2328. {$ifdef symansistr}
  2329. tprocdef(pd).section:=get_stringconst;
  2330. {$else symansistr}
  2331. tprocdef(pd).section:=stringdup(get_stringconst);
  2332. {$endif}
  2333. end;
  2334. type
  2335. pd_handler=procedure(pd:tabstractprocdef);
  2336. proc_dir_rec=record
  2337. idtok : ttoken;
  2338. pd_flags : tpdflags;
  2339. handler : pd_handler;
  2340. pocall : tproccalloption;
  2341. pooption : tprocoptions;
  2342. mutexclpocall : tproccalloptions;
  2343. mutexclpotype : tproctypeoptions;
  2344. mutexclpo : tprocoptions;
  2345. end;
  2346. const
  2347. {Should contain the number of procedure directives we support.}
  2348. num_proc_directives=55;
  2349. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  2350. (
  2351. (
  2352. idtok:_ABSTRACT;
  2353. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2354. handler : @pd_abstract;
  2355. pocall : pocall_none;
  2356. pooption : [po_abstractmethod];
  2357. mutexclpocall : [pocall_internproc];
  2358. mutexclpotype : [];
  2359. mutexclpo : [po_exports,po_interrupt,po_inline]
  2360. ),(
  2361. idtok:_ALIAS;
  2362. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2363. handler : @pd_alias;
  2364. pocall : pocall_none;
  2365. pooption : [];
  2366. mutexclpocall : [];
  2367. mutexclpotype : [];
  2368. mutexclpo : [po_external,po_inline]
  2369. ),(
  2370. idtok:_ASMNAME;
  2371. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  2372. handler : @pd_asmname;
  2373. pocall : pocall_cdecl;
  2374. pooption : [po_external];
  2375. mutexclpocall : [pocall_internproc];
  2376. mutexclpotype : [];
  2377. mutexclpo : [po_external,po_inline]
  2378. ),(
  2379. idtok:_ASSEMBLER;
  2380. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2381. handler : nil;
  2382. pocall : pocall_none;
  2383. pooption : [po_assembler];
  2384. mutexclpocall : [];
  2385. mutexclpotype : [];
  2386. mutexclpo : [po_external]
  2387. ),(
  2388. idtok:_C; {same as cdecl for mode mac}
  2389. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2390. handler : nil;
  2391. pocall : pocall_cdecl;
  2392. pooption : [];
  2393. mutexclpocall : [];
  2394. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2395. mutexclpo : [po_assembler,po_external]
  2396. ),(
  2397. idtok:_CBLOCK;
  2398. pd_flags : [pd_procvar];
  2399. handler : nil;
  2400. pocall : pocall_none;
  2401. pooption : [po_is_block];
  2402. mutexclpocall : [];
  2403. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2404. mutexclpo : [po_assembler,po_external]
  2405. ),(
  2406. idtok:_CDECL;
  2407. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2408. handler : nil;
  2409. pocall : pocall_cdecl;
  2410. pooption : [];
  2411. mutexclpocall : [];
  2412. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2413. mutexclpo : [po_assembler,po_external]
  2414. ),(
  2415. idtok:_DISPID;
  2416. pd_flags : [pd_dispinterface];
  2417. handler : @pd_dispid;
  2418. pocall : pocall_none;
  2419. pooption : [po_dispid];
  2420. mutexclpocall : [pocall_internproc];
  2421. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2422. mutexclpo : [po_interrupt,po_external,po_inline]
  2423. ),(
  2424. idtok:_DYNAMIC;
  2425. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2426. handler : @pd_virtual;
  2427. pocall : pocall_none;
  2428. pooption : [po_virtualmethod];
  2429. mutexclpocall : [pocall_internproc];
  2430. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2431. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  2432. ),(
  2433. idtok:_EXPORT;
  2434. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  2435. handler : @pd_export;
  2436. pocall : pocall_none;
  2437. pooption : [po_exports,po_global];
  2438. mutexclpocall : [pocall_internproc];
  2439. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2440. mutexclpo : [po_external,po_interrupt,po_inline]
  2441. ),(
  2442. idtok:_EXTERNAL;
  2443. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  2444. handler : @pd_external;
  2445. pocall : pocall_none;
  2446. pooption : [po_external];
  2447. mutexclpocall : [pocall_syscall];
  2448. { allowed for external cpp classes }
  2449. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2450. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2451. ),(
  2452. idtok:_FAR;
  2453. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2454. handler : @pd_far;
  2455. pocall : pocall_none;
  2456. pooption : [];
  2457. mutexclpocall : [pocall_internproc];
  2458. mutexclpotype : [];
  2459. mutexclpo : [po_inline]
  2460. ),(
  2461. idtok:_FAR16;
  2462. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  2463. handler : nil;
  2464. pocall : pocall_far16;
  2465. pooption : [];
  2466. mutexclpocall : [];
  2467. mutexclpotype : [];
  2468. mutexclpo : [po_external]
  2469. ),(
  2470. idtok:_FINAL;
  2471. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2472. handler : @pd_final;
  2473. pocall : pocall_none;
  2474. pooption : [po_finalmethod];
  2475. mutexclpocall : [pocall_internproc];
  2476. mutexclpotype : [];
  2477. mutexclpo : [po_exports,po_interrupt,po_inline]
  2478. ),(
  2479. idtok:_FORWARD;
  2480. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2481. handler : @pd_forward;
  2482. pocall : pocall_none;
  2483. pooption : [];
  2484. mutexclpocall : [pocall_internproc];
  2485. mutexclpotype : [];
  2486. mutexclpo : [po_external,po_inline]
  2487. ),(
  2488. idtok:_OLDFPCCALL;
  2489. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2490. handler : nil;
  2491. pocall : pocall_oldfpccall;
  2492. pooption : [];
  2493. mutexclpocall : [];
  2494. mutexclpotype : [];
  2495. mutexclpo : []
  2496. ),(
  2497. idtok:_INLINE;
  2498. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2499. handler : nil;
  2500. pocall : pocall_none;
  2501. pooption : [po_inline];
  2502. mutexclpocall : [pocall_safecall];
  2503. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2504. mutexclpo : [po_noinline,po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck]
  2505. ),(
  2506. idtok:_NOINLINE;
  2507. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2508. handler : nil;
  2509. pocall : pocall_none;
  2510. pooption : [po_noinline];
  2511. mutexclpocall : [];
  2512. mutexclpotype : [];
  2513. mutexclpo : [po_inline,po_external]
  2514. ),(
  2515. idtok:_INTERNCONST;
  2516. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2517. handler : @pd_internconst;
  2518. pocall : pocall_none;
  2519. pooption : [po_internconst];
  2520. mutexclpocall : [];
  2521. mutexclpotype : [potype_operator];
  2522. mutexclpo : []
  2523. ),(
  2524. idtok:_INTERNPROC;
  2525. pd_flags : [pd_interface,pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2526. handler : @pd_internproc;
  2527. pocall : pocall_internproc;
  2528. pooption : [];
  2529. mutexclpocall : [];
  2530. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2531. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2532. ),(
  2533. idtok:_INTERRUPT;
  2534. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2535. handler : @pd_interrupt;
  2536. {$ifdef i386}
  2537. pocall : pocall_oldfpccall;
  2538. {$else i386}
  2539. pocall : pocall_stdcall;
  2540. {$endif i386}
  2541. pooption : [po_interrupt];
  2542. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,pocall_mwpascal,
  2543. pocall_pascal,pocall_far16,pocall_oldfpccall,pocall_sysv_abi_cdecl,pocall_ms_abi_cdecl];
  2544. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2545. mutexclpo : [po_external,po_inline,po_exports]
  2546. ),(
  2547. idtok:_IOCHECK;
  2548. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2549. handler : nil;
  2550. pocall : pocall_none;
  2551. pooption : [po_iocheck];
  2552. mutexclpocall : [pocall_internproc];
  2553. mutexclpotype : [];
  2554. mutexclpo : [po_external]
  2555. ),(
  2556. idtok:_LOCAL;
  2557. pd_flags : [pd_implemen,pd_body];
  2558. handler : nil;
  2559. pocall : pocall_none;
  2560. pooption : [po_kylixlocal];
  2561. mutexclpocall : [pocall_internproc,pocall_far16];
  2562. mutexclpotype : [];
  2563. mutexclpo : [po_external,po_exports]
  2564. ),(
  2565. idtok:_MESSAGE;
  2566. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2567. handler : @pd_message;
  2568. pocall : pocall_none;
  2569. pooption : []; { can be po_msgstr or po_msgint }
  2570. mutexclpocall : [pocall_internproc];
  2571. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2572. mutexclpo : [po_interrupt,po_inline]
  2573. ),(
  2574. idtok:_MWPASCAL;
  2575. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2576. handler : nil;
  2577. pocall : pocall_mwpascal;
  2578. pooption : [];
  2579. mutexclpocall : [];
  2580. mutexclpotype : [];
  2581. mutexclpo : []
  2582. ),(
  2583. idtok:_NEAR;
  2584. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2585. handler : @pd_near;
  2586. pocall : pocall_none;
  2587. pooption : [];
  2588. mutexclpocall : [pocall_internproc];
  2589. mutexclpotype : [];
  2590. mutexclpo : []
  2591. ),(
  2592. idtok:_NORETURN;
  2593. pd_flags : [pd_implemen,pd_interface,pd_body,pd_notobjintf];
  2594. handler : nil;
  2595. pocall : pocall_none;
  2596. pooption : [po_noreturn];
  2597. mutexclpocall : [];
  2598. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2599. mutexclpo : [po_interrupt,po_virtualmethod,po_iocheck]
  2600. ),(
  2601. idtok:_NOSTACKFRAME;
  2602. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2603. handler : nil;
  2604. pocall : pocall_none;
  2605. pooption : [po_nostackframe];
  2606. mutexclpocall : [pocall_internproc];
  2607. mutexclpotype : [];
  2608. mutexclpo : []
  2609. ),(
  2610. idtok:_OVERLOAD;
  2611. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava,pd_objcclass,pd_objcprot];
  2612. handler : @pd_overload;
  2613. pocall : pocall_none;
  2614. pooption : [po_overload];
  2615. mutexclpocall : [pocall_internproc];
  2616. mutexclpotype : [];
  2617. mutexclpo : []
  2618. ),(
  2619. idtok:_OVERRIDE;
  2620. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2621. handler : @pd_override;
  2622. pocall : pocall_none;
  2623. pooption : [po_overridingmethod,po_virtualmethod];
  2624. mutexclpocall : [pocall_internproc];
  2625. mutexclpotype : [];
  2626. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2627. ),(
  2628. idtok:_PASCAL;
  2629. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2630. handler : nil;
  2631. pocall : pocall_pascal;
  2632. pooption : [];
  2633. mutexclpocall : [];
  2634. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2635. mutexclpo : [po_external]
  2636. ),(
  2637. idtok:_PUBLIC;
  2638. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2639. handler : @pd_public;
  2640. pocall : pocall_none;
  2641. pooption : [po_public,po_global];
  2642. mutexclpocall : [pocall_internproc];
  2643. mutexclpotype : [];
  2644. mutexclpo : [po_external,po_inline]
  2645. ),(
  2646. idtok:_REGISTER;
  2647. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2648. handler : nil;
  2649. pocall : pocall_register;
  2650. pooption : [];
  2651. mutexclpocall : [];
  2652. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2653. mutexclpo : [po_external]
  2654. ),(
  2655. idtok:_REINTRODUCE;
  2656. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2657. handler : @pd_reintroduce;
  2658. pocall : pocall_none;
  2659. pooption : [po_reintroduce];
  2660. mutexclpocall : [pocall_internproc];
  2661. mutexclpotype : [];
  2662. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2663. ),(
  2664. idtok:_SAFECALL;
  2665. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2666. handler : nil;
  2667. pocall : pocall_safecall;
  2668. pooption : [];
  2669. mutexclpocall : [];
  2670. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2671. mutexclpo : [po_external]
  2672. ),(
  2673. idtok:_SECTION;
  2674. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2675. handler : @pd_section;
  2676. pocall : pocall_none;
  2677. pooption : [po_public,po_global];
  2678. mutexclpocall : [pocall_internproc];
  2679. mutexclpotype : [];
  2680. mutexclpo : [po_external,po_inline,po_interrupt]
  2681. ),(
  2682. idtok:_SOFTFLOAT;
  2683. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2684. handler : nil;
  2685. pocall : pocall_softfloat;
  2686. pooption : [];
  2687. mutexclpocall : [];
  2688. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2689. { it's available with po_external because the libgcc floating point routines on the arm
  2690. uses this calling convention }
  2691. mutexclpo : []
  2692. ),(
  2693. idtok:_STATIC;
  2694. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2695. handler : @pd_static;
  2696. pocall : pocall_none;
  2697. pooption : [po_staticmethod];
  2698. mutexclpocall : [pocall_internproc];
  2699. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2700. mutexclpo : [po_interrupt,po_exports,po_virtualmethod]
  2701. ),(
  2702. idtok:_STDCALL;
  2703. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2704. handler : nil;
  2705. pocall : pocall_stdcall;
  2706. pooption : [];
  2707. mutexclpocall : [];
  2708. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2709. mutexclpo : [po_external]
  2710. ),(
  2711. idtok:_SYSCALL;
  2712. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2713. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2714. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2715. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2716. handler : @pd_syscall;
  2717. pocall : pocall_syscall;
  2718. pooption : [];
  2719. mutexclpocall : [];
  2720. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2721. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2722. ),(
  2723. idtok:_VIRTUAL;
  2724. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2725. handler : @pd_virtual;
  2726. pocall : pocall_none;
  2727. pooption : [po_virtualmethod];
  2728. mutexclpocall : [pocall_internproc];
  2729. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2730. mutexclpo : PD_VIRTUAL_MUTEXCLPO
  2731. ),(
  2732. idtok:_CPPDECL;
  2733. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2734. handler : nil;
  2735. pocall : pocall_cppdecl;
  2736. pooption : [];
  2737. mutexclpocall : [];
  2738. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2739. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2740. ),(
  2741. idtok:_VARARGS;
  2742. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2743. handler : nil;
  2744. pocall : pocall_none;
  2745. pooption : [po_varargs];
  2746. mutexclpocall : [pocall_internproc,pocall_register,
  2747. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2748. mutexclpotype : [];
  2749. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2750. ),(
  2751. idtok:_COMPILERPROC;
  2752. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2753. handler : @pd_compilerproc;
  2754. pocall : pocall_none;
  2755. pooption : [po_compilerproc];
  2756. mutexclpocall : [];
  2757. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2758. mutexclpo : [po_interrupt]
  2759. ),(
  2760. idtok:_WEAKEXTERNAL;
  2761. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2762. handler : @pd_weakexternal;
  2763. pocall : pocall_none;
  2764. { mark it both external and weak external, so we don't have to
  2765. adapt all code for external symbols to also check for weak external
  2766. }
  2767. pooption : [po_external,po_weakexternal];
  2768. mutexclpocall : [pocall_internproc,pocall_syscall];
  2769. { allowed for external cpp classes }
  2770. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2771. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2772. ),(
  2773. idtok:_WINAPI;
  2774. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2775. handler : @pd_winapi;
  2776. pocall : pocall_none;
  2777. pooption : [];
  2778. mutexclpocall : [pocall_stdcall,pocall_cdecl,pocall_mwpascal,pocall_sysv_abi_cdecl,pocall_ms_abi_cdecl];
  2779. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2780. mutexclpo : [po_external]
  2781. ),(
  2782. idtok:_ENUMERATOR;
  2783. pd_flags : [pd_interface,pd_object,pd_record];
  2784. handler : @pd_enumerator;
  2785. pocall : pocall_none;
  2786. pooption : [];
  2787. mutexclpocall : [pocall_internproc];
  2788. mutexclpotype : [];
  2789. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2790. ),(
  2791. idtok:_RTLPROC;
  2792. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2793. handler : nil;
  2794. pocall : pocall_none;
  2795. pooption : [po_rtlproc];
  2796. mutexclpocall : [];
  2797. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2798. mutexclpo : [po_interrupt]
  2799. ),(
  2800. idtok:_HARDFLOAT;
  2801. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2802. handler : @pd_hardfloat;
  2803. pocall : pocall_hardfloat;
  2804. pooption : [];
  2805. mutexclpocall : [];
  2806. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2807. { it's available with po_external because the libgcc floating point routines on the arm
  2808. uses this calling convention }
  2809. mutexclpo : []
  2810. ),(
  2811. idtok:_SYSV_ABI_DEFAULT;
  2812. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2813. handler : nil;
  2814. pocall : pocall_sysv_abi_default;
  2815. pooption : [];
  2816. mutexclpocall : [];
  2817. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2818. mutexclpo : [po_interrupt]
  2819. ),(
  2820. idtok:_SYSV_ABI_CDECL;
  2821. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2822. handler : nil;
  2823. pocall : pocall_sysv_abi_cdecl;
  2824. pooption : [];
  2825. mutexclpocall : [];
  2826. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2827. mutexclpo : [po_interrupt]
  2828. ),(
  2829. idtok:_MS_ABI_DEFAULT;
  2830. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2831. handler : nil;
  2832. pocall : pocall_ms_abi_default;
  2833. pooption : [];
  2834. mutexclpocall : [];
  2835. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2836. mutexclpo : [po_interrupt]
  2837. ),(
  2838. idtok:_MS_ABI_CDECL;
  2839. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2840. handler : nil;
  2841. pocall : pocall_ms_abi_cdecl;
  2842. pooption : [];
  2843. mutexclpocall : [];
  2844. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2845. mutexclpo : [po_interrupt]
  2846. ),(
  2847. idtok:_VECTORCALL;
  2848. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2849. handler : nil;
  2850. pocall : pocall_vectorcall;
  2851. pooption : [];
  2852. mutexclpocall : [];
  2853. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2854. mutexclpo : [po_interrupt]
  2855. ),(
  2856. idtok:_WASMFUNCREF;
  2857. pd_flags : [pd_procvar];
  2858. handler : nil;
  2859. pocall : pocall_none;
  2860. pooption : [po_wasm_funcref];
  2861. mutexclpocall : [];
  2862. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2863. mutexclpo : [po_interrupt]
  2864. )
  2865. );
  2866. function check_proc_directive(isprocvar:boolean):boolean;
  2867. var
  2868. i : longint;
  2869. begin
  2870. result:=false;
  2871. for i:=1 to num_proc_directives do
  2872. if proc_direcdata[i].idtok=idtoken then
  2873. begin
  2874. if ((not isprocvar) or
  2875. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2876. { don't eat a public directive in classes }
  2877. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2878. result:=true;
  2879. exit;
  2880. end;
  2881. end;
  2882. function find_proc_directive_index(tok: ttoken): longint; inline;
  2883. begin
  2884. result:=-1;
  2885. for result:=1 to num_proc_directives do
  2886. if proc_direcdata[result].idtok=tok then
  2887. exit;
  2888. result:=-1;
  2889. end;
  2890. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2891. {
  2892. Parse the procedure directive, returns true if a correct directive is found
  2893. }
  2894. var
  2895. p : longint;
  2896. name : TIDString;
  2897. po_comp : tprocoptions;
  2898. tokenloc : TFilePosInfo;
  2899. begin
  2900. parse_proc_direc:=false;
  2901. name:=tokeninfo^[idtoken].str;
  2902. { Hint directive? Then exit immediatly }
  2903. if (m_hintdirective in current_settings.modeswitches) then
  2904. begin
  2905. case idtoken of
  2906. _LIBRARY,
  2907. _PLATFORM,
  2908. _UNIMPLEMENTED,
  2909. _EXPERIMENTAL,
  2910. _DEPRECATED :
  2911. if (m_delphi in current_settings.modeswitches) and (pd.typ=procdef) then
  2912. begin
  2913. maybe_parse_hint_directives(tprocdef(pd));
  2914. { could the new token still be a directive? }
  2915. if token<>_ID then
  2916. exit;
  2917. end
  2918. else
  2919. exit;
  2920. else
  2921. ;
  2922. end;
  2923. end;
  2924. { C directive is MacPas only, because it breaks too much existing code
  2925. on other platforms (PFV) }
  2926. if (idtoken=_C) and
  2927. not(m_mac in current_settings.modeswitches) then
  2928. exit;
  2929. { retrieve data for directive if found }
  2930. p:=find_proc_directive_index(idtoken);
  2931. { Check if the procedure directive is known }
  2932. if p=-1 then
  2933. begin
  2934. { parsing a procvar type the name can be any
  2935. next variable !! }
  2936. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2937. not(idtoken in [_PROPERTY,_GENERIC]) then
  2938. Message1(parser_w_unknown_proc_directive_ignored,pattern);
  2939. exit;
  2940. end;
  2941. { check if method and directive not for object, like public.
  2942. This needs to be checked also for procvars }
  2943. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2944. (symtablestack.top.symtabletype=ObjectSymtable) and
  2945. { directive allowed for cpp classes? }
  2946. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2947. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2948. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2949. exit;
  2950. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2951. (symtablestack.top.symtabletype=recordsymtable) then
  2952. exit;
  2953. { check if method and directive not for java class }
  2954. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2955. is_javaclass(tdef(symtablestack.top.defowner)) then
  2956. exit;
  2957. { check if method and directive not for java interface }
  2958. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2959. is_javainterface(tdef(symtablestack.top.defowner)) then
  2960. exit;
  2961. { Keep track of the token's position in the file so it's correctly indicated if an error occurs. }
  2962. tokenloc := current_tokenpos;
  2963. { consume directive, and turn flag on }
  2964. consume(token);
  2965. parse_proc_direc:=true;
  2966. { Conflicts between directives? }
  2967. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) then
  2968. begin
  2969. MessagePos2(tokenloc, parser_e_proc_dir_conflict,name,ProcTypeOptionKeywords[pd.proctypeoption]);
  2970. exit;
  2971. end;
  2972. if (pd.proccalloption in proc_direcdata[p].mutexclpocall) then
  2973. begin
  2974. MessagePos2(tokenloc, parser_e_proc_dir_conflict,name,'"' + UpCase(proccalloptionStr[pd.proccalloption]) + '"');
  2975. exit;
  2976. end;
  2977. po_comp := (pd.procoptions*proc_direcdata[p].mutexclpo);
  2978. if (po_comp<>[]) then
  2979. begin
  2980. MessagePos2(tokenloc, parser_e_proc_dir_conflict,name,get_first_proc_str(po_comp));
  2981. exit;
  2982. end;
  2983. { set calling convention }
  2984. if proc_direcdata[p].pocall<>pocall_none then
  2985. begin
  2986. if (po_hascallingconvention in pd.procoptions) then
  2987. begin
  2988. MessagePos2(tokenloc, parser_w_proc_overriding_calling,
  2989. proccalloptionStr[pd.proccalloption],
  2990. proccalloptionStr[proc_direcdata[p].pocall]);
  2991. end;
  2992. { check if the target processor supports this calling convention }
  2993. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2994. begin
  2995. MessagePos1(tokenloc, parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2996. { recover }
  2997. proc_direcdata[p].pocall:=pocall_stdcall;
  2998. end;
  2999. pd.proccalloption:=proc_direcdata[p].pocall;
  3000. include(pd.procoptions,po_hascallingconvention);
  3001. end;
  3002. if pd.typ=procdef then
  3003. begin
  3004. { Check if the directive is only for objects }
  3005. if (pd_object in proc_direcdata[p].pd_flags) and
  3006. not assigned(tprocdef(pd).struct) then
  3007. exit;
  3008. { Check if the directive is only for records }
  3009. if (pd_record in proc_direcdata[p].pd_flags) and
  3010. not assigned(tprocdef(pd).struct) then
  3011. exit;
  3012. { check if method and directive not for interface }
  3013. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  3014. is_interface(tprocdef(pd).struct) then
  3015. exit;
  3016. { check if method and directive not for interface }
  3017. if is_dispinterface(tprocdef(pd).struct) and
  3018. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  3019. exit;
  3020. { check if method and directive not for objcclass }
  3021. if is_objcclass(tprocdef(pd).struct) and
  3022. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  3023. exit;
  3024. { check if method and directive not for objcprotocol }
  3025. if is_objcprotocol(tprocdef(pd).struct) and
  3026. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  3027. exit;
  3028. { check if method and directive not for record/class helper }
  3029. if is_objectpascal_helper(tprocdef(pd).struct) and
  3030. (pd_nothelper in proc_direcdata[p].pd_flags) then
  3031. exit;
  3032. end;
  3033. { Check the pd_flags if the directive should be allowed }
  3034. if (pd_interface in pdflags) and
  3035. not(pd_interface in proc_direcdata[p].pd_flags) then
  3036. begin
  3037. MessagePos1(tokenloc, parser_e_proc_dir_not_allowed_in_interface,name);
  3038. exit;
  3039. end;
  3040. if (pd_implemen in pdflags) and
  3041. not(pd_implemen in proc_direcdata[p].pd_flags) then
  3042. begin
  3043. MessagePos1(tokenloc, parser_e_proc_dir_not_allowed_in_implementation,name);
  3044. exit;
  3045. end;
  3046. if (pd_procvar in pdflags) and
  3047. not(pd_procvar in proc_direcdata[p].pd_flags) then
  3048. begin
  3049. MessagePos1(tokenloc, parser_e_proc_dir_not_allowed_in_procvar,name);
  3050. exit;
  3051. end;
  3052. { Return the new pd_flags }
  3053. if not(pd_body in proc_direcdata[p].pd_flags) then
  3054. exclude(pdflags,pd_body);
  3055. { Add the correct flag }
  3056. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  3057. { Call the handler }
  3058. if pointer(proc_direcdata[p].handler)<>nil then
  3059. proc_direcdata[p].handler(pd);
  3060. end;
  3061. function proc_get_importname(pd:tprocdef):string;
  3062. var
  3063. dllname, importname : string;
  3064. begin
  3065. result:='';
  3066. if not(po_external in pd.procoptions) then
  3067. internalerror(200412151);
  3068. { external name or number is specified }
  3069. if assigned(pd.import_name) or (pd.import_nr<>0) then
  3070. begin
  3071. if assigned(pd.import_dll) then
  3072. dllname:=pd.import_dll^
  3073. else
  3074. dllname:='';
  3075. if assigned(pd.import_name) then
  3076. importname:=pd.import_name^
  3077. else
  3078. importname:='';
  3079. proc_get_importname:=make_dllmangledname(dllname,
  3080. importname,pd.import_nr,pd.proccalloption);
  3081. end
  3082. else
  3083. begin
  3084. { Default names when importing variables }
  3085. case pd.proccalloption of
  3086. pocall_cdecl,
  3087. pocall_sysv_abi_cdecl,
  3088. pocall_ms_abi_cdecl:
  3089. begin
  3090. if assigned(pd.struct) then
  3091. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  3092. else
  3093. result:=target_info.Cprefix+pd.procsym.realname;
  3094. end;
  3095. pocall_cppdecl :
  3096. begin
  3097. result:=target_info.Cprefix+pd.cplusplusmangledname;
  3098. end;
  3099. else
  3100. begin
  3101. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  3102. { but according to MacPas mode description
  3103. Cprefix should still be used PM }
  3104. if (m_mac in current_settings.modeswitches) then
  3105. result:=target_info.Cprefix+tprocdef(pd).procsym.realname
  3106. else
  3107. result:=pd.procsym.realname;
  3108. {$ifdef i8086}
  3109. { Turbo Pascal expects names of external routines
  3110. to be all uppercase }
  3111. if (target_info.system=system_i8086_msdos) and
  3112. (m_tp7 in current_settings.modeswitches) and
  3113. (pd.proccalloption=pocall_pascal) then
  3114. result:=UpCase(result);
  3115. {$endif i8086}
  3116. end;
  3117. end;
  3118. end;
  3119. end;
  3120. procedure proc_set_mangledname(pd:tprocdef);
  3121. var
  3122. s : string;
  3123. begin
  3124. { When the mangledname is already set we aren't allowed to change
  3125. it because it can already be used somewhere (PFV) }
  3126. if not(po_has_mangledname in pd.procoptions) then
  3127. begin
  3128. if (po_external in pd.procoptions) and not (po_wasm_suspending in pd.procoptions) then
  3129. begin
  3130. { External Procedures are only allowed to change the mangledname
  3131. in their first declaration }
  3132. if (pd.forwarddef or (not pd.hasforward)) then
  3133. begin
  3134. s:=proc_get_importname(pd);
  3135. if s<>'' then
  3136. begin
  3137. pd.setmangledname(s);
  3138. end;
  3139. { since this is an external declaration, there won't be an
  3140. implementation that needs to match the original symbol
  3141. again -> immediately convert here }
  3142. if po_compilerproc in pd.procoptions then
  3143. pd.setcompilerprocname;
  3144. end
  3145. end
  3146. else
  3147. { Normal procedures }
  3148. begin
  3149. if (po_compilerproc in pd.procoptions) then
  3150. begin
  3151. pd.setmangledname(lower(pd.procsym.name));
  3152. end;
  3153. end;
  3154. end;
  3155. { Public/exported alias names }
  3156. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  3157. not(po_has_public_name in pd.procoptions) then
  3158. begin
  3159. case pd.proccalloption of
  3160. pocall_cdecl,
  3161. pocall_sysv_abi_cdecl,
  3162. pocall_ms_abi_cdecl:
  3163. begin
  3164. if assigned(pd.struct) then
  3165. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  3166. else
  3167. begin
  3168. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  3169. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  3170. (po_exports in pd.procoptions) then
  3171. pd.aliasnames.insert(pd.procsym.realname)
  3172. else
  3173. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  3174. end;
  3175. end;
  3176. pocall_cppdecl :
  3177. begin
  3178. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  3179. end;
  3180. else
  3181. ;
  3182. end;
  3183. { prevent adding the alias a second time }
  3184. include(pd.procoptions,po_has_public_name);
  3185. end;
  3186. end;
  3187. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  3188. {
  3189. Parse the procedure directives. It does not matter if procedure directives
  3190. are written using ;procdir; or ['procdir'] syntax.
  3191. }
  3192. var
  3193. stoprecording,
  3194. res : boolean;
  3195. begin
  3196. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  3197. begin
  3198. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  3199. include(pd.procoptions,po_public);
  3200. include(pd.procoptions,po_has_public_name);
  3201. include(pd.procoptions,po_global);
  3202. end;
  3203. { methods from external class definitions are all external themselves }
  3204. if (pd.typ=procdef) and
  3205. assigned(tprocdef(pd).struct) and
  3206. (tprocdef(pd).struct.typ=objectdef) and
  3207. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  3208. tprocdef(pd).make_external;
  3209. { Class constructors and destructor are static class methods in real. }
  3210. { There are many places in the compiler where either class or static }
  3211. { method flag changes the behavior. It is simplier to add them to }
  3212. { the class constructors/destructors options than to fix all the }
  3213. { occurencies. (Paul) }
  3214. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  3215. begin
  3216. include(pd.procoptions,po_classmethod);
  3217. include(pd.procoptions,po_staticmethod);
  3218. end;
  3219. { for a generic routine we also need to record the procedure }
  3220. { directives, but only if we aren't already recording for a }
  3221. { surrounding generic }
  3222. if pd.is_generic and (pd.typ=procdef) and not current_scanner.is_recording_tokens then
  3223. begin
  3224. current_scanner.startrecordtokens(tprocdef(pd).genericdecltokenbuf);
  3225. stoprecording:=true;
  3226. end
  3227. else
  3228. stoprecording:=false;
  3229. while (token=_ID) or
  3230. (
  3231. not (m_prefixed_attributes in current_settings.modeswitches) and
  3232. (token=_LECKKLAMMER)
  3233. ) do
  3234. begin
  3235. if not (m_prefixed_attributes in current_settings.modeswitches) and
  3236. try_to_consume(_LECKKLAMMER) then
  3237. begin
  3238. repeat
  3239. parse_proc_direc(pd,pdflags);
  3240. until not try_to_consume(_COMMA);
  3241. consume(_RECKKLAMMER);
  3242. { we always expect at least '[];' }
  3243. res:=true;
  3244. end
  3245. else
  3246. begin
  3247. res:=parse_proc_direc(pd,pdflags);
  3248. end;
  3249. { A procedure directive normally followed by a semicolon, but in
  3250. a const section or reading a type we should stop when _EQ is found,
  3251. because a constant/default value follows }
  3252. if res then
  3253. begin
  3254. if (block_type=bt_const_type) and
  3255. (token=_EQ) then
  3256. break;
  3257. { support procedure proc;stdcall export; }
  3258. if not(check_proc_directive((pd.typ=procvardef))) then
  3259. begin
  3260. { support "record p : procedure stdcall end;" and
  3261. "var p : procedure stdcall = nil;" }
  3262. if (
  3263. (pd_procvar in pdflags) and
  3264. (token in [_END,_RKLAMMER,_EQ])
  3265. ) or (
  3266. (po_anonymous in pd.procoptions) and
  3267. (token in [_BEGIN,_VAR,_CONST,_TYPE,_LABEL,_FUNCTION,_PROCEDURE,_OPERATOR])
  3268. ) then
  3269. break
  3270. else
  3271. begin
  3272. if (token=_COLON) then
  3273. begin
  3274. Message(parser_e_field_not_allowed_here);
  3275. consume_all_until(_SEMICOLON);
  3276. end;
  3277. consume(_SEMICOLON)
  3278. end;
  3279. end;
  3280. end
  3281. else
  3282. break;
  3283. end;
  3284. if stoprecording then
  3285. current_scanner.stoprecordtokens;
  3286. { nostackframe requires assembler, but assembler
  3287. may be specified in the implementation part only,
  3288. and in not required if the function is first forward declared
  3289. if it is a procdef that has forwardef set to true
  3290. we postpone the possible error message to the real implementation
  3291. parse_only does not need to be considered as po_nostackframe
  3292. is an implementation only directive }
  3293. if (po_nostackframe in pd.procoptions) and
  3294. not (po_assembler in pd.procoptions) and
  3295. ((pd.typ<>procdef) or not tprocdef(pd).forwarddef) then
  3296. message(parser_e_nostackframe_without_assembler);
  3297. end;
  3298. procedure parse_proctype_directives(pd_or_invkdef:tdef);
  3299. var
  3300. pdflags : tpdflags;
  3301. pd : tabstractprocdef;
  3302. begin
  3303. if is_funcref(pd_or_invkdef) then
  3304. pd:=get_invoke_procdef(tobjectdef(pd_or_invkdef))
  3305. else if pd_or_invkdef.typ=procvardef then
  3306. pd:=tprocvardef(pd_or_invkdef)
  3307. else
  3308. internalerror(2022012501);
  3309. pdflags:=[pd_procvar];
  3310. parse_proc_directives(pd,pdflags);
  3311. end;
  3312. procedure parse_object_proc_directives(pd:tprocdef);
  3313. var
  3314. pdflags : tpdflags;
  3315. begin
  3316. pdflags:=[pd_object];
  3317. parse_proc_directives(pd,pdflags);
  3318. end;
  3319. procedure parse_record_proc_directives(pd:tprocdef);
  3320. var
  3321. pdflags : tpdflags;
  3322. begin
  3323. pdflags:=[pd_record];
  3324. parse_proc_directives(pd,pdflags);
  3325. end;
  3326. end.