| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // Examine.cc -- Helpers for examining AST code.
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Andrew Beach
 | 
|---|
| 10 | // Created On       : Wed Sept 2 14:02 2020
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Fri Dec 10 10:27 2021
 | 
|---|
| 13 | // Update Count     : 1
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "Common/Examine.h"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "AST/Type.hpp"
 | 
|---|
| 19 | #include "CodeGen/OperatorTable.h"
 | 
|---|
| 20 | #include "InitTweak/InitTweak.h"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | namespace {
 | 
|---|
| 23 | 
 | 
|---|
| 24 | // getTypeofThis but does some extra checks used in this module.
 | 
|---|
| 25 | const ast::Type * getTypeofThisSolo( const ast::FunctionDecl * func ) {
 | 
|---|
| 26 |         if ( 1 != func->params.size() ) {
 | 
|---|
| 27 |                 return nullptr;
 | 
|---|
| 28 |         }
 | 
|---|
| 29 |         auto ref = func->type->params.front().as<ast::ReferenceType>();
 | 
|---|
| 30 |         return (ref) ? ref->base : nullptr;
 | 
|---|
| 31 | }
 | 
|---|
| 32 | 
 | 
|---|
| 33 | }
 | 
|---|
| 34 | 
 | 
|---|
| 35 | const ast::DeclWithType * isMainFor(
 | 
|---|
| 36 |                 const ast::FunctionDecl * func, ast::AggregateDecl::Aggregate kind ) {
 | 
|---|
| 37 |         if ( "main" != func->name ) return nullptr;
 | 
|---|
| 38 |         if ( 1 != func->params.size() ) return nullptr;
 | 
|---|
| 39 | 
 | 
|---|
| 40 |         auto param = func->params.front();
 | 
|---|
| 41 | 
 | 
|---|
| 42 |         auto type = dynamic_cast<const ast::ReferenceType *>( param->get_type() );
 | 
|---|
| 43 |         if ( !type ) return nullptr;
 | 
|---|
| 44 | 
 | 
|---|
| 45 |         auto obj = type->base.as<ast::StructInstType>();
 | 
|---|
| 46 |         if ( !obj ) return nullptr;
 | 
|---|
| 47 | 
 | 
|---|
| 48 |         if ( kind != obj->base->kind ) return nullptr;
 | 
|---|
| 49 | 
 | 
|---|
| 50 |         return param;
 | 
|---|
| 51 | }
 | 
|---|
| 52 | 
 | 
|---|
| 53 | namespace {
 | 
|---|
| 54 | 
 | 
|---|
| 55 | const ast::Type * getDestructorParam( const ast::FunctionDecl * func ) {
 | 
|---|
| 56 |         if ( !CodeGen::isDestructor( func->name ) ) return nullptr;
 | 
|---|
| 57 |         //return InitTweak::getParamThis( func )->type;
 | 
|---|
| 58 |         return getTypeofThisSolo( func );
 | 
|---|
| 59 | }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | bool isDestructorFor(
 | 
|---|
| 64 |                 const ast::FunctionDecl * func, const ast::StructDecl * type_decl ) {
 | 
|---|
| 65 |         if ( const ast::Type * type = getDestructorParam( func ) ) {
 | 
|---|
| 66 |                 auto stype = dynamic_cast<const ast::StructInstType *>( type );
 | 
|---|
| 67 |                 return stype && stype->base.get() == type_decl;
 | 
|---|
| 68 |         }
 | 
|---|
| 69 |         return false;
 | 
|---|
| 70 | }
 | 
|---|