pdecsub.pas 128 KB

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