pdecsub.pas 126 KB

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