pdecsub.pas 122 KB

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