pdecsub.pas 126 KB

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