pdecsub.pas 127 KB

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