pdecsub.pas 127 KB

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