pdecsub.pas 134 KB

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