pdecsub.pas 125 KB

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